@hocuspocus/provider 3.1.3 → 3.1.4
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 +111 -92
- package/dist/hocuspocus-provider.cjs.map +1 -1
- package/dist/hocuspocus-provider.esm.js +111 -92
- package/dist/hocuspocus-provider.esm.js.map +1 -1
- package/dist/packages/common/src/auth.d.ts +2 -2
- package/dist/packages/common/src/index.d.ts +4 -4
- package/dist/packages/extension-database/src/Database.d.ts +1 -1
- package/dist/packages/extension-database/src/index.d.ts +1 -1
- package/dist/packages/extension-logger/src/Logger.d.ts +1 -1
- package/dist/packages/extension-logger/src/index.d.ts +1 -1
- package/dist/packages/extension-redis/src/index.d.ts +1 -1
- package/dist/packages/extension-sqlite/src/SQLite.d.ts +3 -3
- package/dist/packages/extension-sqlite/src/index.d.ts +1 -1
- package/dist/packages/extension-throttle/src/index.d.ts +1 -1
- package/dist/packages/extension-webhook/src/index.d.ts +3 -3
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +10 -10
- package/dist/packages/provider/src/IncomingMessage.d.ts +3 -3
- package/dist/packages/provider/src/MessageReceiver.d.ts +2 -2
- package/dist/packages/provider/src/OutgoingMessage.d.ts +2 -2
- package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/AwarenessMessage.d.ts +4 -4
- package/dist/packages/provider/src/OutgoingMessages/CloseMessage.d.ts +4 -4
- package/dist/packages/provider/src/OutgoingMessages/QueryAwarenessMessage.d.ts +4 -4
- package/dist/packages/provider/src/OutgoingMessages/StatelessMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/SyncStepOneMessage.d.ts +4 -4
- package/dist/packages/provider/src/OutgoingMessages/SyncStepTwoMessage.d.ts +4 -4
- package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +3 -3
- package/dist/packages/provider/src/index.d.ts +3 -3
- package/dist/packages/provider/src/types.d.ts +14 -14
- package/dist/packages/server/src/ClientConnection.d.ts +17 -17
- package/dist/packages/server/src/Connection.d.ts +6 -6
- package/dist/packages/server/src/DirectConnection.d.ts +3 -3
- package/dist/packages/server/src/Document.d.ts +4 -4
- package/dist/packages/server/src/Hocuspocus.d.ts +8 -8
- package/dist/packages/server/src/IncomingMessage.d.ts +3 -3
- package/dist/packages/server/src/MessageReceiver.d.ts +3 -3
- package/dist/packages/server/src/OutgoingMessage.d.ts +3 -3
- package/dist/packages/server/src/Server.d.ts +5 -5
- package/dist/packages/server/src/index.d.ts +9 -9
- package/dist/packages/server/src/types.d.ts +7 -7
- package/dist/packages/server/src/util/getParameters.d.ts +5 -5
- package/dist/packages/transformer/src/Prosemirror.d.ts +3 -3
- package/dist/packages/transformer/src/Tiptap.d.ts +3 -3
- package/dist/packages/transformer/src/index.d.ts +3 -3
- package/dist/packages/transformer/src/types.d.ts +1 -1
- package/dist/playground/frontend/app/SocketContext1.d.ts +2 -0
- package/dist/playground/frontend/app/SocketContext2.d.ts +2 -0
- package/package.json +40 -40
- package/src/EventEmitter.ts +32 -32
- package/src/HocuspocusProvider.ts +566 -498
- package/src/IncomingMessage.ts +47 -50
- package/src/MessageReceiver.ts +135 -132
- package/src/OutgoingMessage.ts +18 -14
- package/src/OutgoingMessages/AuthenticationMessage.ts +18 -16
- package/src/OutgoingMessages/AwarenessMessage.ts +38 -32
- package/src/OutgoingMessages/CloseMessage.ts +11 -11
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +11 -12
- package/src/OutgoingMessages/StatelessMessage.ts +12 -12
- package/src/OutgoingMessages/SyncStepOneMessage.ts +18 -16
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +18 -16
- package/src/OutgoingMessages/UpdateMessage.ts +13 -13
- package/src/index.ts +3 -3
- package/src/types.ts +69 -69
- package/dist/playground/frontend/app/SocketContext.d.ts +0 -2
|
@@ -1,508 +1,576 @@
|
|
|
1
|
-
import { awarenessStatesToArray } from
|
|
2
|
-
import type { Event, MessageEvent } from
|
|
3
|
-
import { Awareness, removeAwarenessStates } from
|
|
4
|
-
import * as Y from
|
|
5
|
-
import EventEmitter from
|
|
1
|
+
import { awarenessStatesToArray } from "@hocuspocus/common";
|
|
2
|
+
import type { Event, MessageEvent } from "ws";
|
|
3
|
+
import { Awareness, removeAwarenessStates } from "y-protocols/awareness";
|
|
4
|
+
import * as Y from "yjs";
|
|
5
|
+
import EventEmitter from "./EventEmitter.ts";
|
|
6
|
+
import type { CompleteHocuspocusProviderWebsocketConfiguration } from "./HocuspocusProviderWebsocket.ts";
|
|
7
|
+
import { HocuspocusProviderWebsocket } from "./HocuspocusProviderWebsocket.ts";
|
|
8
|
+
import { IncomingMessage } from "./IncomingMessage.ts";
|
|
9
|
+
import { MessageReceiver } from "./MessageReceiver.ts";
|
|
10
|
+
import { MessageSender } from "./MessageSender.ts";
|
|
11
|
+
import { AuthenticationMessage } from "./OutgoingMessages/AuthenticationMessage.ts";
|
|
12
|
+
import { AwarenessMessage } from "./OutgoingMessages/AwarenessMessage.ts";
|
|
13
|
+
import { StatelessMessage } from "./OutgoingMessages/StatelessMessage.ts";
|
|
14
|
+
import { SyncStepOneMessage } from "./OutgoingMessages/SyncStepOneMessage.ts";
|
|
15
|
+
import { UpdateMessage } from "./OutgoingMessages/UpdateMessage.ts";
|
|
6
16
|
import type {
|
|
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
|
-
onUnsyncedChangesParameters,
|
|
33
|
-
} from './types.ts'
|
|
34
|
-
|
|
35
|
-
export type HocuspocusProviderConfiguration =
|
|
36
|
-
Required<Pick<CompleteHocuspocusProviderConfiguration, 'name'>>
|
|
37
|
-
& Partial<CompleteHocuspocusProviderConfiguration> & (
|
|
38
|
-
Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, 'url'>> |
|
|
39
|
-
Required<Pick<CompleteHocuspocusProviderConfiguration, 'websocketProvider'>>
|
|
40
|
-
)
|
|
17
|
+
ConstructableOutgoingMessage,
|
|
18
|
+
onAuthenticatedParameters,
|
|
19
|
+
onAuthenticationFailedParameters,
|
|
20
|
+
onAwarenessChangeParameters,
|
|
21
|
+
onAwarenessUpdateParameters,
|
|
22
|
+
onCloseParameters,
|
|
23
|
+
onDisconnectParameters,
|
|
24
|
+
onMessageParameters,
|
|
25
|
+
onOpenParameters,
|
|
26
|
+
onOutgoingMessageParameters,
|
|
27
|
+
onStatelessParameters,
|
|
28
|
+
onSyncedParameters,
|
|
29
|
+
onUnsyncedChangesParameters,
|
|
30
|
+
} from "./types.ts";
|
|
31
|
+
|
|
32
|
+
export type HocuspocusProviderConfiguration = Required<
|
|
33
|
+
Pick<CompleteHocuspocusProviderConfiguration, "name">
|
|
34
|
+
> &
|
|
35
|
+
Partial<CompleteHocuspocusProviderConfiguration> &
|
|
36
|
+
(
|
|
37
|
+
| Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">>
|
|
38
|
+
| Required<
|
|
39
|
+
Pick<CompleteHocuspocusProviderConfiguration, "websocketProvider">
|
|
40
|
+
>
|
|
41
|
+
);
|
|
41
42
|
|
|
42
43
|
export interface CompleteHocuspocusProviderConfiguration {
|
|
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
|
-
|
|
44
|
+
/**
|
|
45
|
+
* The identifier/name of your document
|
|
46
|
+
*/
|
|
47
|
+
name: string;
|
|
48
|
+
/**
|
|
49
|
+
* The actual Y.js document
|
|
50
|
+
*/
|
|
51
|
+
document: Y.Doc;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* An Awareness instance to keep the presence state of all clients.
|
|
55
|
+
*
|
|
56
|
+
* You can disable sharing awareness information by passing `null`.
|
|
57
|
+
* Note that having no awareness information shared across all connections will break our ping checks
|
|
58
|
+
* and thus trigger reconnects. You should always have at least one Provider with enabled awareness per
|
|
59
|
+
* socket connection, or ensure that the Provider receives messages before running into `HocuspocusProviderWebsocket.messageReconnectTimeout`.
|
|
60
|
+
*/
|
|
61
|
+
awareness: Awareness | null;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A token that’s sent to the backend for authentication purposes.
|
|
65
|
+
*/
|
|
66
|
+
token: string | (() => string) | (() => Promise<string>) | null;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Hocuspocus websocket provider
|
|
70
|
+
*/
|
|
71
|
+
websocketProvider: HocuspocusProviderWebsocket;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Force syncing the document in the defined interval.
|
|
75
|
+
*/
|
|
76
|
+
forceSyncInterval: false | number;
|
|
77
|
+
|
|
78
|
+
onAuthenticated: (data: onAuthenticatedParameters) => void;
|
|
79
|
+
onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;
|
|
80
|
+
onOpen: (data: onOpenParameters) => void;
|
|
81
|
+
onConnect: () => void;
|
|
82
|
+
onMessage: (data: onMessageParameters) => void;
|
|
83
|
+
onOutgoingMessage: (data: onOutgoingMessageParameters) => void;
|
|
84
|
+
onSynced: (data: onSyncedParameters) => void;
|
|
85
|
+
onDisconnect: (data: onDisconnectParameters) => void;
|
|
86
|
+
onClose: (data: onCloseParameters) => void;
|
|
87
|
+
onDestroy: () => void;
|
|
88
|
+
onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
|
|
89
|
+
onAwarenessChange: (data: onAwarenessChangeParameters) => void;
|
|
90
|
+
onStateless: (data: onStatelessParameters) => void;
|
|
91
|
+
onUnsyncedChanges: (data: onUnsyncedChangesParameters) => void;
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
export class AwarenessError extends Error {
|
|
94
|
-
|
|
95
|
+
code = 1001;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
export class HocuspocusProvider extends EventEmitter {
|
|
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
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
99
|
+
public configuration: CompleteHocuspocusProviderConfiguration = {
|
|
100
|
+
name: "",
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
document: undefined,
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
awareness: undefined,
|
|
105
|
+
token: null,
|
|
106
|
+
forceSyncInterval: false,
|
|
107
|
+
onAuthenticated: () => null,
|
|
108
|
+
onAuthenticationFailed: () => null,
|
|
109
|
+
onOpen: () => null,
|
|
110
|
+
onConnect: () => null,
|
|
111
|
+
onMessage: () => null,
|
|
112
|
+
onOutgoingMessage: () => null,
|
|
113
|
+
onSynced: () => null,
|
|
114
|
+
onDisconnect: () => null,
|
|
115
|
+
onClose: () => null,
|
|
116
|
+
onDestroy: () => null,
|
|
117
|
+
onAwarenessUpdate: () => null,
|
|
118
|
+
onAwarenessChange: () => null,
|
|
119
|
+
onStateless: () => null,
|
|
120
|
+
onUnsyncedChanges: () => null,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
isSynced = false;
|
|
124
|
+
|
|
125
|
+
unsyncedChanges = 0;
|
|
126
|
+
|
|
127
|
+
isAuthenticated = false;
|
|
128
|
+
|
|
129
|
+
authorizedScope: string | undefined = undefined;
|
|
130
|
+
|
|
131
|
+
// @internal
|
|
132
|
+
manageSocket = false;
|
|
133
|
+
|
|
134
|
+
private _isAttached = false;
|
|
135
|
+
|
|
136
|
+
intervals: any = {
|
|
137
|
+
forceSync: null,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
constructor(configuration: HocuspocusProviderConfiguration) {
|
|
141
|
+
super();
|
|
142
|
+
this.setConfiguration(configuration);
|
|
143
|
+
|
|
144
|
+
this.configuration.document = configuration.document
|
|
145
|
+
? configuration.document
|
|
146
|
+
: new Y.Doc();
|
|
147
|
+
this.configuration.awareness =
|
|
148
|
+
configuration.awareness !== undefined
|
|
149
|
+
? configuration.awareness
|
|
150
|
+
: new Awareness(this.document);
|
|
151
|
+
|
|
152
|
+
this.on("open", this.configuration.onOpen);
|
|
153
|
+
this.on("message", this.configuration.onMessage);
|
|
154
|
+
this.on("outgoingMessage", this.configuration.onOutgoingMessage);
|
|
155
|
+
this.on("synced", this.configuration.onSynced);
|
|
156
|
+
this.on("destroy", this.configuration.onDestroy);
|
|
157
|
+
this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
|
|
158
|
+
this.on("awarenessChange", this.configuration.onAwarenessChange);
|
|
159
|
+
this.on("stateless", this.configuration.onStateless);
|
|
160
|
+
this.on("unsyncedChanges", this.configuration.onUnsyncedChanges);
|
|
161
|
+
|
|
162
|
+
this.on("authenticated", this.configuration.onAuthenticated);
|
|
163
|
+
this.on("authenticationFailed", this.configuration.onAuthenticationFailed);
|
|
164
|
+
|
|
165
|
+
this.awareness?.on("update", () => {
|
|
166
|
+
this.emit("awarenessUpdate", {
|
|
167
|
+
states: awarenessStatesToArray(this.awareness!.getStates()),
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
this.awareness?.on("change", () => {
|
|
172
|
+
this.emit("awarenessChange", {
|
|
173
|
+
states: awarenessStatesToArray(this.awareness!.getStates()),
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
this.document.on("update", this.boundDocumentUpdateHandler);
|
|
178
|
+
this.awareness?.on("update", this.boundAwarenessUpdateHandler);
|
|
179
|
+
|
|
180
|
+
this.registerEventListeners();
|
|
181
|
+
|
|
182
|
+
if (
|
|
183
|
+
this.configuration.forceSyncInterval &&
|
|
184
|
+
typeof this.configuration.forceSyncInterval === "number"
|
|
185
|
+
) {
|
|
186
|
+
this.intervals.forceSync = setInterval(
|
|
187
|
+
this.forceSync.bind(this),
|
|
188
|
+
this.configuration.forceSyncInterval,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (this.manageSocket) {
|
|
193
|
+
this.attach();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
|
|
198
|
+
|
|
199
|
+
boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
|
|
200
|
+
|
|
201
|
+
boundPageHide = this.pageHide.bind(this);
|
|
202
|
+
|
|
203
|
+
boundOnOpen = this.onOpen.bind(this);
|
|
204
|
+
|
|
205
|
+
boundOnClose = this.onClose.bind(this);
|
|
206
|
+
|
|
207
|
+
forwardConnect = (e: any) => this.emit("connect", e);
|
|
208
|
+
|
|
209
|
+
forwardClose = (e: any) => this.emit("close", e);
|
|
210
|
+
|
|
211
|
+
forwardDisconnect = (e: any) => this.emit("disconnect", e);
|
|
212
|
+
|
|
213
|
+
forwardDestroy = (e: any) => this.emit("destroy", e);
|
|
214
|
+
|
|
215
|
+
public setConfiguration(
|
|
216
|
+
configuration: Partial<HocuspocusProviderConfiguration> = {},
|
|
217
|
+
): void {
|
|
218
|
+
if (!configuration.websocketProvider) {
|
|
219
|
+
const websocketProviderConfig =
|
|
220
|
+
configuration as CompleteHocuspocusProviderWebsocketConfiguration;
|
|
221
|
+
this.manageSocket = true;
|
|
222
|
+
this.configuration.websocketProvider = new HocuspocusProviderWebsocket({
|
|
223
|
+
url: websocketProviderConfig.url,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
this.configuration = { ...this.configuration, ...configuration };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
get document() {
|
|
231
|
+
return this.configuration.document;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
public get isAttached() {
|
|
235
|
+
return this._isAttached;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
get awareness() {
|
|
239
|
+
return this.configuration.awareness;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
get hasUnsyncedChanges(): boolean {
|
|
243
|
+
return this.unsyncedChanges > 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private resetUnsyncedChanges() {
|
|
247
|
+
this.unsyncedChanges = 1;
|
|
248
|
+
this.emit("unsyncedChanges", { number: this.unsyncedChanges });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
incrementUnsyncedChanges() {
|
|
252
|
+
this.unsyncedChanges += 1;
|
|
253
|
+
this.emit("unsyncedChanges", { number: this.unsyncedChanges });
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
decrementUnsyncedChanges() {
|
|
257
|
+
if (this.unsyncedChanges > 0) {
|
|
258
|
+
this.unsyncedChanges -= 1;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (this.unsyncedChanges === 0) {
|
|
262
|
+
this.synced = true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
this.emit("unsyncedChanges", { number: this.unsyncedChanges });
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
forceSync() {
|
|
269
|
+
this.resetUnsyncedChanges();
|
|
270
|
+
|
|
271
|
+
this.send(SyncStepOneMessage, {
|
|
272
|
+
document: this.document,
|
|
273
|
+
documentName: this.configuration.name,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
pageHide() {
|
|
278
|
+
if (this.awareness) {
|
|
279
|
+
removeAwarenessStates(
|
|
280
|
+
this.awareness,
|
|
281
|
+
[this.document.clientID],
|
|
282
|
+
"page hide",
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
registerEventListeners() {
|
|
288
|
+
if (typeof window === "undefined" || !("addEventListener" in window)) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
window.addEventListener("pagehide", this.boundPageHide);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
sendStateless(payload: string) {
|
|
296
|
+
this.send(StatelessMessage, {
|
|
297
|
+
documentName: this.configuration.name,
|
|
298
|
+
payload,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
documentUpdateHandler(update: Uint8Array, origin: any) {
|
|
303
|
+
if (origin === this) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
this.incrementUnsyncedChanges();
|
|
308
|
+
this.send(UpdateMessage, { update, documentName: this.configuration.name });
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
awarenessUpdateHandler({ added, updated, removed }: any, origin: any) {
|
|
312
|
+
const changedClients = added.concat(updated).concat(removed);
|
|
313
|
+
|
|
314
|
+
this.send(AwarenessMessage, {
|
|
315
|
+
awareness: this.awareness,
|
|
316
|
+
clients: changedClients,
|
|
317
|
+
documentName: this.configuration.name,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Indicates whether a first handshake with the server has been established
|
|
323
|
+
*
|
|
324
|
+
* Note: this does not mean all updates from the client have been persisted to the backend. For this,
|
|
325
|
+
* use `hasUnsyncedChanges`.
|
|
326
|
+
*/
|
|
327
|
+
get synced(): boolean {
|
|
328
|
+
return this.isSynced;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
set synced(state) {
|
|
332
|
+
if (this.isSynced === state) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
this.isSynced = state;
|
|
337
|
+
|
|
338
|
+
if (state) {
|
|
339
|
+
this.emit("synced", { state });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
receiveStateless(payload: string) {
|
|
344
|
+
this.emit("stateless", { payload });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// not needed, but provides backward compatibility with e.g. lexical/yjs
|
|
348
|
+
async connect() {
|
|
349
|
+
if (this.manageSocket) {
|
|
350
|
+
return this.configuration.websocketProvider.connect();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
console.warn(
|
|
354
|
+
"HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.",
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
disconnect() {
|
|
359
|
+
if (this.manageSocket) {
|
|
360
|
+
return this.configuration.websocketProvider.disconnect();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
console.warn(
|
|
364
|
+
"HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.",
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async onOpen(event: Event) {
|
|
369
|
+
this.isAuthenticated = false;
|
|
370
|
+
|
|
371
|
+
this.emit("open", { event });
|
|
372
|
+
|
|
373
|
+
let token: string | null;
|
|
374
|
+
try {
|
|
375
|
+
token = await this.getToken();
|
|
376
|
+
} catch (error) {
|
|
377
|
+
this.permissionDeniedHandler(`Failed to get token: ${error}`);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
this.send(AuthenticationMessage, {
|
|
382
|
+
token: token ?? "",
|
|
383
|
+
documentName: this.configuration.name,
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
this.startSync();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async getToken() {
|
|
390
|
+
if (typeof this.configuration.token === "function") {
|
|
391
|
+
const token = await this.configuration.token();
|
|
392
|
+
return token;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return this.configuration.token;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
startSync() {
|
|
399
|
+
this.resetUnsyncedChanges();
|
|
400
|
+
|
|
401
|
+
this.send(SyncStepOneMessage, {
|
|
402
|
+
document: this.document,
|
|
403
|
+
documentName: this.configuration.name,
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
if (this.awareness && this.awareness.getLocalState() !== null) {
|
|
407
|
+
this.send(AwarenessMessage, {
|
|
408
|
+
awareness: this.awareness,
|
|
409
|
+
clients: [this.document.clientID],
|
|
410
|
+
documentName: this.configuration.name,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
send(message: ConstructableOutgoingMessage, args: any) {
|
|
416
|
+
if (!this._isAttached) return;
|
|
417
|
+
|
|
418
|
+
const messageSender = new MessageSender(message, args);
|
|
419
|
+
|
|
420
|
+
this.emit("outgoingMessage", { message: messageSender.message });
|
|
421
|
+
messageSender.send(this.configuration.websocketProvider);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
onMessage(event: MessageEvent) {
|
|
425
|
+
const message = new IncomingMessage(event.data);
|
|
426
|
+
|
|
427
|
+
const documentName = message.readVarString();
|
|
428
|
+
|
|
429
|
+
message.writeVarString(documentName);
|
|
430
|
+
|
|
431
|
+
this.emit("message", { event, message: new IncomingMessage(event.data) });
|
|
432
|
+
|
|
433
|
+
new MessageReceiver(message).apply(this, true);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
onClose() {
|
|
437
|
+
this.isAuthenticated = false;
|
|
438
|
+
this.synced = false;
|
|
439
|
+
|
|
440
|
+
// update awareness (all users except local left)
|
|
441
|
+
if (this.awareness) {
|
|
442
|
+
removeAwarenessStates(
|
|
443
|
+
this.awareness,
|
|
444
|
+
Array.from(this.awareness.getStates().keys()).filter(
|
|
445
|
+
(client) => client !== this.document.clientID,
|
|
446
|
+
),
|
|
447
|
+
this,
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
destroy() {
|
|
453
|
+
this.emit("destroy");
|
|
454
|
+
|
|
455
|
+
if (this.intervals.forceSync) {
|
|
456
|
+
clearInterval(this.intervals.forceSync);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (this.awareness) {
|
|
460
|
+
removeAwarenessStates(
|
|
461
|
+
this.awareness,
|
|
462
|
+
[this.document.clientID],
|
|
463
|
+
"provider destroy",
|
|
464
|
+
);
|
|
465
|
+
this.awareness.off("update", this.boundAwarenessUpdateHandler);
|
|
466
|
+
this.awareness.destroy();
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
this.document.off("update", this.boundDocumentUpdateHandler);
|
|
470
|
+
|
|
471
|
+
this.removeAllListeners();
|
|
472
|
+
|
|
473
|
+
this.detach();
|
|
474
|
+
|
|
475
|
+
if (this.manageSocket) {
|
|
476
|
+
this.configuration.websocketProvider.destroy();
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (typeof window === "undefined" || !("removeEventListener" in window)) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
window.removeEventListener("pagehide", this.boundPageHide);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
detach() {
|
|
487
|
+
this.configuration.websocketProvider.off(
|
|
488
|
+
"connect",
|
|
489
|
+
this.configuration.onConnect,
|
|
490
|
+
);
|
|
491
|
+
this.configuration.websocketProvider.off("connect", this.forwardConnect);
|
|
492
|
+
this.configuration.websocketProvider.off("open", this.boundOnOpen);
|
|
493
|
+
this.configuration.websocketProvider.off("close", this.boundOnClose);
|
|
494
|
+
this.configuration.websocketProvider.off(
|
|
495
|
+
"close",
|
|
496
|
+
this.configuration.onClose,
|
|
497
|
+
);
|
|
498
|
+
this.configuration.websocketProvider.off("close", this.forwardClose);
|
|
499
|
+
this.configuration.websocketProvider.off(
|
|
500
|
+
"disconnect",
|
|
501
|
+
this.configuration.onDisconnect,
|
|
502
|
+
);
|
|
503
|
+
this.configuration.websocketProvider.off(
|
|
504
|
+
"disconnect",
|
|
505
|
+
this.forwardDisconnect,
|
|
506
|
+
);
|
|
507
|
+
this.configuration.websocketProvider.off(
|
|
508
|
+
"destroy",
|
|
509
|
+
this.configuration.onDestroy,
|
|
510
|
+
);
|
|
511
|
+
this.configuration.websocketProvider.off("destroy", this.forwardDestroy);
|
|
512
|
+
|
|
513
|
+
this.configuration.websocketProvider.detach(this);
|
|
514
|
+
|
|
515
|
+
this._isAttached = false;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
attach() {
|
|
519
|
+
if (this._isAttached) return;
|
|
520
|
+
|
|
521
|
+
this.configuration.websocketProvider.on(
|
|
522
|
+
"connect",
|
|
523
|
+
this.configuration.onConnect,
|
|
524
|
+
);
|
|
525
|
+
this.configuration.websocketProvider.on("connect", this.forwardConnect);
|
|
526
|
+
|
|
527
|
+
this.configuration.websocketProvider.on("open", this.boundOnOpen);
|
|
528
|
+
|
|
529
|
+
this.configuration.websocketProvider.on("close", this.boundOnClose);
|
|
530
|
+
this.configuration.websocketProvider.on(
|
|
531
|
+
"close",
|
|
532
|
+
this.configuration.onClose,
|
|
533
|
+
);
|
|
534
|
+
this.configuration.websocketProvider.on("close", this.forwardClose);
|
|
535
|
+
|
|
536
|
+
this.configuration.websocketProvider.on(
|
|
537
|
+
"disconnect",
|
|
538
|
+
this.configuration.onDisconnect,
|
|
539
|
+
);
|
|
540
|
+
this.configuration.websocketProvider.on(
|
|
541
|
+
"disconnect",
|
|
542
|
+
this.forwardDisconnect,
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
this.configuration.websocketProvider.on(
|
|
546
|
+
"destroy",
|
|
547
|
+
this.configuration.onDestroy,
|
|
548
|
+
);
|
|
549
|
+
this.configuration.websocketProvider.on("destroy", this.forwardDestroy);
|
|
550
|
+
|
|
551
|
+
this.configuration.websocketProvider.attach(this);
|
|
552
|
+
|
|
553
|
+
this._isAttached = true;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
permissionDeniedHandler(reason: string) {
|
|
557
|
+
this.emit("authenticationFailed", { reason });
|
|
558
|
+
this.isAuthenticated = false;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
authenticatedHandler(scope: string) {
|
|
562
|
+
this.isAuthenticated = true;
|
|
563
|
+
this.authorizedScope = scope;
|
|
564
|
+
|
|
565
|
+
this.emit("authenticated", { scope });
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
setAwarenessField(key: string, value: any) {
|
|
569
|
+
if (!this.awareness) {
|
|
570
|
+
throw new AwarenessError(
|
|
571
|
+
`Cannot set awareness field "${key}" to ${JSON.stringify(value)}. You have disabled Awareness for this provider by explicitly passing awareness: null in the provider configuration.`,
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
this.awareness.setLocalStateField(key, value);
|
|
575
|
+
}
|
|
508
576
|
}
|