11import { decompress } from './decompress' ;
2- import { resolveWS } from './ws' ;
2+ import { openWebSocket , type WebSocketAdapter , type WebSocketArgs } from './ws' ;
33
4- export interface WebsocketAdapter {
5- readonly protocol : string ;
6- send ( msg : Uint8Array ) : void ;
7- close ( ) : void ;
8-
9- set onclose ( handler : ( ev : CloseEvent ) => void ) ;
10- set onopen ( handler : ( ) => void ) ;
11- set onmessage ( handler : ( msg : { data : Uint8Array } ) => void ) ;
12- set onerror ( handler : ( msg : ErrorEvent ) => void ) ;
13- }
14-
15- export class WebsocketDecompressAdapter implements WebsocketAdapter {
4+ export class WebsocketDecompressAdapter implements WebSocketAdapter {
165 get protocol ( ) : string {
176 return this . #ws. protocol ;
187 }
19-
208 set onclose ( handler : ( ev : CloseEvent ) => void ) {
219 this . #ws. onclose = handler ;
2210 }
@@ -35,16 +23,17 @@ export class WebsocketDecompressAdapter implements WebsocketAdapter {
3523
3624 #ws: WebSocket ;
3725
38- async #decompress( buffer : Uint8Array ) : Promise < Uint8Array > {
26+ async #decompress( buffer : Uint8Array < ArrayBuffer > ) : Promise < Uint8Array > {
3927 const tag = buffer [ 0 ] ;
4028 const data = buffer . subarray ( 1 ) ;
4129 switch ( tag ) {
4230 case 0 :
4331 return data ;
4432 case 1 :
45- throw new Error (
46- 'Brotli Compression not supported. Please use gzip or none compression in withCompression method on DbConnection.'
47- ) ;
33+ // Some runtimes support brotli, but it's not yet defined in `lib.dom.d.ts`.
34+ // We assert runtime support in `DbConnectionBuilder.withCompression`, so
35+ // this cast is safe.
36+ return await decompress ( data , 'brotli' as CompressionFormat ) ;
4837 case 2 :
4938 return await decompress ( data , 'gzip' ) ;
5039 default :
@@ -54,7 +43,7 @@ export class WebsocketDecompressAdapter implements WebsocketAdapter {
5443 }
5544 }
5645
57- send ( msg : Uint8Array ) : void {
46+ send ( msg : Uint8Array < ArrayBuffer > ) : void {
5847 this . #ws. send ( msg ) ;
5948 }
6049
@@ -63,68 +52,12 @@ export class WebsocketDecompressAdapter implements WebsocketAdapter {
6352 }
6453
6554 constructor ( ws : WebSocket ) {
66- ws . binaryType = 'arraybuffer' ;
67-
6855 this . #ws = ws ;
6956 }
7057
71- static async createWebSocketFn ( {
72- url,
73- nameOrAddress,
74- wsProtocol,
75- authToken,
76- compression,
77- lightMode,
78- confirmedReads,
79- } : {
80- url : URL ;
81- wsProtocol : string | string [ ] ;
82- nameOrAddress : string ;
83- authToken ?: string ;
84- compression : 'gzip' | 'none' ;
85- lightMode : boolean ;
86- confirmedReads ?: boolean ;
87- } ) : Promise < WebsocketDecompressAdapter > {
88- const headers = new Headers ( ) ;
89-
90- const WS = await resolveWS ( ) ;
91-
92- // We swap our original token to a shorter-lived token
93- // to avoid sending the original via query params.
94- let temporaryAuthToken : string | undefined = undefined ;
95- if ( authToken ) {
96- headers . set ( 'Authorization' , `Bearer ${ authToken } ` ) ;
97- const tokenUrl = new URL ( 'v1/identity/websocket-token' , url ) ;
98- tokenUrl . protocol = url . protocol === 'wss:' ? 'https:' : 'http:' ;
99-
100- const response = await fetch ( tokenUrl , { method : 'POST' , headers } ) ;
101- if ( response . ok ) {
102- const { token } = await response . json ( ) ;
103- temporaryAuthToken = token ;
104- } else {
105- return Promise . reject (
106- new Error ( `Failed to verify token: ${ response . statusText } ` )
107- ) ;
108- }
109- }
110-
111- const databaseUrl = new URL ( `v1/database/${ nameOrAddress } /subscribe` , url ) ;
112- if ( temporaryAuthToken ) {
113- databaseUrl . searchParams . set ( 'token' , temporaryAuthToken ) ;
114- }
115- databaseUrl . searchParams . set (
116- 'compression' ,
117- compression === 'gzip' ? 'Gzip' : 'None'
118- ) ;
119- if ( lightMode ) {
120- databaseUrl . searchParams . set ( 'light' , 'true' ) ;
121- }
122- if ( confirmedReads !== undefined ) {
123- databaseUrl . searchParams . set ( 'confirmed' , confirmedReads . toString ( ) ) ;
124- }
125-
126- const ws = new WS ( databaseUrl . toString ( ) , wsProtocol ) ;
127-
128- return new WebsocketDecompressAdapter ( ws ) ;
58+ static async openWebSocket (
59+ args : WebSocketArgs
60+ ) : Promise < WebsocketDecompressAdapter > {
61+ return new this ( await openWebSocket ( args ) ) ;
12962 }
13063}
0 commit comments