deno.land / std@0.166.0 / node / _tools / test / parallel / test-stream2-transform.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478// 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
// 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.
'use strict';const common = require('../common');const assert = require('assert');const { PassThrough, Transform } = require('stream');
{ // Verify writable side consumption const tx = new Transform({ highWaterMark: 10 });
let transformed = 0; tx._transform = function(chunk, encoding, cb) { transformed += chunk.length; tx.push(chunk); cb(); };
for (let i = 1; i <= 10; i++) { tx.write(Buffer.allocUnsafe(i)); } tx.end();
assert.strictEqual(tx.readableLength, 10); assert.strictEqual(transformed, 10); assert.deepStrictEqual(tx.writableBuffer.map(function(c) { return c.chunk.length; }), [5, 6, 7, 8, 9, 10]);}
{ // Verify passthrough behavior const pt = new PassThrough();
pt.write(Buffer.from('foog')); pt.write(Buffer.from('bark')); pt.write(Buffer.from('bazy')); pt.write(Buffer.from('kuel')); pt.end();
assert.strictEqual(pt.read(5).toString(), 'foogb'); assert.strictEqual(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5).toString(), 'zykue'); assert.strictEqual(pt.read(5).toString(), 'l');}
{ // Verify object passthrough behavior const pt = new PassThrough({ objectMode: true });
pt.write(1); pt.write(true); pt.write(false); pt.write(0); pt.write('foo'); pt.write(''); pt.write({ a: 'b' }); pt.end();
assert.strictEqual(pt.read(), 1); assert.strictEqual(pt.read(), true); assert.strictEqual(pt.read(), false); assert.strictEqual(pt.read(), 0); assert.strictEqual(pt.read(), 'foo'); assert.strictEqual(pt.read(), ''); assert.deepStrictEqual(pt.read(), { a: 'b' });}
{ // Verify passthrough constructor behavior const pt = PassThrough();
assert(pt instanceof PassThrough);}
{ // Verify transform constructor behavior const pt = Transform();
assert(pt instanceof Transform);}
{ // Perform a simple transform const pt = new Transform(); pt._transform = function(c, e, cb) { const ret = Buffer.alloc(c.length, 'x'); pt.push(ret); cb(); };
pt.write(Buffer.from('foog')); pt.write(Buffer.from('bark')); pt.write(Buffer.from('bazy')); pt.write(Buffer.from('kuel')); pt.end();
assert.strictEqual(pt.read(5).toString(), 'xxxxx'); assert.strictEqual(pt.read(5).toString(), 'xxxxx'); assert.strictEqual(pt.read(5).toString(), 'xxxxx'); assert.strictEqual(pt.read(5).toString(), 'x');}
{ // Verify simple object transform const pt = new Transform({ objectMode: true }); pt._transform = function(c, e, cb) { pt.push(JSON.stringify(c)); cb(); };
pt.write(1); pt.write(true); pt.write(false); pt.write(0); pt.write('foo'); pt.write(''); pt.write({ a: 'b' }); pt.end();
assert.strictEqual(pt.read(), '1'); assert.strictEqual(pt.read(), 'true'); assert.strictEqual(pt.read(), 'false'); assert.strictEqual(pt.read(), '0'); assert.strictEqual(pt.read(), '"foo"'); assert.strictEqual(pt.read(), '""'); assert.strictEqual(pt.read(), '{"a":"b"}');}
{ // Verify async passthrough const pt = new Transform(); pt._transform = function(chunk, encoding, cb) { setTimeout(function() { pt.push(chunk); cb(); }, 10); };
pt.write(Buffer.from('foog')); pt.write(Buffer.from('bark')); pt.write(Buffer.from('bazy')); pt.write(Buffer.from('kuel')); pt.end();
pt.on('finish', common.mustCall(function() { assert.strictEqual(pt.read(5).toString(), 'foogb'); assert.strictEqual(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5).toString(), 'zykue'); assert.strictEqual(pt.read(5).toString(), 'l'); }));}
{ // Verify asymmetric transform (expand) const pt = new Transform();
// Emit each chunk 2 times. pt._transform = function(chunk, encoding, cb) { setTimeout(function() { pt.push(chunk); setTimeout(function() { pt.push(chunk); cb(); }, 10); }, 10); };
pt.write(Buffer.from('foog')); pt.write(Buffer.from('bark')); pt.write(Buffer.from('bazy')); pt.write(Buffer.from('kuel')); pt.end();
pt.on('finish', common.mustCall(function() { assert.strictEqual(pt.read(5).toString(), 'foogf'); assert.strictEqual(pt.read(5).toString(), 'oogba'); assert.strictEqual(pt.read(5).toString(), 'rkbar'); assert.strictEqual(pt.read(5).toString(), 'kbazy'); assert.strictEqual(pt.read(5).toString(), 'bazyk'); assert.strictEqual(pt.read(5).toString(), 'uelku'); assert.strictEqual(pt.read(5).toString(), 'el'); }));}
{ // Verify asymmetric transform (compress) const pt = new Transform();
// Each output is the first char of 3 consecutive chunks, // or whatever's left. pt.state = '';
pt._transform = function(chunk, encoding, cb) { if (!chunk) chunk = ''; const s = chunk.toString(); setTimeout(() => { this.state += s.charAt(0); if (this.state.length === 3) { pt.push(Buffer.from(this.state)); this.state = ''; } cb(); }, 10); };
pt._flush = function(cb) { // Just output whatever we have. pt.push(Buffer.from(this.state)); this.state = ''; cb(); };
pt.write(Buffer.from('aaaa')); pt.write(Buffer.from('bbbb')); pt.write(Buffer.from('cccc')); pt.write(Buffer.from('dddd')); pt.write(Buffer.from('eeee')); pt.write(Buffer.from('aaaa')); pt.write(Buffer.from('bbbb')); pt.write(Buffer.from('cccc')); pt.write(Buffer.from('dddd')); pt.write(Buffer.from('eeee')); pt.write(Buffer.from('aaaa')); pt.write(Buffer.from('bbbb')); pt.write(Buffer.from('cccc')); pt.write(Buffer.from('dddd')); pt.end();
// 'abcdeabcdeabcd' pt.on('finish', common.mustCall(function() { assert.strictEqual(pt.read(5).toString(), 'abcde'); assert.strictEqual(pt.read(5).toString(), 'abcde'); assert.strictEqual(pt.read(5).toString(), 'abcd'); }));}
// This tests for a stall when data is written to a full stream// that has empty transforms.{ // Verify complex transform behavior let count = 0; let saved = null; const pt = new Transform({ highWaterMark: 3 }); pt._transform = function(c, e, cb) { if (count++ === 1) saved = c; else { if (saved) { pt.push(saved); saved = null; } pt.push(c); }
cb(); };
pt.once('readable', function() { process.nextTick(function() { pt.write(Buffer.from('d')); pt.write(Buffer.from('ef'), common.mustCall(function() { pt.end(); })); assert.strictEqual(pt.read().toString(), 'abcdef'); assert.strictEqual(pt.read(), null); }); });
pt.write(Buffer.from('abc'));}
{ // Verify passthrough event emission const pt = new PassThrough(); let emits = 0; pt.on('readable', function() { emits++; });
pt.write(Buffer.from('foog')); pt.write(Buffer.from('bark'));
assert.strictEqual(emits, 0); assert.strictEqual(pt.read(5).toString(), 'foogb'); assert.strictEqual(String(pt.read(5)), 'null'); assert.strictEqual(emits, 0);
pt.write(Buffer.from('bazy')); pt.write(Buffer.from('kuel'));
assert.strictEqual(emits, 0); assert.strictEqual(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5).toString(), 'zykue'); assert.strictEqual(pt.read(5), null);
pt.end();
assert.strictEqual(emits, 1); assert.strictEqual(pt.read(5).toString(), 'l'); assert.strictEqual(pt.read(5), null); assert.strictEqual(emits, 1);}
{ // Verify passthrough event emission reordering const pt = new PassThrough(); let emits = 0; pt.on('readable', function() { emits++; });
pt.write(Buffer.from('foog')); pt.write(Buffer.from('bark'));
assert.strictEqual(emits, 0); assert.strictEqual(pt.read(5).toString(), 'foogb'); assert.strictEqual(pt.read(5), null);
pt.once('readable', common.mustCall(function() { assert.strictEqual(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5), null);
pt.once('readable', common.mustCall(function() { assert.strictEqual(pt.read(5).toString(), 'zykue'); assert.strictEqual(pt.read(5), null); pt.once('readable', common.mustCall(function() { assert.strictEqual(pt.read(5).toString(), 'l'); assert.strictEqual(pt.read(5), null); assert.strictEqual(emits, 3); })); pt.end(); })); pt.write(Buffer.from('kuel')); }));
pt.write(Buffer.from('bazy'));}
{ // Verify passthrough facade const pt = new PassThrough(); const datas = []; pt.on('data', function(chunk) { datas.push(chunk.toString()); });
pt.on('end', common.mustCall(function() { assert.deepStrictEqual(datas, ['foog', 'bark', 'bazy', 'kuel']); }));
pt.write(Buffer.from('foog')); setTimeout(function() { pt.write(Buffer.from('bark')); setTimeout(function() { pt.write(Buffer.from('bazy')); setTimeout(function() { pt.write(Buffer.from('kuel')); setTimeout(function() { pt.end(); }, 10); }, 10); }, 10); }, 10);}
{ // Verify object transform (JSON parse) const jp = new Transform({ objectMode: true }); jp._transform = function(data, encoding, cb) { try { jp.push(JSON.parse(data)); cb(); } catch (er) { cb(er); } };
// Anything except null/undefined is fine. // those are "magic" in the stream API, because they signal EOF. const objects = [ { foo: 'bar' }, 100, 'string', { nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }, ];
let ended = false; jp.on('end', function() { ended = true; });
objects.forEach(function(obj) { jp.write(JSON.stringify(obj)); const res = jp.read(); assert.deepStrictEqual(res, obj); });
jp.end(); // Read one more time to get the 'end' event jp.read();
process.nextTick(common.mustCall(function() { assert.strictEqual(ended, true); }));}
{ // Verify object transform (JSON stringify) const js = new Transform({ objectMode: true }); js._transform = function(data, encoding, cb) { try { js.push(JSON.stringify(data)); cb(); } catch (er) { cb(er); } };
// Anything except null/undefined is fine. // those are "magic" in the stream API, because they signal EOF. const objects = [ { foo: 'bar' }, 100, 'string', { nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }, ];
let ended = false; js.on('end', function() { ended = true; });
objects.forEach(function(obj) { js.write(obj); const res = js.read(); assert.strictEqual(res, JSON.stringify(obj)); });
js.end(); // Read one more time to get the 'end' event js.read();
process.nextTick(common.mustCall(function() { assert.strictEqual(ended, true); }));}
Version Info