-
Notifications
You must be signed in to change notification settings - Fork 1
/
for-stream-tag.js
45 lines (38 loc) · 1.11 KB
/
for-stream-tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
module.exports = function (input, out) {
const stream = input._dataProvider;
// This async fragment doesn't have timeout handling
let asyncOut = out.beginAsync({timeout: input.timeout, 'name': input});
const batchSize = input.batchSize || 1;
let buffer = [];
stream.on('data', data => {
if (batchSize > 1) {
buffer.push(data);
if (batchSize > buffer.length) {
return;
}
}
input.renderBody && input.renderBody(asyncOut,
batchSize > 1 ? buffer : data);
buffer = [];
asyncOut.flush();
});
stream.on('end', handleEnd);
stream.on('error', handleEnd);
function handleEnd(err) {
if (asyncOut) {
if (buffer.length > 0 && input.renderBody) {
input.renderBody(asyncOut,
buffer);
asyncOut.flush();
}
let out = asyncOut;
asyncOut = undefined;
if (err) {
out.error(err);
return;
}
out.end();
}
}
};