Skip to content

Commit

Permalink
net: throw error to object mode in Socket
Browse files Browse the repository at this point in the history
Fixes: #40336
  • Loading branch information
watilde committed Oct 7, 2021
1 parent 87da53c commit 3964bc3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
NumberIsNaN,
NumberParseInt,
ObjectDefineProperty,
ObjectKeys,
ObjectSetPrototypeOf,
Symbol,
} = primordials;
Expand Down Expand Up @@ -282,6 +283,20 @@ const kSetNoDelay = Symbol('kSetNoDelay');

function 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;
// Problem with this is that users can supply their own handle, that may not
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-net-connect-options-invalid.js
Original file line number Diff line number Diff line change
@@ -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)
});
});
}
26 changes: 26 additions & 0 deletions test/parallel/test-socket-options-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

{
const invalidKeys = [
'objectMode',
'readableObjectMode',
'writableObjectMode',
];
invalidKeys.forEach((invalidKey) => {
const option = {
[invalidKey]: true
};
const message = `The property 'options.objectMode' is not supported. Received true`;

assert.throws(() => {
const socket = new net.Socket({ objectMode: true });
}, {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
message: new RegExp(message)
});
});
}

0 comments on commit 3964bc3

Please sign in to comment.