Skip to content

Commit 20bd7c7

Browse files
committed
[fix] Don't reassign the options argument when protocols is null
1 parent 4598093 commit 20bd7c7

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

lib/WebSocket.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ class WebSocket extends EventEmitter {
3939
constructor (address, protocols, options) {
4040
super();
4141

42-
if (typeof protocols === 'object' && !Array.isArray(protocols)) {
42+
if (!protocols) {
43+
protocols = [];
44+
} else if (typeof protocols === 'string') {
45+
protocols = [protocols];
46+
} else if (!Array.isArray(protocols)) {
4347
options = protocols;
44-
protocols = null;
48+
protocols = [];
4549
}
4650

47-
if (typeof protocols === 'string') protocols = [protocols];
48-
if (!Array.isArray(protocols)) protocols = [];
49-
5051
this.readyState = WebSocket.CONNECTING;
5152
this.bytesReceived = 0;
5253
this.extensions = {};

test/WebSocket.test.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('WebSocket', function () {
3131
});
3232

3333
describe('options', function () {
34-
it('should accept an `agent` option', function (done) {
34+
it('accepts an `agent` option', function (done) {
3535
const agent = new CustomAgent();
3636

3737
agent.addRequest = () => {
@@ -41,11 +41,18 @@ describe('WebSocket', function () {
4141
const ws = new WebSocket('ws://localhost', { agent });
4242
});
4343

44-
// GH-227
45-
it('should accept the `options` object as the 3rd argument', function () {
46-
const ws = new WebSocket('ws://localhost', [], {
47-
agent: new CustomAgent()
48-
});
44+
it('accepts the `options` object as the 3rd argument', function () {
45+
const agent = new CustomAgent();
46+
let count = 0;
47+
let ws;
48+
49+
agent.addRequest = (req) => count++;
50+
51+
ws = new WebSocket('ws://localhost', undefined, { agent });
52+
ws = new WebSocket('ws://localhost', null, { agent });
53+
ws = new WebSocket('ws://localhost', [], { agent });
54+
55+
assert.strictEqual(count, 3);
4956
});
5057

5158
it('throws an error when using an invalid `protocolVersion`', function () {
@@ -57,7 +64,7 @@ describe('WebSocket', function () {
5764
);
5865
});
5966

60-
it('should accept the localAddress option', function (done) {
67+
it('accepts the localAddress option', function (done) {
6168
//
6269
// Skip this test on macOS as by default all loopback addresses other
6370
// than 127.0.0.1 are disabled.
@@ -76,14 +83,14 @@ describe('WebSocket', function () {
7683
});
7784
});
7885

79-
it('should accept the localAddress option whether it was wrong interface', function () {
86+
it('accepts the localAddress option whether it was wrong interface', function () {
8087
assert.throws(
8188
() => new WebSocket(`ws://localhost:${port}`, { localAddress: '123.456.789.428' }),
8289
/must be a valid IP: 123.456.789.428/
8390
);
8491
});
8592

86-
it('should accept the family option', function (done) {
93+
it('accepts the family option', function (done) {
8794
const wss = new WebSocketServer({ host: '::1', port: ++port }, () => {
8895
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
8996
});

0 commit comments

Comments
 (0)