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