@verdant-web/server 1.8.13 → 1.8.14-next.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.
Files changed (150) hide show
  1. package/dist/cjs/Baselines.d.ts +15 -15
  2. package/dist/cjs/Baselines.js +83 -83
  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 +36 -36
  8. package/dist/cjs/OperationHistory.js +120 -120
  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 +24 -24
  16. package/dist/cjs/Replicas.js +137 -137
  17. package/dist/cjs/Server.d.ts +143 -143
  18. package/dist/cjs/Server.js +396 -396
  19. package/dist/cjs/ServerLibrary.d.ts +77 -77
  20. package/dist/cjs/ServerLibrary.js +487 -487
  21. package/dist/cjs/ServerStorage.d.ts +35 -0
  22. package/dist/cjs/ServerStorage.js +60 -0
  23. package/dist/cjs/ServerStorage.js.map +1 -0
  24. package/dist/cjs/TokenProvider.d.ts +16 -16
  25. package/dist/cjs/TokenProvider.js +26 -26
  26. package/dist/cjs/TokenVerifier.d.ts +16 -16
  27. package/dist/cjs/TokenVerifier.js +28 -28
  28. package/dist/cjs/files/FileMetadata.d.ts +28 -28
  29. package/dist/cjs/files/FileMetadata.js +46 -46
  30. package/dist/cjs/files/FileStorage.d.ts +35 -35
  31. package/dist/cjs/files/FileStorage.js +71 -71
  32. package/dist/cjs/index.d.ts +9 -9
  33. package/dist/cjs/index.js +11 -11
  34. package/dist/cjs/migrations/v0.d.ts +1 -0
  35. package/dist/cjs/migrations/v0.js +30 -0
  36. package/dist/cjs/migrations/v0.js.map +1 -0
  37. package/dist/cjs/migrations/v0.sql.d.ts +2 -2
  38. package/dist/cjs/migrations/v0.sql.js +3 -3
  39. package/dist/cjs/migrations/v1.d.ts +1 -0
  40. package/dist/cjs/migrations/v1.js +32 -0
  41. package/dist/cjs/migrations/v1.js.map +1 -0
  42. package/dist/cjs/migrations/v1.sql.d.ts +2 -2
  43. package/dist/cjs/migrations/v1.sql.js +3 -3
  44. package/dist/cjs/migrations/v2.d.ts +1 -0
  45. package/dist/cjs/migrations/v2.js +29 -0
  46. package/dist/cjs/migrations/v2.js.map +1 -0
  47. package/dist/cjs/migrations/v2.sql.d.ts +2 -2
  48. package/dist/cjs/migrations/v2.sql.js +4 -4
  49. package/dist/cjs/migrations/v3.d.ts +1 -0
  50. package/dist/cjs/migrations/v3.js +15 -0
  51. package/dist/cjs/migrations/v3.js.map +1 -0
  52. package/dist/cjs/migrations/v3.sql.d.ts +2 -2
  53. package/dist/cjs/migrations/v3.sql.js +4 -4
  54. package/dist/cjs/migrations/v4.d.ts +1 -0
  55. package/dist/cjs/migrations/v4.js +9 -0
  56. package/dist/cjs/migrations/v4.js.map +1 -0
  57. package/dist/cjs/migrations/v4.sql.d.ts +2 -2
  58. package/dist/cjs/migrations/v4.sql.js +4 -4
  59. package/dist/cjs/migrations/v5.d.ts +2 -0
  60. package/dist/cjs/migrations/v5.js +35 -0
  61. package/dist/cjs/migrations/v5.js.map +1 -0
  62. package/dist/cjs/migrations.d.ts +6 -2
  63. package/dist/cjs/migrations.js +62 -32
  64. package/dist/cjs/migrations.js.map +1 -1
  65. package/dist/cjs/migrations.test.d.ts +1 -0
  66. package/dist/cjs/migrations.test.js +121 -0
  67. package/dist/cjs/migrations.test.js.map +1 -0
  68. package/dist/cjs/types.d.ts +22 -22
  69. package/dist/cjs/types.js +2 -2
  70. package/dist/esm/Baselines.d.ts +15 -15
  71. package/dist/esm/Baselines.js +79 -79
  72. package/dist/esm/ClientConnection.d.ts +18 -18
  73. package/dist/esm/ClientConnection.js +101 -101
  74. package/dist/esm/MessageSender.d.ts +5 -5
  75. package/dist/esm/MessageSender.js +1 -1
  76. package/dist/esm/OperationHistory.d.ts +36 -36
  77. package/dist/esm/OperationHistory.js +116 -116
  78. package/dist/esm/Presence.d.ts +15 -15
  79. package/dist/esm/Presence.js +52 -52
  80. package/dist/esm/Profiles.d.ts +10 -10
  81. package/dist/esm/Profiles.js +15 -15
  82. package/dist/esm/ReplicaKeepaliveTimers.d.ts +17 -17
  83. package/dist/esm/ReplicaKeepaliveTimers.js +26 -26
  84. package/dist/esm/Replicas.d.ts +24 -24
  85. package/dist/esm/Replicas.js +133 -133
  86. package/dist/esm/Server.d.ts +143 -143
  87. package/dist/esm/Server.js +389 -389
  88. package/dist/esm/ServerLibrary.d.ts +77 -77
  89. package/dist/esm/ServerLibrary.js +483 -483
  90. package/dist/esm/ServerStorage.d.ts +44 -0
  91. package/dist/esm/ServerStorage.js +58 -0
  92. package/dist/esm/ServerStorage.js.map +1 -0
  93. package/dist/esm/TokenProvider.d.ts +16 -16
  94. package/dist/esm/TokenProvider.js +19 -19
  95. package/dist/esm/TokenVerifier.d.ts +16 -16
  96. package/dist/esm/TokenVerifier.js +21 -21
  97. package/dist/esm/files/FileMetadata.d.ts +28 -28
  98. package/dist/esm/files/FileMetadata.js +42 -42
  99. package/dist/esm/files/FileStorage.d.ts +35 -35
  100. package/dist/esm/files/FileStorage.js +44 -44
  101. package/dist/esm/index.d.ts +9 -9
  102. package/dist/esm/index.js +4 -4
  103. package/dist/esm/migrations/v0.d.ts +1 -0
  104. package/dist/esm/migrations/v0.js +27 -0
  105. package/dist/esm/migrations/v0.js.map +1 -0
  106. package/dist/esm/migrations/v0.sql.d.ts +2 -2
  107. package/dist/esm/migrations/v0.sql.js +1 -1
  108. package/dist/esm/migrations/v1.d.ts +1 -0
  109. package/dist/esm/migrations/v1.js +29 -0
  110. package/dist/esm/migrations/v1.js.map +1 -0
  111. package/dist/esm/migrations/v1.sql.d.ts +2 -2
  112. package/dist/esm/migrations/v1.sql.js +1 -1
  113. package/dist/esm/migrations/v2.d.ts +1 -0
  114. package/dist/esm/migrations/v2.js +26 -0
  115. package/dist/esm/migrations/v2.js.map +1 -0
  116. package/dist/esm/migrations/v2.sql.d.ts +2 -2
  117. package/dist/esm/migrations/v2.sql.js +2 -2
  118. package/dist/esm/migrations/v3.d.ts +1 -0
  119. package/dist/esm/migrations/v3.js +12 -0
  120. package/dist/esm/migrations/v3.js.map +1 -0
  121. package/dist/esm/migrations/v3.sql.d.ts +2 -2
  122. package/dist/esm/migrations/v3.sql.js +2 -2
  123. package/dist/esm/migrations/v4.d.ts +1 -0
  124. package/dist/esm/migrations/v4.js +6 -0
  125. package/dist/esm/migrations/v4.js.map +1 -0
  126. package/dist/esm/migrations/v4.sql.d.ts +2 -2
  127. package/dist/esm/migrations/v4.sql.js +2 -2
  128. package/dist/esm/migrations/v5.d.ts +2 -0
  129. package/dist/esm/migrations/v5.js +31 -0
  130. package/dist/esm/migrations/v5.js.map +1 -0
  131. package/dist/esm/migrations.d.ts +6 -2
  132. package/dist/esm/migrations.js +35 -25
  133. package/dist/esm/migrations.js.map +1 -1
  134. package/dist/esm/migrations.test.d.ts +1 -0
  135. package/dist/esm/migrations.test.js +116 -0
  136. package/dist/esm/migrations.test.js.map +1 -0
  137. package/dist/esm/types.d.ts +22 -22
  138. package/dist/esm/types.js +1 -1
  139. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  140. package/dist/tsconfig.tsbuildinfo +1 -1
  141. package/package.json +6 -4
  142. package/src/__snapshots__/migrations.test.ts.snap +55 -0
  143. package/src/migrations/{v0.sql.ts → v0.ts} +1 -1
  144. package/src/migrations/{v1.sql.ts → v1.ts} +1 -1
  145. package/src/migrations/{v2.sql.ts → v2.ts} +1 -1
  146. package/src/migrations/{v3.sql.ts → v3.ts} +1 -1
  147. package/src/migrations/{v4.sql.ts → v4.ts} +1 -1
  148. package/src/migrations/v5.ts +47 -0
  149. package/src/migrations.test.ts +133 -0
  150. package/src/migrations.ts +28 -19
