-
Notifications
You must be signed in to change notification settings - Fork 2
/
streamz.js
196 lines (161 loc) · 4.73 KB
/
streamz.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const stream = require('stream');
const Promise = require('bluebird');
const util = require('util');
const noop = () => undefined;
function Streamz(_c, fn, options) {
if (!(this instanceof Streamz))
return new Streamz(_c, fn, options);
if (isNaN(_c)) {
options = fn;
fn = _c;
_c = undefined;
}
if (typeof fn !== 'function') {
options = fn;
fn = undefined;
}
// Legacy way to define concurrency
if (!_c && options && !isNaN(options)) {
_c = options;
options = undefined;
}
options = options || {};
this.options = options;
options.objectMode = true;
if (options.highWaterMark === undefined)
options.highWaterMark = 10;
stream.Transform.call(this,options);
this._concurrency = _c || options.concurrency || options.cap || 1;
if (fn)
this._fn = fn;
else if (options.fn)
this._fn = options.fn;
this._incomingPipes = (options.keepAlive ? 1 : 0);
this._concurrent = 0;
if (options.flush)
this._flush = options.flush;
if (typeof options.catch === 'function')
this._catch = options.catch;
this.on('error',e => {
if (this._events.error.length < 2) {
const pipes = this._readableState.pipes;
if (pipes && (pipes instanceof stream || pipes.length)) [].concat(pipes).forEach(child => child.emit('error', e));
else throw e;
}
});
this.on('pipe',p => {
if (!(p instanceof Streamz) && (!p._events.error || !p._events.error.length || p._events.error.length === 1))
p.on('error',e => this.emit('error', e));
this._incomingPipes++;
});
}
util.inherits(Streamz,stream.Transform);
Streamz.prototype.callbacks = undefined;
Streamz.prototype._flush = function(cb) { setImmediate(cb);};
Streamz.prototype.emitError = function(e,d) {
if (this._catch)
Promise.try(() => this._catch.call(this,e,d))
.catch(e => this.emit('error',e));
else
this.emit('error',e);
};
Streamz.prototype._transform = function(d, e, _cb) {
let ret;
this._concurrent+=1;
// If we haven't reached the concurrency limit, we schedule
// a callback to the transform stream at the next tick
let concurrency = this._concurrency;
if (typeof concurrency === 'function') concurrency = concurrency();
if (this._concurrent < concurrency)
setImmediate(_cb);
else
this.callbacks = (this.callbacks || []).concat(_cb);
let pop = () => {
pop = noop;
if (this.callbacks && this.callbacks.length)
this.callbacks.shift()();
};
let done = () => {
// Ensure done is only called once
done = noop;
this._concurrent--;
pop();
setImmediate( () => this._finalize());
};
// If the return value is not a promise then vanillaCb = `done`
// If a promise is returned, we switch the reference to the
// original stream callback and only execute `done` when the
// promise has been resolved
let vanillaCb = done;
try {
ret = this._fn(d, (e, d) => {
if (e)
this.emitError(e,d);
else if (d !== undefined)
this.push(d);
vanillaCb();
});
} catch(e) {
this.emitError(e,d);
vanillaCb();
}
if (ret && typeof ret.then === 'function') {
// switch reference to the original stream callback
// and only call done when the promise is resolved
vanillaCb = pop;
Promise.resolve(ret).then(d => {
if (d !== undefined)
this.push(d);
},e => {
this.emitError(e,d);
})
.finally(done);
} else {
// If we got non-promise value, we push it
if (ret !== undefined)
this.push(ret);
// If the fn was synchronous we signal we are done
if (this._fn.length < 2)
vanillaCb();
}
};
Streamz.prototype._fn = function(d) {
// The default is a simple passthrough.
this.push(d);
};
Streamz.prototype._finalize = function() {
// In node 14 we need to look at `_writableState.buffered.length` - in older versions we look at `_writableState.length`
if (this.endedCb && !this._concurrent && (this._writableState.buffered ? !this._writableState.buffered.length : !this._writableState.length)) {
let endedCb = this.endedCb;
this.endedCb = undefined;
stream.Transform.prototype.end.apply(this, undefined, endedCb);
}
}
Streamz.prototype.end = function(d,cb) {
this._incomingPipes--;
const end = () => {
if (this._incomingPipes < 1) {
this.endedCb = cb || noop;
this._finalize();
} else {
if (cb) cb();
}
}
if (d !== undefined) {
this._transform(d, null, end);
} else {
end();
}
};
Streamz.prototype.promise = function() {
const buffer = [];
const bufferStream = Streamz(d => {
buffer.push(d);
});
return new Promise((resolve,reject) => {
this.pipe(bufferStream)
.on('error', reject)
.on('finish', () => resolve(buffer));
});
};
module.exports = Streamz;