@hocuspocus/extension-redis 1.0.0-alpha.61 → 1.0.0-alpha.62

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.
@@ -2,67 +2,186 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var yRedis = require('y-redis');
5
+ var RedisClient = require('ioredis');
6
+ var Redlock = require('redlock');
7
+ var uuid = require('uuid');
8
+ var server = require('@hocuspocus/server');
9
+ var kleur = require('kleur');
10
+
11
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
+
13
+ var RedisClient__default = /*#__PURE__*/_interopDefaultLegacy(RedisClient);
14
+ var Redlock__default = /*#__PURE__*/_interopDefaultLegacy(Redlock);
15
+ var kleur__default = /*#__PURE__*/_interopDefaultLegacy(kleur);
6
16
 
7
17
  class Redis {
8
- /**
9
- * Constructor
10
- */
11
18
  constructor(configuration) {
12
- this.configuration = {};
13
- this.cluster = false;
19
+ /**
20
+ * Make sure to give that extension a higher priority, so
21
+ * the `onStoreDocument` hook is able to intercept the chain,
22
+ * before documents are stored to the database.
23
+ */
24
+ this.priority = 1000;
25
+ this.configuration = {
26
+ port: 6379,
27
+ host: '127.0.0.1',
28
+ prefix: 'hocuspocus',
29
+ identifier: `host-${uuid.v4()}`,
30
+ lockTimeout: 1000,
31
+ };
32
+ this.documents = new Map();
33
+ this.locks = new Map();
34
+ /**
35
+ * Handle incoming messages published on all subscribed document channels.
36
+ * Note that this will also include messages from ourselves as it is not possible
37
+ * in Redis to filter these.
38
+ */
39
+ this.handleIncomingMessage = async (channel, pattern, data) => {
40
+ const channelName = pattern.toString();
41
+ const [_, documentName, identifier] = channelName.split(':');
42
+ const document = this.documents.get(documentName);
43
+ if (identifier === this.configuration.identifier) {
44
+ return;
45
+ }
46
+ if (!document) {
47
+ return;
48
+ }
49
+ new server.MessageReceiver(new server.IncomingMessage(data), this.logger).apply(document, undefined, reply => {
50
+ return this.pub.publishBuffer(this.pubKey(document.name), Buffer.from(reply));
51
+ });
52
+ };
53
+ /**
54
+ * Make sure to *not* listen for further changes, when there’s
55
+ * noone connected anymore.
56
+ */
57
+ this.onDisconnect = async ({ documentName, clientsCount }) => {
58
+ // Do nothing, when other users are still connected to the document.
59
+ if (clientsCount > 0) {
60
+ return;
61
+ }
62
+ // It was indeed the last connected user.
63
+ this.documents.delete(documentName);
64
+ // Time to end the subscription on the document channel.
65
+ this.sub.punsubscribe(this.subKey(documentName), error => {
66
+ if (error) {
67
+ console.error(error);
68
+ }
69
+ });
70
+ };
71
+ const { port, host, options } = configuration;
14
72
  this.configuration = {
15
73
  ...this.configuration,
16
74
  ...configuration,
17
75
  };
76
+ this.pub = new RedisClient__default["default"](port, host, options);
77
+ this.sub = new RedisClient__default["default"](port, host, options);
78
+ this.sub.on('pmessageBuffer', this.handleIncomingMessage);
79
+ this.redlock = new Redlock__default["default"]([this.pub]);
80
+ // We’ll replace that in the onConfigure hook with the global instance.
81
+ this.logger = new server.Debugger();
82
+ }
83
+ async onConfigure({ instance }) {
84
+ this.logger = instance.debugger;
85
+ }
86
+ async onListen() {
87
+ console.warn(` ${kleur__default["default"].yellow('[BREAKING CHANGE] Wait, the Redis extension got an overhaul. The new Redis extension doesn’t persist data, it only syncs data between instances. Use @hocuspocus/extension-database to store your documents. It works well with the Redis extension.')}`);
88
+ console.log();
89
+ }
90
+ getKey(documentName) {
91
+ return `${this.configuration.prefix}:${documentName}`;
92
+ }
93
+ pubKey(documentName) {
94
+ return `${this.getKey(documentName)}:${this.configuration.identifier.replace(/:/g, '')}`;
95
+ }
96
+ subKey(documentName) {
97
+ return `${this.getKey(documentName)}:*`;
98
+ }
99
+ lockKey(documentName) {
100
+ return `${this.getKey(documentName)}:lock`;
101
+ }
102
+ /**
103
+ * Once a document is laoded, subscribe to the channel in Redis.
104
+ */
105
+ async afterLoadDocument({ documentName, document }) {
106
+ this.documents.set(documentName, document);
107
+ return new Promise((resolve, reject) => {
108
+ // On document creation the node will connect to pub and sub channels
109
+ // for the document.
110
+ this.sub.psubscribe(this.subKey(documentName), async (error) => {
111
+ if (error) {
112
+ reject(error);
113
+ return;
114
+ }
115
+ this.publishFirstSyncStep(documentName, document);
116
+ this.requestAwarenessFromOtherInstances(documentName);
117
+ resolve(undefined);
118
+ });
119
+ });
18
120
  }
19
- /*
20
- * onLoadDocument hook
121
+ /**
122
+ * Publish the first sync step through Redis.
21
123
  */
22
- async onLoadDocument(data) {
23
- if (!this.persistence) {
24
- return;
25
- }
26
- // If another connection has already loaded this doc, return this one instead
27
- const binding = this.persistence.docs.get(data.documentName);
28
- if (binding) {
29
- return binding.doc;
30
- }
31
- await this.persistence.bindState(data.documentName, data.document).synced;
124
+ async publishFirstSyncStep(documentName, document) {
125
+ const syncMessage = new server.OutgoingMessage()
126
+ .createSyncMessage()
127
+ .writeFirstSyncStepFor(document);
128
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(syncMessage.toUint8Array()));
32
129
  }
33
- async onConnect(data) {
34
- // Bind to Redis already? Ok, no worries.
35
- if (this.persistence) {
36
- return;
37
- }
38
- this.persistence = new yRedis.RedisPersistence(
39
- // @ts-ignore
40
- this.cluster
41
- ? { redisClusterOpts: this.configuration }
42
- : { redisOpts: this.configuration });
130
+ /**
131
+ * Let’s ask Redis who is connected already.
132
+ */
133
+ async requestAwarenessFromOtherInstances(documentName) {
134
+ const awarenessMessage = new server.OutgoingMessage()
135
+ .writeQueryAwareness();
136
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(awarenessMessage.toUint8Array()));
43
137
  }
44
- async onDisconnect(data) {
45
- // Not binded to Redis? Never mind!
46
- if (!this.persistence) {
47
- return;
48
- }
49
- // Still clients connected?
50
- if (data.clientsCount > 0) {
51
- return;
52
- }
53
- // Fine, let’s remove the binding …
54
- this.persistence.destroy();
55
- this.persistence = undefined;
138
+ /**
139
+ * Before the document is stored, make sure to set a lock in Redis.
140
+ * That’s meant to avoid conflicts with other instances trying to store the document.
141
+ */
142
+ async onStoreDocument({ documentName }) {
143
+ // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,
144
+ // to avoid conflict with other instances storing the same document.
145
+ return new Promise((resolve, reject) => {
146
+ this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {
147
+ if (error || !lock) {
148
+ // Expected behavior: Could not acquire lock, another instance locked it already.
149
+ // No further `onStoreDocument` hooks will be executed.
150
+ reject();
151
+ return;
152
+ }
153
+ this.locks.set(this.lockKey(documentName), lock);
154
+ resolve(undefined);
155
+ });
156
+ });
56
157
  }
57
- }
58
-
59
- class RedisCluster extends Redis {
60
- constructor() {
61
- super(...arguments);
62
- this.cluster = true;
158
+ /**
159
+ * Release the Redis lock, so other instances can store documents.
160
+ */
161
+ async afterStoreDocument({ documentName }) {
162
+ var _a;
163
+ (_a = this.locks.get(this.lockKey(documentName))) === null || _a === void 0 ? void 0 : _a.unlock().catch(() => {
164
+ // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.
165
+ // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)
166
+ }).finally(() => {
167
+ this.locks.delete(this.lockKey(documentName));
168
+ });
169
+ }
170
+ /**
171
+ * Handle awareness update messages received directly by this Hocuspocus instance.
172
+ */
173
+ async onAwarenessUpdate({ documentName, awareness }) {
174
+ const message = new server.OutgoingMessage()
175
+ .createAwarenessUpdateMessage(awareness);
176
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(message.toUint8Array()));
177
+ }
178
+ /**
179
+ * Kill the Redlock connection immediately.
180
+ */
181
+ async onDestroy() {
182
+ this.redlock.quit();
63
183
  }
64
184
  }
