Skip to content

Commit 2474cfe

Browse files
Use as browser check
1 parent 37fefd7 commit 2474cfe

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

Diff for: lib/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
1414
this.apiKey = apiKey || null;
1515
this.debug = !!debug;
1616
this.ws = null;
17-
if (globalThis.WebSocket && this.apiKey) {
17+
if (globalThis.document && this.apiKey) {
1818
if (!dangerouslyAllowAPIKeyInBrowser) {
1919
throw new Error(
2020
`Can not provide API key in the browser without "dangerouslyAllowAPIKeyInBrowser" set to true`,
@@ -63,7 +63,7 @@ export class RealtimeAPI extends RealtimeEventHandler {
6363
if (this.isConnected()) {
6464
throw new Error(`Already connected`);
6565
}
66-
if (globalThis.WebSocket) {
66+
if (globalThis.document) {
6767
/**
6868
* Web browser
6969
*/

Diff for: test/tests/browser.js

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export async function run({ debug = false } = {}) {
1111
before(async () => {
1212
const WebSocket = (await import('websocket')).default.w3cwebsocket;
1313
globalThis.WebSocket = WebSocket;
14+
globalThis.document = {};
15+
});
16+
17+
after(async () => {
18+
globalThis.WebSocket = void 0;
19+
globalThis.document = void 0;
1420
});
1521

1622
it('Should fail to instantiate the RealtimeClient when "dangerouslyAllowAPIKeyInBrowser" is not set', () => {

Diff for: test/tests/client.js

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ export async function run({ debug = false } = {}) {
88
let client;
99
let realtimeEvents = [];
1010

11-
before(async () => {
12-
globalThis.WebSocket = void 0;
13-
});
14-
1511
it('Should instantiate the RealtimeClient', () => {
1612
client = new RealtimeClient({
1713
apiKey: process.env.OPENAI_API_KEY,

0 commit comments

Comments
 (0)