11cb0ef41Sopenharmony_ci// Flags: --no-warnings --expose-internals 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci'use strict'; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst common = require('../common'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst assert = require('assert'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst { 101cb0ef41Sopenharmony_ci newWritableStreamFromStreamWritable, 111cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters'); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst { 141cb0ef41Sopenharmony_ci Duplex, 151cb0ef41Sopenharmony_ci Writable, 161cb0ef41Sopenharmony_ci PassThrough, 171cb0ef41Sopenharmony_ci} = require('stream'); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciclass TestWritable extends Writable { 201cb0ef41Sopenharmony_ci constructor(asyncWrite = false) { 211cb0ef41Sopenharmony_ci super(); 221cb0ef41Sopenharmony_ci this.chunks = []; 231cb0ef41Sopenharmony_ci this.asyncWrite = asyncWrite; 241cb0ef41Sopenharmony_ci } 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci _write(chunk, encoding, callback) { 271cb0ef41Sopenharmony_ci this.chunks.push({ chunk, encoding }); 281cb0ef41Sopenharmony_ci if (this.asyncWrite) { 291cb0ef41Sopenharmony_ci setImmediate(() => callback()); 301cb0ef41Sopenharmony_ci return; 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci callback(); 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci} 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci[1, {}, false, []].forEach((arg) => { 371cb0ef41Sopenharmony_ci assert.throws(() => newWritableStreamFromStreamWritable(arg), { 381cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci}); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci{ 431cb0ef41Sopenharmony_ci // Closing the WritableStream normally closes the stream.Writable 441cb0ef41Sopenharmony_ci // without errors. 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci const writable = new TestWritable(); 471cb0ef41Sopenharmony_ci writable.on('error', common.mustNotCall()); 481cb0ef41Sopenharmony_ci writable.on('finish', common.mustCall()); 491cb0ef41Sopenharmony_ci writable.on('close', common.mustCall()); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci writableStream.close().then(common.mustCall(() => { 541cb0ef41Sopenharmony_ci assert(writable.destroyed); 551cb0ef41Sopenharmony_ci })); 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci{ 591cb0ef41Sopenharmony_ci // Aborting the WritableStream errors the stream.Writable 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci const error = new Error('boom'); 621cb0ef41Sopenharmony_ci const writable = new TestWritable(); 631cb0ef41Sopenharmony_ci writable.on('error', common.mustCall((reason) => { 641cb0ef41Sopenharmony_ci assert.strictEqual(reason, error); 651cb0ef41Sopenharmony_ci })); 661cb0ef41Sopenharmony_ci writable.on('finish', common.mustNotCall()); 671cb0ef41Sopenharmony_ci writable.on('close', common.mustCall()); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci writableStream.abort(error).then(common.mustCall(() => { 721cb0ef41Sopenharmony_ci assert(writable.destroyed); 731cb0ef41Sopenharmony_ci })); 741cb0ef41Sopenharmony_ci} 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci{ 771cb0ef41Sopenharmony_ci // Destroying the stream.Writable prematurely errors the 781cb0ef41Sopenharmony_ci // WritableStream 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci const error = new Error('boom'); 811cb0ef41Sopenharmony_ci const writable = new TestWritable(); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 841cb0ef41Sopenharmony_ci assert.rejects(writableStream.close(), error); 851cb0ef41Sopenharmony_ci writable.destroy(error); 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci{ 891cb0ef41Sopenharmony_ci // Ending the stream.Writable directly errors the WritableStream 901cb0ef41Sopenharmony_ci const writable = new TestWritable(); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci assert.rejects(writableStream.close(), { 951cb0ef41Sopenharmony_ci code: 'ABORT_ERR' 961cb0ef41Sopenharmony_ci }); 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci writable.end(); 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci{ 1021cb0ef41Sopenharmony_ci const writable = new TestWritable(); 1031cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 1041cb0ef41Sopenharmony_ci const writer = writableStream.getWriter(); 1051cb0ef41Sopenharmony_ci const ec = new TextEncoder(); 1061cb0ef41Sopenharmony_ci writer.write(ec.encode('hello')).then(common.mustCall(() => { 1071cb0ef41Sopenharmony_ci assert.strictEqual(writable.chunks.length, 1); 1081cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1091cb0ef41Sopenharmony_ci writable.chunks[0], 1101cb0ef41Sopenharmony_ci { 1111cb0ef41Sopenharmony_ci chunk: Buffer.from('hello'), 1121cb0ef41Sopenharmony_ci encoding: 'buffer' 1131cb0ef41Sopenharmony_ci }); 1141cb0ef41Sopenharmony_ci })); 1151cb0ef41Sopenharmony_ci} 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci{ 1181cb0ef41Sopenharmony_ci const writable = new TestWritable(true); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci writable.on('error', common.mustNotCall()); 1211cb0ef41Sopenharmony_ci writable.on('close', common.mustCall()); 1221cb0ef41Sopenharmony_ci writable.on('finish', common.mustCall()); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 1251cb0ef41Sopenharmony_ci const writer = writableStream.getWriter(); 1261cb0ef41Sopenharmony_ci const ec = new TextEncoder(); 1271cb0ef41Sopenharmony_ci writer.write(ec.encode('hello')).then(common.mustCall(() => { 1281cb0ef41Sopenharmony_ci assert.strictEqual(writable.chunks.length, 1); 1291cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1301cb0ef41Sopenharmony_ci writable.chunks[0], 1311cb0ef41Sopenharmony_ci { 1321cb0ef41Sopenharmony_ci chunk: Buffer.from('hello'), 1331cb0ef41Sopenharmony_ci encoding: 'buffer' 1341cb0ef41Sopenharmony_ci }); 1351cb0ef41Sopenharmony_ci writer.close().then(common.mustCall()); 1361cb0ef41Sopenharmony_ci })); 1371cb0ef41Sopenharmony_ci} 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci{ 1401cb0ef41Sopenharmony_ci const duplex = new PassThrough(); 1411cb0ef41Sopenharmony_ci duplex.setEncoding('utf8'); 1421cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(duplex); 1431cb0ef41Sopenharmony_ci const ec = new TextEncoder(); 1441cb0ef41Sopenharmony_ci writableStream 1451cb0ef41Sopenharmony_ci .getWriter() 1461cb0ef41Sopenharmony_ci .write(ec.encode('hello')) 1471cb0ef41Sopenharmony_ci .then(common.mustCall()); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci duplex.on('data', common.mustCall((chunk) => { 1501cb0ef41Sopenharmony_ci assert.strictEqual(chunk, 'hello'); 1511cb0ef41Sopenharmony_ci })); 1521cb0ef41Sopenharmony_ci} 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci{ 1551cb0ef41Sopenharmony_ci const writable = new Writable(); 1561cb0ef41Sopenharmony_ci writable.destroy(); 1571cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(writable); 1581cb0ef41Sopenharmony_ci const writer = writableStream.getWriter(); 1591cb0ef41Sopenharmony_ci writer.closed.then(common.mustCall()); 1601cb0ef41Sopenharmony_ci} 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci{ 1631cb0ef41Sopenharmony_ci const duplex = new Duplex({ writable: false }); 1641cb0ef41Sopenharmony_ci const writableStream = newWritableStreamFromStreamWritable(duplex); 1651cb0ef41Sopenharmony_ci const writer = writableStream.getWriter(); 1661cb0ef41Sopenharmony_ci writer.closed.then(common.mustCall()); 1671cb0ef41Sopenharmony_ci} 168