65
185
 
66
186
  exports.Redis = Redis;
67
- exports.RedisCluster = RedisCluster;
68
187
  //# sourceMappingURL=hocuspocus-redis.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-redis.cjs","sources":["../src/Redis.ts","../src/RedisCluster.ts"],"sourcesContent":["import { RedisPersistence } from 'y-redis'\nimport {\n Extension,\n onConnectPayload,\n onLoadDocumentPayload,\n onDisconnectPayload,\n} from '@hocuspocus/server'\n\nexport interface Configuration {\n}\n\nexport class Redis implements Extension {\n\n configuration: Configuration = {}\n\n cluster = false\n\n persistence!: RedisPersistence | undefined\n\n /**\n * Constructor\n */\n constructor(configuration?: Partial<Configuration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n /*\n * onLoadDocument hook\n */\n async onLoadDocument(data: onLoadDocumentPayload) {\n if (!this.persistence) {\n return\n }\n\n // If another connection has already loaded this doc, return this one instead\n const binding = this.persistence.docs.get(data.documentName)\n\n if (binding) {\n return binding.doc\n }\n\n await this.persistence.bindState(data.documentName, data.document).synced\n }\n\n async onConnect(data: onConnectPayload) {\n // Bind to Redis already? Ok, no worries.\n if (this.persistence) {\n return\n }\n\n this.persistence = new RedisPersistence(\n // @ts-ignore\n this.cluster\n ? { redisClusterOpts: this.configuration }\n : { redisOpts: this.configuration },\n )\n\n }\n\n async onDisconnect(data: onDisconnectPayload) {\n // Not binded to Redis? Never mind!\n if (!this.persistence) {\n return\n }\n\n // Still clients connected?\n if (data.clientsCount > 0) {\n return\n }\n\n // Fine, let’s remove the binding …\n this.persistence.destroy()\n this.persistence = undefined\n\n }\n\n}\n","import { Redis } from './Redis'\n\nexport class RedisCluster extends Redis {\n\n cluster = true\n\n}\n"],"names":["RedisPersistence"],"mappings":";;;;;;MAWa,KAAK,CAAA;AAQhB;;AAEG;AACH,IAAA,WAAA,CAAY,aAAsC,EAAA;QATlD,IAAa,CAAA,aAAA,GAAkB,EAAE,CAAA;QAEjC,IAAO,CAAA,OAAA,GAAG,KAAK,CAAA;QAQb,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;AACrB,YAAA,GAAG,aAAa;SACjB,CAAA;KACF;AAED;;AAEG;IACH,MAAM,cAAc,CAAC,IAA2B,EAAA;AAC9C,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACrB,OAAM;AACP,SAAA;;AAGD,QAAA,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;AAE5D,QAAA,IAAI,OAAO,EAAE;YACX,OAAO,OAAO,CAAC,GAAG,CAAA;AACnB,SAAA;AAED,QAAA,MAAM,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAA;KAC1E;IAED,MAAM,SAAS,CAAC,IAAsB,EAAA;;QAEpC,IAAI,IAAI,CAAC,WAAW,EAAE;YACpB,OAAM;AACP,SAAA;AAED,QAAA,IAAI,CAAC,WAAW,GAAG,IAAIA,uBAAgB;;AAErC,QAAA,IAAI,CAAC,OAAO;AACV,cAAE,EAAE,gBAAgB,EAAE,IAAI,CAAC,aAAa,EAAE;cACxC,EAAE,SAAS,EAAE,IAAI,CAAC,aAAa,EAAE,CACtC,CAAA;KAEF;IAED,MAAM,YAAY,CAAC,IAAyB,EAAA;;AAE1C,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACrB,OAAM;AACP,SAAA;;AAGD,QAAA,IAAI,IAAI,CAAC,YAAY,GAAG,CAAC,EAAE;YACzB,OAAM;AACP,SAAA;;AAGD,QAAA,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAA;AAC1B,QAAA,IAAI,CAAC,WAAW,GAAG,SAAS,CAAA;KAE7B;AAEF;;AC7EK,MAAO,YAAa,SAAQ,KAAK,CAAA;AAAvC,IAAA,WAAA,GAAA;;QAEE,IAAO,CAAA,OAAA,GAAG,IAAI,CAAA;KAEf;AAAA;;;;;"}
