Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: throw error to object mode in Socket #40344

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
watilde marked this conversation as resolved.
Show resolved Hide resolved
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'
);
}
});
watilde marked this conversation as resolved.
Show resolved Hide resolved

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,
watilde marked this conversation as resolved.
Show resolved Hide resolved
[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 changes: 28 additions & 0 deletions test/parallel/test-socket-options-invalid.js
Original file line number Diff line number Diff line change
@@ -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,
watilde marked this conversation as resolved.
Show resolved Hide resolved
[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)
});
});
}