mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
http,stream: make virtual methods throw an error
Make virtual methods throw an ERR_METHOD_NOT_IMPLEMENTED error instead of emitting it. The error is not recoverable and the only way to handle it is to override the method. PR-URL: https://github.com/nodejs/node/pull/31912 Refs: https://github.com/nodejs/node/pull/31818#pullrequestreview-359403469 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
86ab4ee6e4
commit
6f0ec79e42
10 files changed
+63
-46
No files matched your search
@@ -619,7 +619,7 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
|
||||
|
||||
|
||||
OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
|
||||
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()'));
|
||||
throw new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()');
|
||||
};
|
||||
|
||||
ObjectDefineProperty(OutgoingMessage.prototype, 'headersSent', {
|
||||
|
||||
@@ -628,7 +628,7 @@ function maybeReadMore_(stream, state) {
|
||||
// for virtual (non-string, non-buffer) streams, "length" is somewhat
|
||||
// arbitrary, and perhaps not very meaningful.
|
||||
Readable.prototype._read = function(n) {
|
||||
errorOrDestroy(this, new ERR_METHOD_NOT_IMPLEMENTED('_read()'));
|
||||
throw new ERR_METHOD_NOT_IMPLEMENTED('_read()');
|
||||
};
|
||||
|
||||
Readable.prototype.pipe = function(dest, pipeOpts) {
|
||||
|
||||
@@ -163,7 +163,7 @@ Transform.prototype.push = function(chunk, encoding) {
|
||||
// an error, then that'll put the hurt on the whole operation. If you
|
||||
// never call cb(), then you'll never get another chunk.
|
||||
Transform.prototype._transform = function(chunk, encoding, cb) {
|
||||
cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()'));
|
||||
throw new ERR_METHOD_NOT_IMPLEMENTED('_transform()');
|
||||
};
|
||||
|
||||
Transform.prototype._write = function(chunk, encoding, cb) {
|
||||
|
||||
@@ -562,7 +562,7 @@ Writable.prototype._write = function(chunk, encoding, cb) {
|
||||
if (this._writev) {
|
||||
this._writev([{ chunk, encoding }], cb);
|
||||
} else {
|
||||
process.nextTick(cb, new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
|
||||
throw new ERR_METHOD_NOT_IMPLEMENTED('_write()');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
const http = require('http');
|
||||
@@ -62,13 +62,16 @@ assert.throws(() => {
|
||||
{
|
||||
const outgoingMessage = new OutgoingMessage();
|
||||
|
||||
outgoingMessage.on('error', common.expectsError({
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
name: 'Error',
|
||||
message: 'The _implicitHeader() method is not implemented'
|
||||
}));
|
||||
|
||||
outgoingMessage.write('');
|
||||
assert.throws(
|
||||
() => {
|
||||
outgoingMessage.write('');
|
||||
},
|
||||
{
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
name: 'Error',
|
||||
message: 'The _implicitHeader() method is not implemented'
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));
|
||||
|
||||
@@ -14,7 +14,9 @@ async function tests() {
|
||||
{
|
||||
const AsyncIteratorPrototype = Object.getPrototypeOf(
|
||||
Object.getPrototypeOf(async function* () {}).prototype);
|
||||
const rs = new Readable({});
|
||||
const rs = new Readable({
|
||||
read() {}
|
||||
});
|
||||
assert.strictEqual(
|
||||
Object.getPrototypeOf(Object.getPrototypeOf(rs[Symbol.asyncIterator]())),
|
||||
AsyncIteratorPrototype);
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
const { Readable } = require('stream');
|
||||
|
||||
const readable = new Readable();
|
||||
|
||||
readable.on('error', common.expectsError({
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
name: 'Error',
|
||||
message: 'The _read() method is not implemented'
|
||||
}));
|
||||
|
||||
readable.read();
|
||||
assert.throws(
|
||||
() => {
|
||||
readable.read();
|
||||
},
|
||||
{
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
name: 'Error',
|
||||
message: 'The _read() method is not implemented'
|
||||
}
|
||||
);
|
||||
@@ -1,18 +1,21 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
|
||||
const { strictEqual } = require('assert');
|
||||
const assert = require('assert');
|
||||
const { Transform } = require('stream');
|
||||
|
||||
const t = new Transform();
|
||||
|
||||
t.on('error', common.expectsError({
|
||||
name: 'Error',
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
message: 'The _transform() method is not implemented'
|
||||
}));
|
||||
|
||||
t.end(Buffer.from('blerg'));
|
||||
assert.throws(
|
||||
() => {
|
||||
t.end(Buffer.from('blerg'));
|
||||
},
|
||||
{
|
||||
name: 'Error',
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
message: 'The _transform() method is not implemented'
|
||||
}
|
||||
);
|
||||
|
||||
const _transform = common.mustCall((chunk, _, next) => {
|
||||
next();
|
||||
@@ -32,9 +35,9 @@ const t2 = new Transform({
|
||||
final: _final
|
||||
});
|
||||
|
||||
strictEqual(t2._transform, _transform);
|
||||
strictEqual(t2._flush, _flush);
|
||||
strictEqual(t2._final, _final);
|
||||
assert.strictEqual(t2._transform, _transform);
|
||||
assert.strictEqual(t2._flush, _flush);
|
||||
assert.strictEqual(t2._final, _final);
|
||||
|
||||
t2.end(Buffer.from('blerg'));
|
||||
t2.resume();
|
||||
@@ -1,34 +1,36 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
|
||||
const { strictEqual } = require('assert');
|
||||
const assert = require('assert');
|
||||
const { Writable } = require('stream');
|
||||
|
||||
const bufferBlerg = Buffer.from('blerg');
|
||||
const w = new Writable();
|
||||
|
||||
w.on('error', common.expectsError({
|
||||
name: 'Error',
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
message: 'The _write() method is not implemented'
|
||||
}));
|
||||
|
||||
const bufferBlerg = Buffer.from('blerg');
|
||||
|
||||
w.end(bufferBlerg);
|
||||
assert.throws(
|
||||
() => {
|
||||
w.end(bufferBlerg);
|
||||
},
|
||||
{
|
||||
name: 'Error',
|
||||
code: 'ERR_METHOD_NOT_IMPLEMENTED',
|
||||
message: 'The _write() method is not implemented'
|
||||
}
|
||||
);
|
||||
|
||||
const _write = common.mustCall((chunk, _, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
const _writev = common.mustCall((chunks, next) => {
|
||||
strictEqual(chunks.length, 2);
|
||||
assert.strictEqual(chunks.length, 2);
|
||||
next();
|
||||
});
|
||||
|
||||
const w2 = new Writable({ write: _write, writev: _writev });
|
||||
|
||||
strictEqual(w2._write, _write);
|
||||
strictEqual(w2._writev, _writev);
|
||||
assert.strictEqual(w2._write, _write);
|
||||
assert.strictEqual(w2._writev, _writev);
|
||||
|
||||
w2.write(bufferBlerg);
|
||||
|
||||
|
||||
@@ -21,7 +21,10 @@ const tls = require('tls');
|
||||
{
|
||||
// The option is ignored when the `socket` argument is a generic
|
||||
// `stream.Duplex`.
|
||||
const duplex = new stream.Duplex({ allowHalfOpen: false });
|
||||
const duplex = new stream.Duplex({
|
||||
allowHalfOpen: false,
|
||||
read() {}
|
||||
});
|
||||
const socket = new tls.TLSSocket(duplex, { allowHalfOpen: true });
|
||||
assert.strictEqual(socket.allowHalfOpen, false);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user