@verdant-web/server 1.7.8 → 1.7.9

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.
Files changed (97) hide show
  1. package/dist/cjs/Baselines.d.ts +14 -14
  2. package/dist/cjs/Baselines.js +77 -77
  3. package/dist/cjs/ClientConnection.d.ts +18 -18
  4. package/dist/cjs/ClientConnection.js +105 -105
  5. package/dist/cjs/MessageSender.d.ts +5 -5
  6. package/dist/cjs/MessageSender.js +2 -2
  7. package/dist/cjs/OperationHistory.d.ts +34 -34
  8. package/dist/cjs/OperationHistory.js +108 -108
  9. package/dist/cjs/Presence.d.ts +15 -15
  10. package/dist/cjs/Presence.js +59 -59
  11. package/dist/cjs/Profiles.d.ts +10 -10
  12. package/dist/cjs/Profiles.js +19 -19
  13. package/dist/cjs/ReplicaKeepaliveTimers.d.ts +17 -17
  14. package/dist/cjs/ReplicaKeepaliveTimers.js +30 -30
  15. package/dist/cjs/Replicas.d.ts +23 -23
  16. package/dist/cjs/Replicas.js +132 -132
  17. package/dist/cjs/Server.d.ts +121 -121
  18. package/dist/cjs/Server.js +377 -377
  19. package/dist/cjs/ServerLibrary.d.ts +63 -63
  20. package/dist/cjs/ServerLibrary.js +467 -467
  21. package/dist/cjs/TokenProvider.d.ts +16 -16
  22. package/dist/cjs/TokenProvider.js +26 -26
  23. package/dist/cjs/TokenVerifier.d.ts +16 -16
  24. package/dist/cjs/TokenVerifier.js +28 -28
  25. package/dist/cjs/files/FileMetadata.d.ts +28 -28
  26. package/dist/cjs/files/FileMetadata.js +46 -46
  27. package/dist/cjs/files/FileStorage.d.ts +35 -35
  28. package/dist/cjs/files/FileStorage.js +71 -71
  29. package/dist/cjs/index.d.ts +9 -9
  30. package/dist/cjs/index.js +11 -11
  31. package/dist/cjs/migrations/v0.sql.d.ts +2 -2
  32. package/dist/cjs/migrations/v0.sql.js +3 -3
  33. package/dist/cjs/migrations/v1.sql.d.ts +2 -2
  34. package/dist/cjs/migrations/v1.sql.js +3 -3
  35. package/dist/cjs/migrations/v2.sql.d.ts +2 -2
  36. package/dist/cjs/migrations/v2.sql.js +4 -4
  37. package/dist/cjs/migrations/v3.sql.d.ts +2 -2
  38. package/dist/cjs/migrations/v3.sql.js +4 -4
  39. package/dist/cjs/migrations/v4.sql.d.ts +2 -2
  40. package/dist/cjs/migrations/v4.sql.js +4 -4
  41. package/dist/cjs/migrations.d.ts +2 -2
  42. package/dist/cjs/migrations.js +32 -32
  43. package/dist/cjs/types.d.ts +22 -22
  44. package/dist/cjs/types.js +2 -2
  45. package/dist/esm/Baselines.d.ts +14 -14
  46. package/dist/esm/Baselines.js +73 -73
  47. package/dist/esm/ClientConnection.d.ts +18 -18
  48. package/dist/esm/ClientConnection.js +101 -101
  49. package/dist/esm/MessageSender.d.ts +5 -5
  50. package/dist/esm/MessageSender.js +1 -1
  51. package/dist/esm/OperationHistory.d.ts +34 -34
  52. package/dist/esm/OperationHistory.js +104 -104
  53. package/dist/esm/Presence.d.ts +15 -15
  54. package/dist/esm/Presence.js +52 -52
  55. package/dist/esm/Profiles.d.ts +10 -10
  56. package/dist/esm/Profiles.js +15 -15
  57. package/dist/esm/ReplicaKeepaliveTimers.d.ts +17 -17
  58. package/dist/esm/ReplicaKeepaliveTimers.js +26 -26
  59. package/dist/esm/Replicas.d.ts +23 -23
  60. package/dist/esm/Replicas.js +128 -128
  61. package/dist/esm/Server.d.ts +121 -121
  62. package/dist/esm/Server.js +370 -370
  63. package/dist/esm/ServerLibrary.d.ts +63 -63
  64. package/dist/esm/ServerLibrary.js +463 -463
  65. package/dist/esm/TokenProvider.d.ts +16 -16
  66. package/dist/esm/TokenProvider.js +19 -19
  67. package/dist/esm/TokenVerifier.d.ts +16 -16
  68. package/dist/esm/TokenVerifier.js +21 -21
  69. package/dist/esm/files/FileMetadata.d.ts +28 -28
  70. package/dist/esm/files/FileMetadata.js +42 -42
  71. package/dist/esm/files/FileStorage.d.ts +35 -35
  72. package/dist/esm/files/FileStorage.js +44 -44
  73. package/dist/esm/index.d.ts +9 -9
  74. package/dist/esm/index.js +4 -4
  75. package/dist/esm/migrations/v0.sql.d.ts +2 -2
  76. package/dist/esm/migrations/v0.sql.js +1 -1
  77. package/dist/esm/migrations/v1.sql.d.ts +2 -2
  78. package/dist/esm/migrations/v1.sql.js +1 -1
  79. package/dist/esm/migrations/v2.sql.d.ts +2 -2
  80. package/dist/esm/migrations/v2.sql.js +2 -2
  81. package/dist/esm/migrations/v3.sql.d.ts +2 -2
  82. package/dist/esm/migrations/v3.sql.js +2 -2
  83. package/dist/esm/migrations/v4.sql.d.ts +2 -2
  84. package/dist/esm/migrations/v4.sql.js +2 -2
  85. package/dist/esm/migrations.d.ts +2 -2
  86. package/dist/esm/migrations.js +25 -25
  87. package/dist/esm/types.d.ts +22 -22
  88. package/dist/esm/types.js +1 -1
  89. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  90. package/dist/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +2 -2
  92. package/dist/cjs/ServerStorage.d.ts +0 -35
  93. package/dist/cjs/ServerStorage.js +0 -60
  94. package/dist/cjs/ServerStorage.js.map +0 -1
  95. package/dist/esm/ServerStorage.d.ts +0 -44
  96. package/dist/esm/ServerStorage.js +0 -58
  97. package/dist/esm/ServerStorage.js.map +0 -1
