@drupflare/spool 0.3.0 → 1.0.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 (206) hide show
  1. package/README.md +279 -1607
  2. package/dist/angular/index.d.ts +13 -0
  3. package/dist/angular/index.js +25 -0
  4. package/dist/angular/pages.d.ts +28 -0
  5. package/dist/angular/pages.js +85 -0
  6. package/dist/cdn/client.js +1 -1
  7. package/dist/cdn/location.d.ts +6 -0
  8. package/dist/cdn/location.js +8 -0
  9. package/dist/cdn/manifest.json +7 -7
  10. package/dist/cdn/spool-sw.js +1 -1
  11. package/dist/client/bootstrap.d.ts +57 -0
  12. package/dist/client/bootstrap.js +307 -0
  13. package/dist/client/contact.d.ts +24 -0
  14. package/dist/client/contact.js +66 -0
  15. package/dist/client/declared.d.ts +23 -0
  16. package/dist/client/declared.js +68 -0
  17. package/dist/client/entry.js +123 -2
  18. package/dist/client/gate.d.ts +1 -0
  19. package/dist/client/gate.js +148 -0
  20. package/dist/client/index.d.ts +25 -0
  21. package/dist/client/index.js +1 -0
  22. package/dist/client/live.d.ts +66 -0
  23. package/dist/client/live.js +225 -0
  24. package/dist/client/navigate-entry.d.ts +1 -0
  25. package/dist/client/navigate-entry.js +4 -0
  26. package/dist/client/navigate.d.ts +54 -0
  27. package/dist/client/navigate.js +272 -0
  28. package/dist/client/persist.d.ts +2 -0
  29. package/dist/client/persist.js +9 -0
  30. package/dist/client/register.d.ts +34 -2
  31. package/dist/client/register.js +64 -7
  32. package/dist/express/index.d.ts +11 -0
  33. package/dist/express/index.js +209 -0
  34. package/dist/hono/index.d.ts +19 -0
  35. package/dist/hono/index.js +49 -0
  36. package/dist/next/index.d.ts +33 -0
  37. package/dist/next/index.js +41 -0
  38. package/dist/next/pages.d.ts +22 -0
  39. package/dist/next/pages.js +39 -0
  40. package/dist/node/listener.d.ts +3 -0
  41. package/dist/node/listener.js +57 -2
  42. package/dist/nuxt/index.d.ts +22 -0
  43. package/dist/nuxt/index.js +104 -0
  44. package/dist/nuxt/pages.d.ts +26 -0
  45. package/dist/nuxt/pages.js +48 -0
  46. package/dist/nuxt/runtime/index.d.ts +24 -0
  47. package/dist/nuxt/runtime/index.js +58 -0
  48. package/dist/plugins/coverage.d.ts +14 -0
  49. package/dist/plugins/coverage.js +27 -0
  50. package/dist/plugins/mode.d.ts +19 -0
  51. package/dist/plugins/mode.js +20 -0
  52. package/dist/plugins/path.d.ts +16 -0
  53. package/dist/plugins/path.js +139 -0
  54. package/dist/plugins/report.d.ts +22 -0
  55. package/dist/plugins/report.js +18 -0
  56. package/dist/plugins/routes.d.ts +38 -0
  57. package/dist/plugins/routes.js +52 -0
  58. package/dist/plugins/static.d.ts +42 -0
  59. package/dist/plugins/static.js +97 -0
  60. package/dist/protocol/bundle.d.ts +111 -0
  61. package/dist/protocol/bundle.js +428 -0
  62. package/dist/protocol/config.d.ts +80 -1
  63. package/dist/protocol/config.js +31 -1
  64. package/dist/protocol/dataset.d.ts +189 -0
  65. package/dist/protocol/dataset.js +212 -0
  66. package/dist/protocol/envelope.d.ts +34 -1
  67. package/dist/protocol/envelope.js +16 -1
  68. package/dist/protocol/errors.d.ts +1 -1
  69. package/dist/protocol/graph.d.ts +23 -4
  70. package/dist/protocol/graph.js +34 -3
  71. package/dist/protocol/index-rows.d.ts +22 -0
  72. package/dist/protocol/index-rows.js +53 -0
  73. package/dist/protocol/index.d.ts +5 -0
  74. package/dist/protocol/index.js +5 -0
  75. package/dist/protocol/normalise.d.ts +4 -4
  76. package/dist/protocol/normalise.js +9 -5
  77. package/dist/protocol/plan.d.ts +100 -0
  78. package/dist/protocol/plan.js +661 -0
  79. package/dist/protocol/provision.d.ts +46 -0
  80. package/dist/protocol/provision.js +38 -0
  81. package/dist/protocol/query.d.ts +66 -5
  82. package/dist/protocol/query.js +103 -4
  83. package/dist/protocol/ring.d.ts +58 -0
  84. package/dist/protocol/ring.js +6 -0
  85. package/dist/protocol/route.d.ts +63 -13
  86. package/dist/protocol/route.js +126 -6
  87. package/dist/protocol/tag.d.ts +22 -2
  88. package/dist/protocol/tag.js +25 -4
  89. package/dist/protocol/view.d.ts +2 -0
  90. package/dist/protocol/view.js +8 -2
  91. package/dist/protocol/views.d.ts +6 -0
  92. package/dist/protocol/views.js +15 -0
  93. package/dist/protocol/write.d.ts +234 -0
  94. package/dist/protocol/write.js +343 -0
  95. package/dist/proxy/eligible.d.ts +22 -1
  96. package/dist/proxy/eligible.js +62 -15
  97. package/dist/proxy/proxy.js +28 -18
  98. package/dist/react-router/index.d.ts +20 -0
  99. package/dist/react-router/index.js +60 -0
  100. package/dist/react-router/pages.d.ts +34 -0
  101. package/dist/react-router/pages.js +83 -0
  102. package/dist/report/dataset.d.ts +48 -0
  103. package/dist/report/dataset.js +93 -0
  104. package/dist/report/demand.d.ts +49 -0
  105. package/dist/report/demand.js +60 -0
  106. package/dist/report/derive.d.ts +285 -0
  107. package/dist/report/derive.js +676 -1
  108. package/dist/report/economics.d.ts +66 -1
  109. package/dist/report/economics.js +208 -1
  110. package/dist/report/headline.d.ts +158 -0
  111. package/dist/report/headline.js +473 -0
  112. package/dist/report/index.d.ts +4 -0
  113. package/dist/report/index.js +4 -0
  114. package/dist/report/inputs.d.ts +176 -0
  115. package/dist/report/inputs.js +248 -0
  116. package/dist/report/institution.d.ts +188 -1
  117. package/dist/report/institution.js +146 -0
  118. package/dist/report/loops.d.ts +73 -0
  119. package/dist/report/loops.js +101 -0
  120. package/dist/report/measurement.d.ts +1 -1
  121. package/dist/report/measurement.js +7 -1
  122. package/dist/report/model.d.ts +2 -0
  123. package/dist/report/model.js +4 -0
  124. package/dist/report/thirdparty.d.ts +57 -0
  125. package/dist/report/thirdparty.js +111 -0
  126. package/dist/report/traffic.d.ts +8 -0
  127. package/dist/report/traffic.js +7 -0
  128. package/dist/report/trim.d.ts +56 -0
  129. package/dist/report/trim.js +83 -1
  130. package/dist/server/bundle.d.ts +55 -0
  131. package/dist/server/bundle.js +172 -0
  132. package/dist/server/dataset.d.ts +54 -0
  133. package/dist/server/dataset.js +355 -0
  134. package/dist/server/embedded.d.ts +7 -3
  135. package/dist/server/embedded.js +7 -3
  136. package/dist/server/index.d.ts +6 -0
  137. package/dist/server/index.js +6 -0
  138. package/dist/server/live-sockets.d.ts +54 -0
  139. package/dist/server/live-sockets.js +131 -0
  140. package/dist/server/live.d.ts +146 -0
  141. package/dist/server/live.js +228 -0
  142. package/dist/server/ring.d.ts +77 -0
  143. package/dist/server/ring.js +311 -0
  144. package/dist/server/spool.d.ts +158 -3
  145. package/dist/server/spool.js +578 -41
  146. package/dist/server/static.d.ts +42 -0
  147. package/dist/server/static.js +29 -0
  148. package/dist/server/stub.d.ts +43 -5
  149. package/dist/server/stub.js +126 -8
  150. package/dist/server/write.d.ts +49 -0
  151. package/dist/server/write.js +223 -0
  152. package/dist/stores/durable-object.d.ts +46 -0
  153. package/dist/stores/durable-object.js +114 -0
  154. package/dist/sw/analytics.d.ts +7 -1
  155. package/dist/sw/analytics.js +16 -2
  156. package/dist/sw/coverage.d.ts +6 -0
  157. package/dist/sw/coverage.js +34 -0
  158. package/dist/sw/dataset-sync.d.ts +76 -0
  159. package/dist/sw/dataset-sync.js +149 -0
  160. package/dist/sw/dataset.d.ts +91 -0
  161. package/dist/sw/dataset.js +318 -0
  162. package/dist/sw/family.d.ts +15 -0
  163. package/dist/sw/family.js +48 -0
  164. package/dist/sw/graph/index.d.ts +31 -1
  165. package/dist/sw/graph/index.js +57 -5
  166. package/dist/sw/graph/invalidate.d.ts +15 -3
  167. package/dist/sw/graph/invalidate.js +42 -4
  168. package/dist/sw/idb.d.ts +2 -0
  169. package/dist/sw/idb.js +80 -11
  170. package/dist/sw/index.d.ts +2 -0
  171. package/dist/sw/index.js +1 -0
  172. package/dist/sw/pack/store.d.ts +128 -0
  173. package/dist/sw/pack/store.js +737 -0
  174. package/dist/sw/partition.d.ts +6 -1
  175. package/dist/sw/partition.js +21 -2
  176. package/dist/sw/policy.d.ts +2 -0
  177. package/dist/sw/policy.js +11 -0
  178. package/dist/sw/principal-dataset.d.ts +64 -0
  179. package/dist/sw/principal-dataset.js +254 -0
  180. package/dist/sw/probe.d.ts +16 -0
  181. package/dist/sw/probe.js +25 -0
  182. package/dist/sw/render.d.ts +7 -2
  183. package/dist/sw/render.js +19 -5
  184. package/dist/sw/replica.d.ts +12 -3
  185. package/dist/sw/replica.js +40 -5
  186. package/dist/sw/router.d.ts +3 -2
  187. package/dist/sw/router.js +15 -2
  188. package/dist/sw/runtime.d.ts +90 -2
  189. package/dist/sw/runtime.js +857 -61
  190. package/dist/sw/scheduler.d.ts +6 -1
  191. package/dist/sw/scheduler.js +22 -5
  192. package/dist/sw/seal.d.ts +13 -0
  193. package/dist/sw/seal.js +13 -4
  194. package/dist/sw/static.d.ts +18 -0
  195. package/dist/sw/static.js +84 -1
  196. package/dist/sw/storage.d.ts +51 -1
  197. package/dist/sw/storage.js +33 -1
  198. package/dist/sw/write.d.ts +147 -0
  199. package/dist/sw/write.js +625 -0
  200. package/dist/version.d.ts +1 -1
  201. package/dist/version.js +1 -2
  202. package/dist/vite/index.d.ts +26 -0
  203. package/dist/vite/index.js +66 -0
  204. package/dist/vue/index.d.ts +36 -0
  205. package/dist/vue/index.js +53 -0
  206. package/package.json +65 -4