@@ -1,133 +1,133 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReplicaInfos = void 0;
4
- class ReplicaInfos {
5
- constructor(db, replicaTruancyMinutes) {
6
- this.db = db;
7
- this.replicaTruancyMinutes = replicaTruancyMinutes;
8
- this.get = (libraryId, replicaId) => {
9
- const row = this.db
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReplicaInfos = void 0;
4
+ class ReplicaInfos {
5
+ constructor(db, replicaTruancyMinutes) {
6
+ this.db = db;
7
+ this.replicaTruancyMinutes = replicaTruancyMinutes;
8
+ this.get = (libraryId, replicaId) => {
9
+ const row = this.db
10
10
  .prepare(`
11
11
  SELECT * FROM ReplicaInfo
12
12
  WHERE id = ? AND libraryId = ?
13
- `)
14
- .get(replicaId, libraryId);
15
- if (!row) {
16
- return null;
17
- }
18
- return {
19
- id: row.id,
20
- libraryId: row.libraryId,
21
- clientId: row.clientId,
22
- lastSeenWallClockTime: row.lastSeenWallClockTime,
23
- ackedLogicalTime: row.ackedLogicalTime,
24
- type: row.type,
25
- ackedServerOrder: row.ackedServerOrder,
26
- };
27
- };
28
- this.getOrCreate = (libraryId, replicaId, info) => {
29
- const replicaInfo = this.db
13
+ `)
14
+ .get(replicaId, libraryId);
15
+ if (!row) {
16
+ return null;
17
+ }
18
+ return {
19
+ id: row.id,
20
+ libraryId: row.libraryId,
21
+ clientId: row.clientId,
22
+ lastSeenWallClockTime: row.lastSeenWallClockTime,
23
+ ackedLogicalTime: row.ackedLogicalTime,
24
+ type: row.type,
25
+ ackedServerOrder: row.ackedServerOrder,
26
+ };
27
+ };
28
+ this.getOrCreate = (libraryId, replicaId, info) => {
29
+ const replicaInfo = this.db
30
30
  .prepare(`
31
31
  SELECT * FROM ReplicaInfo
32
32
  WHERE id = ? AND libraryId = ?
33
- `)
34
- .get(replicaId, libraryId);
35
- if (!replicaInfo) {
36
- this.db
33
+ `)
34
+ .get(replicaId, libraryId);
35
+ if (!replicaInfo) {
36
+ this.db
37
37
  .prepare(`
38
38
  INSERT INTO ReplicaInfo (id, clientId, libraryId, type)
39
39
  VALUES (?, ?, ?, ?)
40
- `)
41
- .run(replicaId, info.userId, libraryId, info.type);
42
- return {
43
- status: 'new',
44
- replicaInfo: {
45
- id: replicaId,
46
- clientId: info.userId,
47
- ackedLogicalTime: null,
48
- lastSeenWallClockTime: null,
49
- libraryId: libraryId,
50
- type: info.type,
51
- ackedServerOrder: 0,
52
- },
53
- };
54
- }
55
- if (replicaInfo.type !== info.type) {
56
- // type should be updated if a new token changes it
57
- this.db
40
+ `)
41
+ .run(replicaId, info.userId, libraryId, info.type);
42
+ return {
43
+ status: 'new',
44
+ replicaInfo: {
45
+ id: replicaId,
46
+ clientId: info.userId,
47
+ ackedLogicalTime: null,
48
+ lastSeenWallClockTime: null,
49
+ libraryId: libraryId,
50
+ type: info.type,
51
+ ackedServerOrder: 0,
52
+ },
53
+ };
54
+ }
55
+ if (replicaInfo.type !== info.type) {
56
+ // type should be updated if a new token changes it
57
+ this.db
58
58
  .prepare(`
59
59
  UPDATE ReplicaInfo
60
60
  SET type = ?
61
61
  WHERE id = ? AND libraryId = ?
62
- `)
63
- .run(info.type, replicaId, libraryId);
64
- }
65
- if (replicaInfo.clientId !== info.userId) {
66
- throw new Error(`Replica ${replicaId} already exists with a different user ID`);
67
- }
68
- if (replicaInfo.lastSeenWallClockTime &&
69
- replicaInfo.lastSeenWallClockTime < this.truantCutoff) {
70
- return {
71
- status: 'truant',
72
- replicaInfo,
73
- };
74
- }
75
- return {
76
- status: 'existing',
77
- replicaInfo,
78
- };
79
- };
80
- this.getAllNonTruant = (libraryId) => {
81
- return this.db
62
+ `)
63
+ .run(info.type, replicaId, libraryId);
64
+ }
65
+ if (replicaInfo.clientId !== info.userId) {
66
+ throw new Error(`Replica ${replicaId} already exists with a different user ID`);
67
+ }
68
+ if (replicaInfo.lastSeenWallClockTime &&
69
+ replicaInfo.lastSeenWallClockTime < this.truantCutoff) {
70
+ return {
71
+ status: 'truant',
72
+ replicaInfo,
73
+ };
74
+ }
75
+ return {
76
+ status: 'existing',
77
+ replicaInfo,
78
+ };
79
+ };
80
+ this.getAllNonTruant = (libraryId) => {
81
+ return this.db
82
82
  .prepare(`
83
83
  SELECT * FROM ReplicaInfo
84
84
  WHERE libraryId = ? AND lastSeenWallClockTime > ?
85
- `)
86
- .all(libraryId, this.truantCutoff);
87
- };
88
- this.getAll = (libraryId) => {
89
- return this.db
85
+ `)
86
+ .all(libraryId, this.truantCutoff);
87
+ };
88
+ this.getAll = (libraryId) => {
89
+ return this.db
90
90
  .prepare(`
91
91
  SELECT * FROM ReplicaInfo
92
92
  WHERE libraryId = ?
93
- `)
94
- .all(libraryId);
95
- };
96
- this.updateLastSeen = (libraryId, replicaId) => {
97
- const clockTime = new Date().getTime();
98
- return this.db
93
+ `)
94
+ .all(libraryId);
95
+ };
96
+ this.updateLastSeen = (libraryId, replicaId) => {
97
+ const clockTime = new Date().getTime();
98
+ return this.db
99
99
  .prepare(`
100
100
  UPDATE ReplicaInfo
101
101
  SET lastSeenWallClockTime = ?
102
102
  WHERE id = ? AND libraryId = ?
103
- `)
104
- .run(clockTime, replicaId, libraryId);
105
- };
106
- this.updateAcknowledged = (libraryId, replicaId, timestamp) => {
107
- this.db
103
+ `)
104
+ .run(clockTime, replicaId, libraryId);
105
+ };
106
+ this.updateAcknowledged = (libraryId, replicaId, timestamp) => {
107
+ this.db
108
108
  .prepare(`
109
109
  UPDATE ReplicaInfo
110
110
  SET ackedLogicalTime = ?
111
111
  WHERE id = ? AND libraryId = ?
112
- `)
113
- .run(timestamp, replicaId, libraryId);
114
- };
115
- this.updateServerOrder = (libraryId, replicaId, serverOrder) => {
116
- // only increment the server order, never set it to lower than before
117
- this.db
112
+ `)
113
+ .run(timestamp, replicaId, libraryId);
114
+ };
115
+ this.updateServerOrder = (libraryId, replicaId, serverOrder) => {
116
+ // only increment the server order, never set it to lower than before
117
+ this.db
118
118
  .prepare(`
119
119
  UPDATE ReplicaInfo
120
120
  SET ackedServerOrder = MAX(ackedServerOrder, ?)
121
121
  WHERE id = ? AND libraryId = ?
122
- `)
123
- .run(serverOrder, replicaId, libraryId);
124
- };
125
- this.acknowledgeOperation = (libraryId, replicaId, timestamp) => {
126
- if (!timestamp)
127
- return;
128
- // get the server order from the operation with the timestamp and update the replica's server order
129
- // to the max of the current server order and the server order of the operation
130
- this.db
122
+ `)
123
+ .run(serverOrder, replicaId, libraryId);
124
+ };
125
+ this.acknowledgeOperation = (libraryId, replicaId, timestamp) => {
126
+ if (!timestamp)
127
+ return;
128
+ // get the server order from the operation with the timestamp and update the replica's server order
129
+ // to the max of the current server order and the server order of the operation
130
+ this.db
131
131
  .prepare(`
132
132
  UPDATE ReplicaInfo
133
133
  SET ackedLogicalTime = ?, ackedServerOrder = MAX(ackedServerOrder, COALESCE((
@@ -135,48 +135,48 @@ class ReplicaInfos {
135
135
  WHERE libraryId = ? AND timestamp = ?
136
136
  ), 0))
137
137
  WHERE id = ? AND libraryId = ?
138
- `)
139
- .run(timestamp, libraryId, timestamp, replicaId, libraryId);
140
- };
141
- this.getGlobalAck = (libraryId, onlineReplicaIds) => {
142
- const nonTruant = this.getAllNonTruant(libraryId);
143
- const globalAckEligible = nonTruant.filter((replica) => replica.type < 2 || (onlineReplicaIds === null || onlineReplicaIds === void 0 ? void 0 : onlineReplicaIds.includes(replica.id)));
144
- // get the earliest acked time
145
- return globalAckEligible.reduce((acc, replica) => {
146
- if (!replica.ackedLogicalTime) {
147
- return acc;
148
- }
149
- if (acc === undefined) {
150
- return replica.ackedLogicalTime;
151
- }
152
- return acc < replica.ackedLogicalTime ? acc : replica.ackedLogicalTime;
153
- }, undefined);
154
- };
155
- this.delete = (libraryId, replicaId) => {
156
- return this.db
138
+ `)
139
+ .run(timestamp, libraryId, timestamp, replicaId, libraryId);
140
+ };
141
+ this.getGlobalAck = (libraryId, onlineReplicaIds) => {
142
+ const nonTruant = this.getAllNonTruant(libraryId);
143
+ const globalAckEligible = nonTruant.filter((replica) => replica.type < 2 || (onlineReplicaIds === null || onlineReplicaIds === void 0 ? void 0 : onlineReplicaIds.includes(replica.id)));
144
+ // get the earliest acked time
145
+ return globalAckEligible.reduce((acc, replica) => {
146
+ if (!replica.ackedLogicalTime) {
147
+ return acc;
148
+ }
149
+ if (acc === undefined) {
150
+ return replica.ackedLogicalTime;
151
+ }
152
+ return acc < replica.ackedLogicalTime ? acc : replica.ackedLogicalTime;
153
+ }, undefined);
154
+ };
155
+ this.delete = (libraryId, replicaId) => {
156
+ return this.db
157
157
  .prepare(`
158
158
  DELETE FROM ReplicaInfo
159
159
  WHERE id = ? AND libraryId = ?
160
- `)
161
- .run(replicaId, libraryId);
162
- };
163
- this.deleteAll = (libraryId) => {
164
- return this.db
160
+ `)
161
+ .run(replicaId, libraryId);
162
+ };
163
+ this.deleteAll = (libraryId) => {
164
+ return this.db
165
165
  .prepare(`
166
166
  DELETE FROM ReplicaInfo
167
167
  WHERE libraryId = ?
168
- `)
169
- .run(libraryId);
170
- };
171
- this.deleteAllForUser = (libraryId, userId) => {
172
- return this.db
173
- .prepare(`DELETE FROM ReplicaInfo WHERE libraryId = ? AND clientId = ?`)
174
- .run(libraryId, userId);
175
- };
176
- }
177
- get truantCutoff() {
178
- return Date.now() - this.replicaTruancyMinutes * 60 * 1000;
179
- }
180
- }
181
- exports.ReplicaInfos = ReplicaInfos;
168
+ `)
169
+ .run(libraryId);
170
+ };
171
+ this.deleteAllForUser = (libraryId, userId) => {
172
+ return this.db
173
+ .prepare(`DELETE FROM ReplicaInfo WHERE libraryId = ? AND clientId = ?`)
174
+ .run(libraryId, userId);
175
+ };
176
+ }
177
+ get truantCutoff() {
178
+ return Date.now() - this.replicaTruancyMinutes * 60 * 1000;
179
+ }
180
+ }
181
+ exports.ReplicaInfos = ReplicaInfos;
182
182
  //# sourceMappingURL=Replicas.js.map
