2017-01-03 13:16:48 -08: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 13:00:06 +02:00
|
|
|
'use strict';
|
2017-03-24 09:46:44 -07:00
|
|
|
|
|
|
|
const common = require('../common');
|
2020-12-29 15:20:41 +01:00
|
|
|
const { Readable: R, Writable: W } = require('stream');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2016-12-30 18:38:06 -05:00
|
|
|
const EE = require('events').EventEmitter;
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-11-10 19:18:12 +01:00
|
|
|
class TestReader extends R {
|
|
|
|
constructor(n) {
|
|
|
|
super();
|
|
|
|
this._buffer = Buffer.alloc(n || 100, 'x');
|
|
|
|
this._pos = 0;
|
|
|
|
this._bufs = 10;
|
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-11-10 19:18:12 +01:00
|
|
|
_read(n) {
|
|
|
|
const max = this._buffer.length - this._pos;
|
|
|
|
n = Math.max(n, 0);
|
|
|
|
const toRead = Math.min(n, max);
|
|
|
|
if (toRead === 0) {
|
2018-12-03 17:15:45 +01:00
|
|
|
// Simulate the read buffer filling up with some more bytes some time
|
2017-11-10 19:18:12 +01:00
|
|
|
// in the future.
|
|
|
|
setTimeout(() => {
|
|
|
|
this._pos = 0;
|
|
|
|
this._bufs -= 1;
|
|
|
|
if (this._bufs <= 0) {
|
|
|
|
// read them all!
|
|
|
|
if (!this.ended)
|
|
|
|
this.push(null);
|
|
|
|
} else {
|
|
|
|
// now we have more.
|
|
|
|
// kinda cheating by calling _read, but whatever,
|
|
|
|
// it's just fake anyway.
|
|
|
|
this._read(n);
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-11-10 19:18:12 +01:00
|
|
|
const ret = this._buffer.slice(this._pos, this._pos + toRead);
|
|
|
|
this._pos += toRead;
|
|
|
|
this.push(ret);
|
2012-10-07 13:26:03 -07:00
|
|
|
}
|
2017-11-10 19:18:12 +01:00
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-11-10 19:18:12 +01:00
|
|
|
class TestWriter extends EE {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.received = [];
|
|
|
|
this.flush = false;
|
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-11-10 19:18:12 +01:00
|
|
|
write(c) {
|
|
|
|
this.received.push(c.toString());
|
|
|
|
this.emit('write', c);
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-11-10 19:18:12 +01:00
|
|
|
end(c) {
|
|
|
|
if (c) this.write(c);
|
|
|
|
this.emit('end', this.received);
|
|
|
|
}
|
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Test basic functionality
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new TestReader(20);
|
|
|
|
|
|
|
|
const reads = [];
|
|
|
|
const expect = [ 'x',
|
|
|
|
'xx',
|
|
|
|
'xxx',
|
|
|
|
'xxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxxxxxx',
|
|
|
|
'xxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
|
|
'xxxxxxxxxxxxxxxxxxxxx' ];
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
r.on('end', common.mustCall(function() {
|
2017-05-21 14:40:17 -07:00
|
|
|
assert.deepStrictEqual(reads, expect);
|
2017-06-15 18:03:37 -04:00
|
|
|
}));
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let readSize = 1;
|
2012-10-07 13:26:03 -07:00
|
|
|
function flow() {
|
2017-01-08 13:19:00 +00:00
|
|
|
let res;
|
2012-10-07 13:26:03 -07:00
|
|
|
while (null !== (res = r.read(readSize++))) {
|
|
|
|
reads.push(res.toString());
|
|
|
|
}
|
|
|
|
r.once('readable', flow);
|
|
|
|
}
|
|
|
|
|
|
|
|
flow();
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify pipe
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new TestReader(5);
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const expect = [ 'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx' ];
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const w = new TestWriter();
|
2013-03-02 16:03:22 -08:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
w.on('end', common.mustCall(function(received) {
|
2017-05-21 14:40:17 -07:00
|
|
|
assert.deepStrictEqual(received, expect);
|
2017-06-15 18:03:37 -04:00
|
|
|
}));
|
2012-10-07 13:26:03 -07:00
|
|
|
|
|
|
|
r.pipe(w);
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(SPLIT) {
|
2017-06-15 18:03:37 -04:00
|
|
|
// Verify unpipe
|
|
|
|
const r = new TestReader(5);
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Unpipe after 3 writes, then write to another stream instead.
|
2017-06-15 18:03:37 -04:00
|
|
|
let expect = [ 'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx' ];
|
|
|
|
expect = [ expect.slice(0, SPLIT), expect.slice(SPLIT) ];
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
const w = [ new TestWriter(), new TestWriter() ];
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
let writes = SPLIT;
|
|
|
|
w[0].on('write', function() {
|
|
|
|
if (--writes === 0) {
|
|
|
|
r.unpipe();
|
2019-07-11 23:14:49 +08:00
|
|
|
assert.deepStrictEqual(r._readableState.pipes, []);
|
2017-06-15 18:03:37 -04:00
|
|
|
w[0].end();
|
|
|
|
r.pipe(w[1]);
|
2019-07-11 23:14:49 +08:00
|
|
|
assert.deepStrictEqual(r._readableState.pipes, [w[1]]);
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
|
|
|
});
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
let ended = 0;
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
w[0].on('end', common.mustCall(function(results) {
|
|
|
|
ended++;
|
|
|
|
assert.strictEqual(ended, 1);
|
|
|
|
assert.deepStrictEqual(results, expect[0]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
w[1].on('end', common.mustCall(function(results) {
|
|
|
|
ended++;
|
|
|
|
assert.strictEqual(ended, 2);
|
|
|
|
assert.deepStrictEqual(results, expect[1]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
r.pipe(w[0]);
|
2012-10-07 13:26:03 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify both writers get the same data when piping to destinations
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new TestReader(5);
|
|
|
|
const w = [ new TestWriter(), new TestWriter() ];
|
|
|
|
|
|
|
|
const expect = [ 'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx' ];
|
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
w[0].on('end', common.mustCall(function(received) {
|
2018-06-13 12:45:42 +05:30
|
|
|
assert.deepStrictEqual(received, expect);
|
2017-06-15 18:03:37 -04:00
|
|
|
}));
|
|
|
|
w[1].on('end', common.mustCall(function(received) {
|
2018-06-13 12:45:42 +05:30
|
|
|
assert.deepStrictEqual(received, expect);
|
2017-06-15 18:03:37 -04:00
|
|
|
}));
|
2012-10-07 13:26:03 -07:00
|
|
|
|
|
|
|
r.pipe(w[0]);
|
|
|
|
r.pipe(w[1]);
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
2012-10-07 13:26:03 -07:00
|
|
|
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(SPLIT) {
|
2017-06-15 18:03:37 -04:00
|
|
|
// Verify multi-unpipe
|
|
|
|
const r = new TestReader(5);
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Unpipe after 3 writes, then write to another stream instead.
|
2017-06-15 18:03:37 -04:00
|
|
|
let expect = [ 'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx',
|
|
|
|
'xxxxx' ];
|
|
|
|
expect = [ expect.slice(0, SPLIT), expect.slice(SPLIT) ];
|
|
|
|
|
|
|
|
const w = [ new TestWriter(), new TestWriter(), new TestWriter() ];
|
|
|
|
|
|
|
|
let writes = SPLIT;
|
|
|
|
w[0].on('write', function() {
|
|
|
|
if (--writes === 0) {
|
|
|
|
r.unpipe();
|
|
|
|
w[0].end();
|
|
|
|
r.pipe(w[1]);
|
|
|
|
}
|
|
|
|
});
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
let ended = 0;
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
w[0].on('end', common.mustCall(function(results) {
|
|
|
|
ended++;
|
|
|
|
assert.strictEqual(ended, 1);
|
|
|
|
assert.deepStrictEqual(results, expect[0]);
|
|
|
|
}));
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
w[1].on('end', common.mustCall(function(results) {
|
|
|
|
ended++;
|
|
|
|
assert.strictEqual(ended, 2);
|
|
|
|
assert.deepStrictEqual(results, expect[1]);
|
|
|
|
}));
|
2012-10-07 13:26:03 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
r.pipe(w[0]);
|
|
|
|
r.pipe(w[2]);
|
2012-10-07 13:26:03 -07:00
|
|
|
});
|
2013-01-14 16:03:38 -08:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify that back pressure is respected
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new R({ objectMode: true });
|
2017-05-21 14:36:12 -07:00
|
|
|
r._read = common.mustNotCall();
|
2017-01-08 13:19:00 +00:00
|
|
|
let counter = 0;
|
2015-05-19 13:00:06 +02:00
|
|
|
r.push(['one']);
|
|
|
|
r.push(['two']);
|
|
|
|
r.push(['three']);
|
|
|
|
r.push(['four']);
|
2013-01-11 20:59:57 -08:00
|
|
|
r.push(null);
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const w1 = new R();
|
2015-05-19 13:00:06 +02:00
|
|
|
w1.write = function(chunk) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(chunk[0], 'one');
|
2015-05-19 13:00:06 +02:00
|
|
|
w1.emit('close');
|
|
|
|
process.nextTick(function() {
|
2013-01-11 20:59:57 -08:00
|
|
|
r.pipe(w2);
|
|
|
|
r.pipe(w3);
|
2015-05-19 13:00:06 +02:00
|
|
|
});
|
2013-01-11 20:59:57 -08:00
|
|
|
};
|
2017-05-21 14:36:12 -07:00
|
|
|
w1.end = common.mustNotCall();
|
2013-01-11 20:59:57 -08:00
|
|
|
|
|
|
|
r.pipe(w1);
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const expected = ['two', 'two', 'three', 'three', 'four', 'four'];
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const w2 = new R();
|
2015-05-19 13:00:06 +02:00
|
|
|
w2.write = function(chunk) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(chunk[0], expected.shift());
|
|
|
|
assert.strictEqual(counter, 0);
|
2013-01-11 20:59:57 -08:00
|
|
|
|
|
|
|
counter++;
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
if (chunk[0] === 'four') {
|
2013-01-11 20:59:57 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
setTimeout(function() {
|
2013-01-11 20:59:57 -08:00
|
|
|
counter--;
|
2015-05-19 13:00:06 +02:00
|
|
|
w2.emit('drain');
|
2013-01-11 20:59:57 -08:00
|
|
|
}, 10);
|
|
|
|
|
|
|
|
return false;
|
2015-05-19 13:00:06 +02:00
|
|
|
};
|
2017-05-21 14:36:12 -07:00
|
|
|
w2.end = common.mustCall();
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const w3 = new R();
|
2015-05-19 13:00:06 +02:00
|
|
|
w3.write = function(chunk) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(chunk[0], expected.shift());
|
|
|
|
assert.strictEqual(counter, 1);
|
2013-01-11 20:59:57 -08:00
|
|
|
|
|
|
|
counter++;
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
if (chunk[0] === 'four') {
|
2013-01-11 20:59:57 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
setTimeout(function() {
|
2013-01-11 20:59:57 -08:00
|
|
|
counter--;
|
2015-05-19 13:00:06 +02:00
|
|
|
w3.emit('drain');
|
2013-01-11 20:59:57 -08:00
|
|
|
}, 50);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
2017-06-15 18:03:37 -04:00
|
|
|
w3.end = common.mustCall(function() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(counter, 2);
|
|
|
|
assert.strictEqual(expected.length, 0);
|
2017-06-15 18:03:37 -04:00
|
|
|
});
|
|
|
|
}
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify read(0) behavior for ended streams
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new R();
|
|
|
|
let written = false;
|
|
|
|
let ended = false;
|
2017-05-21 14:36:12 -07:00
|
|
|
r._read = common.mustNotCall();
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2016-01-25 15:00:06 -08:00
|
|
|
r.push(Buffer.from('foo'));
|
2013-01-11 20:59:57 -08:00
|
|
|
r.push(null);
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const v = r.read(0);
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(v, null);
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const w = new R();
|
2015-05-19 13:00:06 +02:00
|
|
|
w.write = function(buffer) {
|
2013-01-11 20:59:57 -08:00
|
|
|
written = true;
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(ended, false);
|
|
|
|
assert.strictEqual(buffer.toString(), 'foo');
|
2013-01-11 20:59:57 -08:00
|
|
|
};
|
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
w.end = common.mustCall(function() {
|
2013-01-11 20:59:57 -08:00
|
|
|
ended = true;
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(written, true);
|
2017-06-15 18:03:37 -04:00
|
|
|
});
|
2013-01-11 20:59:57 -08:00
|
|
|
|
|
|
|
r.pipe(w);
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
2013-01-11 20:59:57 -08:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify synchronous _read ending
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new R();
|
|
|
|
let called = false;
|
2015-05-19 13:00:06 +02:00
|
|
|
r._read = function(n) {
|
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 15:32:32 -08:00
|
|
|
r.push(null);
|
2013-01-11 20:59:57 -08:00
|
|
|
};
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
r.once('end', function() {
|
2017-06-15 18:03:37 -04:00
|
|
|
// Verify that this is called before the next tick
|
2013-01-11 20:59:57 -08:00
|
|
|
called = true;
|
2015-05-19 13:00:06 +02:00
|
|
|
});
|
2013-01-11 20:59:57 -08:00
|
|
|
|
|
|
|
r.read();
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
process.nextTick(function() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(called, true);
|
2015-05-19 13:00:06 +02:00
|
|
|
});
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
2013-03-02 16:03:22 -08:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify that adding readable listeners trigger data flow
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new R({ highWaterMark: 5 });
|
|
|
|
let onReadable = false;
|
|
|
|
let readCalled = 0;
|
2013-03-02 16:03:22 -08:00
|
|
|
|
|
|
|
r._read = function(n) {
|
|
|
|
if (readCalled++ === 2)
|
|
|
|
r.push(null);
|
|
|
|
else
|
2016-01-25 15:00:06 -08:00
|
|
|
r.push(Buffer.from('asdf'));
|
2013-03-02 16:03:22 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
r.on('readable', function() {
|
|
|
|
onReadable = true;
|
|
|
|
r.read();
|
|
|
|
});
|
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
r.on('end', common.mustCall(function() {
|
2017-05-21 14:40:17 -07:00
|
|
|
assert.strictEqual(readCalled, 3);
|
|
|
|
assert.ok(onReadable);
|
2017-06-15 18:03:37 -04:00
|
|
|
}));
|
|
|
|
}
|
2013-08-27 18:59:58 -07:00
|
|
|
|
2017-06-15 18:03:37 -04:00
|
|
|
{
|
|
|
|
// Verify that streams are chainable
|
2017-01-08 13:19:00 +00:00
|
|
|
const r = new R();
|
2017-05-21 14:36:12 -07:00
|
|
|
r._read = common.mustCall();
|
2017-01-08 13:19:00 +00:00
|
|
|
const r2 = r.setEncoding('utf8').pause().resume().pause();
|
2017-05-21 14:40:17 -07:00
|
|
|
assert.strictEqual(r, r2);
|
2017-06-15 18:03:37 -04:00
|
|
|
}
|
2019-05-19 17:24:07 +05:30
|
|
|
|
2019-07-05 15:54:34 +08:00
|
|
|
{
|
|
|
|
// Verify readableEncoding property
|
2022-01-24 23:03:17 -08:00
|
|
|
assert(Object.hasOwn(R.prototype, 'readableEncoding'));
|
2019-07-05 15:54:34 +08:00
|
|
|
|
|
|
|
const r = new R({ encoding: 'utf8' });
|
|
|
|
assert.strictEqual(r.readableEncoding, 'utf8');
|
|
|
|
}
|
|
|
|
|
2019-05-19 17:24:07 +05:30
|
|
|
{
|
|
|
|
// Verify readableObjectMode property
|
2022-01-24 23:03:17 -08:00
|
|
|
assert(Object.hasOwn(R.prototype, 'readableObjectMode'));
|
2019-07-05 15:54:34 +08:00
|
|
|
|
2019-05-19 17:24:07 +05:30
|
|
|
const r = new R({ objectMode: true });
|
|
|
|
assert.strictEqual(r.readableObjectMode, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Verify writableObjectMode property
|
2022-01-24 23:03:17 -08:00
|
|
|
assert(Object.hasOwn(W.prototype, 'writableObjectMode'));
|
2019-07-05 15:54:34 +08:00
|
|
|
|
2019-05-19 17:24:07 +05:30
|
|
|
const w = new W({ objectMode: true });
|
|
|
|
assert.strictEqual(w.writableObjectMode, true);
|
|
|
|
}
|