mirror of https://github.com/nodejs/node.git
stream: make virtual methods errors consistent
Use the same error code and always emit the error instead of throwing it. PR-URL: https://github.com/nodejs/node/pull/18813 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Michaë Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
8403f00dc3
commit
c9794880e8
|
@ -616,7 +616,7 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
|
||||||
|
|
||||||
|
|
||||||
OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
|
OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
|
||||||
throw new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()');
|
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()'));
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
|
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
|
||||||
|
|
|
@ -35,7 +35,7 @@ const { getHighWaterMark } = require('internal/streams/state');
|
||||||
const {
|
const {
|
||||||
ERR_INVALID_ARG_TYPE,
|
ERR_INVALID_ARG_TYPE,
|
||||||
ERR_STREAM_PUSH_AFTER_EOF,
|
ERR_STREAM_PUSH_AFTER_EOF,
|
||||||
ERR_STREAM_READ_NOT_IMPLEMENTED,
|
ERR_METHOD_NOT_IMPLEMENTED,
|
||||||
ERR_STREAM_UNSHIFT_AFTER_END_EVENT
|
ERR_STREAM_UNSHIFT_AFTER_END_EVENT
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
const ReadableAsyncIterator = require('internal/streams/async_iterator');
|
const ReadableAsyncIterator = require('internal/streams/async_iterator');
|
||||||
|
@ -568,7 +568,7 @@ function maybeReadMore_(stream, state) {
|
||||||
// for virtual (non-string, non-buffer) streams, "length" is somewhat
|
// for virtual (non-string, non-buffer) streams, "length" is somewhat
|
||||||
// arbitrary, and perhaps not very meaningful.
|
// arbitrary, and perhaps not very meaningful.
|
||||||
Readable.prototype._read = function(n) {
|
Readable.prototype._read = function(n) {
|
||||||
this.emit('error', new ERR_STREAM_READ_NOT_IMPLEMENTED());
|
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_read()'));
|
||||||
};
|
};
|
||||||
|
|
||||||
Readable.prototype.pipe = function(dest, pipeOpts) {
|
Readable.prototype.pipe = function(dest, pipeOpts) {
|
||||||
|
|
|
@ -162,7 +162,7 @@ Transform.prototype.push = function(chunk, encoding) {
|
||||||
// an error, then that'll put the hurt on the whole operation. If you
|
// an error, then that'll put the hurt on the whole operation. If you
|
||||||
// never call cb(), then you'll never get another chunk.
|
// never call cb(), then you'll never get another chunk.
|
||||||
Transform.prototype._transform = function(chunk, encoding, cb) {
|
Transform.prototype._transform = function(chunk, encoding, cb) {
|
||||||
throw new ERR_METHOD_NOT_IMPLEMENTED('_transform');
|
cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()'));
|
||||||
};
|
};
|
||||||
|
|
||||||
Transform.prototype._write = function(chunk, encoding, cb) {
|
Transform.prototype._write = function(chunk, encoding, cb) {
|
||||||
|
|
|
@ -553,7 +553,7 @@ function clearBuffer(stream, state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Writable.prototype._write = function(chunk, encoding, cb) {
|
Writable.prototype._write = function(chunk, encoding, cb) {
|
||||||
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write'));
|
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
|
||||||
};
|
};
|
||||||
|
|
||||||
Writable.prototype._writev = null;
|
Writable.prototype._writev = null;
|
||||||
|
|
|
@ -849,7 +849,6 @@ E('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable', Error);
|
||||||
E('ERR_STREAM_DESTROYED', 'Cannot call %s after a stream was destroyed', Error);
|
E('ERR_STREAM_DESTROYED', 'Cannot call %s after a stream was destroyed', Error);
|
||||||
E('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
|
E('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
|
||||||
E('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF', Error);
|
E('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF', Error);
|
||||||
E('ERR_STREAM_READ_NOT_IMPLEMENTED', '_read() is not implemented', Error);
|
|
||||||
E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT',
|
E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT',
|
||||||
'stream.unshift() after end event', Error);
|
'stream.unshift() after end event', Error);
|
||||||
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
|
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
|
||||||
|
|
|
@ -7,14 +7,6 @@ const OutgoingMessage = http.OutgoingMessage;
|
||||||
const ClientRequest = http.ClientRequest;
|
const ClientRequest = http.ClientRequest;
|
||||||
const ServerResponse = http.ServerResponse;
|
const ServerResponse = http.ServerResponse;
|
||||||
|
|
||||||
common.expectsError(
|
|
||||||
OutgoingMessage.prototype._implicitHeader,
|
|
||||||
{
|
|
||||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
|
||||||
type: Error,
|
|
||||||
message: 'The _implicitHeader() method is not implemented'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
typeof ClientRequest.prototype._implicitHeader, 'function');
|
typeof ClientRequest.prototype._implicitHeader, 'function');
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
|
@ -67,14 +59,17 @@ common.expectsError(() => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// write
|
// write
|
||||||
common.expectsError(() => {
|
{
|
||||||
const outgoingMessage = new OutgoingMessage();
|
const outgoingMessage = new OutgoingMessage();
|
||||||
outgoingMessage.write();
|
|
||||||
}, {
|
outgoingMessage.on('error', common.expectsError({
|
||||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||||
type: Error,
|
type: Error,
|
||||||
message: 'The _implicitHeader() method is not implemented'
|
message: 'The _implicitHeader() method is not implemented'
|
||||||
});
|
}));
|
||||||
|
|
||||||
|
outgoingMessage.write('');
|
||||||
|
}
|
||||||
|
|
||||||
assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));
|
assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const stream = require('stream');
|
|
||||||
|
|
||||||
const readable = new stream.Readable();
|
const { Readable } = require('stream');
|
||||||
|
|
||||||
common.expectsError(() => readable.read(), {
|
const readable = new Readable();
|
||||||
code: 'ERR_STREAM_READ_NOT_IMPLEMENTED',
|
|
||||||
|
readable.on('error', common.expectsError({
|
||||||
|
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||||
type: Error,
|
type: Error,
|
||||||
message: '_read() is not implemented'
|
message: 'The _read() method is not implemented'
|
||||||
});
|
}));
|
||||||
|
|
||||||
|
readable.read();
|
||||||
|
|
|
@ -4,6 +4,16 @@ const common = require('../common');
|
||||||
const { strictEqual } = require('assert');
|
const { strictEqual } = require('assert');
|
||||||
const { Transform } = require('stream');
|
const { Transform } = require('stream');
|
||||||
|
|
||||||
|
const t = new Transform();
|
||||||
|
|
||||||
|
t.on('error', common.expectsError({
|
||||||
|
type: Error,
|
||||||
|
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||||
|
message: 'The _transform() method is not implemented'
|
||||||
|
}));
|
||||||
|
|
||||||
|
t.end(Buffer.from('blerg'));
|
||||||
|
|
||||||
const _transform = common.mustCall((chunk, _, next) => {
|
const _transform = common.mustCall((chunk, _, next) => {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
@ -16,25 +26,15 @@ const _flush = common.mustCall((next) => {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
const t = new Transform({
|
const t2 = new Transform({
|
||||||
transform: _transform,
|
transform: _transform,
|
||||||
flush: _flush,
|
flush: _flush,
|
||||||
final: _final
|
final: _final
|
||||||
});
|
});
|
||||||
|
|
||||||
strictEqual(t._transform, _transform);
|
strictEqual(t2._transform, _transform);
|
||||||
strictEqual(t._flush, _flush);
|
strictEqual(t2._flush, _flush);
|
||||||
strictEqual(t._final, _final);
|
strictEqual(t2._final, _final);
|
||||||
|
|
||||||
t.end(Buffer.from('blerg'));
|
|
||||||
t.resume();
|
|
||||||
|
|
||||||
const t2 = new Transform({});
|
|
||||||
|
|
||||||
common.expectsError(() => {
|
|
||||||
t2.end(Buffer.from('blerg'));
|
t2.end(Buffer.from('blerg'));
|
||||||
}, {
|
t2.resume();
|
||||||
type: Error,
|
|
||||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
|
||||||
message: 'The _transform method is not implemented'
|
|
||||||
});
|
|
||||||
|
|
|
@ -4,6 +4,16 @@ const common = require('../common');
|
||||||
const { strictEqual } = require('assert');
|
const { strictEqual } = require('assert');
|
||||||
const { Writable } = require('stream');
|
const { Writable } = require('stream');
|
||||||
|
|
||||||
|
const w = new Writable();
|
||||||
|
|
||||||
|
w.on('error', common.expectsError({
|
||||||
|
type: Error,
|
||||||
|
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||||
|
message: 'The _write() method is not implemented'
|
||||||
|
}));
|
||||||
|
|
||||||
|
w.end(Buffer.from('blerg'));
|
||||||
|
|
||||||
const _write = common.mustCall((chunk, _, next) => {
|
const _write = common.mustCall((chunk, _, next) => {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
@ -13,25 +23,15 @@ const _writev = common.mustCall((chunks, next) => {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
const w = new Writable({ write: _write, writev: _writev });
|
const w2 = new Writable({ write: _write, writev: _writev });
|
||||||
|
|
||||||
strictEqual(w._write, _write);
|
strictEqual(w2._write, _write);
|
||||||
strictEqual(w._writev, _writev);
|
strictEqual(w2._writev, _writev);
|
||||||
|
|
||||||
w.write(Buffer.from('blerg'));
|
w2.write(Buffer.from('blerg'));
|
||||||
|
|
||||||
w.cork();
|
w2.cork();
|
||||||
w.write(Buffer.from('blerg'));
|
w2.write(Buffer.from('blerg'));
|
||||||
w.write(Buffer.from('blerg'));
|
w2.write(Buffer.from('blerg'));
|
||||||
|
|
||||||
w.end();
|
w2.end();
|
||||||
|
|
||||||
const w2 = new Writable();
|
|
||||||
|
|
||||||
w2.on('error', common.expectsError({
|
|
||||||
type: Error,
|
|
||||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
|
||||||
message: 'The _write method is not implemented'
|
|
||||||
}));
|
|
||||||
|
|
||||||
w2.end(Buffer.from('blerg'));
|
|
||||||
|
|
Loading…
Reference in New Issue