Skip to content

Commit

Permalink
[major] Drop support for url.Url in the WebSocket constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Apr 25, 2019
1 parent 1e6999b commit 692d7b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 43 deletions.
2 changes: 1 addition & 1 deletion doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ This class represents a WebSocket. It extends the `EventEmitter`.

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

- `address` {String|url.Url|url.URL} The URL to which to connect.
- `address` {String|url.URL} The URL to which to connect.
- `protocols` {String|Array} The list of subprotocols.
- `options` {Object}
- `followRedirects` {Boolean} Whether or not to follow redirects. Defaults to
Expand Down
17 changes: 6 additions & 11 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class WebSocket extends EventEmitter {
/**
* Create a new `WebSocket`.
*
* @param {(String|url.Url|url.URL)} address The URL to which to connect
* @param {(String|url.URL)} address The URL to which to connect
* @param {(String|String[])} protocols The subprotocols
* @param {Object} options Connection options
*/
Expand Down Expand Up @@ -427,7 +427,7 @@ module.exports = WebSocket;
* Initialize a WebSocket client.
*
* @param {WebSocket} websocket The client to initialize
* @param {(String|url.Url|url.URL)} address The URL to which to connect
* @param {(String|url.URL)} address The URL to which to connect
* @param {String} protocols The subprotocols
* @param {Object} options Connection options
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
Expand Down Expand Up @@ -476,7 +476,7 @@ function initAsClient(websocket, address, protocols, options) {

var parsedUrl;

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

opts.createConnection = isSecure ? tlsConnect : netConnect;
Expand All @@ -515,7 +512,7 @@ function initAsClient(websocket, address, protocols, options) {
},
opts.headers
);
opts.path = path;
opts.path = parsedUrl.pathname + parsedUrl.search;
opts.timeout = opts.handshakeTimeout;

if (opts.perMessageDeflate) {
Expand All @@ -538,14 +535,12 @@ function initAsClient(websocket, address, protocols, options) {
opts.headers.Origin = opts.origin;
}
}
if (parsedUrl.auth) {
opts.auth = parsedUrl.auth;
} else if (parsedUrl.username || parsedUrl.password) {
if (parsedUrl.username || parsedUrl.password) {
opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
}

if (isUnixSocket) {
const parts = path.split(':');
const parts = opts.path.split(':');

opts.socketPath = parts[0];
opts.path = parts[1];
Expand Down
34 changes: 3 additions & 31 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const assert = require('assert');
const crypto = require('crypto');
const https = require('https');
const http = require('http');
const url = require('url');
const fs = require('fs');
const { URL } = require('url');

const WebSocket = require('..');
const { GUID, NOOP } = require('../lib/constants');
Expand All @@ -25,17 +25,6 @@ describe('WebSocket', () => {
);
});

it('accepts `url.Url` objects as url', (done) => {
const agent = new CustomAgent();

agent.addRequest = (req) => {
assert.strictEqual(req.path, '/');
done();
};

const ws = new WebSocket(url.parse('ws://localhost'), { agent });
});

it('accepts `url.URL` objects as url', function(done) {
const agent = new CustomAgent();

Expand All @@ -45,7 +34,7 @@ describe('WebSocket', () => {
done();
};

const ws = new WebSocket(new url.URL('ws://[::1]'), { agent });
const ws = new WebSocket(new URL('ws://[::1]'), { agent });
});

describe('options', () => {
Expand Down Expand Up @@ -2054,7 +2043,7 @@ describe('WebSocket', () => {
});

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

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

it('adds the authorization header if the url has userinfo (2/2)', (done) => {
const agent = new CustomAgent();
const auth = 'test:testpass';

agent.addRequest = (req) => {
assert.strictEqual(
req._headers.authorization,
`Basic ${Buffer.from(auth).toString('base64')}`
);
done();
};

const ws = new WebSocket(url.parse(`ws://${auth}@localhost`), {
agent
});
});

it('adds custom headers', (done) => {
const agent = new CustomAgent();

Expand Down

0 comments on commit 692d7b4

Please sign in to comment.