1+ import type { ClientRequestArgs } from "node:http" ;
2+
13import TTLCache from "@isaacs/ttlcache" ;
24import type { ErrorEvent } from "isomorphic-ws" ;
35import WebSocket from "isomorphic-ws" ;
@@ -7,7 +9,16 @@ import { dummyLogger } from "ts-log";
79import type { Request , Response } from "../protocol.js" ;
810import type { ResilientWebSocketConfig } from "./resilient-websocket.js" ;
911import { ResilientWebSocket } from "./resilient-websocket.js" ;
10- import type { ClientRequestArgs } from "node:http" ;
12+
13+ /**
14+ * Browser global interface for proper typing
15+ */
16+ type BrowserGlobal = {
17+ window ?: {
18+ document ?: unknown ;
19+ } ;
20+ importScripts ?: ( ...urls : string [ ] ) => void ;
21+ }
1122
1223/**
1324 * Detects if the code is running in a regular DOM or Web Worker context.
@@ -17,8 +28,8 @@ function isBrowser(): boolean {
1728 try {
1829 // Check for browser's window object (DOM context)
1930 if ( typeof globalThis !== "undefined" ) {
20- const global = globalThis as any ;
21- if ( global . window && global . window . document ) {
31+ const global = globalThis as BrowserGlobal ;
32+ if ( global . window ? .document ) {
2233 return true ;
2334 }
2435
@@ -51,19 +62,16 @@ function addAuthentication(
5162 url : string ,
5263 token : string ,
5364 wsOptions : WebSocket . ClientOptions | ClientRequestArgs | undefined = { }
54- ) : { endpoint : string ; wsOptions : any } {
65+ ) : { endpoint : string ; wsOptions : WebSocket . ClientOptions | ClientRequestArgs | undefined } {
5566 if ( isBrowser ( ) ) {
5667 // Browser: Add token as query parameter
5768 const urlObj = new URL ( url ) ;
5869 urlObj . searchParams . set ( "ACCESS_TOKEN" , token ) ;
5970
60- // For browsers, we need to filter out any options that aren't valid for WebSocket constructor
61- // Browser WebSocket constructor only accepts protocols as second parameter
62- const browserWsOptions = wsOptions . protocol ? wsOptions . protocol : undefined ;
63-
71+ // For browsers, filter out wsOptions since headers aren't supported
6472 return {
6573 endpoint : urlObj . toString ( ) ,
66- wsOptions : browserWsOptions ,
74+ wsOptions : undefined ,
6775 } ;
6876 } else {
6977 // Node.js: Add Authorization header
@@ -72,7 +80,7 @@ function addAuthentication(
7280 wsOptions : {
7381 ...wsOptions ,
7482 headers : {
75- ...wsOptions . headers ,
83+ ...( wsOptions . headers as Record < string , string > | undefined ) ,
7684 Authorization : `Bearer ${ token } ` ,
7785 } ,
7886 } ,
0 commit comments