Skip to content

Commit 692d7b4

Browse files
committedApr 25, 2019
[major] Drop support for url.Url in the WebSocket constructor
1 parent 1e6999b commit 692d7b4

File tree

3 files changed

+10
-43
lines changed

3 files changed

+10
-43
lines changed
 

‎doc/ws.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ This class represents a WebSocket. It extends the `EventEmitter`.
234234

235235
### new WebSocket(address[, protocols][, options])
236236

237-
- `address` {String|url.Url|url.URL} The URL to which to connect.
237+
- `address` {String|url.URL} The URL to which to connect.
238238
- `protocols` {String|Array} The list of subprotocols.
239239
- `options` {Object}
240240
- `followRedirects` {Boolean} Whether or not to follow redirects. Defaults to

‎lib/websocket.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class WebSocket extends EventEmitter {
3636
/**
3737
* Create a new `WebSocket`.
3838
*
39-
* @param {(String|url.Url|url.URL)} address The URL to which to connect
39+
* @param {(String|url.URL)} address The URL to which to connect
4040
* @param {(String|String[])} protocols The subprotocols
4141
* @param {Object} options Connection options
4242
*/
@@ -427,7 +427,7 @@ module.exports = WebSocket;
427427
* Initialize a WebSocket client.
428428
*
429429
* @param {WebSocket} websocket The client to initialize
430-
* @param {(String|url.Url|url.URL)} address The URL to which to connect
430+
* @param {(String|url.URL)} address The URL to which to connect
431431
* @param {String} protocols The subprotocols
432432
* @param {Object} options Connection options
433433
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
@@ -476,7 +476,7 @@ function initAsClient(websocket, address, protocols, options) {
476476

477477
var parsedUrl;
478478

479-
if (typeof address === 'object' && address.href !== undefined) {
479+
if (address instanceof URL) {
480480
parsedUrl = address;
481481
websocket.url = address.href;
482482
} else {
@@ -495,9 +495,6 @@ function initAsClient(websocket, address, protocols, options) {
495495
const defaultPort = isSecure ? 443 : 80;
496496
const key = randomBytes(16).toString('base64');
497497
const get = isSecure ? https.get : http.get;
498-
const path = parsedUrl.search
499-
? `${parsedUrl.pathname || '/'}${parsedUrl.search}`
500-
: parsedUrl.pathname || '/';
501498
var perMessageDeflate;
502499

503500
opts.createConnection = isSecure ? tlsConnect : netConnect;
@@ -515,7 +512,7 @@ function initAsClient(websocket, address, protocols, options) {
515512
},
516513
opts.headers
517514
);
518-
opts.path = path;
515+
opts.path = parsedUrl.pathname + parsedUrl.search;
519516
opts.timeout = opts.handshakeTimeout;
520517

521518
if (opts.perMessageDeflate) {
@@ -538,14 +535,12 @@ function initAsClient(websocket, address, protocols, options) {
538535
opts.headers.Origin = opts.origin;
539536
}
540537
}
541-
if (parsedUrl.auth) {
542-
opts.auth = parsedUrl.auth;
543-
} else if (parsedUrl.username || parsedUrl.password) {
538+
if (parsedUrl.username || parsedUrl.password) {
544539
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
545540
}
546541

547542
if (isUnixSocket) {
548-
const parts = path.split(':');
543+
const parts = opts.path.split(':');
549544

550545
opts.socketPath = parts[0];
551546
opts.path = parts[1];

‎test/websocket.test.js

+3-31
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const assert = require('assert');
66
const crypto = require('crypto');
77
const https = require('https');
88
const http = require('http');
9-
const url = require('url');
109
const fs = require('fs');
10+
const { URL } = require('url');
1111

1212
const WebSocket = require('..');
1313
const { GUID, NOOP } = require('../lib/constants');
@@ -25,17 +25,6 @@ describe('WebSocket', () => {
2525
);
2626
});
2727

28-
it('accepts `url.Url` objects as url', (done) => {
29-
const agent = new CustomAgent();
30-
31-
agent.addRequest = (req) => {
32-
assert.strictEqual(req.path, '/');
33-
done();
34-
};
35-
36-
const ws = new WebSocket(url.parse('ws://localhost'), { agent });
37-
});
38-
3928
it('accepts `url.URL` objects as url', function(done) {
4029
const agent = new CustomAgent();
4130

@@ -45,7 +34,7 @@ describe('WebSocket', () => {
4534
done();
4635
};
4736

48-
const ws = new WebSocket(new url.URL('ws://[::1]'), { agent });
37+
const ws = new WebSocket(new URL('ws://[::1]'), { agent });
4938
});
5039

5140
describe('options', () => {
@@ -2054,7 +2043,7 @@ describe('WebSocket', () => {
20542043
});
20552044

20562045
describe('Request headers', () => {
2057-
it('adds the authorization header if the url has userinfo (1/2)', (done) => {
2046+
it('adds the authorization header if the url has userinfo', (done) => {
20582047
const agent = new CustomAgent();
20592048
const auth = 'test:testpass';
20602049

@@ -2069,23 +2058,6 @@ describe('WebSocket', () => {
20692058
const ws = new WebSocket(`ws://${auth}@localhost`, { agent });
20702059
});
20712060

2072-
it('adds the authorization header if the url has userinfo (2/2)', (done) => {
2073-
const agent = new CustomAgent();
2074-
const auth = 'test:testpass';
2075-
2076-
agent.addRequest = (req) => {
2077-
assert.strictEqual(
2078-
req._headers.authorization,
2079-
`Basic ${Buffer.from(auth).toString('base64')}`
2080-
);
2081-
done();
2082-
};
2083-
2084-
const ws = new WebSocket(url.parse(`ws://${auth}@localhost`), {
2085-
agent
2086-
});
2087-
});
2088-
20892061
it('adds custom headers', (done) => {
20902062
const agent = new CustomAgent();
20912063

0 commit comments

Comments
 (0)
Please sign in to comment.