nowity 1.10.0 → 1.12.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 +89 -15
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/core/protocol.d.ts +40 -0
- package/dist/core/protocol.d.ts.map +1 -1
- package/dist/core/types.d.ts +7 -0
- package/dist/core/types.d.ts.map +1 -1
- 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 +46 -12
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.js +153 -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 +130 -1
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +475 -111
- package/dist/server/ws-server.d.ts +20 -1
- package/dist/server/ws-server.d.ts.map +1 -1
- package/dist/server/ws-server.js +47 -11
- package/package.json +1 -1
|
@@ -54,6 +54,15 @@ export interface NowitySyncServerOptions {
|
|
|
54
54
|
* anything the client claims. Its failure means off.
|
|
55
55
|
*/
|
|
56
56
|
timing?: boolean | ((session: SessionInfo) => boolean | Promise<boolean>);
|
|
57
|
+
/**
|
|
58
|
+
* Send a connection how far each of its subscriptions could be narrowed
|
|
59
|
+
* (`narrowing` on `snapshot` and `tx`, see `SubNarrowing`) — which ones
|
|
60
|
+
* every change recomputes. Table and column names only, never a value.
|
|
61
|
+
*
|
|
62
|
+
* Off unless set; decided per connection at its hello like {@link timing},
|
|
63
|
+
* a failure means off.
|
|
64
|
+
*/
|
|
65
|
+
narrowing?: boolean | ((session: SessionInfo) => boolean | Promise<boolean>);
|
|
57
66
|
onError?: (err: Error) => void;
|
|
58
67
|
}
|
|
59
68
|
/** WebSocket front end for {@link SyncEngine}. */
|
|
@@ -70,6 +79,14 @@ export declare class NowitySyncServer {
|
|
|
70
79
|
* processed before the session exists.
|
|
71
80
|
*/
|
|
72
81
|
private readonly chains;
|
|
82
|
+
/**
|
|
83
|
+
* Where a socket's mutations wait: behind the last `hello` (the session
|
|
84
|
+
* they are written under) and the mutations before them, not behind
|
|
85
|
+
* subscribes. A subscribe can take as long as its query; a move should not.
|
|
86
|
+
*/
|
|
87
|
+
private readonly mutationChains;
|
|
88
|
+
/** Settles once the socket's last `hello` has been handled. */
|
|
89
|
+
private readonly helloBarriers;
|
|
73
90
|
/** Registered on a borrowed server, and removed again on close. */
|
|
74
91
|
private upgradeHandler;
|
|
75
92
|
/** Pings sent since the socket last proved it is alive. */
|
|
@@ -79,6 +96,8 @@ export declare class NowitySyncServer {
|
|
|
79
96
|
private readonly helloAt;
|
|
80
97
|
/** Sockets that get timing (see {@link NowitySyncServerOptions.timing}). */
|
|
81
98
|
private readonly timed;
|
|
99
|
+
/** Sockets that get narrowing (see {@link NowitySyncServerOptions.narrowing}). */
|
|
100
|
+
private readonly narrowed;
|
|
82
101
|
/** Pongs sent in the current window, per socket. */
|
|
83
102
|
private readonly pings;
|
|
84
103
|
constructor(opts: NowitySyncServerOptions);
|
|
@@ -92,7 +111,7 @@ export declare class NowitySyncServer {
|
|
|
92
111
|
private handleConnection;
|
|
93
112
|
private handleMessage;
|
|
94
113
|
private allowPing;
|
|
95
|
-
private
|
|
114
|
+
private optIn;
|
|
96
115
|
private sendTo;
|
|
97
116
|
}
|
|
98
117
|
//# 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;AAI7B,O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ClE;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA2C;IAC1E,+DAA+D;IAC/D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA2C;IACzE,mEAAmE;IACnE,OAAO,CAAC,cAAc,CACb;IACT,2DAA2D;IAC3D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;IAC3D,OAAO,CAAC,cAAc,CAA+B;IACrD,2EAA2E;IAC3E,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoC;IAC5D,4EAA4E;IAC5E,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA4B;IAClD,kFAAkF;IAClF,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4B;IACrD,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;IA8I3B,OAAO,CAAC,SAAS;YAUH,KAAK;IAanB,OAAO,CAAC,MAAM;CAmBjB"}
|
package/dist/server/ws-server.js
CHANGED
|
@@ -59,6 +59,14 @@ class NowitySyncServer {
|
|
|
59
59
|
* processed before the session exists.
|
|
60
60
|
*/
|
|
61
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();
|
|
62
70
|
/** Registered on a borrowed server, and removed again on close. */
|
|
63
71
|
this.upgradeHandler = null;
|
|
64
72
|
/** Pings sent since the socket last proved it is alive. */
|
|
@@ -68,6 +76,8 @@ class NowitySyncServer {
|
|
|
68
76
|
this.helloAt = new WeakMap();
|
|
69
77
|
/** Sockets that get timing (see {@link NowitySyncServerOptions.timing}). */
|
|
70
78
|
this.timed = new WeakSet();
|
|
79
|
+
/** Sockets that get narrowing (see {@link NowitySyncServerOptions.narrowing}). */
|
|
80
|
+
this.narrowed = new WeakSet();
|
|
71
81
|
/** Pongs sent in the current window, per socket. */
|
|
72
82
|
this.pings = new WeakMap();
|
|
73
83
|
this.opts = opts;
|
|
@@ -205,11 +215,30 @@ class NowitySyncServer {
|
|
|
205
215
|
}
|
|
206
216
|
return;
|
|
207
217
|
}
|
|
208
|
-
const
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
218
|
+
const handle = (auth) => auth ? this.handleMessage(socket, auth, message, receivedAt) : undefined;
|
|
219
|
+
const barrier = this.helloBarriers.get(socket);
|
|
220
|
+
if (message.t === "mutate" && barrier) {
|
|
221
|
+
// Mutations keep their order among themselves — they are
|
|
222
|
+
// chained here and again per session in the engine — and keep
|
|
223
|
+
// waiting for the session they belong to; only the queue of
|
|
224
|
+
// subscribes is no longer in front of them. Before any hello
|
|
225
|
+
// they go the ordinary way, to be told to say hello.
|
|
226
|
+
const previous = this.mutationChains.get(socket) ?? Promise.resolve();
|
|
227
|
+
const next = previous.then(() => barrier).then(() => authenticated).then(handle);
|
|
228
|
+
this.mutationChains.set(socket, next.catch((err) => this.onError(err)));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
// A hello still waits for everything before it, mutations included:
|
|
232
|
+
// it may replace the session they are being written under.
|
|
233
|
+
const control = this.chains.get(socket) ?? Promise.resolve();
|
|
234
|
+
const previous = message.t === "hello"
|
|
235
|
+
? Promise.all([control, this.mutationChains.get(socket)]).then(() => undefined)
|
|
236
|
+
: control;
|
|
237
|
+
const next = previous.then(() => authenticated).then(handle);
|
|
238
|
+
const settled = next.catch((err) => this.onError(err));
|
|
239
|
+
this.chains.set(socket, settled);
|
|
240
|
+
if (message.t === "hello")
|
|
241
|
+
this.helloBarriers.set(socket, settled);
|
|
213
242
|
});
|
|
214
243
|
socket.on("close", () => {
|
|
215
244
|
settleAuth(null);
|
|
@@ -258,14 +287,20 @@ class NowitySyncServer {
|
|
|
258
287
|
if (existing)
|
|
259
288
|
await this.opts.engine.closeSession(existing);
|
|
260
289
|
const context = validContext(message.context) ? (message.context ?? null) : null;
|
|
261
|
-
const timing = await this.
|
|
290
|
+
const timing = await this.optIn(this.opts.timing, info);
|
|
262
291
|
if (timing)
|
|
263
292
|
this.timed.add(socket);
|
|
264
293
|
else
|
|
265
294
|
this.timed.delete(socket);
|
|
295
|
+
const narrowing = await this.optIn(this.opts.narrowing, info);
|
|
296
|
+
if (narrowing)
|
|
297
|
+
this.narrowed.add(socket);
|
|
298
|
+
else
|
|
299
|
+
this.narrowed.delete(socket);
|
|
266
300
|
this.helloAt.set(socket, receivedAt);
|
|
267
301
|
const session = this.opts.engine.openSession(info, (msg) => this.sendTo(socket, msg), context);
|
|
268
302
|
session.timing = timing;
|
|
303
|
+
session.narrowing = narrowing;
|
|
269
304
|
this.sessions.set(socket, session);
|
|
270
305
|
// The socket may have closed while the previous session was
|
|
271
306
|
// being closed above. Its close handler has run already and
|
|
@@ -364,12 +399,11 @@ class NowitySyncServer {
|
|
|
364
399
|
window.count += 1;
|
|
365
400
|
return window.count <= PINGS_PER_WINDOW;
|
|
366
401
|
}
|
|
367
|
-
async
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
return timing === true;
|
|
402
|
+
async optIn(option, info) {
|
|
403
|
+
if (typeof option !== "function")
|
|
404
|
+
return option === true;
|
|
371
405
|
try {
|
|
372
|
-
return (await
|
|
406
|
+
return (await option(info)) === true;
|
|
373
407
|
}
|
|
374
408
|
catch (err) {
|
|
375
409
|
this.onError(err);
|
|
@@ -388,6 +422,8 @@ class NowitySyncServer {
|
|
|
388
422
|
message.clock = { receivedAt, sentAt: Date.now() };
|
|
389
423
|
if (this.timed.has(socket))
|
|
390
424
|
message.timing = true;
|
|
425
|
+
if (this.narrowed.has(socket))
|
|
426
|
+
message.narrowing = true;
|
|
391
427
|
break;
|
|
392
428
|
}
|
|
393
429
|
case "mutation-result":
|