2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2017-03-24 16:46:44 +00:00
|
|
|
|
|
|
|
const common = require('../common');
|
2020-12-29 14:20:41 +00:00
|
|
|
const { Readable, Writable } = require('stream');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
function toArray(callback) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const stream = new Writable({ objectMode: true });
|
|
|
|
const list = [];
|
2013-01-12 04:59:57 +00:00
|
|
|
stream.write = function(chunk) {
|
|
|
|
list.push(chunk);
|
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
stream.end = common.mustCall(function() {
|
2013-01-12 04:59:57 +00:00
|
|
|
callback(list);
|
2017-06-15 22:03:37 +00:00
|
|
|
});
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromArray(list) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({ objectMode: true });
|
2017-05-26 21:54:51 +00:00
|
|
|
r._read = common.mustNotCall();
|
2013-01-12 04:59:57 +00:00
|
|
|
list.forEach(function(chunk) {
|
|
|
|
r.push(chunk);
|
|
|
|
});
|
|
|
|
r.push(null);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that objects can be read from the stream
|
2017-07-11 00:55:21 +00:00
|
|
|
const r = fromArray([{ one: '1' }, { two: '2' }]);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const v1 = r.read();
|
|
|
|
const v2 = r.read();
|
|
|
|
const v3 = r.read();
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(v1, { one: '1' });
|
|
|
|
assert.deepStrictEqual(v2, { two: '2' });
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(v3, null);
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that objects can be piped into the stream
|
2017-07-11 00:55:21 +00:00
|
|
|
const r = fromArray([{ one: '1' }, { two: '2' }]);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
r.pipe(toArray(common.mustCall(function(list) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [
|
2013-01-12 04:59:57 +00:00
|
|
|
{ one: '1' },
|
2021-03-26 15:51:08 +00:00
|
|
|
{ two: '2' },
|
2013-01-12 04:59:57 +00:00
|
|
|
]);
|
2017-06-15 22:03:37 +00:00
|
|
|
})));
|
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that read(n) is ignored
|
2017-07-11 00:55:21 +00:00
|
|
|
const r = fromArray([{ one: '1' }, { two: '2' }]);
|
2017-01-08 13:19:00 +00:00
|
|
|
const value = r.read(2);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(value, { one: '1' });
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that objects can be synchronously read
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({ objectMode: true });
|
2017-07-11 00:55:21 +00:00
|
|
|
const list = [{ one: '1' }, { two: '2' }];
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
r._read = function(n) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const item = list.shift();
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
r.push(item || null);
|
2013-01-12 04:59:57 +00:00
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
r.pipe(toArray(common.mustCall(function(list) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [
|
2013-01-12 04:59:57 +00:00
|
|
|
{ one: '1' },
|
2021-03-26 15:51:08 +00:00
|
|
|
{ two: '2' },
|
2013-01-12 04:59:57 +00:00
|
|
|
]);
|
2017-06-15 22:03:37 +00:00
|
|
|
})));
|
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that objects can be asynchronously read
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({ objectMode: true });
|
2017-07-11 00:55:21 +00:00
|
|
|
const list = [{ one: '1' }, { two: '2' }];
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
r._read = function(n) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const item = list.shift();
|
2013-01-12 04:59:57 +00:00
|
|
|
process.nextTick(function() {
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
r.push(item || null);
|
2013-01-12 04:59:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
r.pipe(toArray(common.mustCall(function(list) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [
|
2013-01-12 04:59:57 +00:00
|
|
|
{ one: '1' },
|
2021-03-26 15:51:08 +00:00
|
|
|
{ two: '2' },
|
2013-01-12 04:59:57 +00:00
|
|
|
]);
|
2017-06-15 22:03:37 +00:00
|
|
|
})));
|
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that strings can be read as objects
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
2017-05-26 21:54:51 +00:00
|
|
|
r._read = common.mustNotCall();
|
2017-01-08 13:19:00 +00:00
|
|
|
const list = ['one', 'two', 'three'];
|
2013-01-12 04:59:57 +00:00
|
|
|
list.forEach(function(str) {
|
|
|
|
r.push(str);
|
|
|
|
});
|
|
|
|
r.push(null);
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
r.pipe(toArray(common.mustCall(function(array) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(array, list);
|
2017-06-15 22:03:37 +00:00
|
|
|
})));
|
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify read(0) behavior for object streams
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
2017-05-26 21:54:51 +00:00
|
|
|
r._read = common.mustNotCall();
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
r.push('foobar');
|
|
|
|
r.push(null);
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
r.pipe(toArray(common.mustCall(function(array) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(array, ['foobar']);
|
2017-06-15 22:03:37 +00:00
|
|
|
})));
|
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify the behavior of pushing falsey values
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
2017-05-26 21:54:51 +00:00
|
|
|
r._read = common.mustNotCall();
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
r.push(false);
|
|
|
|
r.push(0);
|
|
|
|
r.push('');
|
|
|
|
r.push(null);
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
r.pipe(toArray(common.mustCall(function(array) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(array, [false, 0, '']);
|
2017-06-15 22:03:37 +00:00
|
|
|
})));
|
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify high watermark _read() behavior
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
highWaterMark: 6,
|
|
|
|
objectMode: true
|
|
|
|
});
|
2017-01-08 13:19:00 +00:00
|
|
|
let calls = 0;
|
|
|
|
const list = ['1', '2', '3', '4', '5', '6', '7', '8'];
|
2013-01-12 04:59:57 +00:00
|
|
|
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
r._read = function(n) {
|
2013-01-12 04:59:57 +00:00
|
|
|
calls++;
|
|
|
|
};
|
|
|
|
|
|
|
|
list.forEach(function(c) {
|
|
|
|
r.push(c);
|
|
|
|
});
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const v = r.read();
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(calls, 0);
|
|
|
|
assert.strictEqual(v, '1');
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const v2 = r.read();
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(v2, '2');
|
stream: Simplify flowing, passive data listening
Closes #5860
In streams2, there is an "old mode" for compatibility. Once switched
into this mode, there is no going back.
With this change, there is a "flowing mode" and a "paused mode". If you
add a data listener, then this will start the flow of data. However,
hitting the `pause()` method will switch *back* into a non-flowing mode,
where the `read()` method will pull data out.
Every time `read()` returns a data chunk, it also emits a `data` event.
In this way, a passive data listener can be added, and the stream passed
off to some other reader, for use with progress bars and the like.
There is no API change beyond this added flexibility.
2013-07-18 01:24:02 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const v3 = r.read();
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(v3, '3');
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(calls, 1);
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify high watermark push behavior
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
highWaterMark: 6,
|
|
|
|
objectMode: true
|
|
|
|
});
|
2017-05-26 21:54:51 +00:00
|
|
|
r._read = common.mustNotCall();
|
2017-01-08 13:19:00 +00:00
|
|
|
for (let i = 0; i < 6; i++) {
|
|
|
|
const bool = r.push(i);
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(bool, i !== 5);
|
2013-01-12 04:59:57 +00:00
|
|
|
}
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that objects can be written to stream
|
2017-01-08 13:19:00 +00:00
|
|
|
const w = new Writable({ objectMode: true });
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2013-03-04 03:14:06 +00:00
|
|
|
w._write = function(chunk, encoding, cb) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(chunk, { foo: 'bar' });
|
2013-01-12 04:59:57 +00:00
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
w.on('finish', common.mustCall());
|
2013-01-12 04:59:57 +00:00
|
|
|
w.write({ foo: 'bar' });
|
|
|
|
w.end();
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that multiple objects can be written to stream
|
2017-01-08 13:19:00 +00:00
|
|
|
const w = new Writable({ objectMode: true });
|
|
|
|
const list = [];
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2013-03-04 03:14:06 +00:00
|
|
|
w._write = function(chunk, encoding, cb) {
|
2013-01-12 04:59:57 +00:00
|
|
|
list.push(chunk);
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
w.on('finish', common.mustCall(function() {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [0, 1, 2, 3, 4]);
|
2017-06-15 22:03:37 +00:00
|
|
|
}));
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
w.write(0);
|
|
|
|
w.write(1);
|
|
|
|
w.write(2);
|
|
|
|
w.write(3);
|
|
|
|
w.write(4);
|
|
|
|
w.end();
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that strings can be written as objects
|
2017-01-08 13:19:00 +00:00
|
|
|
const w = new Writable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
2017-01-08 13:19:00 +00:00
|
|
|
const list = [];
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2013-03-04 03:14:06 +00:00
|
|
|
w._write = function(chunk, encoding, cb) {
|
2013-01-12 04:59:57 +00:00
|
|
|
list.push(chunk);
|
|
|
|
process.nextTick(cb);
|
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
w.on('finish', common.mustCall(function() {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, ['0', '1', '2', '3', '4']);
|
2017-06-15 22:03:37 +00:00
|
|
|
}));
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
w.write('0');
|
|
|
|
w.write('1');
|
|
|
|
w.write('2');
|
|
|
|
w.write('3');
|
|
|
|
w.write('4');
|
|
|
|
w.end();
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
{
|
|
|
|
// Verify that stream buffers finish until callback is called
|
2017-01-08 13:19:00 +00:00
|
|
|
const w = new Writable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
2017-01-08 13:19:00 +00:00
|
|
|
let called = false;
|
2013-01-12 04:59:57 +00:00
|
|
|
|
2013-03-04 03:14:06 +00:00
|
|
|
w._write = function(chunk, encoding, cb) {
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(chunk, 'foo');
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
process.nextTick(function() {
|
|
|
|
called = true;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-15 22:03:37 +00:00
|
|
|
w.on('finish', common.mustCall(function() {
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(called, true);
|
2017-06-15 22:03:37 +00:00
|
|
|
}));
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
w.write('foo');
|
|
|
|
w.end();
|
2017-06-15 22:03:37 +00:00
|
|
|
}
|