@drupflare/spool 0.2.0 → 0.4.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.
Files changed (118) hide show
  1. package/README.md +499 -43
  2. package/dist/cdn/client.js +1 -1
  3. package/dist/cdn/manifest.json +7 -7
  4. package/dist/cdn/spool-sw.js +1 -1
  5. package/dist/client/activity.d.ts +37 -0
  6. package/dist/client/activity.js +114 -0
  7. package/dist/client/entry.js +34 -1
  8. package/dist/client/index.d.ts +1 -0
  9. package/dist/client/index.js +1 -0
  10. package/dist/client/register.d.ts +23 -1
  11. package/dist/client/register.js +53 -7
  12. package/dist/media/embeds.d.ts +3 -2
  13. package/dist/media/embeds.js +16 -1
  14. package/dist/protocol/config.d.ts +16 -1
  15. package/dist/protocol/config.js +3 -1
  16. package/dist/protocol/dataset.d.ts +183 -0
  17. package/dist/protocol/dataset.js +206 -0
  18. package/dist/protocol/envelope.d.ts +3 -0
  19. package/dist/protocol/envelope.js +14 -3
  20. package/dist/protocol/errors.d.ts +1 -1
  21. package/dist/protocol/graph.d.ts +23 -4
  22. package/dist/protocol/graph.js +34 -3
  23. package/dist/protocol/hash.d.ts +2 -0
  24. package/dist/protocol/hash.js +8 -0
  25. package/dist/protocol/index.d.ts +7 -0
  26. package/dist/protocol/index.js +7 -0
  27. package/dist/protocol/inline.d.ts +14 -0
  28. package/dist/protocol/inline.js +43 -0
  29. package/dist/protocol/navigation.d.ts +81 -0
  30. package/dist/protocol/navigation.js +249 -0
  31. package/dist/protocol/normalise.d.ts +4 -3
  32. package/dist/protocol/normalise.js +6 -4
  33. package/dist/protocol/plan.d.ts +100 -0
  34. package/dist/protocol/plan.js +661 -0
  35. package/dist/protocol/provision.d.ts +52 -1
  36. package/dist/protocol/provision.js +53 -5
  37. package/dist/protocol/query.d.ts +141 -0
  38. package/dist/protocol/query.js +252 -0
  39. package/dist/protocol/route.d.ts +85 -5
  40. package/dist/protocol/route.js +91 -0
  41. package/dist/protocol/write.d.ts +146 -0
  42. package/dist/protocol/write.js +101 -0
  43. package/dist/proxy/bases.d.ts +53 -0
  44. package/dist/proxy/bases.js +76 -0
  45. package/dist/proxy/index.d.ts +1 -0
  46. package/dist/proxy/index.js +1 -0
  47. package/dist/proxy/proxy.js +8 -2
  48. package/dist/report/capacity.d.ts +59 -0
  49. package/dist/report/capacity.js +76 -0
  50. package/dist/report/channels.d.ts +60 -0
  51. package/dist/report/channels.js +65 -0
  52. package/dist/report/dataset.d.ts +48 -0
  53. package/dist/report/dataset.js +93 -0
  54. package/dist/report/derive.d.ts +285 -0
  55. package/dist/report/derive.js +676 -1
  56. package/dist/report/economics.d.ts +11 -1
  57. package/dist/report/economics.js +19 -1
  58. package/dist/report/index.d.ts +5 -0
  59. package/dist/report/index.js +5 -0
  60. package/dist/report/inputs.d.ts +97 -0
  61. package/dist/report/inputs.js +133 -0
  62. package/dist/report/institution.d.ts +284 -0
  63. package/dist/report/institution.js +225 -0
  64. package/dist/report/ledger.d.ts +28 -0
  65. package/dist/report/ledger.js +30 -0
  66. package/dist/report/loops.d.ts +73 -0
  67. package/dist/report/loops.js +101 -0
  68. package/dist/report/measurement.d.ts +1 -1
  69. package/dist/report/measurement.js +6 -1
  70. package/dist/report/model.d.ts +2 -0
  71. package/dist/report/model.js +4 -0
  72. package/dist/report/thirdparty.d.ts +57 -0
  73. package/dist/report/thirdparty.js +111 -0
  74. package/dist/report/traffic.d.ts +11 -2
  75. package/dist/report/traffic.js +13 -3
  76. package/dist/report/trim.d.ts +56 -0
  77. package/dist/report/trim.js +83 -1
  78. package/dist/server/dataset.d.ts +48 -0
  79. package/dist/server/dataset.js +308 -0
  80. package/dist/server/embedded.d.ts +3 -3
  81. package/dist/server/embedded.js +3 -3
  82. package/dist/server/index.d.ts +3 -0
  83. package/dist/server/index.js +3 -0
  84. package/dist/server/spool.d.ts +40 -0
  85. package/dist/server/spool.js +184 -23
  86. package/dist/server/write.d.ts +27 -0
  87. package/dist/server/write.js +127 -0
  88. package/dist/sw/analytics.d.ts +6 -0
  89. package/dist/sw/analytics.js +14 -0
  90. package/dist/sw/dataset-sync.d.ts +76 -0
  91. package/dist/sw/dataset-sync.js +149 -0
  92. package/dist/sw/dataset.d.ts +91 -0
  93. package/dist/sw/dataset.js +318 -0
  94. package/dist/sw/graph/index.d.ts +31 -1
  95. package/dist/sw/graph/index.js +57 -5
  96. package/dist/sw/graph/invalidate.d.ts +15 -3
  97. package/dist/sw/graph/invalidate.js +42 -4
  98. package/dist/sw/idb.js +78 -9
  99. package/dist/sw/partition.d.ts +6 -1
  100. package/dist/sw/partition.js +21 -2
  101. package/dist/sw/principal-dataset.d.ts +64 -0
  102. package/dist/sw/principal-dataset.js +254 -0
  103. package/dist/sw/render.d.ts +8 -1
  104. package/dist/sw/render.js +28 -1
  105. package/dist/sw/replica.d.ts +14 -3
  106. package/dist/sw/replica.js +46 -5
  107. package/dist/sw/router.js +2 -2
  108. package/dist/sw/runtime.d.ts +43 -2
  109. package/dist/sw/runtime.js +440 -29
  110. package/dist/sw/seal.d.ts +13 -0
  111. package/dist/sw/seal.js +13 -4
  112. package/dist/sw/static.d.ts +18 -0
  113. package/dist/sw/static.js +84 -1
  114. package/dist/sw/storage.d.ts +51 -1
  115. package/dist/sw/storage.js +33 -1
  116. package/dist/sw/write.d.ts +107 -0
  117. package/dist/sw/write.js +442 -0
  118. package/package.json +5 -2
