Skip to content

Commit 4644662

Browse files
watildenodejs-github-bot
authored andcommitted
net: throw error to object mode in Socket
Fixes: #40336 PR-URL: #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>
1 parent c350c21 commit 4644662

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

lib/net.js

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
NumberIsNaN,
3131
NumberParseInt,
3232
ObjectDefineProperty,
33+
ObjectKeys,
3334
ObjectSetPrototypeOf,
3435
Symbol,
3536
} = primordials;
@@ -282,6 +283,20 @@ const kSetNoDelay = Symbol('kSetNoDelay');
282283

283284
function Socket(options) {
284285
if (!(this instanceof Socket)) return new Socket(options);
286+
const invalidKeys = [
287+
'objectMode',
288+
'readableObjectMode',
289+
'writableObjectMode',
290+
];
291+
invalidKeys.forEach((invalidKey) => {
292+
if (ObjectKeys(options).includes(invalidKey)) {
293+
throw new ERR_INVALID_ARG_VALUE(
294+
`options.${invalidKey}`,
295+
options[invalidKey],
296+
'is not supported'
297+
);
298+
}
299+
});
285300

286301
this.connecting = false;
287302
// Problem with this is that users can supply their own handle, that may not
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
{
7+
const invalidKeys = [
8+
'objectMode',
9+
'readableObjectMode',
10+
'writableObjectMode',
11+
];
12+
invalidKeys.forEach((invalidKey) => {
13+
const option = {
14+
...common.localhostIPv4,
15+
[invalidKey]: true
16+
};
17+
const message = `The property 'options.${invalidKey}' is not supported. Received true`;
18+
19+
assert.throws(() => {
20+
net.createConnection(option);
21+
}, {
22+
code: 'ERR_INVALID_ARG_VALUE',
23+
name: 'TypeError',
24+
message: new RegExp(message)
25+
});
26+
});
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
{
7+
const invalidKeys = [
8+
'objectMode',
9+
'readableObjectMode',
10+
'writableObjectMode',
11+
];
12+
invalidKeys.forEach((invalidKey) => {
13+
const option = {
14+
...common.localhostIPv4,
15+
[invalidKey]: true
16+
};
17+
const message = `The property 'options.${invalidKey}' is not supported. Received true`;
18+
19+
assert.throws(() => {
20+
const socket = new net.Socket(option);
21+
socket.connect({ port: 8080 });
22+
}, {
23+
code: 'ERR_INVALID_ARG_VALUE',
24+
name: 'TypeError',
25+
message: new RegExp(message)
26+
});
27+
});
28+
}

0 commit comments

Comments
 (0)