Skip to content

Commit 08d933b

Browse files
committed
feat(realtime): implement V2 serializer
1 parent 7511686 commit 08d933b

File tree

6 files changed

+631
-52
lines changed

6 files changed

+631
-52
lines changed

packages/core/realtime-js/src/RealtimeClient.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
DEFAULT_TIMEOUT,
88
SOCKET_STATES,
99
TRANSPORTS,
10-
VSN,
10+
DEFAULT_VSN,
11+
VSN_1_0_0,
12+
VSN_2_0_0,
1113
WS_CLOSE_NORMAL,
1214
} from './lib/constants'
1315

@@ -70,6 +72,7 @@ export type RealtimeClientOptions = {
7072
timeout?: number
7173
heartbeatIntervalMs?: number
7274
heartbeatCallback?: (status: HeartbeatStatus) => void
75+
vsn?: string
7376
logger?: Function
7477
encode?: Function
7578
decode?: Function
@@ -109,6 +112,7 @@ export default class RealtimeClient {
109112
heartbeatCallback: (status: HeartbeatStatus) => void = noop
110113
ref: number = 0
111114
reconnectTimer: Timer | null = null
115+
vsn: string = DEFAULT_VSN
112116
logger: Function = noop
113117
logLevel?: LogLevel
114118
encode!: Function
@@ -226,7 +230,7 @@ export default class RealtimeClient {
226230
* @returns string The URL of the websocket.
227231
*/
228232
endpointURL(): string {
229-
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: VSN }))
233+
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: this.vsn }))
230234
}
231235

232236
/**
@@ -825,6 +829,8 @@ export default class RealtimeClient {
825829
this.worker = options?.worker ?? false
826830
this.accessToken = options?.accessToken ?? null
827831
this.heartbeatCallback = options?.heartbeatCallback ?? noop
832+
this.vsn = options?.vsn ?? DEFAULT_VSN
833+
828834
// Handle special cases
829835
if (options?.params) this.params = options.params
830836
if (options?.logger) this.logger = options.logger
@@ -840,13 +846,27 @@ export default class RealtimeClient {
840846
return RECONNECT_INTERVALS[tries - 1] || DEFAULT_RECONNECT_FALLBACK
841847
})
842848

843-
this.encode =
844-
options?.encode ??
845-
((payload: JSON, callback: Function) => {
846-
return callback(JSON.stringify(payload))
847-
})
849+
switch (this.vsn) {
850+
case VSN_1_0_0:
851+
this.encode =
852+
options?.encode ??
853+
((payload: JSON, callback: Function) => {
854+
return callback(JSON.stringify(payload))
855+
})
848856

849-
this.decode = options?.decode ?? this.serializer.decode.bind(this.serializer)
857+
this.decode =
858+
options?.decode ??
859+
((payload: string, callback: Function) => {
860+
return callback(JSON.parse(payload))
861+
})
862+
break
863+
case VSN_2_0_0:
864+
this.encode = options?.encode ?? this.serializer.encode.bind(this.serializer)
865+
this.decode = options?.decode ?? this.serializer.decode.bind(this.serializer)
866+
break
867+
default:
868+
throw new Error(`Unsupported serializer version: ${this.vsn}`)
869+
}
850870

851871
// Handle worker setup
852872
if (this.worker) {

packages/core/realtime-js/src/lib/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { version } from './version'
22

33
export const DEFAULT_VERSION = `realtime-js/${version}`
4-
export const VSN: string = '1.0.0'
4+
5+
export const VSN_1_0_0: string = '1.0.0'
6+
export const VSN_2_0_0: string = '2.0.0'
7+
export const DEFAULT_VSN: string = VSN_1_0_0
58

69
export const VERSION = version
710

0 commit comments

Comments
 (0)