-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (36 loc) · 1.37 KB
/
server.js
File metadata and controls
45 lines (36 loc) · 1.37 KB
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
import { Server } from '@hocuspocus/server';
import * as Y from 'yjs';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DATA_DIR = path.join(__dirname, '.data');
const PORT = Number(process.env.HOCUSPOCUS_PORT || 1234);
const toSnapshotFilename = (documentName) =>
encodeURIComponent(documentName).replace(
/[!'()*]/g,
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
);
const getSnapshotPath = (documentName) => path.join(DATA_DIR, `${toSnapshotFilename(documentName)}.yjs`);
const server = Server.configure({
port: PORT,
debounce: 500,
maxDebounce: 2000,
async onLoadDocument({ documentName, document }) {
try {
const update = await readFile(getSnapshotPath(documentName));
Y.applyUpdate(document, update);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
return document;
},
async onStoreDocument({ documentName, document }) {
await mkdir(DATA_DIR, { recursive: true });
const update = Y.encodeStateAsUpdate(document);
await writeFile(getSnapshotPath(documentName), Buffer.from(update));
},
});
server.listen();
console.log(`Hocuspocus server running on ws://localhost:${PORT}`);
console.log(`Yjs snapshots are stored in ${DATA_DIR}`);