Skip to content

Commit

Permalink
[feature] Allow http(s) scheme in the WebSocket constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Aug 20, 2023
1 parent 8eb2c47 commit 570da50
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,17 +667,22 @@ function initAsClient(websocket, address, protocols, options) {

if (address instanceof URL) {
parsedUrl = address;
websocket._url = address.href;
} else {
try {
parsedUrl = new URL(address);
} catch (e) {
throw new SyntaxError(`Invalid URL: ${address}`);
}
}

websocket._url = address;
if (parsedUrl.protocol === 'http:') {
parsedUrl.protocol = 'ws:';
} else if (parsedUrl.protocol === 'https:') {
parsedUrl.protocol = 'wss:';
}

websocket._url = parsedUrl.href;

const isSecure = parsedUrl.protocol === 'wss:';
const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
let invalidUrlMessage;
Expand Down
44 changes: 39 additions & 5 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('WebSocket', () => {
);

assert.throws(
() => new WebSocket('https://websocket-echo.com'),
() => new WebSocket('http+ws://websocket-echo.com'),
/^SyntaxError: The URL's protocol must be one of "ws:", "wss:", or "ws\+unix:"$/
);

Expand Down Expand Up @@ -72,6 +72,30 @@ describe('WebSocket', () => {
const ws = new WebSocket(new URL('ws://[::1]'), { agent });
});

it('allows the http scheme', (done) => {
const agent = new CustomAgent();

agent.addRequest = (req, opts) => {
assert.strictEqual(opts.host, 'localhost');
assert.strictEqual(opts.port, 80);
done();
};

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

it('allows the https scheme', (done) => {
const agent = new https.Agent();

agent.addRequest = (req, opts) => {
assert.strictEqual(opts.host, 'localhost');
assert.strictEqual(opts.port, 443);
done();
};

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

describe('options', () => {
it('accepts the `options` object as 3rd argument', () => {
const agent = new http.Agent();
Expand Down Expand Up @@ -539,10 +563,18 @@ describe('WebSocket', () => {
});

it('exposes the server url', () => {
const url = 'ws://localhost';
const ws = new WebSocket(url, { agent: new CustomAgent() });
const schemes = new Map([
['ws', 'ws'],
['wss', 'wss'],
['http', 'ws'],
['https', 'wss']
]);

assert.strictEqual(ws.url, url);
for (const [key, value] of schemes) {
const ws = new WebSocket(`${key}://localhost/`, { lookup() {} });

assert.strictEqual(ws.url, `${value}://localhost/`);
}
});
});
});
Expand Down Expand Up @@ -1174,7 +1206,9 @@ describe('WebSocket', () => {

it('emits an error if the redirect URL is invalid (2/2)', (done) => {
server.once('upgrade', (req, socket) => {
socket.end('HTTP/1.1 302 Found\r\nLocation: http://localhost\r\n\r\n');
socket.end(
'HTTP/1.1 302 Found\r\nLocation: http+ws://localhost\r\n\r\n'
);
});

const ws = new WebSocket(`ws://localhost:${server.address().port}`, {
Expand Down

0 comments on commit 570da50

Please sign in to comment.