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  newReadableWritablePairFromDuplex,
101cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst {
131cb0ef41Sopenharmony_ci  PassThrough,
141cb0ef41Sopenharmony_ci} = require('stream');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  // Destroying the duplex without an error should close
181cb0ef41Sopenharmony_ci  // the readable and error the writable.
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
211cb0ef41Sopenharmony_ci  const {
221cb0ef41Sopenharmony_ci    readable,
231cb0ef41Sopenharmony_ci    writable,
241cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const reader = readable.getReader();
271cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, {
301cb0ef41Sopenharmony_ci    code: 'ABORT_ERR',
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  assert.rejects(writer.closed, {
341cb0ef41Sopenharmony_ci    code: 'ABORT_ERR',
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  duplex.destroy();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci  // Destroying the duplex with an error should error
441cb0ef41Sopenharmony_ci  // both the readable and writable
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  const error = new Error('boom');
471cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
481cb0ef41Sopenharmony_ci  const {
491cb0ef41Sopenharmony_ci    readable,
501cb0ef41Sopenharmony_ci    writable,
511cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
541cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((reason) => {
551cb0ef41Sopenharmony_ci    assert.strictEqual(reason, error);
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  const reader = readable.getReader();
591cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
621cb0ef41Sopenharmony_ci  assert.rejects(writer.closed, error);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  duplex.destroy(error);
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci{
681cb0ef41Sopenharmony_ci  const error = new Error('boom');
691cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
701cb0ef41Sopenharmony_ci  const {
711cb0ef41Sopenharmony_ci    readable,
721cb0ef41Sopenharmony_ci    writable,
731cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
761cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((reason) => {
771cb0ef41Sopenharmony_ci    assert.strictEqual(reason, error);
781cb0ef41Sopenharmony_ci  }));
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  const reader = readable.getReader();
811cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
841cb0ef41Sopenharmony_ci  assert.rejects(writer.closed, error);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  reader.cancel(error).then(common.mustCall());
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
911cb0ef41Sopenharmony_ci  const {
921cb0ef41Sopenharmony_ci    readable,
931cb0ef41Sopenharmony_ci    writable,
941cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
971cb0ef41Sopenharmony_ci  duplex.on('error', common.mustNotCall());
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  const reader = readable.getReader();
1001cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
1031cb0ef41Sopenharmony_ci  writer.closed.then(common.mustCall());
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  writer.close().then(common.mustCall());
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci{
1091cb0ef41Sopenharmony_ci  const error = new Error('boom');
1101cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
1111cb0ef41Sopenharmony_ci  const {
1121cb0ef41Sopenharmony_ci    readable,
1131cb0ef41Sopenharmony_ci    writable,
1141cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
1171cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((reason) => {
1181cb0ef41Sopenharmony_ci    assert.strictEqual(reason, error);
1191cb0ef41Sopenharmony_ci  }));
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  const reader = readable.getReader();
1221cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
1251cb0ef41Sopenharmony_ci  assert.rejects(writer.closed, error);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  writer.abort(error).then(common.mustCall());
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci{
1311cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
1321cb0ef41Sopenharmony_ci  const {
1331cb0ef41Sopenharmony_ci    readable,
1341cb0ef41Sopenharmony_ci    writable,
1351cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((error) => {
1401cb0ef41Sopenharmony_ci    assert.strictEqual(error.code, 'ABORT_ERR');
1411cb0ef41Sopenharmony_ci  }));
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  const reader = readable.getReader();
1441cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  assert.rejects(writer.closed, {
1471cb0ef41Sopenharmony_ci    code: 'ABORT_ERR',
1481cb0ef41Sopenharmony_ci  });
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  reader.cancel();
1511cb0ef41Sopenharmony_ci}
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci{
1541cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
1551cb0ef41Sopenharmony_ci  const {
1561cb0ef41Sopenharmony_ci    readable,
1571cb0ef41Sopenharmony_ci    writable,
1581cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
1611cb0ef41Sopenharmony_ci  duplex.on('error', common.mustNotCall());
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  const reader = readable.getReader();
1641cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
1671cb0ef41Sopenharmony_ci  assert.rejects(writer.closed, {
1681cb0ef41Sopenharmony_ci    code: 'ABORT_ERR',
1691cb0ef41Sopenharmony_ci  });
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  duplex.end();
1721cb0ef41Sopenharmony_ci}
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci{
1751cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
1761cb0ef41Sopenharmony_ci  const {
1771cb0ef41Sopenharmony_ci    readable,
1781cb0ef41Sopenharmony_ci    writable,
1791cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci  duplex.on('data', common.mustCall(2));
1821cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
1831cb0ef41Sopenharmony_ci  duplex.on('end', common.mustCall());
1841cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustCall());
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1871cb0ef41Sopenharmony_ci  const reader = readable.getReader();
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  const ec = new TextEncoder();
1901cb0ef41Sopenharmony_ci  const dc = new TextDecoder();
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  Promise.all([
1931cb0ef41Sopenharmony_ci    writer.write(ec.encode('hello')),
1941cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ done, value }) => {
1951cb0ef41Sopenharmony_ci      assert(!done);
1961cb0ef41Sopenharmony_ci      assert.strictEqual(dc.decode(value), 'hello');
1971cb0ef41Sopenharmony_ci    })),
1981cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ done, value }) => {
1991cb0ef41Sopenharmony_ci      assert(!done);
2001cb0ef41Sopenharmony_ci      assert.strictEqual(dc.decode(value), 'there');
2011cb0ef41Sopenharmony_ci    })),
2021cb0ef41Sopenharmony_ci    writer.write(ec.encode('there')),
2031cb0ef41Sopenharmony_ci    writer.close(),
2041cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ done, value }) => {
2051cb0ef41Sopenharmony_ci      assert(done);
2061cb0ef41Sopenharmony_ci      assert.strictEqual(value, undefined);
2071cb0ef41Sopenharmony_ci    })),
2081cb0ef41Sopenharmony_ci  ]).then(common.mustCall());
2091cb0ef41Sopenharmony_ci}
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci{
2121cb0ef41Sopenharmony_ci  const duplex = new PassThrough();
2131cb0ef41Sopenharmony_ci  duplex.destroy();
2141cb0ef41Sopenharmony_ci  const {
2151cb0ef41Sopenharmony_ci    readable,
2161cb0ef41Sopenharmony_ci    writable,
2171cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
2181cb0ef41Sopenharmony_ci  const reader = readable.getReader();
2191cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
2201cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
2211cb0ef41Sopenharmony_ci  writer.closed.then(common.mustCall());
2221cb0ef41Sopenharmony_ci}
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci{
2251cb0ef41Sopenharmony_ci  const duplex = new PassThrough({ writable: false });
2261cb0ef41Sopenharmony_ci  assert(duplex.readable);
2271cb0ef41Sopenharmony_ci  assert(!duplex.writable);
2281cb0ef41Sopenharmony_ci  const {
2291cb0ef41Sopenharmony_ci    readable,
2301cb0ef41Sopenharmony_ci    writable,
2311cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
2321cb0ef41Sopenharmony_ci  const reader = readable.getReader();
2331cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
2341cb0ef41Sopenharmony_ci  writer.closed.then(common.mustCall());
2351cb0ef41Sopenharmony_ci  reader.cancel().then(common.mustCall());
2361cb0ef41Sopenharmony_ci}
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci{
2391cb0ef41Sopenharmony_ci  const duplex = new PassThrough({ readable: false });
2401cb0ef41Sopenharmony_ci  assert(!duplex.readable);
2411cb0ef41Sopenharmony_ci  assert(duplex.writable);
2421cb0ef41Sopenharmony_ci  const {
2431cb0ef41Sopenharmony_ci    readable,
2441cb0ef41Sopenharmony_ci    writable,
2451cb0ef41Sopenharmony_ci  } = newReadableWritablePairFromDuplex(duplex);
2461cb0ef41Sopenharmony_ci  const reader = readable.getReader();
2471cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
2481cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
2491cb0ef41Sopenharmony_ci  writer.close().then(common.mustCall());
2501cb0ef41Sopenharmony_ci}
251