@@ -1,143 +1,143 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
- import { DocumentBaseline, Operation, ServerMessage } from '@verdant-web/common';
4
- import EventEmitter from 'events';
5
- import { IncomingMessage, Server as HttpServer, ServerResponse } from 'http';
6
- import { UserProfiles } from './Profiles.js';
7
- import { MessageSender } from './MessageSender.js';
8
- import { TokenInfo } from './TokenVerifier.js';
9
- import { FileStorage } from './files/FileStorage.js';
10
- import { FileMetadataConfig } from './files/FileMetadata.js';
11
- export interface ServerOptions {
12
- /**
13
- * Attach to a pre-existing HTTP server. Provide this if you have another server,
14
- * like an Express server, that you want to add lofi to. If provided, you don't
15
- * need to call .listen()
16
- */
17
- httpServer?: HttpServer;
18
- /**
19
- * The secret value used to generate tokens from your authentication endpoint.
20
- * Used to verify and decode user identity and library access on connection.
21
- */
22
- tokenSecret: string;
23
- /**
24
- * The path to your SQLite database.
25
- */
26
- databaseFile: string;
27
- /**
28
- * Optionally provide an implementation of the UserProfiles interface to look up
29
- * static user profile data to use in persistence. This data is considered trusted
30
- * and can be used to identify users with information like name or profile image.
31
- */
32
- profiles?: UserProfiles<any>;
33
- /**
34
- * How many minutes of inactivity before a replica is considered truant and
35
- * removed from the library. Defaults to 30 days.
36
- */
37
- replicaTruancyMinutes?: number;
38
- /**
39
- * Supply a logging function to log debug messages.
40
- */
41
- log?: (...args: any[]) => void;
42
- /**
43
- * Disable history compaction. Storage usage will grow indefinitely. Not recommended.
44
- */
45
- disableRebasing?: boolean;
46
- /**
47
- * Provide a FileStorage backend to accept file uploads. Without a file backend,
48
- * users will not be able to synchronize files, and all files added on peer replicas
49
- * will be forever unavailable.
50
- *
51
- * A default, filesystem-backed FileStorage implementation is provided as an export
52
- * of this library.
53
- */
54
- fileStorage?: FileStorage;
55
- fileConfig?: FileMetadataConfig;
56
- }
57
- export interface ServerEvents {
58
- error: (err: any) => void;
59
- warning: (msg: string, ...args: any[]) => void;
60
- changes: (info: Omit<TokenInfo, 'token'>, operations: Operation[], baselines: DocumentBaseline[]) => void;
61
- request: (info: TokenInfo) => void;
62
- 'socket-close': (info: TokenInfo) => void;
63
- 'socket-connection': (info: TokenInfo) => void;
64
- }
65
- export declare interface Server {
66
- on<Event extends keyof ServerEvents>(ev: Event, cb: ServerEvents[Event]): this;
67
- off<Event extends keyof ServerEvents>(ev: Event, cb: ServerEvents[Event]): this;
68
- emit<Event extends keyof ServerEvents>(ev: Event, ...args: Parameters<ServerEvents[Event]>): boolean;
69
- }
70
- export declare class Server extends EventEmitter implements MessageSender {
71
- readonly httpServer: HttpServer;
72
- private wss;
73
- private fileStorage?;
74
- private fileMetadata;
75
- private library;
76
- private clientConnections;
77
- private keepalives;
78
- private tokenVerifier;
79
- private connectionToReplicaIdMap;
80
- private log;
81
- private __testMode;
82
- constructor(options: ServerOptions);
83
- private authorizeRequest;
84
- private getRequestToken;
85
- private internalServerHandleRequest;
86
- handleRequest: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
87
- /**
88
- * Handles a multipart upload of a file from a verdant client. The upload
89
- * will include parameters for the file's ID, name, and type. The request
90
- * must be authenticated with a token to tie it to a library.
91
- */
92
- handleFileRequest: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
93
- broadcast: (libraryId: string, message: ServerMessage, omitKeys?: string[]) => void;
94
- send: (libraryId: string, key: string, message: ServerMessage) => void;
95
- private handleMessage;
96
- private handleConnection;
97
- listen: (handle: any, listeningListener?: (() => void) | undefined) => void;
98
- close: () => Promise<void>;
99
- /**
100
- * Completely remove a library from the server.
101
- * This will remove all data associated with the library.
102
- * Use it very carefully! Data will still be stored on user
103
- * devices, so this is not as scary as it sounds - the next time
104
- * any replica connects, it will repopulate the library. But
105
- * you should still be careful.
106
- */
107
- evictLibrary: (libraryId: string) => void;
108
- /**
109
- * Returns currently connected replica info for a library
110
- */
111
- getLibraryPresence: (libraryId: string) => Record<string, import("@verdant-web/common").UserInfo<any, any>>;
112
- /**
113
- * Returns helpful information about a library.
114
- * This is currently only the known replicas and some
115
- * more low-level metadata.
116
- */
117
- getLibraryInfo: (libraryId: string) => Promise<{
118
- replicas: {
119
- id: string;
120
- ackedLogicalTime: string | null;
121
- ackedServerOrder: number;
122
- type: import("@verdant-web/common").ReplicaType;
123
- truant: boolean | 0 | null;
124
- profile: any;
125
- }[];
126
- latestServerOrder: number;
127
- operationsCount: number;
128
- baselinesCount: number;
129
- globalAck: string | undefined;
130
- }>;
131
- /**
132
- * Removes all replicas associated with a User ID from
133
- * the server. Consistency algorithms will no longer wait on these
134
- * replicas before confirming and squashing changes.
135
- */
136
- evictUser: (libraryId: string, userId: string) => void;
137
- /**
138
- * Retrieves a snapshot of the full document contents at this time.
139
- * Useful for capturing the shape of data for static usage outside
140
- * of the live system.
141
- */
142
- getDocumentSnapshot: (libraryId: string, collection: string, documentId: string) => any;
143
- }
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import { DocumentBaseline, Operation, ServerMessage } from '@verdant-web/common';
4
+ import EventEmitter from 'events';
5
+ import { IncomingMessage, Server as HttpServer, ServerResponse } from 'http';
6
+ import { UserProfiles } from './Profiles.js';
7
+ import { MessageSender } from './MessageSender.js';
8
+ import { TokenInfo } from './TokenVerifier.js';
9
+ import { FileStorage } from './files/FileStorage.js';
10
+ import { FileMetadataConfig } from './files/FileMetadata.js';
11
+ export interface ServerOptions {
12
+ /**
13
+ * Attach to a pre-existing HTTP server. Provide this if you have another server,
14
+ * like an Express server, that you want to add lofi to. If provided, you don't
15
+ * need to call .listen()
16
+ */
17
+ httpServer?: HttpServer;
18
+ /**
19
+ * The secret value used to generate tokens from your authentication endpoint.
20
+ * Used to verify and decode user identity and library access on connection.
21
+ */
22
+ tokenSecret: string;
23
+ /**
24
+ * The path to your SQLite database.
25
+ */
26
+ databaseFile: string;
27
+ /**
28
+ * Optionally provide an implementation of the UserProfiles interface to look up
29
+ * static user profile data to use in persistence. This data is considered trusted
30
+ * and can be used to identify users with information like name or profile image.
31
+ */
32
+ profiles?: UserProfiles<any>;
33
+ /**
34
+ * How many minutes of inactivity before a replica is considered truant and
35
+ * removed from the library. Defaults to 30 days.
36
+ */
37
+ replicaTruancyMinutes?: number;
38
+ /**
39
+ * Supply a logging function to log debug messages.
40
+ */
41
+ log?: (...args: any[]) => void;
42
+ /**
43
+ * Disable history compaction. Storage usage will grow indefinitely. Not recommended.
44
+ */
45
+ disableRebasing?: boolean;
46
+ /**
47
+ * Provide a FileStorage backend to accept file uploads. Without a file backend,
48
+ * users will not be able to synchronize files, and all files added on peer replicas
49
+ * will be forever unavailable.
50
+ *
51
+ * A default, filesystem-backed FileStorage implementation is provided as an export
52
+ * of this library.
53
+ */
54
+ fileStorage?: FileStorage;
55
+ fileConfig?: FileMetadataConfig;
56
+ }
57
+ export interface ServerEvents {
58
+ error: (err: any) => void;
59
+ warning: (msg: string, ...args: any[]) => void;
60
+ changes: (info: Omit<TokenInfo, 'token'>, operations: Operation[], baselines: DocumentBaseline[]) => void;
61
+ request: (info: TokenInfo) => void;
62
+ 'socket-close': (info: TokenInfo) => void;
63
+ 'socket-connection': (info: TokenInfo) => void;
64
+ }
65
+ export declare interface Server {
66
+ on<Event extends keyof ServerEvents>(ev: Event, cb: ServerEvents[Event]): this;
67
+ off<Event extends keyof ServerEvents>(ev: Event, cb: ServerEvents[Event]): this;
68
+ emit<Event extends keyof ServerEvents>(ev: Event, ...args: Parameters<ServerEvents[Event]>): boolean;
69
+ }
70
+ export declare class Server extends EventEmitter implements MessageSender {
71
+ readonly httpServer: HttpServer;
72
+ private wss;
73
+ private fileStorage?;
74
+ private fileMetadata;
75
+ private library;
76
+ private clientConnections;
77
+ private keepalives;
78
+ private tokenVerifier;
79
+ private connectionToReplicaIdMap;
80
+ private log;
81
+ private __testMode;
82
+ constructor(options: ServerOptions);
83
+ private authorizeRequest;
84
+ private getRequestToken;
85
+ private internalServerHandleRequest;
86
+ handleRequest: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
87
+ /**
88
+ * Handles a multipart upload of a file from a verdant client. The upload
89
+ * will include parameters for the file's ID, name, and type. The request
90
+ * must be authenticated with a token to tie it to a library.
91
+ */
92
+ handleFileRequest: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
93
+ broadcast: (libraryId: string, message: ServerMessage, omitKeys?: string[]) => void;
94
+ send: (libraryId: string, key: string, message: ServerMessage) => void;
95
+ private handleMessage;
96
+ private handleConnection;
97
+ listen: (handle: any, listeningListener?: (() => void) | undefined) => void;
98
+ close: () => Promise<void>;
99
+ /**
100
+ * Completely remove a library from the server.
101
+ * This will remove all data associated with the library.
102
+ * Use it very carefully! Data will still be stored on user
103
+ * devices, so this is not as scary as it sounds - the next time
104
+ * any replica connects, it will repopulate the library. But
105
+ * you should still be careful.
106
+ */
107
+ evictLibrary: (libraryId: string) => void;
108
+ /**
109
+ * Returns currently connected replica info for a library
110
+ */
111
+ getLibraryPresence: (libraryId: string) => Record<string, import("@verdant-web/common").UserInfo<any, any>>;
112
+ /**
113
+ * Returns helpful information about a library.
114
+ * This is currently only the known replicas and some
115
+ * more low-level metadata.
116
+ */
117
+ getLibraryInfo: (libraryId: string) => Promise<{
118
+ replicas: {
119
+ id: string;
120
+ ackedLogicalTime: string | null;
121
+ ackedServerOrder: number;
122
+ type: import("@verdant-web/common").ReplicaType;
123
+ truant: boolean | 0 | null;
124
+ profile: any;
125
+ }[];
126
+ latestServerOrder: number;
127
+ operationsCount: number;
128
+ baselinesCount: number;
129
+ globalAck: string | undefined;
130
+ }>;
131
+ /**
132
+ * Removes all replicas associated with a User ID from
133
+ * the server. Consistency algorithms will no longer wait on these
134
+ * replicas before confirming and squashing changes.
135
+ */
136
+ evictUser: (libraryId: string, userId: string) => void;
137
+ /**
138
+ * Retrieves a snapshot of the full document contents at this time.
139
+ * Useful for capturing the shape of data for static usage outside
140
+ * of the live system.
141
+ */
142
+ getDocumentSnapshot: (libraryId: string, collection: string, documentId: string) => any;
143
+ }