1
+ {"version":3,"file":"hocuspocus-redis.cjs","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient 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 MessageReceiver,\n Debugger,\n onConfigurePayload,\n} from '@hocuspocus/server'\nimport kleur from 'kleur'\n\nexport interface Configuration {\n /**\n * Redis port\n */\n port: number,\n /**\n * Redis host\n */\n host: string,\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?: RedisClient.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\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 }\n\n pub: RedisClient.Redis\n\n sub: RedisClient.Redis\n\n documents: Map<string, Document> = new Map()\n\n redlock: Redlock\n\n locks = new Map<string, Redlock.Lock>()\n\n logger: Debugger\n\n public constructor(configuration: Partial<Configuration>) {\n const { port, host, options } = configuration\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n\n this.pub = new RedisClient(port, host, options)\n\n this.sub = new RedisClient(port, host, options)\n this.sub.on('pmessageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub])\n\n // We’ll replace that in the onConfigure hook with the global instance.\n this.logger = new Debugger()\n }\n\n async onConfigure({ instance }: onConfigurePayload) {\n this.logger = instance.debugger\n }\n\n async onListen() {\n console.warn(` ${kleur.yellow('[BREAKING CHANGE] Wait, the Redis extension got an overhaul. The new Redis extension doesn’t persist data, it only syncs data between instances. Use @hocuspocus/extension-database to store your documents. It works well with the Redis extension.')}`)\n console.log()\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)}:${this.configuration.identifier.replace(/:/g, '')}`\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 /**\n * Once a document is laoded, subscribe to the channel in Redis.\n */\n public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {\n this.documents.set(documentName, document)\n\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.psubscribe(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()\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(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()\n .writeQueryAwareness()\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n Buffer.from(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 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 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 }: 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\n /**\n * Handle awareness update messages received directly by this Hocuspocus instance.\n */\n async onAwarenessUpdate({ documentName, awareness }: onAwarenessUpdatePayload) {\n const message = new OutgoingMessage()\n .createAwarenessUpdateMessage(awareness)\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n Buffer.from(message.toUint8Array()),\n )\n }\n\n /**\n * Handle incoming messages published on all 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, pattern: Buffer, data: Buffer) => {\n const channelName = pattern.toString()\n const [_, documentName, identifier] = channelName.split(':')\n const document = this.documents.get(documentName)\n\n if (identifier === this.configuration.identifier) {\n return\n }\n\n if (!document) {\n return\n }\n\n new MessageReceiver(\n new IncomingMessage(data),\n this.logger,\n ).apply(document, undefined, reply => {\n return this.pub.publishBuffer(\n this.pubKey(document.name),\n Buffer.from(reply),\n )\n })\n }\n\n /**\n * Make sure to *not* listen for further changes, when there’s\n * noone connected anymore.\n */\n public onDisconnect = async ({ documentName, clientsCount }: onDisconnectPayload) => {\n // Do nothing, when other users are still connected to the document.\n if (clientsCount > 0) {\n return\n }\n\n // It was indeed the last connected user.\n this.documents.delete(documentName)\n\n // Time to end the subscription on the document channel.\n this.sub.punsubscribe(this.subKey(documentName), error => {\n if (error) {\n console.error(error)\n }\n })\n }\n\n /**\n * Kill the Redlock connection immediately.\n */\n async onDestroy() {\n this.redlock.qu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B;AACF;;;;"}
@@ -1,63 +1,177 @@
1
- import { RedisPersistence } from 'y-redis';
1
+ import RedisClient from 'ioredis';
2
+ import Redlock from 'redlock';
3
+ import { v4 } from 'uuid';
4
+ import { MessageReceiver, IncomingMessage, Debugger, OutgoingMessage } from '@hocuspocus/server';
5
+ import kleur from 'kleur';
2
6
 
