/
readable.js
357 lines (296 loc) · 9.1 KB
/
readable.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict';
module.exports = Readable;
var Stream = require('stream');
var util = require('util');
var fromList = require('./from-list.js');
var assert = require('assert');
util.inherits(Readable, Stream);
function ReadableState(options, stream) {
options = options || {};
this.bufferSize = options.bufferSize || 16 * 1024;
assert(typeof this.bufferSize === 'number');
// cast to an int
this.bufferSize = ~~this.bufferSize;
this.lowWaterMark = options.lowWaterMark || 1024;
this.buffer = [];
this.length = 0;
this.pipes = [];
this.flowing = false;
this.ended = false;
this.stream = stream;
this.reading = false;
// whenever we return null, then we set a flag to say
// that we're awaiting a 'readable' event emission.
this.needReadable = false;
}
function Readable(options) {
this._readableState = new ReadableState(options, this);
Stream.apply(this);
}
// you can override either this method, or _read(n, cb) below.
Readable.prototype.read = function(n) {
var state = this._readableState;
if (state.length === 0 && state.ended) {
process.nextTick(this.emit.bind(this, 'end'));
return null;
}
if (isNaN(n) || n <= 0)
n = state.length
// XXX: controversial.
// don't have that much. return null, unless we've ended.
// However, if the low water mark is lower than the number of bytes,
// then we still need to return what we have, or else it won't kick
// off another _read() call. For example,
// lwm=5
// len=9
// read(10)
// We don't have that many bytes, so it'd be tempting to return null,
// but then it won't ever cause _read to be called, so in that case,
// we just return what we have, and let the programmer deal with it.
if (n > state.length) {
if (!state.ended && state.length < state.lowWaterMark) {
state.needReadable = true;
n = 0;
} else
n = state.length;
}
var ret = n > 0 ? fromList(n, state.buffer, state.length) : null;
if (ret === null || ret.length === 0)
state.needReadable = true;
state.length -= n;
if (!state.ended &&
state.length < state.lowWaterMark &&
!state.reading) {
state.reading = true;
// call internal read method
this._read(state.bufferSize, function onread(er, chunk) {
state.reading = false;
if (er)
return this.emit('error', er);
if (!chunk || !chunk.length) {
state.ended = true;
// if we've ended and we have some data left, then emit
// 'readable' now to make sure it gets picked up.
if (state.length > 0)
this.emit('readable');
return;
}
state.length += chunk.length;
state.buffer.push(chunk);
if (state.length < state.lowWaterMark)
this._read(state.bufferSize, onread.bind(this));
// now we have something to call this.read() to get.
if (state.needReadable) {
state.needReadable = false;
this.emit('readable');
}
}.bind(this));
}
return ret;
};
// abstract method. to be overridden in specific implementation classes.
// call cb(er, data) where data is <= n in length.
// for virtual (non-string, non-buffer) streams, "length" is somewhat
// arbitrary, and perhaps not very meaningful.
Readable.prototype._read = function(n, cb) {
process.nextTick(cb.bind(this, new Error('not implemented')));
};
Readable.prototype.pipe = function(dest, pipeOpts) {
var src = this;
var state = this._readableState;
if (!pipeOpts)
pipeOpts = {};
state.pipes.push(dest);
if ((!pipeOpts || pipeOpts.end !== false) &&
dest !== process.stdout &&
dest !== process.stderr) {
src.once('end', onend);
dest.on('unpipe', function(readable) {
if (readable === src)
src.removeListener('end', onend);
});
}
function onend() {
dest.end();
}
dest.emit('pipe', src);
// start the flow.
if (!state.flowing)
process.nextTick(flow.bind(null, src, pipeOpts));
return dest;
};
function flow(src, pipeOpts) {
var state = src._readableState;
var chunk;
var dest;
var needDrain = 0;
function ondrain() {
needDrain--;
if (needDrain === 0)
flow(src, pipeOpts);
}
while (state.pipes.length &&
null !== (chunk = src.read(pipeOpts.chunkSize))) {
state.pipes.forEach(function(dest, i, list) {
var written = dest.write(chunk);
if (false === written) {
needDrain++;
dest.once('drain', ondrain);
}
});
src.emit('data', chunk);
// if anyone needs a drain, then we have to wait for that.
if (needDrain > 0)
return;
}
// if every destination was unpiped, either before entering this
// function, or in the while loop, then stop flowing.
//
// NB: This is a pretty rare edge case.
if (state.pipes.length === 0) {
state.flowing = false;
// if there were data event listeners added, then switch to old mode.
if (this.listeners('data').length)
emitDataEvents(this);
return;
}
// at this point, no one needed a drain, so we just ran out of data
// on the next readable event, start it over again.
src.once('readable', flow.bind(null, src, pipeOpts));
}
Readable.prototype.unpipe = function(dest) {
var state = this._readableState;
if (!dest) {
// remove all of them.
state.pipes.forEach(function(dest, i, list) {
dest.emit('unpipe', this);
}, this);
state.pipes.length = 0;
} else {
var i = state.pipes.indexOf(dest);
if (i !== -1) {
dest.emit('unpipe', this);
state.pipes.splice(i, 1);
}
}
return this;
};
// kludge for on('data', fn) consumers. Sad.
// This is *not* part of the new readable stream interface.
// It is an ugly unfortunate mess of history.
Readable.prototype.on = function(ev, fn) {
// https://github.com/isaacs/readable-stream/issues/16
// if we're already flowing, then no need to set up data events.
if (ev === 'data' && !this._readableState.flowing)
emitDataEvents(this);
return Stream.prototype.on.call(this, ev, fn);
};
Readable.prototype.addListener = Readable.prototype.on;
// pause() and resume() are remnants of the legacy readable stream API
// If the user uses them, then switch into old mode.
Readable.prototype.resume = function() {
emitDataEvents(this);
return this.resume();
};
Readable.prototype.pause = function() {
emitDataEvents(this);
return this.pause();
};
function emitDataEvents(stream) {
var state = stream._readableState;
if (state.flowing) {
// https://github.com/isaacs/readable-stream/issues/16
throw new Error('Cannot switch to old mode now.');
}
var paused = false;
var readable = false;
// convert to an old-style stream.
stream.readable = true;
stream.pipe = Stream.prototype.pipe;
stream.on = stream.addEventListener = Stream.prototype.on;
stream.on('readable', function() {
readable = true;
var c;
while (!paused && (null !== (c = stream.read())))
stream.emit('data', c);
if (c === null) {
readable = false;
stream._readableState.needReadable = true;
}
});
stream.pause = function() {
paused = true;
};
stream.resume = function() {
paused = false;
if (readable)
stream.emit('readable');
};
// now make it start, just in case it hadn't already.
process.nextTick(function() {
stream.emit('readable');
});
}
// wrap an old-style stream as the async data source.
// This is *not* part of the readable stream interface.
// It is an ugly unfortunate mess of history.
Readable.prototype.wrap = function(stream) {
var state = this._readableState;
var paused = false;
stream.on('end', function() {
state.ended = true;
if (state.length === 0)
this.emit('end');
}.bind(this));
stream.on('data', function(chunk) {
state.buffer.push(chunk);
state.length += chunk.length;
this.emit('readable');
// if not consumed, then pause the stream.
if (state.length > state.lowWaterMark && !paused) {
paused = true;
stream.pause();
}
}.bind(this));
// proxy all the other methods.
// important when wrapping filters and duplexes.
for (var i in stream) {
if (typeof stream[i] === 'function' &&
typeof this[i] === 'undefined') {
this[i] = function(method) { return function() {
return stream[method].apply(stream, arguments);
}}(i);
}
}
// proxy certain important events.
var events = ['error', 'close', 'destroy', 'pause', 'resume'];
events.forEach(function(ev) {
stream.on(ev, this.emit.bind(this, ev));
}.bind(this));
// consume some bytes. if not all is consumed, then
// pause the underlying stream.
this.read = function(n) {
if (state.length === 0) {
state.needReadable = true;
return null;
}
if (isNaN(n) || n <= 0)
n = state.length;
if (n > state.length) {
if (!state.ended) {
state.needReadable = true;
return null;
} else
n = state.length;
}
var ret = fromList(n, state.buffer, state.length);
state.length -= n;
if (state.length < state.lowWaterMark && paused) {
stream.resume();
paused = false;
}
if (state.length === 0 && ended)
process.nextTick(this.emit.bind(this, 'end'));
return ret;
};
};