@hocuspocus/extension-redis 2.13.6 → 2.13.7
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.
|
@@ -30,6 +30,12 @@ class Redis {
|
|
|
30
30
|
};
|
|
31
31
|
this.redisTransactionOrigin = '__hocuspocus__redis__origin__';
|
|
32
32
|
this.locks = new Map();
|
|
33
|
+
/**
|
|
34
|
+
* When we have a high frequency of updates to a document we don't need tons of setTimeouts
|
|
35
|
+
* piling up, so we'll track them to keep it to the most recent per document.
|
|
36
|
+
*/
|
|
37
|
+
this.pendingDisconnects = new Map();
|
|
38
|
+
this.pendingAfterStoreDocumentResolves = new Map();
|
|
33
39
|
/**
|
|
34
40
|
* Handle incoming messages published on subscribed document channels.
|
|
35
41
|
* Note that this will also include messages from ourselves as it is not possible
|
|
@@ -58,8 +64,14 @@ class Redis {
|
|
|
58
64
|
* no one connected anymore.
|
|
59
65
|
*/
|
|
60
66
|
this.onDisconnect = async ({ documentName }) => {
|
|
67
|
+
const pending = this.pendingDisconnects.get(documentName);
|
|
68
|
+
if (pending) {
|
|
69
|
+
clearTimeout(pending);
|
|
70
|
+
this.pendingDisconnects.delete(documentName);
|
|
71
|
+
}
|
|
61
72
|
const disconnect = () => {
|
|
62
73
|
const document = this.instance.documents.get(documentName);
|
|
74
|
+
this.pendingDisconnects.delete(documentName);
|
|
63
75
|
// Do nothing, when other users are still connected to the document.
|
|
64
76
|
if (!document || document.getConnectionsCount() > 0) {
|
|
65
77
|
return;
|
|
@@ -73,7 +85,8 @@ class Redis {
|
|
|
73
85
|
this.instance.unloadDocument(document);
|
|
74
86
|
};
|
|
75
87
|
// Delay the disconnect procedure to allow last minute syncs to happen
|
|
76
|
-
setTimeout(disconnect, this.configuration.disconnectDelay);
|
|
88
|
+
const timeout = setTimeout(disconnect, this.configuration.disconnectDelay);
|
|
89
|
+
this.pendingDisconnects.set(documentName, timeout);
|
|
77
90
|
};
|
|
78
91
|
this.configuration = {
|
|
79
92
|
...this.configuration,
|
|
@@ -200,9 +213,22 @@ class Redis {
|
|
|
200
213
|
// if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.
|
|
201
214
|
// for provider connections, this usually happens in the onDisconnect hook
|
|
202
215
|
if (socketId === 'server') {
|
|
203
|
-
|
|
204
|
-
|
|
216
|
+
const pending = this.pendingAfterStoreDocumentResolves.get(documentName);
|
|
217
|
+
if (pending) {
|
|
218
|
+
clearTimeout(pending.timeout);
|
|
219
|
+
pending.resolve();
|
|
220
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName);
|
|
221
|
+
}
|
|
222
|
+
let resolveFunction = () => { };
|
|
223
|
+
const delayedPromise = new Promise(resolve => {
|
|
224
|
+
resolveFunction = resolve;
|
|
205
225
|
});
|
|
226
|
+
const timeout = setTimeout(() => {
|
|
227
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName);
|
|
228
|
+
resolveFunction();
|
|
229
|
+
}, this.configuration.disconnectDelay);
|
|
230
|
+
this.pendingAfterStoreDocumentResolves.set(documentName, { timeout, resolve: resolveFunction });
|
|
231
|
+
await delayedPromise;
|
|
206
232
|
}
|
|
207
233
|
}
|
|
208
234
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hocuspocus-redis.cjs","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient, { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'\nimport Redlock from 'redlock'\nimport { v4 as uuid } from 'uuid'\nimport {\n IncomingMessage,\n OutgoingMessage,\n Document,\n Extension,\n afterLoadDocumentPayload,\n afterStoreDocumentPayload,\n onDisconnectPayload,\n onStoreDocumentPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n MessageReceiver,\n Debugger,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus,\n} from '@hocuspocus/server'\n\nexport type RedisInstance = RedisClient.Cluster | RedisClient.Redis\n\nexport interface Configuration {\n /**\n * Redis port\n */\n port: number,\n /**\n * Redis host\n */\n host: string,\n /**\n * Redis Cluster\n */\n nodes?: ClusterNode[],\n /**\n * Duplicate from an existed Redis instance\n */\n redis?: RedisInstance,\n /**\n * Redis instance creator\n */\n createClient?: () => RedisInstance,\n /**\n * Options passed directly to Redis constructor\n *\n * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options\n */\n options?: ClusterOptions | RedisOptions,\n /**\n * An unique instance name, required to filter messages in Redis.\n * If none is provided an unique id is generated.\n */\n identifier: string,\n /**\n * Namespace for Redis keys, if none is provided 'hocuspocus' is used\n */\n prefix: string,\n /**\n * The maximum time for the Redis lock in ms (in case it can’t be released).\n */\n lockTimeout: number,\n /**\n * A delay before onDisconnect is executed. This allows last minute updates'\n * sync messages to be received by the subscription before it's closed.\n */\n disconnectDelay: number,\n}\n\nexport class Redis implements Extension {\n /**\n * Make sure to give that extension a higher priority, so\n * the `onStoreDocument` hook is able to intercept the chain,\n * before documents are stored to the database.\n */\n priority = 1000\n\n configuration: Configuration = {\n port: 6379,\n host: '127.0.0.1',\n prefix: 'hocuspocus',\n identifier: `host-${uuid()}`,\n lockTimeout: 1000,\n disconnectDelay: 1000,\n }\n\n redisTransactionOrigin = '__hocuspocus__redis__origin__'\n\n pub: RedisInstance\n\n sub: RedisInstance\n\n instance!: Hocuspocus\n\n redlock: Redlock\n\n locks = new Map<string, Redlock.Lock>()\n\n logger: Debugger\n\n messagePrefix: Buffer\n\n public constructor(configuration: Partial<Configuration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n\n // We’ll replace that in the onConfigure hook with the global instance.\n this.logger = new Debugger()\n\n // Create Redis instance\n const {\n port,\n host,\n options,\n nodes,\n redis,\n createClient,\n } = this.configuration\n\n if (typeof createClient === 'function') {\n this.pub = createClient()\n this.sub = createClient()\n } else if (redis) {\n this.pub = redis.duplicate()\n this.sub = redis.duplicate()\n } else if (nodes && nodes.length > 0) {\n this.pub = new RedisClient.Cluster(nodes, options)\n this.sub = new RedisClient.Cluster(nodes, options)\n } else {\n this.pub = new RedisClient(port, host, options)\n this.sub = new RedisClient(port, host, options)\n }\n this.sub.on('messageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub], {\n retryCount: 0,\n })\n\n const identifierBuffer = Buffer.from(this.configuration.identifier, 'utf-8')\n this.messagePrefix = Buffer.concat([Buffer.from([identifierBuffer.length]), identifierBuffer])\n }\n\n async onConfigure({ instance }: onConfigurePayload) {\n this.logger = instance.debugger\n this.instance = instance\n }\n\n private getKey(documentName: string) {\n return `${this.configuration.prefix}:${documentName}`\n }\n\n private pubKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private subKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private lockKey(documentName: string) {\n return `${this.getKey(documentName)}:lock`\n }\n\n private encodeMessage(message: Uint8Array) {\n return Buffer.concat([this.messagePrefix, Buffer.from(message)])\n }\n\n private decodeMessage(buffer: Buffer) {\n const identifierLength = buffer[0]\n const identifier = buffer.toString('utf-8', 1, identifierLength + 1)\n\n return [identifier, buffer.slice(identifierLength + 1)]\n }\n\n /**\n * Once a document is loaded, subscribe to the channel in Redis.\n */\n public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {\n return new Promise((resolve, reject) => {\n // On document creation the node will connect to pub and sub channels\n // for the document.\n this.sub.subscribe(this.subKey(documentName), async error => {\n if (error) {\n reject(error)\n return\n }\n\n this.publishFirstSyncStep(documentName, document)\n this.requestAwarenessFromOtherInstances(documentName)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Publish the first sync step through Redis.\n */\n private async publishFirstSyncStep(documentName: string, document: Document) {\n const syncMessage = new OutgoingMessage(documentName)\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publishBuffer(this.pubKey(documentName), this.encodeMessage(syncMessage.toUint8Array()))\n }\n\n /**\n * Let’s ask Redis who is connected already.\n */\n private async requestAwarenessFromOtherInstances(documentName: string) {\n const awarenessMessage = new OutgoingMessage(documentName)\n .writeQueryAwareness()\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(awarenessMessage.toUint8Array()),\n )\n }\n\n /**\n * Before the document is stored, make sure to set a lock in Redis.\n * That’s meant to avoid conflicts with other instances trying to store the document.\n */\n async onStoreDocument({ documentName }: onStoreDocumentPayload) {\n // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,\n // to avoid conflict with other instances storing the same document.\n\n return new Promise((resolve, reject) => {\n this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {\n if (error || !lock) {\n // Expected behavior: Could not acquire lock, another instance locked it already.\n // No further `onStoreDocument` hooks will be executed.\n console.log('unable to acquire lock')\n reject()\n return\n }\n\n this.locks.set(this.lockKey(documentName), lock)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Release the Redis lock, so other instances can store documents.\n */\n async afterStoreDocument({ documentName, socketId }: afterStoreDocumentPayload) {\n this.locks.get(this.lockKey(documentName))?.unlock()\n .catch(() => {\n // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.\n // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)\n })\n .finally(() => {\n this.locks.delete(this.lockKey(documentName))\n })\n\n // if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.\n // for provider connections, this usually happens in the onDisconnect hook\n if (socketId === 'server') {\n await new Promise(resolve => {\n setTimeout(() => resolve(''), this.configuration.disconnectDelay)\n })\n }\n }\n\n /**\n * Handle awareness update messages received directly by this Hocuspocus instance.\n */\n async onAwarenessUpdate({\n documentName, awareness, added, updated, removed,\n }: onAwarenessUpdatePayload) {\n const changedClients = added.concat(updated, removed)\n const message = new OutgoingMessage(documentName)\n .createAwarenessUpdateMessage(awareness, changedClients)\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Handle incoming messages published on subscribed document channels.\n * Note that this will also include messages from ourselves as it is not possible\n * in Redis to filter these.\n */\n private handleIncomingMessage = async (channel: Buffer, data: Buffer) => {\n const [identifier, messageBuffer] = this.decodeMessage(data)\n\n if (identifier === this.configuration.identifier) {\n return\n }\n\n const message = new IncomingMessage(messageBuffer)\n const documentName = message.readVarString()\n message.writeVarString(documentName)\n\n const document = this.instance.documents.get(documentName)\n\n if (!document) {\n // What does this mean? Why are we subscribed to this document?\n this.logger.log(`Received message for unknown document ${documentName}`)\n return\n }\n\n new MessageReceiver(\n message,\n this.logger,\n this.redisTransactionOrigin,\n ).apply(document, undefined, reply => {\n return this.pub.publishBuffer(\n this.pubKey(document.name),\n this.encodeMessage(reply),\n )\n })\n }\n\n /**\n * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.\n */\n public async onChange(data: onChangePayload): Promise<any> {\n if (data.transactionOrigin !== this.redisTransactionOrigin) {\n return this.publishFirstSyncStep(data.documentName, data.document)\n }\n }\n\n /**\n * Make sure to *not* listen for further changes, when there’s\n * no one connected anymore.\n */\n public onDisconnect = async ({ documentName }: onDisconnectPayload) => {\n const disconnect = () => {\n const document = this.instance.documents.get(documentName)\n\n // Do nothing, when other users are still connected to the document.\n if (!document || document.getConnectionsCount() > 0) {\n return\n }\n\n // Time to end the subscription on the document channel.\n this.sub.unsubscribe(this.subKey(documentName), (error: any) => {\n if (error) {\n console.error(error)\n }\n })\n\n this.instance.unloadDocument(document)\n }\n // Delay the disconnect procedure to allow last minute syncs to happen\n setTimeout(disconnect, this.configuration.disconnectDelay)\n }\n\n async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {\n const message = new OutgoingMessage(data.documentName)\n .writeBroadcastStateless(data.payload)\n\n return this.pub.publishBuffer(\n this.pubKey(data.documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Kill the Redlock connection immediately.\n */\n async onDestroy() {\n await this.redlock.quit()\n this.pub.disconnect(false)\n this.sub.disconnect(false)\n }\n}\n"],"names":["uuid","IncomingMessage","MessageReceiver","Debugger","RedisClient","Redlock","OutgoingMessage"],"mappings":";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I,CAAC,aAAa,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAC3C,CAAA;KACF;AAED;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;AACzB,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;AAC1B,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;KAC3B;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"hocuspocus-redis.cjs","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient, { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'\nimport Redlock from 'redlock'\nimport { v4 as uuid } from 'uuid'\nimport {\n IncomingMessage,\n OutgoingMessage,\n Document,\n Extension,\n afterLoadDocumentPayload,\n afterStoreDocumentPayload,\n onDisconnectPayload,\n onStoreDocumentPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n MessageReceiver,\n Debugger,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus,\n} from '@hocuspocus/server'\n\nexport type RedisInstance = RedisClient.Cluster | RedisClient.Redis\n\nexport interface Configuration {\n /**\n * Redis port\n */\n port: number,\n /**\n * Redis host\n */\n host: string,\n /**\n * Redis Cluster\n */\n nodes?: ClusterNode[],\n /**\n * Duplicate from an existed Redis instance\n */\n redis?: RedisInstance,\n /**\n * Redis instance creator\n */\n createClient?: () => RedisInstance,\n /**\n * Options passed directly to Redis constructor\n *\n * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options\n */\n options?: ClusterOptions | RedisOptions,\n /**\n * An unique instance name, required to filter messages in Redis.\n * If none is provided an unique id is generated.\n */\n identifier: string,\n /**\n * Namespace for Redis keys, if none is provided 'hocuspocus' is used\n */\n prefix: string,\n /**\n * The maximum time for the Redis lock in ms (in case it can’t be released).\n */\n lockTimeout: number,\n /**\n * A delay before onDisconnect is executed. This allows last minute updates'\n * sync messages to be received by the subscription before it's closed.\n */\n disconnectDelay: number,\n}\n\nexport class Redis implements Extension {\n /**\n * Make sure to give that extension a higher priority, so\n * the `onStoreDocument` hook is able to intercept the chain,\n * before documents are stored to the database.\n */\n priority = 1000\n\n configuration: Configuration = {\n port: 6379,\n host: '127.0.0.1',\n prefix: 'hocuspocus',\n identifier: `host-${uuid()}`,\n lockTimeout: 1000,\n disconnectDelay: 1000,\n }\n\n redisTransactionOrigin = '__hocuspocus__redis__origin__'\n\n pub: RedisInstance\n\n sub: RedisInstance\n\n instance!: Hocuspocus\n\n redlock: Redlock\n\n locks = new Map<string, Redlock.Lock>()\n\n logger: Debugger\n\n messagePrefix: Buffer\n\n /**\n * When we have a high frequency of updates to a document we don't need tons of setTimeouts\n * piling up, so we'll track them to keep it to the most recent per document.\n */\n private pendingDisconnects = new Map<string, NodeJS.Timeout>()\n\n private pendingAfterStoreDocumentResolves = new Map<string, { timeout: NodeJS.Timeout; resolve:() => void }>()\n\n public constructor(configuration: Partial<Configuration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n\n // We’ll replace that in the onConfigure hook with the global instance.\n this.logger = new Debugger()\n\n // Create Redis instance\n const {\n port,\n host,\n options,\n nodes,\n redis,\n createClient,\n } = this.configuration\n\n if (typeof createClient === 'function') {\n this.pub = createClient()\n this.sub = createClient()\n } else if (redis) {\n this.pub = redis.duplicate()\n this.sub = redis.duplicate()\n } else if (nodes && nodes.length > 0) {\n this.pub = new RedisClient.Cluster(nodes, options)\n this.sub = new RedisClient.Cluster(nodes, options)\n } else {\n this.pub = new RedisClient(port, host, options)\n this.sub = new RedisClient(port, host, options)\n }\n this.sub.on('messageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub], {\n retryCount: 0,\n })\n\n const identifierBuffer = Buffer.from(this.configuration.identifier, 'utf-8')\n this.messagePrefix = Buffer.concat([Buffer.from([identifierBuffer.length]), identifierBuffer])\n }\n\n async onConfigure({ instance }: onConfigurePayload) {\n this.logger = instance.debugger\n this.instance = instance\n }\n\n private getKey(documentName: string) {\n return `${this.configuration.prefix}:${documentName}`\n }\n\n private pubKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private subKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private lockKey(documentName: string) {\n return `${this.getKey(documentName)}:lock`\n }\n\n private encodeMessage(message: Uint8Array) {\n return Buffer.concat([this.messagePrefix, Buffer.from(message)])\n }\n\n private decodeMessage(buffer: Buffer) {\n const identifierLength = buffer[0]\n const identifier = buffer.toString('utf-8', 1, identifierLength + 1)\n\n return [identifier, buffer.slice(identifierLength + 1)]\n }\n\n /**\n * Once a document is loaded, subscribe to the channel in Redis.\n */\n public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {\n return new Promise((resolve, reject) => {\n // On document creation the node will connect to pub and sub channels\n // for the document.\n this.sub.subscribe(this.subKey(documentName), async error => {\n if (error) {\n reject(error)\n return\n }\n\n this.publishFirstSyncStep(documentName, document)\n this.requestAwarenessFromOtherInstances(documentName)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Publish the first sync step through Redis.\n */\n private async publishFirstSyncStep(documentName: string, document: Document) {\n const syncMessage = new OutgoingMessage(documentName)\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publishBuffer(this.pubKey(documentName), this.encodeMessage(syncMessage.toUint8Array()))\n }\n\n /**\n * Let’s ask Redis who is connected already.\n */\n private async requestAwarenessFromOtherInstances(documentName: string) {\n const awarenessMessage = new OutgoingMessage(documentName)\n .writeQueryAwareness()\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(awarenessMessage.toUint8Array()),\n )\n }\n\n /**\n * Before the document is stored, make sure to set a lock in Redis.\n * That’s meant to avoid conflicts with other instances trying to store the document.\n */\n async onStoreDocument({ documentName }: onStoreDocumentPayload) {\n // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,\n // to avoid conflict with other instances storing the same document.\n\n return new Promise((resolve, reject) => {\n this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {\n if (error || !lock) {\n // Expected behavior: Could not acquire lock, another instance locked it already.\n // No further `onStoreDocument` hooks will be executed.\n console.log('unable to acquire lock')\n reject()\n return\n }\n\n this.locks.set(this.lockKey(documentName), lock)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Release the Redis lock, so other instances can store documents.\n */\n async afterStoreDocument({ documentName, socketId }: afterStoreDocumentPayload) {\n this.locks.get(this.lockKey(documentName))?.unlock()\n .catch(() => {\n // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.\n // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)\n })\n .finally(() => {\n this.locks.delete(this.lockKey(documentName))\n })\n\n // if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.\n // for provider connections, this usually happens in the onDisconnect hook\n if (socketId === 'server') {\n const pending = this.pendingAfterStoreDocumentResolves.get(documentName)\n\n if (pending) {\n clearTimeout(pending.timeout)\n pending.resolve()\n this.pendingAfterStoreDocumentResolves.delete(documentName)\n }\n\n let resolveFunction: () => void = () => {}\n const delayedPromise = new Promise<void>(resolve => {\n resolveFunction = resolve\n })\n\n const timeout = setTimeout(() => {\n this.pendingAfterStoreDocumentResolves.delete(documentName)\n resolveFunction()\n }, this.configuration.disconnectDelay)\n\n this.pendingAfterStoreDocumentResolves.set(documentName, { timeout, resolve: resolveFunction })\n\n await delayedPromise\n }\n }\n\n /**\n * Handle awareness update messages received directly by this Hocuspocus instance.\n */\n async onAwarenessUpdate({\n documentName, awareness, added, updated, removed,\n }: onAwarenessUpdatePayload) {\n const changedClients = added.concat(updated, removed)\n const message = new OutgoingMessage(documentName)\n .createAwarenessUpdateMessage(awareness, changedClients)\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Handle incoming messages published on subscribed document channels.\n * Note that this will also include messages from ourselves as it is not possible\n * in Redis to filter these.\n */\n private handleIncomingMessage = async (channel: Buffer, data: Buffer) => {\n const [identifier, messageBuffer] = this.decodeMessage(data)\n\n if (identifier === this.configuration.identifier) {\n return\n }\n\n const message = new IncomingMessage(messageBuffer)\n const documentName = message.readVarString()\n message.writeVarString(documentName)\n\n const document = this.instance.documents.get(documentName)\n\n if (!document) {\n // What does this mean? Why are we subscribed to this document?\n this.logger.log(`Received message for unknown document ${documentName}`)\n return\n }\n\n new MessageReceiver(\n message,\n this.logger,\n this.redisTransactionOrigin,\n ).apply(document, undefined, reply => {\n return this.pub.publishBuffer(\n this.pubKey(document.name),\n this.encodeMessage(reply),\n )\n })\n }\n\n /**\n * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.\n */\n public async onChange(data: onChangePayload): Promise<any> {\n if (data.transactionOrigin !== this.redisTransactionOrigin) {\n return this.publishFirstSyncStep(data.documentName, data.document)\n }\n }\n\n /**\n * Make sure to *not* listen for further changes, when there’s\n * no one connected anymore.\n */\n public onDisconnect = async ({ documentName }: onDisconnectPayload) => {\n const pending = this.pendingDisconnects.get(documentName)\n\n if (pending) {\n clearTimeout(pending)\n this.pendingDisconnects.delete(documentName)\n }\n\n const disconnect = () => {\n const document = this.instance.documents.get(documentName)\n\n this.pendingDisconnects.delete(documentName)\n\n // Do nothing, when other users are still connected to the document.\n if (!document || document.getConnectionsCount() > 0) {\n return\n }\n\n // Time to end the subscription on the document channel.\n this.sub.unsubscribe(this.subKey(documentName), (error: any) => {\n if (error) {\n console.error(error)\n }\n })\n\n this.instance.unloadDocument(document)\n }\n // Delay the disconnect procedure to allow last minute syncs to happen\n const timeout = setTimeout(disconnect, this.configuration.disconnectDelay)\n this.pendingDisconnects.set(documentName, timeout)\n }\n\n async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {\n const message = new OutgoingMessage(data.documentName)\n .writeBroadcastStateless(data.payload)\n\n return this.pub.publishBuffer(\n this.pubKey(data.documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Kill the Redlock connection immediately.\n */\n async onDestroy() {\n await this.redlock.quit()\n this.pub.disconnect(false)\n this.sub.disconnect(false)\n }\n}\n"],"names":["uuid","IncomingMessage","MessageReceiver","Debugger","RedisClient","Redlock","OutgoingMessage"],"mappings":";;;;;;;;;;;;;;MAqEa,KAAK,CAAA;AAyChB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AAxCxD;;;;AAIG;QACH,IAAQ,CAAA,QAAA,GAAG,IAAI,CAAA;AAEf,QAAA,IAAA,CAAA,aAAa,GAAkB;AAC7B,YAAA,IAAI,EAAE,IAAI;AACV,YAAA,IAAI,EAAE,WAAW;AACjB,YAAA,MAAM,EAAE,YAAY;AACpB,YAAA,UAAU,EAAE,CAAA,KAAA,EAAQA,OAAI,EAAE,CAAE,CAAA;AAC5B,YAAA,WAAW,EAAE,IAAI;AACjB,YAAA,eAAe,EAAE,IAAI;SACtB,CAAA;QAED,IAAsB,CAAA,sBAAA,GAAG,+BAA+B,CAAA;AAUxD,QAAA,IAAA,CAAA,KAAK,GAAG,IAAI,GAAG,EAAwB,CAAA;AAMvC;;;AAGG;AACK,QAAA,IAAA,CAAA,kBAAkB,GAAG,IAAI,GAAG,EAA0B,CAAA;AAEtD,QAAA,IAAA,CAAA,iCAAiC,GAAG,IAAI,GAAG,EAA2D,CAAA;AA0M9G;;;;AAIE;AACM,QAAA,IAAA,CAAA,qBAAqB,GAAG,OAAO,OAAe,EAAE,IAAY,KAAI;AACtE,YAAA,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;AAE5D,YAAA,IAAI,UAAU,KAAK,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;gBAChD,OAAM;AACP,aAAA;AAED,YAAA,MAAM,OAAO,GAAG,IAAIC,sBAAe,CAAC,aAAa,CAAC,CAAA;AAClD,YAAA,MAAM,YAAY,GAAG,OAAO,CAAC,aAAa,EAAE,CAAA;AAC5C,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C,CAAA;QAExC,OAAO,IAAI,CAAC,GAAG,CAAC,aAAa,CAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAC9B,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAC3C,CAAA;KACF;AAED;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;AACzB,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;AAC1B,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;KAC3B;AACF;;;;"}
|
|
@@ -21,6 +21,12 @@ class Redis {
|
|
|
21
21
|
};
|
|
22
22
|
this.redisTransactionOrigin = '__hocuspocus__redis__origin__';
|
|
23
23
|
this.locks = new Map();
|
|
24
|
+
/**
|
|
25
|
+
* When we have a high frequency of updates to a document we don't need tons of setTimeouts
|
|
26
|
+
* piling up, so we'll track them to keep it to the most recent per document.
|
|
27
|
+
*/
|
|
28
|
+
this.pendingDisconnects = new Map();
|
|
29
|
+
this.pendingAfterStoreDocumentResolves = new Map();
|
|
24
30
|
/**
|
|
25
31
|
* Handle incoming messages published on subscribed document channels.
|
|
26
32
|
* Note that this will also include messages from ourselves as it is not possible
|
|
@@ -49,8 +55,14 @@ class Redis {
|
|
|
49
55
|
* no one connected anymore.
|
|
50
56
|
*/
|
|
51
57
|
this.onDisconnect = async ({ documentName }) => {
|
|
58
|
+
const pending = this.pendingDisconnects.get(documentName);
|
|
59
|
+
if (pending) {
|
|
60
|
+
clearTimeout(pending);
|
|
61
|
+
this.pendingDisconnects.delete(documentName);
|
|
62
|
+
}
|
|
52
63
|
const disconnect = () => {
|
|
53
64
|
const document = this.instance.documents.get(documentName);
|
|
65
|
+
this.pendingDisconnects.delete(documentName);
|
|
54
66
|
// Do nothing, when other users are still connected to the document.
|
|
55
67
|
if (!document || document.getConnectionsCount() > 0) {
|
|
56
68
|
return;
|
|
@@ -64,7 +76,8 @@ class Redis {
|
|
|
64
76
|
this.instance.unloadDocument(document);
|
|
65
77
|
};
|
|
66
78
|
// Delay the disconnect procedure to allow last minute syncs to happen
|
|
67
|
-
setTimeout(disconnect, this.configuration.disconnectDelay);
|
|
79
|
+
const timeout = setTimeout(disconnect, this.configuration.disconnectDelay);
|
|
80
|
+
this.pendingDisconnects.set(documentName, timeout);
|
|
68
81
|
};
|
|
69
82
|
this.configuration = {
|
|
70
83
|
...this.configuration,
|
|
@@ -191,9 +204,22 @@ class Redis {
|
|
|
191
204
|
// if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.
|
|
192
205
|
// for provider connections, this usually happens in the onDisconnect hook
|
|
193
206
|
if (socketId === 'server') {
|
|
194
|
-
|
|
195
|
-
|
|
207
|
+
const pending = this.pendingAfterStoreDocumentResolves.get(documentName);
|
|
208
|
+
if (pending) {
|
|
209
|
+
clearTimeout(pending.timeout);
|
|
210
|
+
pending.resolve();
|
|
211
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName);
|
|
212
|
+
}
|
|
213
|
+
let resolveFunction = () => { };
|
|
214
|
+
const delayedPromise = new Promise(resolve => {
|
|
215
|
+
resolveFunction = resolve;
|
|
196
216
|
});
|
|
217
|
+
const timeout = setTimeout(() => {
|
|
218
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName);
|
|
219
|
+
resolveFunction();
|
|
220
|
+
}, this.configuration.disconnectDelay);
|
|
221
|
+
this.pendingAfterStoreDocumentResolves.set(documentName, { timeout, resolve: resolveFunction });
|
|
222
|
+
await delayedPromise;
|
|
197
223
|
}
|
|
198
224
|
}
|
|
199
225
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hocuspocus-redis.esm.js","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient, { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'\nimport Redlock from 'redlock'\nimport { v4 as uuid } from 'uuid'\nimport {\n IncomingMessage,\n OutgoingMessage,\n Document,\n Extension,\n afterLoadDocumentPayload,\n afterStoreDocumentPayload,\n onDisconnectPayload,\n onStoreDocumentPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n MessageReceiver,\n Debugger,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus,\n} from '@hocuspocus/server'\n\nexport type RedisInstance = RedisClient.Cluster | RedisClient.Redis\n\nexport interface Configuration {\n /**\n * Redis port\n */\n port: number,\n /**\n * Redis host\n */\n host: string,\n /**\n * Redis Cluster\n */\n nodes?: ClusterNode[],\n /**\n * Duplicate from an existed Redis instance\n */\n redis?: RedisInstance,\n /**\n * Redis instance creator\n */\n createClient?: () => RedisInstance,\n /**\n * Options passed directly to Redis constructor\n *\n * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options\n */\n options?: ClusterOptions | RedisOptions,\n /**\n * An unique instance name, required to filter messages in Redis.\n * If none is provided an unique id is generated.\n */\n identifier: string,\n /**\n * Namespace for Redis keys, if none is provided 'hocuspocus' is used\n */\n prefix: string,\n /**\n * The maximum time for the Redis lock in ms (in case it can’t be released).\n */\n lockTimeout: number,\n /**\n * A delay before onDisconnect is executed. This allows last minute updates'\n * sync messages to be received by the subscription before it's closed.\n */\n disconnectDelay: number,\n}\n\nexport class Redis implements Extension {\n /**\n * Make sure to give that extension a higher priority, so\n * the `onStoreDocument` hook is able to intercept the chain,\n * before documents are stored to the database.\n */\n priority = 1000\n\n configuration: Configuration = {\n port: 6379,\n host: '127.0.0.1',\n prefix: 'hocuspocus',\n identifier: `host-${uuid()}`,\n lockTimeout: 1000,\n disconnectDelay: 1000,\n }\n\n redisTransactionOrigin = '__hocuspocus__redis__origin__'\n\n pub: RedisInstance\n\n sub: RedisInstance\n\n instance!: Hocuspocus\n\n redlock: Redlock\n\n locks = new Map<string, Redlock.Lock>()\n\n logger: Debugger\n\n messagePrefix: Buffer\n\n public constructor(configuration: Partial<Configuration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n\n // We’ll replace that in the onConfigure hook with the global instance.\n this.logger = new Debugger()\n\n // Create Redis instance\n const {\n port,\n host,\n options,\n nodes,\n redis,\n createClient,\n } = this.configuration\n\n if (typeof createClient === 'function') {\n this.pub = createClient()\n this.sub = createClient()\n } else if (redis) {\n this.pub = redis.duplicate()\n this.sub = redis.duplicate()\n } else if (nodes && nodes.length > 0) {\n this.pub = new RedisClient.Cluster(nodes, options)\n this.sub = new RedisClient.Cluster(nodes, options)\n } else {\n this.pub = new RedisClient(port, host, options)\n this.sub = new RedisClient(port, host, options)\n }\n this.sub.on('messageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub], {\n retryCount: 0,\n })\n\n const identifierBuffer = Buffer.from(this.configuration.identifier, 'utf-8')\n this.messagePrefix = Buffer.concat([Buffer.from([identifierBuffer.length]), identifierBuffer])\n }\n\n async onConfigure({ instance }: onConfigurePayload) {\n this.logger = instance.debugger\n this.instance = instance\n }\n\n private getKey(documentName: string) {\n return `${this.configuration.prefix}:${documentName}`\n }\n\n private pubKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private subKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private lockKey(documentName: string) {\n return `${this.getKey(documentName)}:lock`\n }\n\n private encodeMessage(message: Uint8Array) {\n return Buffer.concat([this.messagePrefix, Buffer.from(message)])\n }\n\n private decodeMessage(buffer: Buffer) {\n const identifierLength = buffer[0]\n const identifier = buffer.toString('utf-8', 1, identifierLength + 1)\n\n return [identifier, buffer.slice(identifierLength + 1)]\n }\n\n /**\n * Once a document is loaded, subscribe to the channel in Redis.\n */\n public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {\n return new Promise((resolve, reject) => {\n // On document creation the node will connect to pub and sub channels\n // for the document.\n this.sub.subscribe(this.subKey(documentName), async error => {\n if (error) {\n reject(error)\n return\n }\n\n this.publishFirstSyncStep(documentName, document)\n this.requestAwarenessFromOtherInstances(documentName)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Publish the first sync step through Redis.\n */\n private async publishFirstSyncStep(documentName: string, document: Document) {\n const syncMessage = new OutgoingMessage(documentName)\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publishBuffer(this.pubKey(documentName), this.encodeMessage(syncMessage.toUint8Array()))\n }\n\n /**\n * Let’s ask Redis who is connected already.\n */\n private async requestAwarenessFromOtherInstances(documentName: string) {\n const awarenessMessage = new OutgoingMessage(documentName)\n .writeQueryAwareness()\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(awarenessMessage.toUint8Array()),\n )\n }\n\n /**\n * Before the document is stored, make sure to set a lock in Redis.\n * That’s meant to avoid conflicts with other instances trying to store the document.\n */\n async onStoreDocument({ documentName }: onStoreDocumentPayload) {\n // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,\n // to avoid conflict with other instances storing the same document.\n\n return new Promise((resolve, reject) => {\n this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {\n if (error || !lock) {\n // Expected behavior: Could not acquire lock, another instance locked it already.\n // No further `onStoreDocument` hooks will be executed.\n console.log('unable to acquire lock')\n reject()\n return\n }\n\n this.locks.set(this.lockKey(documentName), lock)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Release the Redis lock, so other instances can store documents.\n */\n async afterStoreDocument({ documentName, socketId }: afterStoreDocumentPayload) {\n this.locks.get(this.lockKey(documentName))?.unlock()\n .catch(() => {\n // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.\n // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)\n })\n .finally(() => {\n this.locks.delete(this.lockKey(documentName))\n })\n\n // if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.\n // for provider connections, this usually happens in the onDisconnect hook\n if (socketId === 'server') {\n await new Promise(resolve => {\n setTimeout(() => resolve(''), this.configuration.disconnectDelay)\n })\n }\n }\n\n /**\n * Handle awareness update messages received directly by this Hocuspocus instance.\n */\n async onAwarenessUpdate({\n documentName, awareness, added, updated, removed,\n }: onAwarenessUpdatePayload) {\n const changedClients = added.concat(updated, removed)\n const message = new OutgoingMessage(documentName)\n .createAwarenessUpdateMessage(awareness, changedClients)\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Handle incoming messages published on subscribed document channels.\n * Note that this will also include messages from ourselves as it is not possible\n * in Redis to filter these.\n */\n private handleIncomingMessage = async (channel: Buffer, data: Buffer) => {\n const [identifier, messageBuffer] = this.decodeMessage(data)\n\n if (identifier === this.configuration.identifier) {\n return\n }\n\n const message = new IncomingMessage(messageBuffer)\n const documentName = message.readVarString()\n message.writeVarString(documentName)\n\n const document = this.instance.documents.get(documentName)\n\n if (!document) {\n // What does this mean? Why are we subscribed to this document?\n this.logger.log(`Received message for unknown document ${documentName}`)\n return\n }\n\n new MessageReceiver(\n message,\n this.logger,\n this.redisTransactionOrigin,\n ).apply(document, undefined, reply => {\n return this.pub.publishBuffer(\n this.pubKey(document.name),\n this.encodeMessage(reply),\n )\n })\n }\n\n /**\n * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.\n */\n public async onChange(data: onChangePayload): Promise<any> {\n if (data.transactionOrigin !== this.redisTransactionOrigin) {\n return this.publishFirstSyncStep(data.documentName, data.document)\n }\n }\n\n /**\n * Make sure to *not* listen for further changes, when there’s\n * no one connected anymore.\n */\n public onDisconnect = async ({ documentName }: onDisconnectPayload) => {\n const disconnect = () => {\n const document = this.instance.documents.get(documentName)\n\n // Do nothing, when other users are still connected to the document.\n if (!document || document.getConnectionsCount() > 0) {\n return\n }\n\n // Time to end the subscription on the document channel.\n this.sub.unsubscribe(this.subKey(documentName), (error: any) => {\n if (error) {\n console.error(error)\n }\n })\n\n this.instance.unloadDocument(document)\n }\n // Delay the disconnect procedure to allow last minute syncs to happen\n setTimeout(disconnect, this.configuration.disconnectDelay)\n }\n\n async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {\n const message = new OutgoingMessage(data.documentName)\n .writeBroadcastStateless(data.payload)\n\n return this.pub.publishBuffer(\n this.pubKey(data.documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Kill the Redlock connection immediately.\n */\n async onDestroy() {\n await this.redlock.quit()\n this.pub.disconnect(false)\n this.sub.disconnect(false)\n }\n}\n"],"names":["uuid"],"mappings":";;;;;MAqEa,KAAK,CAAA;AAiChB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AAhCxD;;;;AAIG;QACH,IAAQ,CAAA,QAAA,GAAG,IAAI,CAAA;AAEf,QAAA,IAAA,CAAA,aAAa,GAAkB;AAC7B,YAAA,IAAI,EAAE,IAAI;AACV,YAAA,IAAI,EAAE,WAAW;AACjB,YAAA,MAAM,EAAE,YAAY;AACpB,YAAA,UAAU,EAAE,CAAA,KAAA,EAAQA,EAAI,EAAE,CAAE,CAAA;AAC5B,YAAA,WAAW,EAAE,IAAI;AACjB,YAAA,eAAe,EAAE,IAAI;SACtB,CAAA;QAED,IAAsB,CAAA,sBAAA,GAAG,+BAA+B,CAAA;AAUxD,QAAA,IAAA,CAAA,KAAK,GAAG,IAAI,GAAG,EAAwB,CAAA;AA4LvC;;;;AAIE;AACM,QAAA,IAAA,CAAA,qBAAqB,GAAG,OAAO,OAAe,EAAE,IAAY,KAAI;AACtE,YAAA,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;AAE5D,YAAA,IAAI,UAAU,KAAK,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;gBAChD,OAAM;AACP,aAAA;AAED,YAAA,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAA;AAClD,YAAA,MAAM,YAAY,GAAG,OAAO,CAAC,aAAa,EAAE,CAAA;AAC5C,YAAA,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC,CAAA;AA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EAAA;QAClE,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC;AACnD,aAAA,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAExC,OAAO,IAAI,CAAC,GAAG,CAAC,aAAa,CAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAC9B,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAC3C,CAAA;KACF;AAED;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;AACzB,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;AAC1B,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;KAC3B;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"hocuspocus-redis.esm.js","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient, { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'\nimport Redlock from 'redlock'\nimport { v4 as uuid } from 'uuid'\nimport {\n IncomingMessage,\n OutgoingMessage,\n Document,\n Extension,\n afterLoadDocumentPayload,\n afterStoreDocumentPayload,\n onDisconnectPayload,\n onStoreDocumentPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n MessageReceiver,\n Debugger,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus,\n} from '@hocuspocus/server'\n\nexport type RedisInstance = RedisClient.Cluster | RedisClient.Redis\n\nexport interface Configuration {\n /**\n * Redis port\n */\n port: number,\n /**\n * Redis host\n */\n host: string,\n /**\n * Redis Cluster\n */\n nodes?: ClusterNode[],\n /**\n * Duplicate from an existed Redis instance\n */\n redis?: RedisInstance,\n /**\n * Redis instance creator\n */\n createClient?: () => RedisInstance,\n /**\n * Options passed directly to Redis constructor\n *\n * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options\n */\n options?: ClusterOptions | RedisOptions,\n /**\n * An unique instance name, required to filter messages in Redis.\n * If none is provided an unique id is generated.\n */\n identifier: string,\n /**\n * Namespace for Redis keys, if none is provided 'hocuspocus' is used\n */\n prefix: string,\n /**\n * The maximum time for the Redis lock in ms (in case it can’t be released).\n */\n lockTimeout: number,\n /**\n * A delay before onDisconnect is executed. This allows last minute updates'\n * sync messages to be received by the subscription before it's closed.\n */\n disconnectDelay: number,\n}\n\nexport class Redis implements Extension {\n /**\n * Make sure to give that extension a higher priority, so\n * the `onStoreDocument` hook is able to intercept the chain,\n * before documents are stored to the database.\n */\n priority = 1000\n\n configuration: Configuration = {\n port: 6379,\n host: '127.0.0.1',\n prefix: 'hocuspocus',\n identifier: `host-${uuid()}`,\n lockTimeout: 1000,\n disconnectDelay: 1000,\n }\n\n redisTransactionOrigin = '__hocuspocus__redis__origin__'\n\n pub: RedisInstance\n\n sub: RedisInstance\n\n instance!: Hocuspocus\n\n redlock: Redlock\n\n locks = new Map<string, Redlock.Lock>()\n\n logger: Debugger\n\n messagePrefix: Buffer\n\n /**\n * When we have a high frequency of updates to a document we don't need tons of setTimeouts\n * piling up, so we'll track them to keep it to the most recent per document.\n */\n private pendingDisconnects = new Map<string, NodeJS.Timeout>()\n\n private pendingAfterStoreDocumentResolves = new Map<string, { timeout: NodeJS.Timeout; resolve:() => void }>()\n\n public constructor(configuration: Partial<Configuration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n\n // We’ll replace that in the onConfigure hook with the global instance.\n this.logger = new Debugger()\n\n // Create Redis instance\n const {\n port,\n host,\n options,\n nodes,\n redis,\n createClient,\n } = this.configuration\n\n if (typeof createClient === 'function') {\n this.pub = createClient()\n this.sub = createClient()\n } else if (redis) {\n this.pub = redis.duplicate()\n this.sub = redis.duplicate()\n } else if (nodes && nodes.length > 0) {\n this.pub = new RedisClient.Cluster(nodes, options)\n this.sub = new RedisClient.Cluster(nodes, options)\n } else {\n this.pub = new RedisClient(port, host, options)\n this.sub = new RedisClient(port, host, options)\n }\n this.sub.on('messageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub], {\n retryCount: 0,\n })\n\n const identifierBuffer = Buffer.from(this.configuration.identifier, 'utf-8')\n this.messagePrefix = Buffer.concat([Buffer.from([identifierBuffer.length]), identifierBuffer])\n }\n\n async onConfigure({ instance }: onConfigurePayload) {\n this.logger = instance.debugger\n this.instance = instance\n }\n\n private getKey(documentName: string) {\n return `${this.configuration.prefix}:${documentName}`\n }\n\n private pubKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private subKey(documentName: string) {\n return this.getKey(documentName)\n }\n\n private lockKey(documentName: string) {\n return `${this.getKey(documentName)}:lock`\n }\n\n private encodeMessage(message: Uint8Array) {\n return Buffer.concat([this.messagePrefix, Buffer.from(message)])\n }\n\n private decodeMessage(buffer: Buffer) {\n const identifierLength = buffer[0]\n const identifier = buffer.toString('utf-8', 1, identifierLength + 1)\n\n return [identifier, buffer.slice(identifierLength + 1)]\n }\n\n /**\n * Once a document is loaded, subscribe to the channel in Redis.\n */\n public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {\n return new Promise((resolve, reject) => {\n // On document creation the node will connect to pub and sub channels\n // for the document.\n this.sub.subscribe(this.subKey(documentName), async error => {\n if (error) {\n reject(error)\n return\n }\n\n this.publishFirstSyncStep(documentName, document)\n this.requestAwarenessFromOtherInstances(documentName)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Publish the first sync step through Redis.\n */\n private async publishFirstSyncStep(documentName: string, document: Document) {\n const syncMessage = new OutgoingMessage(documentName)\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publishBuffer(this.pubKey(documentName), this.encodeMessage(syncMessage.toUint8Array()))\n }\n\n /**\n * Let’s ask Redis who is connected already.\n */\n private async requestAwarenessFromOtherInstances(documentName: string) {\n const awarenessMessage = new OutgoingMessage(documentName)\n .writeQueryAwareness()\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(awarenessMessage.toUint8Array()),\n )\n }\n\n /**\n * Before the document is stored, make sure to set a lock in Redis.\n * That’s meant to avoid conflicts with other instances trying to store the document.\n */\n async onStoreDocument({ documentName }: onStoreDocumentPayload) {\n // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,\n // to avoid conflict with other instances storing the same document.\n\n return new Promise((resolve, reject) => {\n this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {\n if (error || !lock) {\n // Expected behavior: Could not acquire lock, another instance locked it already.\n // No further `onStoreDocument` hooks will be executed.\n console.log('unable to acquire lock')\n reject()\n return\n }\n\n this.locks.set(this.lockKey(documentName), lock)\n\n resolve(undefined)\n })\n })\n }\n\n /**\n * Release the Redis lock, so other instances can store documents.\n */\n async afterStoreDocument({ documentName, socketId }: afterStoreDocumentPayload) {\n this.locks.get(this.lockKey(documentName))?.unlock()\n .catch(() => {\n // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.\n // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)\n })\n .finally(() => {\n this.locks.delete(this.lockKey(documentName))\n })\n\n // if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.\n // for provider connections, this usually happens in the onDisconnect hook\n if (socketId === 'server') {\n const pending = this.pendingAfterStoreDocumentResolves.get(documentName)\n\n if (pending) {\n clearTimeout(pending.timeout)\n pending.resolve()\n this.pendingAfterStoreDocumentResolves.delete(documentName)\n }\n\n let resolveFunction: () => void = () => {}\n const delayedPromise = new Promise<void>(resolve => {\n resolveFunction = resolve\n })\n\n const timeout = setTimeout(() => {\n this.pendingAfterStoreDocumentResolves.delete(documentName)\n resolveFunction()\n }, this.configuration.disconnectDelay)\n\n this.pendingAfterStoreDocumentResolves.set(documentName, { timeout, resolve: resolveFunction })\n\n await delayedPromise\n }\n }\n\n /**\n * Handle awareness update messages received directly by this Hocuspocus instance.\n */\n async onAwarenessUpdate({\n documentName, awareness, added, updated, removed,\n }: onAwarenessUpdatePayload) {\n const changedClients = added.concat(updated, removed)\n const message = new OutgoingMessage(documentName)\n .createAwarenessUpdateMessage(awareness, changedClients)\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Handle incoming messages published on subscribed document channels.\n * Note that this will also include messages from ourselves as it is not possible\n * in Redis to filter these.\n */\n private handleIncomingMessage = async (channel: Buffer, data: Buffer) => {\n const [identifier, messageBuffer] = this.decodeMessage(data)\n\n if (identifier === this.configuration.identifier) {\n return\n }\n\n const message = new IncomingMessage(messageBuffer)\n const documentName = message.readVarString()\n message.writeVarString(documentName)\n\n const document = this.instance.documents.get(documentName)\n\n if (!document) {\n // What does this mean? Why are we subscribed to this document?\n this.logger.log(`Received message for unknown document ${documentName}`)\n return\n }\n\n new MessageReceiver(\n message,\n this.logger,\n this.redisTransactionOrigin,\n ).apply(document, undefined, reply => {\n return this.pub.publishBuffer(\n this.pubKey(document.name),\n this.encodeMessage(reply),\n )\n })\n }\n\n /**\n * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.\n */\n public async onChange(data: onChangePayload): Promise<any> {\n if (data.transactionOrigin !== this.redisTransactionOrigin) {\n return this.publishFirstSyncStep(data.documentName, data.document)\n }\n }\n\n /**\n * Make sure to *not* listen for further changes, when there’s\n * no one connected anymore.\n */\n public onDisconnect = async ({ documentName }: onDisconnectPayload) => {\n const pending = this.pendingDisconnects.get(documentName)\n\n if (pending) {\n clearTimeout(pending)\n this.pendingDisconnects.delete(documentName)\n }\n\n const disconnect = () => {\n const document = this.instance.documents.get(documentName)\n\n this.pendingDisconnects.delete(documentName)\n\n // Do nothing, when other users are still connected to the document.\n if (!document || document.getConnectionsCount() > 0) {\n return\n }\n\n // Time to end the subscription on the document channel.\n this.sub.unsubscribe(this.subKey(documentName), (error: any) => {\n if (error) {\n console.error(error)\n }\n })\n\n this.instance.unloadDocument(document)\n }\n // Delay the disconnect procedure to allow last minute syncs to happen\n const timeout = setTimeout(disconnect, this.configuration.disconnectDelay)\n this.pendingDisconnects.set(documentName, timeout)\n }\n\n async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {\n const message = new OutgoingMessage(data.documentName)\n .writeBroadcastStateless(data.payload)\n\n return this.pub.publishBuffer(\n this.pubKey(data.documentName),\n this.encodeMessage(message.toUint8Array()),\n )\n }\n\n /**\n * Kill the Redlock connection immediately.\n */\n async onDestroy() {\n await this.redlock.quit()\n this.pub.disconnect(false)\n this.sub.disconnect(false)\n }\n}\n"],"names":["uuid"],"mappings":";;;;;MAqEa,KAAK,CAAA;AAyChB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AAxCxD;;;;AAIG;QACH,IAAQ,CAAA,QAAA,GAAG,IAAI,CAAA;AAEf,QAAA,IAAA,CAAA,aAAa,GAAkB;AAC7B,YAAA,IAAI,EAAE,IAAI;AACV,YAAA,IAAI,EAAE,WAAW;AACjB,YAAA,MAAM,EAAE,YAAY;AACpB,YAAA,UAAU,EAAE,CAAA,KAAA,EAAQA,EAAI,EAAE,CAAE,CAAA;AAC5B,YAAA,WAAW,EAAE,IAAI;AACjB,YAAA,eAAe,EAAE,IAAI;SACtB,CAAA;QAED,IAAsB,CAAA,sBAAA,GAAG,+BAA+B,CAAA;AAUxD,QAAA,IAAA,CAAA,KAAK,GAAG,IAAI,GAAG,EAAwB,CAAA;AAMvC;;;AAGG;AACK,QAAA,IAAA,CAAA,kBAAkB,GAAG,IAAI,GAAG,EAA0B,CAAA;AAEtD,QAAA,IAAA,CAAA,iCAAiC,GAAG,IAAI,GAAG,EAA2D,CAAA;AA0M9G;;;;AAIE;AACM,QAAA,IAAA,CAAA,qBAAqB,GAAG,OAAO,OAAe,EAAE,IAAY,KAAI;AACtE,YAAA,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;AAE5D,YAAA,IAAI,UAAU,KAAK,IAAI,CAAC,aAAa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D,MAAM,wBAAwB,CAAC,IAAqC,EAAA;QAClE,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC;AACnD,aAAA,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAExC,OAAO,IAAI,CAAC,GAAG,CAAC,aAAa,CAC3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAC9B,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAC3C,CAAA;KACF;AAED;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;AACzB,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;AAC1B,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;KAC3B;AACF;;;;"}
|
|
@@ -65,6 +65,12 @@ export declare class Redis implements Extension {
|
|
|
65
65
|
locks: Map<string, Redlock.Lock>;
|
|
66
66
|
logger: Debugger;
|
|
67
67
|
messagePrefix: Buffer;
|
|
68
|
+
/**
|
|
69
|
+
* When we have a high frequency of updates to a document we don't need tons of setTimeouts
|
|
70
|
+
* piling up, so we'll track them to keep it to the most recent per document.
|
|
71
|
+
*/
|
|
72
|
+
private pendingDisconnects;
|
|
73
|
+
private pendingAfterStoreDocumentResolves;
|
|
68
74
|
constructor(configuration: Partial<Configuration>);
|
|
69
75
|
onConfigure({ instance }: onConfigurePayload): Promise<void>;
|
|
70
76
|
private getKey;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hocuspocus/extension-redis",
|
|
3
|
-
"version": "2.13.
|
|
3
|
+
"version": "2.13.7",
|
|
4
4
|
"description": "Scale Hocuspocus horizontally with Redis",
|
|
5
5
|
"homepage": "https://hocuspocus.dev",
|
|
6
6
|
"keywords": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"@types/redlock": "^4.0.3"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@hocuspocus/server": "^2.13.
|
|
36
|
+
"@hocuspocus/server": "^2.13.7",
|
|
37
37
|
"ioredis": "^4.28.2",
|
|
38
38
|
"kleur": "^4.1.4",
|
|
39
39
|
"lodash.debounce": "^4.0.8",
|
package/src/Redis.ts
CHANGED
|
@@ -100,6 +100,14 @@ export class Redis implements Extension {
|
|
|
100
100
|
|
|
101
101
|
messagePrefix: Buffer
|
|
102
102
|
|
|
103
|
+
/**
|
|
104
|
+
* When we have a high frequency of updates to a document we don't need tons of setTimeouts
|
|
105
|
+
* piling up, so we'll track them to keep it to the most recent per document.
|
|
106
|
+
*/
|
|
107
|
+
private pendingDisconnects = new Map<string, NodeJS.Timeout>()
|
|
108
|
+
|
|
109
|
+
private pendingAfterStoreDocumentResolves = new Map<string, { timeout: NodeJS.Timeout; resolve:() => void }>()
|
|
110
|
+
|
|
103
111
|
public constructor(configuration: Partial<Configuration>) {
|
|
104
112
|
this.configuration = {
|
|
105
113
|
...this.configuration,
|
|
@@ -260,9 +268,27 @@ export class Redis implements Extension {
|
|
|
260
268
|
// if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.
|
|
261
269
|
// for provider connections, this usually happens in the onDisconnect hook
|
|
262
270
|
if (socketId === 'server') {
|
|
263
|
-
|
|
264
|
-
|
|
271
|
+
const pending = this.pendingAfterStoreDocumentResolves.get(documentName)
|
|
272
|
+
|
|
273
|
+
if (pending) {
|
|
274
|
+
clearTimeout(pending.timeout)
|
|
275
|
+
pending.resolve()
|
|
276
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
let resolveFunction: () => void = () => {}
|
|
280
|
+
const delayedPromise = new Promise<void>(resolve => {
|
|
281
|
+
resolveFunction = resolve
|
|
265
282
|
})
|
|
283
|
+
|
|
284
|
+
const timeout = setTimeout(() => {
|
|
285
|
+
this.pendingAfterStoreDocumentResolves.delete(documentName)
|
|
286
|
+
resolveFunction()
|
|
287
|
+
}, this.configuration.disconnectDelay)
|
|
288
|
+
|
|
289
|
+
this.pendingAfterStoreDocumentResolves.set(documentName, { timeout, resolve: resolveFunction })
|
|
290
|
+
|
|
291
|
+
await delayedPromise
|
|
266
292
|
}
|
|
267
293
|
}
|
|
268
294
|
|
|
@@ -332,9 +358,18 @@ export class Redis implements Extension {
|
|
|
332
358
|
* no one connected anymore.
|
|
333
359
|
*/
|
|
334
360
|
public onDisconnect = async ({ documentName }: onDisconnectPayload) => {
|
|
361
|
+
const pending = this.pendingDisconnects.get(documentName)
|
|
362
|
+
|
|
363
|
+
if (pending) {
|
|
364
|
+
clearTimeout(pending)
|
|
365
|
+
this.pendingDisconnects.delete(documentName)
|
|
366
|
+
}
|
|
367
|
+
|
|
335
368
|
const disconnect = () => {
|
|
336
369
|
const document = this.instance.documents.get(documentName)
|
|
337
370
|
|
|
371
|
+
this.pendingDisconnects.delete(documentName)
|
|
372
|
+
|
|
338
373
|
// Do nothing, when other users are still connected to the document.
|
|
339
374
|
if (!document || document.getConnectionsCount() > 0) {
|
|
340
375
|
return
|
|
@@ -350,7 +385,8 @@ export class Redis implements Extension {
|
|
|
350
385
|
this.instance.unloadDocument(document)
|
|
351
386
|
}
|
|
352
387
|
// Delay the disconnect procedure to allow last minute syncs to happen
|
|
353
|
-
setTimeout(disconnect, this.configuration.disconnectDelay)
|
|
388
|
+
const timeout = setTimeout(disconnect, this.configuration.disconnectDelay)
|
|
389
|
+
this.pendingDisconnects.set(documentName, timeout)
|
|
354
390
|
}
|
|
355
391
|
|
|
356
392
|
async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {
|