Skip to content

Commit 8b918b0

Browse files
committed
[feature] Introduce the protocols option
Allow specifying subprotocols with the `protocols` option. Refs: whatwg/websockets#42
1 parent 73e03eb commit 8b918b0

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎doc/ws.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ This class represents a WebSocket. It extends the `EventEmitter`.
307307
### new WebSocket(address[, protocols][, options])
308308

309309
- `address` {String|url.URL} The URL to which to connect.
310-
- `protocols` {String|Array} The list of subprotocols.
310+
- `protocols` {String|Array} The list of subprotocols. If specified, this
311+
overrides the `protocols` option.
311312
- `options` {Object}
312313
- `allowSynchronousEvents` {Boolean} Specifies whether any of the `'message'`,
313314
`'ping'`, and `'pong'` events can be emitted multiple times in the same
@@ -342,6 +343,7 @@ This class represents a WebSocket. It extends the `EventEmitter`.
342343
- `origin` {String} Value of the `Origin` or `Sec-WebSocket-Origin` header
343344
depending on the `protocolVersion`.
344345
- `perMessageDeflate` {Boolean|Object} Enable/disable permessage-deflate.
346+
- `protocols` {String|Array} The list of subprotocols.
345347
- `protocolVersion` {Number} Value of the `Sec-WebSocket-Version` header.
346348
- `skipUTF8Validation` {Boolean} Specifies whether or not to skip UTF-8
347349
validation for text and close messages. Defaults to `false`. Set to `true`

‎lib/websocket.js‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,24 @@ class WebSocket extends EventEmitter {
7575
this._redirects = 0;
7676

7777
if (protocols === undefined) {
78-
protocols = [];
78+
if (!options || options.protocols === undefined) {
79+
protocols = [];
80+
} else if (Array.isArray(options.protocols)) {
81+
protocols = options.protocols;
82+
} else {
83+
protocols = [options.protocols];
84+
}
7985
} else if (!Array.isArray(protocols)) {
8086
if (typeof protocols === 'object' && protocols !== null) {
8187
options = protocols;
82-
protocols = [];
88+
89+
if (options.protocols === undefined) {
90+
protocols = [];
91+
} else if (Array.isArray(options.protocols)) {
92+
protocols = options.protocols;
93+
} else {
94+
protocols = [options.protocols];
95+
}
8396
} else {
8497
protocols = [protocols];
8598
}
@@ -681,6 +694,7 @@ function initAsClient(websocket, address, protocols, options) {
681694
socketPath: undefined,
682695
hostname: undefined,
683696
protocol: undefined,
697+
protocols: undefined,
684698
timeout: undefined,
685699
method: 'GET',
686700
host: undefined,

‎test/websocket.test.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4389,6 +4389,22 @@ describe('WebSocket', () => {
43894389
ws.close();
43904390
});
43914391
});
4392+
4393+
it('honors the `protocols` option', () => {
4394+
const url = 'ws://localhost';
4395+
function lookup() {}
4396+
4397+
for (const args of [
4398+
[url, { lookup, protocols: ['foo'] }],
4399+
[url, { lookup, protocols: 'foo' }],
4400+
[url, undefined, { lookup, protocols: ['foo'] }],
4401+
[url, undefined, { lookup, protocols: 'foo' }],
4402+
[url, 'foo', { lookup, protocols: ['bar'] }]
4403+
]) {
4404+
const ws = new WebSocket(...args);
4405+
assert.strictEqual(ws._req.getHeader('sec-websocket-protocol'), 'foo');
4406+
}
4407+
});
43924408
});
43934409

43944410
describe('permessage-deflate', () => {

0 commit comments

Comments
 (0)