-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathserver.ts
More file actions
88 lines (69 loc) · 2.55 KB
/
server.ts
File metadata and controls
88 lines (69 loc) · 2.55 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
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
import Fastify from 'fastify';
import websocketPlugin from '@fastify/websocket';
import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import type { WebSocket } from 'ws';
import type { FastifyRequest } from 'fastify';
import {
CollaborationBuilder,
type CollaborationParams,
type CollaborationWebSocket,
type SocketRequest,
type UserContext,
type ServiceConfig
} from '@superdoc-dev/superdoc-yjs-collaboration';
/** Create an example server */
const fastify = Fastify({ logger: false });
fastify.register(websocketPlugin);
/** We create some basic hooks */
const handleConfig = (config: ServiceConfig): void => {
console.debug('[handleConfig] Service has been configured', config);
}
const handleAuth = async ({ documentId, socket, request }: CollaborationParams): Promise<UserContext> => {
console.debug(`[handleAuth] Authenticating connection for document ${documentId}`);
const user = { userid: 'abc', username: 'testuser' };
const organizationid = "someorg123";
const custom = { someCustomKey: 'somevalue' }
const context = { user, organizationid, custom };
return context;
};
const handleLoad = async (params: CollaborationParams): Promise<Uint8Array> => {
const ydoc = new YDoc();
console.debug('[handleLoad] loaded', params)
return encodeStateAsUpdate(ydoc);
}
const handleOnChange = async (params: CollaborationParams): Promise<void> => {
console.debug(`[handleOnChange Document ${params.documentId} changed.`);
};
const handleAutoSave = async (params: CollaborationParams): Promise<void> => {
console.debug('handleAutoSave] params', params)
}
const SuperDocCollaboration = new CollaborationBuilder()
.withName('SuperDoc Collaboration service')
.withDebounce(2000)
.onConfigure(handleConfig)
.onLoad(handleLoad)
.onAuthenticate(handleAuth)
.onChange(handleOnChange)
.onAutoSave(handleAutoSave)
.build();
/** A sample test route */
fastify.get('/', async (request, reply) => 'Hello, SuperDoc!');
/** An example route for websocket collaboration connection */
fastify.register(async function (fastify) {
fastify.get('/collaboration/:documentId', { websocket: true }, (socket, request) => {
SuperDocCollaboration.welcome(socket as any, request as any)
})
});
/** Start the example! */
const start = async (): Promise<void> => {
fastify.listen({ port: 3050 }, errorHandler);
console.log('Server listening at http://localhost:3050');
};
/** Basic error handler example */
const errorHandler = (err: Error | null, address?: string): void => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();