Skip to content

Commit 66e58d2

Browse files
committed
[fix] Make the {noS,s}erver, and port options mutually exclusive
Remove ambiguity and prevent `WebSocketServer.prototype.address()` from throwing an error if the `noServer` option is used along with the `port` and/or `server` options.
1 parent ecb9d9e commit 66e58d2

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

doc/ws.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ This class represents a WebSocket server. It extends the `EventEmitter`.
8080
- `maxPayload` {Number} The maximum allowed message size in bytes.
8181
- `callback` {Function}
8282

83-
Create a new server instance. One of `port`, `server` or `noServer` must be
84-
provided or an error is thrown. An HTTP server is automatically created,
83+
Create a new server instance. One and only one of `port`, `server` or `noServer`
84+
must be provided or an error is thrown. An HTTP server is automatically created,
8585
started, and used if `port` is set. To use an external HTTP/S server instead,
8686
specify only `server` or `noServer`. In this case the HTTP/S server must be
8787
started manually. The "noServer" mode allows the WebSocket server to be

lib/websocket-server.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ class WebSocketServer extends EventEmitter {
6262
...options
6363
};
6464

65-
if (options.port == null && !options.server && !options.noServer) {
65+
if (
66+
(options.port == null && !options.server && !options.noServer) ||
67+
(options.port != null && (options.server || options.noServer)) ||
68+
(options.server && options.noServer)
69+
) {
6670
throw new TypeError(
67-
'One of the "port", "server", or "noServer" options must be specified'
71+
'One and only one of the "port", "server", or "noServer" options ' +
72+
'must be specified'
6873
);
6974
}
7075

test/websocket-server.test.js

+35-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,44 @@ const { NOOP } = require('../lib/constants');
1818
describe('WebSocketServer', () => {
1919
describe('#ctor', () => {
2020
it('throws an error if no option object is passed', () => {
21-
assert.throws(() => new WebSocket.Server());
21+
assert.throws(
22+
() => new WebSocket.Server(),
23+
new RegExp(
24+
'^TypeError: One and only one of the "port", "server", or ' +
25+
'"noServer" options must be specified$'
26+
)
27+
);
2228
});
2329

2430
describe('options', () => {
25-
it('throws an error if no `port` or `server` option is specified', () => {
26-
assert.throws(() => new WebSocket.Server({}));
31+
it('throws an error if required options are not specified', () => {
32+
assert.throws(
33+
() => new WebSocket.Server({}),
34+
new RegExp(
35+
'^TypeError: One and only one of the "port", "server", or ' +
36+
'"noServer" options must be specified$'
37+
)
38+
);
39+
});
40+
41+
it('throws an error if mutually exclusive options are specified', () => {
42+
const server = http.createServer();
43+
const variants = [
44+
{ port: 0, noServer: true, server },
45+
{ port: 0, noServer: true },
46+
{ port: 0, server },
47+
{ noServer: true, server }
48+
];
49+
50+
for (const options of variants) {
51+
assert.throws(
52+
() => new WebSocket.Server(options),
53+
new RegExp(
54+
'^TypeError: One and only one of the "port", "server", or ' +
55+
'"noServer" options must be specified$'
56+
)
57+
);
58+
}
2759
});
2860

2961
it('exposes options passed to constructor', (done) => {

0 commit comments

Comments
 (0)