-
Notifications
You must be signed in to change notification settings - Fork 962
Add WebSocket handler for Browser and Node #9191
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
Open
dlarocque
wants to merge
10
commits into
dl/live
Choose a base branch
from
dl/live-ws
base: dl/live
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9cc2187
Add WebSocket handler for Browser and Node
dlarocque 3ab8bc0
Add additional environment checks
dlarocque 5424470
Clarify usage of `window`
dlarocque 647eb21
remove util websocket handler
dlarocque 7154af6
Use platform/ convention
dlarocque d5b2ff3
use built-in global ws
dlarocque 5bf69cb
revert tsconfig module bump
dlarocque 4c81e4f
cleanup
dlarocque 685fc19
fix stub ws file
dlarocque 7098537
parse strings, and create error queue
dlarocque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect, use } from 'chai'; | ||
import sinon, { SinonFakeTimers, SinonStub } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import { isBrowser } from '@firebase/util'; | ||
import { BrowserWebSocketHandler } from './websocket'; | ||
import { AIError } from '../../errors'; | ||
|
||
use(sinonChai); | ||
use(chaiAsPromised); | ||
|
||
class MockBrowserWebSocket { | ||
static CONNECTING = 0; | ||
static OPEN = 1; | ||
static CLOSING = 2; | ||
static CLOSED = 3; | ||
|
||
readyState: number = MockBrowserWebSocket.CONNECTING; | ||
sentMessages: Array<string | ArrayBuffer> = []; | ||
url: string; | ||
private listeners: Map<string, Set<EventListener>> = new Map(); | ||
|
||
constructor(url: string) { | ||
this.url = url; | ||
} | ||
|
||
send(data: string | ArrayBuffer): void { | ||
if (this.readyState !== MockBrowserWebSocket.OPEN) { | ||
throw new Error('WebSocket is not in OPEN state'); | ||
} | ||
this.sentMessages.push(data); | ||
} | ||
|
||
close(): void { | ||
if ( | ||
this.readyState === MockBrowserWebSocket.CLOSED || | ||
this.readyState === MockBrowserWebSocket.CLOSING | ||
) { | ||
return; | ||
} | ||
this.readyState = MockBrowserWebSocket.CLOSING; | ||
setTimeout(() => { | ||
this.readyState = MockBrowserWebSocket.CLOSED; | ||
this.dispatchEvent(new Event('close')); | ||
}, 10); | ||
} | ||
|
||
addEventListener(type: string, listener: EventListener): void { | ||
if (!this.listeners.has(type)) { | ||
this.listeners.set(type, new Set()); | ||
} | ||
this.listeners.get(type)!.add(listener); | ||
} | ||
|
||
removeEventListener(type: string, listener: EventListener): void { | ||
this.listeners.get(type)?.delete(listener); | ||
} | ||
|
||
dispatchEvent(event: Event): void { | ||
this.listeners.get(event.type)?.forEach(listener => listener(event)); | ||
} | ||
|
||
triggerOpen(): void { | ||
this.readyState = MockBrowserWebSocket.OPEN; | ||
this.dispatchEvent(new Event('open')); | ||
} | ||
|
||
triggerMessage(data: any): void { | ||
this.dispatchEvent(new MessageEvent('message', { data })); | ||
} | ||
|
||
triggerError(): void { | ||
this.dispatchEvent(new Event('error')); | ||
} | ||
} | ||
|
||
describe('BrowserWebSocketHandler', () => { | ||
let handler: BrowserWebSocketHandler; | ||
let mockWebSocket: MockBrowserWebSocket; | ||
let clock: SinonFakeTimers; | ||
let webSocketStub: SinonStub; | ||
|
||
// Only run these tests in a browser environment | ||
if (!isBrowser()) { | ||
return; | ||
} | ||
|
||
beforeEach(() => { | ||
webSocketStub = sinon.stub(window, 'WebSocket').callsFake((url: string) => { | ||
mockWebSocket = new MockBrowserWebSocket(url); | ||
return mockWebSocket as any; | ||
}); | ||
clock = sinon.useFakeTimers(); | ||
handler = new BrowserWebSocketHandler(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
clock.restore(); | ||
}); | ||
|
||
describe('connect()', () => { | ||
it('should resolve on open event', async () => { | ||
const connectPromise = handler.connect('ws://test-url'); | ||
expect(webSocketStub).to.have.been.calledWith('ws://test-url'); | ||
|
||
await clock.tickAsync(1); | ||
mockWebSocket.triggerOpen(); | ||
|
||
await expect(connectPromise).to.be.fulfilled; | ||
}); | ||
|
||
it('should reject on error event', async () => { | ||
const connectPromise = handler.connect('ws://test-url'); | ||
await clock.tickAsync(1); | ||
mockWebSocket.triggerError(); | ||
|
||
await expect(connectPromise).to.be.rejectedWith( | ||
AIError, | ||
/Failed to establish WebSocket connection/ | ||
); | ||
}); | ||
}); | ||
|
||
describe('listen()', () => { | ||
beforeEach(async () => { | ||
const connectPromise = handler.connect('ws://test'); | ||
mockWebSocket.triggerOpen(); | ||
await connectPromise; | ||
}); | ||
|
||
it('should yield multiple messages as they arrive', async () => { | ||
const generator = handler.listen(); | ||
|
||
const received: unknown[] = []; | ||
const listenPromise = (async () => { | ||
for await (const msg of generator) { | ||
received.push(msg); | ||
} | ||
})(); | ||
|
||
// Use tickAsync to allow the consumer to start listening | ||
await clock.tickAsync(1); | ||
mockWebSocket.triggerMessage(new Blob([JSON.stringify({ foo: 1 })])); | ||
|
||
await clock.tickAsync(10); | ||
mockWebSocket.triggerMessage(new Blob([JSON.stringify({ foo: 2 })])); | ||
|
||
await clock.tickAsync(5); | ||
mockWebSocket.close(); | ||
await clock.runAllAsync(); // Let timers finish | ||
|
||
await listenPromise; // Wait for the consumer to finish | ||
|
||
expect(received).to.deep.equal([ | ||
{ | ||
foo: 1 | ||
}, | ||
{ | ||
foo: 2 | ||
} | ||
]); | ||
}); | ||
|
||
it('should buffer messages that arrive before the consumer calls .next()', async () => { | ||
const generator = handler.listen(); | ||
|
||
// Create a promise that will consume the generator in a separate async context | ||
const received: unknown[] = []; | ||
const consumptionPromise = (async () => { | ||
for await (const message of generator) { | ||
received.push(message); | ||
} | ||
})(); | ||
|
||
await clock.tickAsync(1); | ||
|
||
mockWebSocket.triggerMessage(new Blob([JSON.stringify({ foo: 1 })])); | ||
mockWebSocket.triggerMessage(new Blob([JSON.stringify({ foo: 2 })])); | ||
|
||
await clock.tickAsync(1); | ||
mockWebSocket.close(); | ||
await clock.runAllAsync(); | ||
|
||
await consumptionPromise; | ||
|
||
expect(received).to.deep.equal([ | ||
{ | ||
foo: 1 | ||
}, | ||
{ | ||
foo: 2 | ||
} | ||
]); | ||
}); | ||
}); | ||
|
||
describe('close()', () => { | ||
it('should be idempotent and not throw if called multiple times', async () => { | ||
const connectPromise = handler.connect('ws://test'); | ||
mockWebSocket.triggerOpen(); | ||
await connectPromise; | ||
|
||
const closePromise1 = handler.close(); | ||
await clock.runAllAsync(); | ||
await closePromise1; | ||
|
||
await expect(handler.close()).to.be.fulfilled; | ||
}); | ||
|
||
it('should wait for the onclose event before resolving', async () => { | ||
const connectPromise = handler.connect('ws://test'); | ||
mockWebSocket.triggerOpen(); | ||
await connectPromise; | ||
|
||
let closed = false; | ||
const closePromise = handler.close().then(() => { | ||
closed = true; | ||
}); | ||
|
||
// The promise should not have resolved yet | ||
await clock.tickAsync(5); | ||
expect(closed).to.be.false; | ||
|
||
// Now, let the mock's setTimeout for closing run, which triggers onclose | ||
await clock.tickAsync(10); | ||
|
||
await expect(closePromise).to.be.fulfilled; | ||
expect(closed).to.be.true; | ||
}); | ||
}); | ||
|
||
describe('Interaction between listen() and close()', () => { | ||
it('should allow close() to take precedence and resolve correctly, while also terminating the listener', async () => { | ||
const connectPromise = handler.connect('ws://test'); | ||
mockWebSocket.triggerOpen(); | ||
await connectPromise; | ||
|
||
const generator = handler.listen(); | ||
const listenPromise = (async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
for await (const _ of generator) { | ||
} | ||
})(); | ||
|
||
const closePromise = handler.close(); | ||
|
||
await clock.runAllAsync(); | ||
|
||
await expect(closePromise).to.be.fulfilled; | ||
await expect(listenPromise).to.be.fulfilled; | ||
|
||
expect(mockWebSocket.readyState).to.equal(MockBrowserWebSocket.CLOSED); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.