nowity 1.9.0 → 1.11.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.
- package/README.md +124 -16
- package/dist/client/clock.d.ts +98 -0
- package/dist/client/clock.d.ts.map +1 -0
- package/dist/client/clock.js +154 -0
- package/dist/client/devtools.d.ts +64 -1
- package/dist/client/devtools.d.ts.map +1 -1
- package/dist/client/devtools.js +134 -22
- package/dist/client/nowity-client.d.ts +38 -0
- package/dist/client/nowity-client.d.ts.map +1 -1
- package/dist/client/nowity-client.js +93 -3
- package/dist/client.d.ts +3 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +4 -1
- package/dist/core/protocol.d.ts +70 -2
- package/dist/core/protocol.d.ts.map +1 -1
- package/dist/core/types.d.ts +13 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/devtools.d.ts +14 -1
- package/dist/devtools.d.ts.map +1 -1
- package/dist/devtools.js +64 -18
- package/dist/server/api/configure.d.ts +5 -1
- package/dist/server/api/configure.d.ts.map +1 -1
- package/dist/server/api/configure.js +2 -0
- package/dist/server/change-source/change-source.d.ts +23 -5
- package/dist/server/change-source/change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.d.ts +47 -12
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.js +154 -65
- package/dist/server/change-source/pg-snapshot.d.ts +25 -0
- package/dist/server/change-source/pg-snapshot.d.ts.map +1 -0
- package/dist/server/change-source/pg-snapshot.js +43 -0
- package/dist/server/sync-engine.d.ts +119 -0
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +445 -117
- package/dist/server/ws-server.d.ts +28 -0
- package/dist/server/ws-server.d.ts.map +1 -1
- package/dist/server/ws-server.js +112 -6
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as http from "http";
|
|
2
|
+
import type { SessionInfo } from "./query";
|
|
2
3
|
import type { SyncEngine } from "./sync-engine";
|
|
3
4
|
export interface AuthRequest {
|
|
4
5
|
url: string | undefined;
|
|
@@ -42,6 +43,17 @@ export interface NowitySyncServerOptions {
|
|
|
42
43
|
intervalMs?: number;
|
|
43
44
|
maxMissed?: number;
|
|
44
45
|
} | false;
|
|
46
|
+
/**
|
|
47
|
+
* Send a connection where its writes spent their time on the server —
|
|
48
|
+
* received, started, committed, read from the outbox, pushed (`timing` on
|
|
49
|
+
* `mutation-result` and `tx`), for a developer panel to lay next to the
|
|
50
|
+
* client's own times.
|
|
51
|
+
*
|
|
52
|
+
* Off unless set. A function decides per connection, once, at its hello —
|
|
53
|
+
* the place to say "admins only" from the database rather than from
|
|
54
|
+
* anything the client claims. Its failure means off.
|
|
55
|
+
*/
|
|
56
|
+
timing?: boolean | ((session: SessionInfo) => boolean | Promise<boolean>);
|
|
45
57
|
onError?: (err: Error) => void;
|
|
46
58
|
}
|
|
47
59
|
/** WebSocket front end for {@link SyncEngine}. */
|
|
@@ -58,11 +70,25 @@ export declare class NowitySyncServer {
|
|
|
58
70
|
* processed before the session exists.
|
|
59
71
|
*/
|
|
60
72
|
private readonly chains;
|
|
73
|
+
/**
|
|
74
|
+
* Where a socket's mutations wait: behind the last `hello` (the session
|
|
75
|
+
* they are written under) and the mutations before them, not behind
|
|
76
|
+
* subscribes. A subscribe can take as long as its query; a move should not.
|
|
77
|
+
*/
|
|
78
|
+
private readonly mutationChains;
|
|
79
|
+
/** Settles once the socket's last `hello` has been handled. */
|
|
80
|
+
private readonly helloBarriers;
|
|
61
81
|
/** Registered on a borrowed server, and removed again on close. */
|
|
62
82
|
private upgradeHandler;
|
|
63
83
|
/** Pings sent since the socket last proved it is alive. */
|
|
64
84
|
private readonly missed;
|
|
65
85
|
private heartbeatTimer;
|
|
86
|
+
/** When this socket's last hello arrived, for the clock in the welcome. */
|
|
87
|
+
private readonly helloAt;
|
|
88
|
+
/** Sockets that get timing (see {@link NowitySyncServerOptions.timing}). */
|
|
89
|
+
private readonly timed;
|
|
90
|
+
/** Pongs sent in the current window, per socket. */
|
|
91
|
+
private readonly pings;
|
|
66
92
|
constructor(opts: NowitySyncServerOptions);
|
|
67
93
|
/** Start listening; resolves with the actual port. */
|
|
68
94
|
listen(): Promise<number>;
|
|
@@ -73,6 +99,8 @@ export declare class NowitySyncServer {
|
|
|
73
99
|
private startHeartbeat;
|
|
74
100
|
private handleConnection;
|
|
75
101
|
private handleMessage;
|
|
102
|
+
private allowPing;
|
|
103
|
+
private timingFor;
|
|
76
104
|
private sendTo;
|
|
77
105
|
}
|
|
78
106
|
//# sourceMappingURL=ws-server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-server.d.ts","sourceRoot":"","sources":["../../src/server/ws-server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"ws-server.d.ts","sourceRoot":"","sources":["../../src/server/ws-server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAI7B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAiB,UAAU,EAAE,MAAM,eAAe,CAAC;AAE/D,MAAM,WAAW,WAAW;IACxB,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,OAAO,EAAE,IAAI,CAAC,mBAAmB,CAAC;CACrC;AAED,yEAAyE;AACzE,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,uBAAuB;IACpC,MAAM,EAAE,UAAU,CAAC;IACnB;;;OAGG;IACH,YAAY,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,UAAU,GAAG,IAAI,CAAC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AClD,oDAAoD;IACpD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8D;gBAExE,IAAI,EAAE,uBAAuB;IAKzC,sDAAsD;IAChD,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;IAyCzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B5B,IAAI,eAAe,IAAI,MAAM,CAE5B;IAED,4DAA4D;IAC5D,IAAI,mBAAmB,IAAI,MAAM,GAAG,IAAI,CAKvC;IAED,OAAO,CAAC,cAAc;YA4BR,gBAAgB;YAsFhB,aAAa;IA0I3B,OAAO,CAAC,SAAS;YAUH,SAAS;IAWvB,OAAO,CAAC,MAAM;CAkBjB"}
|
package/dist/server/ws-server.js
CHANGED
|
@@ -37,6 +37,13 @@ exports.NowitySyncServer = void 0;
|
|
|
37
37
|
const http = __importStar(require("http"));
|
|
38
38
|
const ws_1 = require("ws");
|
|
39
39
|
const protocol_1 = require("../core/protocol");
|
|
40
|
+
/**
|
|
41
|
+
* Pongs a socket gets per {@link PING_WINDOW_MS}. A client sends a burst of a
|
|
42
|
+
* few after each welcome and one every 30 s; anything beyond is not clock sync,
|
|
43
|
+
* and is answered with silence rather than with work done out of turn.
|
|
44
|
+
*/
|
|
45
|
+
const PINGS_PER_WINDOW = 10;
|
|
46
|
+
const PING_WINDOW_MS = 10000;
|
|
40
47
|
/** Largest context a connection may set, serialised. It is stored per socket. */
|
|
41
48
|
const MAX_CONTEXT_BYTES = 4096;
|
|
42
49
|
/** WebSocket front end for {@link SyncEngine}. */
|
|
@@ -52,11 +59,25 @@ class NowitySyncServer {
|
|
|
52
59
|
* processed before the session exists.
|
|
53
60
|
*/
|
|
54
61
|
this.chains = new WeakMap();
|
|
62
|
+
/**
|
|
63
|
+
* Where a socket's mutations wait: behind the last `hello` (the session
|
|
64
|
+
* they are written under) and the mutations before them, not behind
|
|
65
|
+
* subscribes. A subscribe can take as long as its query; a move should not.
|
|
66
|
+
*/
|
|
67
|
+
this.mutationChains = new WeakMap();
|
|
68
|
+
/** Settles once the socket's last `hello` has been handled. */
|
|
69
|
+
this.helloBarriers = new WeakMap();
|
|
55
70
|
/** Registered on a borrowed server, and removed again on close. */
|
|
56
71
|
this.upgradeHandler = null;
|
|
57
72
|
/** Pings sent since the socket last proved it is alive. */
|
|
58
73
|
this.missed = new WeakMap();
|
|
59
74
|
this.heartbeatTimer = null;
|
|
75
|
+
/** When this socket's last hello arrived, for the clock in the welcome. */
|
|
76
|
+
this.helloAt = new WeakMap();
|
|
77
|
+
/** Sockets that get timing (see {@link NowitySyncServerOptions.timing}). */
|
|
78
|
+
this.timed = new WeakSet();
|
|
79
|
+
/** Pongs sent in the current window, per socket. */
|
|
80
|
+
this.pings = new WeakMap();
|
|
60
81
|
this.opts = opts;
|
|
61
82
|
this.onError = opts.onError ?? ((err) => console.error("[nowity/ws]", err));
|
|
62
83
|
}
|
|
@@ -171,7 +192,10 @@ class NowitySyncServer {
|
|
|
171
192
|
const authenticated = new Promise((resolve) => (settleAuth = resolve));
|
|
172
193
|
// Anything from the peer proves it is there, not only a pong.
|
|
173
194
|
socket.on("pong", () => this.missed.set(socket, 0));
|
|
195
|
+
let authed = false;
|
|
174
196
|
socket.on("message", (data) => {
|
|
197
|
+
// First, before parsing: every later step is the server's time, not the wire's.
|
|
198
|
+
const receivedAt = Date.now();
|
|
175
199
|
this.missed.set(socket, 0);
|
|
176
200
|
let message;
|
|
177
201
|
try {
|
|
@@ -181,11 +205,38 @@ class NowitySyncServer {
|
|
|
181
205
|
this.onError(err);
|
|
182
206
|
return;
|
|
183
207
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
208
|
+
if (message.t === "ping") {
|
|
209
|
+
// Answered now, not in turn: behind a slow mutation in the
|
|
210
|
+
// chain it would measure the mutation instead of the wire.
|
|
211
|
+
if (authed && typeof message.id === "number" && typeof message.t0 === "number" && this.allowPing(socket, receivedAt)) {
|
|
212
|
+
this.sendTo(socket, { t: "pong", id: message.id, t0: message.t0, t1: receivedAt, t2: Date.now() });
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const handle = (auth) => auth ? this.handleMessage(socket, auth, message, receivedAt) : undefined;
|
|
217
|
+
const barrier = this.helloBarriers.get(socket);
|
|
218
|
+
if (message.t === "mutate" && barrier) {
|
|
219
|
+
// Mutations keep their order among themselves — they are
|
|
220
|
+
// chained here and again per session in the engine — and keep
|
|
221
|
+
// waiting for the session they belong to; only the queue of
|
|
222
|
+
// subscribes is no longer in front of them. Before any hello
|
|
223
|
+
// they go the ordinary way, to be told to say hello.
|
|
224
|
+
const previous = this.mutationChains.get(socket) ?? Promise.resolve();
|
|
225
|
+
const next = previous.then(() => barrier).then(() => authenticated).then(handle);
|
|
226
|
+
this.mutationChains.set(socket, next.catch((err) => this.onError(err)));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
// A hello still waits for everything before it, mutations included:
|
|
230
|
+
// it may replace the session they are being written under.
|
|
231
|
+
const control = this.chains.get(socket) ?? Promise.resolve();
|
|
232
|
+
const previous = message.t === "hello"
|
|
233
|
+
? Promise.all([control, this.mutationChains.get(socket)]).then(() => undefined)
|
|
234
|
+
: control;
|
|
235
|
+
const next = previous.then(() => authenticated).then(handle);
|
|
236
|
+
const settled = next.catch((err) => this.onError(err));
|
|
237
|
+
this.chains.set(socket, settled);
|
|
238
|
+
if (message.t === "hello")
|
|
239
|
+
this.helloBarriers.set(socket, settled);
|
|
189
240
|
});
|
|
190
241
|
socket.on("close", () => {
|
|
191
242
|
settleAuth(null);
|
|
@@ -206,11 +257,12 @@ class NowitySyncServer {
|
|
|
206
257
|
this.onError(err);
|
|
207
258
|
auth = null;
|
|
208
259
|
}
|
|
260
|
+
authed = auth !== null;
|
|
209
261
|
settleAuth(auth ?? null);
|
|
210
262
|
if (!auth)
|
|
211
263
|
socket.close(4401, "unauthorized");
|
|
212
264
|
}
|
|
213
|
-
async handleMessage(socket, auth, message) {
|
|
265
|
+
async handleMessage(socket, auth, message, receivedAt) {
|
|
214
266
|
try {
|
|
215
267
|
if (message.t === "hello") {
|
|
216
268
|
if (message.v !== protocol_1.PROTOCOL_VERSION) {
|
|
@@ -233,7 +285,14 @@ class NowitySyncServer {
|
|
|
233
285
|
if (existing)
|
|
234
286
|
await this.opts.engine.closeSession(existing);
|
|
235
287
|
const context = validContext(message.context) ? (message.context ?? null) : null;
|
|
288
|
+
const timing = await this.timingFor(info);
|
|
289
|
+
if (timing)
|
|
290
|
+
this.timed.add(socket);
|
|
291
|
+
else
|
|
292
|
+
this.timed.delete(socket);
|
|
293
|
+
this.helloAt.set(socket, receivedAt);
|
|
236
294
|
const session = this.opts.engine.openSession(info, (msg) => this.sendTo(socket, msg), context);
|
|
295
|
+
session.timing = timing;
|
|
237
296
|
this.sessions.set(socket, session);
|
|
238
297
|
// The socket may have closed while the previous session was
|
|
239
298
|
// being closed above. Its close handler has run already and
|
|
@@ -293,6 +352,14 @@ class NowitySyncServer {
|
|
|
293
352
|
}
|
|
294
353
|
case "mutate": {
|
|
295
354
|
const outcome = await this.opts.engine.mutate(session, message.mutationId, message.name, message.args);
|
|
355
|
+
const timing = this.timed.has(socket)
|
|
356
|
+
? {
|
|
357
|
+
receivedAt,
|
|
358
|
+
startedAt: outcome.startedAt ?? receivedAt,
|
|
359
|
+
...(outcome.committedAt !== undefined ? { committedAt: outcome.committedAt } : {}),
|
|
360
|
+
sentAt: 0,
|
|
361
|
+
}
|
|
362
|
+
: undefined;
|
|
296
363
|
this.sendTo(socket, {
|
|
297
364
|
t: "mutation-result",
|
|
298
365
|
mutationId: message.mutationId,
|
|
@@ -300,6 +367,7 @@ class NowitySyncServer {
|
|
|
300
367
|
...(outcome.error ? { error: outcome.error } : {}),
|
|
301
368
|
...(outcome.value !== undefined ? { value: outcome.value } : {}),
|
|
302
369
|
lastMutationId: outcome.lastMutationId,
|
|
370
|
+
...(timing ? { timing } : {}),
|
|
303
371
|
});
|
|
304
372
|
break;
|
|
305
373
|
}
|
|
@@ -314,9 +382,47 @@ class NowitySyncServer {
|
|
|
314
382
|
});
|
|
315
383
|
}
|
|
316
384
|
}
|
|
385
|
+
allowPing(socket, now) {
|
|
386
|
+
let window = this.pings.get(socket);
|
|
387
|
+
if (!window || now - window.since >= PING_WINDOW_MS) {
|
|
388
|
+
window = { since: now, count: 0 };
|
|
389
|
+
this.pings.set(socket, window);
|
|
390
|
+
}
|
|
391
|
+
window.count += 1;
|
|
392
|
+
return window.count <= PINGS_PER_WINDOW;
|
|
393
|
+
}
|
|
394
|
+
async timingFor(info) {
|
|
395
|
+
const timing = this.opts.timing;
|
|
396
|
+
if (typeof timing !== "function")
|
|
397
|
+
return timing === true;
|
|
398
|
+
try {
|
|
399
|
+
return (await timing(info)) === true;
|
|
400
|
+
}
|
|
401
|
+
catch (err) {
|
|
402
|
+
this.onError(err);
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
317
406
|
sendTo(socket, message) {
|
|
318
407
|
if (socket.readyState !== socket.OPEN)
|
|
319
408
|
return;
|
|
409
|
+
// Stamped as late as possible: the time it leaves is what the client's
|
|
410
|
+
// clock sync and the panel's axis are measured against.
|
|
411
|
+
switch (message.t) {
|
|
412
|
+
case "welcome": {
|
|
413
|
+
const receivedAt = this.helloAt.get(socket);
|
|
414
|
+
if (receivedAt !== undefined)
|
|
415
|
+
message.clock = { receivedAt, sentAt: Date.now() };
|
|
416
|
+
if (this.timed.has(socket))
|
|
417
|
+
message.timing = true;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case "mutation-result":
|
|
421
|
+
case "tx":
|
|
422
|
+
if (message.timing)
|
|
423
|
+
message.timing.sentAt = Date.now();
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
320
426
|
socket.send(JSON.stringify(message));
|
|
321
427
|
}
|
|
322
428
|
}
|