Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ws WebSocket when options are provided #2968

Open
wants to merge 4 commits into
base: 3.7-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Linting and variable naming
  • Loading branch information
KyleBoyer committed Jan 10, 2025
commit a28570c626b58f54bfc8a03efa32bfa73bcded54
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Connection extends EventEmitter {
}
}
// All these options are available to the `ws` package's constructor, but not the global WebSocket class available in DOM
const domUnsupportedOptions = new Set([
const wsSpecificOptions = new Set([
'headers',
'ca',
'cert',
Expand All @@ -134,11 +134,11 @@ class Connection extends EventEmitter {
'perMessageDeflate',
]);
// Check if any `ws` specific options are provided and are non-null / non-undefined
const hasDOMUnsupportedOptions = Object.entries(this.options).some(
([key, value]) => domUnsupportedOptions.has(key) && ![null, undefined].includes(value),
const hasWsSpecificOptions = Object.entries(this.options).some(
([key, value]) => wsSpecificOptions.has(key) && ![null, undefined].includes(value),
);
// Only use the global websocket if we don't have any unsupported options
const useGlobalWebSocket = !hasDOMUnsupportedOptions && globalThis.WebSocket;
const useGlobalWebSocket = !hasWsSpecificOptions && globalThis.WebSocket;
const WebSocket = useGlobalWebSocket || (await import('ws')).default;

this._ws = new WebSocket(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,42 @@ describe('Connection', function () {

describe('#open()', function () {
it('should use the ws WebSocket when ws specific options are provided', function () {
let globalWebsocketCalls = 0;
globalThis.WebSocket = function (){
globalWebsocketCalls++;
}
const domUnsupportedOptions = [
'headers',
'ca',
'cert',
'pfx',
'rejectUnauthorized',
'agent',
'perMessageDeflate',
];
const allOptionTests = domUnsupportedOptions.map(wsOption => {
const connection = helper.getDriverRemoteConnection(`ws://localhost:${testServerPort}/401`, {
[wsOption]: 'this option is set'
});
return connection.open()
let globalWebsocketCalls = 0;
globalThis.WebSocket = function () {
globalWebsocketCalls++;
};
const domUnsupportedOptions = [
'headers',
'ca',
'cert',
'pfx',
'rejectUnauthorized',
'agent',
'perMessageDeflate',
];
const allOptionTests = domUnsupportedOptions.map((wsOption) => {
const connection = helper.getDriverRemoteConnection(`ws://localhost:${testServerPort}/401`, {
[wsOption]: 'this option is set',
});
Promise.allSettled(allOptionTests).then(function() {
if(globalWebsocketCalls != domUnsupportedOptions.length){
assert.fail('global WebSocket should be used when no ws specific options are provided');
}
})
return connection.open();
});
Promise.allSettled(allOptionTests).then(function () {
if (globalWebsocketCalls !== domUnsupportedOptions.length) {
assert.fail('global WebSocket should be used when no ws specific options are provided');
}
});
});
it('should use the global WebSocket when options are not provided', function () {
let globalWebsocketCalls = 0;
globalThis.WebSocket = function (){
globalWebsocketCalls++;
}
const connection = helper.getDriverRemoteConnection(`ws://localhost:${testServerPort}/401`);
return connection
let globalWebsocketCalls = 0;
globalThis.WebSocket = function () {
globalWebsocketCalls++;
};
const connection = helper.getDriverRemoteConnection(`ws://localhost:${testServerPort}/401`);
return connection
.open()
.catch(() => {})
.finally(function () {
if(globalWebsocketCalls <= 0){
if (globalWebsocketCalls <= 0) {
assert.fail('global WebSocket should be used when no ws specific options are provided');
}
});
Expand Down
Loading