@abraca/dabra 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,533 @@
1
+ import { WsReadyStates } from "@hocuspocus/common";
2
+ import { retry } from "@lifeomic/attempt";
3
+ import * as time from "lib0/time";
4
+ import type { Event, MessageEvent } from "ws";
5
+ import EventEmitter from "./EventEmitter.ts";
6
+ import type { HocuspocusProvider } from "./HocuspocusProvider.ts";
7
+ import { IncomingMessage } from "./IncomingMessage.ts";
8
+ import { CloseMessage } from "./OutgoingMessages/CloseMessage.ts";
9
+ import {
10
+ WebSocketStatus,
11
+ type onAwarenessChangeParameters,
12
+ type onAwarenessUpdateParameters,
13
+ type onCloseParameters,
14
+ type onDisconnectParameters,
15
+ type onMessageParameters,
16
+ type onOpenParameters,
17
+ type onOutgoingMessageParameters,
18
+ type onStatusParameters,
19
+ } from "./types.ts";
20
+
21
+ export type HocuspocusWebSocket = WebSocket & { identifier: string };
22
+ export type HocusPocusWebSocket = HocuspocusWebSocket;
23
+
24
+ export type HocuspocusProviderWebsocketConfiguration = Required<
25
+ Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">
26
+ > &
27
+ Partial<CompleteHocuspocusProviderWebsocketConfiguration>;
28
+
29
+ export interface CompleteHocuspocusProviderWebsocketConfiguration {
30
+ /**
31
+ * Whether to connect automatically when creating the provider instance. Default=true
32
+ */
33
+ autoConnect: boolean;
34
+
35
+ /**
36
+ * URL of your @hocuspocus/server instance
37
+ */
38
+ url: string;
39
+
40
+ /**
41
+ * By default, trailing slashes are removed from the URL. Set this to true
42
+ * to preserve trailing slashes if your server configuration requires them.
43
+ */
44
+ preserveTrailingSlash: boolean;
45
+
46
+ /**
47
+ * An optional WebSocket polyfill, for example for Node.js
48
+ */
49
+ WebSocketPolyfill: any;
50
+
51
+ /**
52
+ * Disconnect when no message is received for the defined amount of milliseconds.
53
+ */
54
+ messageReconnectTimeout: number;
55
+ /**
56
+ * The delay between each attempt in milliseconds. You can provide a factor to have the delay grow exponentially.
57
+ */
58
+ delay: number;
59
+ /**
60
+ * The initialDelay is the amount of time to wait before making the first attempt. This option should typically be 0 since you typically want the first attempt to happen immediately.
61
+ */
62
+ initialDelay: number;
63
+ /**
64
+ * The factor option is used to grow the delay exponentially.
65
+ */
66
+ factor: number;
67
+ /**
68
+ * The maximum number of attempts or 0 if there is no limit on number of attempts.
69
+ */
70
+ maxAttempts: number;
71
+ /**
72
+ * minDelay is used to set a lower bound of delay when jitter is enabled. This property has no effect if jitter is disabled.
73
+ */
74
+ minDelay: number;
75
+ /**
76
+ * The maxDelay option is used to set an upper bound for the delay when factor is enabled. A value of 0 can be provided if there should be no upper bound when calculating delay.
77
+ */
78
+ maxDelay: number;
79
+ /**
80
+ * If jitter is true then the calculated delay will be a random integer value between minDelay and the calculated delay for the current iteration.
81
+ */
82
+ jitter: boolean;
83
+ /**
84
+ * A timeout in milliseconds. If timeout is non-zero then a timer is set using setTimeout. If the timeout is triggered then future attempts will be aborted.
85
+ */
86
+ timeout: number;
87
+ handleTimeout: (() => Promise<unknown>) | null;
88
+ onOpen: (data: onOpenParameters) => void;
89
+ onConnect: () => void;
90
+ onMessage: (data: onMessageParameters) => void;
91
+ onOutgoingMessage: (data: onOutgoingMessageParameters) => void;
92
+ onStatus: (data: onStatusParameters) => void;
93
+ onDisconnect: (data: onDisconnectParameters) => void;
94
+ onClose: (data: onCloseParameters) => void;
95
+ onDestroy: () => void;
96
+ onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
97
+ onAwarenessChange: (data: onAwarenessChangeParameters) => void;
98
+
99
+ /**
100
+ * Map of attached providers keyed by documentName.
101
+ */
102
+ providerMap: Map<string, HocuspocusProvider>;
103
+ }
104
+
105
+ export class HocuspocusProviderWebsocket extends EventEmitter {
106
+ private messageQueue: any[] = [];
107
+
108
+ public configuration: CompleteHocuspocusProviderWebsocketConfiguration = {
109
+ url: "",
110
+ autoConnect: true,
111
+ preserveTrailingSlash: false,
112
+ // @ts-ignore
113
+ document: undefined,
114
+ WebSocketPolyfill: undefined,
115
+ // TODO: this should depend on awareness.outdatedTime
116
+ messageReconnectTimeout: 30000,
117
+ // 1 second
118
+ delay: 1000,
119
+ // instant
120
+ initialDelay: 0,
121
+ // double the delay each time
122
+ factor: 2,
123
+ // unlimited retries
124
+ maxAttempts: 0,
125
+ // wait at least 1 second
126
+ minDelay: 1000,
127
+ // at least every 30 seconds
128
+ maxDelay: 30000,
129
+ // randomize
130
+ jitter: true,
131
+ // retry forever
132
+ timeout: 0,
133
+ onOpen: () => null,
134
+ onConnect: () => null,
135
+ onMessage: () => null,
136
+ onOutgoingMessage: () => null,
137
+ onStatus: () => null,
138
+ onDisconnect: () => null,
139
+ onClose: () => null,
140
+ onDestroy: () => null,
141
+ onAwarenessUpdate: () => null,
142
+ onAwarenessChange: () => null,
143
+ handleTimeout: null,
144
+ providerMap: new Map(),
145
+ };
146
+
147
+ webSocket: HocusPocusWebSocket | null = null;
148
+
149
+ webSocketHandlers: { [key: string]: any } = {};
150
+
151
+ shouldConnect = true;
152
+
153
+ status = WebSocketStatus.Disconnected;
154
+
155
+ lastMessageReceived = 0;
156
+
157
+ identifier = 0;
158
+
159
+ intervals: any = {
160
+ connectionChecker: null,
161
+ };
162
+
163
+ connectionAttempt: {
164
+ resolve: (value?: any) => void;
165
+ reject: (reason?: any) => void;
166
+ } | null = null;
167
+
168
+ constructor(configuration: HocuspocusProviderWebsocketConfiguration) {
169
+ super();
170
+ this.setConfiguration(configuration);
171
+
172
+ this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill
173
+ ? configuration.WebSocketPolyfill
174
+ : WebSocket;
175
+
176
+ this.on("open", this.configuration.onOpen);
177
+ this.on("open", this.onOpen.bind(this));
178
+ this.on("connect", this.configuration.onConnect);
179
+ this.on("message", this.configuration.onMessage);
180
+ this.on("outgoingMessage", this.configuration.onOutgoingMessage);
181
+ this.on("status", this.configuration.onStatus);
182
+ this.on("disconnect", this.configuration.onDisconnect);
183
+ this.on("close", this.configuration.onClose);
184
+ this.on("destroy", this.configuration.onDestroy);
185
+ this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
186
+ this.on("awarenessChange", this.configuration.onAwarenessChange);
187
+
188
+ this.on("close", this.onClose.bind(this));
189
+ this.on("message", this.onMessage.bind(this));
190
+
191
+ this.intervals.connectionChecker = setInterval(
192
+ this.checkConnection.bind(this),
193
+ this.configuration.messageReconnectTimeout / 10,
194
+ );
195
+
196
+ if (this.shouldConnect) {
197
+ this.connect();
198
+ }
199
+ }
200
+
201
+ receivedOnOpenPayload?: Event | undefined = undefined;
202
+
203
+ async onOpen(event: Event) {
204
+ this.status = WebSocketStatus.Connected;
205
+ this.emit("status", { status: WebSocketStatus.Connected });
206
+
207
+ this.cancelWebsocketRetry = undefined;
208
+ this.receivedOnOpenPayload = event;
209
+ }
210
+
211
+ attach(provider: HocuspocusProvider) {
212
+ this.configuration.providerMap.set(provider.configuration.name, provider);
213
+
214
+ if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
215
+ this.connect();
216
+ }
217
+
218
+ if (
219
+ this.receivedOnOpenPayload &&
220
+ this.status === WebSocketStatus.Connected
221
+ ) {
222
+ provider.onOpen(this.receivedOnOpenPayload);
223
+ }
224
+ }
225
+
226
+ detach(provider: HocuspocusProvider) {
227
+ if (this.configuration.providerMap.has(provider.configuration.name)) {
228
+ provider.send(CloseMessage, {
229
+ documentName: provider.configuration.name,
230
+ });
231
+ this.configuration.providerMap.delete(provider.configuration.name);
232
+ }
233
+ }
234
+
235
+ public setConfiguration(
236
+ configuration: Partial<HocuspocusProviderWebsocketConfiguration> = {},
237
+ ): void {
238
+ this.configuration = { ...this.configuration, ...configuration };
239
+
240
+ if (!this.configuration.autoConnect) {
241
+ this.shouldConnect = false;
242
+ }
243
+ }
244
+
245
+ cancelWebsocketRetry?: () => void;
246
+
247
+ async connect() {
248
+ if (this.status === WebSocketStatus.Connected) {
249
+ return;
250
+ }
251
+
252
+ // Always cancel any previously initiated connection retryer instances
253
+ if (this.cancelWebsocketRetry) {
254
+ this.cancelWebsocketRetry();
255
+ this.cancelWebsocketRetry = undefined;
256
+ }
257
+
258
+ this.receivedOnOpenPayload = undefined;
259
+ this.shouldConnect = true;
260
+
261
+ const abortableRetry = () => {
262
+ let cancelAttempt = false;
263
+
264
+ const retryPromise = retry(this.createWebSocketConnection.bind(this), {
265
+ delay: this.configuration.delay,
266
+ initialDelay: this.configuration.initialDelay,
267
+ factor: this.configuration.factor,
268
+ maxAttempts: this.configuration.maxAttempts,
269
+ minDelay: this.configuration.minDelay,
270
+ maxDelay: this.configuration.maxDelay,
271
+ jitter: this.configuration.jitter,
272
+ timeout: this.configuration.timeout,
273
+ handleTimeout: this.configuration.handleTimeout,
274
+ beforeAttempt: (context) => {
275
+ if (!this.shouldConnect || cancelAttempt) {
276
+ context.abort();
277
+ }
278
+ },
279
+ }).catch((error: any) => {
280
+ // If we aborted the connection attempt then don’t throw an error
281
+ // ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
282
+ if (error && error.code !== "ATTEMPT_ABORTED") {
283
+ throw error;
284
+ }
285
+ });
286
+
287
+ return {
288
+ retryPromise,
289
+ cancelFunc: () => {
290
+ cancelAttempt = true;
291
+ },
292
+ };
293
+ };
294
+
295
+ const { retryPromise, cancelFunc } = abortableRetry();
296
+ this.cancelWebsocketRetry = cancelFunc;
297
+
298
+ return retryPromise;
299
+ }
300
+
301
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
302
+ attachWebSocketListeners(ws: HocusPocusWebSocket, reject: Function) {
303
+ const { identifier } = ws;
304
+ const onMessageHandler = (payload: any) => this.emit("message", payload);
305
+ const onCloseHandler = (payload: any) =>
306
+ this.emit("close", { event: payload });
307
+ const onOpenHandler = (payload: any) => this.emit("open", payload);
308
+ const onErrorHandler = (err: any) => {
309
+ reject(err);
310
+ };
311
+
312
+ this.webSocketHandlers[identifier] = {
313
+ message: onMessageHandler,
314
+ close: onCloseHandler,
315
+ open: onOpenHandler,
316
+ error: onErrorHandler,
317
+ };
318
+
319
+ const handlers = this.webSocketHandlers[ws.identifier];
320
+
321
+ Object.keys(handlers).forEach((name) => {
322
+ ws.addEventListener(name, handlers[name]);
323
+ });
324
+ }
325
+
326
+ cleanupWebSocket() {
327
+ if (!this.webSocket) {
328
+ return;
329
+ }
330
+ const { identifier } = this.webSocket;
331
+ const handlers = this.webSocketHandlers[identifier];
332
+
333
+ Object.keys(handlers).forEach((name) => {
334
+ this.webSocket?.removeEventListener(name, handlers[name]);
335
+ delete this.webSocketHandlers[identifier];
336
+ });
337
+
338
+ try {
339
+ // Check if the WebSocket is still in CONNECTING state (0)
340
+ // If so, calling close() might throw in some environments or be race-prone
341
+ if (this.webSocket.readyState !== 0 && this.webSocket.readyState !== 3) {
342
+ this.webSocket.close();
343
+ }
344
+ } catch (e) {
345
+ // Ignore errors during close
346
+ }
347
+
348
+ this.webSocket = null;
349
+ }
350
+
351
+ createWebSocketConnection() {
352
+ return new Promise((resolve, reject) => {
353
+ if (this.webSocket) {
354
+ this.messageQueue = [];
355
+ this.cleanupWebSocket();
356
+ }
357
+ this.lastMessageReceived = 0;
358
+ this.identifier += 1;
359
+
360
+ // Init the WebSocket connection
361
+ const ws = new this.configuration.WebSocketPolyfill(this.url);
362
+ ws.binaryType = "arraybuffer";
363
+ ws.identifier = this.identifier;
364
+
365
+ this.attachWebSocketListeners(ws, reject);
366
+
367
+ this.webSocket = ws;
368
+
369
+ // Reset the status
370
+ this.status = WebSocketStatus.Connecting;
371
+ this.emit("status", { status: WebSocketStatus.Connecting });
372
+
373
+ // Store resolve/reject for later use
374
+ this.connectionAttempt = {
375
+ resolve,
376
+ reject,
377
+ };
378
+ });
379
+ }
380
+
381
+ onMessage(event: MessageEvent) {
382
+ this.resolveConnectionAttempt();
383
+
384
+ this.lastMessageReceived = time.getUnixTime();
385
+
386
+ const message = new IncomingMessage(event.data);
387
+ const documentName = message.peekVarString();
388
+
389
+ this.configuration.providerMap.get(documentName)?.onMessage(event);
390
+ }
391
+
392
+ resolveConnectionAttempt() {
393
+ if (this.connectionAttempt) {
394
+ this.connectionAttempt.resolve();
395
+ this.connectionAttempt = null;
396
+
397
+ this.status = WebSocketStatus.Connected;
398
+ this.emit("status", { status: WebSocketStatus.Connected });
399
+ this.emit("connect");
400
+ this.messageQueue.forEach((message) => this.send(message));
401
+ this.messageQueue = [];
402
+ }
403
+ }
404
+
405
+ stopConnectionAttempt() {
406
+ this.connectionAttempt = null;
407
+ }
408
+
409
+ rejectConnectionAttempt() {
410
+ this.connectionAttempt?.reject();
411
+ this.connectionAttempt = null;
412
+ }
413
+
414
+ closeTries = 0;
415
+
416
+ checkConnection() {
417
+ // Don’t check the connection when it’s not even established
418
+ if (this.status !== WebSocketStatus.Connected) {
419
+ return;
420
+ }
421
+
422
+ // Don’t close the connection while waiting for the first message
423
+ if (!this.lastMessageReceived) {
424
+ return;
425
+ }
426
+
427
+ // Don’t close the connection when a message was received recently
428
+ if (
429
+ this.configuration.messageReconnectTimeout >=
430
+ time.getUnixTime() - this.lastMessageReceived
431
+ ) {
432
+ return;
433
+ }
434
+
435
+ // No message received in a long time, not even your own
436
+ // Awareness updates, which are updated every 15 seconds
437
+ // if awareness is enabled.
438
+ this.closeTries += 1;
439
+ // https://bugs.webkit.org/show_bug.cgi?id=247943
440
+ if (this.closeTries > 2) {
441
+ this.onClose({
442
+ event: {
443
+ code: 4408,
444
+ reason: "forced",
445
+ },
446
+ });
447
+ this.closeTries = 0;
448
+ } else {
449
+ this.webSocket?.close();
450
+ this.messageQueue = [];
451
+ }
452
+ }
453
+
454
+ get serverUrl() {
455
+ if (this.configuration.preserveTrailingSlash) {
456
+ return this.configuration.url;
457
+ }
458
+
459
+ // By default, ensure that the URL never ends with /
460
+ let url = this.configuration.url;
461
+ while (url[url.length - 1] === "/") {
462
+ url = url.slice(0, url.length - 1);
463
+ }
464
+
465
+ return url;
466
+ }
467
+
468
+ get url() {
469
+ return this.serverUrl;
470
+ }
471
+
472
+ disconnect() {
473
+ this.shouldConnect = false;
474
+
475
+ if (this.webSocket === null) {
476
+ return;
477
+ }
478
+
479
+ try {
480
+ this.webSocket.close();
481
+ this.messageQueue = [];
482
+ } catch (e) {
483
+ console.error(e);
484
+ }
485
+ }
486
+
487
+ send(message: any) {
488
+ if (this.webSocket?.readyState === WsReadyStates.Open) {
489
+ this.webSocket.send(message);
490
+ } else {
491
+ this.messageQueue.push(message);
492
+ }
493
+ }
494
+
495
+ onClose({ event }: onCloseParameters) {
496
+ this.closeTries = 0;
497
+ this.cleanupWebSocket();
498
+
499
+ if (this.connectionAttempt) {
500
+ // That connection attempt failed.
501
+ this.rejectConnectionAttempt();
502
+ }
503
+
504
+ // Let’s update the connection status.
505
+ this.status = WebSocketStatus.Disconnected;
506
+ this.emit("status", { status: WebSocketStatus.Disconnected });
507
+ this.emit("disconnect", { event });
508
+
509
+ // trigger connect if no retry is running and we want to have a connection
510
+ if (!this.cancelWebsocketRetry && this.shouldConnect) {
511
+ setTimeout(() => {
512
+ this.connect();
513
+ }, this.configuration.delay);
514
+ }
515
+ }
516
+
517
+ destroy() {
518
+ this.emit("destroy");
519
+
520
+ clearInterval(this.intervals.connectionChecker);
521
+
522
+ // If there is still a connection attempt outstanding then we should stop
523
+ // it before calling disconnect, otherwise it will be rejected in the onClose
524
+ // handler and trigger a retry
525
+ this.stopConnectionAttempt();
526
+
527
+ this.disconnect();
528
+
529
+ this.removeAllListeners();
530
+
531
+ this.cleanupWebSocket();
532
+ }
533
+ }
@@ -0,0 +1,63 @@
1
+ import type { Decoder } from "lib0/decoding";
2
+ import {
3
+ createDecoder,
4
+ peekVarString,
5
+ readVarUint,
6
+ readVarUint8Array,
7
+ readVarString,
8
+ } from "lib0/decoding";
9
+ import type { Encoder } from "lib0/encoding";
10
+ import {
11
+ createEncoder,
12
+ writeVarUint,
13
+ writeVarUint8Array,
14
+ writeVarString,
15
+ length,
16
+ } from "lib0/encoding";
17
+ import type { MessageType } from "./types.ts";
18
+
19
+ export class IncomingMessage {
20
+ data: any;
21
+
22
+ encoder: Encoder;
23
+
24
+ decoder: Decoder;
25
+
26
+ constructor(data: any) {
27
+ this.data = data;
28
+ this.encoder = createEncoder();
29
+ this.decoder = createDecoder(new Uint8Array(this.data));
30
+ }
31
+
32
+ peekVarString(): string {
33
+ return peekVarString(this.decoder);
34
+ }
35
+
36
+ readVarUint(): MessageType {
37
+ return readVarUint(this.decoder);
38
+ }
39
+
40
+ readVarString(): string {
41
+ return readVarString(this.decoder);
42
+ }
43
+
44
+ readVarUint8Array() {
45
+ return readVarUint8Array(this.decoder);
46
+ }
47
+
48
+ writeVarUint(type: MessageType) {
49
+ return writeVarUint(this.encoder, type);
50
+ }
51
+
52
+ writeVarString(string: string) {
53
+ return writeVarString(this.encoder, string);
54
+ }
55
+
56
+ writeVarUint8Array(data: Uint8Array) {
57
+ return writeVarUint8Array(this.encoder, data);
58
+ }
59
+
60
+ length() {
61
+ return length(this.encoder);
62
+ }
63
+ }