@abraca/dabra 0.1.0 → 0.1.2

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.
@@ -1,4 +1,4 @@
1
- import { WsReadyStates } from "@hocuspocus/common";
1
+ import { WsReadyStates } from "@abraca/dabra-common";
2
2
  import { retry } from "@lifeomic/attempt";
3
3
  import * as time from "lib0/time";
4
4
  import type { Event, MessageEvent } from "ws";
@@ -7,527 +7,523 @@ import type { HocuspocusProvider } from "./HocuspocusProvider.ts";
7
7
  import { IncomingMessage } from "./IncomingMessage.ts";
8
8
  import { CloseMessage } from "./OutgoingMessages/CloseMessage.ts";
9
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,
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
19
  } from "./types.ts";
20
20
 
21
21
  export type HocuspocusWebSocket = WebSocket & { identifier: string };
22
22
  export type HocusPocusWebSocket = HocuspocusWebSocket;
23
23
 
24
24
  export type HocuspocusProviderWebsocketConfiguration = Required<
25
- Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">
25
+ Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">
26
26
  > &
27
- Partial<CompleteHocuspocusProviderWebsocketConfiguration>;
27
+ Partial<CompleteHocuspocusProviderWebsocketConfiguration>;
28
28
 
29
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>;
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
103
  }
104
104
 
