Skip to content

Commit

Permalink
[minor] Do not use the legacy URL API
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Oct 29, 2018
1 parent d2317b1 commit 0da3fdb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/websocket-server.js
Expand Up @@ -3,7 +3,6 @@
const EventEmitter = require('events');
const crypto = require('crypto');
const http = require('http');
const url = require('url');

const PerMessageDeflate = require('./permessage-deflate');
const extension = require('./extension');
Expand Down Expand Up @@ -147,8 +146,11 @@ class WebSocketServer extends EventEmitter {
* @public
*/
shouldHandle (req) {
if (this.options.path && url.parse(req.url).pathname !== this.options.path) {
return false;
if (this.options.path) {
const index = req.url.indexOf('?');
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;

if (pathname !== this.options.path) return false;
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions lib/websocket.js
Expand Up @@ -6,7 +6,7 @@ const https = require('https');
const http = require('http');
const net = require('net');
const tls = require('tls');
const url = require('url');
const { URL } = require('url');

const PerMessageDeflate = require('./permessage-deflate');
const EventTarget = require('./event-target');
Expand Down Expand Up @@ -447,7 +447,7 @@ function initAsClient (address, protocols, options) {
parsedUrl = address;
this.url = address.href;
} else {
parsedUrl = url.parse(address);
parsedUrl = new URL(address);
this.url = address;
}

Expand Down
5 changes: 3 additions & 2 deletions test/websocket.test.js
Expand Up @@ -20,8 +20,8 @@ describe('WebSocket', function () {
describe('#ctor', function () {
it('throws an error when using an invalid url', function () {
assert.throws(
() => new WebSocket('echo.websocket.org'),
/^Error: Invalid URL: echo\.websocket\.org$/
() => new WebSocket('ws+unix:'),
/^Error: Invalid URL: ws\+unix:$/
);
});

Expand All @@ -33,6 +33,7 @@ describe('WebSocket', function () {
done();
};

// eslint-disable-next-line node/no-deprecated-api
const ws = new WebSocket(url.parse('ws://localhost'), { agent });
});

Expand Down

0 comments on commit 0da3fdb

Please sign in to comment.