11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst { ReadableStream, WritableStream } = require('stream/web'); 61cb0ef41Sopenharmony_ciconst { finished } = require('stream'); 71cb0ef41Sopenharmony_ciconst { finished: finishedPromise } = require('stream/promises'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci{ 101cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 111cb0ef41Sopenharmony_ci start(controller) { 121cb0ef41Sopenharmony_ci controller.enqueue('asd'); 131cb0ef41Sopenharmony_ci controller.close(); 141cb0ef41Sopenharmony_ci }, 151cb0ef41Sopenharmony_ci }); 161cb0ef41Sopenharmony_ci finished(rs, common.mustSucceed()); 171cb0ef41Sopenharmony_ci async function test() { 181cb0ef41Sopenharmony_ci const values = []; 191cb0ef41Sopenharmony_ci for await (const chunk of rs) { 201cb0ef41Sopenharmony_ci values.push(chunk); 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci assert.deepStrictEqual(values, ['asd']); 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci test(); 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci{ 281cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 291cb0ef41Sopenharmony_ci start(controller) { 301cb0ef41Sopenharmony_ci controller.error(new Error('asd')); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci }); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci finished(rs, common.mustCall((err) => { 351cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 361cb0ef41Sopenharmony_ci })); 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci{ 401cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 411cb0ef41Sopenharmony_ci async start(controller) { 421cb0ef41Sopenharmony_ci throw new Error('asd'); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci finished(rs, common.mustCall((err) => { 471cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 481cb0ef41Sopenharmony_ci })); 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci{ 521cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 531cb0ef41Sopenharmony_ci start(controller) { 541cb0ef41Sopenharmony_ci controller.enqueue('asd'); 551cb0ef41Sopenharmony_ci controller.close(); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci }); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci async function test() { 601cb0ef41Sopenharmony_ci const values = []; 611cb0ef41Sopenharmony_ci for await (const chunk of rs) { 621cb0ef41Sopenharmony_ci values.push(chunk); 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci assert.deepStrictEqual(values, ['asd']); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci finishedPromise(rs).then(common.mustSucceed()); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci test(); 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci{ 731cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 741cb0ef41Sopenharmony_ci start(controller) { 751cb0ef41Sopenharmony_ci controller.error(new Error('asd')); 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci }); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci finishedPromise(rs).then(common.mustNotCall()).catch(common.mustCall((err) => { 801cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 811cb0ef41Sopenharmony_ci })); 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci{ 851cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 861cb0ef41Sopenharmony_ci async start(controller) { 871cb0ef41Sopenharmony_ci throw new Error('asd'); 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci }); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci finishedPromise(rs).then(common.mustNotCall()).catch(common.mustCall((err) => { 921cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 931cb0ef41Sopenharmony_ci })); 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci{ 971cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 981cb0ef41Sopenharmony_ci start(controller) { 991cb0ef41Sopenharmony_ci controller.enqueue('asd'); 1001cb0ef41Sopenharmony_ci controller.close(); 1011cb0ef41Sopenharmony_ci } 1021cb0ef41Sopenharmony_ci }); 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci const { 0: s1, 1: s2 } = rs.tee(); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci finished(s1, common.mustSucceed()); 1071cb0ef41Sopenharmony_ci finished(s2, common.mustSucceed()); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci async function test(stream) { 1101cb0ef41Sopenharmony_ci const values = []; 1111cb0ef41Sopenharmony_ci for await (const chunk of stream) { 1121cb0ef41Sopenharmony_ci values.push(chunk); 1131cb0ef41Sopenharmony_ci } 1141cb0ef41Sopenharmony_ci assert.deepStrictEqual(values, ['asd']); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci Promise.all([ 1181cb0ef41Sopenharmony_ci test(s1), 1191cb0ef41Sopenharmony_ci test(s2), 1201cb0ef41Sopenharmony_ci ]).then(common.mustCall()); 1211cb0ef41Sopenharmony_ci} 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci{ 1241cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 1251cb0ef41Sopenharmony_ci start(controller) { 1261cb0ef41Sopenharmony_ci controller.error(new Error('asd')); 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci }); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci const { 0: s1, 1: s2 } = rs.tee(); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci finished(s1, common.mustCall((err) => { 1331cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 1341cb0ef41Sopenharmony_ci })); 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci finished(s2, common.mustCall((err) => { 1371cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 1381cb0ef41Sopenharmony_ci })); 1391cb0ef41Sopenharmony_ci} 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci{ 1421cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 1431cb0ef41Sopenharmony_ci start(controller) { 1441cb0ef41Sopenharmony_ci controller.enqueue('asd'); 1451cb0ef41Sopenharmony_ci controller.close(); 1461cb0ef41Sopenharmony_ci } 1471cb0ef41Sopenharmony_ci }); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci finished(rs, common.mustSucceed()); 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci rs.cancel(); 1521cb0ef41Sopenharmony_ci} 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci{ 1551cb0ef41Sopenharmony_ci let str = ''; 1561cb0ef41Sopenharmony_ci const ws = new WritableStream({ 1571cb0ef41Sopenharmony_ci write(chunk) { 1581cb0ef41Sopenharmony_ci str += chunk; 1591cb0ef41Sopenharmony_ci } 1601cb0ef41Sopenharmony_ci }); 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci finished(ws, common.mustSucceed(() => { 1631cb0ef41Sopenharmony_ci assert.strictEqual(str, 'asd'); 1641cb0ef41Sopenharmony_ci })); 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci const writer = ws.getWriter(); 1671cb0ef41Sopenharmony_ci writer.write('asd'); 1681cb0ef41Sopenharmony_ci writer.close(); 1691cb0ef41Sopenharmony_ci} 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci{ 1721cb0ef41Sopenharmony_ci const ws = new WritableStream({ 1731cb0ef41Sopenharmony_ci async write(chunk) { 1741cb0ef41Sopenharmony_ci throw new Error('asd'); 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci }); 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci finished(ws, common.mustCall((err) => { 1791cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 1801cb0ef41Sopenharmony_ci })); 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci const writer = ws.getWriter(); 1831cb0ef41Sopenharmony_ci writer.write('asd').catch((err) => { 1841cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 1851cb0ef41Sopenharmony_ci }); 1861cb0ef41Sopenharmony_ci} 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci{ 1891cb0ef41Sopenharmony_ci let str = ''; 1901cb0ef41Sopenharmony_ci const ws = new WritableStream({ 1911cb0ef41Sopenharmony_ci write(chunk) { 1921cb0ef41Sopenharmony_ci str += chunk; 1931cb0ef41Sopenharmony_ci } 1941cb0ef41Sopenharmony_ci }); 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci finishedPromise(ws).then(common.mustSucceed(() => { 1971cb0ef41Sopenharmony_ci assert.strictEqual(str, 'asd'); 1981cb0ef41Sopenharmony_ci })); 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci const writer = ws.getWriter(); 2011cb0ef41Sopenharmony_ci writer.write('asd'); 2021cb0ef41Sopenharmony_ci writer.close(); 2031cb0ef41Sopenharmony_ci} 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci{ 2061cb0ef41Sopenharmony_ci const ws = new WritableStream({ 2071cb0ef41Sopenharmony_ci write(chunk) { } 2081cb0ef41Sopenharmony_ci }); 2091cb0ef41Sopenharmony_ci finished(ws, common.mustCall((err) => { 2101cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 2111cb0ef41Sopenharmony_ci })); 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci const writer = ws.getWriter(); 2141cb0ef41Sopenharmony_ci writer.abort(new Error('asd')); 2151cb0ef41Sopenharmony_ci} 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci{ 2181cb0ef41Sopenharmony_ci const ws = new WritableStream({ 2191cb0ef41Sopenharmony_ci async write(chunk) { 2201cb0ef41Sopenharmony_ci throw new Error('asd'); 2211cb0ef41Sopenharmony_ci } 2221cb0ef41Sopenharmony_ci }); 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci finishedPromise(ws).then(common.mustNotCall()).catch(common.mustCall((err) => { 2251cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 2261cb0ef41Sopenharmony_ci })); 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci const writer = ws.getWriter(); 2291cb0ef41Sopenharmony_ci writer.write('asd').catch((err) => { 2301cb0ef41Sopenharmony_ci assert.strictEqual(err?.message, 'asd'); 2311cb0ef41Sopenharmony_ci }); 2321cb0ef41Sopenharmony_ci} 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci{ 2351cb0ef41Sopenharmony_ci // Check pre-cancelled 2361cb0ef41Sopenharmony_ci const signal = new EventTarget(); 2371cb0ef41Sopenharmony_ci signal.aborted = true; 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 2401cb0ef41Sopenharmony_ci start() {} 2411cb0ef41Sopenharmony_ci }); 2421cb0ef41Sopenharmony_ci finished(rs, { signal }, common.mustCall((err) => { 2431cb0ef41Sopenharmony_ci assert.strictEqual(err.name, 'AbortError'); 2441cb0ef41Sopenharmony_ci })); 2451cb0ef41Sopenharmony_ci} 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ci{ 2481cb0ef41Sopenharmony_ci // Check cancelled before the stream ends sync. 2491cb0ef41Sopenharmony_ci const ac = new AbortController(); 2501cb0ef41Sopenharmony_ci const { signal } = ac; 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 2531cb0ef41Sopenharmony_ci start() {} 2541cb0ef41Sopenharmony_ci }); 2551cb0ef41Sopenharmony_ci finished(rs, { signal }, common.mustCall((err) => { 2561cb0ef41Sopenharmony_ci assert.strictEqual(err.name, 'AbortError'); 2571cb0ef41Sopenharmony_ci })); 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ci ac.abort(); 2601cb0ef41Sopenharmony_ci} 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci{ 2631cb0ef41Sopenharmony_ci // Check cancelled before the stream ends async. 2641cb0ef41Sopenharmony_ci const ac = new AbortController(); 2651cb0ef41Sopenharmony_ci const { signal } = ac; 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 2681cb0ef41Sopenharmony_ci start() {} 2691cb0ef41Sopenharmony_ci }); 2701cb0ef41Sopenharmony_ci setTimeout(() => ac.abort(), 1); 2711cb0ef41Sopenharmony_ci finished(rs, { signal }, common.mustCall((err) => { 2721cb0ef41Sopenharmony_ci assert.strictEqual(err.name, 'AbortError'); 2731cb0ef41Sopenharmony_ci })); 2741cb0ef41Sopenharmony_ci} 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci{ 2771cb0ef41Sopenharmony_ci // Check cancelled after doesn't throw. 2781cb0ef41Sopenharmony_ci const ac = new AbortController(); 2791cb0ef41Sopenharmony_ci const { signal } = ac; 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 2821cb0ef41Sopenharmony_ci start(controller) { 2831cb0ef41Sopenharmony_ci controller.enqueue('asd'); 2841cb0ef41Sopenharmony_ci controller.close(); 2851cb0ef41Sopenharmony_ci } 2861cb0ef41Sopenharmony_ci }); 2871cb0ef41Sopenharmony_ci finished(rs, { signal }, common.mustSucceed()); 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci rs.getReader().read().then(common.mustCall((chunk) => { 2901cb0ef41Sopenharmony_ci assert.strictEqual(chunk.value, 'asd'); 2911cb0ef41Sopenharmony_ci setImmediate(() => ac.abort()); 2921cb0ef41Sopenharmony_ci })); 2931cb0ef41Sopenharmony_ci} 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci{ 2961cb0ef41Sopenharmony_ci // Promisified abort works 2971cb0ef41Sopenharmony_ci async function run() { 2981cb0ef41Sopenharmony_ci const ac = new AbortController(); 2991cb0ef41Sopenharmony_ci const { signal } = ac; 3001cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 3011cb0ef41Sopenharmony_ci start() {} 3021cb0ef41Sopenharmony_ci }); 3031cb0ef41Sopenharmony_ci setImmediate(() => ac.abort()); 3041cb0ef41Sopenharmony_ci await finishedPromise(rs, { signal }); 3051cb0ef41Sopenharmony_ci } 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); 3081cb0ef41Sopenharmony_ci} 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci{ 3111cb0ef41Sopenharmony_ci // Promisified pre-aborted works 3121cb0ef41Sopenharmony_ci async function run() { 3131cb0ef41Sopenharmony_ci const signal = new EventTarget(); 3141cb0ef41Sopenharmony_ci signal.aborted = true; 3151cb0ef41Sopenharmony_ci const rs = new ReadableStream({ 3161cb0ef41Sopenharmony_ci start() {} 3171cb0ef41Sopenharmony_ci }); 3181cb0ef41Sopenharmony_ci await finishedPromise(rs, { signal }); 3191cb0ef41Sopenharmony_ci } 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); 3221cb0ef41Sopenharmony_ci} 323