Skip to content

Commit 4a0466f

Browse files
yanivfriedensohnaddaleax
authored andcommitted
net: throw error if port/path does not exist in options
Throw error ERR_INVALID_ARG_VALUE if the port and path properties do not exist in the options object argument when calling server.listen(). Refs: #16712 PR-URL: #22085 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 7aac706 commit 4a0466f

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

lib/net.js

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const errors = require('internal/errors');
6868
const {
6969
ERR_INVALID_ADDRESS_FAMILY,
7070
ERR_INVALID_ARG_TYPE,
71+
ERR_INVALID_ARG_VALUE,
7172
ERR_INVALID_FD_TYPE,
7273
ERR_INVALID_IP_ADDRESS,
7374
ERR_INVALID_OPT_VALUE,
@@ -1496,6 +1497,11 @@ Server.prototype.listen = function(...args) {
14961497
return this;
14971498
}
14981499

1500+
if (!(('port' in options) || ('path' in options))) {
1501+
throw new ERR_INVALID_ARG_VALUE('options', options,
1502+
'must have the property "port" or "path"');
1503+
}
1504+
14991505
throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options));
15001506
};
15011507

test/parallel/test-net-server-listen-options.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@ const listenOnPort = [
5757
const block = () => {
5858
net.createServer().listen(options, common.mustNotCall());
5959
};
60-
common.expectsError(block,
61-
{
62-
code: 'ERR_INVALID_OPT_VALUE',
63-
type: TypeError,
64-
message: /^The value "{.*}" is invalid for option "options"$/
65-
});
60+
61+
if (typeof options === 'object' &&
62+
!(('port' in options) || ('path' in options))) {
63+
common.expectsError(block,
64+
{
65+
code: 'ERR_INVALID_ARG_VALUE',
66+
type: TypeError,
67+
message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/,
68+
});
69+
} else {
70+
common.expectsError(block,
71+
{
72+
code: 'ERR_INVALID_OPT_VALUE',
73+
type: TypeError,
74+
message: /^The value "{.*}" is invalid for option "options"(?:\. .+)?$/,
75+
});
76+
}
6677
}
6778

6879
shouldFailToListen(false, { port: false });
@@ -73,6 +84,11 @@ const listenOnPort = [
7384
shouldFailToListen({ fd: -1 });
7485
// Invalid path in listen(options)
7586
shouldFailToListen({ path: -1 });
76-
// Host without port
87+
88+
// Neither port or path are specified in options
89+
shouldFailToListen({});
7790
shouldFailToListen({ host: 'localhost' });
91+
shouldFailToListen({ host: 'localhost:3000' });
92+
shouldFailToListen({ host: { port: 3000 } });
93+
shouldFailToListen({ exclusive: true });
7894
}

0 commit comments

Comments
 (0)