@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.
- package/README.md +279 -1607
- package/dist/angular/index.d.ts +13 -0
- package/dist/angular/index.js +25 -0
- package/dist/angular/pages.d.ts +28 -0
- package/dist/angular/pages.js +85 -0
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/location.d.ts +6 -0
- package/dist/cdn/location.js +8 -0
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/bootstrap.d.ts +57 -0
- package/dist/client/bootstrap.js +307 -0
- package/dist/client/contact.d.ts +24 -0
- package/dist/client/contact.js +66 -0
- package/dist/client/declared.d.ts +23 -0
- package/dist/client/declared.js +68 -0
- package/dist/client/entry.js +123 -2
- package/dist/client/gate.d.ts +1 -0
- package/dist/client/gate.js +148 -0
- package/dist/client/index.d.ts +25 -0
- package/dist/client/index.js +1 -0
- package/dist/client/live.d.ts +66 -0
- package/dist/client/live.js +225 -0
- package/dist/client/navigate-entry.d.ts +1 -0
- package/dist/client/navigate-entry.js +4 -0
- package/dist/client/navigate.d.ts +54 -0
- package/dist/client/navigate.js +272 -0
- package/dist/client/persist.d.ts +2 -0
- package/dist/client/persist.js +9 -0
- package/dist/client/register.d.ts +34 -2
- package/dist/client/register.js +64 -7
- package/dist/express/index.d.ts +11 -0
- package/dist/express/index.js +209 -0
- package/dist/hono/index.d.ts +19 -0
- package/dist/hono/index.js +49 -0
- package/dist/next/index.d.ts +33 -0
- package/dist/next/index.js +41 -0
- package/dist/next/pages.d.ts +22 -0
- package/dist/next/pages.js +39 -0
- package/dist/node/listener.d.ts +3 -0
- package/dist/node/listener.js +57 -2
- package/dist/nuxt/index.d.ts +22 -0
- package/dist/nuxt/index.js +104 -0
- package/dist/nuxt/pages.d.ts +26 -0
- package/dist/nuxt/pages.js +48 -0
- package/dist/nuxt/runtime/index.d.ts +24 -0
- package/dist/nuxt/runtime/index.js +58 -0
- package/dist/plugins/coverage.d.ts +14 -0
- package/dist/plugins/coverage.js +27 -0
- package/dist/plugins/mode.d.ts +19 -0
- package/dist/plugins/mode.js +20 -0
- package/dist/plugins/path.d.ts +16 -0
- package/dist/plugins/path.js +139 -0
- package/dist/plugins/report.d.ts +22 -0
- package/dist/plugins/report.js +18 -0
- package/dist/plugins/routes.d.ts +38 -0
- package/dist/plugins/routes.js +52 -0
- package/dist/plugins/static.d.ts +42 -0
- package/dist/plugins/static.js +97 -0
- package/dist/protocol/bundle.d.ts +111 -0
- package/dist/protocol/bundle.js +428 -0
- package/dist/protocol/config.d.ts +80 -1
- package/dist/protocol/config.js +31 -1
- package/dist/protocol/dataset.d.ts +189 -0
- package/dist/protocol/dataset.js +212 -0
- package/dist/protocol/envelope.d.ts +34 -1
- package/dist/protocol/envelope.js +16 -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-rows.d.ts +22 -0
- package/dist/protocol/index-rows.js +53 -0
- package/dist/protocol/index.d.ts +5 -0
- package/dist/protocol/index.js +5 -0
- package/dist/protocol/normalise.d.ts +4 -4
- package/dist/protocol/normalise.js +9 -5
- 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/ring.d.ts +58 -0
- package/dist/protocol/ring.js +6 -0
- package/dist/protocol/route.d.ts +63 -13
- package/dist/protocol/route.js +126 -6
- package/dist/protocol/tag.d.ts +22 -2
- package/dist/protocol/tag.js +25 -4
- package/dist/protocol/view.d.ts +2 -0
- package/dist/protocol/view.js +8 -2
- package/dist/protocol/views.d.ts +6 -0
- package/dist/protocol/views.js +15 -0
- package/dist/protocol/write.d.ts +234 -0
- package/dist/protocol/write.js +343 -0
- package/dist/proxy/eligible.d.ts +22 -1
- package/dist/proxy/eligible.js +62 -15
- package/dist/proxy/proxy.js +28 -18
- package/dist/react-router/index.d.ts +20 -0
- package/dist/react-router/index.js +60 -0
- package/dist/react-router/pages.d.ts +34 -0
- package/dist/react-router/pages.js +83 -0
- package/dist/report/dataset.d.ts +48 -0
- package/dist/report/dataset.js +93 -0
- package/dist/report/demand.d.ts +49 -0
- package/dist/report/demand.js +60 -0
- package/dist/report/derive.d.ts +285 -0
- package/dist/report/derive.js +676 -1
- package/dist/report/economics.d.ts +66 -1
- package/dist/report/economics.js +208 -1
- package/dist/report/headline.d.ts +158 -0
- package/dist/report/headline.js +473 -0
- package/dist/report/index.d.ts +4 -0
- package/dist/report/index.js +4 -0
- package/dist/report/inputs.d.ts +176 -0
- package/dist/report/inputs.js +248 -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 +7 -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/traffic.d.ts +8 -0
- package/dist/report/traffic.js +7 -0
- package/dist/report/trim.d.ts +56 -0
- package/dist/report/trim.js +83 -1
- package/dist/server/bundle.d.ts +55 -0
- package/dist/server/bundle.js +172 -0
- package/dist/server/dataset.d.ts +54 -0
- package/dist/server/dataset.js +355 -0
- package/dist/server/embedded.d.ts +7 -3
- package/dist/server/embedded.js +7 -3
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +6 -0
- package/dist/server/live-sockets.d.ts +54 -0
- package/dist/server/live-sockets.js +131 -0
- package/dist/server/live.d.ts +146 -0
- package/dist/server/live.js +228 -0
- package/dist/server/ring.d.ts +77 -0
- package/dist/server/ring.js +311 -0
- package/dist/server/spool.d.ts +158 -3
- package/dist/server/spool.js +578 -41
- package/dist/server/static.d.ts +42 -0
- package/dist/server/static.js +29 -0
- package/dist/server/stub.d.ts +43 -5
- package/dist/server/stub.js +126 -8
- package/dist/server/write.d.ts +49 -0
- package/dist/server/write.js +223 -0
- package/dist/stores/durable-object.d.ts +46 -0
- package/dist/stores/durable-object.js +114 -0
- package/dist/sw/analytics.d.ts +7 -1
- package/dist/sw/analytics.js +16 -2
- package/dist/sw/coverage.d.ts +6 -0
- package/dist/sw/coverage.js +34 -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/family.d.ts +15 -0
- package/dist/sw/family.js +48 -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.d.ts +2 -0
- package/dist/sw/idb.js +80 -11
- package/dist/sw/index.d.ts +2 -0
- package/dist/sw/index.js +1 -0
- package/dist/sw/pack/store.d.ts +128 -0
- package/dist/sw/pack/store.js +737 -0
- package/dist/sw/partition.d.ts +6 -1
- package/dist/sw/partition.js +21 -2
- package/dist/sw/policy.d.ts +2 -0
- package/dist/sw/policy.js +11 -0
- package/dist/sw/principal-dataset.d.ts +64 -0
- package/dist/sw/principal-dataset.js +254 -0
- package/dist/sw/probe.d.ts +16 -0
- package/dist/sw/probe.js +25 -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/router.d.ts +3 -2
- package/dist/sw/router.js +15 -2
- package/dist/sw/runtime.d.ts +90 -2
- package/dist/sw/runtime.js +857 -61
- package/dist/sw/scheduler.d.ts +6 -1
- package/dist/sw/scheduler.js +22 -5
- 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 +147 -0
- package/dist/sw/write.js +625 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -2
- package/dist/vite/index.d.ts +26 -0
- package/dist/vite/index.js +66 -0
- package/dist/vue/index.d.ts +36 -0
- package/dist/vue/index.js +53 -0
- package/package.json +65 -4
package/dist/sw/scheduler.d.ts
CHANGED
|
@@ -76,8 +76,13 @@ export declare class Scheduler {
|
|
|
76
76
|
* next view, when the age is longer.
|
|
77
77
|
*/
|
|
78
78
|
defer(need: Record<string, number>, ageSec: number): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Queues a sync for the next network moment whatever its benefit. `undefined` queues a full sync from
|
|
81
|
+
* the cursor. Returns false when `need` names no id.
|
|
82
|
+
*/
|
|
83
|
+
queue(need: Record<string, number> | undefined, ageSec: number): boolean;
|
|
79
84
|
/** ids queued by {@link Scheduler.defer} */
|
|
80
85
|
get pending(): string[];
|
|
81
|
-
/** runs the queued syncs as one request; true when nothing was queued */
|
|
86
|
+
/** runs the queued syncs as one request, or one full sync when one was queued; true when nothing was queued */
|
|
82
87
|
drain(): Promise<boolean>;
|
|
83
88
|
}
|
package/dist/sw/scheduler.js
CHANGED
|
@@ -51,6 +51,7 @@ export class Scheduler {
|
|
|
51
51
|
#inflight = new Map();
|
|
52
52
|
#policy;
|
|
53
53
|
#pending = {};
|
|
54
|
+
#full = false;
|
|
54
55
|
#pendingAge = 0;
|
|
55
56
|
/** the rate the benefit uses; moves as syncs report what changed */
|
|
56
57
|
rate;
|
|
@@ -104,8 +105,22 @@ export class Scheduler {
|
|
|
104
105
|
});
|
|
105
106
|
if (benefit <= 0)
|
|
106
107
|
return false;
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
return this.queue(need, ageSec);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Queues a sync for the next network moment whatever its benefit. `undefined` queues a full sync from
|
|
112
|
+
* the cursor. Returns false when `need` names no id.
|
|
113
|
+
*/
|
|
114
|
+
queue(need, ageSec) {
|
|
115
|
+
if (need === undefined) {
|
|
116
|
+
this.#full = true;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
if (Object.keys(need).length === 0)
|
|
120
|
+
return false;
|
|
121
|
+
for (const [id, version] of Object.entries(need)) {
|
|
122
|
+
this.#pending[id] = Math.min(version, this.#pending[id] ?? Infinity);
|
|
123
|
+
}
|
|
109
124
|
}
|
|
110
125
|
this.#pendingAge = Math.max(this.#pendingAge, ageSec);
|
|
111
126
|
return true;
|
|
@@ -114,15 +129,17 @@ export class Scheduler {
|
|
|
114
129
|
get pending() {
|
|
115
130
|
return Object.keys(this.#pending).sort();
|
|
116
131
|
}
|
|
117
|
-
/** runs the queued syncs as one request; true when nothing was queued */
|
|
132
|
+
/** runs the queued syncs as one request, or one full sync when one was queued; true when nothing was queued */
|
|
118
133
|
drain() {
|
|
119
134
|
const need = this.#pending;
|
|
120
|
-
|
|
135
|
+
const full = this.#full;
|
|
136
|
+
if (!full && Object.keys(need).length === 0)
|
|
121
137
|
return Promise.resolve(true);
|
|
122
138
|
const age = this.#pendingAge;
|
|
123
139
|
this.#pending = {};
|
|
124
140
|
this.#pendingAge = 0;
|
|
125
|
-
|
|
141
|
+
this.#full = false;
|
|
142
|
+
return this.sync(full ? undefined : need, age);
|
|
126
143
|
}
|
|
127
144
|
async #run(need, ageSec) {
|
|
128
145
|
const { replica, fetchSync, onNetwork } = this.#deps;
|
package/dist/sw/seal.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Retention } from '../protocol/route.js';
|
|
2
|
+
import type { PrincipalDatasets } from './principal-dataset.js';
|
|
2
3
|
import type { ReplicaStorage } from './storage.js';
|
|
3
4
|
/** where the keys that seal a partition live */
|
|
4
5
|
export interface KeyVault {
|
|
@@ -9,6 +10,16 @@ export interface KeyVault {
|
|
|
9
10
|
}
|
|
10
11
|
/** keys held in memory only, both retentions; for tests and for a worker with no IndexedDB */
|
|
11
12
|
export declare function memoryVault(): KeyVault;
|
|
13
|
+
/** a sealed value as it sits in storage, beside the id and version the store needs as keys */
|
|
14
|
+
export type Row = {
|
|
15
|
+
sealed: {
|
|
16
|
+
iv: Uint8Array;
|
|
17
|
+
data: ArrayBuffer;
|
|
18
|
+
at: Retention;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare function seal(key: CryptoKey, aad: string, value: unknown, at: Retention): Promise<Row>;
|
|
22
|
+
export declare function unseal<T>(vault: KeyVault, partition: string, aad: string, row: Row | undefined): Promise<T | undefined>;
|
|
12
23
|
/** what {@link sealedStorage} needs to decide a key and to bind a value to its place */
|
|
13
24
|
export type SealOptions = {
|
|
14
25
|
partition: string;
|
|
@@ -17,6 +28,8 @@ export type SealOptions = {
|
|
|
17
28
|
retentionOf(id: string): Retention;
|
|
18
29
|
/** the retention for records, projections and the bookkeeping, which no one route owns */
|
|
19
30
|
shared: Retention;
|
|
31
|
+
/** the sealed dataset store of this partition; absent while principal datasets are off */
|
|
32
|
+
datasets?: PrincipalDatasets;
|
|
20
33
|
};
|
|
21
34
|
/** a partition's storage that can be shut for good */
|
|
22
35
|
export type SealedStorage = ReplicaStorage & {
|
package/dist/sw/seal.js
CHANGED
|
@@ -27,12 +27,12 @@ export function memoryVault() {
|
|
|
27
27
|
})
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
-
async function seal(key, aad, value, at) {
|
|
30
|
+
export async function seal(key, aad, value, at) {
|
|
31
31
|
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
32
32
|
const data = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: encoder.encode(aad) }, key, encoder.encode(JSON.stringify(value)));
|
|
33
33
|
return { sealed: { iv, data, at } };
|
|
34
34
|
}
|
|
35
|
-
async function unseal(vault, partition, aad, row) {
|
|
35
|
+
export async function unseal(vault, partition, aad, row) {
|
|
36
36
|
const sealed = row?.sealed;
|
|
37
37
|
if (!sealed)
|
|
38
38
|
return undefined;
|
|
@@ -75,7 +75,10 @@ export function sealedStorage(inner, options) {
|
|
|
75
75
|
}
|
|
76
76
|
const open = (kind, id, row) => unseal(vault, partition, aad(kind, id), row);
|
|
77
77
|
return {
|
|
78
|
-
kill: () =>
|
|
78
|
+
kill: () => {
|
|
79
|
+
dead = true;
|
|
80
|
+
options.datasets?.kill();
|
|
81
|
+
},
|
|
79
82
|
async getArtifact(id) {
|
|
80
83
|
if (dead)
|
|
81
84
|
return undefined;
|
|
@@ -178,6 +181,12 @@ export function sealedStorage(inner, options) {
|
|
|
178
181
|
return out;
|
|
179
182
|
},
|
|
180
183
|
deleteProjections: (ids) => (dead ? Promise.resolve() : inner.deleteProjections(ids)),
|
|
181
|
-
clearGraph: () => (dead ? Promise.resolve() : inner.clearGraph())
|
|
184
|
+
clearGraph: () => (dead ? Promise.resolve() : inner.clearGraph()),
|
|
185
|
+
// without the principal datasets flag a partition holds no dataset and refuses to be given one
|
|
186
|
+
getDatasets: options.datasets?.getDatasets ?? (async () => []),
|
|
187
|
+
readDataset: options.datasets?.readDataset ?? (async () => []),
|
|
188
|
+
commitDataset: options.datasets?.commitDataset ??
|
|
189
|
+
(() => Promise.reject(new Error('a principal partition holds no dataset'))),
|
|
190
|
+
clearDatasets: options.datasets?.clearDatasets ?? (async () => undefined)
|
|
182
191
|
};
|
|
183
192
|
}
|
package/dist/sw/static.d.ts
CHANGED
|
@@ -9,6 +9,24 @@ export declare function replayHeaders(headers: Headers): Headers;
|
|
|
9
9
|
* counting, gives null and the rest is not read.
|
|
10
10
|
*/
|
|
11
11
|
export declare function readCapped(response: Response, cap: number): Promise<Uint8Array | null>;
|
|
12
|
+
/**
|
|
13
|
+
* Splits a body into the visitor's stream and a capture stream, both fed by one read of `body`. A
|
|
14
|
+
* tee drops the chunks a branch has not read yet when the source errors, so the visitor would lose
|
|
15
|
+
* bytes the network already sent. Here the error reaches the visitor only after those chunks.
|
|
16
|
+
*/
|
|
17
|
+
export declare function splitBody(body: ReadableStream<Uint8Array>): {
|
|
18
|
+
visitor: ReadableStream<Uint8Array>;
|
|
19
|
+
capture: ReadableStream<Uint8Array>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The response a visitor gets and the copy the runtime keeps from one network answer. Every body is
|
|
23
|
+
* split (see {@link splitBody}), whatever its status. A redirect and a body-less answer are cloned,
|
|
24
|
+
* because a split response would lose the redirect's url and a body-less one has no stream.
|
|
25
|
+
*/
|
|
26
|
+
export declare function branch(response: Response): {
|
|
27
|
+
visitor: Response;
|
|
28
|
+
copy: Response;
|
|
29
|
+
};
|
|
12
30
|
/**
|
|
13
31
|
* Whether `bytes` are the file a manifest `version` names. A `sha256-`, `sha384-` or `sha512-`
|
|
14
32
|
* version must equal the digest of the bytes, and an entity tag must equal the response's `ETag`.
|
package/dist/sw/static.js
CHANGED
|
@@ -26,8 +26,11 @@ export function replayHeaders(headers) {
|
|
|
26
26
|
* counting, gives null and the rest is not read.
|
|
27
27
|
*/
|
|
28
28
|
export async function readCapped(response, cap) {
|
|
29
|
-
if (Number(response.headers.get('content-length')) > cap)
|
|
29
|
+
if (Number(response.headers.get('content-length')) > cap) {
|
|
30
|
+
// not awaited: a cancelled tee branch settles only once the other branch is read or cancelled
|
|
31
|
+
void response.body?.cancel().catch(() => undefined);
|
|
30
32
|
return null;
|
|
33
|
+
}
|
|
31
34
|
const reader = response.body?.getReader();
|
|
32
35
|
if (!reader)
|
|
33
36
|
return new Uint8Array();
|
|
@@ -53,6 +56,86 @@ export async function readCapped(response, cap) {
|
|
|
53
56
|
}
|
|
54
57
|
return out;
|
|
55
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Splits a body into the visitor's stream and a capture stream, both fed by one read of `body`. A
|
|
61
|
+
* tee drops the chunks a branch has not read yet when the source errors, so the visitor would lose
|
|
62
|
+
* bytes the network already sent. Here the error reaches the visitor only after those chunks.
|
|
63
|
+
*/
|
|
64
|
+
export function splitBody(body) {
|
|
65
|
+
const reader = body.getReader();
|
|
66
|
+
const held = [];
|
|
67
|
+
let end = null;
|
|
68
|
+
let wake = () => { };
|
|
69
|
+
let open = true;
|
|
70
|
+
let copy;
|
|
71
|
+
const capture = new ReadableStream({
|
|
72
|
+
start(controller) {
|
|
73
|
+
copy = controller;
|
|
74
|
+
},
|
|
75
|
+
cancel() {
|
|
76
|
+
open = false;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const finish = (next) => {
|
|
80
|
+
end = next;
|
|
81
|
+
wake();
|
|
82
|
+
};
|
|
83
|
+
void (async () => {
|
|
84
|
+
for (;;) {
|
|
85
|
+
let step;
|
|
86
|
+
try {
|
|
87
|
+
step = await reader.read();
|
|
88
|
+
}
|
|
89
|
+
catch (reason) {
|
|
90
|
+
if (open)
|
|
91
|
+
copy.error(reason);
|
|
92
|
+
return finish({ failed: true, reason });
|
|
93
|
+
}
|
|
94
|
+
if (step.done) {
|
|
95
|
+
if (open)
|
|
96
|
+
copy.close();
|
|
97
|
+
return finish({ failed: false });
|
|
98
|
+
}
|
|
99
|
+
held.push(step.value);
|
|
100
|
+
wake();
|
|
101
|
+
if (open)
|
|
102
|
+
copy.enqueue(step.value);
|
|
103
|
+
}
|
|
104
|
+
})();
|
|
105
|
+
const visitor = new ReadableStream({
|
|
106
|
+
async pull(controller) {
|
|
107
|
+
for (;;) {
|
|
108
|
+
const next = held.shift();
|
|
109
|
+
if (next)
|
|
110
|
+
return controller.enqueue(next);
|
|
111
|
+
if (end?.failed)
|
|
112
|
+
return controller.error(end.reason);
|
|
113
|
+
if (end)
|
|
114
|
+
return controller.close();
|
|
115
|
+
await new Promise((resolve) => (wake = resolve));
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
cancel: (reason) => reader.cancel(reason)
|
|
119
|
+
}, { highWaterMark: 0 });
|
|
120
|
+
return { visitor, capture };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The response a visitor gets and the copy the runtime keeps from one network answer. Every body is
|
|
124
|
+
* split (see {@link splitBody}), whatever its status. A redirect and a body-less answer are cloned,
|
|
125
|
+
* because a split response would lose the redirect's url and a body-less one has no stream.
|
|
126
|
+
*/
|
|
127
|
+
export function branch(response) {
|
|
128
|
+
if (response.redirected || !response.body) {
|
|
129
|
+
return { visitor: response, copy: response.clone() };
|
|
130
|
+
}
|
|
131
|
+
const { visitor, capture } = splitBody(response.body);
|
|
132
|
+
const init = {
|
|
133
|
+
status: response.status,
|
|
134
|
+
statusText: response.statusText,
|
|
135
|
+
headers: response.headers
|
|
136
|
+
};
|
|
137
|
+
return { visitor: new Response(visitor, init), copy: new Response(capture, init) };
|
|
138
|
+
}
|
|
56
139
|
const HASH = /^sha(256|384|512)-([A-Za-z0-9+/]+={0,2})$/;
|
|
57
140
|
const TAG = /^(?:W\/)?"/;
|
|
58
141
|
function base64(bytes) {
|
package/dist/sw/storage.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Artifact } from '../protocol/artifact.js';
|
|
2
2
|
import type { BeaconEvent } from '../protocol/beacon.js';
|
|
3
|
+
import type { DatasetRow } from '../protocol/dataset.js';
|
|
3
4
|
import type { ProjectionEntry, RecordEntry } from '../protocol/graph.js';
|
|
4
5
|
import type { HeaderSet } from './policy.js';
|
|
5
6
|
/** an artifact as held on the device, with the time the worker stored it */
|
|
@@ -38,6 +39,42 @@ export type QueuedBeacon = {
|
|
|
38
39
|
key: number;
|
|
39
40
|
event: BeaconEvent;
|
|
40
41
|
};
|
|
42
|
+
/** what the device keeps about one held dataset; the rows sit beside it, by name and key */
|
|
43
|
+
export type DatasetMeta = {
|
|
44
|
+
name: string;
|
|
45
|
+
/** the row shape version the rows were seeded at */
|
|
46
|
+
schema: string;
|
|
47
|
+
/** the cursor the rows are current to */
|
|
48
|
+
cursor: number;
|
|
49
|
+
/** rows held */
|
|
50
|
+
count: number;
|
|
51
|
+
/** the collection digest of the held rows, 64 hex digits */
|
|
52
|
+
digest: string;
|
|
53
|
+
/** bytes of row JSON held */
|
|
54
|
+
bytes: number;
|
|
55
|
+
/** when the last apply or confirming sync finished, epoch milliseconds */
|
|
56
|
+
appliedAt: number;
|
|
57
|
+
};
|
|
58
|
+
/** a row as stored, with its hash so a patch and a load need not compute it again */
|
|
59
|
+
export type DatasetRowRecord = {
|
|
60
|
+
name: string;
|
|
61
|
+
key: string;
|
|
62
|
+
row: DatasetRow;
|
|
63
|
+
hash: string;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Everything one apply writes. The store commits all of it or none of it: `clear` removes the
|
|
67
|
+
* dataset's rows first, then `deletes` and `puts` apply, and `meta` is written, or removed when null.
|
|
68
|
+
*/
|
|
69
|
+
export type DatasetCommit = {
|
|
70
|
+
name: string;
|
|
71
|
+
meta: DatasetMeta | null;
|
|
72
|
+
clear: boolean;
|
|
73
|
+
puts: readonly DatasetRowRecord[];
|
|
74
|
+
deletes: readonly string[];
|
|
75
|
+
};
|
|
76
|
+
/** the steps of a commit, in order; a test makes the in-memory store fail at one */
|
|
77
|
+
export type DatasetStep = 'clear' | 'deletes' | 'puts' | 'meta';
|
|
41
78
|
/** durable state of the replica; IndexedDB in a browser, a Map in tests */
|
|
42
79
|
export interface ReplicaStorage {
|
|
43
80
|
getArtifact(id: string): Promise<StoredArtifact | undefined>;
|
|
@@ -59,6 +96,19 @@ export interface ReplicaStorage {
|
|
|
59
96
|
allProjections(): Promise<ProjectionEntry[]>;
|
|
60
97
|
deleteProjections(ids: readonly string[]): Promise<void>;
|
|
61
98
|
clearGraph(): Promise<void>;
|
|
99
|
+
/** the meta of every held dataset */
|
|
100
|
+
getDatasets(): Promise<DatasetMeta[]>;
|
|
101
|
+
/** the stored rows of one dataset in ascending key order */
|
|
102
|
+
readDataset(name: string): Promise<DatasetRowRecord[]>;
|
|
103
|
+
/** applies a commit atomically; rejects, leaving the store as it was, when any step fails */
|
|
104
|
+
commitDataset(commit: DatasetCommit): Promise<void>;
|
|
105
|
+
/** removes every dataset's rows and meta */
|
|
106
|
+
clearDatasets(): Promise<void>;
|
|
62
107
|
}
|
|
108
|
+
/** what {@link memoryStorage} can be told to do wrong */
|
|
109
|
+
export type MemoryOptions = {
|
|
110
|
+
/** called before each step of a dataset commit; true makes the commit fail at that step */
|
|
111
|
+
failDatasetAt?: (step: DatasetStep) => boolean;
|
|
112
|
+
};
|
|
63
113
|
/** storage in memory, for tests and for routes that opt out of persistence */
|
|
64
|
-
export declare function memoryStorage(): ReplicaStorage;
|
|
114
|
+
export declare function memoryStorage(options?: MemoryOptions): ReplicaStorage;
|
package/dist/sw/storage.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/** storage in memory, for tests and for routes that opt out of persistence */
|
|
2
|
-
export function memoryStorage() {
|
|
2
|
+
export function memoryStorage(options = {}) {
|
|
3
3
|
const artifacts = new Map();
|
|
4
4
|
const beacons = new Map();
|
|
5
5
|
const records = new Map();
|
|
6
6
|
const projections = new Map();
|
|
7
|
+
const datasetMeta = new Map();
|
|
8
|
+
const datasetRows = new Map();
|
|
7
9
|
let meta;
|
|
8
10
|
let flush;
|
|
9
11
|
let next = 1;
|
|
@@ -29,6 +31,36 @@ export function memoryStorage() {
|
|
|
29
31
|
clearGraph: async () => {
|
|
30
32
|
records.clear();
|
|
31
33
|
projections.clear();
|
|
34
|
+
},
|
|
35
|
+
getDatasets: async () => structuredClone([...datasetMeta.values()]),
|
|
36
|
+
readDataset: async (name) => structuredClone([...(datasetRows.get(name)?.values() ?? [])]).sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0),
|
|
37
|
+
commitDataset: async (commit) => {
|
|
38
|
+
const fail = (step) => {
|
|
39
|
+
if (options.failDatasetAt?.(step))
|
|
40
|
+
throw new Error(`dataset commit failed at ${step}`);
|
|
41
|
+
};
|
|
42
|
+
// steps run on a copy, so a failure leaves the held rows as they were
|
|
43
|
+
const staged = new Map(commit.clear ? undefined : datasetRows.get(commit.name));
|
|
44
|
+
fail('clear');
|
|
45
|
+
for (const key of commit.deletes)
|
|
46
|
+
staged.delete(key);
|
|
47
|
+
fail('deletes');
|
|
48
|
+
for (const put of commit.puts)
|
|
49
|
+
staged.set(put.key, structuredClone(put));
|
|
50
|
+
fail('puts');
|
|
51
|
+
fail('meta');
|
|
52
|
+
if (staged.size === 0)
|
|
53
|
+
datasetRows.delete(commit.name);
|
|
54
|
+
else
|
|
55
|
+
datasetRows.set(commit.name, staged);
|
|
56
|
+
if (commit.meta)
|
|
57
|
+
datasetMeta.set(commit.name, structuredClone(commit.meta));
|
|
58
|
+
else
|
|
59
|
+
datasetMeta.delete(commit.name);
|
|
60
|
+
},
|
|
61
|
+
clearDatasets: async () => {
|
|
62
|
+
datasetMeta.clear();
|
|
63
|
+
datasetRows.clear();
|
|
32
64
|
}
|
|
33
65
|
};
|
|
34
66
|
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { BundleDelta } from '../protocol/bundle.js';
|
|
2
|
+
import { type IndexRoute } from '../protocol/index-rows.js';
|
|
3
|
+
import { type ResourceWrite, type WriteConfig, type WriteConflict, type WriteFlush, type WriteQueued, type WriteRefusal, type WriteRefusalCode, type WriteResolved, type WriteState, type WriteStatus } from '../protocol/write.js';
|
|
4
|
+
import type { PackStore } from './pack/store.js';
|
|
5
|
+
import { type KeyVault } from './seal.js';
|
|
6
|
+
/** one queued value as the worker holds it, whole and unsealed */
|
|
7
|
+
export type WriteRecord = {
|
|
8
|
+
name: string;
|
|
9
|
+
key: string;
|
|
10
|
+
/** the value a flush sends */
|
|
11
|
+
value: unknown;
|
|
12
|
+
/** the version the first edit since the last answered flush started from */
|
|
13
|
+
base: string;
|
|
14
|
+
/** the `Spool-Write-Id` the next request carries; a retry reuses it */
|
|
15
|
+
id: string;
|
|
16
|
+
state: WriteState;
|
|
17
|
+
queuedAt: number;
|
|
18
|
+
updatedAt: number;
|
|
19
|
+
/** bytes of `{ value }` as JSON */
|
|
20
|
+
bytes: number;
|
|
21
|
+
/** edits folded into this record since it was created or last re-based */
|
|
22
|
+
edits: number;
|
|
23
|
+
/** flushes that failed with the outcome unknown; the same id and value are sent again */
|
|
24
|
+
attempts: number;
|
|
25
|
+
/** epoch ms the class deadline falls due; absent when the class has none or a flush failed */
|
|
26
|
+
due?: number;
|
|
27
|
+
/** an edit made after a request for `value` may have left; it goes out in a later flush */
|
|
28
|
+
next?: {
|
|
29
|
+
value: unknown;
|
|
30
|
+
bytes: number;
|
|
31
|
+
};
|
|
32
|
+
conflict?: WriteConflict;
|
|
33
|
+
refusal?: WriteRefusalCode;
|
|
34
|
+
};
|
|
35
|
+
/** durable records of one partition's queue, by class name and key */
|
|
36
|
+
export interface WriteStore {
|
|
37
|
+
get(name: string, key: string): Promise<WriteRecord | undefined>;
|
|
38
|
+
put(record: WriteRecord): Promise<void>;
|
|
39
|
+
delete(name: string, key: string): Promise<void>;
|
|
40
|
+
all(): Promise<WriteRecord[]>;
|
|
41
|
+
}
|
|
42
|
+
/** a queue in memory, for tests */
|
|
43
|
+
export declare function memoryWriteStore(): WriteStore;
|
|
44
|
+
/**
|
|
45
|
+
* The queue of one partition in its own database, `spool-writes` at version 1 (the shared `spool`
|
|
46
|
+
* database is never raised: older workers open it at 2). A row is `{ partition, name, key, sealed }`:
|
|
47
|
+
* the partition, class and key stay readable as the keys, and the value, the versions, the write
|
|
48
|
+
* id and the conflict are AES-GCM ciphertext under the partition's persistent key, bound to the
|
|
49
|
+
* partition, class and key as additional data. A persistent key is used for every queue, since a key
|
|
50
|
+
* that lives in the worker's memory would lose an unflushed value when the browser stops the worker.
|
|
51
|
+
* The database is created by the first call that reaches the store.
|
|
52
|
+
*/
|
|
53
|
+
export declare function idbWriteStore(factory: IDBFactory, partition: string, vault: KeyVault): WriteStore;
|
|
54
|
+
/**
|
|
55
|
+
* Deletes every row of `partition` from `spool-writes`. A database that was never created stays
|
|
56
|
+
* uncreated: the open is rolled back when it would have made one.
|
|
57
|
+
*/
|
|
58
|
+
export declare function destroyWrites(factory: IDBFactory, partition: string): Promise<void>;
|
|
59
|
+
/** what the worker's network layer answers for one flush request */
|
|
60
|
+
export type WriteAnswer = {
|
|
61
|
+
status: number;
|
|
62
|
+
body?: unknown;
|
|
63
|
+
};
|
|
64
|
+
/** the timer a write deadline is set on; a test injects one that fires on demand */
|
|
65
|
+
export type WriteTimers = {
|
|
66
|
+
set(fn: () => unknown, ms: number): unknown;
|
|
67
|
+
clear(handle: unknown): void;
|
|
68
|
+
};
|
|
69
|
+
/** what {@link WriteQueue} needs from the worker */
|
|
70
|
+
export type WriteQueueDeps = {
|
|
71
|
+
store: WriteStore;
|
|
72
|
+
/** the declared classes and their limits */
|
|
73
|
+
classes(): readonly WriteConfig[];
|
|
74
|
+
now(): number;
|
|
75
|
+
/** sends one record's value as `PUT /.spool/write`; rejects when the network fails */
|
|
76
|
+
send(record: WriteRecord): Promise<WriteAnswer>;
|
|
77
|
+
/** called after a flush that tried at least one record, with counts only */
|
|
78
|
+
onFlush?(flush: WriteFlush): void;
|
|
79
|
+
/** the timer the deadlines use; default the global timers */
|
|
80
|
+
timers?: WriteTimers;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* The device queue of one partition for declared writes of the `last-value` kind. One record holds
|
|
84
|
+
* the newest value of a `(class, key)` and the version its first edit started from, and a flush sends
|
|
85
|
+
* one request per record. A record leaves the queue only after the server answers 200; any failure
|
|
86
|
+
* leaves it, and a retry sends the same write id. A 412 or a permanent refusal parks the record as
|
|
87
|
+
* `conflicted` with its local value, and nothing sends it again until `resolve`. Every change to the
|
|
88
|
+
* store runs one at a time, and `kill` stops all of them, so a wipe cannot be followed by a send or
|
|
89
|
+
* a store change.
|
|
90
|
+
*/
|
|
91
|
+
export declare class WriteQueue {
|
|
92
|
+
#private;
|
|
93
|
+
constructor(deps: WriteQueueDeps);
|
|
94
|
+
/** stops the queue for good: no later call sends or stores anything */
|
|
95
|
+
kill(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Sends every queued record when the earliest deadline has passed, and arms the timer for a later
|
|
98
|
+
* one. The worker calls it at a timer fire and at the first contact of each start, so a record
|
|
99
|
+
* whose deadline passed while the worker was stopped goes out once.
|
|
100
|
+
*/
|
|
101
|
+
flushDue(): Promise<WriteFlush>;
|
|
102
|
+
/**
|
|
103
|
+
* Queues `value` as the newest for `(name, key)`. An edit folds into the record already there and
|
|
104
|
+
* keeps the version the first edit started from. When a request for the earlier value may have
|
|
105
|
+
* left, the edit waits in `next` so that request and its retry stay byte-identical.
|
|
106
|
+
*/
|
|
107
|
+
write(name: string, key: string, value: unknown, version: unknown): Promise<WriteQueued | WriteRefusal>;
|
|
108
|
+
/** what the page may see of each queued value */
|
|
109
|
+
list(): Promise<WriteStatus[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Sends one request per record that is not conflicted, or only `only`'s record, and reports
|
|
112
|
+
* counts. A flush already running is joined. A network failure, a sign-in answer and a turned-off
|
|
113
|
+
* server stop the pass and leave the rest queued.
|
|
114
|
+
*/
|
|
115
|
+
flush(only?: {
|
|
116
|
+
name: string;
|
|
117
|
+
key: string;
|
|
118
|
+
}): Promise<WriteFlush>;
|
|
119
|
+
/**
|
|
120
|
+
* Settles a conflicted record. `local` keeps the local value, re-bases it onto the server's
|
|
121
|
+
* version, gives it a new write id and flushes that key. `server` drops the local value and
|
|
122
|
+
* returns what the server held.
|
|
123
|
+
*/
|
|
124
|
+
resolve(name: string, key: string, use: 'local' | 'server'): Promise<WriteResolved | WriteRefusal>;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Brings a held pack to the version a declared write's answer reached. The answer's `Spool-Delta` goes
|
|
128
|
+
* first; one that starts at another version, or that the store refuses, costs one delta fetch to the
|
|
129
|
+
* version the answer names. The store checks a delta's digest before it writes, so a refused delta
|
|
130
|
+
* changes nothing. An answer that landed on another origin, after a redirect, applies nothing.
|
|
131
|
+
*/
|
|
132
|
+
/** the declared write a page sent, with its URL, its JSON body and the routes its index tables read */
|
|
133
|
+
export type SentWrite = {
|
|
134
|
+
write: ResourceWrite;
|
|
135
|
+
url: URL;
|
|
136
|
+
body: unknown;
|
|
137
|
+
routes: readonly IndexRoute[];
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Brings the held pack to the version a declared write's answer reached. The answer's `Spool-Delta` goes
|
|
141
|
+
* first; one that starts at another version, or that the store refuses, costs one delta fetch to the
|
|
142
|
+
* version the answer names. An answer that names no version and sends no delta is predicted from the
|
|
143
|
+
* write's mode when `sent` is given. An answer with a digest and no delta is predicted, and the digest
|
|
144
|
+
* confirms the prediction or drops it at the cost of one delta fetch. An answer that landed on another
|
|
145
|
+
* origin, after a redirect, applies nothing.
|
|
146
|
+
*/
|
|
147
|
+
export declare function settleWrite(response: Response, pack: PackStore, fetchDelta: (from: number, to: number) => Promise<BundleDelta>, expected: string, sent?: SentWrite): Promise<void>;
|