@@ -0,0 +1,254 @@
1
+ import { seal, unseal } from './seal.js';
2
+ const DB = 'spool-principal-datasets';
3
+ const ROWS = 'rows';
4
+ const METAS = 'meta';
5
+ const encoder = new TextEncoder();
6
+ function wrap(request) {
7
+ return new Promise((resolve, reject) => {
8
+ request.onsuccess = () => resolve(request.result);
9
+ request.onerror = () => reject(request.error);
10
+ });
11
+ }
12
+ function done(tx) {
13
+ return new Promise((resolve, reject) => {
14
+ tx.oncomplete = () => resolve();
15
+ tx.onerror = () => reject(tx.error);
16
+ tx.onabort = () => reject(tx.error);
17
+ });
18
+ }
19
+ const rangeOf = (partition) => IDBKeyRange.bound([partition], [partition, []]);
20
+ const rangeOfDataset = (partition, name) => IDBKeyRange.bound([partition, name], [partition, name, []]);
21
+ /**
22
+ * The sealed records of one partition in their own database, `spool-principal-datasets` at version
23
+ * 1 (the shared `spool` database is never raised, and `spool-datasets` holds the public rows only).
24
+ * Every record carries its partition as the first key part, so one partition's rows are never read
25
+ * for another and a logout deletes a key range. The database is created by the first call that
26
+ * reaches it.
27
+ */
28
+ export function idbPrincipalDatasets(factory, partition) {
29
+ let opened;
30
+ const open = () => (opened ??= new Promise((resolve, reject) => {
31
+ const request = factory.open(DB, 1);
32
+ request.onupgradeneeded = () => {
33
+ const db = request.result;
34
+ db.createObjectStore(ROWS, { keyPath: ['partition', 'name', 'slot'] });
35
+ db.createObjectStore(METAS, { keyPath: ['partition', 'name'] });
36
+ };
37
+ request.onsuccess = () => {
38
+ const db = request.result;
39
+ db.onversionchange = () => {
40
+ db.close();
41
+ opened = undefined;
42
+ };
43
+ resolve(db);
44
+ };
45
+ request.onerror = () => reject(request.error);
46
+ }));
47
+ const strip = (record) => {
48
+ const { partition: _partition, ...rest } = record;
49
+ return rest;
50
+ };
51
+ async function read(store, run) {
52
+ const db = await open();
53
+ const tx = db.transaction(store, 'readonly');
54
+ const finished = done(tx);
55
+ const result = await wrap(run(tx.objectStore(store)));
56
+ await finished;
57
+ return result;
58
+ }
59
+ return {
60
+ metas: async () => (await read(METAS, (s) => s.getAll(rangeOf(partition)))).map((record) => strip(record)),
61
+ meta: async (name) => {
62
+ const record = await read(METAS, (s) => s.get([partition, name]));
63
+ return record ? strip(record) : undefined;
64
+ },
65
+ rows: async (name) => (await read(ROWS, (s) => s.getAll(rangeOfDataset(partition, name)))).map((record) => strip(record)),
66
+ commit: async (commit) => {
67
+ const db = await open();
68
+ // strict durability: the commit is reported only after the engine verified the write
69
+ const tx = db.transaction([ROWS, METAS], 'readwrite', { durability: 'strict' });
70
+ const finished = done(tx);
71
+ // a rejected commit has aborted its transaction; the caller learns it from `finished`
72
+ finished.catch(() => undefined);
73
+ try {
74
+ const rows = tx.objectStore(ROWS);
75
+ const metas = tx.objectStore(METAS);
76
+ if (commit.clear)
77
+ rows.delete(rangeOfDataset(partition, commit.name));
78
+ for (const slot of commit.deletes)
79
+ rows.delete([partition, commit.name, slot]);
80
+ for (const put of commit.puts)
81
+ rows.put({ partition, ...put });
82
+ if (commit.meta)
83
+ metas.put({ partition, ...commit.meta });
84
+ else
85
+ metas.delete([partition, commit.name]);
86
+ }
87
+ catch (error) {
88
+ try {
89
+ tx.abort();
90
+ }
91
+ catch {
92
+ // already finished
93
+ }
94
+ throw error;
95
+ }
96
+ await finished;
97
+ },
98
+ clear: async () => {
99
+ const db = await open();
100
+ const tx = db.transaction([ROWS, METAS], 'readwrite');
101
+ const finished = done(tx);
102
+ tx.objectStore(ROWS).delete(rangeOf(partition));
103
+ tx.objectStore(METAS).delete(rangeOf(partition));
104
+ await finished;
105
+ }
106
+ };
107
+ }
108
+ /**
109
+ * Deletes every principal dataset record of `partition`. A database that was never created stays
110
+ * uncreated: the open is rolled back when it would have made one.
111
+ */
112
+ export async function destroyPrincipalDatasets(factory, partition) {
113
+ const db = await new Promise((resolve) => {
114
+ const request = factory.open(DB);
115
+ request.onupgradeneeded = () => {
116
+ try {
117
+ request.transaction?.abort();
118
+ }
119
+ catch {
120
+ // nothing was created
121
+ }
122
+ };
123
+ request.onsuccess = () => resolve(request.result);
124
+ request.onerror = () => resolve(null);
125
+ request.onblocked = () => resolve(null);
126
+ });
127
+ if (!db)
128
+ return;
129
+ try {
130
+ if (!db.objectStoreNames.contains(ROWS) || !db.objectStoreNames.contains(METAS))
131
+ return;
132
+ const tx = db.transaction([ROWS, METAS], 'readwrite');
133
+ const finished = done(tx);
134
+ tx.objectStore(ROWS).delete(rangeOf(partition));
135
+ tx.objectStore(METAS).delete(rangeOf(partition));
136
+ await finished;
137
+ }
138
+ catch {
139
+ // the records are sealed under a key that is shredded alongside
140
+ }
141
+ finally {
142
+ db.close();
143
+ }
144
+ }
145
+ function base64url(bytes) {
146
+ let binary = '';
147
+ for (const byte of bytes)
148
+ binary += String.fromCharCode(byte);
149
+ return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
150
+ }
151
+ /**
152
+ * Seals a partition's datasets. A row is `{ name, slot, sealed }` where the slot is SHA-256 of a
153
+ * per-dataset random salt, the dataset and the row key, so no key, field name or value sits in
154
+ * IndexedDB in the clear. The row record, the meta and the digest are AES-GCM ciphertext bound to
155
+ * the partition, dataset and slot as additional data: a record copied into another partition or
156
+ * slot does not open. The salt lives only inside the sealed meta, and a clearing commit draws a new
157
+ * one. A record whose key is gone reads as absent, and a meta that does not open clears its
158
+ * dataset. After `kill` every call does nothing, so a request that outlives a wipe writes nothing.
159
+ */
160
+ export function sealedDatasets(backend, options) {
161
+ const { partition, vault, retention } = options;
162
+ let dead = false;
163
+ const salts = new Map();
164
+ const aadMeta = (name) => `${partition}\u0000dataset-meta\u0000${name}`;
165
+ const aadRow = (name, slot) => `${partition}\u0000dataset-row\u0000${name}\u0000${slot}`;
166
+ async function sealWith(aad, value) {
167
+ const key = await vault.get(partition, retention, true);
168
+ if (!key)
169
+ throw new Error('no key');
170
+ return seal(key, aad, value, retention);
171
+ }
172
+ async function slotOf(name, key, salt) {
173
+ const bytes = encoder.encode(`${salt}\u0000${name}\u0000${key}`);
174
+ return base64url(new Uint8Array(await crypto.subtle.digest('SHA-256', bytes)));
175
+ }
176
+ async function open(record) {
177
+ const value = await unseal(vault, partition, aadMeta(record.name), record);
178
+ if (value) {
179
+ salts.set(record.name, value.salt);
180
+ return value;
181
+ }
182
+ salts.delete(record.name);
183
+ await backend.commit({ name: record.name, meta: null, clear: true, puts: [], deletes: [] });
184
+ return undefined;
185
+ }
186
+ async function saltOf(name) {
187
+ const known = salts.get(name);
188
+ if (known !== undefined)
189
+ return known;
190
+ const record = await backend.meta(name);
191
+ return record ? (await open(record))?.salt : undefined;
192
+ }
193
+ return {
194
+ kill: () => void (dead = true),
195
+ async getDatasets() {
196
+ if (dead)
197
+ return [];
198
+ const out = [];
199
+ for (const record of await backend.metas()) {
200
+ const value = await open(record);
201
+ if (value)
202
+ out.push(value.meta);
203
+ }
204
+ return out;
205
+ },
206
+ async readDataset(name) {
207
+ if (dead || (await saltOf(name)) === undefined)
208
+ return [];
209
+ const out = [];
210
+ for (const record of await backend.rows(name)) {
211
+ const value = await unseal(vault, partition, aadRow(name, record.slot), record);
212
+ if (value && value.name === name)
213
+ out.push(value);
214
+ }
215
+ return out.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
216
+ },
217
+ async commitDataset(commit) {
218
+ if (dead)
219
+ return;
220
+ const { name } = commit;
221
+ if (commit.meta === null) {
222
+ await backend.commit({ name, meta: null, clear: true, puts: [], deletes: [] });
223
+ salts.delete(name);
224
+ return;
225
+ }
226
+ let salt = commit.clear ? undefined : await saltOf(name);
227
+ const fresh = salt === undefined;
228
+ salt ??= base64url(crypto.getRandomValues(new Uint8Array(16)));
229
+ const puts = [];
230
+ for (const put of commit.puts) {
231
+ const slot = await slotOf(name, put.key, salt);
232
+ puts.push({ name, slot, ...(await sealWith(aadRow(name, slot), put)) });
233
+ }
234
+ const deletes = await Promise.all(commit.deletes.map((key) => slotOf(name, key, salt)));
235
+ const sealedMeta = await sealWith(aadMeta(name), { meta: commit.meta, salt });
236
+ if (dead)
237
+ return;
238
+ await backend.commit({
239
+ name,
240
+ meta: { name, ...sealedMeta },
241
+ clear: commit.clear || fresh,
242
+ puts,
243
+ deletes
244
+ });
245
+ salts.set(name, salt);
246
+ },
247
+ async clearDatasets() {
248
+ if (dead)
249
+ return;
250
+ await backend.clear();
251
+ salts.clear();
252
+ }
253
+ };
254
+ }
@@ -1,5 +1,7 @@
1
1
  import type { Artifact } from '../protocol/artifact.js';
