-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathprovider.ts
More file actions
495 lines (410 loc) · 14 KB
/
Copy pathprovider.ts
File metadata and controls
495 lines (410 loc) · 14 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { ClientToken } from '@y-sweet/sdk'
import * as decoding from 'lib0/decoding'
import * as encoding from 'lib0/encoding'
import * as awarenessProtocol from 'y-protocols/awareness'
import * as syncProtocol from 'y-protocols/sync'
import * as Y from 'yjs'
const MESSAGE_SYNC = 0
const MESSAGE_QUERY_AWARENESS = 3
const MESSAGE_AWARENESS = 1
const MESSAGE_SYNC_STATUS = 102
const EVENT_STATUS = 'status'
/** Fired when the _initial_ sync is complete. Only refired after that if there is a reconnection. */
const EVENT_SYNC = 'sync'
const EVENT_CONNECTION_CLOSE = 'connection-close'
const EVENT_CONNECTION_ERROR = 'connection-error'
/** Fired every time the sync status changes. */
const EVENT_SYNC_STATUS = 'sync-status'
/**
* Note: this should always be a superset of y-websocket's valid events.
* Ref: https://github.com/yjs/y-websocket/blob/aa4220407bda51ab6282d1291de6493c136c2089/README.md?plain=1#L119-L125
*/
type YSweetEvent =
| typeof EVENT_STATUS
| typeof EVENT_SYNC
| typeof EVENT_CONNECTION_CLOSE
| typeof EVENT_CONNECTION_ERROR
| typeof EVENT_SYNC_STATUS
const STATUS_CONNECTED = 'connected'
const STATUS_DISCONNECTED = 'disconnected'
const STATUS_CONNECTING = 'connecting'
const RETRIES_BEFORE_TOKEN_REFRESH = 3
const DELAY_MS_BEFORE_RECONNECT = 500
const DELAY_MS_BEFORE_RETRY_TOKEN_REFRESH = 3000
/**
* Note: this should always be a superset of y-websocket's valid statuses.
* Ref: https://github.com/yjs/y-websocket/blob/aa4220407bda51ab6282d1291de6493c136c2089/README.md?plain=1#L121
*/
type YSweetStatus = {
status: typeof STATUS_CONNECTED | typeof STATUS_DISCONNECTED | typeof STATUS_CONNECTING
}
type WebSocketPolyfillType = {
new (url: string | URL): WebSocket
prototype: WebSocket
readonly CLOSED: number
readonly CLOSING: number
readonly CONNECTING: number
readonly OPEN: number
}
export type AuthEndpoint = string | (() => Promise<ClientToken>)
export type YSweetProviderParams = {
/** Whether to connect to the websocket on creation (otherwise use `connect()`) */
connect?: boolean
/** Awareness protocol instance */
awareness?: awarenessProtocol.Awareness
/** WebSocket constructor to use (defaults to `WebSocket`) */
WebSocketPolyfill?: WebSocketPolyfillType
/** Maximum backoff time when retrying */
maxBackoffTime?: number
/** An initial client token to use (skips the first auth request if provided.) */
initialClientToken?: ClientToken
}
async function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function getClientToken(authEndpoint: AuthEndpoint, roomname: string): Promise<ClientToken> {
if (typeof authEndpoint === 'function') {
return await authEndpoint()
}
const body = JSON.stringify({ docId: roomname })
const res = await fetch(authEndpoint, {
method: 'POST',
body,
headers: { 'Content-Type': 'application/json' },
})
if (!res.ok) {
throw new Error(`Failed to get client token: ${res.status} ${res.statusText}`)
}
const clientToken = await res.json()
if (clientToken.docId !== roomname) {
throw new Error(
`Client token docId does not match roomname: ${clientToken.docId} !== ${roomname}`,
)
}
return clientToken
}
export class YSweetProvider {
private websocket: WebSocket | null = null
public clientToken: ClientToken | null = null
public synced: boolean = false
private status: YSweetStatus = { status: STATUS_DISCONNECTED }
public awareness: awarenessProtocol.Awareness
private WebSocketPolyfill: WebSocketPolyfillType
private listeners: Map<YSweetEvent, Set<EventListener>> = new Map()
private lastSyncSent: number = 0
private lastSyncAcked: number = 0
/** Whether we should attempt to connect if we are in a disconnected state. */
private shouldConnect: boolean
/** Whether we are currently in the process of connecting. */
private isConnecting: boolean = false
constructor(
private authEndpoint: AuthEndpoint,
private docId: string,
private doc: Y.Doc,
extraOptions: Partial<YSweetProviderParams> = {},
) {
this.shouldConnect = extraOptions.connect !== false
if (extraOptions.initialClientToken) {
this.clientToken = extraOptions.initialClientToken
}
this.awareness = extraOptions.awareness ?? new awarenessProtocol.Awareness(doc)
this.awareness.on('update', this.handleAwarenessUpdate.bind(this))
this.WebSocketPolyfill = extraOptions.WebSocketPolyfill || WebSocket
doc.on('update', this.update.bind(this))
if (this.shouldConnect) {
this.connect()
}
}
private updateSyncedState() {
if (this.lastSyncAcked === this.lastSyncSent) {
this.synced = true
this.emit(EVENT_SYNC_STATUS, true)
} else {
this.synced = false
this.emit(EVENT_SYNC_STATUS, false)
}
}
private update(update: Uint8Array, origin: YSweetProvider) {
if (!this.websocket) {
console.warn('Websocket not connected')
return
}
if (origin !== this) {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, MESSAGE_SYNC)
syncProtocol.writeUpdate(encoder, update)
this.websocket.send(encoding.toUint8Array(encoder))
this.checkSync()
}
}
private checkSync() {
if (!this.websocket) {
console.warn('Websocket not connected')
return
}
this.lastSyncSent += 1
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, MESSAGE_SYNC_STATUS)
const versionEncoder = encoding.createEncoder()
encoding.writeVarUint(versionEncoder, this.lastSyncSent)
encoding.writeVarUint8Array(encoder, encoding.toUint8Array(versionEncoder))
this.websocket.send(encoding.toUint8Array(encoder))
this.updateSyncedState()
}
private async ensureClientToken(): Promise<ClientToken> {
if (this.clientToken) {
return this.clientToken
}
if (typeof this.authEndpoint === 'string') {
this.clientToken = await getClientToken(this.authEndpoint, this.docId)
return this.clientToken
} else {
this.clientToken = await this.authEndpoint()
return this.clientToken
}
}
async connect() {
if (this.isConnecting) {
return
}
this.shouldConnect = true
this.isConnecting = true
while (this.shouldConnect) {
let clientToken
try {
clientToken = await this.ensureClientToken()
} catch (e) {
console.warn('Failed to get client token', e)
await sleep(DELAY_MS_BEFORE_RETRY_TOKEN_REFRESH)
continue
}
for (let i = 0; i < RETRIES_BEFORE_TOKEN_REFRESH && this.shouldConnect; i++) {
let resolve_: (value: boolean) => void
let promise = new Promise((resolve) => {
resolve_ = resolve
})
let statusListener = (event: YSweetStatus) => {
if (event.status === STATUS_CONNECTED) {
resolve_(true)
} else if (event.status === STATUS_DISCONNECTED) {
resolve_(false)
}
}
let errorListener = () => {
resolve_(false)
}
this.on(EVENT_STATUS, statusListener)
this.on(EVENT_CONNECTION_ERROR, errorListener)
this.setupWs(clientToken)
if ((await promise) === true) {
this.isConnecting = false
return
}
this.off(EVENT_STATUS, statusListener)
this.off(EVENT_CONNECTION_ERROR, errorListener)
await sleep(DELAY_MS_BEFORE_RECONNECT)
}
// Delete the current client token to force a token refresh on the next attempt.
this.clientToken = null
}
this.isConnecting = false
}
disconnect() {
if (this.websocket) {
this.websocket.close()
}
this.shouldConnect = false
}
private bindWebsocket(websocket: WebSocket) {
if (this.websocket) {
this.websocket.close()
this.websocket.onopen = null
this.websocket.onmessage = null
this.websocket.onclose = null
this.websocket.onerror = null
}
this.websocket = websocket
this.websocket.binaryType = 'arraybuffer'
this.websocket.onopen = this.websocketOpen.bind(this)
this.websocket.onmessage = this.receiveMessage.bind(this)
this.websocket.onclose = this.websocketClose.bind(this)
this.websocket.onerror = this.websocketError.bind(this)
}
private setupWs(clientToken: ClientToken) {
let url = clientToken.url + `/${clientToken.docId}`
if (clientToken.token) {
url = url + `?token=${clientToken.token}`
}
this.setStatus({ status: STATUS_CONNECTING })
const websocket = new (this.WebSocketPolyfill || WebSocket)(url)
this.bindWebsocket(websocket)
}
private syncStep1() {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, MESSAGE_SYNC)
syncProtocol.writeSyncStep1(encoder, this.doc)
this.websocket?.send(encoding.toUint8Array(encoder))
}
private receiveSyncMessage(decoder: decoding.Decoder) {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, MESSAGE_SYNC)
const syncMessageType = syncProtocol.readSyncMessage(decoder, encoder, this.doc, this)
if (syncMessageType === syncProtocol.messageYjsSyncStep2) {
this.setSynced(true)
}
if (encoding.length(encoder) > 1) {
this.websocket?.send(encoding.toUint8Array(encoder))
}
}
private queryAwareness() {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, MESSAGE_QUERY_AWARENESS)
encoding.writeVarUint8Array(
encoder,
awarenessProtocol.encodeAwarenessUpdate(
this.awareness,
Array.from(this.awareness.getStates().keys()),
),
)
this.websocket?.send(encoding.toUint8Array(encoder))
}
private broadcastAwareness() {
if (this.awareness.getLocalState() !== null) {
const encoderAwarenessState = encoding.createEncoder()
encoding.writeVarUint(encoderAwarenessState, MESSAGE_AWARENESS)
encoding.writeVarUint8Array(
encoderAwarenessState,
awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID]),
)
this.websocket?.send(encoding.toUint8Array(encoderAwarenessState))
}
}
private updateAwareness(decoder: decoding.Decoder) {
awarenessProtocol.applyAwarenessUpdate(
this.awareness,
decoding.readVarUint8Array(decoder),
this,
)
}
private websocketOpen() {
this.setStatus({ status: STATUS_CONNECTED })
this.syncStep1()
this.broadcastAwareness()
}
private receiveMessage(event: MessageEvent) {
let message: Uint8Array = new Uint8Array(event.data)
const decoder = decoding.createDecoder(message)
const messageType = decoding.readVarUint(decoder)
switch (messageType) {
case MESSAGE_SYNC:
this.receiveSyncMessage(decoder)
break
case MESSAGE_AWARENESS:
this.updateAwareness(decoder)
break
case MESSAGE_QUERY_AWARENESS:
this.queryAwareness()
break
case MESSAGE_SYNC_STATUS:
this.lastSyncAcked = decoding.readVarUint(decoder)
this.updateSyncedState()
break
default:
break
}
}
private websocketClose(event: CloseEvent) {
this.emit(EVENT_CONNECTION_CLOSE, event)
this.setSynced(false)
this.setStatus({ status: STATUS_DISCONNECTED })
if (this.shouldConnect && !this.isConnecting) {
this.connect()
}
// Remove all awareness states except for our own.
awarenessProtocol.removeAwarenessStates(
this.awareness,
Array.from(this.awareness.getStates().keys()).filter(
(client) => client !== this.doc.clientID,
),
this,
)
}
private websocketError(event: Event) {
this.emit('connection-error', event)
if (this.shouldConnect && !this.isConnecting) {
this.connect()
}
}
protected emit(eventName: YSweetEvent, data: any = null): void {
const listeners = this.listeners.get(eventName)
if (listeners) {
for (const listener of listeners) {
listener(data)
}
}
}
private setSynced(state: boolean) {
if (this.synced !== state) {
this.synced = state
this.emit(EVENT_SYNC, state)
}
}
private setStatus(status: YSweetStatus) {
if (this.status.status !== status.status) {
this.status = status
this.emit('status', status)
}
}
private handleAwarenessUpdate(
{ added, updated, removed }: { added: Array<any>; updated: Array<any>; removed: Array<any> },
_origin: any,
) {
const changedClients = added.concat(updated).concat(removed)
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, MESSAGE_AWARENESS)
encoding.writeVarUint8Array(
encoder,
awarenessProtocol.encodeAwarenessUpdate(this.awareness, changedClients),
)
this.websocket?.send(encoding.toUint8Array(encoder))
}
public destroy() {
if (this.websocket) {
this.websocket.close()
}
awarenessProtocol.removeAwarenessStates(this.awareness, [this.doc.clientID], 'window unload')
}
/** Aliases for compatibility with y-websocket provider. */
private _on(type: YSweetEvent, listener: (d: any) => void, once?: boolean): void {
if ((type as any) === 'synced') {
// Yjs emits both 'sync' and 'synced' events for legacy reasons. We only emit 'sync', but if
// we attempt to subscribe to 'synced', we rewrite it to 'sync' to maintain compatibility.
// Ref: https://github.com/yjs/y-websocket/blob/aa4220407bda51ab6282d1291de6493c136c2089/src/y-websocket.js#L404
type = EVENT_SYNC
}
if (!this.listeners.has(type)) {
this.listeners.set(type, new Set())
}
if (once) {
let listenerOnce = (d: any) => {
listener(d)
this.listeners.get(type)?.delete(listenerOnce)
}
this.listeners.get(type)?.add(listenerOnce)
} else {
this.listeners.get(type)?.add(listener)
}
}
on(type: YSweetEvent, listener: (d: any) => void): void {
this._on(type, listener)
}
once(type: YSweetEvent, listener: (d: any) => void): void {
this._on(type, listener, true)
}
off(type: YSweetEvent, listener: (d: any) => void): void {
const listeners = this.listeners.get(type)
if (listeners) {
listeners.delete(listener)
}
}
}