@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
@@ -1,4 +1,4 @@
1
- import { collectRefs, resolveRefs } from '../../protocol/graph.js';
1
+ import { collectRefs, resolveRefs, splitRowId } from '../../protocol/graph.js';
2
2
  import { DependencyIndex } from './invalidate.js';
3
3
  import { projectionStore } from './projections.js';
4
4
  import { recordStore } from './records.js';
@@ -12,10 +12,11 @@ export function usesGraph(artifact) {
12
12
  artifact.projections !== undefined ||
13
13
  hasRefs(collectRefs(artifact.body)));
14
14
  }
15
- const hasRefs = (refs) => refs.records.length + refs.projections.length > 0;
15
+ const hasRefs = (refs) => refs.records.length + refs.projections.length + (refs.rows?.length ?? 0) > 0;
16
16
  function childProjections(entry) {
17
17
  return collectRefs(entry.data).projections;
18
18
  }
19
+ const rowsOf = (entry) => collectRefs(entry.data).rows ?? [];
19
20
  /**
20
21
  * Records and projections stored once, the index over them, and the rules that tie them to
21
22
  * artifacts. Nothing is read from storage until {@link Graph.ensure}: a worker that wakes for a
@@ -49,7 +50,7 @@ export class Graph {
49
50
  async #load() {
50
51
  await Promise.all([this.records.load(), this.projections.load()]);
51
52
  for (const entry of this.projections.all()) {
52
- this.index.addProjection(entry.id, entry.deps, childProjections(entry));
53
+ this.index.addProjection(entry.id, entry.deps, childProjections(entry), rowsOf(entry));
53
54
  }
54
55
  for (const { artifact } of await this.#deps.artifacts()) {
55
56
  const uses = collectRefs(artifact.body);
@@ -77,7 +78,7 @@ export class Graph {
77
78
  const added = await this.projections.put(artifact.projections ?? [], persist);
78
79
  for (const id of added) {
79
80
  const entry = this.projections.get(id);
80
- this.index.addProjection(id, entry.deps, childProjections(entry));
81
+ this.index.addProjection(id, entry.deps, childProjections(entry), rowsOf(entry));
81
82
  }
82
83
  const uses = collectRefs(artifact.body);
83
84
  if (hasRefs(uses))
@@ -118,6 +119,24 @@ export class Graph {
118
119
  this.index.removeProjections(projections);
119
120
  return { records: records.sort(), projections, artifacts };
120
121
  }
122
+ /**
123
+ * Applies a change to the rows of dataset `dataset`. A page or projection that references a
124
+ * row holds its id, so a put needs no repair: the row resolves again at the next render, and
125
+ * only the pages that reference it are reported as touched. A deleted row leaves the pages that
126
+ * reference it unresolvable, so they and the projections above them are dropped. Nothing
127
+ * else is looked at.
128
+ */
129
+ async rowsChanged(dataset, puts, deletes) {
130
+ const id = (key) => `${dataset}/${key}`;
131
+ const touched = this.index.rowsReaching([...puts, ...deletes].map(id));
132
+ const gone = this.index.rowsReaching(deletes.map(id));
133
+ await this.projections.drop(gone.projections);
134
+ this.index.removeProjections(gone.projections);
135
+ return {
136
+ touched,
137
+ dropped: { records: [], projections: gone.projections, artifacts: gone.artifacts }
138
+ };
139
+ }
121
140
  /** forgets everything in memory and in storage */