@@ -1,371 +1,371 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- import { assert, createOid, generateId, } from '@verdant-web/common';
13
- import EventEmitter from 'events';
14
- import { Server as HttpServer } from 'http';
15
- import { WebSocketServer } from 'ws';
16
- import { UserProfileLoader } from './Profiles.js';
17
- import create from 'better-sqlite3';
18
- import { URL } from 'url';
19
- import { ClientConnectionManager } from './ClientConnection.js';
20
- import { ReplicaKeepaliveTimers } from './ReplicaKeepaliveTimers.js';
21
- import { TokenVerifier } from './TokenVerifier.js';
22
- import busboy from 'busboy';
23
- import { Readable } from 'stream';
24
- import { FileMetadata } from './files/FileMetadata.js';
25
- import { ServerLibrary } from './ServerLibrary.js';
26
- import { migrations } from './migrations.js';
27
- class DefaultProfiles {
28
- constructor() {
29
- this.get = async (userId) => {
30
- return { id: userId };
31
- };
32
- }
33
- }
34
- export class Server extends EventEmitter {
35
- constructor(options) {
36
- super();
37
- this.clientConnections = new ClientConnectionManager();
38
- this.keepalives = new ReplicaKeepaliveTimers();
39
- this.connectionToReplicaIdMap = new WeakMap();
40
- this.log = () => { };
41
- this.__testMode = false;
42
- this.authorizeRequest = (req) => {
43
- return this.tokenVerifier.verifyToken(this.getRequestToken(req));
44
- };
45
- this.getRequestToken = (req) => {
46
- if (req.headers.authorization) {
47
- const [type, token] = req.headers.authorization.split(' ');
48
- if (type === 'Bearer') {
49
- return token;
50
- }
51
- }
52
- if (req.headers['sec-websocket-protocol']) {
53
- // hack: read the token from the websocket protocol header
54
- const [type, token] = req.headers['sec-websocket-protocol'].split(',');
55
- if (type === 'Bearer') {
56
- return token.trim();
57
- }
58
- }
59
- assert(req.url, 'Request URL is required');
60
- const url = new URL(req.url, 'http://localhost');
61
- const token = url.searchParams.get('token');
62
- assert(token, 'Token is required');
63
- return token;
64
- };
65
- this.internalServerHandleRequest = (req, res) => {
66
- const url = new URL(req.url || '', 'http://localhost');
67
- if (url.pathname.startsWith('lofi')) {
68
- if (url.pathname === 'lofi') {
69
- return this.handleRequest(req, res);
70
- }
71
- else if (url.pathname.startsWith('/lofi/files/')) {
72
- return this.handleFileRequest(req, res);
73
- }
74
- }
75
- else {
76
- res.writeHead(404);
77
- res.end();
78
- }
79
- };
80
- this.handleRequest = async (req, res) => {
81
- var _a;
82
- try {
83
- if (req.method === 'POST') {
84
- const info = this.authorizeRequest(req);
85
- const key = generateId();
86
- const finish = this.clientConnections.addRequest(info.libraryId, key, req, res, info);
87
- // FIXME: extremely naive compatibility with connect-like
88
- // servers...
89
- const body = (_a = req.body) !== null && _a !== void 0 ? _a : (await new Promise((resolve, reject) => {
90
- let body = '';
91
- req.on('data', (chunk) => {
92
- body += chunk;
93
- });
94
- req.on('end', () => {
95
- resolve(JSON.parse(body));
96
- });
97
- req.on('error', (e) => {
98
- reject(e);
99
- });
100
- }));
101
- for (const message of body.messages) {
102
- await this.handleMessage(key, info, message);
103
- }
104
- // update our keepalive timers for presence management
105
- const firstMessage = body.messages[0];
106
- if (firstMessage) {
107
- this.keepalives.refresh(info.libraryId, firstMessage.replicaId);
108
- }
109
- finish();
110
- this.emit('request', info);
111
- }
112
- }
113
- catch (e) {
114
- this.emit('error', e);
115
- res.writeHead(500);
116
- res.end();
117
- }
118
- };
119
- /**
120
- * Handles a multipart upload of a file from a verdant client. The upload
121
- * will include parameters for the file's ID, name, and type. The request
122
- * must be authenticated with a token to tie it to a library.
123
- */
124
- this.handleFileRequest = async (req, res) => {
125
- const fs = this.fileStorage;
126
- if (!fs) {
127
- this.emit('error', new Error('No file storage configured, but a client attempted to upload a file.'));
128
- res.writeHead(500);
129
- res.write('File storage is not configured');
130
- res.end();
131
- return;
132
- }
133
- // FIXME: rather than trying to support Express, I should
134
- // just expose regular methods you call from whatever HTTP handler...
135
- const url = new URL(req.originalUrl || req.baseUrl || req.url || '', 'http://localhost');
136
- const id = url.pathname.split('/').pop();
137
- if (!id || id === 'files') {
138
- res.writeHead(400);
139
- res.write('File ID is required to be in the URL path as the last parameter');
140
- res.end();
141
- return;
142
- }
143
- try {
144
- if (req.method === 'POST') {
145
- const info = this.authorizeRequest(req);
146
- await new Promise((resolve, reject) => {
147
- const bb = busboy({ headers: req.headers });
148
- bb.on('file', (fieldName, stream, fileInfo) => {
149
- // too many 'info's....
150
- const lofiFileInfo = {
151
- id,
152
- libraryId: info.libraryId,
153
- fileName: fileInfo.filename,
154
- type: fileInfo.mimeType,
155
- };
156
- // write metadata to storage
157
- try {
158
- this.fileMetadata.put(info.libraryId, lofiFileInfo);
159
- fs.put(stream, lofiFileInfo);
160
- }
161
- catch (e) {
162
- reject(e);
163
- }
164
- });
165
- bb.on('field', (fieldName, value) => {
166
- if (fieldName === 'file') {
167
- if (this.__testMode) {
168
- // this isn't right in the real world, but in testing it's
169
- // the only way we get file data.
170
- // we create a stream from the data and pass it as if it
171
- // were a file stream
172
- const stream = new Readable();
173
- stream.push(value);
174
- stream.push(null);
175
- const fileInfo = {
176
- filename: 'test.txt',
177
- mimeType: 'text/plain',
178
- };
179
- bb.emit('file', fieldName, stream, fileInfo);
180
- }
181
- else {
182
- throw new Error('Invalid file upload');
183
- }
184
- }
185
- });
186
- req.pipe(bb);
187
- bb.on('finish', resolve);
188
- bb.on('error', reject);
189
- });
190
- this.log('File upload complete');
191
- res.writeHead(200);
192
- res.write(JSON.stringify({ success: true }));
193
- res.end();
194
- }
195
- else if (req.method === 'GET') {
196
- const info = this.authorizeRequest(req);
197
- const fileInfo = this.fileMetadata.get(info.libraryId, id);
198
- if (!fileInfo) {
199
- res.writeHead(404);
200
- res.end();
201
- return;
202
- }
203
- const url = await fs.getUrl({
204
- fileName: fileInfo.name,
205
- id: fileInfo.fileId,
206
- libraryId: info.libraryId,
207
- type: fileInfo.type,
208
- });
209
- res.writeHead(200, {
210
- 'Content-Type': 'application/json',
211
- });
212
- // we need to augment that data with the URL from the file backend.
213
- // and generally enforce the FileData interface here...
214
- const data = {
215
- id: fileInfo.fileId,
216
- url,
217
- remote: true,
218
- name: fileInfo.name,
219
- type: fileInfo.type,
220
- };
221
- res.write(JSON.stringify(data));
222
- res.end();
223
- }
224
- }
225
- catch (e) {
226
- this.log('Error handling file request', e);
227
- this.emit('error', e);
228
- res.writeHead(500);
229
- res.end();
230
- }
231
- };
232
- this.broadcast = (libraryId, message, omitKeys = []) => {
233
- this.clientConnections.broadcast(libraryId, message, omitKeys);
234
- };
235
- this.send = (libraryId, key, message) => {
236
- this.clientConnections.respond(libraryId, key, message);
237
- };
238
- this.handleMessage = (clientKey, info, message) => {
239
- this.log('Got message from user', info.userId, ', library', info.libraryId, JSON.stringify(message));
240
- return this.library.receive(message, clientKey, info);
241
- };
242
- this.handleConnection = (ws, req, info) => {
243
- const key = generateId();
244
- this.clientConnections.addSocket(info.libraryId, key, ws, info);
245
- ws.on('message', async (message) => {
246
- const data = JSON.parse(message.toString());
247
- if (data.replicaId) {
248
- this.connectionToReplicaIdMap.set(ws, data.replicaId);
249
- this.keepalives.refresh(info.libraryId, data.replicaId);
250
- }
251
- await this.handleMessage(key, info, data);
252
- });
253
- ws.on('close', () => {
254
- const replicaId = this.connectionToReplicaIdMap.get(ws);
255
- if (!replicaId) {
256
- this.emit('warning', 'Unknown replica disconnected', ws);
257
- return;
258
- }
259
- this.library.remove(info.libraryId, replicaId);
260
- this.emit('socket-close', info);
261
- });
262
- this.emit('socket-connection', info);
263
- };
264
- this.listen = (...params) => {
265
- this.httpServer.listen(...params);
266
- };
267
- this.close = async () => {
268
- await Promise.all([
269
- new Promise((resolve) => {
270
- setTimeout(() => {
271
- console.warn('HTTP server close timed out');
272
- resolve();
273
- }, 5 * 1000);
274
- this.httpServer.close(() => {
275
- resolve();
276
- this.log('HTTP server closed');
277
- });
278
- }),
279
- new Promise((resolve) => {
280
- setTimeout(() => {
281
- console.warn('Socket server close timed out');
282
- resolve();
283
- }, 5 * 1000);
284
- this.wss.close(() => {
285
- resolve();
286
- this.log('Socket server closed');
287
- });
288
- }),
289
- ]);
290
- this.library.close();
291
- };
292
- /**
293
- * Completely remove a library from the server.
294
- * This will remove all data associated with the library.
295
- * Use it very carefully! Data will still be stored on user
296
- * devices, so this is not as scary as it sounds - the next time
297
- * any replica connects, it will repopulate the library. But
298
- * you should still be careful.
299
- */
300
- this.evictLibrary = (libraryId) => {
301
- // disconnect all clients
302
- this.clientConnections.disconnectAll(libraryId);
303
- this.library.destroy(libraryId);
304
- this.log('Evicted library', libraryId);
305
- };
306
- this.getLibraryPresence = (libraryId) => {
307
- return this.library.getPresence(libraryId);
308
- };
309
- /**
310
- * Removes all replicas associated with a User ID from
311
- * the server. Consistency algorithms will no longer wait on these
312
- * replicas before confirming and squashing changes.
313
- */
314
- this.evictUser = (libraryId, userId) => {
315
- this.library.evictUser(libraryId, userId);
316
- this.log('Evicted user', userId, 'from library', libraryId);
317
- };
318
- /**
319
- * Retrieves a snapshot of the full document contents at this time.
320
- * Useful for capturing the shape of data for static usage outside
321
- * of the live system.
322
- */
323
- this.getDocumentSnapshot = (libraryId, collection, documentId) => {
324
- return this.library.getDocumentSnapshot(libraryId, createOid(collection, documentId, []));
325
- };
326
- this.__testMode =
327
- process.env.NODE_ENV === 'test' && process.env.VITEST === 'true';
328
- this.log = options.log || this.log;
329
- this.tokenVerifier = new TokenVerifier({
330
- secret: options.tokenSecret,
331
- });
332
- this.fileStorage = options.fileStorage;
333
- const db = create(options.databaseFile);
334
- migrations(db);
335
- this.fileMetadata = new FileMetadata(db, options.fileConfig);
336
- this.library = new ServerLibrary({
337
- db,
338
- sender: this,
339
- profiles: new UserProfileLoader(options.profiles || new DefaultProfiles()),
340
- log: this.log,
341
- disableRebasing: options.disableRebasing,
342
- fileMetadata: this.fileMetadata,
343
- fileStorage: this.fileStorage,
344
- replicaTruancyMinutes: options.replicaTruancyMinutes || 60 * 24 * 14,
345
- });
346
- this.library.subscribe('changes', (_a, operations, baselines) => {
347
- var { token } = _a, info = __rest(_a, ["token"]);
348
- this.emit('changes', info, operations, baselines);
349
- });
350
- this.wss = new WebSocketServer({
351
- noServer: true,
352
- });
353
- this.wss.on('connection', this.handleConnection);
354
- this.httpServer =
355
- options.httpServer || new HttpServer(this.internalServerHandleRequest);
356
- this.httpServer.on('upgrade', async (req, socket, head) => {
357
- try {
358
- const info = this.authorizeRequest(req);
359
- this.wss.handleUpgrade(req, socket, head, (ws) => {
360
- this.wss.emit('connection', ws, req, info);
361
- });
362
- }
363
- catch (e) {
364
- this.emit('error', e);
365
- socket.destroy();
366
- }
367
- });
368
- this.keepalives.subscribe('lost', this.library.remove);
369
- }
370
- }
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { assert, createOid, generateId, } from '@verdant-web/common';
13
+ import EventEmitter from 'events';
14
+ import { Server as HttpServer } from 'http';
15
+ import { WebSocketServer } from 'ws';
16
+ import { UserProfileLoader } from './Profiles.js';
17
+ import create from 'better-sqlite3';
18
+ import { URL } from 'url';
19
+ import { ClientConnectionManager } from './ClientConnection.js';
20
+ import { ReplicaKeepaliveTimers } from './ReplicaKeepaliveTimers.js';
21
+ import { TokenVerifier } from './TokenVerifier.js';
22
+ import busboy from 'busboy';
23
+ import { Readable } from 'stream';
24
+ import { FileMetadata } from './files/FileMetadata.js';
25
+ import { ServerLibrary } from './ServerLibrary.js';
26
+ import { migrations } from './migrations.js';
27
+ class DefaultProfiles {
28
+ constructor() {
29
+ this.get = async (userId) => {
30
+ return { id: userId };
31
+ };
32
+ }
33
+ }
34
+ export class Server extends EventEmitter {
35
+ constructor(options) {
36
+ super();
37
+ this.clientConnections = new ClientConnectionManager();
38
+ this.keepalives = new ReplicaKeepaliveTimers();
39
+ this.connectionToReplicaIdMap = new WeakMap();
40
+ this.log = () => { };
41
+ this.__testMode = false;
42
+ this.authorizeRequest = (req) => {
43
+ return this.tokenVerifier.verifyToken(this.getRequestToken(req));
44
+ };
45
+ this.getRequestToken = (req) => {
46
+ if (req.headers.authorization) {
47
+ const [type, token] = req.headers.authorization.split(' ');
48
+ if (type === 'Bearer') {
49
+ return token;
50
+ }
51
+ }
52
+ if (req.headers['sec-websocket-protocol']) {
53
+ // hack: read the token from the websocket protocol header
54
+ const [type, token] = req.headers['sec-websocket-protocol'].split(',');
55
+ if (type === 'Bearer') {
56
+ return token.trim();
57
+ }
58
+ }
59
+ assert(req.url, 'Request URL is required');
60
+ const url = new URL(req.url, 'http://localhost');
61
+ const token = url.searchParams.get('token');
62
+ assert(token, 'Token is required');
63
+ return token;
64
+ };
65
+ this.internalServerHandleRequest = (req, res) => {
66
+ const url = new URL(req.url || '', 'http://localhost');
67
+ if (url.pathname.startsWith('lofi')) {
68
+ if (url.pathname === 'lofi') {
69
+ return this.handleRequest(req, res);
70
+ }
71
+ else if (url.pathname.startsWith('/lofi/files/')) {
72
+ return this.handleFileRequest(req, res);
73
+ }
74
+ }
75
+ else {
76
+ res.writeHead(404);
77
+ res.end();
78
+ }
79
+ };
80
+ this.handleRequest = async (req, res) => {
81
+ var _a;
82
+ try {
83
+ if (req.method === 'POST') {
84
+ const info = this.authorizeRequest(req);
85
+ const key = generateId();
86
+ const finish = this.clientConnections.addRequest(info.libraryId, key, req, res, info);
87
+ // FIXME: extremely naive compatibility with connect-like
88
+ // servers...
89
+ const body = (_a = req.body) !== null && _a !== void 0 ? _a : (await new Promise((resolve, reject) => {
90
+ let body = '';
91
+ req.on('data', (chunk) => {
92
+ body += chunk;
93
+ });
94
+ req.on('end', () => {
95
+ resolve(JSON.parse(body));
96
+ });
97
+ req.on('error', (e) => {
98
+ reject(e);
99
+ });
100
+ }));
101
+ for (const message of body.messages) {
102
+ await this.handleMessage(key, info, message);
103
+ }
104
+ // update our keepalive timers for presence management
105
+ const firstMessage = body.messages[0];
106
+ if (firstMessage) {
107
+ this.keepalives.refresh(info.libraryId, firstMessage.replicaId);
108
+ }
109
+ finish();
110
+ this.emit('request', info);
111
+ }
112
+ }
113
+ catch (e) {
114
+ this.emit('error', e);
115
+ res.writeHead(500);
116
+ res.end();
117
+ }
118
+ };
119
+ /**
120
+ * Handles a multipart upload of a file from a verdant client. The upload
121
+ * will include parameters for the file's ID, name, and type. The request
122
+ * must be authenticated with a token to tie it to a library.
123
+ */
124
+ this.handleFileRequest = async (req, res) => {
125
+ const fs = this.fileStorage;
126
+ if (!fs) {
127
+ this.emit('error', new Error('No file storage configured, but a client attempted to upload a file.'));
128
+ res.writeHead(500);
129
+ res.write('File storage is not configured');
130
+ res.end();
131
+ return;
132
+ }
133
+ // FIXME: rather than trying to support Express, I should
134
+ // just expose regular methods you call from whatever HTTP handler...
135
+ const url = new URL(req.originalUrl || req.baseUrl || req.url || '', 'http://localhost');
136
+ const id = url.pathname.split('/').pop();
137
+ if (!id || id === 'files') {
138
+ res.writeHead(400);
139
+ res.write('File ID is required to be in the URL path as the last parameter');
140
+ res.end();
141
+ return;
142
+ }
143
+ try {
144
+ if (req.method === 'POST') {
145
+ const info = this.authorizeRequest(req);
146
+ await new Promise((resolve, reject) => {
147
+ const bb = busboy({ headers: req.headers });
148
+ bb.on('file', (fieldName, stream, fileInfo) => {
149
+ // too many 'info's....
150
+ const lofiFileInfo = {
151
+ id,
152
+ libraryId: info.libraryId,
153
+ fileName: fileInfo.filename,
154
+ type: fileInfo.mimeType,
155
+ };
156
+ // write metadata to storage
157
+ try {
158
+ this.fileMetadata.put(info.libraryId, lofiFileInfo);
159
+ fs.put(stream, lofiFileInfo);
160
+ }
161
+ catch (e) {
162
+ reject(e);
163
+ }
164
+ });
165
+ bb.on('field', (fieldName, value) => {
166
+ if (fieldName === 'file') {
167
+ if (this.__testMode) {
168
+ // this isn't right in the real world, but in testing it's
169
+ // the only way we get file data.
170
+ // we create a stream from the data and pass it as if it
171
+ // were a file stream
172
+ const stream = new Readable();
173
+ stream.push(value);
174
+ stream.push(null);
175
+ const fileInfo = {
176
+ filename: 'test.txt',
177
+ mimeType: 'text/plain',
178
+ };
179
+ bb.emit('file', fieldName, stream, fileInfo);
180
+ }
181
+ else {
182
+ throw new Error('Invalid file upload');
183
+ }
184
+ }
185
+ });
186
+ req.pipe(bb);
187
+ bb.on('finish', resolve);
188
+ bb.on('error', reject);
189
+ });
190
+ this.log('File upload complete');
191
+ res.writeHead(200);
192
+ res.write(JSON.stringify({ success: true }));
193
+ res.end();
194
+ }
195
+ else if (req.method === 'GET') {
196
+ const info = this.authorizeRequest(req);
197
+ const fileInfo = this.fileMetadata.get(info.libraryId, id);
198
+ if (!fileInfo) {
199
+ res.writeHead(404);
200
+ res.end();
201
+ return;
202
+ }
203
+ const url = await fs.getUrl({
204
+ fileName: fileInfo.name,
205
+ id: fileInfo.fileId,
206
+ libraryId: info.libraryId,
207
+ type: fileInfo.type,
208
+ });
209
+ res.writeHead(200, {
210
+ 'Content-Type': 'application/json',
211
+ });
212
+ // we need to augment that data with the URL from the file backend.
213
+ // and generally enforce the FileData interface here...
214
+ const data = {
215
+ id: fileInfo.fileId,
216
+ url,
217
+ remote: true,
218
+ name: fileInfo.name,
219
+ type: fileInfo.type,
220
+ };
221
+ res.write(JSON.stringify(data));
222
+ res.end();
223
+ }
224
+ }
225
+ catch (e) {
226
+ this.log('Error handling file request', e);
227
+ this.emit('error', e);
228
+ res.writeHead(500);
229
+ res.end();
230
+ }
231
+ };
232
+ this.broadcast = (libraryId, message, omitKeys = []) => {
233
+ this.clientConnections.broadcast(libraryId, message, omitKeys);
234
+ };
235
+ this.send = (libraryId, key, message) => {
236
+ this.clientConnections.respond(libraryId, key, message);
237
+ };
238
+ this.handleMessage = (clientKey, info, message) => {
239
+ this.log('Got message from user', info.userId, ', library', info.libraryId, JSON.stringify(message));
240
+ return this.library.receive(message, clientKey, info);
241
+ };
242
+ this.handleConnection = (ws, req, info) => {
243
+ const key = generateId();
244
+ this.clientConnections.addSocket(info.libraryId, key, ws, info);
245
+ ws.on('message', async (message) => {
246
+ const data = JSON.parse(message.toString());
247
+ if (data.replicaId) {
248
+ this.connectionToReplicaIdMap.set(ws, data.replicaId);
249
+ this.keepalives.refresh(info.libraryId, data.replicaId);
250
+ }
251
+ await this.handleMessage(key, info, data);
252
+ });
253
+ ws.on('close', () => {
254
+ const replicaId = this.connectionToReplicaIdMap.get(ws);
255
+ if (!replicaId) {
256
+ this.emit('warning', 'Unknown replica disconnected', ws);
257
+ return;
258
+ }
259
+ this.library.remove(info.libraryId, replicaId);
260
+ this.emit('socket-close', info);
261
+ });
262
+ this.emit('socket-connection', info);
263
+ };
264
+ this.listen = (...params) => {
265
+ this.httpServer.listen(...params);
266
+ };
267
+ this.close = async () => {
268
+ await Promise.all([
269
+ new Promise((resolve) => {
270
+ setTimeout(() => {
271
+ console.warn('HTTP server close timed out');
272
+ resolve();
273
+ }, 5 * 1000);
274
+ this.httpServer.close(() => {
275
+ resolve();
276
+ this.log('HTTP server closed');
277
+ });
278
+ }),
279
+ new Promise((resolve) => {
280
+ setTimeout(() => {
281
+ console.warn('Socket server close timed out');
282
+ resolve();
283
+ }, 5 * 1000);
284
+ this.wss.close(() => {
285
+ resolve();
286
+ this.log('Socket server closed');
287
+ });
288
+ }),
289
+ ]);
290
+ this.library.close();
291
+ };
292
+ /**
293
+ * Completely remove a library from the server.
294
+ * This will remove all data associated with the library.
295
+ * Use it very carefully! Data will still be stored on user
296
+ * devices, so this is not as scary as it sounds - the next time
297
+ * any replica connects, it will repopulate the library. But
298
+ * you should still be careful.
299
+ */
300
+ this.evictLibrary = (libraryId) => {
301
+ // disconnect all clients
302
+ this.clientConnections.disconnectAll(libraryId);
303
+ this.library.destroy(libraryId);
304
+ this.log('Evicted library', libraryId);
305
+ };
306
+ this.getLibraryPresence = (libraryId) => {
307
+ return this.library.getPresence(libraryId);
308
+ };
309
+ /**
310
+ * Removes all replicas associated with a User ID from
311
+ * the server. Consistency algorithms will no longer wait on these
312
+ * replicas before confirming and squashing changes.
313
+ */
314
+ this.evictUser = (libraryId, userId) => {
315
+ this.library.evictUser(libraryId, userId);
316
+ this.log('Evicted user', userId, 'from library', libraryId);
317
+ };
318
+ /**
319
+ * Retrieves a snapshot of the full document contents at this time.
320
+ * Useful for capturing the shape of data for static usage outside
321
+ * of the live system.
322
+ */
323
+ this.getDocumentSnapshot = (libraryId, collection, documentId) => {
324
+ return this.library.getDocumentSnapshot(libraryId, createOid(collection, documentId, []));
325
+ };
326
+ this.__testMode =
327
+ process.env.NODE_ENV === 'test' && process.env.VITEST === 'true';
328
+ this.log = options.log || this.log;
329
+ this.tokenVerifier = new TokenVerifier({
330
+ secret: options.tokenSecret,
331
+ });
332
+ this.fileStorage = options.fileStorage;
333
+ const db = create(options.databaseFile);
334
+ migrations(db);
335
+ this.fileMetadata = new FileMetadata(db, options.fileConfig);
336
+ this.library = new ServerLibrary({
337
+ db,
338
+ sender: this,
339
+ profiles: new UserProfileLoader(options.profiles || new DefaultProfiles()),
340
+ log: this.log,
341
+ disableRebasing: options.disableRebasing,
342
+ fileMetadata: this.fileMetadata,
343
+ fileStorage: this.fileStorage,
344
+ replicaTruancyMinutes: options.replicaTruancyMinutes || 60 * 24 * 14,
345
+ });
346
+ this.library.subscribe('changes', (_a, operations, baselines) => {
347
+ var { token } = _a, info = __rest(_a, ["token"]);
348
+ this.emit('changes', info, operations, baselines);
349
+ });
350
+ this.wss = new WebSocketServer({
351
+ noServer: true,
352
+ });
353
+ this.wss.on('connection', this.handleConnection);
354
+ this.httpServer =
355
+ options.httpServer || new HttpServer(this.internalServerHandleRequest);
356
+ this.httpServer.on('upgrade', async (req, socket, head) => {
357
+ try {
358
+ const info = this.authorizeRequest(req);
359
+ this.wss.handleUpgrade(req, socket, head, (ws) => {
360
+ this.wss.emit('connection', ws, req, info);
361
+ });
362
+ }
363
+ catch (e) {
364
+ this.emit('error', e);
365
+ socket.destroy();
366
+ }
367
+ });
368
+ this.keepalives.subscribe('lost', this.library.remove);
369
+ }
370
+ }
371
371
  //# sourceMappingURL=Server.js.map