-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
370 lines (337 loc) · 11.4 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const Y = require("yjs");
const syncProtocol = require("y-protocols/dist/sync.cjs");
const awarenessProtocol = require("y-protocols/dist/awareness.cjs");
const encoding = require("lib0/dist/encoding.cjs");
const decoding = require("lib0/dist/decoding.cjs");
const map = require("lib0/dist/map.cjs");
const debounce = require("lodash.debounce");
const callbackHandler = require("./callback.js").callbackHandler;
const isCallbackSet = require("./callback.js").isCallbackSet;
const CALLBACK_DEBOUNCE_WAIT = 2000;
const CALLBACK_DEBOUNCE_MAXWAIT = 10000;
const wsReadyStateConnecting = 0;
const wsReadyStateOpen = 1;
const wsReadyStateClosing = 2; // eslint-disable-line
const wsReadyStateClosed = 3; // eslint-disable-line
// disable gc when using snapshots!
const gcEnabled = process.env.GC !== "false" && process.env.GC !== "0";
const persistenceDir = "./whiteboard-data";
const fs = require("fs");
if (!fs.existsSync(persistenceDir)) {
fs.mkdirSync(persistenceDir);
}
/**
* @type {{bindState: function(string,WSSharedDoc):void, writeState:function(string,WSSharedDoc):Promise<any>, provider: any}|null}
*/
let persistence = null;
if (typeof persistenceDir === "string") {
//console.info('Persisting documents to "' + persistenceDir + '"');
// @ts-ignore
const LeveldbPersistence = require("y-leveldb").LeveldbPersistence;
const ldb = new LeveldbPersistence(persistenceDir);
persistence = {
provider: ldb,
bindState: async (docName, ydoc) => {
const persistedYdoc = await ldb.getYDoc(docName);
const yUsers = persistedYdoc.getArray("users");
yUsers.delete(0, yUsers.length);
const newUpdates = Y.encodeStateAsUpdate(ydoc);
ldb.storeUpdate(docName, newUpdates);
Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc));
ydoc.on("update", (update) => {
ldb.storeUpdate(docName, update);
});
},
writeState: async (docName, ydoc) => {},
};
}
/**
* @param {{bindState: function(string,WSSharedDoc):void,
* writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null} persistence_
*/
exports.setPersistence = (persistence_) => {
persistence = persistence_;
};
/**
* @return {null|{bindState: function(string,WSSharedDoc):void,
* writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
*/
exports.getPersistence = () => persistence;
/**
* @type {Map<string,WSSharedDoc>}
*/
const docs = new Map();
// exporting docs so that others can use it
exports.docs = docs;
const messageSync = 0;
const messageAwareness = 1;
// const messageAuth = 2
/**
* @param {Uint8Array} update
* @param {any} origin
* @param {WSSharedDoc} doc
*/
const updateHandler = (update, origin, doc) => {
const encoder = encoding.createEncoder();
encoding.writeVarUint(encoder, messageSync);
syncProtocol.writeUpdate(encoder, update);
const message = encoding.toUint8Array(encoder);
doc.conns.forEach((_, conn) => send(doc, conn, message));
};
class WSSharedDoc extends Y.Doc {
/**
* @param {string} name
*/
constructor(name) {
super({ gc: gcEnabled });
this.name = name;
/**
* Maps from conn to set of controlled user ids. Delete all user ids from awareness when this conn is closed
* @type {Map<Object, Set<number>>}
*/
this.conns = new Map();
/**
* @type {awarenessProtocol.Awareness}
*/
this.awareness = new awarenessProtocol.Awareness(this);
this.awareness.setLocalState(null);
/**
* @param {{ added: Array<number>, updated: Array<number>, removed: Array<number> }} changes
* @param {Object | null} conn Origin is the connection that made the change
*/
const awarenessChangeHandler = ({ added, updated, removed }, conn) => {
const changedClients = added.concat(updated, removed);
if (conn !== null) {
const connControlledIDs = /** @type {Set<number>} */ (
this.conns.get(conn)
);
if (connControlledIDs !== undefined) {
added.forEach((clientID) => {
connControlledIDs.add(clientID);
});
removed.forEach((clientID) => {
connControlledIDs.delete(clientID);
});
}
}
// broadcast awareness update
const encoder = encoding.createEncoder();
encoding.writeVarUint(encoder, messageAwareness);
encoding.writeVarUint8Array(
encoder,
awarenessProtocol.encodeAwarenessUpdate(
this.awareness,
changedClients
)
);
const buff = encoding.toUint8Array(encoder);
this.conns.forEach((_, c) => {
send(this, c, buff);
});
};
this.awareness.on("update", awarenessChangeHandler);
this.on("update", updateHandler);
if (isCallbackSet) {
this.on(
"update",
debounce(callbackHandler, CALLBACK_DEBOUNCE_WAIT, {
maxWait: CALLBACK_DEBOUNCE_MAXWAIT,
})
);
}
}
canAddMoreUsers(doc) {
return parseInt(doc.getArray("users").length + 1) <= 30;
}
}
/**
* Gets a Y.Doc by name, whether in memory or on disk
*
* @param {string} docname - the name of the Y.Doc to find or create
* @param {boolean} gc - whether to allow gc on the doc (applies only when created)
* @return {WSSharedDoc}
*/
const getYDoc = (docname, gc = true) =>
map.setIfUndefined(docs, docname, () => {
const doc = new WSSharedDoc(docname);
doc.gc = gc;
if (persistence !== null) {
persistence.bindState(docname, doc);
}
docs.set(docname, doc);
return doc;
});
exports.getYDoc = getYDoc;
/**
* @param {any} conn
* @param {WSSharedDoc} doc
* @param {Uint8Array} message
*/
const messageListener = (conn, doc, message) => {
try {
const encoder = encoding.createEncoder();
const decoder = decoding.createDecoder(message);
const messageType = decoding.readVarUint(decoder);
switch (messageType) {
case messageSync:
encoding.writeVarUint(encoder, messageSync);
syncProtocol.readSyncMessage(decoder, encoder, doc, conn);
// If the `encoder` only contains the type of reply message and no
// message, there is no need to send the message. When `encoder` only
// contains the type of reply, its length is 1.
if (encoding.length(encoder) > 1) {
send(doc, conn, encoding.toUint8Array(encoder));
}
break;
case messageAwareness: {
awarenessProtocol.applyAwarenessUpdate(
doc.awareness,
decoding.readVarUint8Array(decoder),
conn
);
break;
}
}
} catch (err) {
console.error(err);
doc.emit("error", [err]);
}
};
/**
* @param {WSSharedDoc} doc
* @param {any} conn
*/
const closeConn = (doc, conn) => {
if (doc.conns.has(conn)) {
const controlledIds = doc.conns.get(conn);
controlledIds.forEach((clientId) => {
doc.awareness.setLocalStateField(clientId, null);
});
doc.conns.delete(conn);
if (doc.conns.size === 0 && persistence !== null) {
persistence.writeState(doc.name, doc).then(() => {
doc.destroy();
});
docs.delete(doc.name);
}
}
conn.close();
};
/**
* @param {WSSharedDoc} doc
* @param {any} conn
* @param {Uint8Array} m
*/
const send = (doc, conn, m) => {
if (
conn.readyState !== wsReadyStateConnecting &&
conn.readyState !== wsReadyStateOpen
) {
closeConn(doc, conn);
}
try {
conn.send(
m,
/** @param {any} err */ (err) => {
err != null && closeConn(doc, conn);
}
);
} catch (e) {
closeConn(doc, conn);
}
};
const pingTimeout = 30000;
/**
* @param {any} conn
* @param {any} req
* @param {any} opts
*/
exports.setupWSConnection = (
conn,
req,
{ docName = req.url.slice(1).split("?")[0], gc = true } = {}
) => {
conn.binaryType = "arraybuffer";
// get doc, initialize if it does not exist yet
const doc = getYDoc(docName, gc);
if (!doc.canAddMoreUsers(doc)) {
conn.close(4000, "The room is full");
return;
}
doc.conns.set(conn, new Set());
// listen and reply to events
conn.on(
"message",
/** @param {ArrayBuffer} message */ (message) =>
messageListener(conn, doc, new Uint8Array(message))
);
// Check if connection is still alive
let pongReceived = true;
const pingInterval = setInterval(() => {
if (!pongReceived) {
if (doc.conns.has(conn)) {
closeConn(doc, conn);
}
clearInterval(pingInterval);
} else if (doc.conns.has(conn)) {
pongReceived = false;
try {
conn.ping();
} catch (e) {
closeConn(doc, conn);
clearInterval(pingInterval);
}
}
}, pingTimeout);
conn.on("close", () => {
const clientID = doc.conns.get(conn);
doc.awareness.setLocalStateField(clientID, null);
closeConn(doc, conn);
clearInterval(pingInterval);
});
conn.on("pong", () => {
pongReceived = true;
});
{
// send sync step 1
const encoder = encoding.createEncoder();
encoding.writeVarUint(encoder, messageSync);
syncProtocol.writeSyncStep1(encoder, doc);
send(doc, conn, encoding.toUint8Array(encoder));
const awarenessStates = doc.awareness.getStates();
if (awarenessStates.size > 0) {
const encoder = encoding.createEncoder();
encoding.writeVarUint(encoder, messageAwareness);
encoding.writeVarUint8Array(
encoder,
awarenessProtocol.encodeAwarenessUpdate(
doc.awareness,
Array.from(awarenessStates.keys())
)
);
send(doc, conn, encoding.toUint8Array(encoder));
}
}
};
exports.cloneDoc = async (originalRoomId, newRoomId) => {
try {
const originalDoc = getYDoc(originalRoomId, true);
await new Promise((resolve) => setTimeout(resolve, 100));
const encodedState = Y.encodeStateAsUpdate(originalDoc);
const newDoc = getYDoc(newRoomId, true);
Y.applyUpdate(newDoc, encodedState);
const originalType = newDoc.get(`tl_${originalRoomId}`);
newDoc.share.set(`tl_${newRoomId}`, originalType);
newDoc.share.delete(`tl_${originalRoomId}`);
const updateStates = Y.encodeStateAsUpdate(newDoc);
Y.applyUpdate(newDoc, updateStates);
if (persistence && persistence.provider) {
await persistence.provider.storeUpdate(newRoomId, updateStates);
await persistence.bindState(newRoomId, newDoc);
} else {
console.error("Persistence is not available.");
}
} catch (error) {
console.error("Error cloning room:", error);
}
};