nowity 1.2.1 → 1.5.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 +316 -3
- package/dist/client/connection.d.ts +2 -0
- package/dist/client/connection.d.ts.map +1 -1
- package/dist/client/connection.js +13 -0
- package/dist/client/nowity-client.d.ts +23 -0
- package/dist/client/nowity-client.d.ts.map +1 -1
- package/dist/client/nowity-client.js +29 -1
- package/dist/client/overlay.d.ts +52 -2
- package/dist/client/overlay.d.ts.map +1 -1
- package/dist/client/overlay.js +43 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/core/protocol.d.ts +20 -1
- package/dist/core/protocol.d.ts.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -1
- package/dist/server/api/collector.d.ts.map +1 -1
- package/dist/server/api/collector.js +4 -1
- package/dist/server/api/configure.d.ts +24 -0
- package/dist/server/api/configure.d.ts.map +1 -1
- package/dist/server/api/configure.js +51 -1
- package/dist/server/api/execution.d.ts +9 -0
- package/dist/server/api/execution.d.ts.map +1 -1
- package/dist/server/api/execution.js +19 -0
- package/dist/server/api/presence.d.ts +66 -0
- package/dist/server/api/presence.d.ts.map +1 -0
- package/dist/server/api/presence.js +181 -0
- package/dist/server/api/read-keys.d.ts +6 -0
- package/dist/server/api/read-keys.d.ts.map +1 -1
- package/dist/server/api/read-keys.js +12 -0
- package/dist/server/api/runtime.d.ts.map +1 -1
- package/dist/server/api/runtime.js +12 -0
- package/dist/server/api/scheduler.d.ts +122 -0
- package/dist/server/api/scheduler.d.ts.map +1 -0
- package/dist/server/api/scheduler.js +217 -0
- package/dist/server/change-source/wal-change-source.d.ts.map +1 -1
- package/dist/server/change-source/wal-change-source.js +10 -1
- package/dist/server/leader.d.ts +138 -0
- package/dist/server/leader.d.ts.map +1 -0
- package/dist/server/leader.js +342 -0
- package/dist/server/presence.d.ts +217 -0
- package/dist/server/presence.d.ts.map +1 -0
- package/dist/server/presence.js +428 -0
- package/dist/server/scheduler.d.ts +195 -0
- package/dist/server/scheduler.d.ts.map +1 -0
- package/dist/server/scheduler.js +432 -0
- package/dist/server/setup-sql.d.ts +1 -1
- package/dist/server/setup-sql.d.ts.map +1 -1
- package/dist/server/setup-sql.js +547 -0
- package/dist/server/sync-engine.d.ts +22 -1
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +30 -1
- package/dist/server/ws-server.d.ts +22 -0
- package/dist/server/ws-server.d.ts.map +1 -1
- package/dist/server/ws-server.js +81 -1
- package/package.json +1 -1
|
@@ -4,6 +4,7 @@ import type { ChangeSource } from "./change-source/change-source";
|
|
|
4
4
|
import type { SnapshotRunner } from "./db";
|
|
5
5
|
import { type QueryOutput } from "./evaluate";
|
|
6
6
|
import { type MutatorRegistry, type WriteRunner } from "./mutator";
|
|
7
|
+
import type { ConnectionObserver, PresenceContext } from "./presence";
|
|
7
8
|
import type { QueryDef, QueryRegistry, SessionInfo } from "./query";
|
|
8
9
|
export interface SyncEngineOptions {
|
|
9
10
|
snapshots: SnapshotRunner;
|
|
@@ -35,6 +36,11 @@ export interface SyncEngineOptions {
|
|
|
35
36
|
* however many subscriptions share a table, and impossible to get wrong.
|
|
36
37
|
*/
|
|
37
38
|
narrowInvalidation?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Told about every session that opens and closes, when presence is on.
|
|
41
|
+
* Absent, nothing about connections is written anywhere.
|
|
42
|
+
*/
|
|
43
|
+
presence?: ConnectionObserver;
|
|
38
44
|
onError?: (err: Error) => void;
|
|
39
45
|
}
|
|
40
46
|
interface Subscription {
|
|
@@ -93,6 +99,14 @@ export declare class EngineSession {
|
|
|
93
99
|
/** Serialises this client's mutations; they must be applied in order. */
|
|
94
100
|
mutationChain: Promise<unknown>;
|
|
95
101
|
closed: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* This socket, as presence records it. Not the client id: a client id
|
|
104
|
+
* survives a reconnect, and the old socket's row has to be told apart from
|
|
105
|
+
* the new one's when both briefly exist on two nodes.
|
|
106
|
+
*/
|
|
107
|
+
readonly connectionId: `${string}-${string}-${string}-${string}-${string}`;
|
|
108
|
+
/** What the client last said it is looking at (`setContext`). */
|
|
109
|
+
context: PresenceContext | null;
|
|
96
110
|
constructor(info: SessionInfo, send: (msg: ServerMessage) => void);
|
|
97
111
|
}
|
|
98
112
|
/**
|
|
@@ -165,7 +179,7 @@ export declare class SyncEngine {
|
|
|
165
179
|
settle(): Promise<void>;
|
|
166
180
|
/** Serialised execution of everything that reads or writes subscription state. */
|
|
167
181
|
private run;
|
|
168
|
-
openSession(info: SessionInfo, send: (msg: ServerMessage) => void): EngineSession;
|
|
182
|
+
openSession(info: SessionInfo, send: (msg: ServerMessage) => void, context?: PresenceContext | null): EngineSession;
|
|
169
183
|
closeSession(session: EngineSession): Promise<void>;
|
|
170
184
|
welcome(session: EngineSession): Promise<void>;
|
|
171
185
|
/**
|
|
@@ -177,6 +191,13 @@ export declare class SyncEngine {
|
|
|
177
191
|
* of having it silently dropped.
|
|
178
192
|
*/
|
|
179
193
|
private claimClientId;
|
|
194
|
+
/**
|
|
195
|
+
* Remember what the connection is looking at, for presence readers.
|
|
196
|
+
*
|
|
197
|
+
* Written only when it changed: a client resends its context after every
|
|
198
|
+
* reconnect, and the same value again is not news.
|
|
199
|
+
*/
|
|
200
|
+
setContext(session: EngineSession, context: PresenceContext | null): void;
|
|
180
201
|
subscribe(session: EngineSession, subId: string, queryName: string, args: unknown): Promise<void>;
|
|
181
202
|
unsubscribe(session: EngineSession, subId: string): Promise<void>;
|
|
182
203
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync-engine.d.ts","sourceRoot":"","sources":["../../src/server/sync-engine.ts"],"names":[],"mappings":"AAGA,OAAO,EAEH,KAAK,aAAa,EAClB,KAAK,aAAa,EAIrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAGH,KAAK,WAAW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,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBpD;;;;;;;OAOG;YACW,aAAa;IAc3B;;;;;OAKG;IACH,UAAU,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,eAAe,GAAG,IAAI,GAAG,IAAI;IASnE,SAAS,CACX,OAAO,EAAE,aAAa,EACtB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC;IAuEV,WAAW,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWvE;;;;;OAKG;IACH,OAAO,CAAC,QAAQ;IAQhB;;;;;;OAMG;IACH,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,MAAM;IAWd,yEAAyE;YAC3D,UAAU;YASV,WAAW;IAyJzB;;;;;;;;OAQG;IACH,OAAO,CAAC,SAAS;IAgBjB;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IA2BrB;;;;;;;;OAQG;IACH,OAAO,CAAC,MAAM;IAUd,OAAO,CAAC,oBAAoB;IAO5B;;;OAGG;YACW,cAAc;YAwCd,wBAAwB;YA6BxB,QAAQ;IAItB,gFAAgF;IAChF,OAAO,CAAC,QAAQ;IAmChB,OAAO,CAAC,QAAQ;IAUhB,OAAO,CAAC,UAAU;IAqBlB,sEAAsE;IACtE,MAAM,CACF,OAAO,EAAE,aAAa,EACtB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,OAAO,GACd,OAAO,CAAC,eAAe,CAAC;YAQb,eAAe;IAuH7B;;;;;;;OAOG;YACW,SAAS;YAMT,SAAS;YAgBT,kBAAkB;YAQlB,mBAAmB;CAiBpC"}
|
|
@@ -27,6 +27,14 @@ class EngineSession {
|
|
|
27
27
|
/** Serialises this client's mutations; they must be applied in order. */
|
|
28
28
|
this.mutationChain = Promise.resolve();
|
|
29
29
|
this.closed = false;
|
|
30
|
+
/**
|
|
31
|
+
* This socket, as presence records it. Not the client id: a client id
|
|
32
|
+
* survives a reconnect, and the old socket's row has to be told apart from
|
|
33
|
+
* the new one's when both briefly exist on two nodes.
|
|
34
|
+
*/
|
|
35
|
+
this.connectionId = (0, node_crypto_1.randomUUID)();
|
|
36
|
+
/** What the client last said it is looking at (`setContext`). */
|
|
37
|
+
this.context = null;
|
|
30
38
|
}
|
|
31
39
|
}
|
|
32
40
|
exports.EngineSession = EngineSession;
|
|
@@ -115,7 +123,7 @@ class SyncEngine {
|
|
|
115
123
|
return next;
|
|
116
124
|
}
|
|
117
125
|
// ------------------------------------------------------------------- sessions
|
|
118
|
-
openSession(info, send) {
|
|
126
|
+
openSession(info, send, context = null) {
|
|
119
127
|
// Two connections cannot share one mutation sequence, and the way they
|
|
120
128
|
// fail if they do is the worst kind: the second client's mutation 1 is
|
|
121
129
|
// indistinguishable from a replay of the first client's, so it is
|
|
@@ -135,10 +143,14 @@ class SyncEngine {
|
|
|
135
143
|
break;
|
|
136
144
|
}
|
|
137
145
|
const session = new EngineSession(info, send);
|
|
146
|
+
session.context = context;
|
|
138
147
|
this.sessions.add(session);
|
|
148
|
+
this.opts.presence?.opened(session);
|
|
139
149
|
return session;
|
|
140
150
|
}
|
|
141
151
|
async closeSession(session) {
|
|
152
|
+
if (!session.closed)
|
|
153
|
+
this.opts.presence?.closed(session);
|
|
142
154
|
session.closed = true;
|
|
143
155
|
await this.run(async () => {
|
|
144
156
|
for (const sub of session.subs.values())
|
|
@@ -149,6 +161,9 @@ class SyncEngine {
|
|
|
149
161
|
}
|
|
150
162
|
async welcome(session) {
|
|
151
163
|
await this.claimClientId(session);
|
|
164
|
+
// The welcome is the client's cue that it is connected, so by then it
|
|
165
|
+
// is also online for everyone else.
|
|
166
|
+
await this.opts.presence?.settled(session);
|
|
152
167
|
const lastMutationId = await this.opts.snapshots.withSnapshot((snap) => this.readLastMutationId(snap, session.info.clientId));
|
|
153
168
|
session.send({
|
|
154
169
|
t: "welcome",
|
|
@@ -175,6 +190,20 @@ class SyncEngine {
|
|
|
175
190
|
});
|
|
176
191
|
session.token = token;
|
|
177
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Remember what the connection is looking at, for presence readers.
|
|
195
|
+
*
|
|
196
|
+
* Written only when it changed: a client resends its context after every
|
|
197
|
+
* reconnect, and the same value again is not news.
|
|
198
|
+
*/
|
|
199
|
+
setContext(session, context) {
|
|
200
|
+
if (session.closed)
|
|
201
|
+
return;
|
|
202
|
+
if (JSON.stringify(session.context) === JSON.stringify(context))
|
|
203
|
+
return;
|
|
204
|
+
session.context = context;
|
|
205
|
+
this.opts.presence?.contextChanged(session);
|
|
206
|
+
}
|
|
178
207
|
// -------------------------------------------------------------- subscriptions
|
|
179
208
|
async subscribe(session, subId, queryName, args) {
|
|
180
209
|
const def = this.opts.queries.get(queryName);
|
|
@@ -26,6 +26,22 @@ export interface NowitySyncServerOptions {
|
|
|
26
26
|
* a dev server's own hot-reload socket working.
|
|
27
27
|
*/
|
|
28
28
|
server?: http.Server;
|
|
29
|
+
/**
|
|
30
|
+
* Ping every connection on this interval and drop the ones that stop
|
|
31
|
+
* answering. A socket whose peer vanished without a FIN — a laptop lid, a
|
|
32
|
+
* mobile network, a NAT that forgot the mapping — otherwise stays open on
|
|
33
|
+
* this side for as long as the kernel cares to keep it, and the user it
|
|
34
|
+
* belongs to stays online. Browsers answer pings on their own; nothing has
|
|
35
|
+
* to be written for it on the client.
|
|
36
|
+
*
|
|
37
|
+
* Off unless set — `{}` means 15 s and two missed pongs. `createNowityServer`
|
|
38
|
+
* turns it on by itself when presence is on, since that is when a socket
|
|
39
|
+
* nobody is behind any more costs something visible.
|
|
40
|
+
*/
|
|
41
|
+
heartbeat?: {
|
|
42
|
+
intervalMs?: number;
|
|
43
|
+
maxMissed?: number;
|
|
44
|
+
} | false;
|
|
29
45
|
onError?: (err: Error) => void;
|
|
30
46
|
}
|
|
31
47
|
/** WebSocket front end for {@link SyncEngine}. */
|
|
@@ -44,11 +60,17 @@ export declare class NowitySyncServer {
|
|
|
44
60
|
private readonly chains;
|
|
45
61
|
/** Registered on a borrowed server, and removed again on close. */
|
|
46
62
|
private upgradeHandler;
|
|
63
|
+
/** Pings sent since the socket last proved it is alive. */
|
|
64
|
+
private readonly missed;
|
|
65
|
+
private heartbeatTimer;
|
|
47
66
|
constructor(opts: NowitySyncServerOptions);
|
|
48
67
|
/** Start listening; resolves with the actual port. */
|
|
49
68
|
listen(): Promise<number>;
|
|
50
69
|
close(): Promise<void>;
|
|
51
70
|
get connectionCount(): number;
|
|
71
|
+
/** The ping interval, or null when the heartbeat is off. */
|
|
72
|
+
get heartbeatIntervalMs(): number | null;
|
|
73
|
+
private startHeartbeat;
|
|
52
74
|
private handleConnection;
|
|
53
75
|
private handleMessage;
|
|
54
76
|
private sendTo;
|
|
@@ -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;AAK7B,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;IACpF,IAAI,CAAC,EAAE,MA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;CAClC;AAKD,kDAAkD;AAClD,qBAAa,gBAAgB;IACzB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA0B;IAC/C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuB;IAC/C,OAAO,CAAC,UAAU,CAA4B;IAC9C,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,GAAG,CAAgC;IAC3C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAuC;IAChE;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2C;IAClE,mEAAmE;IACnE,OAAO,CAAC,cAAc,CACb;IACT,2DAA2D;IAC3D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;IAC3D,OAAO,CAAC,cAAc,CAA+B;gBAEzC,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;YAsDhB,aAAa;IA2H3B,OAAO,CAAC,MAAM;CAIjB"}
|
package/dist/server/ws-server.js
CHANGED
|
@@ -37,6 +37,8 @@ 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
|
+
/** Largest context a connection may set, serialised. It is stored per socket. */
|
|
41
|
+
const MAX_CONTEXT_BYTES = 4096;
|
|
40
42
|
/** WebSocket front end for {@link SyncEngine}. */
|
|
41
43
|
class NowitySyncServer {
|
|
42
44
|
constructor(opts) {
|
|
@@ -52,6 +54,9 @@ class NowitySyncServer {
|
|
|
52
54
|
this.chains = new WeakMap();
|
|
53
55
|
/** Registered on a borrowed server, and removed again on close. */
|
|
54
56
|
this.upgradeHandler = null;
|
|
57
|
+
/** Pings sent since the socket last proved it is alive. */
|
|
58
|
+
this.missed = new WeakMap();
|
|
59
|
+
this.heartbeatTimer = null;
|
|
55
60
|
this.opts = opts;
|
|
56
61
|
this.onError = opts.onError ?? ((err) => console.error("[nowity/ws]", err));
|
|
57
62
|
}
|
|
@@ -83,6 +88,7 @@ class NowitySyncServer {
|
|
|
83
88
|
void this.handleConnection(socket, req);
|
|
84
89
|
});
|
|
85
90
|
this.wss.on("error", (err) => this.onError(err));
|
|
91
|
+
this.startHeartbeat();
|
|
86
92
|
if (this.ownsHttpServer) {
|
|
87
93
|
await new Promise((resolve) => {
|
|
88
94
|
httpServer.listen(this.opts.port ?? 0, this.opts.host ?? "127.0.0.1", () => resolve());
|
|
@@ -92,6 +98,9 @@ class NowitySyncServer {
|
|
|
92
98
|
return typeof address === "object" && address ? address.port : (this.opts.port ?? 0);
|
|
93
99
|
}
|
|
94
100
|
async close() {
|
|
101
|
+
if (this.heartbeatTimer)
|
|
102
|
+
clearInterval(this.heartbeatTimer);
|
|
103
|
+
this.heartbeatTimer = null;
|
|
95
104
|
for (const [socket, session] of this.sessions) {
|
|
96
105
|
await this.opts.engine.closeSession(session);
|
|
97
106
|
socket.terminate();
|
|
@@ -115,6 +124,43 @@ class NowitySyncServer {
|
|
|
115
124
|
get connectionCount() {
|
|
116
125
|
return this.sessions.size;
|
|
117
126
|
}
|
|
127
|
+
/** The ping interval, or null when the heartbeat is off. */
|
|
128
|
+
get heartbeatIntervalMs() {
|
|
129
|
+
const heartbeat = this.opts.heartbeat;
|
|
130
|
+
if (heartbeat === undefined || heartbeat === false)
|
|
131
|
+
return null;
|
|
132
|
+
const intervalMs = heartbeat.intervalMs ?? 15000;
|
|
133
|
+
return intervalMs > 0 ? intervalMs : null;
|
|
134
|
+
}
|
|
135
|
+
startHeartbeat() {
|
|
136
|
+
const heartbeat = this.opts.heartbeat;
|
|
137
|
+
if (heartbeat === undefined || heartbeat === false)
|
|
138
|
+
return;
|
|
139
|
+
const intervalMs = heartbeat?.intervalMs ?? 15000;
|
|
140
|
+
const maxMissed = heartbeat?.maxMissed ?? 2;
|
|
141
|
+
if (intervalMs <= 0)
|
|
142
|
+
return;
|
|
143
|
+
this.heartbeatTimer = setInterval(() => {
|
|
144
|
+
for (const socket of this.wss?.clients ?? []) {
|
|
145
|
+
const missed = this.missed.get(socket) ?? 0;
|
|
146
|
+
if (missed >= maxMissed) {
|
|
147
|
+
// `terminate`, not `close`: a closing handshake needs the
|
|
148
|
+
// very peer that has stopped answering. The close event
|
|
149
|
+
// that follows ends the session like any other.
|
|
150
|
+
socket.terminate();
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
this.missed.set(socket, missed + 1);
|
|
154
|
+
try {
|
|
155
|
+
socket.ping();
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
this.onError(err);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}, intervalMs);
|
|
162
|
+
this.heartbeatTimer.unref?.();
|
|
163
|
+
}
|
|
118
164
|
async handleConnection(socket, req) {
|
|
119
165
|
// The listener goes on before authentication is awaited. A client sends
|
|
120
166
|
// `hello` (and whatever it queued) the moment the socket opens, and a
|
|
@@ -123,7 +169,10 @@ class NowitySyncServer {
|
|
|
123
169
|
// before anything else". Messages wait in the chain until auth decides.
|
|
124
170
|
let settleAuth;
|
|
125
171
|
const authenticated = new Promise((resolve) => (settleAuth = resolve));
|
|
172
|
+
// Anything from the peer proves it is there, not only a pong.
|
|
173
|
+
socket.on("pong", () => this.missed.set(socket, 0));
|
|
126
174
|
socket.on("message", (data) => {
|
|
175
|
+
this.missed.set(socket, 0);
|
|
127
176
|
let message;
|
|
128
177
|
try {
|
|
129
178
|
message = JSON.parse(String(data));
|
|
@@ -183,8 +232,18 @@ class NowitySyncServer {
|
|
|
183
232
|
const existing = this.sessions.get(socket);
|
|
184
233
|
if (existing)
|
|
185
234
|
await this.opts.engine.closeSession(existing);
|
|
186
|
-
const
|
|
235
|
+
const context = validContext(message.context) ? (message.context ?? null) : null;
|
|
236
|
+
const session = this.opts.engine.openSession(info, (msg) => this.sendTo(socket, msg), context);
|
|
187
237
|
this.sessions.set(socket, session);
|
|
238
|
+
// The socket may have closed while the previous session was
|
|
239
|
+
// being closed above. Its close handler has run already and
|
|
240
|
+
// found the old session, so nobody else would close this one —
|
|
241
|
+
// and with presence on, its user would stay online for good.
|
|
242
|
+
if (socket.readyState !== socket.OPEN) {
|
|
243
|
+
this.sessions.delete(socket);
|
|
244
|
+
await this.opts.engine.closeSession(session);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
188
247
|
await this.opts.engine.welcome(session);
|
|
189
248
|
return;
|
|
190
249
|
}
|
|
@@ -219,6 +278,19 @@ class NowitySyncServer {
|
|
|
219
278
|
case "unsubscribe":
|
|
220
279
|
await this.opts.engine.unsubscribe(session, message.subId);
|
|
221
280
|
break;
|
|
281
|
+
case "context": {
|
|
282
|
+
const context = message.context;
|
|
283
|
+
if (!validContext(context) || context === undefined) {
|
|
284
|
+
this.sendTo(socket, {
|
|
285
|
+
t: "error",
|
|
286
|
+
code: "context-invalid",
|
|
287
|
+
message: `A context is a plain object of at most ${MAX_CONTEXT_BYTES} bytes, or null`,
|
|
288
|
+
});
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
this.opts.engine.setContext(session, context);
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
222
294
|
case "mutate": {
|
|
223
295
|
const outcome = await this.opts.engine.mutate(session, message.mutationId, message.name, message.args);
|
|
224
296
|
this.sendTo(socket, {
|
|
@@ -249,3 +321,11 @@ class NowitySyncServer {
|
|
|
249
321
|
}
|
|
250
322
|
}
|
|
251
323
|
exports.NowitySyncServer = NowitySyncServer;
|
|
324
|
+
/** A context is a small plain object, or null to clear it. */
|
|
325
|
+
function validContext(context) {
|
|
326
|
+
if (context === null || context === undefined)
|
|
327
|
+
return true;
|
|
328
|
+
if (typeof context !== "object" || Array.isArray(context))
|
|
329
|
+
return false;
|
|
330
|
+
return JSON.stringify(context).length <= MAX_CONTEXT_BYTES;
|
|
331
|
+
}
|