2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2015-12-24 00:02:12 +00:00
|
|
|
require('../common');
|
2016-12-30 23:38:06 +00:00
|
|
|
const Readable = require('_stream_readable');
|
|
|
|
const Writable = require('_stream_writable');
|
|
|
|
const assert = require('assert');
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
// tiny node-tap lookalike.
|
2017-01-08 13:19:00 +00:00
|
|
|
const tests = [];
|
|
|
|
let count = 0;
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
function test(name, fn) {
|
|
|
|
count++;
|
|
|
|
tests.push([name, fn]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const next = tests.shift();
|
2013-01-12 04:59:57 +00:00
|
|
|
if (!next)
|
|
|
|
return console.error('ok');
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const name = next[0];
|
|
|
|
const fn = next[1];
|
2013-01-12 04:59:57 +00:00
|
|
|
console.log('# %s', name);
|
|
|
|
fn({
|
2016-04-19 22:37:45 +00:00
|
|
|
same: assert.deepStrictEqual,
|
2017-01-08 15:36:25 +00:00
|
|
|
equal: assert.strictEqual,
|
2013-01-12 04:59:57 +00:00
|
|
|
end: function() {
|
|
|
|
count--;
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure all tests have run
|
|
|
|
process.on('exit', function() {
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(count, 0);
|
2013-01-12 04:59:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
process.nextTick(run);
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
stream.end = function() {
|
|
|
|
callback(list);
|
|
|
|
};
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromArray(list) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({ objectMode: true });
|
2013-02-22 19:24:05 +00:00
|
|
|
r._read = noop;
|
2013-01-12 04:59:57 +00:00
|
|
|
list.forEach(function(chunk) {
|
|
|
|
r.push(chunk);
|
|
|
|
});
|
|
|
|
r.push(null);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
|
|
|
test('can read objects from stream', function(t) {
|
2017-01-08 13:19:00 +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' });
|
|
|
|
assert.deepStrictEqual(v3, null);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can pipe objects into stream', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = fromArray([{ one: '1'}, { two: '2' }]);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
r.pipe(toArray(function(list) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [
|
2013-01-12 04:59:57 +00:00
|
|
|
{ one: '1' },
|
|
|
|
{ two: '2' }
|
|
|
|
]);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('read(n) is ignored', function(t) {
|
2017-01-08 13:19:00 +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 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' });
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can read objects from _read (sync)', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({ objectMode: true });
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
r.pipe(toArray(function(list) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [
|
2013-01-12 04:59:57 +00:00
|
|
|
{ one: '1' },
|
|
|
|
{ two: '2' }
|
|
|
|
]);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can read objects from _read (async)', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({ objectMode: true });
|
|
|
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
r.pipe(toArray(function(list) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [
|
2013-01-12 04:59:57 +00:00
|
|
|
{ one: '1' },
|
|
|
|
{ two: '2' }
|
|
|
|
]);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can read strings as objects', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
|
|
|
r._read = noop;
|
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);
|
|
|
|
|
|
|
|
r.pipe(toArray(function(array) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(array, list);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('read(0) for object streams', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
|
|
|
r._read = noop;
|
|
|
|
|
|
|
|
r.push('foobar');
|
|
|
|
r.push(null);
|
|
|
|
|
|
|
|
r.pipe(toArray(function(array) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(array, ['foobar']);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('falsey values', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
objectMode: true
|
|
|
|
});
|
|
|
|
r._read = noop;
|
|
|
|
|
|
|
|
r.push(false);
|
|
|
|
r.push(0);
|
|
|
|
r.push('');
|
|
|
|
r.push(null);
|
|
|
|
|
|
|
|
r.pipe(toArray(function(array) {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(array, [false, 0, '']);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('high watermark _read', function(t) {
|
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);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('high watermark push', function(t) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new Readable({
|
2013-01-12 04:59:57 +00:00
|
|
|
highWaterMark: 6,
|
|
|
|
objectMode: true
|
|
|
|
});
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can write objects to stream', function(t) {
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
w.on('finish', function() {
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
w.write({ foo: 'bar' });
|
|
|
|
w.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can write multiple objects to stream', function(t) {
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
w.on('finish', function() {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, [0, 1, 2, 3, 4]);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
w.write(0);
|
|
|
|
w.write(1);
|
|
|
|
w.write(2);
|
|
|
|
w.write(3);
|
|
|
|
w.write(4);
|
|
|
|
w.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can write strings as objects', function(t) {
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
w.on('finish', function() {
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(list, ['0', '1', '2', '3', '4']);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
w.write('0');
|
|
|
|
w.write('1');
|
|
|
|
w.write('2');
|
|
|
|
w.write('3');
|
|
|
|
w.write('4');
|
|
|
|
w.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('buffers finish until cb is called', function(t) {
|
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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
w.on('finish', function() {
|
2016-11-12 07:26:52 +00:00
|
|
|
assert.strictEqual(called, true);
|
2013-01-12 04:59:57 +00:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
w.write('foo');
|
|
|
|
w.end();
|
|
|
|
});
|