2
2
  import type { SwConfig } from '../protocol/config.js';
3
+ import type { DatasetRow } from '../protocol/dataset.js';
4
+ import { type PlanTables } from '../protocol/plan.js';
3
5
  import type { SerializedRoute } from '../protocol/route.js';
4
6
  /** a custom renderer for one route pattern; it replaces the built-in one for that route */
5
7
  export type Renderer = (artifact: Artifact) => string | Promise<string>;
@@ -17,5 +19,10 @@ export type Rendered = {
17
19
  * Builds the HTML for a stored artifact, or the JSON text of a `data` one. The result carries the same client tag the server
18
20
  * injects, so it equals the network render after normalisation. Any failure (a missing template,
19
21
  * an unfilled placeholder, a renderer that throws) is a refusal, never a partial page.
22
+ * With `search` and a route that declares `query`, the template or renderer gets the page's
23
+ * items in place of the collection, after the dataset proof checks out. A route whose `query`
24
+ * names a `dataset` always takes this path, with the rows the device holds as `rows` in ascending
25
+ * key order; they were verified when the dataset loaded, so no proof is checked again.
26
+ * A route with a `plan` takes `tables` instead: the held base table and each joined one.
20
27
  */
21
- export declare function renderLocal(config: SwConfig, route: SerializedRoute, artifact: Artifact, renderers?: Renderers): Promise<Rendered>;
28
+ export declare function renderLocal(config: SwConfig, route: SerializedRoute, source: Artifact, renderers?: Renderers, search?: string, rows?: readonly DatasetRow[], tables?: PlanTables): Promise<Rendered>;
package/dist/sw/render.js CHANGED
@@ -1,13 +1,40 @@
1
1
  import { renderAssembly } from '../protocol/assembly.js';
2
+ import { shapePlan } from '../protocol/plan.js';
3
+ import { keyField, shapeQuery, shapeRows } from '../protocol/query.js';
2
4
  import { injectClientTag } from '../protocol/tag.js';
3
5
  import { interpretTemplate } from '../protocol/template/interpret.js';
6
+ const isPlain = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
4
7
  /**
5
8
  * Builds the HTML for a stored artifact, or the JSON text of a `data` one. The result carries the same client tag the server
6
9
  * injects, so it equals the network render after normalisation. Any failure (a missing template,
7
10
  * an unfilled placeholder, a renderer that throws) is a refusal, never a partial page.
11
+ * With `search` and a route that declares `query`, the template or renderer gets the page's
12
+ * items in place of the collection, after the dataset proof checks out. A route whose `query`
13
+ * names a `dataset` always takes this path, with the rows the device holds as `rows` in ascending
14
+ * key order; they were verified when the dataset loaded, so no proof is checked again.
15
+ * A route with a `plan` takes `tables` instead: the held base table and each joined one.
8
16
  */
9
- export async function renderLocal(config, route, artifact, renderers = {}) {
17
+ export async function renderLocal(config, route, source, renderers = {}, search, rows, tables) {
10
18
  try {
19
+ let artifact = source;
20
+ const query = route.query;
21
+ if (query &&
22
+ (query.plan !== undefined ||
23
+ rows !== undefined ||
24
+ (search !== undefined && search !== ''))) {
25
+ if (source.strategy === 'assembly' || source.strategy === 'static') {
26
+ return { ok: false, reason: `${source.strategy} artifact cannot answer a query` };
27
+ }
28
+ const data = source.body.data;
29
+ const shaped = query.plan
30
+ ? shapePlan(query.list, query.plan, data, search ?? '', tables)
31
+ : rows === undefined
32
+ ? await shapeQuery(query, data, search ?? '', keyField(query))
33
+ : shapeRows(query, { ...(isPlain(data) ? data : {}), [query.list]: rows }, search ?? '');
34
+ if (!shaped.ok)
35
+ return shaped;
36
+ artifact = { ...source, body: { data: shaped.data } };
37
+ }
11
38
  const custom = Object.hasOwn(renderers, route.pattern)
12
39
  ? renderers[route.pattern]
13
40
  : undefined;
@@ -1,6 +1,7 @@
1
1
  import type { Artifact } from '../protocol/artifact.js';
2
2
  import { type Envelope } from '../protocol/envelope.js';
3
3
  import { type SyncResponse } from '../protocol/sync.js';
4
+ import { DatasetStore, type DatasetStoreDeps } from './dataset.js';
4
5
  import { Graph, type Materialized } from './graph/index.js';
5
6
  import { type HeaderSet } from './policy.js';
6
7
  import type { ReplicaStorage, StoredArtifact } from './storage.js';
@@ -14,7 +15,9 @@ export declare class Replica {
14
15
  #private;
15
16
  /** records and projections; untouched until a page that references them is stored or shown */
16
17
  readonly graph: Graph;
17
- constructor(storage: ReplicaStorage, version: string, now?: () => number);
18
+ /** the datasets held, apart from the version vector: each has its own cursor */
19
+ readonly datasets: DatasetStore;
20
+ constructor(storage: ReplicaStorage, version: string, now?: () => number, hash?: DatasetStoreDeps['hash']);
18
21
  /** reads the persisted meta; a different runtime version wipes what the old one stored */
19
22
  load(): Promise<void>;
20
23
  /** the vector cursor the replica is current to, or null before the first artifact */
@@ -31,6 +34,8 @@ export declare class Replica {
31
34
  get(id: string): Promise<StoredArtifact | undefined>;
32
35
  /** every stored artifact id */
33
36
  ids(): Promise<string[]>;
37
+ /** counts the wipes so far; a write that began before one and ends after it can tell and drop itself */
38
+ get epoch(): number;
34
39
  /** whether no dependency of `artifact` has changed since it was built */
35
40
  isCurrent(artifact: Artifact): boolean;
36
41
  /** when `stored` was last known good */
@@ -46,6 +51,8 @@ export declare class Replica {
46
51
  needFor(artifact: Artifact, always?: boolean): Record<string, number> | null;
47
52
  /** the records and projections held, for an artifact request's `have`; loads the graph */
48
53
  held(): Promise<Record<string, number>>;
54
+ /** the datasets whose rows `artifact` references, directly or through projections; loads the graph when it has references */
55
+ datasetsOf(artifact: Artifact): Promise<string[]>;
49
56
  /** the artifact with its references resolved from the graph; loads the graph when it has any */
50
57
  materialize(artifact: Artifact): Promise<Materialized>;
51
58
  /**
@@ -64,8 +71,12 @@ export declare class Replica {
64
71
  sweep(need: Record<string, number>): Promise<Record<string, number> | null>;
65
72
  /** removes artifacts by id */
66
73
  drop(ids: readonly string[]): Promise<void>;
67
- /** empties the replica; `cursor` is where it restarts */
68
- wipe(cursor?: number | null): Promise<void>;
74
+ /**
75
+ * Empties the replica; `cursor` is where it restarts. Datasets go too unless `keepDatasets`: a
76
+ * vector reset says nothing about a dataset, whose own cursor and horizon decide when it reseeds.
77
+ * Either way an apply that began before the wipe is dropped.
78
+ */
79
+ wipe(cursor?: number | null, keepDatasets?: boolean): Promise<void>;
69
80
  /** applies a `/.spool/sync` answer and returns the ids of the artifacts it invalidated */
70
81
  applySync(response: SyncResponse): Promise<string[]>;
71
82
  /**
@@ -1,5 +1,6 @@
1
1
  import { CLEAR_ALL } from '../protocol/envelope.js';
2
2
  import { ARTIFACT_TAG_PREFIX, artifactTag, MAX_HAVE, MAX_NEED } from '../protocol/sync.js';
3
+ import { DatasetStore } from './dataset.js';
3
4
  import { isCurrent, validatedAt } from './freshness.js';
4
5
  import { Graph, usesGraph } from './graph/index.js';
5
6
  import { MAX_POLICY_PATHS } from './policy.js';
@@ -18,20 +19,41 @@ export class Replica {
18
19
  #tags = new Map();
19
20
  #cursor = null;
20
21
  #syncedAt = 0;
22
+ #epoch = 0;
21
23
  #site;
22
24
  #policies = new Map();
23
25
  /** records and projections; untouched until a page that references them is stored or shown */
24
26
  graph;
25
- constructor(storage, version, now = Date.now) {
27
+ /** the datasets held, apart from the version vector: each has its own cursor */
28
+ datasets;
29
+ constructor(storage, version, now = Date.now, hash) {
26
30
  this.#storage = storage;
27
31
  this.#version = version;
28
32
  this.#now = now;
33
+ this.datasets = new DatasetStore({
34
+ storage,
35
+ now,
36
+ epoch: () => this.#epoch,
37
+ ...(hash ? { hash } : {}),
38
+ onChange: (name, puts, deletes) => this.#rowsChanged(name, puts, deletes)
39
+ });
29
40
  this.graph = new Graph({
30
41
  storage,
31
42
  tagVersion: (tag) => this.#tags.get(tag) ?? 0,
32
- artifacts: () => this.#all()
43
+ artifacts: () => this.#all(),
44
+ row: (dataset, key) => this.datasets.row(dataset, key),
45
+ held: (dataset) => this.datasets.loaded(dataset),
46
+ loadDatasets: async (names) => void (await Promise.all(names.map((n) => this.datasets.get(n))))
33
47
  });
34
48
  }
49
+ /** repairs what references changed dataset rows: a deleted row drops the pages that used it */
50
+ async #rowsChanged(name, puts, deletes) {
51
+ if (!this.graph.loaded)
52
+ return;
53
+ const effect = await this.graph.rowsChanged(name, puts, deletes);
54
+ if (effect.dropped.artifacts.length > 0)
55
+ await this.drop(effect.dropped.artifacts);
56
+ }
35
57
  /** reads the persisted meta; a different runtime version wipes what the old one stored */
36
58
  async load() {
37
59
  const meta = await this.#storage.getMeta();
@@ -45,6 +67,7 @@ export class Replica {
45
67
  }
46
68
  await this.#storage.clearArtifacts();
47
69
  await this.graph.clear();
70
+ await this.datasets.clear();
48
71
  await this.#save();
49
72
  }
50
73
  /** the vector cursor the replica is current to, or null before the first artifact */
@@ -90,6 +113,10 @@ export class Replica {
90
113
  async ids() {
91
114
  return (await this.#all()).map((s) => s.artifact.id);
92
115
  }
116
+ /** counts the wipes so far; a write that began before one and ends after it can tell and drop itself */
117
+ get epoch() {
118
+ return this.#epoch;
119
+ }
93
120
  /** whether no dependency of `artifact` has changed since it was built */
94
121
  isCurrent(artifact) {
95
122
  return isCurrent(artifact, (tag) => this.#tags.get(tag) ?? 0);
@@ -128,6 +155,13 @@ export class Replica {
128
155
  await this.#wake();
129
156
  return this.graph.held(MAX_HAVE);
130
157
  }
158
+ /** the datasets whose rows `artifact` references, directly or through projections; loads the graph when it has references */
159
+ async datasetsOf(artifact) {
160
+ if (!usesGraph(artifact))
161
+ return [];
162
+ await this.#wake();
163
+ return this.graph.datasetsOf(artifact);
164
+ }
131
165
  /** the artifact with its references resolved from the graph; loads the graph when it has any */
132
166
  async materialize(artifact) {
133
167
  if (usesGraph(artifact))
@@ -206,11 +240,18 @@ export class Replica {
206
240
  if (this.graph.loaded)
207
241
  this.graph.forgetArtifacts(ids);
208
242
  }
209
- /** empties the replica; `cursor` is where it restarts */
210
- async wipe(cursor = null) {
243
+ /**
244
+ * Empties the replica; `cursor` is where it restarts. Datasets go too unless `keepDatasets`: a
245
+ * vector reset says nothing about a dataset, whose own cursor and horizon decide when it reseeds.
246
+ * Either way an apply that began before the wipe is dropped.
247
+ */
248
+ async wipe(cursor = null, keepDatasets = false) {
249
+ this.#epoch += 1;
211
250
  this.#memory.clear();
212
251
  await this.#storage.clearArtifacts();
213
252
  await this.graph.clear();
253
+ if (!keepDatasets)
254
+ await this.datasets.clear();
214
255
  this.#tags = new Map();
215
256
  this.#site = undefined;
216
257
  this.#policies = new Map();
@@ -221,7 +262,7 @@ export class Replica {
221
262
  async applySync(response) {
222
263
  if (response.reset) {
223
264
  const held = await this.ids();
224
- await this.wipe(response.cursor);
265
+ await this.wipe(response.cursor, true);
225
266
  this.#syncedAt = this.#now();
226
267
  await this.#save();
227
268
  return held;
package/dist/sw/router.js CHANGED
@@ -31,8 +31,8 @@ export function classify(config, request, origin) {
31
31
  if (request.method !== 'GET')
32
32
  return { kind: 'pass' };
33
33
  const hit = matchRoute(config.routes, url.pathname);
34
- // an artifact is keyed by path alone, so a query string can never be answered from one
35
- const queried = url.search !== '';
34
+ // an artifact is keyed by path alone, so only a route with a query schema answers a query
35
+ const queried = url.search !== '' && !hit?.route.query;
36
36
  if (request.mode !== 'navigate') {
37
37
  if (hit?.route.strategy === 'static' && !request.headers.has('range')) {
38
38
  return { kind: 'static', route: hit.route, path: url.pathname + url.search };
@@ -1,10 +1,12 @@
1
1
  import { type AssetMode, type SwConfig } from '../protocol/config.js';
2
+ import { type WriteFlush, type WriteQueued, type WriteRefusal, type WriteResolved, type WriteStatus } from '../protocol/write.js';
3
+ import { type DatasetRefusal } from './dataset-sync.js';
2
4
  import type { PartitionHost } from './partition.js';
3
5
  import { Ledger, type LedgerEntry, type LedgerTotals } from './provision.js';
4
6
  import { type Renderers } from './render.js';
5
7
  import { Replica } from './replica.js';
6
8
  import { type RoutableInstall } from './routing.js';
7
- import type { ReplicaStorage } from './storage.js';
9
+ import type { DatasetMeta, ReplicaStorage } from './storage.js';
8
10
  /** the slice of a `Cache` the runtime uses */
9
11
  export type CacheLike = {
10
12
  match(request: Request): Promise<Response | undefined>;
@@ -39,6 +41,11 @@ export type RuntimeDeps = {
39
41
  partitions?: PartitionHost;
40
42
  /** tells every page of the origin something, such as that a wipe happened */
41
43
  broadcast?(message: unknown): void;
44
+ /** the storage the origin may use and has used; default `navigator.storage.estimate` */
45
+ estimate?(): Promise<{
46
+ quota?: number;
47
+ usage?: number;
48
+ }>;
42
49
  };
43
50
  /** the worker's answer to a page's `spool:hello` */
44
51
  export type Hello = {
@@ -80,6 +87,8 @@ export type PartitionStatus = {
80
87
  artifacts: string[];
81
88
  records: string[];
82
89
  projections: string[];
90
+ /** the principal datasets held for this partition; always empty while the flag is off */
91
+ datasets: DatasetMeta[];
83
92
  };
84
93
  /** the worker's answer to `spool:assets`: the mode asked for and whether Static Routing took effect */
85
94
  export type AssetsStatus = {
@@ -93,8 +102,40 @@ export type HeldStatus = {
93
102
  dropped: Record<string, number>;
94
103
  third: string[];
95
104
  };
105
+ /**
106
+ * the worker's answer to `spool:datasets`: what it holds, how each sync ended (counts by
107
+ * `<name>:<status>` or `<name>:refused:<reason>`), the datasets it will not retry this run, the
108
+ * datasets it found damaged and cleared, and whether a page was granted persistent storage
109
+ */
110
+ export type DatasetStatus = {
111
+ type: 'spool:datasets';
112
+ datasets: DatasetMeta[];
113
+ outcomes: Record<string, number>;
114
+ refused: Record<string, DatasetRefusal>;
115
+ evicted: number;
116
+ persisted: boolean | null;
117
+ };
118
+ /** the worker's answer to `spool:write`, `spool:flush`, `spool:writes` and `spool:resolve` */
119
+ export type WriteReply = {
120
+ type: 'spool:write';
121
+ result: WriteQueued | WriteRefusal;
122
+ } | {
123
+ type: 'spool:flush';
124
+ result: ({
125
+ ok: true;
126
+ } & WriteFlush) | WriteRefusal;
127
+ } | {
128
+ type: 'spool:writes';
129
+ result: {
130
+ ok: true;
131
+ writes: WriteStatus[];
132
+ } | WriteRefusal;
133
+ } | {
134
+ type: 'spool:resolve';
135
+ result: WriteResolved | WriteRefusal;
136
+ };
96
137
  /** every reply the worker gives a page */
97
- export type Reply = Hello | Status | ViewId | GraphStatus | LedgerStatus | PartitionStatus | AssetsStatus | HeldStatus;
138
+ export type Reply = WriteReply | Hello | Status | ViewId | DatasetStatus | GraphStatus | LedgerStatus | PartitionStatus | AssetsStatus | HeldStatus;
98
139
  /** the object `serve` wires to service worker events */
99
140
  export type Runtime = {
100
141
  /** a response promise for a request the worker handles, `undefined` for one it leaves alone */