122
141
  async clear() {
123
142
  this.records.forget();
@@ -125,15 +144,48 @@ export class Graph {
125
144
  this.index.clear();
126
145
  await this.#deps.storage.clearGraph();
127
146
  }
147
+ /** the datasets whose rows the artifact references, directly or through projections it reaches */
148
+ datasetsOf(artifact) {
149
+ const names = new Set();
150
+ const seen = new Set();
151
+ const queue = [artifact.body];
152
+ for (let node = queue.pop(); node !== undefined; node = queue.pop()) {
153
+ const refs = collectRefs(node);
154
+ for (const id of refs.rows ?? [])
155
+ names.add(splitRowId(id)?.dataset ?? id);
156
+ for (const id of refs.projections) {
157
+ if (seen.has(id))
158
+ continue;
159
+ seen.add(id);
160
+ const entry = this.projections.get(id);
161
+ if (entry)
162
+ queue.push(entry.data);
163
+ }
164
+ }
165
+ return [...names];
166
+ }
128
167
  /** the artifact with every reference replaced by what the graph holds */
129
168
  async materialize(artifact) {
130
169
  if (artifact.strategy === 'static' || !hasRefs(collectRefs(artifact.body))) {
131
170
  return { ok: true, artifact };
132
171
  }
133
172
  await this.ensure();
173
+ const datasets = this.datasetsOf(artifact);
174
+ if (datasets.length > 0) {
175
+ await this.#deps.loadDatasets?.(datasets);
176
+ const missing = datasets.filter((name) => !this.#deps.held?.(name));
177
+ if (missing.length > 0) {
178
+ return {
179
+ ok: false,
180
+ reason: `dataset ${missing[0]} is not held`,
181
+ datasets: missing
182
+ };
183
+ }
184
+ }
134
185
  const lookup = {
135
186
  record: (id) => this.records.get(id),
136
- projection: (id) => this.projections.get(id)
187
+ projection: (id) => this.projections.get(id),
188
+ ...(this.#deps.row ? { row: this.#deps.row } : {})
137
189
  };
138
190
  if (artifact.strategy !== 'assembly') {
139
191
  const data = resolveRefs(artifact.body.data, lookup);
@@ -6,14 +6,26 @@ import type { RefSet } from '../../protocol/graph.js';
6
6
  */
7
7
  export declare class DependencyIndex {
8
8
  #private;
9
- /** records that `projection` reads the tags in `deps` and references the projections in `children` */
10
- addProjection(projection: string, deps: readonly string[], children: readonly string[]): void;
9
+ /**
10
+ * records that `projection` reads the tags in `deps`, references the projections in `children`
11
+ * and the dataset rows in `rows`
12
+ */
13
+ addProjection(projection: string, deps: readonly string[], children: readonly string[], rows?: readonly string[]): void;
11
14
  removeProjections(ids: readonly string[]): void;
12
- /** records that `artifact` references these records and projections */
15
+ /** records that `artifact` references these records, projections and dataset rows */
13
16
  addArtifact(artifact: string, uses: RefSet): void;
14
17
  removeArtifacts(ids: readonly string[]): void;
15
18
  /** forgets everything */
16
19
  clear(): void;
20
+ /**
21
+ * What a change to dataset rows reaches: the projections that reference one directly, those
22
+ * and every projection above them, and the artifacts that use a reached projection or one of
23
+ * the rows. Row ids are `<dataset>/<key>`.
24
+ */
25
+ rowsReaching(rows: Iterable<string>): {
26
+ projections: string[];
27
+ artifacts: string[];
28
+ };
17
29
  /** the projections that read any of `tags` directly */
18
30
  projectionsReading(tags: Iterable<string>): string[];
19
31
  /** `projections` and every projection that references one of them, directly or not */
@@ -21,16 +21,27 @@ export class DependencyIndex {
21
21
  #parentsByProjection = new Map();
22
22
  #artifactsByRecord = new Map();
23
23
  #artifactsByProjection = new Map();
24
+ #projectionsByRow = new Map();
25
+ #artifactsByRow = new Map();
24
26
  #usesByArtifact = new Map();
25
27
  #readsByProjection = new Map();
26
- /** records that `projection` reads the tags in `deps` and references the projections in `children` */
27
- addProjection(projection, deps, children) {
28
+ /**
29
+ * records that `projection` reads the tags in `deps`, references the projections in `children`
30
+ * and the dataset rows in `rows`
31
+ */
32
+ addProjection(projection, deps, children, rows = []) {
28
33
  this.removeProjections([projection]);
29
- this.#readsByProjection.set(projection, { deps: [...deps], children: [...children] });
34
+ this.#readsByProjection.set(projection, {
35
+ deps: [...deps],
36
+ children: [...children],
37
+ rows: [...rows]
38
+ });
30
39
  for (const tag of deps)
31
40
  add(this.#projectionsByTag, tag, projection);
32
41
  for (const child of children)
33
42
  add(this.#parentsByProjection, child, projection);
43
+ for (const row of rows)
44
+ add(this.#projectionsByRow, row, projection);
34
45
  }
35
46
  removeProjections(ids) {
36
47
  for (const id of ids) {
@@ -41,10 +52,12 @@ export class DependencyIndex {
41
52
  remove(this.#projectionsByTag, tag, id);
42
53
  for (const child of reads.children)
43
54
  remove(this.#parentsByProjection, child, id);
55
+ for (const row of reads.rows)
56
+ remove(this.#projectionsByRow, row, id);
44
57
  this.#readsByProjection.delete(id);
45
58
  }
46
59
  }
47
- /** records that `artifact` references these records and projections */
60
+ /** records that `artifact` references these records, projections and dataset rows */
48
61
  addArtifact(artifact, uses) {
49
62
  this.removeArtifacts([artifact]);
50
63
  this.#usesByArtifact.set(artifact, uses);
@@ -52,6 +65,8 @@ export class DependencyIndex {
52
65
  add(this.#artifactsByRecord, id, artifact);
53
66
  for (const id of uses.projections)
54
67
  add(this.#artifactsByProjection, id, artifact);
68
+ for (const id of uses.rows ?? [])
69
+ add(this.#artifactsByRow, id, artifact);
55
70
  }
56
71
  removeArtifacts(ids) {
57
72
  for (const id of ids) {
@@ -62,6 +77,8 @@ export class DependencyIndex {
62
77
  remove(this.#artifactsByRecord, record, id);
63
78
  for (const projection of uses.projections)
64
79
  remove(this.#artifactsByProjection, projection, id);
80
+ for (const row of uses.rows ?? [])
81
+ remove(this.#artifactsByRow, row, id);
65
82
  this.#usesByArtifact.delete(id);
66
83
  }
67
84
  }
@@ -71,9 +88,30 @@ export class DependencyIndex {
71
88
  this.#parentsByProjection.clear();
72
89
  this.#artifactsByRecord.clear();
73
90
  this.#artifactsByProjection.clear();
91
+ this.#projectionsByRow.clear();
92
+ this.#artifactsByRow.clear();
74
93
  this.#usesByArtifact.clear();
75
94
  this.#readsByProjection.clear();
76
95
  }
96
+ /**
97
+ * What a change to dataset rows reaches: the projections that reference one directly, those
98
+ * and every projection above them, and the artifacts that use a reached projection or one of
99
+ * the rows. Row ids are `<dataset>/<key>`.
100
+ */
101
+ rowsReaching(rows) {
102
+ const direct = new Set();
103
+ const artifacts = new Set();
104
+ for (const row of rows) {
105
+ for (const id of this.#projectionsByRow.get(row) ?? [])
106
+ direct.add(id);
107
+ for (const id of this.#artifactsByRow.get(row) ?? [])
108
+ artifacts.add(id);
109
+ }
110
+ const projections = this.withParents(direct);
111
+ for (const id of this.artifactsUsing([], projections))
112
+ artifacts.add(id);
113
+ return { projections, artifacts: [...artifacts].sort() };
114
+ }
77
115
  /** the projections that read any of `tags` directly */
78
116
  projectionsReading(tags) {
79
117
  const out = new Set();
package/dist/sw/idb.js CHANGED
@@ -4,6 +4,8 @@ const META = 'meta';
4
4
  const BEACONS = 'beacons';
5
5
  const RECORDS = 'records';
6
6
  const PROJECTIONS = 'projections';
7
+ const DATASET_ROWS = 'datasetRows';
8
+ const DATASET_META = 'datasetMeta';
7
9
  const META_KEY = 'replica';
8
10
  const FLUSH_KEY = 'flush';
9
11
  function wrap(request) {
@@ -25,6 +27,7 @@ function done(tx) {
25
27
  */
26
28
  export function idbStorage(factory = indexedDB, name = NAME) {
27
29
  let opened;
30
+ let openedSets;
28
31
  const open = () => (opened ??= new Promise((resolve, reject) => {
29
32
  const request = factory.open(name, 2);
30
33
  request.onupgradeneeded = () => {
@@ -51,8 +54,26 @@ export function idbStorage(factory = indexedDB, name = NAME) {
51
54
  };
52
55
  request.onerror = () => reject(request.error);
53
56
  }));
54
- async function tx(store, mode, run) {
55
- const db = await open();
57
+ // datasets get their own database: an older worker opens `name` at version 2 and fails on a higher one
58
+ const openSets = () => (openedSets ??= new Promise((resolve, reject) => {
59
+ const request = factory.open(`${name}-datasets`, 1);
60
+ request.onupgradeneeded = () => {
61
+ const db = request.result;
62
+ db.createObjectStore(DATASET_ROWS, { keyPath: ['name', 'key'] });
63
+ db.createObjectStore(DATASET_META, { keyPath: 'name' });
64
+ };
65
+ request.onsuccess = () => {
66
+ const db = request.result;
67
+ db.onversionchange = () => {
68
+ db.close();
69
+ openedSets = undefined;
70
+ };
71
+ resolve(db);
72
+ };
73
+ request.onerror = () => reject(request.error);
74
+ }));
75
+ async function tx(store, mode, run, from = open) {
76
+ const db = await from();
56
77
  const transaction = db.transaction(store, mode);
57
78
  const finished = done(transaction);
58
79
  const result = await run(transaction.objectStore(store));
@@ -61,14 +82,18 @@ export function idbStorage(factory = indexedDB, name = NAME) {
61
82
  }
62
83
  return {
63
84
  destroy: async () => {
64
- const db = await opened?.catch(() => undefined);
65
- db?.close();
85
+ const dbs = await Promise.all([opened, openedSets].map((p) => p?.catch(() => undefined)));
86
+ for (const db of dbs)
87
+ db?.close();
66
88
  opened = undefined;
67
- await new Promise((resolve) => {
68
- const request = factory.deleteDatabase(name);
69
- request.onsuccess = () => resolve();
70
- request.onerror = () => resolve();
71
- });
89
+ openedSets = undefined;
90
+ for (const dbName of [name, `${name}-datasets`]) {
91
+ await new Promise((resolve) => {
92
+ const request = factory.deleteDatabase(dbName);
93
+ request.onsuccess = () => resolve();
94
+ request.onerror = () => resolve();
95
+ });
96
+ }
72
97
  },
73
98
  getArtifact: (id) => tx(ARTIFACTS, 'readonly', (s) => wrap(s.get(id))),
74
99
  putArtifact: (stored) => tx(ARTIFACTS, 'readwrite', async (s) => void (await wrap(s.put(stored)))),
@@ -112,6 +137,50 @@ export function idbStorage(factory = indexedDB, name = NAME) {
112
137
  clearGraph: async () => {
113
138
  await tx(RECORDS, 'readwrite', async (s) => void (await wrap(s.clear())));
114
139
  await tx(PROJECTIONS, 'readwrite', async (s) => void (await wrap(s.clear())));
140
+ },
141
+ getDatasets: () => tx(DATASET_META, 'readonly', (s) => wrap(s.getAll()), openSets),
142
+ readDataset: (name) => tx(DATASET_ROWS, 'readonly', (s) => wrap(s.getAll(IDBKeyRange.bound([name], [name, []]))), openSets),
143
+ commitDataset: async (commit) => {
144
+ const db = await openSets();
145
+ // strict durability: the commit is reported only after the engine verified the write
146
+ const transaction = db.transaction([DATASET_ROWS, DATASET_META], 'readwrite', {
147
+ durability: 'strict'
148
+ });
149
+ const finished = done(transaction);
150
+ // a rejected commit has aborted its transaction; the caller learns it from `finished`
151
+ finished.catch(() => undefined);
152
+ try {
153
+ const rows = transaction.objectStore(DATASET_ROWS);
154
+ const metas = transaction.objectStore(DATASET_META);
155
+ if (commit.clear)
156
+ rows.delete(IDBKeyRange.bound([commit.name], [commit.name, []]));
157
+ for (const key of commit.deletes)
158
+ rows.delete([commit.name, key]);
159
+ for (const put of commit.puts)
160
+ rows.put(put);
161
+ if (commit.meta)
162
+ metas.put(commit.meta);
163
+ else
164
+ metas.delete(commit.name);
165
+ }
166
+ catch (error) {
167
+ try {
168
+ transaction.abort();
169
+ }
170
+ catch {
171
+ // already finished
172
+ }
173
+ throw error;
174
+ }
175
+ await finished;
176
+ },
177
+ clearDatasets: async () => {
178
+ const db = await openSets();
179
+ const transaction = db.transaction([DATASET_ROWS, DATASET_META], 'readwrite');
180
+ const finished = done(transaction);
181
+ transaction.objectStore(DATASET_ROWS).clear();
182
+ transaction.objectStore(DATASET_META).clear();
183
+ await finished;
115
184
  }
116
185
  };
117
186
  }
@@ -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 {
@@ -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;