11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { mustCall } = require('../common'); 41cb0ef41Sopenharmony_ciconst { once } = require('events'); 51cb0ef41Sopenharmony_ciconst { Readable } = require('stream'); 61cb0ef41Sopenharmony_ciconst { strictEqual, throws } = require('assert'); 71cb0ef41Sopenharmony_ciconst common = require('../common'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci{ 101cb0ef41Sopenharmony_ci throws(() => { 111cb0ef41Sopenharmony_ci Readable.from(null); 121cb0ef41Sopenharmony_ci }, /ERR_INVALID_ARG_TYPE/); 131cb0ef41Sopenharmony_ci} 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciasync function toReadableBasicSupport() { 161cb0ef41Sopenharmony_ci async function* generate() { 171cb0ef41Sopenharmony_ci yield 'a'; 181cb0ef41Sopenharmony_ci yield 'b'; 191cb0ef41Sopenharmony_ci yield 'c'; 201cb0ef41Sopenharmony_ci } 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci const stream = Readable.from(generate()); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci const expected = ['a', 'b', 'c']; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci for await (const chunk of stream) { 271cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciasync function toReadableSyncIterator() { 321cb0ef41Sopenharmony_ci function* generate() { 331cb0ef41Sopenharmony_ci yield 'a'; 341cb0ef41Sopenharmony_ci yield 'b'; 351cb0ef41Sopenharmony_ci yield 'c'; 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci const stream = Readable.from(generate()); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci const expected = ['a', 'b', 'c']; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci for await (const chunk of stream) { 431cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciasync function toReadablePromises() { 481cb0ef41Sopenharmony_ci const promises = [ 491cb0ef41Sopenharmony_ci Promise.resolve('a'), 501cb0ef41Sopenharmony_ci Promise.resolve('b'), 511cb0ef41Sopenharmony_ci Promise.resolve('c'), 521cb0ef41Sopenharmony_ci ]; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci const stream = Readable.from(promises); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci const expected = ['a', 'b', 'c']; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci for await (const chunk of stream) { 591cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciasync function toReadableString() { 641cb0ef41Sopenharmony_ci const stream = Readable.from('abc'); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci const expected = ['abc']; 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci for await (const chunk of stream) { 691cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciasync function toReadableBuffer() { 741cb0ef41Sopenharmony_ci const stream = Readable.from(Buffer.from('abc')); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci const expected = ['abc']; 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci for await (const chunk of stream) { 791cb0ef41Sopenharmony_ci strictEqual(chunk.toString(), expected.shift()); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ciasync function toReadableOnData() { 841cb0ef41Sopenharmony_ci async function* generate() { 851cb0ef41Sopenharmony_ci yield 'a'; 861cb0ef41Sopenharmony_ci yield 'b'; 871cb0ef41Sopenharmony_ci yield 'c'; 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci const stream = Readable.from(generate()); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci let iterations = 0; 931cb0ef41Sopenharmony_ci const expected = ['a', 'b', 'c']; 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci stream.on('data', (chunk) => { 961cb0ef41Sopenharmony_ci iterations++; 971cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 981cb0ef41Sopenharmony_ci }); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci await once(stream, 'end'); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci strictEqual(iterations, 3); 1031cb0ef41Sopenharmony_ci} 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ciasync function toReadableOnDataNonObject() { 1061cb0ef41Sopenharmony_ci async function* generate() { 1071cb0ef41Sopenharmony_ci yield 'a'; 1081cb0ef41Sopenharmony_ci yield 'b'; 1091cb0ef41Sopenharmony_ci yield 'c'; 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci const stream = Readable.from(generate(), { objectMode: false }); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci let iterations = 0; 1151cb0ef41Sopenharmony_ci const expected = ['a', 'b', 'c']; 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci stream.on('data', (chunk) => { 1181cb0ef41Sopenharmony_ci iterations++; 1191cb0ef41Sopenharmony_ci strictEqual(chunk instanceof Buffer, true); 1201cb0ef41Sopenharmony_ci strictEqual(chunk.toString(), expected.shift()); 1211cb0ef41Sopenharmony_ci }); 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci await once(stream, 'end'); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci strictEqual(iterations, 3); 1261cb0ef41Sopenharmony_ci} 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ciasync function destroysTheStreamWhenThrowing() { 1291cb0ef41Sopenharmony_ci async function* generate() { // eslint-disable-line require-yield 1301cb0ef41Sopenharmony_ci throw new Error('kaboom'); 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci const stream = Readable.from(generate()); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci stream.read(); 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci const [err] = await once(stream, 'error'); 1381cb0ef41Sopenharmony_ci strictEqual(err.message, 'kaboom'); 1391cb0ef41Sopenharmony_ci strictEqual(stream.destroyed, true); 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci} 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ciasync function asTransformStream() { 1441cb0ef41Sopenharmony_ci async function* generate(stream) { 1451cb0ef41Sopenharmony_ci for await (const chunk of stream) { 1461cb0ef41Sopenharmony_ci yield chunk.toUpperCase(); 1471cb0ef41Sopenharmony_ci } 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci const source = new Readable({ 1511cb0ef41Sopenharmony_ci objectMode: true, 1521cb0ef41Sopenharmony_ci read() { 1531cb0ef41Sopenharmony_ci this.push('a'); 1541cb0ef41Sopenharmony_ci this.push('b'); 1551cb0ef41Sopenharmony_ci this.push('c'); 1561cb0ef41Sopenharmony_ci this.push(null); 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci }); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci const stream = Readable.from(generate(source)); 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci const expected = ['A', 'B', 'C']; 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci for await (const chunk of stream) { 1651cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 1661cb0ef41Sopenharmony_ci } 1671cb0ef41Sopenharmony_ci} 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ciasync function endWithError() { 1701cb0ef41Sopenharmony_ci async function* generate() { 1711cb0ef41Sopenharmony_ci yield 1; 1721cb0ef41Sopenharmony_ci yield 2; 1731cb0ef41Sopenharmony_ci yield Promise.reject('Boum'); 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci const stream = Readable.from(generate()); 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci const expected = [1, 2]; 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci try { 1811cb0ef41Sopenharmony_ci for await (const chunk of stream) { 1821cb0ef41Sopenharmony_ci strictEqual(chunk, expected.shift()); 1831cb0ef41Sopenharmony_ci } 1841cb0ef41Sopenharmony_ci throw new Error(); 1851cb0ef41Sopenharmony_ci } catch (err) { 1861cb0ef41Sopenharmony_ci strictEqual(expected.length, 0); 1871cb0ef41Sopenharmony_ci strictEqual(err, 'Boum'); 1881cb0ef41Sopenharmony_ci } 1891cb0ef41Sopenharmony_ci} 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ciasync function destroyingStreamWithErrorThrowsInGenerator() { 1921cb0ef41Sopenharmony_ci const validateError = common.mustCall((e) => { 1931cb0ef41Sopenharmony_ci strictEqual(e, 'Boum'); 1941cb0ef41Sopenharmony_ci }); 1951cb0ef41Sopenharmony_ci async function* generate() { 1961cb0ef41Sopenharmony_ci try { 1971cb0ef41Sopenharmony_ci yield 1; 1981cb0ef41Sopenharmony_ci yield 2; 1991cb0ef41Sopenharmony_ci yield 3; 2001cb0ef41Sopenharmony_ci throw new Error(); 2011cb0ef41Sopenharmony_ci } catch (e) { 2021cb0ef41Sopenharmony_ci validateError(e); 2031cb0ef41Sopenharmony_ci } 2041cb0ef41Sopenharmony_ci } 2051cb0ef41Sopenharmony_ci const stream = Readable.from(generate()); 2061cb0ef41Sopenharmony_ci stream.read(); 2071cb0ef41Sopenharmony_ci stream.once('error', common.mustCall()); 2081cb0ef41Sopenharmony_ci stream.destroy('Boum'); 2091cb0ef41Sopenharmony_ci} 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciPromise.all([ 2121cb0ef41Sopenharmony_ci toReadableBasicSupport(), 2131cb0ef41Sopenharmony_ci toReadableSyncIterator(), 2141cb0ef41Sopenharmony_ci toReadablePromises(), 2151cb0ef41Sopenharmony_ci toReadableString(), 2161cb0ef41Sopenharmony_ci toReadableBuffer(), 2171cb0ef41Sopenharmony_ci toReadableOnData(), 2181cb0ef41Sopenharmony_ci toReadableOnDataNonObject(), 2191cb0ef41Sopenharmony_ci destroysTheStreamWhenThrowing(), 2201cb0ef41Sopenharmony_ci asTransformStream(), 2211cb0ef41Sopenharmony_ci endWithError(), 2221cb0ef41Sopenharmony_ci destroyingStreamWithErrorThrowsInGenerator(), 2231cb0ef41Sopenharmony_ci]).then(mustCall()); 224