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