3
7
  class Redis {
4
- /**
5
- * Constructor
6
- */
7
8
  constructor(configuration) {
8
- this.configuration = {};
9
- this.cluster = false;
9
+ /**
10
+ * Make sure to give that extension a higher priority, so
11
+ * the `onStoreDocument` hook is able to intercept the chain,
12
+ * before documents are stored to the database.
13
+ */
14
+ this.priority = 1000;
15
+ this.configuration = {
16
+ port: 6379,
17
+ host: '127.0.0.1',
18
+ prefix: 'hocuspocus',
19
+ identifier: `host-${v4()}`,
20
+ lockTimeout: 1000,
21
+ };
22
+ this.documents = new Map();
23
+ this.locks = new Map();
24
+ /**
25
+ * Handle incoming messages published on all subscribed document channels.
26
+ * Note that this will also include messages from ourselves as it is not possible
27
+ * in Redis to filter these.
28
+ */
29
+ this.handleIncomingMessage = async (channel, pattern, data) => {
30
+ const channelName = pattern.toString();
31
+ const [_, documentName, identifier] = channelName.split(':');
32
+ const document = this.documents.get(documentName);
33
+ if (identifier === this.configuration.identifier) {
34
+ return;
35
+ }
36
+ if (!document) {
37
+ return;
38
+ }
39
+ new MessageReceiver(new IncomingMessage(data), this.logger).apply(document, undefined, reply => {
40
+ return this.pub.publishBuffer(this.pubKey(document.name), Buffer.from(reply));
41
+ });
42
+ };
43
+ /**
44
+ * Make sure to *not* listen for further changes, when there’s
45
+ * noone connected anymore.
46
+ */
47
+ this.onDisconnect = async ({ documentName, clientsCount }) => {
48
+ // Do nothing, when other users are still connected to the document.
49
+ if (clientsCount > 0) {
50
+ return;
51
+ }
52
+ // It was indeed the last connected user.
53
+ this.documents.delete(documentName);
54
+ // Time to end the subscription on the document channel.
55
+ this.sub.punsubscribe(this.subKey(documentName), error => {
56
+ if (error) {
57
+ console.error(error);
58
+ }
59
+ });
60
+ };
61
+ const { port, host, options } = configuration;
10
62
  this.configuration = {
11
63
  ...this.configuration,
12
64
  ...configuration,
13
65
  };
66
+ this.pub = new RedisClient(port, host, options);
67
+ this.sub = new RedisClient(port, host, options);
68
+ this.sub.on('pmessageBuffer', this.handleIncomingMessage);
69
+ this.redlock = new Redlock([this.pub]);
70
+ // We’ll replace that in the onConfigure hook with the global instance.
71
+ this.logger = new Debugger();
72
+ }
73
+ async onConfigure({ instance }) {
74
+ this.logger = instance.debugger;
75
+ }
76
+ async onListen() {
77
+ console.warn(` ${kleur.yellow('[BREAKING CHANGE] Wait, the Redis extension got an overhaul. The new Redis extension doesn’t persist data, it only syncs data between instances. Use @hocuspocus/extension-database to store your documents. It works well with the Redis extension.')}`);
78
+ console.log();
14
79
  }
15
- /*
16
- * onLoadDocument hook
80
+ getKey(documentName) {
81
+ return `${this.configuration.prefix}:${documentName}`;
82
+ }
83
+ pubKey(documentName) {
84
+ return `${this.getKey(documentName)}:${this.configuration.identifier.replace(/:/g, '')}`;
85
+ }
86
+ subKey(documentName) {
87
+ return `${this.getKey(documentName)}:*`;
88
+ }
89
+ lockKey(documentName) {
90
+ return `${this.getKey(documentName)}:lock`;
91
+ }
92
+ /**
93
+ * Once a document is laoded, subscribe to the channel in Redis.
17
94
  */
18
- async onLoadDocument(data) {
19
- if (!this.persistence) {
20
- return;
21
- }
22
- // If another connection has already loaded this doc, return this one instead
23
- const binding = this.persistence.docs.get(data.documentName);
24
- if (binding) {
25
- return binding.doc;
26
- }
27
- await this.persistence.bindState(data.documentName, data.document).synced;
28
- }
29
- async onConnect(data) {
30
- // Bind to Redis already? Ok, no worries.
31
- if (this.persistence) {
32
- return;
33
- }
34
- this.persistence = new RedisPersistence(
35
- // @ts-ignore
36
- this.cluster
37
- ? { redisClusterOpts: this.configuration }
38
- : { redisOpts: this.configuration });
39
- }
40
- async onDisconnect(data) {
41
- // Not binded to Redis? Never mind!
42
- if (!this.persistence) {
43
- return;
44
- }
45
- // Still clients connected?
46
- if (data.clientsCount > 0) {
47
- return;
48
- }
49
- // Fine, let’s remove the binding …
50
- this.persistence.destroy();
51
- this.persistence = undefined;
95
+ async afterLoadDocument({ documentName, document }) {
96
+ this.documents.set(documentName, document);
97
+ return new Promise((resolve, reject) => {
98
+ // On document creation the node will connect to pub and sub channels
99
+ // for the document.
100
+ this.sub.psubscribe(this.subKey(documentName), async (error) => {
101
+ if (error) {
102
+ reject(error);
103
+ return;
104
+ }
105
+ this.publishFirstSyncStep(documentName, document);
106
+ this.requestAwarenessFromOtherInstances(documentName);
107
+ resolve(undefined);
108
+ });
109
+ });
52
110
  }
53
- }
54
-
55
- class RedisCluster extends Redis {
56
- constructor() {
57
- super(...arguments);
58
- this.cluster = true;
111
+ /**
112
+ * Publish the first sync step through Redis.
113
+ */
114
+ async publishFirstSyncStep(documentName, document) {
115
+ const syncMessage = new OutgoingMessage()
116
+ .createSyncMessage()
117
+ .writeFirstSyncStepFor(document);
118
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(syncMessage.toUint8Array()));
119
+ }
120
+ /**
121
+ * Let’s ask Redis who is connected already.
122
+ */
123
+ async requestAwarenessFromOtherInstances(documentName) {
124
+ const awarenessMessage = new OutgoingMessage()
125
+ .writeQueryAwareness();
126
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(awarenessMessage.toUint8Array()));
127
+ }
128
+ /**
129
+ * Before the document is stored, make sure to set a lock in Redis.
130
+ * That’s meant to avoid conflicts with other instances trying to store the document.
131
+ */
132
+ async onStoreDocument({ documentName }) {
133
+ // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,
134
+ // to avoid conflict with other instances storing the same document.
135
+ return new Promise((resolve, reject) => {
136
+ this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {
137
+ if (error || !lock) {
138
+ // Expected behavior: Could not acquire lock, another instance locked it already.
139
+ // No further `onStoreDocument` hooks will be executed.
140
+ reject();
141
+ return;
142
+ }
143
+ this.locks.set(this.lockKey(documentName), lock);
144
+ resolve(undefined);
145
+ });
146
+ });
147
+ }
148
+ /**
149
+ * Release the Redis lock, so other instances can store documents.
150
+ */
151
+ async afterStoreDocument({ documentName }) {
152
+ var _a;
153
+ (_a = this.locks.get(this.lockKey(documentName))) === null || _a === void 0 ? void 0 : _a.unlock().catch(() => {
154
+ // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.
155
+ // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)
156
+ }).finally(() => {
157
+ this.locks.delete(this.lockKey(documentName));
158
+ });
159
+ }
160
+ /**
161
+ * Handle awareness update messages received directly by this Hocuspocus instance.
162
+ */
163
+ async onAwarenessUpdate({ documentName, awareness }) {
164
+ const message = new OutgoingMessage()
165
+ .createAwarenessUpdateMessage(awareness);
166
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(message.toUint8Array()));
167
+ }
168
+ /**
169
+ * Kill the Redlock connection immediately.
170
+ */
171
+ async onDestroy() {
172
+ this.redlock.quit();
59
173
  }
60
174
  }
61
175
 
