diff --git a/lib/net.js b/lib/net.js index ded48732c2e11e..41ff284e1ec027 100644 --- a/lib/net.js +++ b/lib/net.js @@ -282,6 +282,21 @@ const kSetNoDelay = Symbol('kSetNoDelay'); function Socket(options) { if (!(this instanceof Socket)) return new Socket(options); + if (options?.objectMode) { + throw new ERR_INVALID_ARG_VALUE( + 'options.objectMode', + options.objectMode, + 'is not supported' + ); + } else if (options?.readableObjectMode || options?.writableObjectMode) { + throw new ERR_INVALID_ARG_VALUE( + `options.${ + options.readableObjectMode ? 'readableObjectMode' : 'writableObjectMode' + }`, + options.readableObjectMode || options.writableObjectMode, + 'is not supported' + ); + } this.connecting = false; // Problem with this is that users can supply their own handle, that may not diff --git a/test/parallel/test-net-connect-options-invalid.js b/test/parallel/test-net-connect-options-invalid.js new file mode 100644 index 00000000000000..05a565463099ec --- /dev/null +++ b/test/parallel/test-net-connect-options-invalid.js @@ -0,0 +1,27 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const net = require('net'); + +{ + const invalidKeys = [ + 'objectMode', + 'readableObjectMode', + 'writableObjectMode', + ]; + invalidKeys.forEach((invalidKey) => { + const option = { + port: 8080, + [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) + }); + }); +} diff --git a/test/parallel/test-socket-options-invalid.js b/test/parallel/test-socket-options-invalid.js new file mode 100644 index 00000000000000..32bf0810c65692 --- /dev/null +++ b/test/parallel/test-socket-options-invalid.js @@ -0,0 +1,27 @@ +'use strict'; +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.${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) + }); + }); +}