UNPKG

833 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformBuffer = void 0;
4const stream_1 = require("stream");
5/**
6 * Similar to RxJS bufferCount()
7 *
8 * @default batchSize is 10
9 */
10function transformBuffer(opt) {
11 const { batchSize } = opt;
12 let buf = [];
13 return new stream_1.Transform({
14 objectMode: true,
15 ...opt,
16 transform(chunk, _encoding, cb) {
17 buf.push(chunk);
18 if (buf.length >= batchSize) {
19 cb(null, buf);
20 buf = [];
21 }
22 else {
23 cb();
24 }
25 },
26 final(cb) {
27 if (buf.length) {
28 this.push(buf);
29 buf = [];
30 }
31 cb();
32 },
33 });
34}
35exports.transformBuffer = transformBuffer;