62
- export { Redis, RedisCluster };
176
+ export { Redis };
63
177
  //# sourceMappingURL=hocuspocus-redis.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-redis.esm.js","sources":["../src/Redis.ts","../src/RedisCluster.ts"],"sourcesContent":["import { RedisPersistence } from 'y-redis'\nimport {\n Extension,\n onConnectPayload,\n onLoadDocumentPayload,\n onDisconnectPayload,\n} from '@hocuspocus/server'\n\nexport interface Configuration {\n}\n\nexport class Redis implements Extension {\n\n configuration: Configuration = {}\n\n cluster = false\n\n persistence!: RedisPersistence | undefined\n\n /**\n * Constructor\n */\n constructor(configuration?: Partial<Configuration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n /*\n * onLoadDocument hook\n */\n async onLoadDocument(data: onLoadDocumentPayload) {\n if (!this.persistence) {\n return\n }\n\n // If another connection has already loaded this doc, return this one instead\n const binding = this.persistence.docs.get(data.documentName)\n\n if (binding) {\n return binding.doc\n }\n\n await this.persistence.bindState(data.documentName, data.document).synced\n }\n\n async onConnect(data: onConnectPayload) {\n // Bind to Redis already? Ok, no worries.\n if (this.persistence) {\n return\n }\n\n this.persistence = new RedisPersistence(\n // @ts-ignore\n this.cluster\n ? { redisClusterOpts: this.configuration }\n : { redisOpts: this.configuration },\n )\n\n }\n\n async onDisconnect(data: onDisconnectPayload) {\n // Not binded to Redis? Never mind!\n if (!this.persistence) {\n return\n }\n\n // Still clients connected?\n if (data.clientsCount > 0) {\n return\n }\n\n // Fine, let’s remove the binding …\n this.persistence.destroy()\n this.persistence = undefined\n\n }\n\n}\n","import { Redis } from './Redis'\n\nexport class RedisCluster extends Redis {\n\n cluster = true\n\n}\n"],"names":[],"mappings":";;MAWa,KAAK,CAAA;AAQhB;;AAEG;AACH,IAAA,WAAA,CAAY,aAAsC,EAAA;QATlD,IAAa,CAAA,aAAA,GAAkB,EAAE,CAAA;QAEjC,IAAO,CAAA,OAAA,GAAG,KAAK,CAAA;QAQb,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;AACrB,YAAA,GAAG,aAAa;SACjB,CAAA;KACF;AAED;;AAEG;IACH,MAAM,cAAc,CAAC,IAA2B,EAAA;AAC9C,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACrB,OAAM;AACP,SAAA;;AAGD,QAAA,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;AAE5D,QAAA,IAAI,OAAO,EAAE;YACX,OAAO,OAAO,CAAC,GAAG,CAAA;AACnB,SAAA;AAED,QAAA,MAAM,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAA;KAC1E;IAED,MAAM,SAAS,CAAC,IAAsB,EAAA;;QAEpC,IAAI,IAAI,CAAC,WAAW,EAAE;YACpB,OAAM;AACP,SAAA;AAED,QAAA,IAAI,CAAC,WAAW,GAAG,IAAI,gBAAgB;;AAErC,QAAA,IAAI,CAAC,OAAO;AACV,cAAE,EAAE,gBAAgB,EAAE,IAAI,CAAC,aAAa,EAAE;cACxC,EAAE,SAAS,EAAE,IAAI,CAAC,aAAa,EAAE,CACtC,CAAA;KAEF;IAED,MAAM,YAAY,CAAC,IAAyB,EAAA;;AAE1C,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACrB,OAAM;AACP,SAAA;;AAGD,QAAA,IAAI,IAAI,CAAC,YAAY,GAAG,CAAC,EAAE;YACzB,OAAM;AACP,SAAA;;AAGD,QAAA,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAA;AAC1B,QAAA,IAAI,CAAC,WAAW,GAAG,SAAS,CAAA;KAE7B;AAEF;;AC7EK,MAAO,YAAa,SAAQ,KAAK,CAAA;AAAvC,IAAA,WAAA,GAAA;;QAEE,IAAO,CAAA,OAAA,GAAG,IAAI,CAAA;KAEf;AAAA;;;;"}
1
+ {"version":3,"file":"hocuspocus-redis.esm.js","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient 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 MessageReceiver,\n Debugger,\n onConfigurePayload,\n} from '@hocuspocus/server'\nimport kleur from 'kleur'\n\nexport interface Configuration {\n /**\n * Redis port\n */\n port: number,\n /**\n * Redis host\n */\n host: string,\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?: RedisClient.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\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 }\n\n pub: RedisClient.Redis\n\n sub: RedisClient.Redis\n\n documents: Map<string, Document> = new Map()\n\n redlock: Redlock\n\n locks = new Map<string, Redlock.Lock>()\n\n logger: Debugger\n\n public constructor(configuration: Partial<Configuration>) {\n const { port, host, options } = configuration\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n\n this.pub = new RedisClient(port, host, options)\n\n this.sub = new RedisClient(port, host, options)\n this.sub.on('pmessageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub])\n\n // We’ll replace that in the onConfigure hook with the global instance.\n this.logger = new Debugger()\n }\n\n async onConfigure({ instance }: onConfigurePayload) {\n this.logger = instance.debugger\n }\n\n async onListen() {\n console.warn(` ${kleur.yellow('[BREAKING CHANGE] Wait, the Redis extension got an overhaul. The new Redis extension doesn’t persist data, it only syncs data between instances. Use @hocuspocus/extension-database to store your documents. It works well with the Redis extension.')}`)\n console.log()\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)}:${this.configuration.identifier.replace(/:/g, '')}`\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 /**\n * Once a document is laoded, subscribe to the channel in Redis.\n */\n public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {\n this.documents.set(documentName, document)\n\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.psubscribe(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()\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(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()\n .writeQueryAwareness()\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n Buffer.from(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 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 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 }: 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\n /**\n * Handle awareness update messages received directly by this Hocuspocus instance.\n */\n async onAwarenessUpdate({ documentName, awareness }: onAwarenessUpdatePayload) {\n const message = new OutgoingMessage()\n .createAwarenessUpdateMessage(awareness)\n\n return this.pub.publishBuffer(\n this.pubKey(documentName),\n Buffer.from(message.toUint8Array()),\n )\n }\n\n /**\n * Handle incoming messages published on all 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, pattern: Buffer, data: Buffer) => {\n const channelName = pattern.toString()\n const [_, documentName, identifier] = channelName.split(':')\n const document = this.documents.get(documentName)\n\n if (identifier === this.configuration.identifier) {\n return\n }\n\n if (!document) {\n return\n }\n\n new MessageReceiver(\n new IncomingMessage(data),\n this.logger,\n ).apply(document, undefined, reply => {\n return this.pub.publishBuffer(\n this.pubKey(document.name),\n Buffer.from(reply),\n )\n })\n }\n\n /**\n * Make sure to *not* listen for further changes, when there’s\n * noone connected anymore.\n */\n public onDisconnect = async ({ documentName, clientsCount }: onDisconnectPayload) => {\n // Do nothing, when other users are still connected to the document.\n if (clientsCount > 0) {\n return\n }\n\n // It was indeed the last connected user.\n this.documents.delete(documentName)\n\n // Time to end the subscription on the document channel.\n this.sub.punsubscribe(this.subKey(documentName), error => {\n if (error) {\n console.error(error)\n }\n })\n }\n\n /**\n * Kill the Redlock connection immediately.\n */\n async onDestroy() {\n this.redlock.quit()\n }\n}\n"],"names":["uuid"],"mappings":";;;;;;MAiDa,KAAK,CAAA;AA4BhB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AA3BxD;;;;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;SAClB,CAAA;AAMD,QAA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CAAC,YAAY,CAAC,CAAC,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,MAAM,GAC/C,KAAK,CAAC,MAAK;;;AAGZ,SAAC,CACA,CAAA,OAAO,CAAC,MAAK;AACZ,YAAA,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAA;AAC/C,SAAC,CAAC,CAAA;KACL;AAED;;AAEG;AACH,IAAA,MAAM,iBAAiB,CAAC,EAAE,YAAY,EAAE,SAAS,EAA4B,EAAA;AAC3E,QAAA,MAAM,OAAO,GAAG,IAAI,eAAe,EAAE;aAClC,4BAA4B,CAAC,SAAS,CAAC,CAAA;QAE1C,OAAO,IAAI,CAAC,GAAG,CAAC,aAAa,CAC3B,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,EACzB,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CACpC,CAAA;KACF;AAoDD;;AAEG;AACH,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;KACpB;AACF;;;;"}
@@ -1,16 +1,94 @@
1
- import { RedisPersistence } from 'y-redis';
2
- import { Extension, onConnectPayload, onLoadDocumentPayload, onDisconnectPayload } from '@hocuspocus/server';
1
+ import RedisClient from 'ioredis';
2
+ import Redlock from 'redlock';
3
+ import { Document, Extension, afterLoadDocumentPayload, afterStoreDocumentPayload, onDisconnectPayload, onStoreDocumentPayload, onAwarenessUpdatePayload, Debugger, onConfigurePayload } from '@hocuspocus/server';
3
4
  export interface Configuration {
5
+ /**
6
+ * Redis port
7
+ */
8
+ port: number;
9
+ /**
10
+ * Redis host
11
+ */
12
+ host: string;
13
+ /**
14
+ * Options passed directly to Redis constructor
15
+ *
16
+ * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
17
+ */
18
+ options?: RedisClient.RedisOptions;
19
+ /**
20
+ * An unique instance name, required to filter messages in Redis.
21
+ * If none is provided an unique id is generated.
22
+ */
23
+ identifier: string;
24
+ /**
25
+ * Namespace for Redis keys, if none is provided 'hocuspocus' is used
26
+ */
27
+ prefix: string;
28
+ /**
29
+ * The maximum time for the Redis lock in ms (in case it can’t be released).
30
+ */
31
+ lockTimeout: number;
4
32
  }
