11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst { 51cb0ef41Sopenharmony_ci Readable, 61cb0ef41Sopenharmony_ci} = require('stream'); 71cb0ef41Sopenharmony_ciconst assert = require('assert'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci{ 101cb0ef41Sopenharmony_ci // Works on a synchronous stream 111cb0ef41Sopenharmony_ci (async () => { 121cb0ef41Sopenharmony_ci const tests = [ 131cb0ef41Sopenharmony_ci [], 141cb0ef41Sopenharmony_ci [1], 151cb0ef41Sopenharmony_ci [1, 2, 3], 161cb0ef41Sopenharmony_ci Array(100).fill().map((_, i) => i), 171cb0ef41Sopenharmony_ci ]; 181cb0ef41Sopenharmony_ci for (const test of tests) { 191cb0ef41Sopenharmony_ci const stream = Readable.from(test); 201cb0ef41Sopenharmony_ci const result = await stream.toArray(); 211cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, test); 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci })().then(common.mustCall()); 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci{ 271cb0ef41Sopenharmony_ci // Works on a non-object-mode stream 281cb0ef41Sopenharmony_ci (async () => { 291cb0ef41Sopenharmony_ci const firstBuffer = Buffer.from([1, 2, 3]); 301cb0ef41Sopenharmony_ci const secondBuffer = Buffer.from([4, 5, 6]); 311cb0ef41Sopenharmony_ci const stream = Readable.from( 321cb0ef41Sopenharmony_ci [firstBuffer, secondBuffer], 331cb0ef41Sopenharmony_ci { objectMode: false }); 341cb0ef41Sopenharmony_ci const result = await stream.toArray(); 351cb0ef41Sopenharmony_ci assert.strictEqual(Array.isArray(result), true); 361cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, [firstBuffer, secondBuffer]); 371cb0ef41Sopenharmony_ci })().then(common.mustCall()); 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci{ 411cb0ef41Sopenharmony_ci // Works on an asynchronous stream 421cb0ef41Sopenharmony_ci (async () => { 431cb0ef41Sopenharmony_ci const tests = [ 441cb0ef41Sopenharmony_ci [], 451cb0ef41Sopenharmony_ci [1], 461cb0ef41Sopenharmony_ci [1, 2, 3], 471cb0ef41Sopenharmony_ci Array(100).fill().map((_, i) => i), 481cb0ef41Sopenharmony_ci ]; 491cb0ef41Sopenharmony_ci for (const test of tests) { 501cb0ef41Sopenharmony_ci const stream = Readable.from(test).map((x) => Promise.resolve(x)); 511cb0ef41Sopenharmony_ci const result = await stream.toArray(); 521cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, test); 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci })().then(common.mustCall()); 551cb0ef41Sopenharmony_ci} 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci{ 581cb0ef41Sopenharmony_ci // Support for AbortSignal 591cb0ef41Sopenharmony_ci const ac = new AbortController(); 601cb0ef41Sopenharmony_ci let stream; 611cb0ef41Sopenharmony_ci assert.rejects(async () => { 621cb0ef41Sopenharmony_ci stream = Readable.from([1, 2, 3]).map(async (x) => { 631cb0ef41Sopenharmony_ci if (x === 3) { 641cb0ef41Sopenharmony_ci await new Promise(() => {}); // Explicitly do not pass signal here 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci return Promise.resolve(x); 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci await stream.toArray({ signal: ac.signal }); 691cb0ef41Sopenharmony_ci }, { 701cb0ef41Sopenharmony_ci name: 'AbortError', 711cb0ef41Sopenharmony_ci }).then(common.mustCall(() => { 721cb0ef41Sopenharmony_ci // Only stops toArray, does not destroy the stream 731cb0ef41Sopenharmony_ci assert(stream.destroyed, false); 741cb0ef41Sopenharmony_ci })); 751cb0ef41Sopenharmony_ci ac.abort(); 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci{ 781cb0ef41Sopenharmony_ci // Test result is a Promise 791cb0ef41Sopenharmony_ci const result = Readable.from([1, 2, 3, 4, 5]).toArray(); 801cb0ef41Sopenharmony_ci assert.strictEqual(result instanceof Promise, true); 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci{ 831cb0ef41Sopenharmony_ci // Error cases 841cb0ef41Sopenharmony_ci assert.rejects(async () => { 851cb0ef41Sopenharmony_ci await Readable.from([1]).toArray(1); 861cb0ef41Sopenharmony_ci }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci assert.rejects(async () => { 891cb0ef41Sopenharmony_ci await Readable.from([1]).toArray({ 901cb0ef41Sopenharmony_ci signal: true 911cb0ef41Sopenharmony_ci }); 921cb0ef41Sopenharmony_ci }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); 931cb0ef41Sopenharmony_ci} 94