@phibac/sdk 0.0.0-stage → 0.1.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/dist/live.js ADDED
@@ -0,0 +1,448 @@
1
+ import { PbError } from "./errors.js";
2
+ import { asPbError, isTransient } from "./queue.js";
3
+ const BACKOFF_START_MS = 1000;
4
+ const BACKOFF_MAX_MS = 60_000;
5
+ /** Close reasons after which the SDK must not reconnect. */
6
+ const FINAL_REASONS = new Set(["app_suspended", "account_closed"]);
7
+ /**
8
+ * Live holds at most one WebSocket for the collections that have
9
+ * subscribers. On every open it first catches up from /v1/changes, then
10
+ * applies live changes; it reconnects with backoff (1 s doubling to 60 s)
11
+ * after lagging, revoked or a dropped connection, and stops for good on
12
+ * app_suspended or account_closed (reported through Net.problem).
13
+ */
14
+ export class Live {
15
+ d;
16
+ watched = new Map();
17
+ /** The collections the current (or opening) connection is for. */
18
+ target = [];
19
+ ws = null;
20
+ caughtUp = false;
21
+ gen = 0;
22
+ attempt = 0;
23
+ timer;
24
+ syncQueued = false;
25
+ stopped = false;
26
+ halted = false;
27
+ /** Waiting for something to change (a permanent ticket error). */
28
+ parked = false;
29
+ /** Serialises catch-ups and change application. */
30
+ tail = Promise.resolve();
31
+ /**
32
+ * Collections the server has shown this app can read (a ticket or query
33
+ * for them succeeded). Only for these, or for a collection the changes
34
+ * feed returned a change of, is a catch-up position stored: for an
35
+ * unreadable collection the feed answers "nothing, up to the latest rev",
36
+ * and storing that would skip the history once access is granted.
37
+ */
38
+ proven = new Set();
39
+ /**
40
+ * Watched collections left out of the live connection because the server
41
+ * refused them (no consent, or unknown), so the others still get live
42
+ * updates. Cleared when access is granted or the device comes online.
43
+ */
44
+ blocked = new Set();
45
+ /** Collections whose background catch-up has started in this session. */
46
+ started = new Set();
47
+ /** Background catch-up retries, by collection. */
48
+ retries = new Map();
49
+ constructor(d) {
50
+ this.d = d;
51
+ }
52
+ /** watch keeps collection live until the returned function is called. */
53
+ watch(collection) {
54
+ this.watched.set(collection, (this.watched.get(collection) ?? 0) + 1);
55
+ this.schedule();
56
+ let done = false;
57
+ return () => {
58
+ if (done)
59
+ return;
60
+ done = true;
61
+ const n = (this.watched.get(collection) ?? 1) - 1;
62
+ if (n <= 0)
63
+ this.watched.delete(collection);
64
+ else
65
+ this.watched.set(collection, n);
66
+ this.schedule();
67
+ };
68
+ }
69
+ /** Whether collection is covered by an open, caught-up connection. */
70
+ isLive(collection) {
71
+ return this.caughtUp && this.ws !== null && this.target.includes(collection);
72
+ }
73
+ /**
74
+ * ensure starts collection's background catch-up the first time it is
75
+ * used in this session (reads never wait for it). A failure is retried
76
+ * with backoff and, if it fails again, shown in status().problem.
77
+ */
78
+ ensure(collection) {
79
+ if (this.started.has(collection))
80
+ return;
81
+ this.started.add(collection);
82
+ this.background(collection);
83
+ }
84
+ background(collection) {
85
+ if (this.stopped || !this.d.net.deviceOnline || this.isLive(collection))
86
+ return;
87
+ const r = this.retries.get(collection) ?? { attempt: 0, timer: undefined };
88
+ this.retries.set(collection, r);
89
+ if (r.timer !== undefined)
90
+ clearTimeout(r.timer);
91
+ r.timer = undefined;
92
+ this.serial(() => this.catchUp([collection])).then(() => {
93
+ r.attempt = 0;
94
+ this.d.net.setProblem(`catch-up ${collection}`, undefined);
95
+ }, (e) => {
96
+ if (this.stopped)
97
+ return;
98
+ const err = asPbError(e);
99
+ if (err.code === "consent_required")
100
+ this.unreadable([collection]);
101
+ if (r.attempt > 0 && err.code !== "network")
102
+ this.d.net.setProblem(`catch-up ${collection}`, err);
103
+ const delay = Math.min(BACKOFF_MAX_MS, BACKOFF_START_MS * 2 ** r.attempt);
104
+ r.attempt++;
105
+ r.timer = setTimeout(() => {
106
+ r.timer = undefined;
107
+ this.background(collection);
108
+ }, delay);
109
+ });
110
+ }
111
+ /** readable records that the server just served collection to this app. */
112
+ readable(collection) {
113
+ this.proven.add(collection);
114
+ }
115
+ /**
116
+ * unreadable forgets the catch-up positions of collections the server
117
+ * refused (consent_required, revoked): when access comes back, their
118
+ * history is fetched again from the start.
119
+ */
120
+ unreadable(collections) {
121
+ for (const c of collections) {
122
+ this.proven.delete(c);
123
+ void this.serial(async () => {
124
+ if (!this.stopped)
125
+ await this.d.cache.setLastRev(c, 0);
126
+ }).catch(() => undefined);
127
+ }
128
+ }
129
+ /**
130
+ * granted is called when the user granted read access to collection (or
131
+ * the majors read of it widened): its history is fetched from the start,
132
+ * and the live connection is renewed.
133
+ */
134
+ granted(collection) {
135
+ this.blocked.delete(collection);
136
+ this.unreadable([collection]);
137
+ this.started.add(collection);
138
+ const r = this.retries.get(collection);
139
+ if (r)
140
+ r.attempt = 0;
141
+ this.background(collection);
142
+ this.refresh();
143
+ }
144
+ /** idle resolves once no catch-up or change is being applied. */
145
+ async idle() {
146
+ let t;
147
+ do {
148
+ t = this.tail;
149
+ await t;
150
+ } while (t !== this.tail);
151
+ }
152
+ /**
153
+ * fresh brings collection's cache up to date from /v1/changes now, unless
154
+ * the live connection already does (used before refusing a write to a
155
+ * record the cache does not know). Offline, or when the server cannot be
156
+ * reached, the cache is used as it is.
157
+ */
158
+ async fresh(collection) {
159
+ if (this.isLive(collection) || !this.d.net.deviceOnline)
160
+ return;
161
+ try {
162
+ await this.serial(() => this.catchUp([collection]));
163
+ }
164
+ catch (e) {
165
+ if (asPbError(e).code !== "network")
166
+ throw asPbError(e);
167
+ }
168
+ }
169
+ /**
170
+ * refresh reconnects now and re-runs the background catch-ups (the device
171
+ * came online, or access was granted).
172
+ */
173
+ refresh() {
174
+ if (this.stopped || this.halted)
175
+ return;
176
+ for (const c of this.started)
177
+ this.background(c);
178
+ this.blocked.clear();
179
+ this.attempt = 0;
180
+ this.parked = false;
181
+ this.close();
182
+ this.schedule();
183
+ }
184
+ stop() {
185
+ this.stopped = true;
186
+ for (const r of this.retries.values())
187
+ if (r.timer !== undefined)
188
+ clearTimeout(r.timer);
189
+ this.close();
190
+ }
191
+ // ---- connection ----
192
+ schedule() {
193
+ if (this.syncQueued)
194
+ return;
195
+ this.syncQueued = true;
196
+ queueMicrotask(() => {
197
+ this.syncQueued = false;
198
+ this.sync();
199
+ });
200
+ }
201
+ sync() {
202
+ if (this.stopped || this.halted)
203
+ return;
204
+ const want = [...this.watched.keys()].filter((c) => !this.blocked.has(c)).sort();
205
+ const same = want.length === this.target.length && want.every((c, i) => c === this.target[i]);
206
+ if (same && (this.ws !== null || this.timer !== undefined || this.opening || this.parked))
207
+ return;
208
+ this.close();
209
+ if (want.length > 0)
210
+ void this.open(want);
211
+ }
212
+ opening = false;
213
+ close() {
214
+ this.gen++;
215
+ if (this.timer !== undefined)
216
+ clearTimeout(this.timer);
217
+ this.timer = undefined;
218
+ this.opening = false;
219
+ this.caughtUp = false;
220
+ this.target = [];
221
+ const ws = this.ws;
222
+ this.ws = null;
223
+ if (ws) {
224
+ ws.onopen = ws.onmessage = ws.onclose = ws.onerror = null;
225
+ try {
226
+ ws.close();
227
+ }
228
+ catch {
229
+ // already closed
230
+ }
231
+ }
232
+ }
233
+ retryLater() {
234
+ if (this.stopped || this.halted)
235
+ return;
236
+ const delay = Math.min(BACKOFF_MAX_MS, BACKOFF_START_MS * 2 ** this.attempt);
237
+ this.attempt++;
238
+ const want = this.target;
239
+ this.timer = setTimeout(() => {
240
+ this.timer = undefined;
241
+ this.target = [];
242
+ if (want.length > 0)
243
+ this.sync();
244
+ }, delay);
245
+ }
246
+ halt(e) {
247
+ this.halted = true;
248
+ this.close();
249
+ this.d.net.setProblem("live", e);
250
+ }
251
+ async open(want) {
252
+ const gen = this.gen;
253
+ this.target = want;
254
+ this.parked = false;
255
+ if (!this.d.net.deviceOnline) {
256
+ this.parked = true; // the online event calls refresh()
257
+ return;
258
+ }
259
+ this.opening = true;
260
+ let ticket;
261
+ try {
262
+ const res = await this.d.net.request("POST", "/v1/subscribe/ticket", { collections: want });
263
+ if (typeof res?.ticket !== "string")
264
+ throw new PbError("invalid_response", "The server returned no subscribe ticket.", "Retry later.");
265
+ ticket = res.ticket;
266
+ }
267
+ catch (e) {
268
+ if (gen !== this.gen)
269
+ return;
270
+ this.opening = false;
271
+ const err = asPbError(e);
272
+ if (FINAL_REASONS.has(err.code))
273
+ return this.halt(err);
274
+ if (isTransient(err))
275
+ return this.retryLater();
276
+ this.d.net.setProblem("live", err);
277
+ if (err.code === "consent_required" || err.code === "not_found") {
278
+ // Leave out only the collections the server refuses; the rest
279
+ // reconnect at once.
280
+ const bad = await this.refused(want, err);
281
+ if (gen !== this.gen)
282
+ return;
283
+ if (bad.length > 0) {
284
+ for (const c of bad)
285
+ this.blocked.add(c);
286
+ if (err.code === "consent_required")
287
+ this.unreadable(bad);
288
+ this.target = [];
289
+ this.sync();
290
+ return;
291
+ }
292
+ }
293
+ // Nothing to single out: wait until the subscriptions change or
294
+ // access is granted (refresh).
295
+ this.parked = true;
296
+ return;
297
+ }
298
+ if (gen !== this.gen)
299
+ return;
300
+ this.opening = false;
301
+ // The ticket is only issued when every collection is readable.
302
+ for (const c of want)
303
+ this.proven.add(c);
304
+ const url = `${this.d.base.replace(/^http/i, "ws")}/v1/subscribe?ticket=${encodeURIComponent(ticket)}`;
305
+ let ws;
306
+ try {
307
+ ws = new WebSocket(url);
308
+ }
309
+ catch {
310
+ return this.retryLater();
311
+ }
312
+ this.ws = ws;
313
+ let reason = "";
314
+ ws.onopen = () => {
315
+ if (gen !== this.gen)
316
+ return;
317
+ this.serial(() => this.catchUp(want)).then(() => {
318
+ if (gen !== this.gen)
319
+ return;
320
+ this.caughtUp = true;
321
+ this.attempt = 0;
322
+ if (this.blocked.size === 0)
323
+ this.d.net.setProblem("live", undefined);
324
+ }, () => {
325
+ if (gen !== this.gen)
326
+ return;
327
+ this.close();
328
+ this.target = want;
329
+ this.retryLater();
330
+ });
331
+ };
332
+ ws.onmessage = (ev) => {
333
+ if (gen !== this.gen)
334
+ return;
335
+ let msg;
336
+ try {
337
+ msg = JSON.parse(String(ev.data));
338
+ }
339
+ catch {
340
+ return;
341
+ }
342
+ if (msg.type === "change" && msg.change) {
343
+ const ch = msg.change;
344
+ void this.serial(() => this.apply([ch])).catch(() => undefined);
345
+ }
346
+ else if (msg.type === "closed" && typeof msg.reason === "string") {
347
+ reason = msg.reason;
348
+ }
349
+ };
350
+ ws.onclose = () => {
351
+ if (gen !== this.gen)
352
+ return;
353
+ this.ws = null;
354
+ this.caughtUp = false;
355
+ // Access may have been revoked: positions are no longer trusted until
356
+ // a read proves the collection readable again; the reconnect's ticket
357
+ // finds which collection (if any) is now refused.
358
+ if (reason === "revoked")
359
+ for (const c of want)
360
+ this.proven.delete(c);
361
+ if (FINAL_REASONS.has(reason)) {
362
+ return this.halt(new PbError(reason, `The live connection was closed: ${reason}.`, reason === "app_suspended" ? "This app has been suspended; contact its developer." : "The account is closed or being deleted."));
363
+ }
364
+ this.gen++; // ignore anything late from this socket
365
+ this.target = want;
366
+ this.retryLater();
367
+ };
368
+ }
369
+ /**
370
+ * refused returns which of collections the server refused a ticket for:
371
+ * the one the error message names (the server quotes it), else each
372
+ * other app's collection (declared with versions) that a one-record read
373
+ * refuses. The app's own collections are never singled out.
374
+ */
375
+ async refused(collections, err) {
376
+ const named = collections.filter((c) => err.message.includes(`"${c}"`));
377
+ if (named.length > 0)
378
+ return named;
379
+ const out = [];
380
+ for (const c of collections) {
381
+ const versions = this.d.versionsOf(c);
382
+ if (!versions)
383
+ continue;
384
+ try {
385
+ await this.d.net.request("GET", `/v1/records/${encodeURIComponent(c)}?limit=1&versions=${versions.join(",")}`);
386
+ }
387
+ catch (e) {
388
+ const code = asPbError(e).code;
389
+ if (code === "consent_required" || code === "not_found")
390
+ out.push(c);
391
+ }
392
+ }
393
+ return out;
394
+ }
395
+ // ---- applying changes ----
396
+ serial(fn) {
397
+ const run = this.tail.then(fn, fn);
398
+ this.tail = run.catch(() => undefined);
399
+ return run;
400
+ }
401
+ /**
402
+ * catchUp applies every change since the collections' last caught-up
403
+ * revision, calling /v1/changes until the returned since stops moving,
404
+ * then stores that revision for each collection.
405
+ */
406
+ async catchUp(collections) {
407
+ const revs = await Promise.all(collections.map((c) => this.d.cache.lastRev(c)));
408
+ let since = Math.min(...revs);
409
+ const names = collections.map(encodeURIComponent).join(",");
410
+ const seen = new Set();
411
+ for (;;) {
412
+ if (this.stopped)
413
+ return;
414
+ const res = await this.d.net.request("GET", `/v1/changes?since=${since}&collections=${names}`);
415
+ const changes = res?.changes ?? [];
416
+ for (const ch of changes)
417
+ seen.add(ch.collection);
418
+ await this.apply(changes);
419
+ const next = res?.since;
420
+ if (typeof next !== "number" || next <= since)
421
+ break;
422
+ since = next;
423
+ }
424
+ if (this.stopped)
425
+ return;
426
+ for (const [i, c] of collections.entries()) {
427
+ // Only where the server showed the collection is readable (see proven).
428
+ if (this.stopped)
429
+ return;
430
+ if (since > revs[i] && (this.proven.has(c) || seen.has(c)))
431
+ await this.d.cache.setLastRev(c, since);
432
+ }
433
+ }
434
+ async apply(changes) {
435
+ if (changes.length === 0 || this.stopped)
436
+ return;
437
+ await this.d.cache.write(async (w) => {
438
+ for (const ch of changes) {
439
+ const rec = ch.op === "delete" ? null : ch.record;
440
+ // A changes-feed record is the record's current state, which may be
441
+ // newer than the change itself.
442
+ const rev = Math.max(ch.rev, rec?.rev ?? 0);
443
+ await w.applyServer(ch.collection, ch.rkey, rev, rec, this.d.versionsOf(ch.collection));
444
+ }
445
+ });
446
+ }
447
+ }
448
+ //# sourceMappingURL=live.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live.js","sourceRoot":"","sources":["../src/live.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,WAAW,EAAY,MAAM,YAAY,CAAC;AAE9D,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,cAAc,GAAG,MAAM,CAAC;AAE9B,4DAA4D;AAC5D,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,eAAe,EAAE,gBAAgB,CAAC,CAAC,CAAC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YACnC,KAAK,MAAM,EAAE,IAAI,OAAO,EAAE,CAAC;gBACzB,MAAM,GAAG,GAAG,EAAE,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC;gBAClD,oEAAoE;gBACpE,gCAAgC;gBAChC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;gBAC5C,MAAM,CAAC,CAAC,WAAW,CAAC,EAAE,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;YAC1F,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,103 @@
1
+ import { type Cache } from "./cache.js";
2
+ import { PbError } from "./errors.js";
3
+ import type { Http } from "./http.js";
4
+ import type { QueuedOp } from "./index.js";
5
+ export declare function isTransient(e: PbError): boolean;
6
+ export declare function asPbError(e: unknown): PbError;
7
+ /**
8
+ * Net makes every API call for the SDK's background work and remembers
9
+ * whether the last one reached the server. problem() is the first
10
+ * condition the app should show the user (storage_full, trial_ended,
11
+ * app_suspended, account_closed, consent_required for live updates), or
12
+ * undefined.
13
+ */
14
+ export declare class Net {
15
+ readonly http: Http;
16
+ private reached;
17
+ private readonly problems;
18
+ /** Aborted by abort(): cancels every request in flight (signOut). */
19
+ private readonly ac;
20
+ constructor(http: Http);
21
+ /** Whether the device says it is online and the last call reached the server. */
22
+ get online(): boolean;
23
+ /** Whether the device says it is online (requests are worth trying). */
24
+ get deviceOnline(): boolean;
25
+ set online(v: boolean);
26
+ request<T>(method: string, path: string, body?: unknown, headers?: Record<string, string>): Promise<T>;
27
+ /** abort cancels every request in flight and every later one. */
28
+ abort(): void;
29
+ problem(): PbError | undefined;
30
+ setProblem(source: string, e: PbError | undefined): void;
31
+ }
32
+ export interface ConflictEvent {
33
+ op: QueuedOp;
34
+ error: PbError;
35
+ localData: unknown;
36
+ }
37
+ export interface FlusherDeps {
38
+ cache: Cache;
39
+ net: Net;
40
+ /** The Web Locks name: one flusher per server, app and user across tabs. */
41
+ lockName: string;
42
+ /** Reports a write the server refused for good. */
43
+ conflict: (e: ConflictEvent) => void;
44
+ /** Called whenever the number of queued writes may have changed. */
45
+ counted?: () => void;
46
+ }
47
+ /**
48
+ * Flusher sends the queue in order through /v1/batch: chunks of at most
49
+ * 100 operations and about 3.5 MiB, only while the device is online, and
50
+ * only in the tab holding the queue's Web Lock. A success stores the
51
+ * server's record; a permanent error drops the op through onConflict and
52
+ * reverts the record; a transient error keeps the op and everything after
53
+ * it and retries with backoff (1 s doubling to 60 s, at least Retry-After).
54
+ */
55
+ export declare class Flusher {
56
+ private readonly d;
57
+ /** The number of queued writes, as last counted. */
58
+ pending: number;
59
+ private timer;
60
+ private attempt;
61
+ private running;
62
+ private again;
63
+ private stopped;
64
+ /** The flush in progress (awaited by idle()). */
65
+ private current;
66
+ /** Send one op alone next: a whole batch was refused for good. */
67
+ private solo;
68
+ private localTail;
69
+ constructor(d: FlusherDeps);
70
+ /** count re-reads the queue length. */
71
+ count(): Promise<void>;
72
+ /**
73
+ * kick asks for a flush. While a retry is scheduled the retry runs instead,
74
+ * unless now is true (the device just came online), which retries at once
75
+ * with the backoff reset.
76
+ */
77
+ kick(now?: boolean): void;
78
+ stop(): void;
79
+ /**
80
+ * idle resolves once no flush is in progress (after stop(), none starts).
81
+ * It can wait on another tab holding the lock: callers bound the wait.
82
+ */
83
+ idle(): Promise<void>;
84
+ private withLock;
85
+ private retryLater;
86
+ /**
87
+ * chunk takes the first ops of the queue that fit in one batch, cut before
88
+ * the second op on any record. The server runs every op of a batch even
89
+ * after one fails, so two ops on one record in one batch could be applied
90
+ * out of order when the first fails transiently and is retried.
91
+ */
92
+ private chunk;
93
+ private flushLocked;
94
+ private noteProblem;
95
+ /** success stores the server's result of s and removes s from the queue. */
96
+ private success;
97
+ /**
98
+ * conflict drops s (and, when s is a create, every later op on the same
99
+ * record, which cannot succeed without it), reverts the record to its
100
+ * server state and reports each dropped op with the data the app had.
101
+ */
102
+ private conflict;
103
+ }