5
33
  export declare class Redis implements Extension {
34
+ /**
35
+ * Make sure to give that extension a higher priority, so
36
+ * the `onStoreDocument` hook is able to intercept the chain,
37
+ * before documents are stored to the database.
38
+ */
39
+ priority: number;
6
40
  configuration: Configuration;
7
- cluster: boolean;
8
- persistence: RedisPersistence | undefined;
41
+ pub: RedisClient.Redis;
42
+ sub: RedisClient.Redis;
43
+ documents: Map<string, Document>;
44
+ redlock: Redlock;
45
+ locks: Map<string, Redlock.Lock>;
46
+ logger: Debugger;
47
+ constructor(configuration: Partial<Configuration>);
48
+ onConfigure({ instance }: onConfigurePayload): Promise<void>;
49
+ onListen(): Promise<void>;
50
+ private getKey;
51
+ private pubKey;
52
+ private subKey;
53
+ private lockKey;
54
+ /**
55
+ * Once a document is laoded, subscribe to the channel in Redis.
56
+ */
57
+ afterLoadDocument({ documentName, document }: afterLoadDocumentPayload): Promise<unknown>;
58
+ /**
59
+ * Publish the first sync step through Redis.
60
+ */
61
+ private publishFirstSyncStep;
62
+ /**
63
+ * Let’s ask Redis who is connected already.
64
+ */
65
+ private requestAwarenessFromOtherInstances;
66
+ /**
67
+ * Before the document is stored, make sure to set a lock in Redis.
68
+ * That’s meant to avoid conflicts with other instances trying to store the document.
69
+ */
70
+ onStoreDocument({ documentName }: onStoreDocumentPayload): Promise<unknown>;
71
+ /**
72
+ * Release the Redis lock, so other instances can store documents.
73
+ */
74
+ afterStoreDocument({ documentName }: afterStoreDocumentPayload): Promise<void>;
75
+ /**
76
+ * Handle awareness update messages received directly by this Hocuspocus instance.
77
+ */
78
+ onAwarenessUpdate({ documentName, awareness }: onAwarenessUpdatePayload): Promise<number>;
79
+ /**
80
+ * Handle incoming messages published on all subscribed document channels.
81
+ * Note that this will also include messages from ourselves as it is not possible
82
+ * in Redis to filter these.
83
+ */
84
+ private handleIncomingMessage;
85
+ /**
86
+ * Make sure to *not* listen for further changes, when there’s
87
+ * noone connected anymore.
88
+ */
89
+ onDisconnect: ({ documentName, clientsCount }: onDisconnectPayload) => Promise<void>;
9
90
  /**
10
- * Constructor
91
+ * Kill the Redlock connection immediately.
11
92
  */
12
- constructor(configuration?: Partial<Configuration>);
13
- onLoadDocument(data: onLoadDocumentPayload): Promise<import("yjs").Doc | undefined>;
14
- onConnect(data: onConnectPayload): Promise<void>;
15
- onDisconnect(data: onDisconnectPayload): Promise<void>;
93
+ onDestroy(): Promise<void>;
16
94
  }
@@ -1,2 +1 @@
1
1
  export * from './Redis';
2
- export * from './RedisCluster';
@@ -1,9 +1,8 @@
1
- export * from './Hocuspocus';
2
1
  export * from './Connection';
2
+ export * from './Debugger';
3
3
  export * from './Document';
4
+ export * from './Hocuspocus';
4
5
  export * from './IncomingMessage';
6
+ export * from './MessageReceiver';
5
7
  export * from './OutgoingMessage';
6
8
  export * from './types';