@@ -1,5 +1,6 @@
1
1
  import type { Retention } from '../protocol/route.js';
2
2
  import { type KeyVault, type SealedStorage } from './seal.js';
3
+ import { type WriteStore } from './write.js';
3
4
  /**
4
5
  * Keys in two places. A `persistent` key is a non-extractable `CryptoKey` in IndexedDB, so it
5
6
  * survives a worker restart and a browser restart and is deleted on logout or a change of
@@ -13,9 +14,13 @@ export interface PartitionHost {
13
14
  current(): Promise<string | null>;
14
15
  setCurrent(id: string | null): Promise<void>;
15
16
  /** the sealed store of a partition; `retentionOf` names the key of each artifact's route */
16
- open(id: string, retentionOf: (artifactId: string) => Retention, shared: Retention): SealedStorage;
17
+ open(id: string, retentionOf: (artifactId: string) => Retention, shared: Retention,
18
+ /** whether the store holds principal datasets; false creates no dataset database */
19
+ datasets?: boolean): SealedStorage;
17
20
  /** shreds the keys first, then deletes the store; the store is shut before either */
18
21
  destroy(id: string): Promise<void>;
22
+ /** the sealed write queue of a partition; `destroy` deletes its rows */
23
+ openWrites?(id: string): WriteStore;
19
24
  }
