@drupflare/spool 0.3.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.
- package/README.md +263 -41
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/entry.js +33 -1
- package/dist/client/register.d.ts +23 -1
- package/dist/client/register.js +53 -7
- package/dist/protocol/config.d.ts +16 -1
- package/dist/protocol/config.js +3 -1
- package/dist/protocol/dataset.d.ts +183 -0
- package/dist/protocol/dataset.js +206 -0
- package/dist/protocol/envelope.d.ts +1 -0
- package/dist/protocol/envelope.js +2 -1
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/graph.d.ts +23 -4
- package/dist/protocol/graph.js +34 -3
- package/dist/protocol/index.d.ts +3 -0
- package/dist/protocol/index.js +3 -0
- package/dist/protocol/plan.d.ts +100 -0
- package/dist/protocol/plan.js +661 -0
- package/dist/protocol/provision.d.ts +46 -0
- package/dist/protocol/provision.js +38 -0
- package/dist/protocol/query.d.ts +66 -5
- package/dist/protocol/query.js +103 -4
- package/dist/protocol/route.d.ts +6 -2
- package/dist/protocol/route.js +31 -3
- package/dist/protocol/write.d.ts +146 -0
- package/dist/protocol/write.js +101 -0
- package/dist/report/dataset.d.ts +48 -0
- package/dist/report/dataset.js +93 -0
- package/dist/report/derive.d.ts +285 -0
- package/dist/report/derive.js +676 -1
- package/dist/report/economics.d.ts +11 -1
- package/dist/report/economics.js +19 -1
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +2 -0
- package/dist/report/inputs.d.ts +97 -0
- package/dist/report/inputs.js +133 -0
- package/dist/report/institution.d.ts +188 -1
- package/dist/report/institution.js +146 -0
- package/dist/report/loops.d.ts +73 -0
- package/dist/report/loops.js +101 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +3 -1
- package/dist/report/model.d.ts +2 -0
- package/dist/report/model.js +4 -0
- package/dist/report/thirdparty.d.ts +57 -0
- package/dist/report/thirdparty.js +111 -0
- package/dist/report/trim.d.ts +56 -0
- package/dist/report/trim.js +83 -1
- package/dist/server/dataset.d.ts +48 -0
- package/dist/server/dataset.js +308 -0
- package/dist/server/embedded.d.ts +3 -3
- package/dist/server/embedded.js +3 -3
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +2 -0
- package/dist/server/spool.d.ts +20 -0
- package/dist/server/spool.js +126 -18
- package/dist/server/write.d.ts +27 -0
- package/dist/server/write.js +127 -0
- package/dist/sw/analytics.d.ts +6 -0
- package/dist/sw/analytics.js +14 -0
- package/dist/sw/dataset-sync.d.ts +76 -0
- package/dist/sw/dataset-sync.js +149 -0
- package/dist/sw/dataset.d.ts +91 -0
- package/dist/sw/dataset.js +318 -0
- package/dist/sw/graph/index.d.ts +31 -1
- package/dist/sw/graph/index.js +57 -5
- package/dist/sw/graph/invalidate.d.ts +15 -3
- package/dist/sw/graph/invalidate.js +42 -4
- package/dist/sw/idb.js +78 -9
- package/dist/sw/partition.d.ts +6 -1
- package/dist/sw/partition.js +21 -2
- package/dist/sw/principal-dataset.d.ts +64 -0
- package/dist/sw/principal-dataset.js +254 -0
- package/dist/sw/render.d.ts +7 -2
- package/dist/sw/render.js +19 -5
- package/dist/sw/replica.d.ts +12 -3
- package/dist/sw/replica.js +40 -5
- package/dist/sw/runtime.d.ts +43 -2
- package/dist/sw/runtime.js +340 -24
- package/dist/sw/seal.d.ts +13 -0
- package/dist/sw/seal.js +13 -4
- package/dist/sw/static.d.ts +18 -0
- package/dist/sw/static.js +84 -1
- package/dist/sw/storage.d.ts +51 -1
- package/dist/sw/storage.js +33 -1
- package/dist/sw/write.d.ts +107 -0
- package/dist/sw/write.js +442 -0
- package/package.json +3 -2
|
@@ -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
|
-
/**
|
|
27
|
-
|
|
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, {
|
|
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
|
|
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
|
-
|
|
55
|
-
|
|
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
|
|
65
|
-
db
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
}
|
package/dist/sw/partition.d.ts
CHANGED
|
@@ -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
|
|
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;
|
package/dist/sw/partition.js
CHANGED
|
@@ -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, {
|
|
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;
|
|
@@ -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
|
+
}
|
package/dist/sw/render.d.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
|
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 } };
|