105
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
- }
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 (this.receivedOnOpenPayload && this.status === WebSocketStatus.Connected) {
219
+ provider.onOpen(this.receivedOnOpenPayload);
220
+ }
221
+ }
222
+
223
+ detach(provider: HocuspocusProvider) {
224
+ if (this.configuration.providerMap.has(provider.configuration.name)) {
225
+ provider.send(CloseMessage, {
226
+ documentName: provider.configuration.name,
227
+ });
228
+ this.configuration.providerMap.delete(provider.configuration.name);
229
+ }
230
+ }
231
+
232
+ public setConfiguration(
233
+ configuration: Partial<HocuspocusProviderWebsocketConfiguration> = {},
234
+ ): void {
235
+ this.configuration = { ...this.configuration, ...configuration };
236
+
237
+ if (!this.configuration.autoConnect) {
238
+ this.shouldConnect = false;
239
+ }
240
+ }
241
+
242
+ cancelWebsocketRetry?: () => void;
243
+
244
+ async connect() {
245
+ if (this.status === WebSocketStatus.Connected) {
246
+ return;
247
+ }
248
+
249
+ // Always cancel any previously initiated connection retryer instances
250
+ if (this.cancelWebsocketRetry) {
251
+ this.cancelWebsocketRetry();
252
+ this.cancelWebsocketRetry = undefined;
253
+ }
254
+
255
+ this.receivedOnOpenPayload = undefined;
256
+ this.shouldConnect = true;
257
+
258
+ const abortableRetry = () => {
259
+ let cancelAttempt = false;
260
+
261
+ const retryPromise = retry(this.createWebSocketConnection.bind(this), {
262
+ delay: this.configuration.delay,
263
+ initialDelay: this.configuration.initialDelay,
264
+ factor: this.configuration.factor,
265
+ maxAttempts: this.configuration.maxAttempts,
266
+ minDelay: this.configuration.minDelay,
267
+ maxDelay: this.configuration.maxDelay,
268
+ jitter: this.configuration.jitter,
269
+ timeout: this.configuration.timeout,
270
+ handleTimeout: this.configuration.handleTimeout,
271
+ beforeAttempt: (context) => {
272
+ if (!this.shouldConnect || cancelAttempt) {
273
+ context.abort();
274
+ }
275
+ },
276
+ }).catch((error: any) => {
277
+ // If we aborted the connection attempt then don’t throw an error
278
+ // ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
279
+ if (error && error.code !== "ATTEMPT_ABORTED") {
280
+ throw error;
281
+ }
282
+ });
283
+
284
+ return {
285
+ retryPromise,
286
+ cancelFunc: () => {
287
+ cancelAttempt = true;
288
+ },
289
+ };
290
+ };
291
+
292
+ const { retryPromise, cancelFunc } = abortableRetry();
293
+ this.cancelWebsocketRetry = cancelFunc;
294
+
295
+ return retryPromise;
296
+ }
297
+
298
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
299
+ attachWebSocketListeners(ws: HocusPocusWebSocket, reject: Function) {
300
+ const { identifier } = ws;
301
+ const onMessageHandler = (payload: any) => this.emit("message", payload);
302
+ const onCloseHandler = (payload: any) => this.emit("close", { event: payload });
303
+ const onOpenHandler = (payload: any) => this.emit("open", payload);
304
+ const onErrorHandler = (err: any) => {
305
+ reject(err);
306
+ };
307
+
308
+ this.webSocketHandlers[identifier] = {
309
+ message: onMessageHandler,
310
+ close: onCloseHandler,
311
+ open: onOpenHandler,
312
+ error: onErrorHandler,
313
+ };
314
+
315
+ const handlers = this.webSocketHandlers[ws.identifier];
316
+
317
+ Object.keys(handlers).forEach((name) => {
318
+ ws.addEventListener(name, handlers[name]);
319
+ });
320
+ }
321
+
322
+ cleanupWebSocket() {
323
+ if (!this.webSocket) {
324
+ return;
325
+ }
326
+ const { identifier } = this.webSocket;
327
+ const handlers = this.webSocketHandlers[identifier];
328
+
329
+ Object.keys(handlers).forEach((name) => {
330
+ this.webSocket?.removeEventListener(name, handlers[name]);
331
+ delete this.webSocketHandlers[identifier];
332
+ });
333
+
334
+ try {
335
+ // Check if the WebSocket is still in CONNECTING state (0)
336
+ // If so, calling close() might throw in some environments or be race-prone
337
+ if (this.webSocket.readyState !== 0 && this.webSocket.readyState !== 3) {
338
+ this.webSocket.close();
339
+ }
340
+ } catch (e) {
341
+ // Ignore errors during close
342
+ }
343
+
344
+ this.webSocket = null;
345
+ }
346
+
347
+ createWebSocketConnection() {
348
+ return new Promise((resolve, reject) => {
349
+ if (this.webSocket) {
350
+ this.messageQueue = [];
351
+ this.cleanupWebSocket();
352
+ }
353
+ this.lastMessageReceived = 0;
354
+ this.identifier += 1;
355
+
356
+ // Init the WebSocket connection
357
+ const ws = new this.configuration.WebSocketPolyfill(this.url);
358
+ ws.binaryType = "arraybuffer";
359
+ ws.identifier = this.identifier;
360
+
361
+ this.attachWebSocketListeners(ws, reject);
362
+
363
+ this.webSocket = ws;
364
+
365
+ // Reset the status
366
+ this.status = WebSocketStatus.Connecting;
367
+ this.emit("status", { status: WebSocketStatus.Connecting });
368
+
369
+ // Store resolve/reject for later use
370
+ this.connectionAttempt = {
371
+ resolve,
372
+ reject,
373
+ };
374
+ });
375
+ }
376
+
377
+ onMessage(event: MessageEvent) {
378
+ this.resolveConnectionAttempt();
379
+
380
+ this.lastMessageReceived = time.getUnixTime();
381
+
382
+ const message = new IncomingMessage(event.data);
383
+ const documentName = message.peekVarString();
384
+
385
+ this.configuration.providerMap.get(documentName)?.onMessage(event);
386
+ }
387
+
388
+ resolveConnectionAttempt() {
389
+ if (this.connectionAttempt) {
390
+ this.connectionAttempt.resolve();
391
+ this.connectionAttempt = null;
392
+
393
+ this.status = WebSocketStatus.Connected;
394
+ this.emit("status", { status: WebSocketStatus.Connected });
395
+ this.emit("connect");
396
+ this.messageQueue.forEach((message) => this.send(message));
397
+ this.messageQueue = [];
398
+ }
399
+ }
400
+
401
+ stopConnectionAttempt() {
402
+ this.connectionAttempt = null;
403
+ }
404
+
405
+ rejectConnectionAttempt() {
406
+ this.connectionAttempt?.reject();
407
+ this.connectionAttempt = null;
408
+ }
409
+
410
+ closeTries = 0;
411
+
412
+ checkConnection() {
413
+ // Don’t check the connection when it’s not even established
414
+ if (this.status !== WebSocketStatus.Connected) {
415
+ return;
416
+ }
417
+
418
+ // Don’t close the connection while waiting for the first message
419
+ if (!this.lastMessageReceived) {
420
+ return;
421
+ }
422
+
423
+ // Don’t close the connection when a message was received recently
424
+ if (
425
+ this.configuration.messageReconnectTimeout >=
426
+ time.getUnixTime() - this.lastMessageReceived
427
+ ) {
428
+ return;
429
+ }
430
+
431
+ // No message received in a long time, not even your own
432
+ // Awareness updates, which are updated every 15 seconds
433
+ // if awareness is enabled.
434
+ this.closeTries += 1;
435
+ // https://bugs.webkit.org/show_bug.cgi?id=247943
436
+ if (this.closeTries > 2) {
437
+ this.onClose({
438
+ event: {
439
+ code: 4408,
440
+ reason: "forced",
441
+ },
442
+ });
443
+ this.closeTries = 0;
444
+ } else {
445
+ this.webSocket?.close();
446
+ this.messageQueue = [];
447
+ }
448
+ }
449
+
450
+ get serverUrl() {
451
+ if (this.configuration.preserveTrailingSlash) {
452
+ return this.configuration.url;
453
+ }
454
+
455
+ // By default, ensure that the URL never ends with /
456
+ let url = this.configuration.url;
457
+ while (url[url.length - 1] === "/") {
458
+ url = url.slice(0, url.length - 1);
459
+ }
460
+
461
+ return url;
462
+ }
463
+
464
+ get url() {
465
+ return this.serverUrl;
466
+ }
467
+
468
+ disconnect() {
469
+ this.shouldConnect = false;
470
+
471
+ if (this.webSocket === null) {
472
+ return;
473
+ }
474
+
475
+ try {
476
+ this.webSocket.close();
477
+ this.messageQueue = [];
478
+ } catch (e) {
479
+ console.error(e);
480
+ }
481
+ }
482
+
483
+ send(message: any) {
484
+ if (this.webSocket?.readyState === WsReadyStates.Open) {
485
+ this.webSocket.send(message);
486
+ } else {
487
+ this.messageQueue.push(message);
488
+ }
489
+ }
490
+
491
+ onClose({ event }: onCloseParameters) {
492
+ this.closeTries = 0;
493
+ this.cleanupWebSocket();
494
+
495
+ if (this.connectionAttempt) {
496
+ // That connection attempt failed.
497
+ this.rejectConnectionAttempt();
498
+ }
499
+
500
+ // Let’s update the connection status.
501
+ this.status = WebSocketStatus.Disconnected;
502
+ this.emit("status", { status: WebSocketStatus.Disconnected });
503
+ this.emit("disconnect", { event });
504
+
505
+ // trigger connect if no retry is running and we want to have a connection
506
+ if (!this.cancelWebsocketRetry && this.shouldConnect) {
507
+ setTimeout(() => {
508
+ this.connect();
509
+ }, this.configuration.delay);
510
+ }
511
+ }
512
+
513
+ destroy() {
514
+ this.emit("destroy");
515
+
516
+ clearInterval(this.intervals.connectionChecker);
517
+
518
+ // If there is still a connection attempt outstanding then we should stop
519
+ // it before calling disconnect, otherwise it will be rejected in the onClose
520
+ // handler and trigger a retry
521
+ this.stopConnectionAttempt();
522
+
523
+ this.disconnect();
524
+
525
+ this.removeAllListeners();
526
+
527
+ this.cleanupWebSocket();
528
+ }
533
529
  }