File tree Expand file tree Collapse file tree 2 files changed +17
-9
lines changed Expand file tree Collapse file tree 2 files changed +17
-9
lines changed Original file line number Diff line number Diff line change @@ -108,21 +108,19 @@ const getMissingParameters = (
108
108
* (ServerSentEvent)
109
109
*/
110
110
export const watchAgentMetadata = ( agentId : string ) : OneWayWebSocket => {
111
- const protocol = location . protocol === "https:" ? "wss:" : "ws:" ;
112
- return new OneWayWebSocket (
113
- `${ protocol } //${ location . host } /api/v2/workspaceagents/${ agentId } /watch-metadata-ws` ,
114
- ) ;
111
+ return new OneWayWebSocket ( {
112
+ apiRoute : `/api/v2/workspaceagents/${ agentId } /watch-metadata-ws` ,
113
+ } ) ;
115
114
} ;
116
115
117
116
/**
118
117
* @returns {EventSource } An EventSource that emits workspace event objects
119
118
* (ServerSentEvent)
120
119
*/
121
120
export const watchWorkspace = ( workspaceId : string ) : OneWayWebSocket => {
122
- const protocol = location . protocol === "https:" ? "wss:" : "ws:" ;
123
- return new OneWayWebSocket (
124
- `${ protocol } //${ location . host } /api/v2/workspaces/${ workspaceId } /watch-ws` ,
125
- ) ;
121
+ return new OneWayWebSocket ( {
122
+ apiRoute : `/api/v2/workspaces/${ workspaceId } /watch-ws` ,
123
+ } ) ;
126
124
} ;
127
125
128
126
export const getURLWithSearchParams = (
Original file line number Diff line number Diff line change @@ -44,13 +44,23 @@ interface OneWayWebSocketApi {
44
44
close : ( closeCode ?: number , reason ?: string ) => void ;
45
45
}
46
46
47
+ type OneWayWebSocketInit = Readonly < {
48
+ apiRoute : `/${string } `;
49
+ location ?: Location ;
50
+ protocols ?: string | string [ ] ;
51
+ } > ;
52
+
47
53
// Implementing wrapper around the base WebSocket class instead of doing fancy
48
54
// compile-time type-casts so that we have more runtime assurance that we won't
49
55
// accidentally send a message from the client to the server
50
56
export class OneWayWebSocket implements OneWayWebSocketApi {
51
57
#socket: WebSocket ;
52
58
53
- constructor ( url : string | URL , protocols ?: string | string [ ] ) {
59
+ constructor ( init : OneWayWebSocketInit ) {
60
+ const { apiRoute, protocols, location = window . location } = init ;
61
+
62
+ const protocol = location . protocol === "https:" ? "wss:" : "ws:" ;
63
+ const url = `${ protocol } //${ location . host } ${ apiRoute } ` ;
54
64
this . #socket = new WebSocket ( url , protocols ) ;
55
65
}
56
66
You can’t perform that action at this time.
0 commit comments