nowity 1.8.0 → 1.9.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 +43 -0
- package/dist/client/devtools.d.ts +218 -0
- package/dist/client/devtools.d.ts.map +1 -0
- package/dist/client/devtools.js +444 -0
- package/dist/client/nowity-client.d.ts +31 -0
- package/dist/client/nowity-client.d.ts.map +1 -1
- package/dist/client/nowity-client.js +93 -5
- package/dist/client/transport.d.ts +2 -1
- package/dist/client/transport.d.ts.map +1 -1
- package/dist/client/ws-transport.d.ts.map +1 -1
- package/dist/client/ws-transport.js +2 -1
- package/dist/client.d.ts +5 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/devtools.d.ts +25 -0
- package/dist/devtools.d.ts.map +1 -0
- package/dist/devtools.js +228 -0
- package/package.json +5 -1
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* What a browser's Nowity client did, for a developer to look at — the Network
|
|
4
|
+
* tab of the socket.
|
|
5
|
+
*
|
|
6
|
+
* Off unless asked for (`devtools` option or {@link NowityClient.enableDevtools}):
|
|
7
|
+
* a client without a recorder does not measure, serialise or allocate anything
|
|
8
|
+
* for it. With one, every subscribe, mutation and connection event becomes a
|
|
9
|
+
* record in a ring buffer of the last `limit` entries, kept only in this page's
|
|
10
|
+
* memory. Nothing here is sent anywhere.
|
|
11
|
+
*
|
|
12
|
+
* Arguments are masked on the way in, not on the way out: a record never holds a
|
|
13
|
+
* value the allowlist did not let through, so neither the panel nor an export can
|
|
14
|
+
* leak what the recorder never kept. Payloads (rows, mutation results) are not
|
|
15
|
+
* kept at all — only their size.
|
|
16
|
+
*
|
|
17
|
+
* Times are `performance.now()` — milliseconds since the page's navigation
|
|
18
|
+
* started — so the summary reads as the page's own startup: when the socket
|
|
19
|
+
* opened, when the server welcomed it, when the first data arrived.
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.NowityDevtools = exports.MASKED = exports.DEFAULT_ALLOWED_KEYS = exports.UPDATE_LIMIT = exports.DEFAULT_LIMIT = void 0;
|
|
23
|
+
exports.maskArgs = maskArgs;
|
|
24
|
+
exports.byteSize = byteSize;
|
|
25
|
+
exports.jsonBytes = jsonBytes;
|
|
26
|
+
exports.DEFAULT_LIMIT = 500;
|
|
27
|
+
/** Updates kept per subscription; the count goes on. */
|
|
28
|
+
exports.UPDATE_LIMIT = 50;
|
|
29
|
+
/**
|
|
30
|
+
* Keys a value is shown under by default: identifiers and paging, never content.
|
|
31
|
+
* A key that ends in `Id` / `Ids` counts too. A string under any other key is
|
|
32
|
+
* masked — names, e-mails, tokens and message text all arrive as strings.
|
|
33
|
+
*/
|
|
34
|
+
exports.DEFAULT_ALLOWED_KEYS = [
|
|
35
|
+
"id",
|
|
36
|
+
"ids",
|
|
37
|
+
"page",
|
|
38
|
+
"limit",
|
|
39
|
+
"offset",
|
|
40
|
+
"cursor",
|
|
41
|
+
"take",
|
|
42
|
+
"skip",
|
|
43
|
+
"kind",
|
|
44
|
+
"type",
|
|
45
|
+
"status",
|
|
46
|
+
"mode",
|
|
47
|
+
"period",
|
|
48
|
+
];
|
|
49
|
+
/** What a masked string becomes. Its length is not kept either. */
|
|
50
|
+
exports.MASKED = "•••";
|
|
51
|
+
const MAX_DEPTH = 4;
|
|
52
|
+
const MAX_ITEMS = 20;
|
|
53
|
+
const SENSITIVE_KEY = /pass|secret|token|cookie|auth|session|email|e-mail|mail|phone|name|text|message|body|content|note/i;
|
|
54
|
+
function allowedKey(key, extra) {
|
|
55
|
+
if (SENSITIVE_KEY.test(key) && !extra.has(key))
|
|
56
|
+
return false;
|
|
57
|
+
return exports.DEFAULT_ALLOWED_KEYS.includes(key) || extra.has(key) || /(^|[a-z])Ids?$/.test(key);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Mask arguments for the record.
|
|
61
|
+
*
|
|
62
|
+
* Numbers, booleans and `null` stay — they are counts, flags and paging. A
|
|
63
|
+
* string stays only under an allowed key (or at an allowed position of the
|
|
64
|
+
* top-level list); every other string becomes {@link MASKED}. Deep structures
|
|
65
|
+
* are cut off rather than walked, and a key that looks sensitive (`email`,
|
|
66
|
+
* `token`…) is masked even when it ends in `Id`, unless the application listed
|
|
67
|
+
* it explicitly.
|
|
68
|
+
*/
|
|
69
|
+
function maskArgs(endpoint, args, opts = {}) {
|
|
70
|
+
const extra = new Set(opts.allowKeys ?? []);
|
|
71
|
+
const positions = new Set(opts.allowArgs?.[endpoint] ?? []);
|
|
72
|
+
const walk = (value, allowed, depth) => {
|
|
73
|
+
if (value === null || value === undefined)
|
|
74
|
+
return value ?? null;
|
|
75
|
+
switch (typeof value) {
|
|
76
|
+
case "number":
|
|
77
|
+
case "boolean":
|
|
78
|
+
return value;
|
|
79
|
+
case "string":
|
|
80
|
+
return allowed ? value : exports.MASKED;
|
|
81
|
+
case "bigint":
|
|
82
|
+
return allowed ? String(value) : exports.MASKED;
|
|
83
|
+
case "object":
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
return exports.MASKED;
|
|
87
|
+
}
|
|
88
|
+
if (depth >= MAX_DEPTH)
|
|
89
|
+
return "…";
|
|
90
|
+
if (Array.isArray(value)) {
|
|
91
|
+
const items = value.slice(0, MAX_ITEMS).map((item) => walk(item, allowed, depth + 1));
|
|
92
|
+
if (value.length > MAX_ITEMS)
|
|
93
|
+
items.push(`… +${value.length - MAX_ITEMS}`);
|
|
94
|
+
return items;
|
|
95
|
+
}
|
|
96
|
+
if (value instanceof Date)
|
|
97
|
+
return allowed ? value.toISOString() : exports.MASKED;
|
|
98
|
+
const out = {};
|
|
99
|
+
let count = 0;
|
|
100
|
+
for (const [key, item] of Object.entries(value)) {
|
|
101
|
+
if (count++ >= MAX_ITEMS) {
|
|
102
|
+
out["…"] = `+${Object.keys(value).length - MAX_ITEMS}`;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
out[key] = walk(item, allowedKey(key, extra), depth + 1);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
};
|
|
109
|
+
if (Array.isArray(args)) {
|
|
110
|
+
return args.slice(0, MAX_ITEMS).map((item, index) => walk(item, positions.has(index), 1));
|
|
111
|
+
}
|
|
112
|
+
return walk(args, false, 0);
|
|
113
|
+
}
|
|
114
|
+
/** UTF-8 size of a string, without allocating an encoded copy. */
|
|
115
|
+
function byteSize(text) {
|
|
116
|
+
let bytes = 0;
|
|
117
|
+
for (let i = 0; i < text.length; i++) {
|
|
118
|
+
const code = text.charCodeAt(i);
|
|
119
|
+
if (code < 0x80)
|
|
120
|
+
bytes += 1;
|
|
121
|
+
else if (code < 0x800)
|
|
122
|
+
bytes += 2;
|
|
123
|
+
else if (code >= 0xd800 && code <= 0xdbff) {
|
|
124
|
+
bytes += 4;
|
|
125
|
+
i++;
|
|
126
|
+
}
|
|
127
|
+
else
|
|
128
|
+
bytes += 3;
|
|
129
|
+
}
|
|
130
|
+
return bytes;
|
|
131
|
+
}
|
|
132
|
+
/** Size of a value as JSON on the wire. */
|
|
133
|
+
function jsonBytes(value) {
|
|
134
|
+
return byteSize(JSON.stringify(value) ?? "");
|
|
135
|
+
}
|
|
136
|
+
function defaultNow() {
|
|
137
|
+
const perf = globalThis.performance;
|
|
138
|
+
return perf ? perf.now() : Date.now();
|
|
139
|
+
}
|
|
140
|
+
function round(ms) {
|
|
141
|
+
return Math.round(ms * 10) / 10;
|
|
142
|
+
}
|
|
143
|
+
function shorten(text) {
|
|
144
|
+
return text.length > 120 ? `${text.slice(0, 117)}…` : text;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The recorder. The client calls the `on*` methods; a panel reads
|
|
148
|
+
* {@link records}, {@link summary} and listens with {@link subscribe}.
|
|
149
|
+
*/
|
|
150
|
+
class NowityDevtools {
|
|
151
|
+
constructor(opts = {}) {
|
|
152
|
+
this.list = [];
|
|
153
|
+
this.seq = 0;
|
|
154
|
+
this.rev = 0;
|
|
155
|
+
this.listeners = new Set();
|
|
156
|
+
this.notifyQueued = false;
|
|
157
|
+
this.subs = new Map();
|
|
158
|
+
/** Re-sent subscribes waiting for their snapshot: subId → when. */
|
|
159
|
+
this.resubscribedAt = new Map();
|
|
160
|
+
this.mutations = new Map();
|
|
161
|
+
this.connection = 0;
|
|
162
|
+
this.connectAt = null;
|
|
163
|
+
this.openedAt = null;
|
|
164
|
+
this.closedAt = null;
|
|
165
|
+
this.startup = {
|
|
166
|
+
connectAt: null,
|
|
167
|
+
openAt: null,
|
|
168
|
+
welcomeAt: null,
|
|
169
|
+
firstSubscribeAt: null,
|
|
170
|
+
firstDataAt: null,
|
|
171
|
+
connections: 0,
|
|
172
|
+
reconnects: 0,
|
|
173
|
+
};
|
|
174
|
+
this.opts = opts;
|
|
175
|
+
this.limit = Math.max(1, opts.limit ?? exports.DEFAULT_LIMIT);
|
|
176
|
+
this.now = opts.now ?? defaultNow;
|
|
177
|
+
}
|
|
178
|
+
// ------------------------------------------------------------------- reading
|
|
179
|
+
/** Records, oldest first. A new array whenever {@link version} moves. */
|
|
180
|
+
get records() {
|
|
181
|
+
return this.list;
|
|
182
|
+
}
|
|
183
|
+
/** Moves on every change — the snapshot for `useSyncExternalStore`. */
|
|
184
|
+
get version() {
|
|
185
|
+
return this.rev;
|
|
186
|
+
}
|
|
187
|
+
summary() {
|
|
188
|
+
let open = 0;
|
|
189
|
+
let pending = 0;
|
|
190
|
+
for (const record of this.subs.values()) {
|
|
191
|
+
open++;
|
|
192
|
+
if (record.status === "pending")
|
|
193
|
+
pending++;
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
...this.startup,
|
|
197
|
+
openSubscriptions: open,
|
|
198
|
+
pendingSubscriptions: pending,
|
|
199
|
+
pendingMutations: this.mutations.size,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/** Listen for changes; batched to one call per microtask. Returns the unsubscribe. */
|
|
203
|
+
subscribe(listener) {
|
|
204
|
+
this.listeners.add(listener);
|
|
205
|
+
return () => {
|
|
206
|
+
this.listeners.delete(listener);
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** Forget the records. Open subscriptions and mutations keep being tracked, as new rows. */
|
|
210
|
+
clear() {
|
|
211
|
+
this.list = [];
|
|
212
|
+
this.subs.clear();
|
|
213
|
+
this.mutations.clear();
|
|
214
|
+
this.resubscribedAt.clear();
|
|
215
|
+
this.changed();
|
|
216
|
+
}
|
|
217
|
+
/** Everything as JSON — masked already, ready to paste into an issue. */
|
|
218
|
+
export() {
|
|
219
|
+
return JSON.stringify({ summary: this.summary(), records: this.list }, null, 2);
|
|
220
|
+
}
|
|
221
|
+
// -------------------------------------------------------------- connection
|
|
222
|
+
onConnect() {
|
|
223
|
+
var _a;
|
|
224
|
+
const ts = this.now();
|
|
225
|
+
this.connectAt = ts;
|
|
226
|
+
(_a = this.startup).connectAt ?? (_a.connectAt = ts);
|
|
227
|
+
this.add({ type: "ws", name: "connect", ts, durationMs: null, bytes: null, status: "pending", meta: {} });
|
|
228
|
+
}
|
|
229
|
+
onOpen() {
|
|
230
|
+
var _a;
|
|
231
|
+
const ts = this.now();
|
|
232
|
+
this.connection += 1;
|
|
233
|
+
this.startup.connections = this.connection;
|
|
234
|
+
(_a = this.startup).openAt ?? (_a.openAt = ts);
|
|
235
|
+
const meta = { connection: this.connection };
|
|
236
|
+
if (this.closedAt !== null)
|
|
237
|
+
meta.downMs = round(ts - this.closedAt);
|
|
238
|
+
this.openedAt = ts;
|
|
239
|
+
this.closedAt = null;
|
|
240
|
+
const since = this.connectAt ?? ts;
|
|
241
|
+
this.add({ type: "ws", name: "open", ts: since, durationMs: round(ts - since), bytes: null, status: "ok", meta });
|
|
242
|
+
}
|
|
243
|
+
onHello(bytes) {
|
|
244
|
+
this.add({
|
|
245
|
+
type: "ws",
|
|
246
|
+
name: "hello",
|
|
247
|
+
ts: this.now(),
|
|
248
|
+
durationMs: null,
|
|
249
|
+
bytes,
|
|
250
|
+
status: "ok",
|
|
251
|
+
meta: { connection: this.connection },
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
onWelcome(bytes) {
|
|
255
|
+
var _a;
|
|
256
|
+
const ts = this.now();
|
|
257
|
+
(_a = this.startup).welcomeAt ?? (_a.welcomeAt = ts);
|
|
258
|
+
const since = this.openedAt ?? ts;
|
|
259
|
+
this.add({
|
|
260
|
+
type: "ws",
|
|
261
|
+
name: "welcome",
|
|
262
|
+
ts: since,
|
|
263
|
+
durationMs: round(ts - since),
|
|
264
|
+
bytes,
|
|
265
|
+
status: "ok",
|
|
266
|
+
meta: { connection: this.connection },
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
onClose(intentional) {
|
|
270
|
+
const ts = this.now();
|
|
271
|
+
const meta = { connection: this.connection };
|
|
272
|
+
if (this.openedAt !== null)
|
|
273
|
+
meta.upMs = round(ts - this.openedAt);
|
|
274
|
+
this.openedAt = null;
|
|
275
|
+
this.closedAt = ts;
|
|
276
|
+
this.add({ type: "ws", name: intentional ? "close" : "disconnect", ts, durationMs: null, bytes: null, status: intentional ? "closed" : "error", meta });
|
|
277
|
+
}
|
|
278
|
+
onReconnectScheduled(delayMs) {
|
|
279
|
+
this.startup.reconnects += 1;
|
|
280
|
+
this.add({ type: "ws", name: "reconnect", ts: this.now(), durationMs: null, bytes: null, status: "pending", meta: { delayMs } });
|
|
281
|
+
}
|
|
282
|
+
onTransportError(message) {
|
|
283
|
+
this.add({ type: "ws", name: "error", ts: this.now(), durationMs: null, bytes: null, status: "error", meta: { message: shorten(message) } });
|
|
284
|
+
}
|
|
285
|
+
// ------------------------------------------------------------ subscriptions
|
|
286
|
+
onSubscribe(subId, query, args, hydrated) {
|
|
287
|
+
var _a;
|
|
288
|
+
const ts = this.now();
|
|
289
|
+
(_a = this.startup).firstSubscribeAt ?? (_a.firstSubscribeAt = ts);
|
|
290
|
+
const meta = { subId, args: this.mask(query, args), updateCount: 0, updateBytes: 0, updates: [] };
|
|
291
|
+
if (hydrated)
|
|
292
|
+
meta.hydrated = true;
|
|
293
|
+
const record = { id: 0, type: "subscribe", name: query, ts, durationMs: null, bytes: null, status: "pending", meta };
|
|
294
|
+
this.subs.set(subId, record);
|
|
295
|
+
this.add(record);
|
|
296
|
+
}
|
|
297
|
+
/** All subscriptions were sent again after a reconnect. */
|
|
298
|
+
onResubscribe(subId) {
|
|
299
|
+
if (this.subs.has(subId))
|
|
300
|
+
this.resubscribedAt.set(subId, this.now());
|
|
301
|
+
}
|
|
302
|
+
/** A snapshot for `subId` — the first answer, a resubscribe, or a permission change. */
|
|
303
|
+
onSnapshot(subId, bytes, permissionChange) {
|
|
304
|
+
var _a;
|
|
305
|
+
const record = this.subs.get(subId);
|
|
306
|
+
if (!record)
|
|
307
|
+
return;
|
|
308
|
+
const ts = this.now();
|
|
309
|
+
if (record.durationMs === null) {
|
|
310
|
+
(_a = this.startup).firstDataAt ?? (_a.firstDataAt = ts);
|
|
311
|
+
record.durationMs = round(ts - record.ts);
|
|
312
|
+
record.bytes = bytes;
|
|
313
|
+
record.status = "ok";
|
|
314
|
+
this.resubscribedAt.delete(subId);
|
|
315
|
+
this.changed();
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const resubscribed = this.resubscribedAt.get(subId);
|
|
319
|
+
if (resubscribed !== undefined) {
|
|
320
|
+
this.resubscribedAt.delete(subId);
|
|
321
|
+
if (record.status !== "closed")
|
|
322
|
+
record.status = "ok";
|
|
323
|
+
this.update(record, { ts, bytes, cause: "resubscribe", latencyMs: round(ts - resubscribed) });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (permissionChange && record.status === "rejected")
|
|
327
|
+
record.status = "ok";
|
|
328
|
+
this.update(record, { ts, bytes, cause: permissionChange ? "permission-change" : "snapshot" });
|
|
329
|
+
}
|
|
330
|
+
onPatch(subId, bytes) {
|
|
331
|
+
const record = this.subs.get(subId);
|
|
332
|
+
if (!record)
|
|
333
|
+
return;
|
|
334
|
+
this.update(record, { ts: this.now(), bytes, cause: "patch" });
|
|
335
|
+
}
|
|
336
|
+
/** Revoked (`rejected`) or failed (`error`). */
|
|
337
|
+
onSubscriptionRefused(subId, status, code) {
|
|
338
|
+
var _a;
|
|
339
|
+
const record = this.subs.get(subId);
|
|
340
|
+
if (!record)
|
|
341
|
+
return;
|
|
342
|
+
const ts = this.now();
|
|
343
|
+
if (record.durationMs === null) {
|
|
344
|
+
(_a = this.startup).firstDataAt ?? (_a.firstDataAt = ts);
|
|
345
|
+
record.durationMs = round(ts - record.ts);
|
|
346
|
+
}
|
|
347
|
+
record.status = status;
|
|
348
|
+
record.meta.code = shorten(code);
|
|
349
|
+
this.resubscribedAt.delete(subId);
|
|
350
|
+
this.changed();
|
|
351
|
+
}
|
|
352
|
+
onUnsubscribe(subId) {
|
|
353
|
+
const record = this.subs.get(subId);
|
|
354
|
+
if (!record)
|
|
355
|
+
return;
|
|
356
|
+
this.subs.delete(subId);
|
|
357
|
+
this.resubscribedAt.delete(subId);
|
|
358
|
+
record.meta.unsubscribedAt = this.now();
|
|
359
|
+
if (record.status === "pending" || record.status === "ok")
|
|
360
|
+
record.status = "closed";
|
|
361
|
+
this.changed();
|
|
362
|
+
}
|
|
363
|
+
// ---------------------------------------------------------------- mutations
|
|
364
|
+
onMutate(mutationId, name, args, bytes, optimisticMs) {
|
|
365
|
+
const meta = { mutationId, args: this.mask(name, args) };
|
|
366
|
+
if (optimisticMs !== null)
|
|
367
|
+
meta.optimisticMs = round(optimisticMs);
|
|
368
|
+
const record = { id: 0, type: "mutation", name, ts: this.now(), durationMs: null, bytes, status: "pending", meta };
|
|
369
|
+
this.mutations.set(mutationId, record);
|
|
370
|
+
this.add(record);
|
|
371
|
+
}
|
|
372
|
+
onMutationResent(mutationId) {
|
|
373
|
+
const record = this.mutations.get(mutationId);
|
|
374
|
+
if (!record)
|
|
375
|
+
return;
|
|
376
|
+
record.meta.resent = (record.meta.resent ?? 0) + 1;
|
|
377
|
+
this.changed();
|
|
378
|
+
}
|
|
379
|
+
onMutationResult(mutationId, ok, error) {
|
|
380
|
+
const record = this.mutations.get(mutationId);
|
|
381
|
+
if (!record)
|
|
382
|
+
return;
|
|
383
|
+
this.mutations.delete(mutationId);
|
|
384
|
+
record.durationMs = round(this.now() - record.ts);
|
|
385
|
+
record.status = ok ? "ok" : "rejected";
|
|
386
|
+
if (!ok && error)
|
|
387
|
+
record.meta.code = shorten(error);
|
|
388
|
+
this.changed();
|
|
389
|
+
}
|
|
390
|
+
/** Acknowledged by data after a reconnect, its own result lost with the old socket. */
|
|
391
|
+
onMutationSettled(mutationId) {
|
|
392
|
+
this.onMutationResult(mutationId, true, undefined);
|
|
393
|
+
}
|
|
394
|
+
onMutationLost(mutationId) {
|
|
395
|
+
const record = this.mutations.get(mutationId);
|
|
396
|
+
if (!record)
|
|
397
|
+
return;
|
|
398
|
+
this.mutations.delete(mutationId);
|
|
399
|
+
record.status = "lost";
|
|
400
|
+
this.changed();
|
|
401
|
+
}
|
|
402
|
+
// ------------------------------------------------------------------ internal
|
|
403
|
+
mask(endpoint, args) {
|
|
404
|
+
try {
|
|
405
|
+
return this.opts.mask ? this.opts.mask(endpoint, args) : maskArgs(endpoint, args, this.opts);
|
|
406
|
+
}
|
|
407
|
+
catch {
|
|
408
|
+
return exports.MASKED;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
update(record, update) {
|
|
412
|
+
const meta = record.meta;
|
|
413
|
+
meta.updateCount = (meta.updateCount ?? 0) + 1;
|
|
414
|
+
meta.updateBytes = (meta.updateBytes ?? 0) + (update.bytes ?? 0);
|
|
415
|
+
const updates = meta.updates ?? (meta.updates = []);
|
|
416
|
+
updates.push(update);
|
|
417
|
+
if (updates.length > exports.UPDATE_LIMIT)
|
|
418
|
+
updates.splice(0, updates.length - exports.UPDATE_LIMIT);
|
|
419
|
+
this.changed();
|
|
420
|
+
}
|
|
421
|
+
add(fields) {
|
|
422
|
+
const record = fields;
|
|
423
|
+
record.id = ++this.seq;
|
|
424
|
+
this.list.push(record);
|
|
425
|
+
if (this.list.length > this.limit)
|
|
426
|
+
this.list.splice(0, this.list.length - this.limit);
|
|
427
|
+
this.changed();
|
|
428
|
+
}
|
|
429
|
+
changed() {
|
|
430
|
+
this.rev += 1;
|
|
431
|
+
// A record changed in place still has to look new to a reader comparing
|
|
432
|
+
// arrays; a fresh array per change is cheap next to the message that caused it.
|
|
433
|
+
this.list = this.list.slice();
|
|
434
|
+
if (this.notifyQueued || this.listeners.size === 0)
|
|
435
|
+
return;
|
|
436
|
+
this.notifyQueued = true;
|
|
437
|
+
queueMicrotask(() => {
|
|
438
|
+
this.notifyQueued = false;
|
|
439
|
+
for (const listener of [...this.listeners])
|
|
440
|
+
listener();
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
exports.NowityDevtools = NowityDevtools;
|
|
@@ -2,6 +2,7 @@ import { observable } from "mobx";
|
|
|
2
2
|
import { type DerivedMutator } from "../core/derived";
|
|
3
3
|
import type { HydrationPayload, ServerMessage, ServerWelcome } from "../core/protocol";
|
|
4
4
|
import type { EntityId } from "../core/types";
|
|
5
|
+
import { NowityDevtools, type DevtoolsOptions } from "./devtools";
|
|
5
6
|
import { LiveQuery, type QueryDescriptor } from "./live-query";
|
|
6
7
|
import { type LocalTx } from "./overlay";
|
|
7
8
|
import type { Transport } from "./transport";
|
|
@@ -139,6 +140,12 @@ export interface NowityClientOptions {
|
|
|
139
140
|
onError?: (error: Error) => void;
|
|
140
141
|
/** Observability hook: every message received, before it is applied. */
|
|
141
142
|
onServerMessage?: (message: ServerMessage) => void;
|
|
143
|
+
/**
|
|
144
|
+
* Record what this client does for a developer panel (`nowity/devtools`).
|
|
145
|
+
* Off by default; see {@link NowityClient.enableDevtools}, which also
|
|
146
|
+
* switches it on later.
|
|
147
|
+
*/
|
|
148
|
+
devtools?: boolean | DevtoolsOptions;
|
|
142
149
|
}
|
|
143
150
|
export type ConnectionStatus = "idle" | "connecting" | "connected" | "disconnected";
|
|
144
151
|
/**
|
|
@@ -224,7 +231,23 @@ export declare class NowityClient {
|
|
|
224
231
|
private everWelcomed;
|
|
225
232
|
/** What this connection says it is looking at; resent after every reconnect. */
|
|
226
233
|
private context;
|
|
234
|
+
/** See {@link enableDevtools}. `null` = nothing is measured. */
|
|
235
|
+
private recorder;
|
|
227
236
|
constructor(opts: NowityClientOptions);
|
|
237
|
+
/**
|
|
238
|
+
* Start recording subscribes, mutations and connection events for a
|
|
239
|
+
* developer panel (`nowity/devtools`). Idempotent: a second call returns the
|
|
240
|
+
* recorder already running, whatever its options.
|
|
241
|
+
*
|
|
242
|
+
* Turned on before the client connects (the `devtools` option), it sees the
|
|
243
|
+
* page's startup; turned on later, it sees what happens from then on —
|
|
244
|
+
* subscriptions already open are not in it.
|
|
245
|
+
*/
|
|
246
|
+
enableDevtools(options?: DevtoolsOptions): NowityDevtools;
|
|
247
|
+
/** Stop recording and drop what was recorded. */
|
|
248
|
+
disableDevtools(): void;
|
|
249
|
+
/** The running recorder, or `null` when devtools are off. */
|
|
250
|
+
get devtools(): NowityDevtools | null;
|
|
228
251
|
connect(): void;
|
|
229
252
|
close(): void;
|
|
230
253
|
/** Drop the connection as if the network died. For tests and for retry UX. */
|
|
@@ -279,6 +302,14 @@ export declare class NowityClient {
|
|
|
279
302
|
/** The derived mutators a subscription holds, for diagnostics and tests. */
|
|
280
303
|
derivedMutatorsOf(subId: string): DerivedMutator[];
|
|
281
304
|
private handleMessage;
|
|
305
|
+
/**
|
|
306
|
+
* Note a message for the devtools recorder, before it is applied.
|
|
307
|
+
*
|
|
308
|
+
* Sizes come from the raw frame when the transport has it; a transaction
|
|
309
|
+
* spread over several subscriptions is measured per subscription, by its
|
|
310
|
+
* own part of the message.
|
|
311
|
+
*/
|
|
312
|
+
private record;
|
|
282
313
|
private handleWelcome;
|
|
283
314
|
private handleSnapshotMessage;
|
|
284
315
|
private applySnapshot;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"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