@hocuspocus/extension-redis 2.15.2 → 3.0.4-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 +6 -18
- package/dist/hocuspocus-redis.cjs.map +1 -1
- package/dist/hocuspocus-redis.esm.js +2 -7
- package/dist/hocuspocus-redis.esm.js.map +1 -1
- package/dist/packages/extension-database/src/Database.d.ts +1 -1
- package/dist/packages/extension-logger/src/Logger.d.ts +1 -1
- package/dist/packages/extension-redis/src/Redis.d.ts +4 -3
- package/dist/packages/extension-sqlite/src/SQLite.d.ts +2 -1
- package/dist/packages/extension-throttle/src/index.d.ts +2 -2
- package/dist/packages/extension-webhook/src/index.d.ts +3 -3
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +12 -45
- package/dist/packages/provider/src/HocuspocusProviderWebsocket.d.ts +5 -9
- package/dist/packages/provider/src/IncomingMessage.d.ts +3 -3
- package/dist/packages/provider/src/MessageReceiver.d.ts +2 -4
- package/dist/packages/provider/src/MessageSender.d.ts +2 -2
- package/dist/packages/provider/src/OutgoingMessage.d.ts +2 -2
- package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/AwarenessMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/CloseMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/QueryAwarenessMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/StatelessMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/SyncStepOneMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/SyncStepTwoMessage.d.ts +2 -1
- package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +2 -1
- package/dist/packages/provider/src/index.d.ts +0 -2
- package/dist/packages/provider/src/types.d.ts +12 -12
- package/dist/packages/server/src/ClientConnection.d.ts +19 -10
- package/dist/packages/server/src/Connection.d.ts +7 -23
- package/dist/packages/server/src/DirectConnection.d.ts +2 -2
- package/dist/packages/server/src/Document.d.ts +3 -7
- package/dist/packages/server/src/Hocuspocus.d.ts +7 -36
- package/dist/packages/server/src/IncomingMessage.d.ts +3 -3
- package/dist/packages/server/src/MessageReceiver.d.ts +5 -7
- package/dist/packages/server/src/OutgoingMessage.d.ts +4 -3
- package/dist/packages/server/src/Server.d.ts +23 -3
- package/dist/packages/server/src/index.d.ts +1 -1
- package/dist/packages/server/src/types.d.ts +15 -29
- package/dist/packages/server/src/util/getParameters.d.ts +1 -1
- package/dist/packages/transformer/src/Prosemirror.d.ts +1 -1
- package/dist/packages/transformer/src/Tiptap.d.ts +3 -3
- package/dist/packages/transformer/src/types.d.ts +1 -1
- package/dist/tests/utils/newHocuspocus.d.ts +2 -2
- package/dist/tests/utils/newHocuspocusProvider.d.ts +2 -2
- package/dist/tests/utils/newHocuspocusProviderWebsocket.d.ts +4 -3
- package/dist/tests/utils/retryableAssertion.d.ts +1 -1
- package/package.json +2 -2
- package/src/Redis.ts +8 -16
- package/dist/packages/provider/src/TiptapCollabProvider.d.ts +0 -161
- package/dist/packages/provider/src/TiptapCollabProviderWebsocket.d.ts +0 -19
- package/dist/packages/server/src/Debugger.d.ts +0 -14
- package/dist/tests/server/getMessageLogs.d.ts +0 -1
- package/dist/tests/server/requiresAuthentication.d.ts +0 -1
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var RedisClient = require('ioredis');
|
|
6
4
|
var Redlock = require('redlock');
|
|
7
5
|
var uuid = require('uuid');
|
|
8
6
|
var server = require('@hocuspocus/server');
|
|
9
7
|
|
|
10
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
-
|
|
12
|
-
var RedisClient__default = /*#__PURE__*/_interopDefaultLegacy(RedisClient);
|
|
13
|
-
var Redlock__default = /*#__PURE__*/_interopDefaultLegacy(Redlock);
|
|
14
|
-
|
|
15
8
|
class Redis {
|
|
16
9
|
constructor(configuration) {
|
|
17
10
|
/**
|
|
@@ -51,11 +44,9 @@ class Redis {
|
|
|
51
44
|
message.writeVarString(documentName);
|
|
52
45
|
const document = this.instance.documents.get(documentName);
|
|
53
46
|
if (!document) {
|
|
54
|
-
// What does this mean? Why are we subscribed to this document?
|
|
55
|
-
this.logger.log(`Received message for unknown document ${documentName}`);
|
|
56
47
|
return;
|
|
57
48
|
}
|
|
58
|
-
new server.MessageReceiver(message, this.
|
|
49
|
+
new server.MessageReceiver(message, this.redisTransactionOrigin).apply(document, undefined, reply => {
|
|
59
50
|
return this.pub.publishBuffer(this.pubKey(document.name), this.encodeMessage(reply));
|
|
60
51
|
});
|
|
61
52
|
};
|
|
@@ -92,8 +83,6 @@ class Redis {
|
|
|
92
83
|
...this.configuration,
|
|
93
84
|
...configuration,
|
|
94
85
|
};
|
|
95
|
-
// We’ll replace that in the onConfigure hook with the global instance.
|
|
96
|
-
this.logger = new server.Debugger();
|
|
97
86
|
// Create Redis instance
|
|
98
87
|
const { port, host, options, nodes, redis, createClient, } = this.configuration;
|
|
99
88
|
if (typeof createClient === 'function') {
|
|
@@ -105,22 +94,21 @@ class Redis {
|
|
|
105
94
|
this.sub = redis.duplicate();
|
|
106
95
|
}
|
|
107
96
|
else if (nodes && nodes.length > 0) {
|
|
108
|
-
this.pub = new
|
|
109
|
-
this.sub = new
|
|
97
|
+
this.pub = new RedisClient.Cluster(nodes, options);
|
|
98
|
+
this.sub = new RedisClient.Cluster(nodes, options);
|
|
110
99
|
}
|
|
111
100
|
else {
|
|
112
|
-
this.pub = new
|
|
113
|
-
this.sub = new
|
|
101
|
+
this.pub = new RedisClient(port, host, options);
|
|
102
|
+
this.sub = new RedisClient(port, host, options);
|
|
114
103
|
}
|
|
115
104
|
this.sub.on('messageBuffer', this.handleIncomingMessage);
|
|
116
|
-
this.redlock = new
|
|
105
|
+
this.redlock = new Redlock([this.pub], {
|
|
117
106
|
retryCount: 0,
|
|
118
107
|
});
|
|
119
108
|
const identifierBuffer = Buffer.from(this.configuration.identifier, 'utf-8');
|
|
120
109
|
this.messagePrefix = Buffer.concat([Buffer.from([identifierBuffer.length]), identifierBuffer]);
|
|
121
110
|
}
|
|
122
111
|
async onConfigure({ instance }) {
|
|
123
|
-
this.logger = instance.debugger;
|
|
124
112
|
this.instance = instance;
|
|
125
113
|
}
|
|
126
114
|
getKey(documentName) {
|
|
@@ -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 /**\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,GAA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AA,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.cjs","sources":["../src/Redis.ts"],"sourcesContent":["import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'\nimport RedisClient from 'ioredis'\nimport Redlock from 'redlock'\nimport { v4 as uuid } from 'uuid'\nimport type {\n Document,\n Extension,\n afterLoadDocumentPayload,\n afterStoreDocumentPayload,\n onDisconnectPayload,\n onStoreDocumentPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus} from '@hocuspocus/server'\nimport {\n IncomingMessage,\n OutgoingMessage,\n MessageReceiver,\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 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 // 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.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 return\n }\n\n new MessageReceiver(\n message,\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","OutgoingMessage"],"mappings":";;;;;;;MAsEa,KAAK,CAAA;AAuChB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AAtCxD;;;;AAIG;QACH,IAAQ,CAAA,QAAA,GAAG,IAAI;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;QAED,IAAsB,CAAA,sBAAA,GAAG,+BAA+B;AAUxD,QAAA,IAAA,CAAA,KAAK,GAAG,IAAI,GAAG,EAAwB;AAIvC;;;AAGG;AACK,QAAA,IAAA,CAAA,kBAAkB,GAAG,IAAI,GAAG,EAA0B;AAEtD,QAAA,IAAA,CAAA,iCAAiC,GAAG,IAAI,GAAG,EAA2D;AAsM9G;;;;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;AAE5D,YAAA,IAAI,UAAU,KAAK,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;gBAChD;AACD;AAED,YAAA,MAAM,OAAO,GAAG,IAAIC,sBAAe,CAAC,aAAa,CAAC;AAClD,YAAA,MAAM,YAAY,GAAG,OAAO,CAAC,aAAa,EAAE;AAC5C,YAAA,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC;AAEpC,YA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KAAK,IAAI,CAAC,sBAAsB,EAAE;AAC1D,YAAA,OAAO,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC;AACnE;;IAuCH,MAAM,wBAAwB,CAAC,IAAqC,EAAA;QAClE,MAAM,OAAO,GAAG,IAAIA,sBAAe,CAAC,IAAI,CAAC,YAAY;AAClD,aAAA,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC;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;;AAGH;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;AACzB,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;AAC1B,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;;AAE7B;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import RedisClient from 'ioredis';
|
|
2
2
|
import Redlock from 'redlock';
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
|
-
import { IncomingMessage, MessageReceiver,
|
|
4
|
+
import { IncomingMessage, MessageReceiver, OutgoingMessage } from '@hocuspocus/server';
|
|
5
5
|
|
|
6
6
|
class Redis {
|
|
7
7
|
constructor(configuration) {
|
|
@@ -42,11 +42,9 @@ class Redis {
|
|
|
42
42
|
message.writeVarString(documentName);
|
|
43
43
|
const document = this.instance.documents.get(documentName);
|
|
44
44
|
if (!document) {
|
|
45
|
-
// What does this mean? Why are we subscribed to this document?
|
|
46
|
-
this.logger.log(`Received message for unknown document ${documentName}`);
|
|
47
45
|
return;
|
|
48
46
|
}
|
|
49
|
-
new MessageReceiver(message, this.
|
|
47
|
+
new MessageReceiver(message, this.redisTransactionOrigin).apply(document, undefined, reply => {
|
|
50
48
|
return this.pub.publishBuffer(this.pubKey(document.name), this.encodeMessage(reply));
|
|
51
49
|
});
|
|
52
50
|
};
|
|
@@ -83,8 +81,6 @@ class Redis {
|
|
|
83
81
|
...this.configuration,
|
|
84
82
|
...configuration,
|
|
85
83
|
};
|
|
86
|
-
// We’ll replace that in the onConfigure hook with the global instance.
|
|
87
|
-
this.logger = new Debugger();
|
|
88
84
|
// Create Redis instance
|
|
89
85
|
const { port, host, options, nodes, redis, createClient, } = this.configuration;
|
|
90
86
|
if (typeof createClient === 'function') {
|
|
@@ -111,7 +107,6 @@ class Redis {
|
|
|
111
107
|
this.messagePrefix = Buffer.concat([Buffer.from([identifierBuffer.length]), identifierBuffer]);
|
|
112
108
|
}
|
|
113
109
|
async onConfigure({ instance }) {
|
|
114
|
-
this.logger = instance.debugger;
|
|
115
110
|
this.instance = instance;
|
|
116
111
|
}
|
|
117
112
|
getKey(documentName) {
|
|
@@ -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 /**\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,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,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;AACnE,SAAA;KACF;IAsCD,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;;;;"}
|
|
1
|
+
{"version":3,"file":"hocuspocus-redis.esm.js","sources":["../src/Redis.ts"],"sourcesContent":["import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis'\nimport RedisClient from 'ioredis'\nimport Redlock from 'redlock'\nimport { v4 as uuid } from 'uuid'\nimport type {\n Document,\n Extension,\n afterLoadDocumentPayload,\n afterStoreDocumentPayload,\n onDisconnectPayload,\n onStoreDocumentPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus} from '@hocuspocus/server'\nimport {\n IncomingMessage,\n OutgoingMessage,\n MessageReceiver,\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 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 // 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.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 return\n }\n\n new MessageReceiver(\n message,\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":";;;;;MAsEa,KAAK,CAAA;AAuChB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AAtCxD;;;;AAIG;QACH,IAAQ,CAAA,QAAA,GAAG,IAAI;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;QAED,IAAsB,CAAA,sBAAA,GAAG,+BAA+B;AAUxD,QAAA,IAAA,CAAA,KAAK,GAAG,IAAI,GAAG,EAAwB;AAIvC;;;AAGG;AACK,QAAA,IAAA,CAAA,kBAAkB,GAAG,IAAI,GAAG,EAA0B;AAEtD,QAAA,IAAA,CAAA,iCAAiC,GAAG,IAAI,GAAG,EAA2D;AAsM9G;;;;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;AAE5D,YAAA,IAAI,UAAU,KAAK,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;gBAChD;AACD;AAED,YAAA,MAAM,OAAO,GAAG,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AAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAC3C;;AAoCH;;AAEG;IACI,MAAM,QAAQ,CAAC,IAAqB,EAAA;AACzC,QAAA,IAAI,IAAI,CAAC,iBAAiB,KAAK,IAAI,CAAC,sBAAsB,EAAE;AAC1D,YAAA,OAAO,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC;AACnE;;IAuCH,MAAM,wBAAwB,CAAC,IAAqC,EAAA;QAClE,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,YAAY;AAClD,aAAA,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC;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;;AAGH;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;AACzB,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;AAC1B,QAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;;AAE7B;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Extension, onChangePayload, onLoadDocumentPayload, storePayload, fetchPayload } from '@hocuspocus/server';
|
|
1
|
+
import type { Extension, onChangePayload, onLoadDocumentPayload, storePayload, fetchPayload } from '@hocuspocus/server';
|
|
2
2
|
export interface DatabaseConfiguration {
|
|
3
3
|
/**
|
|
4
4
|
* Pass a Promise to retrieve updates from your database. The Promise should resolve to
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDestroyPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
|
|
1
|
+
import type { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDestroyPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
|
|
2
2
|
export interface LoggerConfiguration {
|
|
3
3
|
/**
|
|
4
4
|
* Prepend all logging message with a string.
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
|
|
4
|
+
import RedisClient from 'ioredis';
|
|
3
5
|
import Redlock from 'redlock';
|
|
4
|
-
import { Extension, afterLoadDocumentPayload, afterStoreDocumentPayload, onDisconnectPayload, onStoreDocumentPayload, onAwarenessUpdatePayload, onChangePayload,
|
|
6
|
+
import type { Extension, afterLoadDocumentPayload, afterStoreDocumentPayload, onDisconnectPayload, onStoreDocumentPayload, onAwarenessUpdatePayload, onChangePayload, onConfigurePayload, beforeBroadcastStatelessPayload, Hocuspocus } from '@hocuspocus/server';
|
|
5
7
|
export type RedisInstance = RedisClient.Cluster | RedisClient.Redis;
|
|
6
8
|
export interface Configuration {
|
|
7
9
|
/**
|
|
@@ -63,7 +65,6 @@ export declare class Redis implements Extension {
|
|
|
63
65
|
instance: Hocuspocus;
|
|
64
66
|
redlock: Redlock;
|
|
65
67
|
locks: Map<string, Redlock.Lock>;
|
|
66
|
-
logger: Debugger;
|
|
67
68
|
messagePrefix: Buffer;
|
|
68
69
|
/**
|
|
69
70
|
* When we have a high frequency of updates to a document we don't need tons of setTimeouts
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { DatabaseConfiguration } from '@hocuspocus/extension-database';
|
|
2
|
+
import { Database } from '@hocuspocus/extension-database';
|
|
2
3
|
import sqlite3 from 'sqlite3';
|
|
3
4
|
export declare const schema = "CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL,\n UNIQUE(name)\n)";
|
|
4
5
|
export declare const selectQuery = "\n SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Extension, onConnectPayload } from '@hocuspocus/server';
|
|
2
|
+
import type { Extension, onConnectPayload } from '@hocuspocus/server';
|
|
3
3
|
export interface ThrottleConfiguration {
|
|
4
4
|
throttle: number | null | false;
|
|
5
5
|
consideredSeconds: number;
|
|
@@ -10,7 +10,7 @@ export declare class Throttle implements Extension {
|
|
|
10
10
|
configuration: ThrottleConfiguration;
|
|
11
11
|
connectionsByIp: Map<string, Array<number>>;
|
|
12
12
|
bannedIps: Map<string, number>;
|
|
13
|
-
cleanupInterval?: NodeJS.
|
|
13
|
+
cleanupInterval?: NodeJS.Timeout;
|
|
14
14
|
/**
|
|
15
15
|
* Constructor
|
|
16
16
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Extension, onChangePayload, onConnectPayload, onLoadDocumentPayload, onDisconnectPayload } from '@hocuspocus/server';
|
|
3
|
-
import { Doc } from 'yjs';
|
|
4
|
-
import { Transformer } from '@hocuspocus/transformer';
|
|
2
|
+
import type { Extension, onChangePayload, onConnectPayload, onLoadDocumentPayload, onDisconnectPayload } from '@hocuspocus/server';
|
|
3
|
+
import type { Doc } from 'yjs';
|
|
4
|
+
import type { Transformer } from '@hocuspocus/transformer';
|
|
5
5
|
export declare enum Events {
|
|
6
6
|
onChange = "change",
|
|
7
7
|
onConnect = "connect",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { CloseEvent, Event, MessageEvent } from 'ws';
|
|
1
|
+
import type { Event, MessageEvent } from 'ws';
|
|
3
2
|
import { Awareness } from 'y-protocols/awareness';
|
|
4
3
|
import * as Y from 'yjs';
|
|
5
4
|
import EventEmitter from './EventEmitter.js';
|
|
6
|
-
import { CompleteHocuspocusProviderWebsocketConfiguration
|
|
7
|
-
import {
|
|
5
|
+
import type { CompleteHocuspocusProviderWebsocketConfiguration } from './HocuspocusProviderWebsocket.js';
|
|
6
|
+
import { HocuspocusProviderWebsocket } from './HocuspocusProviderWebsocket.js';
|
|
7
|
+
import type { ConstructableOutgoingMessage, onAuthenticationFailedParameters, onAwarenessChangeParameters, onAwarenessUpdateParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatelessParameters, onSyncedParameters } from './types.js';
|
|
8
8
|
export type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'name'>> & Partial<CompleteHocuspocusProviderConfiguration> & (Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, 'url'>> | Required<Pick<CompleteHocuspocusProviderConfiguration, 'websocketProvider'>>);
|
|
9
9
|
export interface CompleteHocuspocusProviderConfiguration {
|
|
10
10
|
/**
|
|
@@ -15,10 +15,6 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
15
15
|
* The actual Y.js document
|
|
16
16
|
*/
|
|
17
17
|
document: Y.Doc;
|
|
18
|
-
/**
|
|
19
|
-
* Pass false to disable broadcasting between browser tabs.
|
|
20
|
-
*/
|
|
21
|
-
broadcast: boolean;
|
|
22
18
|
/**
|
|
23
19
|
* An Awareness instance to keep the presence state of all clients.
|
|
24
20
|
*
|
|
@@ -32,12 +28,6 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
32
28
|
* A token that’s sent to the backend for authentication purposes.
|
|
33
29
|
*/
|
|
34
30
|
token: string | (() => string) | (() => Promise<string>) | null;
|
|
35
|
-
/**
|
|
36
|
-
* URL parameters that should be added.
|
|
37
|
-
*/
|
|
38
|
-
parameters: {
|
|
39
|
-
[key: string]: any;
|
|
40
|
-
};
|
|
41
31
|
/**
|
|
42
32
|
* Hocuspocus websocket provider
|
|
43
33
|
*/
|
|
@@ -52,7 +42,6 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
52
42
|
onConnect: () => void;
|
|
53
43
|
onMessage: (data: onMessageParameters) => void;
|
|
54
44
|
onOutgoingMessage: (data: onOutgoingMessageParameters) => void;
|
|
55
|
-
onStatus: (data: onStatusParameters) => void;
|
|
56
45
|
onSynced: (data: onSyncedParameters) => void;
|
|
57
46
|
onDisconnect: (data: onDisconnectParameters) => void;
|
|
58
47
|
onClose: (data: onCloseParameters) => void;
|
|
@@ -60,47 +49,29 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
60
49
|
onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
|
|
61
50
|
onAwarenessChange: (data: onAwarenessChangeParameters) => void;
|
|
62
51
|
onStateless: (data: onStatelessParameters) => void;
|
|
63
|
-
/**
|
|
64
|
-
* Don’t output any warnings.
|
|
65
|
-
*/
|
|
66
|
-
quiet: boolean;
|
|
67
|
-
/**
|
|
68
|
-
* Pass `false` to start the connection manually.
|
|
69
|
-
*/
|
|
70
|
-
connect: boolean;
|
|
71
|
-
/**
|
|
72
|
-
* Pass `false` to close the connection manually.
|
|
73
|
-
*/
|
|
74
|
-
preserveConnection: boolean;
|
|
75
52
|
}
|
|
76
53
|
export declare class AwarenessError extends Error {
|
|
77
54
|
code: number;
|
|
78
55
|
}
|
|
79
56
|
export declare class HocuspocusProvider extends EventEmitter {
|
|
80
57
|
configuration: CompleteHocuspocusProviderConfiguration;
|
|
81
|
-
subscribedToBroadcastChannel: boolean;
|
|
82
58
|
isSynced: boolean;
|
|
83
59
|
unsyncedChanges: number;
|
|
84
|
-
status: WebSocketStatus;
|
|
85
60
|
isAuthenticated: boolean;
|
|
86
61
|
authorizedScope: string | undefined;
|
|
87
|
-
|
|
62
|
+
manageSocket: boolean;
|
|
63
|
+
private isAttached;
|
|
88
64
|
intervals: any;
|
|
89
|
-
isConnected: boolean;
|
|
90
65
|
constructor(configuration: HocuspocusProviderConfiguration);
|
|
91
66
|
boundDocumentUpdateHandler: (update: Uint8Array, origin: any) => void;
|
|
92
67
|
boundAwarenessUpdateHandler: ({ added, updated, removed }: any, origin: any) => void;
|
|
93
|
-
boundBroadcastChannelSubscriber: (data: ArrayBuffer) => void;
|
|
94
68
|
boundPageHide: () => void;
|
|
95
69
|
boundOnOpen: (event: Event) => Promise<void>;
|
|
96
|
-
boundOnClose: (
|
|
97
|
-
boundOnStatus: ({ status }: onStatusParameters) => void;
|
|
70
|
+
boundOnClose: () => void;
|
|
98
71
|
forwardConnect: (e: any) => this;
|
|
99
|
-
forwardOpen: (e: any) => this;
|
|
100
72
|
forwardClose: (e: any) => this;
|
|
101
73
|
forwardDisconnect: (e: any) => this;
|
|
102
74
|
forwardDestroy: (e: any) => this;
|
|
103
|
-
onStatus({ status }: onStatusParameters): void;
|
|
104
75
|
setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
|
|
105
76
|
get document(): Y.Doc;
|
|
106
77
|
get awareness(): Awareness | null;
|
|
@@ -123,22 +94,18 @@ export declare class HocuspocusProvider extends EventEmitter {
|
|
|
123
94
|
get synced(): boolean;
|
|
124
95
|
set synced(state: boolean);
|
|
125
96
|
receiveStateless(payload: string): void;
|
|
126
|
-
|
|
127
|
-
connect(): Promise<any>;
|
|
97
|
+
connect(): Promise<void>;
|
|
128
98
|
disconnect(): void;
|
|
129
99
|
onOpen(event: Event): Promise<void>;
|
|
130
100
|
getToken(): Promise<string | null>;
|
|
131
101
|
startSync(): void;
|
|
132
|
-
send(message: ConstructableOutgoingMessage, args: any
|
|
102
|
+
send(message: ConstructableOutgoingMessage, args: any): void;
|
|
133
103
|
onMessage(event: MessageEvent): void;
|
|
134
|
-
onClose(
|
|
104
|
+
onClose(): void;
|
|
135
105
|
destroy(): void;
|
|
106
|
+
detach(): void;
|
|
107
|
+
attach(): void;
|
|
136
108
|
permissionDeniedHandler(reason: string): void;
|
|
137
109
|
authenticatedHandler(scope: string): void;
|
|
138
|
-
get broadcastChannel(): string;
|
|
139
|
-
broadcastChannelSubscriber(data: ArrayBuffer): void;
|
|
140
|
-
subscribeToBroadcastChannel(): void;
|
|
141
|
-
disconnectBroadcastChannel(): void;
|
|
142
|
-
broadcast(Message: ConstructableOutgoingMessage, args?: any): void;
|
|
143
110
|
setAwarenessField(key: string, value: any): void;
|
|
144
111
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { MessageEvent } from 'ws';
|
|
2
|
-
import { Event } from 'ws';
|
|
1
|
+
import type { MessageEvent, Event } from 'ws';
|
|
3
2
|
import EventEmitter from './EventEmitter.js';
|
|
4
|
-
import { HocuspocusProvider } from './HocuspocusProvider.js';
|
|
5
|
-
import {
|
|
3
|
+
import type { HocuspocusProvider } from './HocuspocusProvider.js';
|
|
4
|
+
import type { onAwarenessChangeParameters, onAwarenessUpdateParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatusParameters } from './types.js';
|
|
5
|
+
import { WebSocketStatus } from './types.js';
|
|
6
6
|
export type HocusPocusWebSocket = WebSocket & {
|
|
7
7
|
identifier: string;
|
|
8
8
|
};
|
|
@@ -72,10 +72,6 @@ export interface CompleteHocuspocusProviderWebsocketConfiguration {
|
|
|
72
72
|
onDestroy: () => void;
|
|
73
73
|
onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
|
|
74
74
|
onAwarenessChange: (data: onAwarenessChangeParameters) => void;
|
|
75
|
-
/**
|
|
76
|
-
* Don’t output any warnings.
|
|
77
|
-
*/
|
|
78
|
-
quiet: boolean;
|
|
79
75
|
/**
|
|
80
76
|
* Map of attached providers keyed by documentName.
|
|
81
77
|
*/
|
|
@@ -102,7 +98,7 @@ export declare class HocuspocusProviderWebsocket extends EventEmitter {
|
|
|
102
98
|
receivedOnStatusPayload?: onStatusParameters | undefined;
|
|
103
99
|
onOpen(event: Event): Promise<void>;
|
|
104
100
|
onStatus(data: onStatusParameters): Promise<void>;
|
|
105
|
-
attach(provider: HocuspocusProvider):
|
|
101
|
+
attach(provider: HocuspocusProvider): void;
|
|
106
102
|
detach(provider: HocuspocusProvider): void;
|
|
107
103
|
setConfiguration(configuration?: Partial<HocuspocusProviderWebsocketConfiguration>): void;
|
|
108
104
|
cancelWebsocketRetry?: () => void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Decoder } from 'lib0/decoding';
|
|
2
|
-
import { Encoder } from 'lib0/encoding';
|
|
3
|
-
import { MessageType } from './types.js';
|
|
1
|
+
import type { Decoder } from 'lib0/decoding';
|
|
2
|
+
import type { Encoder } from 'lib0/encoding';
|
|
3
|
+
import type { MessageType } from './types.js';
|
|
4
4
|
export declare class IncomingMessage {
|
|
5
5
|
data: any;
|
|
6
6
|
encoder: Encoder;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { HocuspocusProvider } from './HocuspocusProvider.js';
|
|
2
|
-
import { IncomingMessage } from './IncomingMessage.js';
|
|
1
|
+
import type { HocuspocusProvider } from './HocuspocusProvider.js';
|
|
2
|
+
import type { IncomingMessage } from './IncomingMessage.js';
|
|
3
3
|
export declare class MessageReceiver {
|
|
4
4
|
message: IncomingMessage;
|
|
5
|
-
broadcasted: boolean;
|
|
6
5
|
constructor(message: IncomingMessage);
|
|
7
|
-
setBroadcasted(value: boolean): this;
|
|
8
6
|
apply(provider: HocuspocusProvider, emitSynced: boolean): void;
|
|
9
7
|
private applySyncMessage;
|
|
10
8
|
applySyncStatusMessage(provider: HocuspocusProvider, applied: boolean): void;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Encoder } from 'lib0/encoding';
|
|
2
|
-
import { ConstructableOutgoingMessage } from './types.js';
|
|
1
|
+
import type { Encoder } from 'lib0/encoding';
|
|
2
|
+
import type { ConstructableOutgoingMessage } from './types.js';
|
|
3
3
|
export declare class MessageSender {
|
|
4
4
|
encoder: Encoder;
|
|
5
5
|
message: any;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Encoder } from 'lib0/encoding';
|
|
2
|
-
import { MessageType, OutgoingMessageArguments, OutgoingMessageInterface } from './types.js';
|
|
1
|
+
import type { Encoder } from 'lib0/encoding';
|
|
2
|
+
import type { MessageType, OutgoingMessageArguments, OutgoingMessageInterface } from './types.js';
|
|
3
3
|
export declare class OutgoingMessage implements OutgoingMessageInterface {
|
|
4
4
|
encoder: Encoder;
|
|
5
5
|
type?: MessageType;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { OutgoingMessageArguments } from '../types.js';
|
|
2
|
+
import { MessageType } from '../types.js';
|
|
2
3
|
import { OutgoingMessage } from '../OutgoingMessage.js';
|
|
3
4
|
export declare class AuthenticationMessage extends OutgoingMessage {
|
|
4
5
|
type: MessageType;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as encoding from 'lib0/encoding';
|
|
2
|
-
import {
|
|
2
|
+
import type { OutgoingMessageArguments } from '../types.js';
|
|
3
|
+
import { MessageType } from '../types.js';
|
|
3
4
|
import { OutgoingMessage } from '../OutgoingMessage.js';
|
|
4
5
|
export declare class AwarenessMessage extends OutgoingMessage {
|
|
5
6
|
type: MessageType;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as encoding from 'lib0/encoding';
|
|
2
|
-
import {
|
|
2
|
+
import type { OutgoingMessageArguments } from '../types.js';
|
|
3
|
+
import { MessageType } from '../types.js';
|
|
3
4
|
import { OutgoingMessage } from '../OutgoingMessage.js';
|
|
4
5
|
export declare class CloseMessage extends OutgoingMessage {
|
|
5
6
|
type: MessageType;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as encoding from 'lib0/encoding';
|
|
2
|
-
import {
|
|
2
|
+
import type { OutgoingMessageArguments } from '../types.js';
|
|
3
|
+
import { MessageType } from '../types.js';
|
|
3
4
|
import { OutgoingMessage } from '../OutgoingMessage.js';
|
|
4
5
|
export declare class QueryAwarenessMessage extends OutgoingMessage {
|
|
5
6
|
type: MessageType;
|