@hocuspocus/extension-redis 2.1.0-alpha.0 → 2.2.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 +201 -153
- package/dist/hocuspocus-redis.cjs.map +1 -1
- package/dist/hocuspocus-redis.esm.js +200 -153
- package/dist/hocuspocus-redis.esm.js.map +1 -1
- package/dist/packages/common/src/CloseEvents.d.ts +29 -29
- package/dist/packages/common/src/auth.d.ts +6 -6
- package/dist/packages/common/src/awarenessStatesToArray.d.ts +3 -3
- package/dist/packages/common/src/index.d.ts +4 -4
- package/dist/packages/common/src/types.d.ts +10 -10
- package/dist/packages/extension-database/src/Database.d.ts +30 -30
- package/dist/packages/extension-database/src/index.d.ts +1 -1
- package/dist/packages/extension-logger/src/Logger.d.ts +67 -67
- package/dist/packages/extension-logger/src/index.d.ts +1 -1
- package/dist/packages/extension-redis/src/Redis.d.ts +116 -95
- package/dist/packages/extension-redis/src/index.d.ts +1 -1
- package/dist/packages/extension-sqlite/src/SQLite.d.ts +26 -26
- package/dist/packages/extension-sqlite/src/index.d.ts +1 -1
- package/dist/packages/extension-throttle/src/index.d.ts +31 -31
- package/dist/packages/extension-webhook/src/index.d.ts +57 -57
- package/dist/packages/provider/src/EventEmitter.d.ts +9 -9
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +110 -110
- package/dist/packages/provider/src/HocuspocusProviderWebsocket.d.ts +115 -115
- package/dist/packages/provider/src/IncomingMessage.d.ts +16 -16
- package/dist/packages/provider/src/MessageReceiver.d.ts +13 -13
- package/dist/packages/provider/src/MessageSender.d.ts +10 -10
- package/dist/packages/provider/src/OutgoingMessage.d.ts +9 -9
- package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +7 -7
- package/dist/packages/provider/src/OutgoingMessages/AwarenessMessage.d.ts +8 -8
- package/dist/packages/provider/src/OutgoingMessages/CloseMessage.d.ts +8 -8
- package/dist/packages/provider/src/OutgoingMessages/QueryAwarenessMessage.d.ts +8 -8
- package/dist/packages/provider/src/OutgoingMessages/StatelessMessage.d.ts +7 -7
- package/dist/packages/provider/src/OutgoingMessages/SyncStepOneMessage.d.ts +8 -8
- package/dist/packages/provider/src/OutgoingMessages/SyncStepTwoMessage.d.ts +8 -8
- package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +7 -7
- package/dist/packages/provider/src/TiptapCollabProvider.d.ts +11 -11
- package/dist/packages/provider/src/TiptapCollabProviderWebsocket.d.ts +11 -11
- package/dist/packages/provider/src/index.d.ts +5 -5
- package/dist/packages/provider/src/types.d.ts +84 -84
- package/dist/packages/server/src/ClientConnection.d.ts +56 -0
- package/dist/packages/server/src/Connection.d.ts +71 -71
- package/dist/packages/server/src/Debugger.d.ts +14 -14
- package/dist/packages/server/src/DirectConnection.d.ts +14 -0
- package/dist/packages/server/src/Document.d.ts +91 -88
- package/dist/packages/server/src/Hocuspocus.d.ts +103 -108
- package/dist/packages/server/src/IncomingMessage.d.ts +21 -21
- package/dist/packages/server/src/MessageReceiver.d.ts +12 -12
- package/dist/packages/server/src/OutgoingMessage.d.ts +20 -20
- package/dist/packages/server/src/index.d.ts +8 -8
- package/dist/packages/server/src/types.d.ts +270 -264
- package/dist/packages/server/src/util/getParameters.d.ts +8 -0
- package/dist/packages/transformer/src/Prosemirror.d.ts +11 -11
- package/dist/packages/transformer/src/Tiptap.d.ts +10 -10
- package/dist/packages/transformer/src/index.d.ts +3 -3
- package/dist/packages/transformer/src/types.d.ts +5 -5
- package/dist/playground/backend/src/default.d.ts +1 -1
- package/dist/playground/backend/src/express.d.ts +1 -1
- package/dist/playground/backend/src/koa.d.ts +1 -1
- package/dist/playground/backend/src/load-document.d.ts +1 -1
- package/dist/playground/backend/src/redis.d.ts +1 -1
- package/dist/playground/backend/src/slow.d.ts +1 -1
- package/dist/playground/backend/src/tiptapcollab.d.ts +1 -1
- package/dist/playground/backend/src/webhook.d.ts +1 -1
- package/dist/playground/frontend/src/main.d.ts +1 -1
- package/dist/playground/frontend/vite.config.d.ts +2 -2
- package/dist/tests/extension-database/fetch.d.ts +1 -1
- package/dist/tests/extension-logger/onListen.d.ts +1 -1
- package/dist/tests/extension-redis/closeConnections.d.ts +1 -1
- package/dist/tests/extension-redis/getConnectionCount.d.ts +1 -1
- package/dist/tests/extension-redis/getDocumentsCount.d.ts +1 -1
- package/dist/tests/extension-redis/onAwarenessChange.d.ts +1 -1
- package/dist/tests/extension-redis/onChange.d.ts +1 -1
- package/dist/tests/extension-redis/onStateless.d.ts +1 -1
- package/dist/tests/extension-redis/onStoreDocument.d.ts +1 -1
- package/dist/tests/extension-throttle/banning.d.ts +1 -1
- package/dist/tests/extension-throttle/configuration.d.ts +1 -1
- package/dist/tests/provider/observe.d.ts +1 -1
- package/dist/tests/provider/observeDeep.d.ts +1 -1
- package/dist/tests/provider/onAuthenticated.d.ts +1 -1
- package/dist/tests/provider/onAuthenticationFailed.d.ts +1 -1
- package/dist/tests/provider/onAwarenessChange.d.ts +1 -1
- package/dist/tests/provider/onAwarenessUpdate.d.ts +1 -1
- package/dist/tests/provider/onClose.d.ts +1 -1
- package/dist/tests/provider/onConnect.d.ts +1 -1
- package/dist/tests/provider/onDisconnect.d.ts +1 -1
- package/dist/tests/provider/onMessage.d.ts +1 -1
- package/dist/tests/provider/onOpen.d.ts +1 -1
- package/dist/tests/provider/onStateless.d.ts +1 -1
- package/dist/tests/provider/onSynced.d.ts +1 -1
- package/dist/tests/providerwebsocket/configuration.d.ts +1 -1
- package/dist/tests/server/address.d.ts +1 -1
- package/dist/tests/server/afterStoreDocument.d.ts +1 -1
- package/dist/tests/server/beforeBroadcastStateless.d.ts +1 -1
- package/dist/tests/server/beforeHandleMessage.d.ts +1 -1
- package/dist/tests/server/closeConnections.d.ts +1 -1
- package/dist/tests/server/getConnectionsCount.d.ts +1 -1
- package/dist/tests/server/getDocumentsCount.d.ts +1 -1
- package/dist/tests/server/getMessageLogs.d.ts +1 -1
- package/dist/tests/server/listen.d.ts +1 -1
- package/dist/tests/server/onAuthenticate.d.ts +1 -1
- package/dist/tests/server/onAwarenessUpdate.d.ts +1 -1
- package/dist/tests/server/onChange.d.ts +1 -1
- package/dist/tests/server/onClose.d.ts +1 -1
- package/dist/tests/server/onConfigure.d.ts +1 -1
- package/dist/tests/server/onConnect.d.ts +1 -1
- package/dist/tests/server/onDestroy.d.ts +1 -1
- package/dist/tests/server/onDisconnect.d.ts +1 -1
- package/dist/tests/server/onListen.d.ts +1 -1
- package/dist/tests/server/onLoadDocument.d.ts +1 -1
- package/dist/tests/server/onRequest.d.ts +1 -1
- package/dist/tests/server/onStateless.d.ts +1 -1
- package/dist/tests/server/onStoreDocument.d.ts +1 -1
- package/dist/tests/server/onUpgrade.d.ts +1 -1
- package/dist/tests/server/openDirectConnection.d.ts +1 -0
- package/dist/tests/server/requiresAuthentication.d.ts +1 -1
- package/dist/tests/server/websocketError.d.ts +1 -1
- package/dist/tests/transformer/TiptapTransformer.d.ts +1 -1
- package/dist/tests/utils/createDirectory.d.ts +1 -1
- package/dist/tests/utils/flushRedis.d.ts +1 -1
- package/dist/tests/utils/index.d.ts +9 -9
- package/dist/tests/utils/newHocuspocus.d.ts +2 -2
- package/dist/tests/utils/newHocuspocusProvider.d.ts +3 -3
- package/dist/tests/utils/newHocuspocusProviderWebsocket.d.ts +3 -3
- package/dist/tests/utils/randomInteger.d.ts +1 -1
- package/dist/tests/utils/redisConnectionSettings.d.ts +4 -4
- package/dist/tests/utils/removeDirectory.d.ts +1 -1
- package/dist/tests/utils/retryableAssertion.d.ts +2 -2
- package/dist/tests/utils/sleep.d.ts +1 -1
- package/package.json +9 -5
- package/src/Redis.ts +74 -13
- package/src/index.ts +1 -1
|
@@ -1,160 +1,207 @@
|
|
|
1
1
|
import RedisClient from 'ioredis';
|
|
2
|
+
import Redlock from 'redlock';
|
|
2
3
|
import { v4 } from 'uuid';
|
|
3
4
|
import { IncomingMessage, MessageReceiver, Debugger, OutgoingMessage } from '@hocuspocus/server';
|
|
4
5
|
|
|
5
|
-
class Redis {
|
|
6
|
-
constructor(configuration) {
|
|
7
|
-
/**
|
|
8
|
-
* Make sure to give that extension a higher priority, so
|
|
9
|
-
* the `onStoreDocument` hook is able to intercept the chain,
|
|
10
|
-
* before documents are stored to the database.
|
|
11
|
-
*/
|
|
12
|
-
this.priority = 1000;
|
|
13
|
-
this.configuration = {
|
|
14
|
-
port: 6379,
|
|
15
|
-
host: '127.0.0.1',
|
|
16
|
-
prefix: 'hocuspocus',
|
|
17
|
-
identifier: `host-${v4()}`,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
message.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this.pub =
|
|
76
|
-
this.sub =
|
|
77
|
-
}
|
|
78
|
-
else if (
|
|
79
|
-
this.pub =
|
|
80
|
-
this.sub =
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
this.pub = new RedisClient(
|
|
84
|
-
this.sub = new RedisClient(
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this.
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return `${this.
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return `${this.getKey(documentName)}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
6
|
+
class Redis {
|
|
7
|
+
constructor(configuration) {
|
|
8
|
+
/**
|
|
9
|
+
* Make sure to give that extension a higher priority, so
|
|
10
|
+
* the `onStoreDocument` hook is able to intercept the chain,
|
|
11
|
+
* before documents are stored to the database.
|
|
12
|
+
*/
|
|
13
|
+
this.priority = 1000;
|
|
14
|
+
this.configuration = {
|
|
15
|
+
port: 6379,
|
|
16
|
+
host: '127.0.0.1',
|
|
17
|
+
prefix: 'hocuspocus',
|
|
18
|
+
identifier: `host-${v4()}`,
|
|
19
|
+
lockTimeout: 1000,
|
|
20
|
+
disconnectDelay: 1000,
|
|
21
|
+
};
|
|
22
|
+
this.locks = new Map();
|
|
23
|
+
/**
|
|
24
|
+
* Handle incoming messages published on all subscribed document channels.
|
|
25
|
+
* Note that this will also include messages from ourselves as it is not possible
|
|
26
|
+
* in Redis to filter these.
|
|
27
|
+
*/
|
|
28
|
+
this.handleIncomingMessage = async (channel, pattern, data) => {
|
|
29
|
+
const message = new IncomingMessage(data);
|
|
30
|
+
// we don't need the documentName from the message, we are just taking it from the redis channelName.
|
|
31
|
+
// we have to immediately write it back to the encoder though, to make sure the structure of the message is correct
|
|
32
|
+
message.writeVarString(message.readVarString());
|
|
33
|
+
const channelName = pattern.toString();
|
|
34
|
+
const [_, documentName, identifier] = channelName.split(':');
|
|
35
|
+
const document = this.instance.documents.get(documentName);
|
|
36
|
+
if (identifier === this.configuration.identifier) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!document) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
new MessageReceiver(message, this.logger).apply(document, undefined, reply => {
|
|
43
|
+
return this.pub.publishBuffer(this.pubKey(document.name), Buffer.from(reply));
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Make sure to *not* listen for further changes, when there’s
|
|
48
|
+
* noone connected anymore.
|
|
49
|
+
*/
|
|
50
|
+
this.onDisconnect = async ({ documentName }) => {
|
|
51
|
+
const disconnect = () => {
|
|
52
|
+
const document = this.instance.documents.get(documentName);
|
|
53
|
+
// Do nothing, when other users are still connected to the document.
|
|
54
|
+
if (document && document.getConnectionsCount() > 0) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// Time to end the subscription on the document channel.
|
|
58
|
+
this.sub.punsubscribe(this.subKey(documentName), (error) => {
|
|
59
|
+
if (error) {
|
|
60
|
+
console.error(error);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
// Delay the disconnect procedure to allow last minute syncs to happen
|
|
65
|
+
setTimeout(disconnect, this.configuration.disconnectDelay);
|
|
66
|
+
};
|
|
67
|
+
this.configuration = {
|
|
68
|
+
...this.configuration,
|
|
69
|
+
...configuration,
|
|
70
|
+
};
|
|
71
|
+
// We’ll replace that in the onConfigure hook with the global instance.
|
|
72
|
+
this.logger = new Debugger();
|
|
73
|
+
// Create Redis instance
|
|
74
|
+
const { port, host, options, nodes, redis, createClient, } = this.configuration;
|
|
75
|
+
if (typeof createClient === 'function') {
|
|
76
|
+
this.pub = createClient();
|
|
77
|
+
this.sub = createClient();
|
|
78
|
+
}
|
|
79
|
+
else if (redis) {
|
|
80
|
+
this.pub = redis.duplicate();
|
|
81
|
+
this.sub = redis.duplicate();
|
|
82
|
+
}
|
|
83
|
+
else if (nodes && nodes.length > 0) {
|
|
84
|
+
this.pub = new RedisClient.Cluster(nodes, options);
|
|
85
|
+
this.sub = new RedisClient.Cluster(nodes, options);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.pub = new RedisClient(port, host, options);
|
|
89
|
+
this.sub = new RedisClient(port, host, options);
|
|
90
|
+
}
|
|
91
|
+
this.sub.on('pmessageBuffer', this.handleIncomingMessage);
|
|
92
|
+
this.redlock = new Redlock([this.pub]);
|
|
93
|
+
}
|
|
94
|
+
async onConfigure({ instance }) {
|
|
95
|
+
this.logger = instance.debugger;
|
|
96
|
+
this.instance = instance;
|
|
97
|
+
}
|
|
98
|
+
getKey(documentName) {
|
|
99
|
+
return `${this.configuration.prefix}:${documentName}`;
|
|
100
|
+
}
|
|
101
|
+
pubKey(documentName) {
|
|
102
|
+
return `${this.getKey(documentName)}:${this.configuration.identifier.replace(/:/g, '')}`;
|
|
103
|
+
}
|
|
104
|
+
subKey(documentName) {
|
|
105
|
+
return `${this.getKey(documentName)}:*`;
|
|
106
|
+
}
|
|
107
|
+
lockKey(documentName) {
|
|
108
|
+
return `${this.getKey(documentName)}:lock`;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Once a document is laoded, subscribe to the channel in Redis.
|
|
112
|
+
*/
|
|
113
|
+
async afterLoadDocument({ documentName, document }) {
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
// On document creation the node will connect to pub and sub channels
|
|
116
|
+
// for the document.
|
|
117
|
+
this.sub.psubscribe(this.subKey(documentName), async (error) => {
|
|
118
|
+
if (error) {
|
|
119
|
+
reject(error);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.publishFirstSyncStep(documentName, document);
|
|
123
|
+
this.requestAwarenessFromOtherInstances(documentName);
|
|
124
|
+
resolve(undefined);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Publish the first sync step through Redis.
|
|
130
|
+
*/
|
|
131
|
+
async publishFirstSyncStep(documentName, document) {
|
|
132
|
+
const syncMessage = new OutgoingMessage(documentName)
|
|
133
|
+
.createSyncMessage()
|
|
134
|
+
.writeFirstSyncStepFor(document);
|
|
135
|
+
return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(syncMessage.toUint8Array()));
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Let’s ask Redis who is connected already.
|
|
139
|
+
*/
|
|
140
|
+
async requestAwarenessFromOtherInstances(documentName) {
|
|
141
|
+
const awarenessMessage = new OutgoingMessage(documentName)
|
|
142
|
+
.writeQueryAwareness();
|
|
143
|
+
return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(awarenessMessage.toUint8Array()));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Before the document is stored, make sure to set a lock in Redis.
|
|
147
|
+
* That’s meant to avoid conflicts with other instances trying to store the document.
|
|
148
|
+
*/
|
|
149
|
+
async onStoreDocument({ documentName }) {
|
|
150
|
+
// Attempt to acquire a lock and read lastReceivedTimestamp from Redis,
|
|
151
|
+
// to avoid conflict with other instances storing the same document.
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
this.redlock.lock(this.lockKey(documentName), this.configuration.lockTimeout, async (error, lock) => {
|
|
154
|
+
if (error || !lock) {
|
|
155
|
+
// Expected behavior: Could not acquire lock, another instance locked it already.
|
|
156
|
+
// No further `onStoreDocument` hooks will be executed.
|
|
157
|
+
reject();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.locks.set(this.lockKey(documentName), lock);
|
|
161
|
+
resolve(undefined);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Release the Redis lock, so other instances can store documents.
|
|
167
|
+
*/
|
|
168
|
+
async afterStoreDocument({ documentName }) {
|
|
169
|
+
var _a;
|
|
170
|
+
(_a = this.locks.get(this.lockKey(documentName))) === null || _a === void 0 ? void 0 : _a.unlock().catch(() => {
|
|
171
|
+
// Not able to unlock Redis. The lock will expire after ${lockTimeout} ms.
|
|
172
|
+
// console.error(`Not able to unlock Redis. The lock will expire after ${this.configuration.lockTimeout}ms.`)
|
|
173
|
+
}).finally(() => {
|
|
174
|
+
this.locks.delete(this.lockKey(documentName));
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Handle awareness update messages received directly by this Hocuspocus instance.
|
|
179
|
+
*/
|
|
180
|
+
async onAwarenessUpdate({ documentName, awareness, added, updated, removed, }) {
|
|
181
|
+
const changedClients = added.concat(updated, removed);
|
|
182
|
+
const message = new OutgoingMessage(documentName)
|
|
183
|
+
.createAwarenessUpdateMessage(awareness, changedClients);
|
|
184
|
+
return this.pub.publishBuffer(this.pubKey(documentName), Buffer.from(message.toUint8Array()));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* if the ydoc changed, we'll need to inform other Hocuspocus servers about it.
|
|
188
|
+
*/
|
|
189
|
+
async onChange(data) {
|
|
190
|
+
return this.publishFirstSyncStep(data.documentName, data.document);
|
|
191
|
+
}
|
|
192
|
+
async beforeBroadcastStateless(data) {
|
|
193
|
+
const message = new OutgoingMessage(data.documentName)
|
|
194
|
+
.writeBroadcastStateless(data.payload);
|
|
195
|
+
return this.pub.publishBuffer(this.pubKey(data.documentName), Buffer.from(message.toUint8Array()));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Kill the Redlock connection immediately.
|
|
199
|
+
*/
|
|
200
|
+
async onDestroy() {
|
|
201
|
+
await this.redlock.quit();
|
|
202
|
+
this.pub.disconnect(false);
|
|
203
|
+
this.sub.disconnect(false);
|
|
204
|
+
}
|
|
158
205
|
}
|
|
159
206
|
|
|
160
207
|
export { Redis };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hocuspocus-redis.esm.js","sources":["../src/Redis.ts"],"sourcesContent":["import RedisClient, {\n ClusterNode, ClusterOptions, RedisOptions, Cluster as RedisClusterClient,\n} from 'ioredis'\nimport { v4 as uuid } from 'uuid'\nimport {\n IncomingMessage,\n OutgoingMessage,\n Document,\n Extension,\n afterLoadDocumentPayload,\n onDisconnectPayload,\n onAwarenessUpdatePayload,\n onChangePayload,\n MessageReceiver,\n Debugger,\n onConfigurePayload,\n beforeBroadcastStatelessPayload, Hocuspocus,\n} from '@hocuspocus/server'\n\nexport type RedisInstance = RedisClient | RedisClusterClient\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 * 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 disconnectDelay: 1000,\n }\n\n pub: RedisInstance\n\n sub: RedisInstance\n\n instance!: Hocuspocus\n\n logger: Debugger\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 as RedisOptions)\n this.sub = new RedisClient(port, host, options as RedisOptions)\n }\n this.sub.on('pmessageBuffer', this.handleIncomingMessage)\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 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)}:${this.configuration.identifier.replace(/:/g, '')}`\n }\n\n private subKey(documentName: string) {\n return `${this.getKey(documentName)}:*`\n }\n\n /**\n * Once a document is laoded, 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.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(documentName)\n .createSyncMessage()\n .writeFirstSyncStepFor(document)\n\n return this.pub.publish(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(documentName)\n .writeQueryAwareness()\n\n return this.pub.publish(\n this.pubKey(documentName),\n Buffer.from(awarenessMessage.toUint8Array()),\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.publish(\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 message = new IncomingMessage(data)\n // we don't need the documentName from the message, we are just taking it from the redis channelName.\n // we have to immediately write it back to the encoder though, to make sure the structure of the message is correct\n message.writeVarString(message.readVarString())\n\n const channelName = pattern.toString()\n const [_, documentName, identifier] = channelName.split(':')\n const document = this.instance.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 message,\n this.logger,\n ).apply(document, undefined, reply => {\n return this.pub.publish(\n this.pubKey(document.name),\n Buffer.from(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 return this.publishFirstSyncStep(data.documentName, data.document)\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 }: onDisconnectPayload) => {\n const disconnect = () => {\n const document = this.instance.documents.get(documentName)\n\n // Do nothing, when other users are still connected to the document.\n if (document && document.getConnectionsCount() > 0) {\n return\n }\n\n // Time to end the subscription on the document channel.\n this.sub.punsubscribe(this.subKey(documentName), (error: any) => {\n if (error) {\n console.error(error)\n }\n })\n }\n // Delay the disconnect procedure to allow last minute syncs to happen\n setTimeout(disconnect, this.configuration.disconnectDelay)\n }\n\n async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {\n const message = new OutgoingMessage(data.documentName)\n .writeBroadcastStateless(data.payload)\n\n return this.pub.publish(\n this.pubKey(data.documentName),\n Buffer.from(message.toUint8Array()),\n )\n }\n\n}\n"],"names":["uuid"],"mappings":";;;;MAgEa,KAAK,CAAA;AAwBhB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AAvBxD;;;;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,eAAe,EAAE,IAAI;SACtB,CAAA;AA2HD;;;;AAIE;QACM,IAAqB,CAAA,qBAAA,GAAG,OAAO,OAAe,EAAE,OAAe,EAAE,IAAY,KAAI;AACvF,YAAA,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,CAAA;;;YAGzC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,CAAA;AAE/C,YAAA,MAAM,WAAW,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAA;AACtC,YAAA,MAAM,CAAC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CAAC,oBAAoB,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;KACnE;IA0BD,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,OAAO,CACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAC9B,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CACpC,CAAA;KACF;AAEF;;;;"}
|
|
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 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 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('pmessageBuffer', this.handleIncomingMessage)\n\n this.redlock = new Redlock([this.pub])\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)}:${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 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(documentName)\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(documentName)\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({\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 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 message = new IncomingMessage(data)\n // we don't need the documentName from the message, we are just taking it from the redis channelName.\n // we have to immediately write it back to the encoder though, to make sure the structure of the message is correct\n message.writeVarString(message.readVarString())\n\n const channelName = pattern.toString()\n const [_, documentName, identifier] = channelName.split(':')\n const document = this.instance.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 message,\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 * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.\n */\n public async onChange(data: onChangePayload): Promise<any> {\n return this.publishFirstSyncStep(data.documentName, data.document)\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 }: onDisconnectPayload) => {\n const disconnect = () => {\n const document = this.instance.documents.get(documentName)\n\n // Do nothing, when other users are still connected to the document.\n if (document && document.getConnectionsCount() > 0) {\n return\n }\n\n // Time to end the subscription on the document channel.\n this.sub.punsubscribe(this.subKey(documentName), (error: any) => {\n if (error) {\n console.error(error)\n }\n })\n }\n // Delay the disconnect procedure to allow last minute syncs to happen\n setTimeout(disconnect, this.configuration.disconnectDelay)\n }\n\n async beforeBroadcastStateless(data: beforeBroadcastStatelessPayload) {\n const message = new OutgoingMessage(data.documentName)\n .writeBroadcastStateless(data.payload)\n\n return this.pub.publishBuffer(\n this.pubKey(data.documentName),\n Buffer.from(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;AA6BhB,IAAA,WAAA,CAAmB,aAAqC,EAAA;AA5BxD;;;;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;AAUD,QAAA,IAAA,CAAA,KAAK,GAAG,IAAI,GAAG,EAAwB,CAAA;AAgKvC;;;;AAIE;QACM,IAAqB,CAAA,qBAAA,GAAG,OAAO,OAAe,EAAE,OAAe,EAAE,IAAY,KAAI;AACvF,YAAA,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,CAAA;;;YAGzC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,CAAA;AAE/C,YAAA,MAAM,WAAW,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAA;AACtC,YAAA,MAAM,CAAC,CAAC,EAAE,YAAY,EAAE,UAAU,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;AAC5D,YAAA,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC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AO,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,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CACpC,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,29 +1,29 @@
|
|
|
1
|
-
export interface CloseEvent {
|
|
2
|
-
code: number;
|
|
3
|
-
reason: string;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* The server is terminating the connection because a data frame was received
|
|
7
|
-
* that is too large.
|
|
8
|
-
* See: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
|
|
9
|
-
*/
|
|
10
|
-
export declare const MessageTooBig: CloseEvent;
|
|
11
|
-
/**
|
|
12
|
-
* The server successfully processed the request, asks that the requester reset
|
|
13
|
-
* its document view, and is not returning any content.
|
|
14
|
-
*/
|
|
15
|
-
export declare const ResetConnection: CloseEvent;
|
|
16
|
-
/**
|
|
17
|
-
* Similar to Forbidden, but specifically for use when authentication is required and has
|
|
18
|
-
* failed or has not yet been provided.
|
|
19
|
-
*/
|
|
20
|
-
export declare const Unauthorized: CloseEvent;
|
|
21
|
-
/**
|
|
22
|
-
* The request contained valid data and was understood by the server, but the server
|
|
23
|
-
* is refusing action.
|
|
24
|
-
*/
|
|
25
|
-
export declare const Forbidden: CloseEvent;
|
|
26
|
-
/**
|
|
27
|
-
* The server timed out waiting for the request.
|
|
28
|
-
*/
|
|
29
|
-
export declare const ConnectionTimeout: CloseEvent;
|
|
1
|
+
export interface CloseEvent {
|
|
2
|
+
code: number;
|
|
3
|
+
reason: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The server is terminating the connection because a data frame was received
|
|
7
|
+
* that is too large.
|
|
8
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
|
|
9
|
+
*/
|
|
10
|
+
export declare const MessageTooBig: CloseEvent;
|
|
11
|
+
/**
|
|
12
|
+
* The server successfully processed the request, asks that the requester reset
|
|
13
|
+
* its document view, and is not returning any content.
|
|
14
|
+
*/
|
|
15
|
+
export declare const ResetConnection: CloseEvent;
|
|
16
|
+
/**
|
|
17
|
+
* Similar to Forbidden, but specifically for use when authentication is required and has
|
|
18
|
+
* failed or has not yet been provided.
|
|
19
|
+
*/
|
|
20
|
+
export declare const Unauthorized: CloseEvent;
|
|
21
|
+
/**
|
|
22
|
+
* The request contained valid data and was understood by the server, but the server
|
|
23
|
+
* is refusing action.
|
|
24
|
+
*/
|
|
25
|
+
export declare const Forbidden: CloseEvent;
|
|
26
|
+
/**
|
|
27
|
+
* The server timed out waiting for the request.
|
|
28
|
+
*/
|
|
29
|
+
export declare const ConnectionTimeout: CloseEvent;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as encoding from 'lib0/encoding';
|
|
2
|
-
import * as decoding from 'lib0/decoding';
|
|
3
|
-
export declare const writeAuthentication: (encoder: encoding.Encoder, auth: string) => void;
|
|
4
|
-
export declare const writePermissionDenied: (encoder: encoding.Encoder, reason: string) => void;
|
|
5
|
-
export declare const writeAuthenticated: (encoder: encoding.Encoder) => void;
|
|
6
|
-
export declare const readAuthMessage: (decoder: decoding.Decoder, permissionDeniedHandler: (reason: string) => void, authenticatedHandler: () => void) => void;
|
|
1
|
+
import * as encoding from 'lib0/encoding';
|
|
2
|
+
import * as decoding from 'lib0/decoding';
|
|
3
|
+
export declare const writeAuthentication: (encoder: encoding.Encoder, auth: string) => void;
|
|
4
|
+
export declare const writePermissionDenied: (encoder: encoding.Encoder, reason: string) => void;
|
|
5
|
+
export declare const writeAuthenticated: (encoder: encoding.Encoder, scope: 'readonly' | 'read-write') => void;
|
|
6
|
+
export declare const readAuthMessage: (decoder: decoding.Decoder, permissionDeniedHandler: (reason: string) => void, authenticatedHandler: (scope: string) => void) => void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const awarenessStatesToArray: (states: Map<number, Record<string, any>>) => {
|
|
2
|
-
clientId: number;
|
|
3
|
-
}[];
|
|
1
|
+
export declare const awarenessStatesToArray: (states: Map<number, Record<string, any>>) => {
|
|
2
|
+
clientId: number;
|
|
3
|
+
}[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './auth';
|
|
2
|
-
export * from './CloseEvents';
|
|
3
|
-
export * from './awarenessStatesToArray';
|
|
4
|
-
export * from './types';
|
|
1
|
+
export * from './auth.js';
|
|
2
|
+
export * from './CloseEvents.js';
|
|
3
|
+
export * from './awarenessStatesToArray.js';
|
|
4
|
+
export * from './types.js';
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* State of the WebSocket connection.
|
|
3
|
-
* https://developer.mozilla.org/de/docs/Web/API/WebSocket/readyState
|
|
4
|
-
*/
|
|
5
|
-
export declare enum WsReadyStates {
|
|
6
|
-
Connecting = 0,
|
|
7
|
-
Open = 1,
|
|
8
|
-
Closing = 2,
|
|
9
|
-
Closed = 3
|
|
10
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* State of the WebSocket connection.
|
|
3
|
+
* https://developer.mozilla.org/de/docs/Web/API/WebSocket/readyState
|
|
4
|
+
*/
|
|
5
|
+
export declare enum WsReadyStates {
|
|
6
|
+
Connecting = 0,
|
|
7
|
+
Open = 1,
|
|
8
|
+
Closing = 2,
|
|
9
|
+
Closed = 3
|
|
10
|
+
}
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { Extension, onChangePayload, onLoadDocumentPayload, storePayload, fetchPayload } from '@hocuspocus/server';
|
|
2
|
-
export interface DatabaseConfiguration {
|
|
3
|
-
/**
|
|
4
|
-
* Pass a Promise to retrieve updates from your database. The Promise should resolve to
|
|
5
|
-
* an array of items with Y.js-compatible binary data.
|
|
6
|
-
*/
|
|
7
|
-
fetch: (data: fetchPayload) => Promise<Uint8Array | null>;
|
|
8
|
-
/**
|
|
9
|
-
* Pass a function to store updates in your database.
|
|
10
|
-
*/
|
|
11
|
-
store: (data: storePayload) => void;
|
|
12
|
-
}
|
|
13
|
-
export declare class Database implements Extension {
|
|
14
|
-
/**
|
|
15
|
-
* Default configuration
|
|
16
|
-
*/
|
|
17
|
-
configuration: DatabaseConfiguration;
|
|
18
|
-
/**
|
|
19
|
-
* Constructor
|
|
20
|
-
*/
|
|
21
|
-
constructor(configuration: Partial<DatabaseConfiguration>);
|
|
22
|
-
/**
|
|
23
|
-
* Get stored data from the database.
|
|
24
|
-
*/
|
|
25
|
-
onLoadDocument(data: onLoadDocumentPayload): Promise<any>;
|
|
26
|
-
/**
|
|
27
|
-
* Store new updates in the database.
|
|
28
|
-
*/
|
|
29
|
-
onStoreDocument(data: onChangePayload): Promise<void>;
|
|
30
|
-
}
|
|
1
|
+
import { Extension, onChangePayload, onLoadDocumentPayload, storePayload, fetchPayload } from '@hocuspocus/server';
|
|
2
|
+
export interface DatabaseConfiguration {
|
|
3
|
+
/**
|
|
4
|
+
* Pass a Promise to retrieve updates from your database. The Promise should resolve to
|
|
5
|
+
* an array of items with Y.js-compatible binary data.
|
|
6
|
+
*/
|
|
7
|
+
fetch: (data: fetchPayload) => Promise<Uint8Array | null>;
|
|
8
|
+
/**
|
|
9
|
+
* Pass a function to store updates in your database.
|
|
10
|
+
*/
|
|
11
|
+
store: (data: storePayload) => void;
|
|
12
|
+
}
|
|
13
|
+
export declare class Database implements Extension {
|
|
14
|
+
/**
|
|
15
|
+
* Default configuration
|
|
16
|
+
*/
|
|
17
|
+
configuration: DatabaseConfiguration;
|
|
18
|
+
/**
|
|
19
|
+
* Constructor
|
|
20
|
+
*/
|
|
21
|
+
constructor(configuration: Partial<DatabaseConfiguration>);
|
|
22
|
+
/**
|
|
23
|
+
* Get stored data from the database.
|
|
24
|
+
*/
|
|
25
|
+
onLoadDocument(data: onLoadDocumentPayload): Promise<any>;
|
|
26
|
+
/**
|
|
27
|
+
* Store new updates in the database.
|
|
28
|
+
*/
|
|
29
|
+
onStoreDocument(data: onChangePayload): Promise<void>;
|
|
30
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './Database';
|
|
1
|
+
export * from './Database.js';
|