@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/LICENSE +202 -0
- package/README.md +53 -2
- package/dist/auth.d.ts +104 -0
- package/dist/auth.js +343 -0
- package/dist/auth.js.map +1 -0
- package/dist/brand.d.ts +7 -0
- package/dist/brand.js +8 -0
- package/dist/brand.js.map +1 -0
- package/dist/cache.d.ts +170 -0
- package/dist/cache.js +293 -0
- package/dist/cache.js.map +1 -0
- package/dist/collection.d.ts +119 -0
- package/dist/collection.js +511 -0
- package/dist/collection.js.map +1 -0
- package/dist/dpop.d.ts +50 -0
- package/dist/dpop.js +105 -0
- package/dist/dpop.js.map +1 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +55 -0
- package/dist/errors.js.map +1 -0
- package/dist/files.d.ts +25 -0
- package/dist/files.js +65 -0
- package/dist/files.js.map +1 -0
- package/dist/http.d.ts +53 -0
- package/dist/http.js +130 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +209 -0
- package/dist/index.js.map +1 -0
- package/dist/live.d.ts +116 -0
- package/dist/live.js +448 -0
- package/dist/live.js.map +1 -0
- package/dist/queue.d.ts +103 -0
- package/dist/queue.js +397 -0
- package/dist/queue.js.map +1 -0
- package/package.json +45 -4
- package/src/auth.ts +371 -0
- package/src/brand.ts +8 -0
- package/src/cache.ts +379 -0
- package/src/collection.ts +533 -0
- package/src/dpop.ts +132 -0
- package/src/errors.ts +61 -0
- package/src/files.ts +60 -0
- package/src/http.ts +154 -0
- package/src/index.ts +238 -0
- package/src/live.ts +434 -0
- package/src/queue.ts +419 -0
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
// Collections and queries. Writes go to the cache at once and into the
|
|
2
|
+
// offline queue; reads come from the cache; queries run on the server when
|
|
3
|
+
// online and on the cache offline, with the server's filter and sort rules
|
|
4
|
+
// (server/internal/records/query.go).
|
|
5
|
+
import { BRAND } from "./brand.js";
|
|
6
|
+
import { uuidv7, view } from "./cache.js";
|
|
7
|
+
import { PbError, SIGN_IN_HINT } from "./errors.js";
|
|
8
|
+
import { asPbError } from "./queue.js";
|
|
9
|
+
/** Bus calls the listeners of a collection when its cached records change. */
|
|
10
|
+
export class Bus {
|
|
11
|
+
listeners = new Map();
|
|
12
|
+
on(collection, fn) {
|
|
13
|
+
let set = this.listeners.get(collection);
|
|
14
|
+
if (!set)
|
|
15
|
+
this.listeners.set(collection, (set = new Set()));
|
|
16
|
+
set.add(fn);
|
|
17
|
+
return () => set.delete(fn);
|
|
18
|
+
}
|
|
19
|
+
emit(collections) {
|
|
20
|
+
for (const c of collections)
|
|
21
|
+
for (const fn of [...(this.listeners.get(c) ?? [])])
|
|
22
|
+
fn();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const WIRE_OPS = { "==": "eq", "!=": "ne", "<": "lt", "<=": "lte", ">": "gt", ">=": "gte", in: "in" };
|
|
26
|
+
const SYSTEM_FIELDS = new Set(["createdAt", "updatedAt"]);
|
|
27
|
+
/**
|
|
28
|
+
* The most records Query.get() returns without limit(), online and offline
|
|
29
|
+
* alike (sent to the server as the limit). limit(n) returns up to n.
|
|
30
|
+
*/
|
|
31
|
+
export const DEFAULT_QUERY_LIMIT = 500;
|
|
32
|
+
/**
|
|
33
|
+
* fieldValue is the value the server compares for field: createdAt and
|
|
34
|
+
* updatedAt as milliseconds, booleans as 1/0 (SQLite's json_extract), other
|
|
35
|
+
* JSON as its text; undefined when the record lacks the field or it is null
|
|
36
|
+
* (SQL NULL, which no comparison matches).
|
|
37
|
+
*/
|
|
38
|
+
function fieldValue(r, field) {
|
|
39
|
+
if (SYSTEM_FIELDS.has(field))
|
|
40
|
+
return Date.parse(field === "createdAt" ? r.createdAt : r.updatedAt);
|
|
41
|
+
const v = r.data?.[field];
|
|
42
|
+
if (v === undefined || v === null)
|
|
43
|
+
return undefined;
|
|
44
|
+
if (typeof v === "boolean")
|
|
45
|
+
return v ? 1 : 0;
|
|
46
|
+
if (typeof v === "number" || typeof v === "string")
|
|
47
|
+
return v;
|
|
48
|
+
return JSON.stringify(v);
|
|
49
|
+
}
|
|
50
|
+
/** coerce turns a filter value into a value comparable with sample, as the server coerces to the field's type. */
|
|
51
|
+
function coerce(field, sample, v) {
|
|
52
|
+
if (SYSTEM_FIELDS.has(field))
|
|
53
|
+
return v instanceof Date ? v.getTime() : Date.parse(String(v));
|
|
54
|
+
if (typeof sample === "number") {
|
|
55
|
+
if (typeof v === "number")
|
|
56
|
+
return v;
|
|
57
|
+
if (typeof v === "boolean")
|
|
58
|
+
return v ? 1 : 0;
|
|
59
|
+
const s = String(v);
|
|
60
|
+
if (s === "true")
|
|
61
|
+
return 1;
|
|
62
|
+
if (s === "false")
|
|
63
|
+
return 0;
|
|
64
|
+
const n = Number(s);
|
|
65
|
+
return s.trim() !== "" && Number.isFinite(n) ? n : s;
|
|
66
|
+
}
|
|
67
|
+
return v instanceof Date ? v.toISOString() : String(v);
|
|
68
|
+
}
|
|
69
|
+
/** compareValues orders numbers before strings, strings by code point (SQLite's byte order). */
|
|
70
|
+
function compareValues(a, b) {
|
|
71
|
+
if (typeof a !== typeof b)
|
|
72
|
+
return typeof a === "number" ? -1 : 1;
|
|
73
|
+
if (typeof a === "number")
|
|
74
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
75
|
+
const s = a;
|
|
76
|
+
const t = b;
|
|
77
|
+
if (s === t)
|
|
78
|
+
return 0;
|
|
79
|
+
for (let i = 0; i < s.length && i < t.length;) {
|
|
80
|
+
const x = s.codePointAt(i);
|
|
81
|
+
const y = t.codePointAt(i);
|
|
82
|
+
if (x !== y)
|
|
83
|
+
return x - y;
|
|
84
|
+
i += x > 0xffff ? 2 : 1;
|
|
85
|
+
}
|
|
86
|
+
return s.length - t.length;
|
|
87
|
+
}
|
|
88
|
+
function inValues(v) {
|
|
89
|
+
if (Array.isArray(v))
|
|
90
|
+
return v;
|
|
91
|
+
if (typeof v === "string")
|
|
92
|
+
return v === "" ? [] : v.split(",");
|
|
93
|
+
return [v];
|
|
94
|
+
}
|
|
95
|
+
function matches(r, filters) {
|
|
96
|
+
for (const f of filters) {
|
|
97
|
+
const v = fieldValue(r, f.field);
|
|
98
|
+
if (v === undefined)
|
|
99
|
+
return false;
|
|
100
|
+
if (f.op === "in") {
|
|
101
|
+
if (!inValues(f.value).some((x) => compareValues(v, coerce(f.field, v, x)) === 0))
|
|
102
|
+
return false;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const d = compareValues(v, coerce(f.field, v, f.value));
|
|
106
|
+
const ok = f.op === "==" ? d === 0 : f.op === "!=" ? d !== 0 : f.op === "<" ? d < 0 : f.op === "<=" ? d <= 0 : f.op === ">" ? d > 0 : d >= 0;
|
|
107
|
+
if (!ok)
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* comparator orders by the order field (createdAt by default), then by
|
|
114
|
+
* record key, both in the order's direction; records lacking the field come
|
|
115
|
+
* last in both directions.
|
|
116
|
+
*/
|
|
117
|
+
function comparator(order) {
|
|
118
|
+
const field = order?.field ?? "createdAt";
|
|
119
|
+
const sign = order?.desc ? -1 : 1;
|
|
120
|
+
return (a, b) => {
|
|
121
|
+
const va = fieldValue(a, field);
|
|
122
|
+
const vb = fieldValue(b, field);
|
|
123
|
+
if (va === undefined || vb === undefined) {
|
|
124
|
+
if (va !== vb)
|
|
125
|
+
return va === undefined ? 1 : -1;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const d = compareValues(va, vb);
|
|
129
|
+
if (d !== 0)
|
|
130
|
+
return d * sign;
|
|
131
|
+
}
|
|
132
|
+
return (a.rkey < b.rkey ? -1 : a.rkey > b.rkey ? 1 : 0) * sign;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function wireValue(v) {
|
|
136
|
+
if (Array.isArray(v))
|
|
137
|
+
return v.map(wireValue).join(",");
|
|
138
|
+
if (v instanceof Date)
|
|
139
|
+
return v.toISOString();
|
|
140
|
+
return String(v);
|
|
141
|
+
}
|
|
142
|
+
// ---- collections ----
|
|
143
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
144
|
+
export class CollectionImpl {
|
|
145
|
+
ctx;
|
|
146
|
+
name;
|
|
147
|
+
opts;
|
|
148
|
+
constructor(ctx, name, opts) {
|
|
149
|
+
this.ctx = ctx;
|
|
150
|
+
this.name = name;
|
|
151
|
+
this.opts = opts;
|
|
152
|
+
}
|
|
153
|
+
major() {
|
|
154
|
+
return this.opts.schemaMajor ?? this.ctx.schemaMajors[this.name];
|
|
155
|
+
}
|
|
156
|
+
/** fresh brings the cache up to date from the server now, when it can (see Live.fresh). */
|
|
157
|
+
fresh() {
|
|
158
|
+
return this.ctx.live.fresh(this.name);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* visible is what this handle shows of an entry: its view, hidden when
|
|
162
|
+
* the handle declared versions and the server state is of another major
|
|
163
|
+
* (another handle on the same collection may read more majors).
|
|
164
|
+
*/
|
|
165
|
+
visible(e) {
|
|
166
|
+
const v = view(e, this.ctx.userId);
|
|
167
|
+
const versions = this.opts.versions;
|
|
168
|
+
if (v && versions && e.server && !versions.includes(e.server.schemaMajor))
|
|
169
|
+
return null;
|
|
170
|
+
return v;
|
|
171
|
+
}
|
|
172
|
+
/** enqueue applies op to the cache and queues it, in one transaction; check may refuse it first. */
|
|
173
|
+
async enqueue(op, check, base) {
|
|
174
|
+
const at = new Date().toISOString();
|
|
175
|
+
const e = await this.ctx.cache.write(async (w) => {
|
|
176
|
+
const found = await w.entry(this.name, op.rkey);
|
|
177
|
+
check?.(found);
|
|
178
|
+
const entry = found ?? w.newEntry(this.name, op.rkey);
|
|
179
|
+
base?.(entry);
|
|
180
|
+
await w.enqueue(op, at);
|
|
181
|
+
await w.save(entry);
|
|
182
|
+
return entry;
|
|
183
|
+
});
|
|
184
|
+
this.ctx.flusher.pending++;
|
|
185
|
+
this.ctx.flusher.kick();
|
|
186
|
+
return e;
|
|
187
|
+
}
|
|
188
|
+
newOp(kind, rkey, data) {
|
|
189
|
+
const input = {};
|
|
190
|
+
const major = this.major();
|
|
191
|
+
if (major !== undefined)
|
|
192
|
+
input.schemaMajor = major;
|
|
193
|
+
if (data !== undefined)
|
|
194
|
+
input.data = data;
|
|
195
|
+
return { opId: uuidv7(), kind, collection: this.name, rkey, input };
|
|
196
|
+
}
|
|
197
|
+
async create(data) {
|
|
198
|
+
this.ctx.check();
|
|
199
|
+
if (!isObject(data))
|
|
200
|
+
throw new PbError("invalid_request", "create needs the record's fields as an object.", "Pass a plain object, e.g. create({ text: \"hi\" }).");
|
|
201
|
+
const op = this.newOp("create", uuidv7(), data);
|
|
202
|
+
return view(await this.enqueue(op), this.ctx.userId);
|
|
203
|
+
}
|
|
204
|
+
/** existing makes sure the cache knows rkey, then returns why it cannot be written, if so. */
|
|
205
|
+
async refuse(rkey) {
|
|
206
|
+
if (!view(await this.ctx.cache.entry(this.name, rkey), this.ctx.userId))
|
|
207
|
+
await this.fresh();
|
|
208
|
+
return (e) => {
|
|
209
|
+
if (view(e, this.ctx.userId))
|
|
210
|
+
return;
|
|
211
|
+
if (e?.deleted || e?.local?.data === null) {
|
|
212
|
+
throw new PbError("record_deleted", `Record ${rkey} in ${this.name} was deleted.`, "Create a new record instead; keys of deleted records cannot be reused.");
|
|
213
|
+
}
|
|
214
|
+
throw new PbError("not_found", `There is no record ${rkey} in ${this.name}.`, "Check the record key (rkey) and the collection name.");
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
async update(rkey, patch) {
|
|
218
|
+
this.ctx.check();
|
|
219
|
+
if (!isObject(patch))
|
|
220
|
+
throw new PbError("invalid_request", "update needs the fields to change as an object.", "Pass a plain object; null removes a field.");
|
|
221
|
+
const check = await this.refuse(rkey);
|
|
222
|
+
const op = this.newOp("update", rkey, patch);
|
|
223
|
+
const e = await this.enqueue(op, check, (entry) => {
|
|
224
|
+
if (entry.server)
|
|
225
|
+
op.input.baseRev = entry.server.rev;
|
|
226
|
+
});
|
|
227
|
+
return view(e, this.ctx.userId);
|
|
228
|
+
}
|
|
229
|
+
async delete(rkey) {
|
|
230
|
+
this.ctx.check();
|
|
231
|
+
const e = await this.ctx.cache.entry(this.name, rkey);
|
|
232
|
+
if (e && (e.deleted || e.local?.data === null))
|
|
233
|
+
return; // already deleted
|
|
234
|
+
const check = await this.refuse(rkey);
|
|
235
|
+
await this.enqueue(this.newOp("delete", rkey), check);
|
|
236
|
+
}
|
|
237
|
+
/** get reads the cache at once; the collection is caught up in the background (Live.ensure). */
|
|
238
|
+
async get(rkey) {
|
|
239
|
+
this.ctx.check();
|
|
240
|
+
this.ctx.live.ensure(this.name);
|
|
241
|
+
return this.visible(await this.ctx.cache.entry(this.name, rkey));
|
|
242
|
+
}
|
|
243
|
+
/** list reads the whole collection from the cache at once, oldest first. */
|
|
244
|
+
async list() {
|
|
245
|
+
this.ctx.check();
|
|
246
|
+
this.ctx.live.ensure(this.name);
|
|
247
|
+
return new QueryImpl(this).local(Infinity);
|
|
248
|
+
}
|
|
249
|
+
where(field, op, value) {
|
|
250
|
+
return new QueryImpl(this).where(field, op, value);
|
|
251
|
+
}
|
|
252
|
+
orderBy(field, dir = "asc") {
|
|
253
|
+
return new QueryImpl(this).orderBy(field, dir);
|
|
254
|
+
}
|
|
255
|
+
/** subscribe follows the whole collection (like list(), no default limit). */
|
|
256
|
+
subscribe(cb) {
|
|
257
|
+
return new QueryImpl(this, [], undefined, Infinity).subscribe(cb);
|
|
258
|
+
}
|
|
259
|
+
requestAccess() {
|
|
260
|
+
// One consent popup at a time: a second call for the same collection
|
|
261
|
+
// shares the first; one for another collection would reuse its window.
|
|
262
|
+
const inFlight = this.ctx.consent.current;
|
|
263
|
+
if (inFlight) {
|
|
264
|
+
if (inFlight.collection === this.name)
|
|
265
|
+
return inFlight.promise;
|
|
266
|
+
return Promise.reject(new PbError("invalid_request", `Another consent request is open (for ${inFlight.collection}).`, "Wait until the open consent request resolves, then call requestAccess() again from a click."));
|
|
267
|
+
}
|
|
268
|
+
// No await before window.open: it must run inside the user's gesture.
|
|
269
|
+
let popup = null;
|
|
270
|
+
try {
|
|
271
|
+
this.ctx.check();
|
|
272
|
+
popup = window.open("", CONSENT_POPUP_NAME, POPUP_FEATURES);
|
|
273
|
+
}
|
|
274
|
+
catch (e) {
|
|
275
|
+
return Promise.reject(asPbError(e));
|
|
276
|
+
}
|
|
277
|
+
if (!popup || popup.closed) {
|
|
278
|
+
return Promise.reject(new PbError("popup_blocked", `The browser blocked the ${BRAND} consent popup.`, "Call requestAccess() directly from a click or key handler, not on page load or after an await, so the browser allows the popup."));
|
|
279
|
+
}
|
|
280
|
+
const promise = this.askConsent(popup);
|
|
281
|
+
const current = { collection: this.name, promise };
|
|
282
|
+
this.ctx.consent.current = current;
|
|
283
|
+
const clear = () => {
|
|
284
|
+
if (this.ctx.consent.current === current)
|
|
285
|
+
this.ctx.consent.current = null;
|
|
286
|
+
};
|
|
287
|
+
promise.then(clear, clear);
|
|
288
|
+
return promise;
|
|
289
|
+
}
|
|
290
|
+
async askConsent(popup) {
|
|
291
|
+
try {
|
|
292
|
+
const body = { collection: this.name, permission: "read" };
|
|
293
|
+
if (this.opts.versions)
|
|
294
|
+
body.versions = this.opts.versions;
|
|
295
|
+
const res = await this.ctx.net.request("POST", "/v1/consent", body);
|
|
296
|
+
if (typeof res?.consentUrl !== "string") {
|
|
297
|
+
throw new PbError("invalid_response", `${BRAND} returned no consent page.`, "Check that baseUrl points at the server.");
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
popup.location.href = res.consentUrl;
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
throw new PbError("popup_blocked", `The ${BRAND} consent popup could not be opened.`, "Close other consent windows and call requestAccess() again from a click.");
|
|
304
|
+
}
|
|
305
|
+
const granted = await waitForConsent(popup, this.ctx.origin);
|
|
306
|
+
if (granted)
|
|
307
|
+
this.ctx.live.granted(this.name);
|
|
308
|
+
return granted;
|
|
309
|
+
}
|
|
310
|
+
catch (e) {
|
|
311
|
+
popup.close();
|
|
312
|
+
throw asPbError(e);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const CONSENT_POPUP_NAME = `${BRAND}-consent`;
|
|
317
|
+
const POPUP_FEATURES = "popup,width=480,height=720";
|
|
318
|
+
const POPUP_POLL_MS = 500;
|
|
319
|
+
/** The message type the consent result page posts (server/internal/web/consent.go). */
|
|
320
|
+
const CONSENT_MESSAGE_TYPE = "pb:consent";
|
|
321
|
+
/**
|
|
322
|
+
* waitForConsent resolves with granted from the first message that comes
|
|
323
|
+
* from popup, from origin and of the consent type; every other message is
|
|
324
|
+
* ignored. A popup closed first (seen closed on two polls in a row, so a
|
|
325
|
+
* result page that posts and closes at once is still heard) resolves false.
|
|
326
|
+
*/
|
|
327
|
+
function waitForConsent(popup, origin) {
|
|
328
|
+
return new Promise((resolve) => {
|
|
329
|
+
let closedPolls = 0;
|
|
330
|
+
const onMessage = (ev) => {
|
|
331
|
+
if (ev.origin !== origin || ev.source !== popup)
|
|
332
|
+
return;
|
|
333
|
+
const d = ev.data;
|
|
334
|
+
if (typeof d !== "object" || d === null || d.type !== CONSENT_MESSAGE_TYPE)
|
|
335
|
+
return;
|
|
336
|
+
cleanup();
|
|
337
|
+
resolve(d.granted === true);
|
|
338
|
+
};
|
|
339
|
+
const timer = setInterval(() => {
|
|
340
|
+
if (!popup.closed) {
|
|
341
|
+
closedPolls = 0;
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (++closedPolls < 2)
|
|
345
|
+
return;
|
|
346
|
+
cleanup();
|
|
347
|
+
resolve(false);
|
|
348
|
+
}, POPUP_POLL_MS);
|
|
349
|
+
const cleanup = () => {
|
|
350
|
+
window.removeEventListener("message", onMessage);
|
|
351
|
+
clearInterval(timer);
|
|
352
|
+
};
|
|
353
|
+
window.addEventListener("message", onMessage);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
// ---- queries ----
|
|
357
|
+
export class QueryImpl {
|
|
358
|
+
coll;
|
|
359
|
+
filters;
|
|
360
|
+
order;
|
|
361
|
+
max;
|
|
362
|
+
constructor(coll, filters = [], order, max) {
|
|
363
|
+
this.coll = coll;
|
|
364
|
+
this.filters = filters;
|
|
365
|
+
this.order = order;
|
|
366
|
+
this.max = max;
|
|
367
|
+
}
|
|
368
|
+
where(field, op, value) {
|
|
369
|
+
if (!(op in WIRE_OPS)) {
|
|
370
|
+
throw new PbError("invalid_request", `Unknown operator ${String(op)}.`, 'Use one of "==", "!=", "<", "<=", ">", ">=", "in".');
|
|
371
|
+
}
|
|
372
|
+
return new QueryImpl(this.coll, [...this.filters, { field, op, value }], this.order, this.max);
|
|
373
|
+
}
|
|
374
|
+
orderBy(field, dir = "asc") {
|
|
375
|
+
return new QueryImpl(this.coll, this.filters, { field, desc: dir === "desc" }, this.max);
|
|
376
|
+
}
|
|
377
|
+
limit(n) {
|
|
378
|
+
if (!Number.isInteger(n) || n < 1)
|
|
379
|
+
throw new PbError("invalid_request", `limit(${n}) is not valid.`, "Pass a whole number of at least 1.");
|
|
380
|
+
return new QueryImpl(this.coll, this.filters, this.order, n);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* get runs the query on the server when the device is online (all pages
|
|
384
|
+
* up to the limit, merged into the cache) and on the cache when it is
|
|
385
|
+
* offline or the server cannot be reached. Queued local writes are
|
|
386
|
+
* reflected either way.
|
|
387
|
+
*/
|
|
388
|
+
async get() {
|
|
389
|
+
const ctx = this.coll.ctx;
|
|
390
|
+
ctx.check();
|
|
391
|
+
if (ctx.net.deviceOnline) {
|
|
392
|
+
try {
|
|
393
|
+
const out = await this.remote();
|
|
394
|
+
ctx.live.readable(this.coll.name);
|
|
395
|
+
return out;
|
|
396
|
+
}
|
|
397
|
+
catch (e) {
|
|
398
|
+
const err = asPbError(e);
|
|
399
|
+
if (err.code === "consent_required")
|
|
400
|
+
ctx.live.unreadable([this.coll.name]);
|
|
401
|
+
if (err.code !== "network")
|
|
402
|
+
throw err;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return this.local();
|
|
406
|
+
}
|
|
407
|
+
params() {
|
|
408
|
+
const q = new URLSearchParams();
|
|
409
|
+
for (const f of this.filters)
|
|
410
|
+
q.append("where", `${f.field}:${WIRE_OPS[f.op]}:${wireValue(f.value)}`);
|
|
411
|
+
if (this.order)
|
|
412
|
+
q.set("order", `${this.order.field}:${this.order.desc ? "desc" : "asc"}`);
|
|
413
|
+
if (this.coll.opts.versions)
|
|
414
|
+
q.set("versions", this.coll.opts.versions.join(","));
|
|
415
|
+
return q;
|
|
416
|
+
}
|
|
417
|
+
async remote() {
|
|
418
|
+
const { ctx, name } = this.coll;
|
|
419
|
+
const max = this.max ?? DEFAULT_QUERY_LIMIT;
|
|
420
|
+
const got = [];
|
|
421
|
+
let cursor = "";
|
|
422
|
+
do {
|
|
423
|
+
const q = this.params();
|
|
424
|
+
q.set("limit", String(max - got.length));
|
|
425
|
+
if (cursor)
|
|
426
|
+
q.set("cursor", cursor);
|
|
427
|
+
const page = await ctx.net.request("GET", `/v1/records/${encodeURIComponent(name)}?${q}`);
|
|
428
|
+
got.push(...(page?.records ?? []));
|
|
429
|
+
cursor = page?.nextCursor ?? "";
|
|
430
|
+
} while (cursor && got.length < max);
|
|
431
|
+
const records = got.slice(0, max);
|
|
432
|
+
await ctx.cache.write(async (w) => {
|
|
433
|
+
for (const r of records)
|
|
434
|
+
await w.applyServer(name, r.rkey, r.rev, r, this.coll.opts.versions);
|
|
435
|
+
});
|
|
436
|
+
// The server's answer, with this device's queued writes applied: a
|
|
437
|
+
// record with a queued write is re-checked locally, and records only
|
|
438
|
+
// written locally are added when they match.
|
|
439
|
+
const entries = await ctx.cache.entries(name);
|
|
440
|
+
const byKey = new Map(entries.map((e) => [e.rkey, e]));
|
|
441
|
+
const fromServer = new Set(records.map((r) => r.rkey));
|
|
442
|
+
const out = [];
|
|
443
|
+
for (const rkey of fromServer) {
|
|
444
|
+
const e = byKey.get(rkey);
|
|
445
|
+
const v = this.coll.visible(e);
|
|
446
|
+
if (v && (!e.local || matches(v, this.filters)))
|
|
447
|
+
out.push(v);
|
|
448
|
+
}
|
|
449
|
+
for (const e of entries) {
|
|
450
|
+
if (!e.local || fromServer.has(e.rkey))
|
|
451
|
+
continue;
|
|
452
|
+
const v = this.coll.visible(e);
|
|
453
|
+
if (v && matches(v, this.filters))
|
|
454
|
+
out.push(v);
|
|
455
|
+
}
|
|
456
|
+
return this.finish(out, max);
|
|
457
|
+
}
|
|
458
|
+
/** local evaluates the query over the cache, cut at max (the query's limit by default). */
|
|
459
|
+
async local(max = this.max ?? DEFAULT_QUERY_LIMIT) {
|
|
460
|
+
const { ctx, name } = this.coll;
|
|
461
|
+
const out = [];
|
|
462
|
+
for (const e of await ctx.cache.entries(name)) {
|
|
463
|
+
const v = this.coll.visible(e);
|
|
464
|
+
if (v && matches(v, this.filters))
|
|
465
|
+
out.push(v);
|
|
466
|
+
}
|
|
467
|
+
return this.finish(out, max);
|
|
468
|
+
}
|
|
469
|
+
finish(records, max) {
|
|
470
|
+
records.sort(comparator(this.order));
|
|
471
|
+
return records.slice(0, max);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* subscribe calls cb with the query's result over the cache now and again
|
|
475
|
+
* after every change to the collection's cached records: local writes,
|
|
476
|
+
* queue results, live changes and catch-up. While subscribed the cache is
|
|
477
|
+
* kept current by the live connection.
|
|
478
|
+
*/
|
|
479
|
+
subscribe(cb) {
|
|
480
|
+
const { ctx, name } = this.coll;
|
|
481
|
+
ctx.check();
|
|
482
|
+
let active = true;
|
|
483
|
+
let run = 0;
|
|
484
|
+
const update = async () => {
|
|
485
|
+
const mine = ++run;
|
|
486
|
+
try {
|
|
487
|
+
const records = await this.local();
|
|
488
|
+
if (active && mine === run)
|
|
489
|
+
cb(records);
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
// The cache was closed (signed out); nothing to report.
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
const off = ctx.bus.on(name, () => void update());
|
|
496
|
+
const unwatch = ctx.live.watch(name);
|
|
497
|
+
void update();
|
|
498
|
+
return () => {
|
|
499
|
+
if (!active)
|
|
500
|
+
return;
|
|
501
|
+
active = false;
|
|
502
|
+
off();
|
|
503
|
+
unwatch();
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/** signedOut is the error for any use of a backend after signOut(). */
|
|
508
|
+
export function signedOut() {
|
|
509
|
+
return new PbError("unauthorized", `Signed out of ${BRAND}.`, SIGN_IN_HINT);
|
|
510
|
+
}
|
|
511
|
+
//# sourceMappingURL=collection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.js","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,2EAA2E;AAC3E,2EAA2E;AAC3E,sCAAsC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAgD,MAAM,YAAY,CAAC;AACxF,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGpD,OAAO,EAAE,SAAS,EAA0B,MAAM,YAAY,CAAC;AAE/D,8EAA8E;AAC9E,MAAM,OAAO,GAAG;IACG,SAAS,GAAG,IAAI,GAAG,EAA2B,CAAC;IAEhE,EAAE,CAAC,UAAkB,EAAE,EAAc;QACnC,IAAI,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACzC,IAAI,CAAC,GAAG;YAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC;QAC5D,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K,GAAG;oBAAE,EAAE,CAAC,OAAO,CAAC,CAAC;YAC1C,CAAC;YAAC,MAAM,CAAC;gBACP,wDAAwD;YAC1D,CAAC;QACH,CAAC,CAAC;QACF,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,KAAK,MAAM,EAAE,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACrC,KAAK,MAAM,EAAE,CAAC;QACd,OAAO,GAAG,EAAE;YACV,IAAI,CAAC,MAAM;gBAAE,OAAO;YACpB,MAAM,GAAG,KAAK,CAAC;YACf,GAAG,EAAE,CAAC;YACN,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC;IACJ,CAAC;CACF;AAED,uEAAuE;AACvE,MAAM,UAAU,SAAS;IACvB,OAAO,IAAI,OAAO,CAAC,cAAc,EAAE,iBAAiB,KAAK,GAAG,EAAE,YAAY,CAAC,CAAC;AAC9E,CAAC"}
|
package/dist/dpop.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type DBSchema, type IDBPDatabase } from "idb";
|
|
2
|
+
/**
|
|
3
|
+
* The SDK's IndexedDB database "pb":
|
|
4
|
+
* - keys: the DPoP key pair (CryptoKey objects) per server origin.
|
|
5
|
+
* - tokens: the refresh token and user ID per server origin and app.
|
|
6
|
+
*/
|
|
7
|
+
export interface PbDB extends DBSchema {
|
|
8
|
+
keys: {
|
|
9
|
+
key: string;
|
|
10
|
+
value: CryptoKeyPair;
|
|
11
|
+
};
|
|
12
|
+
tokens: {
|
|
13
|
+
key: string;
|
|
14
|
+
value: StoredTokens;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface StoredTokens {
|
|
18
|
+
refreshToken: string;
|
|
19
|
+
userId: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* openPbDB opens the SDK's database. Callers close it when done: a
|
|
23
|
+
* connection is opened per operation so no handle outlives the page's
|
|
24
|
+
* use of it.
|
|
25
|
+
*/
|
|
26
|
+
export declare function openPbDB(): Promise<IDBPDatabase<PbDB>>;
|
|
27
|
+
export declare function b64url(bytes: ArrayBuffer | Uint8Array): string;
|
|
28
|
+
/** randomToken returns n random bytes, base64url without padding. */
|
|
29
|
+
export declare function randomToken(n?: number): string;
|
|
30
|
+
export declare function sha256(s: string): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* getOrCreateKey returns this browser's DPoP key pair for the server at
|
|
33
|
+
* baseUrl (one key per server origin, shared by every app on it), creating
|
|
34
|
+
* and storing it on first use. The private key is generated with
|
|
35
|
+
* extractable false: it can sign but can never be read, so a stolen
|
|
36
|
+
* IndexedDB copy or XSS cannot take it off the device.
|
|
37
|
+
*
|
|
38
|
+
* Safe when several tabs call it at once: the "is there a key, else store
|
|
39
|
+
* mine" step is one readwrite transaction, which IndexedDB serialises, so
|
|
40
|
+
* every caller ends up with the first key stored.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getOrCreateKey(baseUrl?: string): Promise<CryptoKeyPair>;
|
|
43
|
+
/**
|
|
44
|
+
* createProof returns a DPoP proof (compact JWS, ES256) for one request:
|
|
45
|
+
* header {typ "dpop+jwt", alg "ES256", jwk}, claims htm (upper-case
|
|
46
|
+
* method), htu (url without query and fragment), iat (seconds, the device
|
|
47
|
+
* clock plus clockOffsetMs), a random jti and, when accessToken is given,
|
|
48
|
+
* ath (base64url SHA-256 of the token).
|
|
49
|
+
*/
|
|
50
|
+
export declare function createProof(keys: CryptoKeyPair, method: string, url: string, accessToken?: string, clockOffsetMs?: number): Promise<string>;
|