1
+ import type { ClientRequestArgs } from "node:http" ;
2
+
1
3
import TTLCache from "@isaacs/ttlcache" ;
2
4
import type { ErrorEvent } from "isomorphic-ws" ;
3
5
import WebSocket from "isomorphic-ws" ;
@@ -7,7 +9,16 @@ import { dummyLogger } from "ts-log";
7
9
import type { Request , Response } from "../protocol.js" ;
8
10
import type { ResilientWebSocketConfig } from "./resilient-websocket.js" ;
9
11
import { 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
+ }
11
22
12
23
/**
13
24
* Detects if the code is running in a regular DOM or Web Worker context.
@@ -17,8 +28,8 @@ function isBrowser(): boolean {
17
28
try {
18
29
// Check for browser's window object (DOM context)
19
30
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 ) {
22
33
return true ;
23
34
}
24
35
@@ -51,19 +62,16 @@ function addAuthentication(
51
62
url : string ,
52
63
token : string ,
53
64
wsOptions : WebSocket . ClientOptions | ClientRequestArgs | undefined = { }
54
- ) : { endpoint : string ; wsOptions : any } {
65
+ ) : { endpoint : string ; wsOptions : WebSocket . ClientOptions | ClientRequestArgs | undefined } {
55
66
if ( isBrowser ( ) ) {
56
67
// Browser: Add token as query parameter
57
68
const urlObj = new URL ( url ) ;
58
69
urlObj . searchParams . set ( "ACCESS_TOKEN" , token ) ;
59
70
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
64
72
return {
65
73
endpoint : urlObj . toString ( ) ,
66
- wsOptions : browserWsOptions ,
74
+ wsOptions : undefined ,
67
75
} ;
68
76
} else {
69
77
// Node.js: Add Authorization header
@@ -72,7 +80,7 @@ function addAuthentication(
72
80
wsOptions : {
73
81
...wsOptions ,
74
82
headers : {
75
- ...wsOptions . headers ,
83
+ ...( wsOptions . headers as Record < string , string > | undefined ) ,
76
84
Authorization : `Bearer ${ token } ` ,
77
85
} ,
78
86
} ,
0 commit comments