11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst { Writable, addAbortSignal } = require('stream');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci{
81cb0ef41Sopenharmony_ci  const write = new Writable({
91cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
101cb0ef41Sopenharmony_ci  });
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  write.on('finish', common.mustNotCall());
131cb0ef41Sopenharmony_ci  write.on('close', common.mustCall());
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  write.destroy();
161cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  const write = new Writable({
211cb0ef41Sopenharmony_ci    write(chunk, enc, cb) {
221cb0ef41Sopenharmony_ci      this.destroy(new Error('asd'));
231cb0ef41Sopenharmony_ci      cb();
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  write.on('error', common.mustCall());
281cb0ef41Sopenharmony_ci  write.on('finish', common.mustNotCall());
291cb0ef41Sopenharmony_ci  write.end('asd');
301cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const write = new Writable({
351cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  write.on('finish', common.mustNotCall());
411cb0ef41Sopenharmony_ci  write.on('close', common.mustCall());
421cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((err) => {
431cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
441cb0ef41Sopenharmony_ci  }));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  write.destroy(expected);
471cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  const write = new Writable({
521cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  write._destroy = function(err, cb) {
561cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
571cb0ef41Sopenharmony_ci    cb(err);
581cb0ef41Sopenharmony_ci  };
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  write.on('finish', common.mustNotCall('no finish event'));
631cb0ef41Sopenharmony_ci  write.on('close', common.mustCall());
641cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((err) => {
651cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
661cb0ef41Sopenharmony_ci  }));
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  write.destroy(expected);
691cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci{
731cb0ef41Sopenharmony_ci  const write = new Writable({
741cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); },
751cb0ef41Sopenharmony_ci    destroy: common.mustCall(function(err, cb) {
761cb0ef41Sopenharmony_ci      assert.strictEqual(err, expected);
771cb0ef41Sopenharmony_ci      cb();
781cb0ef41Sopenharmony_ci    })
791cb0ef41Sopenharmony_ci  });
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  write.on('finish', common.mustNotCall('no finish event'));
841cb0ef41Sopenharmony_ci  write.on('close', common.mustCall());
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  // Error is swallowed by the custom _destroy
871cb0ef41Sopenharmony_ci  write.on('error', common.mustNotCall('no error event'));
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  write.destroy(expected);
901cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci{
941cb0ef41Sopenharmony_ci  const write = new Writable({
951cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
961cb0ef41Sopenharmony_ci  });
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  write._destroy = common.mustCall(function(err, cb) {
991cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
1001cb0ef41Sopenharmony_ci    cb();
1011cb0ef41Sopenharmony_ci  });
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  write.destroy();
1041cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci{
1081cb0ef41Sopenharmony_ci  const write = new Writable({
1091cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
1101cb0ef41Sopenharmony_ci  });
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci  write._destroy = common.mustCall(function(err, cb) {
1131cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
1141cb0ef41Sopenharmony_ci    process.nextTick(() => {
1151cb0ef41Sopenharmony_ci      this.end();
1161cb0ef41Sopenharmony_ci      cb();
1171cb0ef41Sopenharmony_ci    });
1181cb0ef41Sopenharmony_ci  });
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  const fail = common.mustNotCall('no finish event');
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  write.on('finish', fail);
1231cb0ef41Sopenharmony_ci  write.on('close', common.mustCall());
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  write.destroy();
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci{
1311cb0ef41Sopenharmony_ci  const write = new Writable({
1321cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
1331cb0ef41Sopenharmony_ci  });
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  write._destroy = common.mustCall(function(err, cb) {
1381cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
1391cb0ef41Sopenharmony_ci    cb(expected);
1401cb0ef41Sopenharmony_ci  });
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  write.on('close', common.mustCall());
1431cb0ef41Sopenharmony_ci  write.on('finish', common.mustNotCall('no finish event'));
1441cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((err) => {
1451cb0ef41Sopenharmony_ci    assert.strictEqual(err, expected);
1461cb0ef41Sopenharmony_ci  }));
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  write.destroy();
1491cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
1501cb0ef41Sopenharmony_ci}
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci{
1531cb0ef41Sopenharmony_ci  // double error case
1541cb0ef41Sopenharmony_ci  const write = new Writable({
1551cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
1561cb0ef41Sopenharmony_ci  });
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  let ticked = false;
1591cb0ef41Sopenharmony_ci  write.on('close', common.mustCall(() => {
1601cb0ef41Sopenharmony_ci    assert.strictEqual(ticked, true);
1611cb0ef41Sopenharmony_ci  }));
1621cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((err) => {
1631cb0ef41Sopenharmony_ci    assert.strictEqual(ticked, true);
1641cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom 1');
1651cb0ef41Sopenharmony_ci    assert.strictEqual(write._writableState.errorEmitted, true);
1661cb0ef41Sopenharmony_ci  }));
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci  const expected = new Error('kaboom 1');
1691cb0ef41Sopenharmony_ci  write.destroy(expected);
1701cb0ef41Sopenharmony_ci  write.destroy(new Error('kaboom 2'));
1711cb0ef41Sopenharmony_ci  assert.strictEqual(write._writableState.errored, expected);
1721cb0ef41Sopenharmony_ci  assert.strictEqual(write._writableState.errorEmitted, false);
1731cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
1741cb0ef41Sopenharmony_ci  ticked = true;
1751cb0ef41Sopenharmony_ci}
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci{
1781cb0ef41Sopenharmony_ci  const writable = new Writable({
1791cb0ef41Sopenharmony_ci    destroy: common.mustCall(function(err, cb) {
1801cb0ef41Sopenharmony_ci      process.nextTick(cb, new Error('kaboom 1'));
1811cb0ef41Sopenharmony_ci    }),
1821cb0ef41Sopenharmony_ci    write(chunk, enc, cb) {
1831cb0ef41Sopenharmony_ci      cb();
1841cb0ef41Sopenharmony_ci    }
1851cb0ef41Sopenharmony_ci  });
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  let ticked = false;
1881cb0ef41Sopenharmony_ci  writable.on('close', common.mustCall(() => {
1891cb0ef41Sopenharmony_ci    writable.on('error', common.mustNotCall());
1901cb0ef41Sopenharmony_ci    writable.destroy(new Error('hello'));
1911cb0ef41Sopenharmony_ci    assert.strictEqual(ticked, true);
1921cb0ef41Sopenharmony_ci    assert.strictEqual(writable._writableState.errorEmitted, true);
1931cb0ef41Sopenharmony_ci  }));
1941cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((err) => {
1951cb0ef41Sopenharmony_ci    assert.strictEqual(ticked, true);
1961cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom 1');
1971cb0ef41Sopenharmony_ci    assert.strictEqual(writable._writableState.errorEmitted, true);
1981cb0ef41Sopenharmony_ci  }));
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  writable.destroy();
2011cb0ef41Sopenharmony_ci  assert.strictEqual(writable.destroyed, true);
2021cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.errored, null);
2031cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.errorEmitted, false);
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci  // Test case where `writable.destroy()` is called again with an error before
2061cb0ef41Sopenharmony_ci  // the `_destroy()` callback is called.
2071cb0ef41Sopenharmony_ci  writable.destroy(new Error('kaboom 2'));
2081cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.errorEmitted, false);
2091cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.errored, null);
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  ticked = true;
2121cb0ef41Sopenharmony_ci}
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci{
2151cb0ef41Sopenharmony_ci  const write = new Writable({
2161cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
2171cb0ef41Sopenharmony_ci  });
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci  write.destroyed = true;
2201cb0ef41Sopenharmony_ci  assert.strictEqual(write.destroyed, true);
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci  // The internal destroy() mechanism should not be triggered
2231cb0ef41Sopenharmony_ci  write.on('close', common.mustNotCall());
2241cb0ef41Sopenharmony_ci  write.destroy();
2251cb0ef41Sopenharmony_ci}
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci{
2281cb0ef41Sopenharmony_ci  function MyWritable() {
2291cb0ef41Sopenharmony_ci    assert.strictEqual(this.destroyed, false);
2301cb0ef41Sopenharmony_ci    this.destroyed = false;
2311cb0ef41Sopenharmony_ci    Writable.call(this);
2321cb0ef41Sopenharmony_ci  }
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci  Object.setPrototypeOf(MyWritable.prototype, Writable.prototype);
2351cb0ef41Sopenharmony_ci  Object.setPrototypeOf(MyWritable, Writable);
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci  new MyWritable();
2381cb0ef41Sopenharmony_ci}
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci{
2411cb0ef41Sopenharmony_ci  // Destroy and destroy callback
2421cb0ef41Sopenharmony_ci  const write = new Writable({
2431cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
2441cb0ef41Sopenharmony_ci  });
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci  write.destroy();
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci  const expected = new Error('kaboom');
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci  write.destroy(expected, common.mustCall((err) => {
2511cb0ef41Sopenharmony_ci    assert.strictEqual(err, undefined);
2521cb0ef41Sopenharmony_ci  }));
2531cb0ef41Sopenharmony_ci}
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci{
2561cb0ef41Sopenharmony_ci  // Checks that `._undestroy()` restores the state so that `final` will be
2571cb0ef41Sopenharmony_ci  // called again.
2581cb0ef41Sopenharmony_ci  const write = new Writable({
2591cb0ef41Sopenharmony_ci    write: common.mustNotCall(),
2601cb0ef41Sopenharmony_ci    final: common.mustCall((cb) => cb(), 2),
2611cb0ef41Sopenharmony_ci    autoDestroy: true
2621cb0ef41Sopenharmony_ci  });
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci  write.end();
2651cb0ef41Sopenharmony_ci  write.once('close', common.mustCall(() => {
2661cb0ef41Sopenharmony_ci    write._undestroy();
2671cb0ef41Sopenharmony_ci    write.end();
2681cb0ef41Sopenharmony_ci  }));
2691cb0ef41Sopenharmony_ci}
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ci{
2721cb0ef41Sopenharmony_ci  const write = new Writable();
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci  write.destroy();
2751cb0ef41Sopenharmony_ci  write.on('error', common.mustNotCall());
2761cb0ef41Sopenharmony_ci  write.write('asd', common.expectsError({
2771cb0ef41Sopenharmony_ci    name: 'Error',
2781cb0ef41Sopenharmony_ci    code: 'ERR_STREAM_DESTROYED',
2791cb0ef41Sopenharmony_ci    message: 'Cannot call write after a stream was destroyed'
2801cb0ef41Sopenharmony_ci  }));
2811cb0ef41Sopenharmony_ci}
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci{
2841cb0ef41Sopenharmony_ci  const write = new Writable({
2851cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
2861cb0ef41Sopenharmony_ci  });
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci  write.on('error', common.mustNotCall());
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci  write.cork();
2911cb0ef41Sopenharmony_ci  write.write('asd', common.mustCall());
2921cb0ef41Sopenharmony_ci  write.uncork();
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci  write.cork();
2951cb0ef41Sopenharmony_ci  write.write('asd', common.expectsError({
2961cb0ef41Sopenharmony_ci    name: 'Error',
2971cb0ef41Sopenharmony_ci    code: 'ERR_STREAM_DESTROYED',
2981cb0ef41Sopenharmony_ci    message: 'Cannot call write after a stream was destroyed'
2991cb0ef41Sopenharmony_ci  }));
3001cb0ef41Sopenharmony_ci  write.destroy();
3011cb0ef41Sopenharmony_ci  write.write('asd', common.expectsError({
3021cb0ef41Sopenharmony_ci    name: 'Error',
3031cb0ef41Sopenharmony_ci    code: 'ERR_STREAM_DESTROYED',
3041cb0ef41Sopenharmony_ci    message: 'Cannot call write after a stream was destroyed'
3051cb0ef41Sopenharmony_ci  }));
3061cb0ef41Sopenharmony_ci  write.uncork();
3071cb0ef41Sopenharmony_ci}
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci{
3101cb0ef41Sopenharmony_ci  // Call end(cb) after error & destroy
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci  const write = new Writable({
3131cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(new Error('asd')); }
3141cb0ef41Sopenharmony_ci  });
3151cb0ef41Sopenharmony_ci  write.on('error', common.mustCall(() => {
3161cb0ef41Sopenharmony_ci    write.destroy();
3171cb0ef41Sopenharmony_ci    let ticked = false;
3181cb0ef41Sopenharmony_ci    write.end(common.mustCall((err) => {
3191cb0ef41Sopenharmony_ci      assert.strictEqual(ticked, true);
3201cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
3211cb0ef41Sopenharmony_ci    }));
3221cb0ef41Sopenharmony_ci    ticked = true;
3231cb0ef41Sopenharmony_ci  }));
3241cb0ef41Sopenharmony_ci  write.write('asd');
3251cb0ef41Sopenharmony_ci}
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ci{
3281cb0ef41Sopenharmony_ci  // Call end(cb) after finish & destroy
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ci  const write = new Writable({
3311cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
3321cb0ef41Sopenharmony_ci  });
3331cb0ef41Sopenharmony_ci  write.on('finish', common.mustCall(() => {
3341cb0ef41Sopenharmony_ci    write.destroy();
3351cb0ef41Sopenharmony_ci    let ticked = false;
3361cb0ef41Sopenharmony_ci    write.end(common.mustCall((err) => {
3371cb0ef41Sopenharmony_ci      assert.strictEqual(ticked, true);
3381cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
3391cb0ef41Sopenharmony_ci    }));
3401cb0ef41Sopenharmony_ci    ticked = true;
3411cb0ef41Sopenharmony_ci  }));
3421cb0ef41Sopenharmony_ci  write.end();
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci{
3461cb0ef41Sopenharmony_ci  // Call end(cb) after error & destroy and don't trigger
3471cb0ef41Sopenharmony_ci  // unhandled exception.
3481cb0ef41Sopenharmony_ci
3491cb0ef41Sopenharmony_ci  const write = new Writable({
3501cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { process.nextTick(cb); }
3511cb0ef41Sopenharmony_ci  });
3521cb0ef41Sopenharmony_ci  const _err = new Error('asd');
3531cb0ef41Sopenharmony_ci  write.once('error', common.mustCall((err) => {
3541cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'asd');
3551cb0ef41Sopenharmony_ci  }));
3561cb0ef41Sopenharmony_ci  write.end('asd', common.mustCall((err) => {
3571cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
3581cb0ef41Sopenharmony_ci  }));
3591cb0ef41Sopenharmony_ci  write.destroy(_err);
3601cb0ef41Sopenharmony_ci}
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci{
3631cb0ef41Sopenharmony_ci  // Call buffered write callback with error
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci  const _err = new Error('asd');
3661cb0ef41Sopenharmony_ci  const write = new Writable({
3671cb0ef41Sopenharmony_ci    write(chunk, enc, cb) {
3681cb0ef41Sopenharmony_ci      process.nextTick(cb, _err);
3691cb0ef41Sopenharmony_ci    },
3701cb0ef41Sopenharmony_ci    autoDestroy: false
3711cb0ef41Sopenharmony_ci  });
3721cb0ef41Sopenharmony_ci  write.cork();
3731cb0ef41Sopenharmony_ci  write.write('asd', common.mustCall((err) => {
3741cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
3751cb0ef41Sopenharmony_ci  }));
3761cb0ef41Sopenharmony_ci  write.write('asd', common.mustCall((err) => {
3771cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
3781cb0ef41Sopenharmony_ci  }));
3791cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((err) => {
3801cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
3811cb0ef41Sopenharmony_ci  }));
3821cb0ef41Sopenharmony_ci  write.uncork();
3831cb0ef41Sopenharmony_ci}
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci{
3861cb0ef41Sopenharmony_ci  // Ensure callback order.
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ci  let state = 0;
3891cb0ef41Sopenharmony_ci  const write = new Writable({
3901cb0ef41Sopenharmony_ci    write(chunk, enc, cb) {
3911cb0ef41Sopenharmony_ci      // `setImmediate()` is used on purpose to ensure the callback is called
3921cb0ef41Sopenharmony_ci      // after `process.nextTick()` callbacks.
3931cb0ef41Sopenharmony_ci      setImmediate(cb);
3941cb0ef41Sopenharmony_ci    }
3951cb0ef41Sopenharmony_ci  });
3961cb0ef41Sopenharmony_ci  write.write('asd', common.mustCall(() => {
3971cb0ef41Sopenharmony_ci    assert.strictEqual(state++, 0);
3981cb0ef41Sopenharmony_ci  }));
3991cb0ef41Sopenharmony_ci  write.write('asd', common.mustCall((err) => {
4001cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
4011cb0ef41Sopenharmony_ci    assert.strictEqual(state++, 1);
4021cb0ef41Sopenharmony_ci  }));
4031cb0ef41Sopenharmony_ci  write.destroy();
4041cb0ef41Sopenharmony_ci}
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci{
4071cb0ef41Sopenharmony_ci  const write = new Writable({
4081cb0ef41Sopenharmony_ci    autoDestroy: false,
4091cb0ef41Sopenharmony_ci    write(chunk, enc, cb) {
4101cb0ef41Sopenharmony_ci      cb();
4111cb0ef41Sopenharmony_ci      cb();
4121cb0ef41Sopenharmony_ci    }
4131cb0ef41Sopenharmony_ci  });
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ci  write.on('error', common.mustCall(() => {
4161cb0ef41Sopenharmony_ci    assert(write._writableState.errored);
4171cb0ef41Sopenharmony_ci  }));
4181cb0ef41Sopenharmony_ci  write.write('asd');
4191cb0ef41Sopenharmony_ci}
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ci{
4221cb0ef41Sopenharmony_ci  const ac = new AbortController();
4231cb0ef41Sopenharmony_ci  const write = addAbortSignal(ac.signal, new Writable({
4241cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
4251cb0ef41Sopenharmony_ci  }));
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((e) => {
4281cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
4291cb0ef41Sopenharmony_ci    assert.strictEqual(write.destroyed, true);
4301cb0ef41Sopenharmony_ci  }));
4311cb0ef41Sopenharmony_ci  write.write('asd');
4321cb0ef41Sopenharmony_ci  ac.abort();
4331cb0ef41Sopenharmony_ci}
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci{
4361cb0ef41Sopenharmony_ci  const ac = new AbortController();
4371cb0ef41Sopenharmony_ci  const write = new Writable({
4381cb0ef41Sopenharmony_ci    signal: ac.signal,
4391cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
4401cb0ef41Sopenharmony_ci  });
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((e) => {
4431cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
4441cb0ef41Sopenharmony_ci    assert.strictEqual(write.destroyed, true);
4451cb0ef41Sopenharmony_ci  }));
4461cb0ef41Sopenharmony_ci  write.write('asd');
4471cb0ef41Sopenharmony_ci  ac.abort();
4481cb0ef41Sopenharmony_ci}
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ci{
4511cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort();
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci  const write = new Writable({
4541cb0ef41Sopenharmony_ci    signal,
4551cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
4561cb0ef41Sopenharmony_ci  });
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci  write.on('error', common.mustCall((e) => {
4591cb0ef41Sopenharmony_ci    assert.strictEqual(e.name, 'AbortError');
4601cb0ef41Sopenharmony_ci    assert.strictEqual(write.destroyed, true);
4611cb0ef41Sopenharmony_ci  }));
4621cb0ef41Sopenharmony_ci}
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci{
4651cb0ef41Sopenharmony_ci  // Destroy twice
4661cb0ef41Sopenharmony_ci  const write = new Writable({
4671cb0ef41Sopenharmony_ci    write(chunk, enc, cb) { cb(); }
4681cb0ef41Sopenharmony_ci  });
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci  write.end(common.mustCall());
4711cb0ef41Sopenharmony_ci  write.destroy();
4721cb0ef41Sopenharmony_ci  write.destroy();
4731cb0ef41Sopenharmony_ci}
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci{
4761cb0ef41Sopenharmony_ci  // https://github.com/nodejs/node/issues/39356
4771cb0ef41Sopenharmony_ci  const s = new Writable({
4781cb0ef41Sopenharmony_ci    final() {}
4791cb0ef41Sopenharmony_ci  });
4801cb0ef41Sopenharmony_ci  const _err = new Error('oh no');
4811cb0ef41Sopenharmony_ci  // Remove `callback` and it works
4821cb0ef41Sopenharmony_ci  s.end(common.mustCall((err) => {
4831cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
4841cb0ef41Sopenharmony_ci  }));
4851cb0ef41Sopenharmony_ci  s.on('error', common.mustCall((err) => {
4861cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
4871cb0ef41Sopenharmony_ci  }));
4881cb0ef41Sopenharmony_ci  s.destroy(_err);
4891cb0ef41Sopenharmony_ci}
490