7
- export * from './MessageReceiver';
8
- export * from './Document';
9
- export * from './Connection';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hocuspocus/extension-redis",
3
- "version": "1.0.0-alpha.61",
4
- "description": "hocuspocus persistence driver for Redis",
3
+ "version": "1.0.0-alpha.62",
4
+ "description": "Scale Hocuspocus horizontally with Redis",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
7
7
  "hocuspocus",
@@ -26,10 +26,20 @@
26
26
  "src",
27
27
  "dist"
28
28
  ],
29
+ "devDependencies": {
30
+ "@types/ioredis": "^4.28.7",
31
+ "@types/lodash.debounce": "^4.0.6",
32
+ "@types/redlock": "^4.0.3"
33
+ },
29
34
  "dependencies": {
30
- "@hocuspocus/server": "^1.0.0-alpha.98",
31
- "y-redis": "^1.0.3",
32
- "yjs": "^13.5.29"
35
+ "@hocuspocus/server": "^1.0.0-alpha.99",
36
+ "ioredis": "^4.28.2",
37
+ "kleur": "^4.1.4",
38
+ "lodash.debounce": "^4.0.8",
39
+ "redlock": "^4.2.0",
40
+ "uuid": "^8.3.2",
41
+ "y-protocols": "^1.0.5",
42
+ "yjs": "^13.5.23"
33
43
  },
34
- "gitHead": "1fc2a6cff1b5fd626b8dd7c486755111965da20d"
44
+ "gitHead": "e26a5eeaa9278b9587d4b475cec53bf14bc569b9"
35
45
  }
package/src/Redis.ts CHANGED
@@ -1,80 +1,274 @@
1
- import { RedisPersistence } from 'y-redis'
1
+ import RedisClient from 'ioredis'
2
+ import Redlock from 'redlock'
3
+ import { v4 as uuid } from 'uuid'
2
4
  import {
5
+ IncomingMessage,
6
+ OutgoingMessage,
7
+ Document,
3
8
  Extension,
4
- onConnectPayload,
5
- onLoadDocumentPayload,
9
+ afterLoadDocumentPayload,
10
+ afterStoreDocumentPayload,
6
11
  onDisconnectPayload,
12
+ onStoreDocumentPayload,
13
+ onAwarenessUpdatePayload,
14
+ MessageReceiver,
15
+ Debugger,
16
+ onConfigurePayload,
7
17
  } from '@hocuspocus/server'
18
+ import kleur from 'kleur'
8
19
 
9
20
  export interface Configuration {
21
+ /**
22
+ * Redis port
23
+ */
24
+ port: number,
25
+ /**
26
+ * Redis host
27
+ */
28
+ host: string,
29
+ /**
30
+ * Options passed directly to Redis constructor
31
+ *
32
+ * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
33
+ */
34
+ options?: RedisClient.RedisOptions,
35
+ /**
36
+ * An unique instance name, required to filter messages in Redis.
37
+ * If none is provided an unique id is generated.
38
+ */
39
+ identifier: string,
40
+ /**
41
+ * Namespace for Redis keys, if none is provided 'hocuspocus' is used
42
+ */
43
+ prefix: string,
44
+ /**
45
+ * The maximum time for the Redis lock in ms (in case it can’t be released).
46
+ */
47
+ lockTimeout: number,
10
48
  }
11
49
 
