From 692d7b47624c5392d78fe4ea1351b5daa17bad94 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Thu, 25 Apr 2019 10:21:40 +0200 Subject: [PATCH] [major] Drop support for `url.Url` in the `WebSocket` constructor --- doc/ws.md | 2 +- lib/websocket.js | 17 ++++++----------- test/websocket.test.js | 34 +++------------------------------- 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/doc/ws.md b/doc/ws.md index d569d02b9..fe7f482d7 100644 --- a/doc/ws.md +++ b/doc/ws.md @@ -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 diff --git a/lib/websocket.js b/lib/websocket.js index b7b704c84..cc5dc598b 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -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 */ @@ -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 @@ -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 { @@ -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; @@ -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) { @@ -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]; diff --git a/test/websocket.test.js b/test/websocket.test.js index f1e1b1746..b51efc9ba 100644 --- a/test/websocket.test.js +++ b/test/websocket.test.js @@ -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'); @@ -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(); @@ -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', () => { @@ -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'; @@ -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();