mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 17:01:08 +00:00

errorOrDestroy emits 'error' synchronously due to compat reasons. However, it should be possible to use correct async behaviour for new code. PR-URL: https://github.com/nodejs/node/pull/29744 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
60 lines
1.1 KiB
JavaScript
60 lines
1.1 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
|
|
const { Writable } = require('stream');
|
|
|
|
function expectError(w, arg, code) {
|
|
let errorCalled = false;
|
|
let ticked = false;
|
|
w.write(arg, common.mustCall((err) => {
|
|
assert.strictEqual(ticked, true);
|
|
assert.strictEqual(errorCalled, false);
|
|
assert.strictEqual(err.code, code);
|
|
}));
|
|
ticked = true;
|
|
w.on('error', common.mustCall((err) => {
|
|
errorCalled = true;
|
|
assert.strictEqual(err.code, code);
|
|
}));
|
|
}
|
|
|
|
function test(autoDestroy) {
|
|
{
|
|
const w = new Writable({
|
|
autoDestroy,
|
|
_write() {}
|
|
});
|
|
w.end();
|
|
expectError(w, 'asd', 'ERR_STREAM_WRITE_AFTER_END');
|
|
}
|
|
|
|
{
|
|
const w = new Writable({
|
|
autoDestroy,
|
|
_write() {}
|
|
});
|
|
w.destroy();
|
|
expectError(w, 'asd', 'ERR_STREAM_DESTROYED');
|
|
}
|
|
|
|
{
|
|
const w = new Writable({
|
|
autoDestroy,
|
|
_write() {}
|
|
});
|
|
expectError(w, null, 'ERR_STREAM_NULL_VALUES');
|
|
}
|
|
|
|
{
|
|
const w = new Writable({
|
|
autoDestroy,
|
|
_write() {}
|
|
});
|
|
expectError(w, {}, 'ERR_INVALID_ARG_TYPE');
|
|
}
|
|
}
|
|
|
|
test(false);
|
|
test(true);
|