12
50
  export class Redis implements Extension {
51
+ /**
52
+ * Make sure to give that extension a higher priority, so
53
+ * the `onStoreDocument` hook is able to intercept the chain,
54
+ * before documents are stored to the database.
55
+ */
56
+ priority = 1000
57
+
58
+ configuration: Configuration = {
59
+ port: 6379,
60
+ host: '127.0.0.1',
61
+ prefix: 'hocuspocus',
62
+ identifier: `host-${uuid()}`,
63
+ lockTimeout: 1000,
64
+ }
13
65
 
14
- configuration: Configuration = {}
66
+ pub: RedisClient.Redis
15
67
 
16
- cluster = false
68
+ sub: RedisClient.Redis
17
69
 
18
- persistence!: RedisPersistence | undefined
70
+ documents: Map<string, Document> = new Map()
19
71
 
20
- /**
21
- * Constructor
22
- */
23
- constructor(configuration?: Partial<Configuration>) {
72
+ redlock: Redlock
73
+
74
+ locks = new Map<string, Redlock.Lock>()
75
+
76
+ logger: Debugger
77
+
78
+ public constructor(configuration: Partial<Configuration>) {
79
+ const { port, host, options } = configuration
24
80
  this.configuration = {
25
81
  ...this.configuration,
26
82
  ...configuration,
27
83
  }
84
+
85
+ this.pub = new RedisClient(port, host, options)
86
+
87
+ this.sub = new RedisClient(port, host, options)
88
+ this.sub.on('pmessageBuffer', this.handleIncomingMessage)
89
+
90
+ this.redlock = new Redlock([this.pub])
91
+
92
+ // We’ll replace that in the onConfigure hook with the global instance.
93
+ this.logger = new Debugger()
28
94
  }
29
95
 
30
- /*
31
- * onLoadDocument hook
96
+ async onConfigure({ instance }: onConfigurePayload) {
97
+ this.logger = instance.debugger
98
+ }
99
+
100
+ async onListen() {
101
+ console.warn(` ${kleur.yellow('[BREAKING CHANGE] Wait, the Redis extension got an overhaul. The new Redis extension doesn’t persist data, it only syncs data between instances. Use @hocuspocus/extension-database to store your documents. It works well with the Redis extension.')}`)
102
+ console.log()
103
+ }
104
+
105
+ private getKey(documentName: string) {
106
+ return `${this.configuration.prefix}:${documentName}`
107
+ }
108
+
109
+ private pubKey(documentName: string) {
110
+ return `${this.getKey(documentName)}:${this.configuration.identifier.replace(/:/g, '')}`
111
+ }
112
+
113
+ private subKey(documentName: string) {
114
+ return `${this.getKey(documentName)}:*`
115
+ }
116
+
117
+ private lockKey(documentName: string) {
118
+ return `${this.getKey(documentName)}:lock`
119
+ }
120
+
121
+ /**
122
+ * Once a document is laoded, subscribe to the channel in Redis.
32
123
  */
33
- async onLoadDocument(data: onLoadDocumentPayload) {
34
- if (!this.persistence) {
35
- return
36
- }
124
+ public async afterLoadDocument({ documentName, document }: afterLoadDocumentPayload) {
125
+ this.documents.set(documentName, document)
37
126
 
38
- // If another connection has already loaded this doc, return this one instead
39
- const binding = this.persistence.docs.get(data.documentName)
127
+ return new Promise((resolve, reject) => {
128
+ // On document creation the node will connect to pub and sub channels
129
+ // for the document.
130
+ this.sub.psubscribe(this.subKey(documentName), async error => {
131
+ if (error) {
132
+ reject(error)
133
+ return
134
+ }
40
135
 
41
- if (binding) {
42
- return binding.doc
43
- }
136
+ this.publishFirstSyncStep(documentName, document)
137
+ this.requestAwarenessFromOtherInstances(documentName)
44
138
 
45
- await this.persistence.bindState(data.documentName, data.document).synced
139
+ resolve(undefined)
140
+ })
141
+ })
46
142
  }
47
143
 
48
- async onConnect(data: onConnectPayload) {
49
- // Bind to Redis already? Ok, no worries.
50
- if (this.persistence) {
51
- return
52
- }
144
+ /**
145
+ * Publish the first sync step through Redis.
146
+ */
147
+ private async publishFirstSyncStep(documentName: string, document: Document) {
148
+ const syncMessage = new OutgoingMessage()
149
+ .createSyncMessage()
150
+ .writeFirstSyncStepFor(document)
151
+
152
+ return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(syncMessage.toUint8Array()))
153
+ }
53
154
 
54
- this.persistence = new RedisPersistence(
55
- // @ts-ignore
56
- this.cluster
57
- ? { redisClusterOpts: this.configuration }
58
- : { redisOpts: this.configuration },
155
+ /**
156
+ * Let’s ask Redis who is connected already.
157
+ */
158
+ private async requestAwarenessFromOtherInstances(documentName: string) {
159
+ const awarenessMessage = new OutgoingMessage()
160
+ .writeQueryAwareness()
161
+
162
+ return this.pub.publishBuffer(
163
+ this.pubKey(documentName),
164
+ Buffer.from(awarenessMessage.toUint8Array()),
59
165
  )
166
+ }
167
+
168
+ /**
169
+ * Before the document is stored, make sure to set a lock in Redis.
170
+ * That’s meant to avoid conflicts with other instances trying to store the document.
171
+ */
172
+ async onStoreDocument({ documentName }: onStoreDocumentPayload) {
173
+ // Attempt to acquire a lock and read lastReceivedTimestamp from Redis,
174
+ // to avoid conflict with other instances storing the same document.
175
+ return new Promise((resolve, reject) => {
176
+ this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {
177
+ if (error || !lock) {
178
+ // Expected behavior: Could not acquire lock, another instance locked it already.
179
+ // No further `onStoreDocument` hooks will be executed.
180
+ reject()
181
+ return
182
+ }
183
+
184
+ this.locks.set(this.lockKey(documentName), lock)
185
+
186
+ resolve(undefined)
187
+ })
188
+ })
189
+ }
190
+
191
+ /**
192
+ * Release the Redis lock, so other instances can store documents.
193
+ */
194
+ async afterStoreDocument({ documentName }: afterStoreDocumentPayload) {
195
+ this.locks.get(this.lockKey(documentName))?.unlock()
196
+ .catch(() => {
197
+ // Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.
198
+ // console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)
199
+ })
200
+ .finally(() => {
201
+ this.locks.delete(this.lockKey(documentName))
202
+ })
203
+ }
204
+
205
+ /**
206
+ * Handle awareness update messages received directly by this Hocuspocus instance.
207
+ */
208
+ async onAwarenessUpdate({ documentName, awareness }: onAwarenessUpdatePayload) {
209
+ const message = new OutgoingMessage()
210
+ .createAwarenessUpdateMessage(awareness)
60
211
 
212
+ return this.pub.publishBuffer(
213
+ this.pubKey(documentName),
214
+ Buffer.from(message.toUint8Array()),
215
+ )
61
216
  }
62
217
 
63
- async onDisconnect(data: onDisconnectPayload) {
64
- // Not binded to Redis? Never mind!
65
- if (!this.persistence) {
218
+ /**
219
+ * Handle incoming messages published on all subscribed document channels.
220
+ * Note that this will also include messages from ourselves as it is not possible
221
+ * in Redis to filter these.
222
+ */
223
+ private handleIncomingMessage = async (channel: Buffer, pattern: Buffer, data: Buffer) => {
224
+ const channelName = pattern.toString()
225
+ const [_, documentName, identifier] = channelName.split(':')
226
+ const document = this.documents.get(documentName)
227
+
228
+ if (identifier === this.configuration.identifier) {
66
229
  return
67
230
  }
68
231
 
69
- // Still clients connected?
70
- if (data.clientsCount > 0) {
232
+ if (!document) {
71
233
  return
72
234
  }
73
235
 
74
- // Fine, let’s remove the binding …
75
- this.persistence.destroy()
76
- this.persistence = undefined
236
+ new MessageReceiver(
237
+ new IncomingMessage(data),
238
+ this.logger,
239
+ ).apply(document, undefined, reply => {
240
+ return this.pub.publishBuffer(
241
+ this.pubKey(document.name),
242
+ Buffer.from(reply),
243
+ )
244
+ })
245
+ }
77
246
 
247
+ /**
248
+ * Make sure to *not* listen for further changes, when there’s
249
+ * noone connected anymore.
250
+ */
251
+ public onDisconnect = async ({ documentName, clientsCount }: onDisconnectPayload) => {
252
+ // Do nothing, when other users are still connected to the document.
253
+ if (clientsCount > 0) {
254
+ return
255
+ }
256
+
257
+ // It was indeed the last connected user.
258
+ this.documents.delete(documentName)
259
+
260
+ // Time to end the subscription on the document channel.
261
+ this.sub.punsubscribe(this.subKey(documentName), error => {
262
+ if (error) {
263
+ console.error(error)
264
+ }
265
+ })
78
266
  }
79
267
 
268
+ /**
269
+ * Kill the Redlock connection immediately.
270
+ */
271
+ async onDestroy() {
272
+ this.redlock.quit()
273
+ }
80
274
  }
package/src/index.ts CHANGED
@@ -1,2 +1 @@
1
1
  export * from './Redis'
2
- export * from './RedisCluster'
@@ -1,4 +0,0 @@
1
- import { Redis } from './Redis';
2
- export declare class RedisCluster extends Redis {
3
- cluster: boolean;
4
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,7 +0,0 @@
1
- import { Redis } from './Redis'
2
-
3
- export class RedisCluster extends Redis {
4
-
5
- cluster = true
6
-
7
- }