11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst { Duplex } = require('stream');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci{
81cb0ef41Sopenharmony_ci  const duplex = new Duplex({
91cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
101cb0ef41Sopenharmony_ci    read() {}
111cb0ef41Sopenharmony_ci  });
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  duplex.resume();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall());
161cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall());
171cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  duplex.destroy();
201cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci{
241cb0ef41Sopenharmony_ci  const duplex = new Duplex({
251cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
261cb0ef41Sopenharmony_ci    read() {}
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci  duplex.resume();
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall());
331cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall());
341cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((err) => {
351cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
361cb0ef41Sopenharmony_ci  }));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  duplex.destroy(expected);
391cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci  const duplex = new Duplex({
441cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
451cb0ef41Sopenharmony_ci    read() {}
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  duplex._destroy = common.mustCall(function(err, cb) {
491cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
501cb0ef41Sopenharmony_ci    cb(err);
511cb0ef41Sopenharmony_ci  });
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall('no finish event'));
561cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((err) => {
571cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
581cb0ef41Sopenharmony_ci  }));
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  duplex.destroy(expected);
611cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci{
651cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
661cb0ef41Sopenharmony_ci  const duplex = new Duplex({
671cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
681cb0ef41Sopenharmony_ci    read() {},
691cb0ef41Sopenharmony_ci    destroy: common.mustCall(function(err, cb) {
701cb0ef41Sopenharmony_ci      assert.strictEqual(err, expected);
711cb0ef41Sopenharmony_ci      cb();
721cb0ef41Sopenharmony_ci    })
731cb0ef41Sopenharmony_ci  });
741cb0ef41Sopenharmony_ci  duplex.resume();
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall('no end event'));
771cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall('no finish event'));
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  // Error is swallowed by the custom _destroy
801cb0ef41Sopenharmony_ci  duplex.on('error', common.mustNotCall('no error event'));
811cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  duplex.destroy(expected);
841cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci{
881cb0ef41Sopenharmony_ci  const duplex = new Duplex({
891cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
901cb0ef41Sopenharmony_ci    read() {}
911cb0ef41Sopenharmony_ci  });
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  duplex._destroy = common.mustCall(function(err, cb) {
941cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
951cb0ef41Sopenharmony_ci    cb();
961cb0ef41Sopenharmony_ci  });
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  duplex.destroy();
991cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci{
1031cb0ef41Sopenharmony_ci  const duplex = new Duplex({
1041cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
1051cb0ef41Sopenharmony_ci    read() {}
1061cb0ef41Sopenharmony_ci  });
1071cb0ef41Sopenharmony_ci  duplex.resume();
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  duplex._destroy = common.mustCall(function(err, cb) {
1101cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
1111cb0ef41Sopenharmony_ci    process.nextTick(() => {
1121cb0ef41Sopenharmony_ci      this.push(null);
1131cb0ef41Sopenharmony_ci      this.end();
1141cb0ef41Sopenharmony_ci      cb();
1151cb0ef41Sopenharmony_ci    });
1161cb0ef41Sopenharmony_ci  });
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  const fail = common.mustNotCall('no finish or end event');
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  duplex.on('finish', fail);
1211cb0ef41Sopenharmony_ci  duplex.on('end', fail);
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  duplex.destroy();
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  duplex.removeListener('end', fail);
1261cb0ef41Sopenharmony_ci  duplex.removeListener('finish', fail);
1271cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall());
1281cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall());
1291cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
1301cb0ef41Sopenharmony_ci}
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci{
1331cb0ef41Sopenharmony_ci  const duplex = new Duplex({
1341cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
1351cb0ef41Sopenharmony_ci    read() {}
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  duplex._destroy = common.mustCall(function(err, cb) {
1411cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
1421cb0ef41Sopenharmony_ci    cb(expected);
1431cb0ef41Sopenharmony_ci  });
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall('no finish event'));
1461cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall('no end event'));
1471cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((err) => {
1481cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
1491cb0ef41Sopenharmony_ci  }));
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  duplex.destroy();
1521cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci{
1561cb0ef41Sopenharmony_ci  const duplex = new Duplex({
1571cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
1581cb0ef41Sopenharmony_ci    read() {},
1591cb0ef41Sopenharmony_ci    allowHalfOpen: true
1601cb0ef41Sopenharmony_ci  });
1611cb0ef41Sopenharmony_ci  duplex.resume();
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall());
1641cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall());
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci  duplex.destroy();
1671cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
1681cb0ef41Sopenharmony_ci}
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci{
1711cb0ef41Sopenharmony_ci  const duplex = new Duplex({
1721cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
1731cb0ef41Sopenharmony_ci    read() {},
1741cb0ef41Sopenharmony_ci  });
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci  duplex.destroyed = true;
1771cb0ef41Sopenharmony_ci  assert.strictEqual(duplex.destroyed, true);
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  // The internal destroy() mechanism should not be triggered
1801cb0ef41Sopenharmony_ci  duplex.on('finish', common.mustNotCall());
1811cb0ef41Sopenharmony_ci  duplex.on('end', common.mustNotCall());
1821cb0ef41Sopenharmony_ci  duplex.destroy();
1831cb0ef41Sopenharmony_ci}
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci{
1861cb0ef41Sopenharmony_ci  function MyDuplex() {
1871cb0ef41Sopenharmony_ci    assert.strictEqual(this.destroyed, false);
1881cb0ef41Sopenharmony_ci    this.destroyed = false;
1891cb0ef41Sopenharmony_ci    Duplex.call(this);
1901cb0ef41Sopenharmony_ci  }
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  Object.setPrototypeOf(MyDuplex.prototype, Duplex.prototype);
1931cb0ef41Sopenharmony_ci  Object.setPrototypeOf(MyDuplex, Duplex);
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci  new MyDuplex();
1961cb0ef41Sopenharmony_ci}
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci{
1991cb0ef41Sopenharmony_ci  const duplex = new Duplex({
2001cb0ef41Sopenharmony_ci    writable: false,
2011cb0ef41Sopenharmony_ci    autoDestroy: true,
2021cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
2031cb0ef41Sopenharmony_ci    read() {},
2041cb0ef41Sopenharmony_ci  });
2051cb0ef41Sopenharmony_ci  duplex.push(null);
2061cb0ef41Sopenharmony_ci  duplex.resume();
2071cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
2081cb0ef41Sopenharmony_ci}
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci{
2111cb0ef41Sopenharmony_ci  const duplex = new Duplex({
2121cb0ef41Sopenharmony_ci    readable: false,
2131cb0ef41Sopenharmony_ci    autoDestroy: true,
2141cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
2151cb0ef41Sopenharmony_ci    read() {},
2161cb0ef41Sopenharmony_ci  });
2171cb0ef41Sopenharmony_ci  duplex.end();
2181cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
2191cb0ef41Sopenharmony_ci}
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci{
2221cb0ef41Sopenharmony_ci  const duplex = new Duplex({
2231cb0ef41Sopenharmony_ci    allowHalfOpen: false,
2241cb0ef41Sopenharmony_ci    autoDestroy: true,
2251cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
2261cb0ef41Sopenharmony_ci    read() {},
2271cb0ef41Sopenharmony_ci  });
2281cb0ef41Sopenharmony_ci  duplex.push(null);
2291cb0ef41Sopenharmony_ci  duplex.resume();
2301cb0ef41Sopenharmony_ci  const orgEnd = duplex.end;
2311cb0ef41Sopenharmony_ci  duplex.end = common.mustNotCall();
2321cb0ef41Sopenharmony_ci  duplex.on('end', () => {
2331cb0ef41Sopenharmony_ci    // Ensure end() is called in next tick to allow
2341cb0ef41Sopenharmony_ci    // any pending writes to be invoked first.
2351cb0ef41Sopenharmony_ci    process.nextTick(() => {
2361cb0ef41Sopenharmony_ci      duplex.end = common.mustCall(orgEnd);
2371cb0ef41Sopenharmony_ci    });
2381cb0ef41Sopenharmony_ci  });
2391cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
2401cb0ef41Sopenharmony_ci}
2411cb0ef41Sopenharmony_ci{
2421cb0ef41Sopenharmony_ci  // Check abort signal
2431cb0ef41Sopenharmony_ci  const controller = new AbortController();
2441cb0ef41Sopenharmony_ci  const { signal } = controller;
2451cb0ef41Sopenharmony_ci  const duplex = new Duplex({
2461cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
2471cb0ef41Sopenharmony_ci    read() {},
2481cb0ef41Sopenharmony_ci    signal,
2491cb0ef41Sopenharmony_ci  });
2501cb0ef41Sopenharmony_ci  let count = 0;
2511cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((e) => {
2521cb0ef41Sopenharmony_ci    assert.strictEqual(count++, 0); // Ensure not called twice
2531cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
2541cb0ef41Sopenharmony_ci  }));
2551cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall());
2561cb0ef41Sopenharmony_ci  controller.abort();
2571cb0ef41Sopenharmony_ci}
258