net: throw error to object mode in Socket
Fixes: https://github.com/nodejs/node/issues/40336 PR-URL: https://github.com/nodejs/node/pull/40344 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
This commit is contained in:
parent
c350c217f4
commit
46446623f5
15
lib/net.js
15
lib/net.js
@ -30,6 +30,7 @@ const {
|
|||||||
NumberIsNaN,
|
NumberIsNaN,
|
||||||
NumberParseInt,
|
NumberParseInt,
|
||||||
ObjectDefineProperty,
|
ObjectDefineProperty,
|
||||||
|
ObjectKeys,
|
||||||
ObjectSetPrototypeOf,
|
ObjectSetPrototypeOf,
|
||||||
Symbol,
|
Symbol,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
@ -282,6 +283,20 @@ const kSetNoDelay = Symbol('kSetNoDelay');
|
|||||||
|
|
||||||
function Socket(options) {
|
function Socket(options) {
|
||||||
if (!(this instanceof Socket)) return new Socket(options);
|
if (!(this instanceof Socket)) return new Socket(options);
|
||||||
|
const invalidKeys = [
|
||||||
|
'objectMode',
|
||||||
|
'readableObjectMode',
|
||||||
|
'writableObjectMode',
|
||||||
|
];
|
||||||
|
invalidKeys.forEach((invalidKey) => {
|
||||||
|
if (ObjectKeys(options).includes(invalidKey)) {
|
||||||
|
throw new ERR_INVALID_ARG_VALUE(
|
||||||
|
`options.${invalidKey}`,
|
||||||
|
options[invalidKey],
|
||||||
|
'is not supported'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
this.connecting = false;
|
this.connecting = false;
|
||||||
// Problem with this is that users can supply their own handle, that may not
|
// Problem with this is that users can supply their own handle, that may not
|
||||||
|
27
test/parallel/test-net-connect-options-invalid.js
Normal file
27
test/parallel/test-net-connect-options-invalid.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const net = require('net');
|
||||||
|
|
||||||
|
{
|
||||||
|
const invalidKeys = [
|
||||||
|
'objectMode',
|
||||||
|
'readableObjectMode',
|
||||||
|
'writableObjectMode',
|
||||||
|
];
|
||||||
|
invalidKeys.forEach((invalidKey) => {
|
||||||
|
const option = {
|
||||||
|
...common.localhostIPv4,
|
||||||
|
[invalidKey]: true
|
||||||
|
};
|
||||||
|
const message = `The property 'options.${invalidKey}' is not supported. Received true`;
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
net.createConnection(option);
|
||||||
|
}, {
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
name: 'TypeError',
|
||||||
|
message: new RegExp(message)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
28
test/parallel/test-socket-options-invalid.js
Normal file
28
test/parallel/test-socket-options-invalid.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const net = require('net');
|
||||||
|
|
||||||
|
{
|
||||||
|
const invalidKeys = [
|
||||||
|
'objectMode',
|
||||||
|
'readableObjectMode',
|
||||||
|
'writableObjectMode',
|
||||||
|
];
|
||||||
|
invalidKeys.forEach((invalidKey) => {
|
||||||
|
const option = {
|
||||||
|
...common.localhostIPv4,
|
||||||
|
[invalidKey]: true
|
||||||
|
};
|
||||||
|
const message = `The property 'options.${invalidKey}' is not supported. Received true`;
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
const socket = new net.Socket(option);
|
||||||
|
socket.connect({ port: 8080 });
|
||||||
|
}, {
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
name: 'TypeError',
|
||||||
|
message: new RegExp(message)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user