11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst { PassThrough, Transform } = require('stream'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci{ 281cb0ef41Sopenharmony_ci // Verify writable side consumption 291cb0ef41Sopenharmony_ci const tx = new Transform({ 301cb0ef41Sopenharmony_ci highWaterMark: 10 311cb0ef41Sopenharmony_ci }); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci let transformed = 0; 341cb0ef41Sopenharmony_ci tx._transform = function(chunk, encoding, cb) { 351cb0ef41Sopenharmony_ci transformed += chunk.length; 361cb0ef41Sopenharmony_ci tx.push(chunk); 371cb0ef41Sopenharmony_ci cb(); 381cb0ef41Sopenharmony_ci }; 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci for (let i = 1; i <= 10; i++) { 411cb0ef41Sopenharmony_ci tx.write(Buffer.allocUnsafe(i)); 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci tx.end(); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci assert.strictEqual(tx.readableLength, 10); 461cb0ef41Sopenharmony_ci assert.strictEqual(transformed, 10); 471cb0ef41Sopenharmony_ci assert.deepStrictEqual(tx.writableBuffer.map(function(c) { 481cb0ef41Sopenharmony_ci return c.chunk.length; 491cb0ef41Sopenharmony_ci }), [5, 6, 7, 8, 9, 10]); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci{ 531cb0ef41Sopenharmony_ci // Verify passthrough behavior 541cb0ef41Sopenharmony_ci const pt = new PassThrough(); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 571cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 581cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 591cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 601cb0ef41Sopenharmony_ci pt.end(); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'foogb'); 631cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'arkba'); 641cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'zykue'); 651cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'l'); 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci{ 691cb0ef41Sopenharmony_ci // Verify object passthrough behavior 701cb0ef41Sopenharmony_ci const pt = new PassThrough({ objectMode: true }); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci pt.write(1); 731cb0ef41Sopenharmony_ci pt.write(true); 741cb0ef41Sopenharmony_ci pt.write(false); 751cb0ef41Sopenharmony_ci pt.write(0); 761cb0ef41Sopenharmony_ci pt.write('foo'); 771cb0ef41Sopenharmony_ci pt.write(''); 781cb0ef41Sopenharmony_ci pt.write({ a: 'b' }); 791cb0ef41Sopenharmony_ci pt.end(); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), 1); 821cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), true); 831cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), false); 841cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), 0); 851cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), 'foo'); 861cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), ''); 871cb0ef41Sopenharmony_ci assert.deepStrictEqual(pt.read(), { a: 'b' }); 881cb0ef41Sopenharmony_ci} 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci{ 911cb0ef41Sopenharmony_ci // Verify passthrough constructor behavior 921cb0ef41Sopenharmony_ci const pt = PassThrough(); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci assert(pt instanceof PassThrough); 951cb0ef41Sopenharmony_ci} 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci{ 981cb0ef41Sopenharmony_ci // Verify transform constructor behavior 991cb0ef41Sopenharmony_ci const pt = Transform(); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci assert(pt instanceof Transform); 1021cb0ef41Sopenharmony_ci} 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci{ 1051cb0ef41Sopenharmony_ci // Perform a simple transform 1061cb0ef41Sopenharmony_ci const pt = new Transform(); 1071cb0ef41Sopenharmony_ci pt._transform = function(c, e, cb) { 1081cb0ef41Sopenharmony_ci const ret = Buffer.alloc(c.length, 'x'); 1091cb0ef41Sopenharmony_ci pt.push(ret); 1101cb0ef41Sopenharmony_ci cb(); 1111cb0ef41Sopenharmony_ci }; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 1141cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 1151cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 1161cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 1171cb0ef41Sopenharmony_ci pt.end(); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'xxxxx'); 1201cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'xxxxx'); 1211cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'xxxxx'); 1221cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'x'); 1231cb0ef41Sopenharmony_ci} 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci{ 1261cb0ef41Sopenharmony_ci // Verify simple object transform 1271cb0ef41Sopenharmony_ci const pt = new Transform({ objectMode: true }); 1281cb0ef41Sopenharmony_ci pt._transform = function(c, e, cb) { 1291cb0ef41Sopenharmony_ci pt.push(JSON.stringify(c)); 1301cb0ef41Sopenharmony_ci cb(); 1311cb0ef41Sopenharmony_ci }; 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci pt.write(1); 1341cb0ef41Sopenharmony_ci pt.write(true); 1351cb0ef41Sopenharmony_ci pt.write(false); 1361cb0ef41Sopenharmony_ci pt.write(0); 1371cb0ef41Sopenharmony_ci pt.write('foo'); 1381cb0ef41Sopenharmony_ci pt.write(''); 1391cb0ef41Sopenharmony_ci pt.write({ a: 'b' }); 1401cb0ef41Sopenharmony_ci pt.end(); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), '1'); 1431cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), 'true'); 1441cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), 'false'); 1451cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), '0'); 1461cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), '"foo"'); 1471cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), '""'); 1481cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), '{"a":"b"}'); 1491cb0ef41Sopenharmony_ci} 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci{ 1521cb0ef41Sopenharmony_ci // Verify async passthrough 1531cb0ef41Sopenharmony_ci const pt = new Transform(); 1541cb0ef41Sopenharmony_ci pt._transform = function(chunk, encoding, cb) { 1551cb0ef41Sopenharmony_ci setTimeout(function() { 1561cb0ef41Sopenharmony_ci pt.push(chunk); 1571cb0ef41Sopenharmony_ci cb(); 1581cb0ef41Sopenharmony_ci }, 10); 1591cb0ef41Sopenharmony_ci }; 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 1621cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 1631cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 1641cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 1651cb0ef41Sopenharmony_ci pt.end(); 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci pt.on('finish', common.mustCall(function() { 1681cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'foogb'); 1691cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'arkba'); 1701cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'zykue'); 1711cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'l'); 1721cb0ef41Sopenharmony_ci })); 1731cb0ef41Sopenharmony_ci} 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci{ 1761cb0ef41Sopenharmony_ci // Verify asymmetric transform (expand) 1771cb0ef41Sopenharmony_ci const pt = new Transform(); 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci // Emit each chunk 2 times. 1801cb0ef41Sopenharmony_ci pt._transform = function(chunk, encoding, cb) { 1811cb0ef41Sopenharmony_ci setTimeout(function() { 1821cb0ef41Sopenharmony_ci pt.push(chunk); 1831cb0ef41Sopenharmony_ci setTimeout(function() { 1841cb0ef41Sopenharmony_ci pt.push(chunk); 1851cb0ef41Sopenharmony_ci cb(); 1861cb0ef41Sopenharmony_ci }, 10); 1871cb0ef41Sopenharmony_ci }, 10); 1881cb0ef41Sopenharmony_ci }; 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 1911cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 1921cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 1931cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 1941cb0ef41Sopenharmony_ci pt.end(); 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci pt.on('finish', common.mustCall(function() { 1971cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'foogf'); 1981cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'oogba'); 1991cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'rkbar'); 2001cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'kbazy'); 2011cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'bazyk'); 2021cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'uelku'); 2031cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'el'); 2041cb0ef41Sopenharmony_ci })); 2051cb0ef41Sopenharmony_ci} 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci{ 2081cb0ef41Sopenharmony_ci // Verify asymmetric transform (compress) 2091cb0ef41Sopenharmony_ci const pt = new Transform(); 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci // Each output is the first char of 3 consecutive chunks, 2121cb0ef41Sopenharmony_ci // or whatever's left. 2131cb0ef41Sopenharmony_ci pt.state = ''; 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci pt._transform = function(chunk, encoding, cb) { 2161cb0ef41Sopenharmony_ci if (!chunk) 2171cb0ef41Sopenharmony_ci chunk = ''; 2181cb0ef41Sopenharmony_ci const s = chunk.toString(); 2191cb0ef41Sopenharmony_ci setTimeout(() => { 2201cb0ef41Sopenharmony_ci this.state += s.charAt(0); 2211cb0ef41Sopenharmony_ci if (this.state.length === 3) { 2221cb0ef41Sopenharmony_ci pt.push(Buffer.from(this.state)); 2231cb0ef41Sopenharmony_ci this.state = ''; 2241cb0ef41Sopenharmony_ci } 2251cb0ef41Sopenharmony_ci cb(); 2261cb0ef41Sopenharmony_ci }, 10); 2271cb0ef41Sopenharmony_ci }; 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci pt._flush = function(cb) { 2301cb0ef41Sopenharmony_ci // Just output whatever we have. 2311cb0ef41Sopenharmony_ci pt.push(Buffer.from(this.state)); 2321cb0ef41Sopenharmony_ci this.state = ''; 2331cb0ef41Sopenharmony_ci cb(); 2341cb0ef41Sopenharmony_ci }; 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_ci pt.write(Buffer.from('aaaa')); 2371cb0ef41Sopenharmony_ci pt.write(Buffer.from('bbbb')); 2381cb0ef41Sopenharmony_ci pt.write(Buffer.from('cccc')); 2391cb0ef41Sopenharmony_ci pt.write(Buffer.from('dddd')); 2401cb0ef41Sopenharmony_ci pt.write(Buffer.from('eeee')); 2411cb0ef41Sopenharmony_ci pt.write(Buffer.from('aaaa')); 2421cb0ef41Sopenharmony_ci pt.write(Buffer.from('bbbb')); 2431cb0ef41Sopenharmony_ci pt.write(Buffer.from('cccc')); 2441cb0ef41Sopenharmony_ci pt.write(Buffer.from('dddd')); 2451cb0ef41Sopenharmony_ci pt.write(Buffer.from('eeee')); 2461cb0ef41Sopenharmony_ci pt.write(Buffer.from('aaaa')); 2471cb0ef41Sopenharmony_ci pt.write(Buffer.from('bbbb')); 2481cb0ef41Sopenharmony_ci pt.write(Buffer.from('cccc')); 2491cb0ef41Sopenharmony_ci pt.write(Buffer.from('dddd')); 2501cb0ef41Sopenharmony_ci pt.end(); 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci // 'abcdeabcdeabcd' 2531cb0ef41Sopenharmony_ci pt.on('finish', common.mustCall(function() { 2541cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'abcde'); 2551cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'abcde'); 2561cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'abcd'); 2571cb0ef41Sopenharmony_ci })); 2581cb0ef41Sopenharmony_ci} 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci// This tests for a stall when data is written to a full stream 2611cb0ef41Sopenharmony_ci// that has empty transforms. 2621cb0ef41Sopenharmony_ci{ 2631cb0ef41Sopenharmony_ci // Verify complex transform behavior 2641cb0ef41Sopenharmony_ci let count = 0; 2651cb0ef41Sopenharmony_ci let saved = null; 2661cb0ef41Sopenharmony_ci const pt = new Transform({ highWaterMark: 3 }); 2671cb0ef41Sopenharmony_ci pt._transform = function(c, e, cb) { 2681cb0ef41Sopenharmony_ci if (count++ === 1) 2691cb0ef41Sopenharmony_ci saved = c; 2701cb0ef41Sopenharmony_ci else { 2711cb0ef41Sopenharmony_ci if (saved) { 2721cb0ef41Sopenharmony_ci pt.push(saved); 2731cb0ef41Sopenharmony_ci saved = null; 2741cb0ef41Sopenharmony_ci } 2751cb0ef41Sopenharmony_ci pt.push(c); 2761cb0ef41Sopenharmony_ci } 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_ci cb(); 2791cb0ef41Sopenharmony_ci }; 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci pt.once('readable', function() { 2821cb0ef41Sopenharmony_ci process.nextTick(function() { 2831cb0ef41Sopenharmony_ci pt.write(Buffer.from('d')); 2841cb0ef41Sopenharmony_ci pt.write(Buffer.from('ef'), common.mustCall(function() { 2851cb0ef41Sopenharmony_ci pt.end(); 2861cb0ef41Sopenharmony_ci })); 2871cb0ef41Sopenharmony_ci assert.strictEqual(pt.read().toString(), 'abcdef'); 2881cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(), null); 2891cb0ef41Sopenharmony_ci }); 2901cb0ef41Sopenharmony_ci }); 2911cb0ef41Sopenharmony_ci 2921cb0ef41Sopenharmony_ci pt.write(Buffer.from('abc')); 2931cb0ef41Sopenharmony_ci} 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci{ 2971cb0ef41Sopenharmony_ci // Verify passthrough event emission 2981cb0ef41Sopenharmony_ci const pt = new PassThrough(); 2991cb0ef41Sopenharmony_ci let emits = 0; 3001cb0ef41Sopenharmony_ci pt.on('readable', function() { 3011cb0ef41Sopenharmony_ci emits++; 3021cb0ef41Sopenharmony_ci }); 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 3051cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci assert.strictEqual(emits, 0); 3081cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'foogb'); 3091cb0ef41Sopenharmony_ci assert.strictEqual(String(pt.read(5)), 'null'); 3101cb0ef41Sopenharmony_ci assert.strictEqual(emits, 0); 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 3131cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ci assert.strictEqual(emits, 0); 3161cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'arkba'); 3171cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'zykue'); 3181cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5), null); 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci pt.end(); 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci assert.strictEqual(emits, 1); 3231cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'l'); 3241cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5), null); 3251cb0ef41Sopenharmony_ci assert.strictEqual(emits, 1); 3261cb0ef41Sopenharmony_ci} 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci{ 3291cb0ef41Sopenharmony_ci // Verify passthrough event emission reordering 3301cb0ef41Sopenharmony_ci const pt = new PassThrough(); 3311cb0ef41Sopenharmony_ci let emits = 0; 3321cb0ef41Sopenharmony_ci pt.on('readable', function() { 3331cb0ef41Sopenharmony_ci emits++; 3341cb0ef41Sopenharmony_ci }); 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 3371cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ci assert.strictEqual(emits, 0); 3401cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'foogb'); 3411cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5), null); 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ci pt.once('readable', common.mustCall(function() { 3441cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'arkba'); 3451cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5), null); 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ci pt.once('readable', common.mustCall(function() { 3481cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'zykue'); 3491cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5), null); 3501cb0ef41Sopenharmony_ci pt.once('readable', common.mustCall(function() { 3511cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5).toString(), 'l'); 3521cb0ef41Sopenharmony_ci assert.strictEqual(pt.read(5), null); 3531cb0ef41Sopenharmony_ci assert.strictEqual(emits, 3); 3541cb0ef41Sopenharmony_ci })); 3551cb0ef41Sopenharmony_ci pt.end(); 3561cb0ef41Sopenharmony_ci })); 3571cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 3581cb0ef41Sopenharmony_ci })); 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 3611cb0ef41Sopenharmony_ci} 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci{ 3641cb0ef41Sopenharmony_ci // Verify passthrough facade 3651cb0ef41Sopenharmony_ci const pt = new PassThrough(); 3661cb0ef41Sopenharmony_ci const datas = []; 3671cb0ef41Sopenharmony_ci pt.on('data', function(chunk) { 3681cb0ef41Sopenharmony_ci datas.push(chunk.toString()); 3691cb0ef41Sopenharmony_ci }); 3701cb0ef41Sopenharmony_ci 3711cb0ef41Sopenharmony_ci pt.on('end', common.mustCall(function() { 3721cb0ef41Sopenharmony_ci assert.deepStrictEqual(datas, ['foog', 'bark', 'bazy', 'kuel']); 3731cb0ef41Sopenharmony_ci })); 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ci pt.write(Buffer.from('foog')); 3761cb0ef41Sopenharmony_ci setTimeout(function() { 3771cb0ef41Sopenharmony_ci pt.write(Buffer.from('bark')); 3781cb0ef41Sopenharmony_ci setTimeout(function() { 3791cb0ef41Sopenharmony_ci pt.write(Buffer.from('bazy')); 3801cb0ef41Sopenharmony_ci setTimeout(function() { 3811cb0ef41Sopenharmony_ci pt.write(Buffer.from('kuel')); 3821cb0ef41Sopenharmony_ci setTimeout(function() { 3831cb0ef41Sopenharmony_ci pt.end(); 3841cb0ef41Sopenharmony_ci }, 10); 3851cb0ef41Sopenharmony_ci }, 10); 3861cb0ef41Sopenharmony_ci }, 10); 3871cb0ef41Sopenharmony_ci }, 10); 3881cb0ef41Sopenharmony_ci} 3891cb0ef41Sopenharmony_ci 3901cb0ef41Sopenharmony_ci{ 3911cb0ef41Sopenharmony_ci // Verify object transform (JSON parse) 3921cb0ef41Sopenharmony_ci const jp = new Transform({ objectMode: true }); 3931cb0ef41Sopenharmony_ci jp._transform = function(data, encoding, cb) { 3941cb0ef41Sopenharmony_ci try { 3951cb0ef41Sopenharmony_ci jp.push(JSON.parse(data)); 3961cb0ef41Sopenharmony_ci cb(); 3971cb0ef41Sopenharmony_ci } catch (er) { 3981cb0ef41Sopenharmony_ci cb(er); 3991cb0ef41Sopenharmony_ci } 4001cb0ef41Sopenharmony_ci }; 4011cb0ef41Sopenharmony_ci 4021cb0ef41Sopenharmony_ci // Anything except null/undefined is fine. 4031cb0ef41Sopenharmony_ci // those are "magic" in the stream API, because they signal EOF. 4041cb0ef41Sopenharmony_ci const objects = [ 4051cb0ef41Sopenharmony_ci { foo: 'bar' }, 4061cb0ef41Sopenharmony_ci 100, 4071cb0ef41Sopenharmony_ci 'string', 4081cb0ef41Sopenharmony_ci { nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }, 4091cb0ef41Sopenharmony_ci ]; 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci let ended = false; 4121cb0ef41Sopenharmony_ci jp.on('end', function() { 4131cb0ef41Sopenharmony_ci ended = true; 4141cb0ef41Sopenharmony_ci }); 4151cb0ef41Sopenharmony_ci 4161cb0ef41Sopenharmony_ci objects.forEach(function(obj) { 4171cb0ef41Sopenharmony_ci jp.write(JSON.stringify(obj)); 4181cb0ef41Sopenharmony_ci const res = jp.read(); 4191cb0ef41Sopenharmony_ci assert.deepStrictEqual(res, obj); 4201cb0ef41Sopenharmony_ci }); 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ci jp.end(); 4231cb0ef41Sopenharmony_ci // Read one more time to get the 'end' event 4241cb0ef41Sopenharmony_ci jp.read(); 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci process.nextTick(common.mustCall(function() { 4271cb0ef41Sopenharmony_ci assert.strictEqual(ended, true); 4281cb0ef41Sopenharmony_ci })); 4291cb0ef41Sopenharmony_ci} 4301cb0ef41Sopenharmony_ci 4311cb0ef41Sopenharmony_ci{ 4321cb0ef41Sopenharmony_ci // Verify object transform (JSON stringify) 4331cb0ef41Sopenharmony_ci const js = new Transform({ objectMode: true }); 4341cb0ef41Sopenharmony_ci js._transform = function(data, encoding, cb) { 4351cb0ef41Sopenharmony_ci try { 4361cb0ef41Sopenharmony_ci js.push(JSON.stringify(data)); 4371cb0ef41Sopenharmony_ci cb(); 4381cb0ef41Sopenharmony_ci } catch (er) { 4391cb0ef41Sopenharmony_ci cb(er); 4401cb0ef41Sopenharmony_ci } 4411cb0ef41Sopenharmony_ci }; 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci // Anything except null/undefined is fine. 4441cb0ef41Sopenharmony_ci // those are "magic" in the stream API, because they signal EOF. 4451cb0ef41Sopenharmony_ci const objects = [ 4461cb0ef41Sopenharmony_ci { foo: 'bar' }, 4471cb0ef41Sopenharmony_ci 100, 4481cb0ef41Sopenharmony_ci 'string', 4491cb0ef41Sopenharmony_ci { nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }, 4501cb0ef41Sopenharmony_ci ]; 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci let ended = false; 4531cb0ef41Sopenharmony_ci js.on('end', function() { 4541cb0ef41Sopenharmony_ci ended = true; 4551cb0ef41Sopenharmony_ci }); 4561cb0ef41Sopenharmony_ci 4571cb0ef41Sopenharmony_ci objects.forEach(function(obj) { 4581cb0ef41Sopenharmony_ci js.write(obj); 4591cb0ef41Sopenharmony_ci const res = js.read(); 4601cb0ef41Sopenharmony_ci assert.strictEqual(res, JSON.stringify(obj)); 4611cb0ef41Sopenharmony_ci }); 4621cb0ef41Sopenharmony_ci 4631cb0ef41Sopenharmony_ci js.end(); 4641cb0ef41Sopenharmony_ci // Read one more time to get the 'end' event 4651cb0ef41Sopenharmony_ci js.read(); 4661cb0ef41Sopenharmony_ci 4671cb0ef41Sopenharmony_ci process.nextTick(common.mustCall(function() { 4681cb0ef41Sopenharmony_ci assert.strictEqual(ended, true); 4691cb0ef41Sopenharmony_ci })); 4701cb0ef41Sopenharmony_ci} 4711cb0ef41Sopenharmony_ci 4721cb0ef41Sopenharmony_ci{ 4731cb0ef41Sopenharmony_ci const s = new Transform({ 4741cb0ef41Sopenharmony_ci objectMode: true, 4751cb0ef41Sopenharmony_ci construct(callback) { 4761cb0ef41Sopenharmony_ci this.push('header from constructor'); 4771cb0ef41Sopenharmony_ci callback(); 4781cb0ef41Sopenharmony_ci }, 4791cb0ef41Sopenharmony_ci transform: (row, encoding, callback) => { 4801cb0ef41Sopenharmony_ci callback(null, row); 4811cb0ef41Sopenharmony_ci }, 4821cb0ef41Sopenharmony_ci }); 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ci const expected = [ 4851cb0ef41Sopenharmony_ci 'header from constructor', 4861cb0ef41Sopenharmony_ci 'firstLine', 4871cb0ef41Sopenharmony_ci 'secondLine', 4881cb0ef41Sopenharmony_ci ]; 4891cb0ef41Sopenharmony_ci s.on('data', common.mustCall((data) => { 4901cb0ef41Sopenharmony_ci assert.strictEqual(data.toString(), expected.shift()); 4911cb0ef41Sopenharmony_ci }, 3)); 4921cb0ef41Sopenharmony_ci s.write('firstLine'); 4931cb0ef41Sopenharmony_ci process.nextTick(() => s.write('secondLine')); 4941cb0ef41Sopenharmony_ci} 495