deno.land / std@0.166.0 / node / _tools / test / parallel / test-stream-duplex-from.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const assert = require('assert');const { Duplex, Readable, Writable, pipeline } = require('stream');const { Blob } = require('buffer');
{ const d = Duplex.from({ readable: new Readable({ read() { this.push('asd'); this.push(null); } }) }); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, false); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); }));}
{ const d = Duplex.from(new Readable({ read() { this.push('asd'); this.push(null); } })); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, false); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); }));}
{ let ret = ''; const d = Duplex.from(new Writable({ write(chunk, encoding, callback) { ret += chunk; callback(); } })); assert.strictEqual(d.readable, false); assert.strictEqual(d.writable, true); d.end('asd'); d.on('finish', common.mustCall(function() { assert.strictEqual(d.writable, false); assert.strictEqual(ret, 'asd'); }));}
{ let ret = ''; const d = Duplex.from({ writable: new Writable({ write(chunk, encoding, callback) { ret += chunk; callback(); } }) }); assert.strictEqual(d.readable, false); assert.strictEqual(d.writable, true); d.end('asd'); d.on('finish', common.mustCall(function() { assert.strictEqual(d.writable, false); assert.strictEqual(ret, 'asd'); }));}
{ let ret = ''; const d = Duplex.from({ readable: new Readable({ read() { this.push('asd'); this.push(null); } }), writable: new Writable({ write(chunk, encoding, callback) { ret += chunk; callback(); } }) }); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, true); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); })); d.end('asd'); d.once('finish', common.mustCall(function() { assert.strictEqual(d.writable, false); assert.strictEqual(ret, 'asd'); }));}
{ const d = Duplex.from(Promise.resolve('asd')); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, false); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); }));}
{ // https://github.com/nodejs/node/issues/40497 pipeline( ['abc\ndef\nghi'], Duplex.from(async function * (source) { let rest = ''; for await (const chunk of source) { const lines = (rest + chunk.toString()).split('\n'); rest = lines.pop(); for (const line of lines) { yield line; } } yield rest; }), async function * (source) { // eslint-disable-line require-yield let ret = ''; for await (const x of source) { ret += x; } assert.strictEqual(ret, 'abcdefghi'); }, common.mustCall(() => {}), );}
// Ensure that isDuplexNodeStream was called{ const duplex = new Duplex(); assert.strictEqual(Duplex.from(duplex), duplex);}
// Ensure that Duplex.from works for blobs{ const blob = new Blob(['blob']); const expectedByteLength = blob.size; const duplex = Duplex.from(blob); duplex.on('data', common.mustCall((arrayBuffer) => { assert.strictEqual(arrayBuffer.byteLength, expectedByteLength); }));}
// Ensure that given a promise rejection it emits an error{ const myErrorMessage = 'myCustomError'; Duplex.from(Promise.reject(myErrorMessage)) .on('error', common.mustCall((error) => { assert.strictEqual(error, myErrorMessage); }));}
// Ensure that given a promise rejection on an async function it emits an error{ const myErrorMessage = 'myCustomError'; async function asyncFn() { return Promise.reject(myErrorMessage); }
Duplex.from(asyncFn) .on('error', common.mustCall((error) => { assert.strictEqual(error, myErrorMessage); }));}
// Ensure that Duplex.from throws an Invalid return value when function is void{ assert.throws(() => Duplex.from(() => {}), { code: 'ERR_INVALID_RETURN_VALUE', });}
// Ensure data if a sub object has a readable stream it's duplexified{ const msg = Buffer.from('hello'); const duplex = Duplex.from({ readable: Readable({ read() { this.push(msg); this.push(null); } }) }).on('data', common.mustCall((data) => { assert.strictEqual(data, msg); }));
assert.strictEqual(duplex.writable, false);}
// Ensure data if a sub object has a writable stream it's duplexified{ const msg = Buffer.from('hello'); const duplex = Duplex.from({ writable: Writable({ write: common.mustCall((data) => { assert.strictEqual(data, msg); }) }) });
duplex.write(msg); assert.strictEqual(duplex.readable, false);}
// Ensure data if a sub object has a writable and readable stream it's duplexified{ const msg = Buffer.from('hello');
const duplex = Duplex.from({ readable: Readable({ read() { this.push(msg); this.push(null); } }), writable: Writable({ write: common.mustCall((data) => { assert.strictEqual(data, msg); }) }) });
duplex.pipe(duplex) .on('data', common.mustCall((data) => { assert.strictEqual(data, msg); assert.strictEqual(duplex.readable, true); assert.strictEqual(duplex.writable, true); })) .on('end', common.mustCall());}
// Ensure that given readable stream that throws an error it calls destroy{ const myErrorMessage = 'error!'; const duplex = Duplex.from(Readable({ read() { throw new Error(myErrorMessage); } })); duplex.on('error', common.mustCall((msg) => { assert.strictEqual(msg.message, myErrorMessage); }));}
// Ensure that given writable stream that throws an error it calls destroy{ const myErrorMessage = 'error!'; const duplex = Duplex.from(Writable({ write(chunk, enc, cb) { cb(myErrorMessage); } }));
duplex.on('error', common.mustCall((msg) => { assert.strictEqual(msg, myErrorMessage); }));
duplex.write('test');}
Version Info