@hocuspocus/extension-redis 3.0.8-rc.0 → 3.1.0-rc.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-redis.cjs +31 -27
- package/dist/hocuspocus-redis.cjs.map +1 -1
- package/dist/hocuspocus-redis.esm.js +31 -27
- package/dist/hocuspocus-redis.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 +3 -4
- package/src/Redis.ts +424 -393
package/src/Redis.ts
CHANGED
|
@@ -1,402 +1,433 @@
|
|
|
1
|
-
import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'
|
|
2
|
-
import RedisClient from 'ioredis'
|
|
3
|
-
import Redlock from 'redlock'
|
|
4
|
-
import { v4 as uuid } from 'uuid'
|
|
5
1
|
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
Document,
|
|
3
|
+
Extension,
|
|
4
|
+
Hocuspocus,
|
|
5
|
+
afterLoadDocumentPayload,
|
|
6
|
+
afterStoreDocumentPayload,
|
|
7
|
+
beforeBroadcastStatelessPayload,
|
|
8
|
+
onAwarenessUpdatePayload,
|
|
9
|
+
onChangePayload,
|
|
10
|
+
onConfigurePayload,
|
|
11
|
+
onDisconnectPayload,
|
|
12
|
+
onStoreDocumentPayload,
|
|
13
|
+
} from "@hocuspocus/server";
|
|
16
14
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from
|
|
15
|
+
IncomingMessage,
|
|
16
|
+
MessageReceiver,
|
|
17
|
+
OutgoingMessage,
|
|
18
|
+
} from "@hocuspocus/server";
|
|
19
|
+
import type { ClusterNode, ClusterOptions, RedisOptions } from "ioredis";
|
|
20
|
+
import RedisClient from "ioredis";
|
|
21
|
+
import Redlock from "redlock";
|
|
22
|
+
import { v4 as uuid } from "uuid";
|
|
21
23
|
|
|
22
|
-
export type RedisInstance = RedisClient.Cluster | RedisClient.Redis
|
|
24
|
+
export type RedisInstance = RedisClient.Cluster | RedisClient.Redis;
|
|
23
25
|
|
|
24
26
|
export interface Configuration {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Redis port
|
|
29
|
+
*/
|
|
30
|
+
port: number;
|
|
31
|
+
/**
|
|
32
|
+
* Redis host
|
|
33
|
+
*/
|
|
34
|
+
host: string;
|
|
35
|
+
/**
|
|
36
|
+
* Redis Cluster
|
|
37
|
+
*/
|
|
38
|
+
nodes?: ClusterNode[];
|
|
39
|
+
/**
|
|
40
|
+
* Duplicate from an existed Redis instance
|
|
41
|
+
*/
|
|
42
|
+
redis?: RedisInstance;
|
|
43
|
+
/**
|
|
44
|
+
* Redis instance creator
|
|
45
|
+
*/
|
|
46
|
+
createClient?: () => RedisInstance;
|
|
47
|
+
/**
|
|
48
|
+
* Options passed directly to Redis constructor
|
|
49
|
+
*
|
|
50
|
+
* https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
|
|
51
|
+
*/
|
|
52
|
+
options?: ClusterOptions | RedisOptions;
|
|
53
|
+
/**
|
|
54
|
+
* An unique instance name, required to filter messages in Redis.
|
|
55
|
+
* If none is provided an unique id is generated.
|
|
56
|
+
*/
|
|
57
|
+
identifier: string;
|
|
58
|
+
/**
|
|
59
|
+
* Namespace for Redis keys, if none is provided 'hocuspocus' is used
|
|
60
|
+
*/
|
|
61
|
+
prefix: string;
|
|
62
|
+
/**
|
|
63
|
+
* The maximum time for the Redis lock in ms (in case it can’t be released).
|
|
64
|
+
*/
|
|
65
|
+
lockTimeout: number;
|
|
66
|
+
/**
|
|
67
|
+
* A delay before onDisconnect is executed. This allows last minute updates'
|
|
68
|
+
* sync messages to be received by the subscription before it's closed.
|
|
69
|
+
*/
|
|
70
|
+
disconnectDelay: number;
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
export class Redis implements Extension {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Make sure to give that extension a higher priority, so
|
|
76
|
+
* the `onStoreDocument` hook is able to intercept the chain,
|
|
77
|
+
* before documents are stored to the database.
|
|
78
|
+
*/
|
|
79
|
+
priority = 1000;
|
|
80
|
+
|
|
81
|
+
configuration: Configuration = {
|
|
82
|
+
port: 6379,
|
|
83
|
+
host: "127.0.0.1",
|
|
84
|
+
prefix: "hocuspocus",
|
|
85
|
+
identifier: `host-${uuid()}`,
|
|
86
|
+
lockTimeout: 1000,
|
|
87
|
+
disconnectDelay: 1000,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
redisTransactionOrigin = "__hocuspocus__redis__origin__";
|
|
91
|
+
|
|
92
|
+
pub: RedisInstance;
|
|
93
|
+
|
|
94
|
+
sub: RedisInstance;
|
|
95
|
+
|
|
96
|
+
instance!: Hocuspocus;
|
|
97
|
+
|
|
98
|
+
redlock: Redlock;
|
|
99
|
+
|
|
100
|
+
locks = new Map<string, Redlock.Lock>();
|
|
101
|
+
|
|
102
|
+
messagePrefix: Buffer;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* When we have a high frequency of updates to a document we don't need tons of setTimeouts
|
|
106
|
+
* piling up, so we'll track them to keep it to the most recent per document.
|
|
107
|
+
*/
|
|
108
|
+
private pendingDisconnects = new Map<string, NodeJS.Timeout>();
|
|
109
|
+
|
|
110
|
+
private pendingAfterStoreDocumentResolves = new Map<
|
|
111
|
+
string,
|
|
112
|
+
{ timeout: NodeJS.Timeout; resolve: () => void }
|
|
113
|
+
>();
|
|
114
|
+
|
|
115
|
+
public constructor(configuration: Partial<Configuration>) {
|
|
116
|
+
this.configuration = {
|
|
117
|
+
...this.configuration,
|
|
118
|
+
...configuration,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Create Redis instance
|
|
122
|
+
const { port, host, options, nodes, redis, createClient } =
|
|
123
|
+
this.configuration;
|
|
124
|
+
|
|
125
|
+
if (typeof createClient === "function") {
|
|
126
|
+
this.pub = createClient();
|
|
127
|
+
this.sub = createClient();
|
|
128
|
+
} else if (redis) {
|
|
129
|
+
this.pub = redis.duplicate();
|
|
130
|
+
this.sub = redis.duplicate();
|
|
131
|
+
} else if (nodes && nodes.length > 0) {
|
|
132
|
+
this.pub = new RedisClient.Cluster(nodes, options);
|
|
133
|
+
this.sub = new RedisClient.Cluster(nodes, options);
|
|
134
|
+
} else {
|
|
135
|
+
this.pub = new RedisClient(port, host, options ?? {});
|
|
136
|
+
this.sub = new RedisClient(port, host, options ?? {});
|
|
137
|
+
}
|
|
138
|
+
this.sub.on("messageBuffer", this.handleIncomingMessage);
|
|
139
|
+
|
|
140
|
+
this.redlock = new Redlock([this.pub], {
|
|
141
|
+
retryCount: 0,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const identifierBuffer = Buffer.from(
|
|
145
|
+
this.configuration.identifier,
|
|
146
|
+
"utf-8",
|
|
147
|
+
);
|
|
148
|
+
this.messagePrefix = Buffer.concat([
|
|
149
|
+
Buffer.from([identifierBuffer.length]),
|
|
150
|
+
identifierBuffer,
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async onConfigure({ instance }: onConfigurePayload) {
|
|
155
|
+
this.instance = instance;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private getKey(documentName: string) {
|
|
159
|
+
return `${this.configuration.prefix}:${documentName}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private pubKey(documentName: string) {
|
|
163
|
+
return this.getKey(documentName);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private subKey(documentName: string) {
|
|
167
|
+
return this.getKey(documentName);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private lockKey(documentName: string) {
|
|
171
|
+
return `${this.getKey(documentName)}:lock`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private encodeMessage(message: Uint8Array) {
|
|
175
|
+
return Buffer.concat([this.messagePrefix, Buffer.from(message)]);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private decodeMessage(buffer: Buffer) {
|
|
179
|
+
const identifierLength = buffer[0];
|
|
180
|
+
const identifier = buffer.toString("utf-8", 1, identifierLength + 1);
|
|
181
|
+
|
|
182
|
+
return [identifier, buffer.slice(identifierLength + 1)];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Once a document is loaded, subscribe to the channel in Redis.
|
|
187
|
+
*/
|
|
188
|
+
public async afterLoadDocument({
|
|
189
|
+
documentName,
|
|
190
|
+
document,
|
|
191
|
+
}: afterLoadDocumentPayload) {
|
|
192
|
+
return new Promise((resolve, reject) => {
|
|
193
|
+
// On document creation the node will connect to pub and sub channels
|
|
194
|
+
// for the document.
|
|
195
|
+
this.sub.subscribe(this.subKey(documentName), async (error: any) => {
|
|
196
|
+
if (error) {
|
|
197
|
+
reject(error);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
this.publishFirstSyncStep(documentName, document);
|
|
202
|
+
this.requestAwarenessFromOtherInstances(documentName);
|
|
203
|
+
|
|
204
|
+
resolve(undefined);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Publish the first sync step through Redis.
|
|
211
|
+
*/
|
|
212
|
+
private async publishFirstSyncStep(documentName: string, document: Document) {
|
|
213
|
+
const syncMessage = new OutgoingMessage(documentName)
|
|
214
|
+
.createSyncMessage()
|
|
215
|
+
.writeFirstSyncStepFor(document);
|
|
216
|
+
|
|
217
|
+
return this.pub.publishBuffer(
|
|
218
|
+
this.pubKey(documentName),
|
|
219
|
+
this.encodeMessage(syncMessage.toUint8Array()),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Let’s ask Redis who is connected already.
|
|
225
|
+
*/
|
|
226
|
+
private async requestAwarenessFromOtherInstances(documentName: string) {
|
|
227
|
+
const awarenessMessage = new OutgoingMessage(
|
|
228
|
+
documentName,
|
|
229
|
+
).writeQueryAwareness();
|
|
230
|
+
|
|
231
|
+
return this.pub.publishBuffer(
|
|
232
|
+
this.pubKey(documentName),
|
|
233
|
+
this.encodeMessage(awarenessMessage.toUint8Array()),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Before the document is stored, make sure to set a lock in Redis.
|
|
239
|
+
* That’s meant to avoid conflicts with other instances trying to store the document.
|
|
240
|
+
*/
|
|
241
|
+
async onStoreDocument({ documentName }: onStoreDocumentPayload) {
|
|
242
|
+
// Attempt to acquire a lock and read lastReceivedTimestamp from Redis,
|
|
243
|
+
// to avoid conflict with other instances storing the same document.
|
|
244
|
+
|
|
245
|
+
return new Promise((resolve, reject) => {
|
|
246
|
+
this.redlock.lock(
|
|
247
|
+
this.lockKey(documentName),
|
|
248
|
+
this.configuration.lockTimeout,
|
|
249
|
+
async (error, lock) => {
|
|
250
|
+
if (error || !lock) {
|
|
251
|
+
// Expected behavior: Could not acquire lock, another instance locked it already.
|
|
252
|
+
// No further `onStoreDocument` hooks will be executed.
|
|
253
|
+
console.log("unable to acquire lock");
|
|
254
|
+
reject();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.locks.set(this.lockKey(documentName), lock);
|
|
259
|
+
|
|
260
|
+
resolve(undefined);
|
|
261
|
+
},
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Release the Redis lock, so other instances can store documents.
|
|
268
|
+
*/
|
|
269
|
+
async afterStoreDocument({
|
|
270
|
+
documentName,
|
|
271
|
+
socketId,
|
|
272
|
+
}: afterStoreDocumentPayload) {
|
|
273
|
+
this.locks
|
|
274
|
+
.get(this.lockKey(documentName))
|
|
275
|
+
?.unlock()
|
|
276
|
+
.catch(() => {
|
|
277
|
+
// Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.
|
|
278
|
+
// console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)
|
|
279
|
+
})
|
|
280
|
+
.finally(() => {
|
|
281
|
+
this.locks.delete(this.lockKey(documentName));
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.
|
|
285
|
+
// for provider connections, this usually happens in the onDisconnect hook
|
|
286
|
+
if (socketId === "server") {
|
|
287
|
+
const pending = this.pendingAfterStoreDocumentResolves.get(documentName);
|
|
288
|
+
|
|
289
|
+
if (pending) {
|
|
290
|
+
clearTimeout(pending.timeout);
|
|
291
|
+
pending.resolve();
|
|
292
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
let resolveFunction: () => void = () => {};
|
|
296
|
+
const delayedPromise = new Promise<void>((resolve) => {
|
|
297
|
+
resolveFunction = resolve;
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const timeout = setTimeout(() => {
|
|
301
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName);
|
|
302
|
+
resolveFunction();
|
|
303
|
+
}, this.configuration.disconnectDelay);
|
|
304
|
+
|
|
305
|
+
this.pendingAfterStoreDocumentResolves.set(documentName, {
|
|
306
|
+
timeout,
|
|
307
|
+
resolve: resolveFunction,
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
await delayedPromise;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Handle awareness update messages received directly by this Hocuspocus instance.
|
|
316
|
+
*/
|
|
317
|
+
async onAwarenessUpdate({
|
|
318
|
+
documentName,
|
|
319
|
+
awareness,
|
|
320
|
+
added,
|
|
321
|
+
updated,
|
|
322
|
+
removed,
|
|
323
|
+
}: onAwarenessUpdatePayload) {
|
|
324
|
+
const changedClients = added.concat(updated, removed);
|
|
325
|
+
const message = new OutgoingMessage(
|
|
326
|
+
documentName,
|
|
327
|
+
).createAwarenessUpdateMessage(awareness, changedClients);
|
|
328
|
+
|
|
329
|
+
return this.pub.publishBuffer(
|
|
330
|
+
this.pubKey(documentName),
|
|
331
|
+
this.encodeMessage(message.toUint8Array()),
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Handle incoming messages published on subscribed document channels.
|
|
337
|
+
* Note that this will also include messages from ourselves as it is not possible
|
|
338
|
+
* in Redis to filter these.
|
|
339
|
+
*/
|
|
340
|
+
private handleIncomingMessage = async (channel: Buffer, data: Buffer) => {
|
|
341
|
+
const [identifier, messageBuffer] = this.decodeMessage(data);
|
|
342
|
+
|
|
343
|
+
if (identifier === this.configuration.identifier) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const message = new IncomingMessage(messageBuffer);
|
|
348
|
+
const documentName = message.readVarString();
|
|
349
|
+
message.writeVarString(documentName);
|
|
350
|
+
|
|
351
|
+
const document = this.instance.documents.get(documentName);
|
|
352
|
+
|
|
353
|
+
if (!document) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
new MessageReceiver(message, this.redisTransactionOrigin).apply(
|
|
358
|
+
document,
|
|
359
|
+
undefined,
|
|
360
|
+
(reply) => {
|
|
361
|
+
return this.pub.publishBuffer(
|
|
362
|
+
this.pubKey(document.name),
|
|
363
|
+
this.encodeMessage(reply),
|
|
364
|
+
);
|
|
365
|
+
},
|
|
366
|
+
);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* if the ydoc changed, we'll need to inform other Hocuspocus servers about it.
|
|
371
|
+
*/
|
|
372
|
+
public async onChange(data: onChangePayload): Promise<any> {
|
|
373
|
+
if (data.transactionOrigin !== this.redisTransactionOrigin) {
|
|
374
|
+
return this.publishFirstSyncStep(data.documentName, data.document);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Make sure to *not* listen for further changes, when there’s
|
|
380
|
+
* no one connected anymore.
|
|
381
|
+
*/
|
|
382
|
+
public onDisconnect = async ({ documentName }: onDisconnectPayload) => {
|
|
383
|
+
const pending = this.pendingDisconnects.get(documentName);
|
|
384
|
+
|
|
385
|
+
if (pending) {
|
|
386
|
+
clearTimeout(pending);
|
|
387
|
+
this.pendingDisconnects.delete(documentName);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const disconnect = () => {
|
|
391
|
+
const document = this.instance.documents.get(documentName);
|
|
392
|
+
|
|
393
|
+
this.pendingDisconnects.delete(documentName);
|
|
394
|
+
|
|
395
|
+
// Do nothing, when other users are still connected to the document.
|
|
396
|
+
if (!document || document.getConnectionsCount() > 0) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Time to end the subscription on the document channel.
|
|
401
|
+
this.sub.unsubscribe(this.subKey(documentName), (error: any) => {
|
|
402
|
+
if (error) {
|
|
403
|
+
console.error(error);
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
this.instance.unloadDocument(document);
|
|
408
|
+
};
|
|
409
|
+
// Delay the disconnect procedure to allow last minute syncs to happen
|
|
410
|
+
const timeout = setTimeout(disconnect, this.configuration.disconnectDelay);
|
|
411
|
+
this.pendingDisconnects.set(documentName, timeout);
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {
|
|
415
|
+
const message = new OutgoingMessage(
|
|
416
|
+
data.documentName,
|
|
417
|
+
).writeBroadcastStateless(data.payload);
|
|
418
|
+
|
|
419
|
+
return this.pub.publishBuffer(
|
|
420
|
+
this.pubKey(data.documentName),
|
|
421
|
+
this.encodeMessage(message.toUint8Array()),
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Kill the Redlock connection immediately.
|
|
427
|
+
*/
|
|
428
|
+
async onDestroy() {
|
|
429
|
+
await this.redlock.quit();
|
|
430
|
+
this.pub.disconnect(false);
|
|
431
|
+
this.sub.disconnect(false);
|
|
432
|
+
}
|
|
402
433
|
}
|