11cb0ef41Sopenharmony_ci// Flags: --no-warnings --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst { 91cb0ef41Sopenharmony_ci WritableStream, 101cb0ef41Sopenharmony_ci} = require('stream/web'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst { 131cb0ef41Sopenharmony_ci newStreamWritableFromWritableStream, 141cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst { 171cb0ef41Sopenharmony_ci finished, 181cb0ef41Sopenharmony_ci pipeline, 191cb0ef41Sopenharmony_ci Readable, 201cb0ef41Sopenharmony_ci} = require('stream'); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst { 231cb0ef41Sopenharmony_ci kState, 241cb0ef41Sopenharmony_ci} = require('internal/webstreams/util'); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciclass TestSource { 271cb0ef41Sopenharmony_ci constructor() { 281cb0ef41Sopenharmony_ci this.chunks = []; 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci start(c) { 321cb0ef41Sopenharmony_ci this.controller = c; 331cb0ef41Sopenharmony_ci this.started = true; 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci write(chunk) { 371cb0ef41Sopenharmony_ci this.chunks.push(chunk); 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci close() { 411cb0ef41Sopenharmony_ci this.closed = true; 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci abort(reason) { 451cb0ef41Sopenharmony_ci this.abortReason = reason; 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci[1, {}, false, []].forEach((arg) => { 501cb0ef41Sopenharmony_ci assert.throws(() => newStreamWritableFromWritableStream(arg), { 511cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 521cb0ef41Sopenharmony_ci }); 531cb0ef41Sopenharmony_ci}); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci{ 561cb0ef41Sopenharmony_ci // Ending the stream.Writable should close the writableStream 571cb0ef41Sopenharmony_ci const source = new TestSource(); 581cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 591cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci assert(writableStream.locked); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci writable.end('chunk'); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci writable.on('close', common.mustCall(() => { 661cb0ef41Sopenharmony_ci assert(writableStream.locked); 671cb0ef41Sopenharmony_ci assert.strictEqual(writableStream[kState].state, 'closed'); 681cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks.length, 1); 691cb0ef41Sopenharmony_ci assert.deepStrictEqual(source.chunks[0], Buffer.from('chunk')); 701cb0ef41Sopenharmony_ci })); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci{ 741cb0ef41Sopenharmony_ci // Destroying the stream.Writable without an error should close 751cb0ef41Sopenharmony_ci // the writableStream with no error. 761cb0ef41Sopenharmony_ci const source = new TestSource(); 771cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 781cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci assert(writableStream.locked); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci writable.destroy(); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci writable.on('close', common.mustCall(() => { 851cb0ef41Sopenharmony_ci assert(writableStream.locked); 861cb0ef41Sopenharmony_ci assert.strictEqual(writableStream[kState].state, 'closed'); 871cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks.length, 0); 881cb0ef41Sopenharmony_ci })); 891cb0ef41Sopenharmony_ci} 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci{ 921cb0ef41Sopenharmony_ci // Destroying the stream.Writable with an error should error 931cb0ef41Sopenharmony_ci // the writableStream 941cb0ef41Sopenharmony_ci const error = new Error('boom'); 951cb0ef41Sopenharmony_ci const source = new TestSource(); 961cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 971cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci assert(writableStream.locked); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci writable.destroy(error); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci writable.on('error', common.mustCall((reason) => { 1041cb0ef41Sopenharmony_ci assert.strictEqual(reason, error); 1051cb0ef41Sopenharmony_ci })); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci writable.on('close', common.mustCall(() => { 1081cb0ef41Sopenharmony_ci assert(writableStream.locked); 1091cb0ef41Sopenharmony_ci assert.strictEqual(writableStream[kState].state, 'errored'); 1101cb0ef41Sopenharmony_ci assert.strictEqual(writableStream[kState].storedError, error); 1111cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks.length, 0); 1121cb0ef41Sopenharmony_ci })); 1131cb0ef41Sopenharmony_ci} 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci{ 1161cb0ef41Sopenharmony_ci // Attempting to close, abort, or getWriter on writableStream 1171cb0ef41Sopenharmony_ci // should fail because it is locked. An internal error in 1181cb0ef41Sopenharmony_ci // writableStream should error the writable. 1191cb0ef41Sopenharmony_ci const error = new Error('boom'); 1201cb0ef41Sopenharmony_ci const source = new TestSource(); 1211cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 1221cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci assert(writableStream.locked); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci assert.rejects(writableStream.close(), { 1271cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE', 1281cb0ef41Sopenharmony_ci }); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci assert.rejects(writableStream.abort(), { 1311cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE', 1321cb0ef41Sopenharmony_ci }); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci assert.throws(() => writableStream.getWriter(), { 1351cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE', 1361cb0ef41Sopenharmony_ci }); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci writable.on('error', common.mustCall((reason) => { 1391cb0ef41Sopenharmony_ci assert.strictEqual(error, reason); 1401cb0ef41Sopenharmony_ci })); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci source.controller.error(error); 1431cb0ef41Sopenharmony_ci} 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci{ 1461cb0ef41Sopenharmony_ci const source = new TestSource(); 1471cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 1481cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci writable.on('error', common.mustNotCall()); 1511cb0ef41Sopenharmony_ci writable.on('finish', common.mustCall()); 1521cb0ef41Sopenharmony_ci writable.on('close', common.mustCall(() => { 1531cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks.length, 1); 1541cb0ef41Sopenharmony_ci assert.deepStrictEqual(source.chunks[0], Buffer.from('hello')); 1551cb0ef41Sopenharmony_ci })); 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci writable.write('hello', common.mustCall()); 1581cb0ef41Sopenharmony_ci writable.end(); 1591cb0ef41Sopenharmony_ci} 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci{ 1621cb0ef41Sopenharmony_ci const source = new TestSource(); 1631cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 1641cb0ef41Sopenharmony_ci const writable = 1651cb0ef41Sopenharmony_ci newStreamWritableFromWritableStream(writableStream, { 1661cb0ef41Sopenharmony_ci decodeStrings: false, 1671cb0ef41Sopenharmony_ci }); 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci writable.on('error', common.mustNotCall()); 1701cb0ef41Sopenharmony_ci writable.on('finish', common.mustCall()); 1711cb0ef41Sopenharmony_ci writable.on('close', common.mustCall(() => { 1721cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks.length, 1); 1731cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks[0], 'hello'); 1741cb0ef41Sopenharmony_ci })); 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci writable.write('hello', common.mustCall()); 1771cb0ef41Sopenharmony_ci writable.end(); 1781cb0ef41Sopenharmony_ci} 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci{ 1811cb0ef41Sopenharmony_ci const source = new TestSource(); 1821cb0ef41Sopenharmony_ci const writableStream = new WritableStream(source); 1831cb0ef41Sopenharmony_ci const writable = 1841cb0ef41Sopenharmony_ci newStreamWritableFromWritableStream( 1851cb0ef41Sopenharmony_ci writableStream, { 1861cb0ef41Sopenharmony_ci objectMode: true 1871cb0ef41Sopenharmony_ci }); 1881cb0ef41Sopenharmony_ci assert(writable.writableObjectMode); 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci writable.on('error', common.mustNotCall()); 1911cb0ef41Sopenharmony_ci writable.on('finish', common.mustCall()); 1921cb0ef41Sopenharmony_ci writable.on('close', common.mustCall(() => { 1931cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks.length, 1); 1941cb0ef41Sopenharmony_ci assert.strictEqual(source.chunks[0], 'hello'); 1951cb0ef41Sopenharmony_ci })); 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci writable.write('hello', common.mustCall()); 1981cb0ef41Sopenharmony_ci writable.end(); 1991cb0ef41Sopenharmony_ci} 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci{ 2021cb0ef41Sopenharmony_ci const writableStream = new WritableStream({ 2031cb0ef41Sopenharmony_ci write: common.mustCall(5), 2041cb0ef41Sopenharmony_ci close: common.mustCall(), 2051cb0ef41Sopenharmony_ci }); 2061cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci finished(writable, common.mustCall()); 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci writable.write('hello'); 2111cb0ef41Sopenharmony_ci writable.write('hello'); 2121cb0ef41Sopenharmony_ci writable.write('hello'); 2131cb0ef41Sopenharmony_ci writable.write('world'); 2141cb0ef41Sopenharmony_ci writable.write('world'); 2151cb0ef41Sopenharmony_ci writable.end(); 2161cb0ef41Sopenharmony_ci} 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci{ 2191cb0ef41Sopenharmony_ci const writableStream = new WritableStream({ 2201cb0ef41Sopenharmony_ci write: common.mustCall(2), 2211cb0ef41Sopenharmony_ci close: common.mustCall(), 2221cb0ef41Sopenharmony_ci }); 2231cb0ef41Sopenharmony_ci const writable = newStreamWritableFromWritableStream(writableStream); 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci const readable = new Readable({ 2261cb0ef41Sopenharmony_ci read() { 2271cb0ef41Sopenharmony_ci readable.push(Buffer.from('hello')); 2281cb0ef41Sopenharmony_ci readable.push(Buffer.from('world')); 2291cb0ef41Sopenharmony_ci readable.push(null); 2301cb0ef41Sopenharmony_ci } 2311cb0ef41Sopenharmony_ci }); 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci pipeline(readable, writable, common.mustCall()); 2341cb0ef41Sopenharmony_ci} 235