20
25
  /** partitions as IndexedDB databases named for the partition, sealed under `vault` */
21
26
  export declare function idbPartitions(factory?: IDBFactory, vault?: KeyVault): PartitionHost;
@@ -1,5 +1,7 @@
1
1
  import { idbStorage } from './idb.js';
2
+ import { destroyPrincipalDatasets, idbPrincipalDatasets, sealedDatasets } from './principal-dataset.js';
2
3
  import { sealedStorage } from './seal.js';
4
+ import { destroyWrites, idbWriteStore } from './write.js';
3
5
  const KEYS_DB = 'spool-keys';
4
6
  const KEYS = 'keys';
5
7
  const META = 'meta';
@@ -109,20 +111,37 @@ export function idbPartitions(factory = indexedDB, vault = idbVault(factory)) {
109
111
  else
110
112
  await wrap(s.put(id, CURRENT));
111
113
  }),
112
- open(id, retentionOf, shared) {
114
+ open(id, retentionOf, shared, datasets = false) {
113
115
  const found = opened.get(id);
114
116
  if (found)
115
117
  return found.sealed;
116
118
  const base = idbStorage(factory, dbName(id));
117
- const sealed = sealedStorage(base, { partition: id, vault, retentionOf, shared });
119
+ const sealed = sealedStorage(base, {
120
+ partition: id,
121
+ vault,
122
+ retentionOf,
123
+ shared,
124
+ ...(datasets
125
+ ? {
126
+ datasets: sealedDatasets(idbPrincipalDatasets(factory, id), {
127
+ partition: id,
128
+ vault,
129
+ retention: shared
130
+ })
131
+ }
132
+ : {})
133
+ });
118
134
  opened.set(id, { sealed, destroy: base.destroy });
119
135
  return sealed;
120
136
  },
137
+ openWrites: (id) => idbWriteStore(factory, id, vault),
121
138
  async destroy(id) {
122
139
  const held = opened.get(id);
123
140
  held?.sealed.kill();
124
141
  opened.delete(id);
125
142
  await vault.shred(id);
143
+ await destroyWrites(factory, id);
144
+ await destroyPrincipalDatasets(factory, id);
126
145
  if (held)
127
146
  await held.destroy();
128
147
  else {
@@ -4,5 +4,7 @@ export declare const POLICY_HEADERS: readonly ["content-security-policy", "conte
4
4
  export declare const MAX_POLICY_PATHS = 128;
5
5
  /** header names and values, as stored in the replica meta */
6
6
  export type HeaderSet = Array<[string, string]>;
7
+ /** the pairs in `value` whose name is an allowed policy header, or undefined when `value` is not a list */
8
+ export declare function allowedPolicy(value: unknown): HeaderSet | undefined;
7
9
  /** the {@link POLICY_HEADERS} a response carries, possibly none */
8
10
  export declare function policyHeaders(headers: Headers): HeaderSet;
package/dist/sw/policy.js CHANGED
@@ -12,6 +12,17 @@ export const POLICY_HEADERS = [
12
12
  ];
13
13
  /** most paths whose own header set is kept, oldest dropped first */
14
14
  export const MAX_POLICY_PATHS = 128;
15
+ /** the pairs in `value` whose name is an allowed policy header, or undefined when `value` is not a list */
16
+ export function allowedPolicy(value) {
17
+ if (!Array.isArray(value))
18
+ return undefined;
19
+ const names = POLICY_HEADERS;
20
+ return value.filter((pair) => Array.isArray(pair) &&
21
+ pair.length === 2 &&
22
+ typeof pair[0] === 'string' &&
23
+ typeof pair[1] === 'string' &&
24
+ names.includes(pair[0]));
25
+ }
15
26
  /** the {@link POLICY_HEADERS} a response carries, possibly none */
16
27
  export function policyHeaders(headers) {
17
28
  const out = [];
@@ -0,0 +1,64 @@
1
+ import type { Retention } from '../protocol/route.js';
2
+ import { type KeyVault, type Row as Sealed } from './seal.js';
3
+ import type { ReplicaStorage } from './storage.js';
4
+ /** a stored row: the dataset and a salted slot stay readable, the key, row and hash are sealed */
5
+ export type StoredRow = Sealed & {
6
+ name: string;
7
+ slot: string;
8
+ };
9
+ /** a stored dataset meta: the name stays readable, the meta, its digest and the slot salt are sealed */
10
+ export type StoredMeta = Sealed & {
11
+ name: string;
12
+ };
13
+ /** what one commit writes, in sealed form; `deletes` are slots */
14
+ export type StoredCommit = {
15
+ name: string;
16
+ meta: StoredMeta | null;
17
+ clear: boolean;
18
+ puts: readonly StoredRow[];
19
+ deletes: readonly string[];
20
+ };
21
+ /** the durable half of one partition's principal datasets; it sees only sealed records */
22
+ export interface PrincipalDatasetBackend {
23
+ metas(): Promise<StoredMeta[]>;
24
+ meta(name: string): Promise<StoredMeta | undefined>;
25
+ rows(name: string): Promise<StoredRow[]>;
26
+ /** applies a commit in one transaction, or none of it */
27
+ commit(commit: StoredCommit): Promise<void>;
28
+ /** removes every dataset of the partition */
29
+ clear(): Promise<void>;
30
+ }
31
+ /** the dataset half of {@link ReplicaStorage} for a principal partition, and the switch that shuts it */
32
+ export type PrincipalDatasets = Pick<ReplicaStorage, 'getDatasets' | 'readDataset' | 'commitDataset' | 'clearDatasets'> & {
33
+ kill(): void;
34
+ };
35
+ /**
36
+ * The sealed records of one partition in their own database, `spool-principal-datasets` at version
37
+ * 1 (the shared `spool` database is never raised, and `spool-datasets` holds the public rows only).
38
+ * Every record carries its partition as the first key part, so one partition's rows are never read
39
+ * for another and a logout deletes a key range. The database is created by the first call that
40
+ * reaches it.
41
+ */
42
+ export declare function idbPrincipalDatasets(factory: IDBFactory, partition: string): PrincipalDatasetBackend;
43
+ /**
44
+ * Deletes every principal dataset record of `partition`. A database that was never created stays
45
+ * uncreated: the open is rolled back when it would have made one.
46
+ */
47
+ export declare function destroyPrincipalDatasets(factory: IDBFactory, partition: string): Promise<void>;
48
+ /** what the sealed layer needs to bind a record to its partition and to pick its key */
49
+ export type SealedDatasetOptions = {
50
+ partition: string;
51
+ vault: KeyVault;
52
+ /** the retention of the key that seals rows and metas */
53
+ retention: Retention;
54
+ };
55
+ /**
56
+ * Seals a partition's datasets. A row is `{ name, slot, sealed }` where the slot is SHA-256 of a
57
+ * per-dataset random salt, the dataset and the row key, so no key, field name or value sits in
58
+ * IndexedDB in the clear. The row record, the meta and the digest are AES-GCM ciphertext bound to
59
+ * the partition, dataset and slot as additional data: a record copied into another partition or
60
+ * slot does not open. The salt lives only inside the sealed meta, and a clearing commit draws a new
61
+ * one. A record whose key is gone reads as absent, and a meta that does not open clears its
62
+ * dataset. After `kill` every call does nothing, so a request that outlives a wipe writes nothing.
63
+ */
64
+ export declare function sealedDatasets(backend: PrincipalDatasetBackend, options: SealedDatasetOptions): PrincipalDatasets;
@@ -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
+ }
@@ -0,0 +1,16 @@
1
+ /** what the worker's own fetch found in the disk cache: a hit, or a miss (any error is a miss) */
2
+ export type CacheProbe = 'hit' | 'miss';
3
+ /** a fetch that read from disk sends no body bytes over the wire, so its timing entry shows zero */
4
+ export declare function timingHit(entry: {
5
+ encodedBodySize: number;
6
+ } | undefined): CacheProbe;
7
+ /** subresources are forwarded only after a hit; until the probe answers the browser keeps them */
8
+ export declare function shouldForward(probe: CacheProbe | undefined): boolean;
9
+ /** the timing entry the worker's last fetch of `url` left in its own performance timeline */
10
+ export declare function resourceTiming(url: string): {
11
+ encodedBodySize: number;
12
+ } | undefined;
13
+ /** fetches an asset with `only-if-cached` and reads its timing; a rejected fetch is a miss */
14
+ export declare function probeCache(url: string, fetcher: (input: string, init: RequestInit) => Promise<Response>, timing: (url: string) => {
15
+ encodedBodySize: number;
16
+ } | undefined): Promise<CacheProbe>;
@@ -0,0 +1,25 @@
1
+ /** a fetch that read from disk sends no body bytes over the wire, so its timing entry shows zero */
2
+ export function timingHit(entry) {
3
+ return entry !== undefined && entry.encodedBodySize === 0 ? 'hit' : 'miss';
4
+ }
5
+ /** subresources are forwarded only after a hit; until the probe answers the browser keeps them */
6
+ export function shouldForward(probe) {
7
+ return probe === 'hit';
8
+ }
9
+ /** the timing entry the worker's last fetch of `url` left in its own performance timeline */
10
+ export function resourceTiming(url) {
11
+ return performance.getEntriesByName(url).at(-1);
12
+ }
13
+ /** fetches an asset with `only-if-cached` and reads its timing; a rejected fetch is a miss */
14
+ export async function probeCache(url, fetcher, timing) {
15
+ try {
16
+ const response = await fetcher(url, { cache: 'only-if-cached', mode: 'same-origin' });
17
+ await response.arrayBuffer();
18
+ if (!response.ok)
19
+ return 'miss';
20
+ return timingHit(timing(url));
21
+ }
22
+ catch {
23
+ return 'miss';
24
+ }
25
+ }
@@ -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>;
@@ -18,6 +20,9 @@ export type Rendered = {
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.
20
22
  * With `search` and a route that declares `query`, the template or renderer gets the page's
21
- * items in place of the collection, after the dataset proof checks out.
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.
22
27
  */
23
- export declare function renderLocal(config: SwConfig, route: SerializedRoute, source: Artifact, renderers?: Renderers, search?: string): 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,22 +1,36 @@
1
1
  import { renderAssembly } from '../protocol/assembly.js';
2
- import { shapeQuery } from '../protocol/query.js';
2
+ import { shapePlan } from '../protocol/plan.js';
3
+ import { keyField, shapeQuery, shapeRows } from '../protocol/query.js';
3
4
  import { injectClientTag } from '../protocol/tag.js';
4
5
  import { interpretTemplate } from '../protocol/template/interpret.js';
6
+ const isPlain = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
5
7
  /**
6
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
7
9
  * injects, so it equals the network render after normalisation. Any failure (a missing template,
8
10
  * an unfilled placeholder, a renderer that throws) is a refusal, never a partial page.
9
11
  * With `search` and a route that declares `query`, the template or renderer gets the page's
10
- * items in place of the collection, after the dataset proof checks out.
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.
11
16
  */
12
- export async function renderLocal(config, route, source, renderers = {}, search) {
17
+ export async function renderLocal(config, route, source, renderers = {}, search, rows, tables) {
13
18
  try {
14
19
  let artifact = source;
15
- if (search !== undefined && search !== '' && route.query) {
20
+ const query = route.query;
21
+ if (query &&
22
+ (query.plan !== undefined ||
23
+ rows !== undefined ||
24
+ (search !== undefined && search !== ''))) {
16
25
  if (source.strategy === 'assembly' || source.strategy === 'static') {
17
26
  return { ok: false, reason: `${source.strategy} artifact cannot answer a query` };
18
27
  }
19
- const shaped = await shapeQuery(route.query, source.body.data, search);
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 ?? '');
20
34
  if (!shaped.ok)
21
35
  return shaped;
22
36
  artifact = { ...source, body: { data: shaped.data } };
@@ -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 */
@@ -48,6 +51,8 @@ export declare class Replica {
48
51
  needFor(artifact: Artifact, always?: boolean): Record<string, number> | null;
49
52
  /** the records and projections held, for an artifact request's `have`; loads the graph */
50
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[]>;
51
56
  /** the artifact with its references resolved from the graph; loads the graph when it has any */
52
57
  materialize(artifact: Artifact): Promise<Materialized>;
53
58
  /**
@@ -66,8 +71,12 @@ export declare class Replica {
66
71
  sweep(need: Record<string, number>): Promise<Record<string, number> | null>;
67
72
  /** removes artifacts by id */
68
73
  drop(ids: readonly string[]): Promise<void>;
69
- /** empties the replica; `cursor` is where it restarts */
70
- 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>;
71
80
  /** applies a `/.spool/sync` answer and returns the ids of the artifacts it invalidated */
72
81
  applySync(response: SyncResponse): Promise<string[]>;
73
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';
@@ -23,16 +24,36 @@ export class Replica {
23
24
  #policies = new Map();
24
25
  /** records and projections; untouched until a page that references them is stored or shown */
25
26
  graph;
26
- 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) {
27
30
  this.#storage = storage;
28
31
  this.#version = version;
29
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
+ });
30
40
  this.graph = new Graph({
31
41
  storage,
32
42
  tagVersion: (tag) => this.#tags.get(tag) ?? 0,
33
- 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))))
34
47
  });
35
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
+ }
36
57
  /** reads the persisted meta; a different runtime version wipes what the old one stored */
37
58
  async load() {
38
59
  const meta = await this.#storage.getMeta();
@@ -46,6 +67,7 @@ export class Replica {
46
67
  }
47
68
  await this.#storage.clearArtifacts();
48
69
  await this.graph.clear();
70
+ await this.datasets.clear();
49
71
  await this.#save();
50
72
  }
51
73
  /** the vector cursor the replica is current to, or null before the first artifact */
@@ -133,6 +155,13 @@ export class Replica {
133
155
  await this.#wake();
134
156
  return this.graph.held(MAX_HAVE);
135
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
+ }
136
165
  /** the artifact with its references resolved from the graph; loads the graph when it has any */
137
166
  async materialize(artifact) {
138
167
  if (usesGraph(artifact))
@@ -211,12 +240,18 @@ export class Replica {
211
240
  if (this.graph.loaded)
212
241
  this.graph.forgetArtifacts(ids);
213
242
  }
214
- /** empties the replica; `cursor` is where it restarts */
215
- 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) {
216
249
  this.#epoch += 1;
217
250
  this.#memory.clear();
218
251
  await this.#storage.clearArtifacts();
219
252
  await this.graph.clear();
253
+ if (!keepDatasets)
254
+ await this.datasets.clear();
220
255
  this.#tags = new Map();
221
256
  this.#site = undefined;
222
257
  this.#policies = new Map();
@@ -227,7 +262,7 @@ export class Replica {
227
262
  async applySync(response) {
228
263
  if (response.reset) {
229
264
  const held = await this.ids();
230
- await this.wipe(response.cursor);
265
+ await this.wipe(response.cursor, true);
231
266
  this.#syncedAt = this.#now();
232
267
  await this.#save();
233
268
  return held;