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: https://github.com/nodejs/node/issues/16712 PR-URL: https://github.com/nodejs/node/pull/22085 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
7aac70607d
commit
4a0466f23a
@ -68,6 +68,7 @@ const errors = require('internal/errors');
|
|||||||
const {
|
const {
|
||||||
ERR_INVALID_ADDRESS_FAMILY,
|
ERR_INVALID_ADDRESS_FAMILY,
|
||||||
ERR_INVALID_ARG_TYPE,
|
ERR_INVALID_ARG_TYPE,
|
||||||
|
ERR_INVALID_ARG_VALUE,
|
||||||
ERR_INVALID_FD_TYPE,
|
ERR_INVALID_FD_TYPE,
|
||||||
ERR_INVALID_IP_ADDRESS,
|
ERR_INVALID_IP_ADDRESS,
|
||||||
ERR_INVALID_OPT_VALUE,
|
ERR_INVALID_OPT_VALUE,
|
||||||
@ -1496,6 +1497,11 @@ Server.prototype.listen = function(...args) {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(('port' in options) || ('path' in options))) {
|
||||||
|
throw new ERR_INVALID_ARG_VALUE('options', options,
|
||||||
|
'must have the property "port" or "path"');
|
||||||
|
}
|
||||||
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options));
|
throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -57,12 +57,23 @@ const listenOnPort = [
|
|||||||
const block = () => {
|
const block = () => {
|
||||||
net.createServer().listen(options, common.mustNotCall());
|
net.createServer().listen(options, common.mustNotCall());
|
||||||
};
|
};
|
||||||
common.expectsError(block,
|
|
||||||
{
|
if (typeof options === 'object' &&
|
||||||
code: 'ERR_INVALID_OPT_VALUE',
|
!(('port' in options) || ('path' in options))) {
|
||||||
type: TypeError,
|
common.expectsError(block,
|
||||||
message: /^The value "{.*}" is invalid for option "options"$/
|
{
|
||||||
});
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
type: TypeError,
|
||||||
|
message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
common.expectsError(block,
|
||||||
|
{
|
||||||
|
code: 'ERR_INVALID_OPT_VALUE',
|
||||||
|
type: TypeError,
|
||||||
|
message: /^The value "{.*}" is invalid for option "options"(?:\. .+)?$/,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldFailToListen(false, { port: false });
|
shouldFailToListen(false, { port: false });
|
||||||
@ -73,6 +84,11 @@ const listenOnPort = [
|
|||||||
shouldFailToListen({ fd: -1 });
|
shouldFailToListen({ fd: -1 });
|
||||||
// Invalid path in listen(options)
|
// Invalid path in listen(options)
|
||||||
shouldFailToListen({ path: -1 });
|
shouldFailToListen({ path: -1 });
|
||||||
// Host without port
|
|
||||||
|
// Neither port or path are specified in options
|
||||||
|
shouldFailToListen({});
|
||||||
shouldFailToListen({ host: 'localhost' });
|
shouldFailToListen({ host: 'localhost' });
|
||||||
|
shouldFailToListen({ host: 'localhost:3000' });
|
||||||
|
shouldFailToListen({ host: { port: 3000 } });
|
||||||
|
shouldFailToListen({ exclusive: true });
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user