-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathserializer.ts
More file actions
228 lines (199 loc) · 7.59 KB
/
serializer.ts
File metadata and controls
228 lines (199 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This file draws heavily from https://github.com/phoenixframework/phoenix/commit/cf098e9cf7a44ee6479d31d911a97d3c7430c6fe
// License: https://github.com/phoenixframework/phoenix/blob/master/LICENSE.md
export type Msg<T> = {
join_ref?: string | null
ref?: string | null
topic: string
event: string
payload: T
}
export default class Serializer {
HEADER_LENGTH = 1
USER_BROADCAST_PUSH_META_LENGTH = 6
KINDS = { userBroadcastPush: 3, userBroadcast: 4 }
BINARY_ENCODING = 0
JSON_ENCODING = 1
BROADCAST_EVENT = 'broadcast'
allowedMetadataKeys: string[] = []
constructor(allowedMetadataKeys?: string[] | null) {
this.allowedMetadataKeys = allowedMetadataKeys ?? []
}
encode(msg: Msg<{ [key: string]: any }>, callback: (result: ArrayBuffer | string) => any) {
if (
msg.event === this.BROADCAST_EVENT &&
!(msg.payload instanceof ArrayBuffer) &&
typeof msg.payload.event === 'string'
) {
return callback(
this._binaryEncodeUserBroadcastPush(msg as Msg<{ event: string } & { [key: string]: any }>)
)
}
let payload = [msg.join_ref, msg.ref, msg.topic, msg.event, msg.payload]
return callback(JSON.stringify(payload))
}
private _binaryEncodeUserBroadcastPush(message: Msg<{ event: string } & { [key: string]: any }>) {
if (this._isArrayBuffer(message.payload?.payload)) {
return this._encodeBinaryUserBroadcastPush(message)
} else {
return this._encodeJsonUserBroadcastPush(message)
}
}
private _encodeBinaryUserBroadcastPush(message: Msg<{ event: string } & { [key: string]: any }>) {
const userPayload = message.payload?.payload ?? new ArrayBuffer(0)
return this._encodeUserBroadcastPush(message, this.BINARY_ENCODING, userPayload)
}
private _encodeJsonUserBroadcastPush(message: Msg<{ event: string } & { [key: string]: any }>) {
const userPayload = message.payload?.payload ?? {}
const encoder = new TextEncoder()
const encodedUserPayload = encoder.encode(JSON.stringify(userPayload)).buffer
return this._encodeUserBroadcastPush(message, this.JSON_ENCODING, encodedUserPayload)
}
private _encodeUserBroadcastPush(
message: Msg<{ event: string } & { [key: string]: any }>,
encodingType: number,
encodedPayload: ArrayBuffer
) {
const topic = message.topic
const ref = message.ref ?? ''
const joinRef = message.join_ref ?? ''
const userEvent = message.payload.event
// Filter metadata based on allowed keys
const rest = this.allowedMetadataKeys
? this._pick(message.payload, this.allowedMetadataKeys)
: {}
const metadata = Object.keys(rest).length === 0 ? '' : JSON.stringify(rest)
// Validate lengths don't exceed uint8 max value (255)
if (joinRef.length > 255) {
throw new Error(`joinRef length ${joinRef.length} exceeds maximum of 255`)
}
if (ref.length > 255) {
throw new Error(`ref length ${ref.length} exceeds maximum of 255`)
}
if (topic.length > 255) {
throw new Error(`topic length ${topic.length} exceeds maximum of 255`)
}
if (userEvent.length > 255) {
throw new Error(`userEvent length ${userEvent.length} exceeds maximum of 255`)
}
if (metadata.length > 255) {
throw new Error(`metadata length ${metadata.length} exceeds maximum of 255`)
}
const metaLength =
this.USER_BROADCAST_PUSH_META_LENGTH +
joinRef.length +
ref.length +
topic.length +
userEvent.length +
metadata.length
const header = new ArrayBuffer(this.HEADER_LENGTH + metaLength)
let view = new DataView(header)
let offset = 0
view.setUint8(offset++, this.KINDS.userBroadcastPush) // kind
view.setUint8(offset++, joinRef.length)
view.setUint8(offset++, ref.length)
view.setUint8(offset++, topic.length)
view.setUint8(offset++, userEvent.length)
view.setUint8(offset++, metadata.length)
view.setUint8(offset++, encodingType)
Array.from(joinRef, (char) => view.setUint8(offset++, char.charCodeAt(0)))
Array.from(ref, (char) => view.setUint8(offset++, char.charCodeAt(0)))
Array.from(topic, (char) => view.setUint8(offset++, char.charCodeAt(0)))
Array.from(userEvent, (char) => view.setUint8(offset++, char.charCodeAt(0)))
Array.from(metadata, (char) => view.setUint8(offset++, char.charCodeAt(0)))
var combined = new Uint8Array(header.byteLength + encodedPayload.byteLength)
combined.set(new Uint8Array(header), 0)
combined.set(new Uint8Array(encodedPayload), header.byteLength)
return combined.buffer
}
decode(rawPayload: ArrayBuffer | string, callback: Function) {
if (this._isArrayBuffer(rawPayload)) {
let result = this._binaryDecode(rawPayload as ArrayBuffer)
if (result === undefined) {
console.error('Failed to decode binary Realtime message')
return
}
return callback(result)
}
if (typeof rawPayload === 'string') {
try {
const jsonPayload = JSON.parse(rawPayload)
if (!Array.isArray(jsonPayload) || jsonPayload.length < 5) {
console.error('Malformed Realtime message:', jsonPayload)
return
}
const [join_ref, ref, topic, event, payload] = jsonPayload
return callback({ join_ref, ref, topic, event, payload })
} catch (error) {
console.error('Error parsing Realtime message:', error, rawPayload)
return
}
}
return
}
private _binaryDecode(buffer: ArrayBuffer) {
const view = new DataView(buffer)
const kind = view.getUint8(0)
const decoder = new TextDecoder()
switch (kind) {
case this.KINDS.userBroadcast:
return this._decodeUserBroadcast(buffer, view, decoder)
}
}
private _decodeUserBroadcast(
buffer: ArrayBuffer,
view: DataView,
decoder: TextDecoder
):
| {
join_ref: null
ref: null
topic: string
event: string
payload: { [key: string]: any }
}
| undefined {
const topicSize = view.getUint8(1)
const userEventSize = view.getUint8(2)
const metadataSize = view.getUint8(3)
const payloadEncoding = view.getUint8(4)
let offset = this.HEADER_LENGTH + 4
const topic = decoder.decode(buffer.slice(offset, offset + topicSize))
offset = offset + topicSize
const userEvent = decoder.decode(buffer.slice(offset, offset + userEventSize))
offset = offset + userEventSize
const metadata = decoder.decode(buffer.slice(offset, offset + metadataSize))
offset = offset + metadataSize
const payload = buffer.slice(offset, buffer.byteLength)
let parsedPayload
if (payloadEncoding === this.JSON_ENCODING) {
const textPayload = decoder.decode(payload)
try {
parsedPayload = JSON.parse(textPayload)
} catch (error) {
console.log('Error decoding JSON payload:', error, textPayload)
return undefined
}
} else {
parsedPayload = payload
}
const data: { [key: string]: any } = {
type: this.BROADCAST_EVENT,
event: userEvent,
payload: parsedPayload,
}
// Metadata is optional and always JSON encoded
if (metadataSize > 0) {
data['meta'] = JSON.parse(metadata)
}
return { join_ref: null, ref: null, topic: topic, event: this.BROADCAST_EVENT, payload: data }
}
private _isArrayBuffer(buffer: any): boolean {
return buffer instanceof ArrayBuffer || buffer?.constructor?.name === 'ArrayBuffer'
}
private _pick(obj: Record<string, any> | null | undefined, keys: string[]): Record<string, any> {
if (!obj || typeof obj !== 'object') {
return {}
}
return Object.fromEntries(Object.entries(obj).filter(([key]) => keys.includes(key)))
}
}