@drupflare/spool 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +1749 -2
- package/dist/cdn/client.js +1 -0
- package/dist/cdn/location.d.ts +11 -0
- package/dist/cdn/location.js +15 -0
- package/dist/cdn/manifest.json +16 -0
- package/dist/cdn/spool-sw.js +1 -0
- package/dist/client/entry.d.ts +1 -0
- package/dist/client/entry.js +25 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +2 -0
- package/dist/client/lifecycle.d.ts +10 -0
- package/dist/client/lifecycle.js +10 -0
- package/dist/client/loads.d.ts +16 -0
- package/dist/client/loads.js +41 -0
- package/dist/client/register.d.ts +34 -0
- package/dist/client/register.js +58 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/media/embeds.d.ts +59 -0
- package/dist/media/embeds.js +120 -0
- package/dist/media/headers.d.ts +13 -0
- package/dist/media/headers.js +44 -0
- package/dist/media/html.d.ts +27 -0
- package/dist/media/html.js +93 -0
- package/dist/media/image.d.ts +222 -0
- package/dist/media/image.js +413 -0
- package/dist/media/index.d.ts +6 -0
- package/dist/media/index.js +6 -0
- package/dist/media/lazy.d.ts +26 -0
- package/dist/media/lazy.js +74 -0
- package/dist/media/responsive.d.ts +45 -0
- package/dist/media/responsive.js +98 -0
- package/dist/media/video.d.ts +30 -0
- package/dist/media/video.js +75 -0
- package/dist/node/listener.d.ts +19 -0
- package/dist/node/listener.js +76 -0
- package/dist/protocol/artifact.d.ts +54 -0
- package/dist/protocol/artifact.js +79 -0
- package/dist/protocol/assembly.d.ts +34 -0
- package/dist/protocol/assembly.js +72 -0
- package/dist/protocol/beacon.d.ts +59 -0
- package/dist/protocol/beacon.js +86 -0
- package/dist/protocol/collector.d.ts +41 -0
- package/dist/protocol/collector.js +84 -0
- package/dist/protocol/config.d.ts +89 -0
- package/dist/protocol/config.js +13 -0
- package/dist/protocol/envelope.d.ts +47 -0
- package/dist/protocol/envelope.js +162 -0
- package/dist/protocol/errors.d.ts +7 -0
- package/dist/protocol/errors.js +9 -0
- package/dist/protocol/graph.d.ts +81 -0
- package/dist/protocol/graph.js +201 -0
- package/dist/protocol/index.d.ts +19 -0
- package/dist/protocol/index.js +19 -0
- package/dist/protocol/manifest.d.ts +29 -0
- package/dist/protocol/manifest.js +94 -0
- package/dist/protocol/normalise.d.ts +20 -0
- package/dist/protocol/normalise.js +29 -0
- package/dist/protocol/pattern.d.ts +12 -0
- package/dist/protocol/pattern.js +82 -0
- package/dist/protocol/provision.d.ts +100 -0
- package/dist/protocol/provision.js +105 -0
- package/dist/protocol/route.d.ts +179 -0
- package/dist/protocol/route.js +135 -0
- package/dist/protocol/sync.d.ts +31 -0
- package/dist/protocol/sync.js +87 -0
- package/dist/protocol/tag.d.ts +12 -0
- package/dist/protocol/tag.js +26 -0
- package/dist/protocol/template/compile.d.ts +35 -0
- package/dist/protocol/template/compile.js +82 -0
- package/dist/protocol/template/interpret.d.ts +9 -0
- package/dist/protocol/template/interpret.js +82 -0
- package/dist/protocol/vector.d.ts +32 -0
- package/dist/protocol/vector.js +56 -0
- package/dist/protocol/view.d.ts +110 -0
- package/dist/protocol/view.js +201 -0
- package/dist/proxy/dictionary.d.ts +58 -0
- package/dist/proxy/dictionary.js +197 -0
- package/dist/proxy/eligible.d.ts +45 -0
- package/dist/proxy/eligible.js +96 -0
- package/dist/proxy/index.d.ts +5 -0
- package/dist/proxy/index.js +5 -0
- package/dist/proxy/proxy.d.ts +133 -0
- package/dist/proxy/proxy.js +507 -0
- package/dist/proxy/purge.d.ts +10 -0
- package/dist/proxy/purge.js +66 -0
- package/dist/proxy/tags.d.ts +8 -0
- package/dist/proxy/tags.js +36 -0
- package/dist/report/derive.d.ts +223 -0
- package/dist/report/derive.js +494 -0
- package/dist/report/economics.d.ts +14 -0
- package/dist/report/economics.js +24 -0
- package/dist/report/index.d.ts +9 -0
- package/dist/report/index.js +9 -0
- package/dist/report/inputs.d.ts +141 -0
- package/dist/report/inputs.js +216 -0
- package/dist/report/ledger.d.ts +169 -0
- package/dist/report/ledger.js +278 -0
- package/dist/report/measurement.d.ts +48 -0
- package/dist/report/measurement.js +117 -0
- package/dist/report/model.d.ts +42 -0
- package/dist/report/model.js +74 -0
- package/dist/report/thirdparty.d.ts +62 -0
- package/dist/report/thirdparty.js +107 -0
- package/dist/report/traffic.d.ts +140 -0
- package/dist/report/traffic.js +228 -0
- package/dist/report/trim.d.ts +81 -0
- package/dist/report/trim.js +105 -0
- package/dist/server/beacon.d.ts +20 -0
- package/dist/server/beacon.js +47 -0
- package/dist/server/embedded.d.ts +6 -0
- package/dist/server/embedded.js +6 -0
- package/dist/server/ga4.d.ts +70 -0
- package/dist/server/ga4.js +165 -0
- package/dist/server/identity.d.ts +12 -0
- package/dist/server/identity.js +17 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +7 -0
- package/dist/server/sink.d.ts +56 -0
- package/dist/server/sink.js +133 -0
- package/dist/server/size.d.ts +7 -0
- package/dist/server/size.js +18 -0
- package/dist/server/spool.d.ts +97 -0
- package/dist/server/spool.js +463 -0
- package/dist/server/stub.d.ts +9 -0
- package/dist/server/stub.js +19 -0
- package/dist/server/sync.d.ts +12 -0
- package/dist/server/sync.js +48 -0
- package/dist/server/work.d.ts +22 -0
- package/dist/server/work.js +30 -0
- package/dist/stores/durable-object.d.ts +19 -0
- package/dist/stores/durable-object.js +53 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/index.js +2 -0
- package/dist/stores/memory.d.ts +4 -0
- package/dist/stores/memory.js +10 -0
- package/dist/stores/version-store.d.ts +11 -0
- package/dist/stores/version-store.js +1 -0
- package/dist/sw/analytics.d.ts +81 -0
- package/dist/sw/analytics.js +275 -0
- package/dist/sw/collect.d.ts +8 -0
- package/dist/sw/collect.js +33 -0
- package/dist/sw/core.d.ts +1 -0
- package/dist/sw/core.js +3 -0
- package/dist/sw/freshness.d.ts +15 -0
- package/dist/sw/freshness.js +20 -0
- package/dist/sw/graph/index.d.ts +67 -0
- package/dist/sw/graph/index.js +170 -0
- package/dist/sw/graph/invalidate.d.ts +23 -0
- package/dist/sw/graph/invalidate.js +109 -0
- package/dist/sw/graph/projections.d.ts +7 -0
- package/dist/sw/graph/projections.js +9 -0
- package/dist/sw/graph/records.d.ts +7 -0
- package/dist/sw/graph/records.js +9 -0
- package/dist/sw/graph/store.d.ts +32 -0
- package/dist/sw/graph/store.js +52 -0
- package/dist/sw/idb.d.ts +8 -0
- package/dist/sw/idb.js +117 -0
- package/dist/sw/index.d.ts +2 -0
- package/dist/sw/index.js +1 -0
- package/dist/sw/partition.d.ts +21 -0
- package/dist/sw/partition.js +137 -0
- package/dist/sw/policy.d.ts +8 -0
- package/dist/sw/policy.js +24 -0
- package/dist/sw/provision.d.ts +39 -0
- package/dist/sw/provision.js +61 -0
- package/dist/sw/render.d.ts +21 -0
- package/dist/sw/render.js +53 -0
- package/dist/sw/replica.d.ts +77 -0
- package/dist/sw/replica.js +299 -0
- package/dist/sw/router.d.ts +42 -0
- package/dist/sw/router.js +48 -0
- package/dist/sw/routing.d.ts +23 -0
- package/dist/sw/routing.js +48 -0
- package/dist/sw/runtime.d.ts +112 -0
- package/dist/sw/runtime.js +1070 -0
- package/dist/sw/scheduler.d.ts +83 -0
- package/dist/sw/scheduler.js +149 -0
- package/dist/sw/seal.d.ts +37 -0
- package/dist/sw/seal.js +183 -0
- package/dist/sw/serve.d.ts +15 -0
- package/dist/sw/serve.js +63 -0
- package/dist/sw/shadow.d.ts +16 -0
- package/dist/sw/shadow.js +27 -0
- package/dist/sw/static.d.ts +17 -0
- package/dist/sw/static.js +81 -0
- package/dist/sw/storage.d.ts +64 -0
- package/dist/sw/storage.js +34 -0
- package/dist/sw/thirdparty.d.ts +17 -0
- package/dist/sw/thirdparty.js +36 -0
- package/dist/trim/css.d.ts +38 -0
- package/dist/trim/css.js +328 -0
- package/dist/trim/index.d.ts +3 -0
- package/dist/trim/index.js +3 -0
- package/dist/trim/manifest.d.ts +67 -0
- package/dist/trim/manifest.js +134 -0
- package/dist/trim/tokens.d.ts +47 -0
- package/dist/trim/tokens.js +142 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +3 -0
- package/package.json +150 -5
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { SyncResponse } from '../protocol/sync.js';
|
|
2
|
+
import type { Replica } from './replica.js';
|
|
3
|
+
/** what the scheduler needs from the outside world */
|
|
4
|
+
export type SchedulerDeps = {
|
|
5
|
+
replica: Replica;
|
|
6
|
+
/**
|
|
7
|
+
* asks the server for everything changed after `from`, or, given `need`, only the ids in it
|
|
8
|
+
* whose version moved past the held one; rejects when the network fails
|
|
9
|
+
*/
|
|
10
|
+
fetchSync(from: number, need?: Record<string, number>): Promise<SyncResponse>;
|
|
11
|
+
/** called after each request the scheduler made, so queued work can ride the same network moment */
|
|
12
|
+
onNetwork?(): void;
|
|
13
|
+
/** what a sync costs and what showing stale content costs, in weighed bytes; see `ProvisionPrior` */
|
|
14
|
+
policy?: SyncPolicy;
|
|
15
|
+
};
|
|
16
|
+
/** the numbers {@link syncBenefit} weighs */
|
|
17
|
+
export type SyncPolicy = {
|
|
18
|
+
/** a dependency tag's change rate per day before any has been observed */
|
|
19
|
+
changesPerDay: number;
|
|
20
|
+
/** bytes one stale view costs, as the page a fresh sync would have saved */
|
|
21
|
+
staleBytes: number;
|
|
22
|
+
/** bytes one sync request costs */
|
|
23
|
+
syncBytes: number;
|
|
24
|
+
};
|
|
25
|
+
/** a policy that always finds a sync worth making only after enough time has passed */
|
|
26
|
+
export declare const DEFAULT_POLICY: SyncPolicy;
|
|
27
|
+
/**
|
|
28
|
+
* A change rate learned from exposure. Each tag a sync asked about was watched for `seconds`
|
|
29
|
+
* and either changed or did not; the estimate is the Gamma posterior mean, which starts at the
|
|
30
|
+
* prior rate and moves toward the observed one as exposure accumulates.
|
|
31
|
+
*/
|
|
32
|
+
export declare class ChangeRate {
|
|
33
|
+
#private;
|
|
34
|
+
/** the prior counts as one change seen over `1 / perDay` days */
|
|
35
|
+
constructor(perDay: number);
|
|
36
|
+
/** records `tags` watched for `seconds` each, of which `changed` did change */
|
|
37
|
+
observe(tags: number, seconds: number, changed: number): void;
|
|
38
|
+
/** changes per second per tag */
|
|
39
|
+
perSecond(): number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Expected benefit of syncing now, in weighed bytes: the chance something changed since the last
|
|
43
|
+
* check times what a stale view costs, less what the sync costs. It grows with `ageSec` toward
|
|
44
|
+
* `staleBytes - syncBytes`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function syncBenefit(input: {
|
|
47
|
+
ageSec: number;
|
|
48
|
+
perSecond: number;
|
|
49
|
+
tags: number;
|
|
50
|
+
policy: SyncPolicy;
|
|
51
|
+
}): number;
|
|
52
|
+
/**
|
|
53
|
+
* Runs syncs for the whole origin. The worker is one instance for every tab, so it is the single
|
|
54
|
+
* sync leader: a required sync, a background sync and a second tab's request join the one already
|
|
55
|
+
* running when it covers them, and otherwise wait for it, so two syncs are never in flight at once.
|
|
56
|
+
* A full sync covers any `need`. A deferred sync waits for a network moment that is happening
|
|
57
|
+
* anyway and runs only when its expected benefit is positive.
|
|
58
|
+
*/
|
|
59
|
+
export declare class Scheduler {
|
|
60
|
+
#private;
|
|
61
|
+
/** the rate the benefit uses; moves as syncs report what changed */
|
|
62
|
+
readonly rate: ChangeRate;
|
|
63
|
+
constructor(deps: SchedulerDeps);
|
|
64
|
+
/**
|
|
65
|
+
* Syncs now, or joins the sync already running. Resolves true when the replica is current to
|
|
66
|
+
* the server for what was asked and false when the request failed. With nothing stored there is
|
|
67
|
+
* nothing to validate, so a full sync makes no request. `ageSec` is how long the asked-about ids
|
|
68
|
+
* went unchecked, which is the exposure the change rate learns from.
|
|
69
|
+
*/
|
|
70
|
+
sync(need?: Record<string, number>, ageSec?: number): Promise<boolean>;
|
|
71
|
+
/** a sync nobody waits on: the soft contract's single refresh after a local view */
|
|
72
|
+
background(need?: Record<string, number>, ageSec?: number): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Queues a sync of `need` for the next network moment when {@link syncBenefit} is positive after
|
|
75
|
+
* `ageSec` without a check. Returns whether it was queued; a refused request is asked again on the
|
|
76
|
+
* next view, when the age is longer.
|
|
77
|
+
*/
|
|
78
|
+
defer(need: Record<string, number>, ageSec: number): boolean;
|
|
79
|
+
/** ids queued by {@link Scheduler.defer} */
|
|
80
|
+
get pending(): string[];
|
|
81
|
+
/** runs the queued syncs as one request; true when nothing was queued */
|
|
82
|
+
drain(): Promise<boolean>;
|
|
83
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { DEFAULT_PRIOR } from '../protocol/provision.js';
|
|
2
|
+
const DAY = 86_400;
|
|
3
|
+
/** a policy that always finds a sync worth making only after enough time has passed */
|
|
4
|
+
export const DEFAULT_POLICY = {
|
|
5
|
+
changesPerDay: DEFAULT_PRIOR.changesPerDay,
|
|
6
|
+
staleBytes: DEFAULT_PRIOR.avoidedBytes,
|
|
7
|
+
syncBytes: DEFAULT_PRIOR.requestBytes
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* A change rate learned from exposure. Each tag a sync asked about was watched for `seconds`
|
|
11
|
+
* and either changed or did not; the estimate is the Gamma posterior mean, which starts at the
|
|
12
|
+
* prior rate and moves toward the observed one as exposure accumulates.
|
|
13
|
+
*/
|
|
14
|
+
export class ChangeRate {
|
|
15
|
+
#changes;
|
|
16
|
+
#seconds;
|
|
17
|
+
/** the prior counts as one change seen over `1 / perDay` days */
|
|
18
|
+
constructor(perDay) {
|
|
19
|
+
this.#changes = 1;
|
|
20
|
+
this.#seconds = DAY / perDay;
|
|
21
|
+
}
|
|
22
|
+
/** records `tags` watched for `seconds` each, of which `changed` did change */
|
|
23
|
+
observe(tags, seconds, changed) {
|
|
24
|
+
this.#changes += changed;
|
|
25
|
+
this.#seconds += tags * seconds;
|
|
26
|
+
}
|
|
27
|
+
/** changes per second per tag */
|
|
28
|
+
perSecond() {
|
|
29
|
+
return this.#changes / this.#seconds;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Expected benefit of syncing now, in weighed bytes: the chance something changed since the last
|
|
34
|
+
* check times what a stale view costs, less what the sync costs. It grows with `ageSec` toward
|
|
35
|
+
* `staleBytes - syncBytes`.
|
|
36
|
+
*/
|
|
37
|
+
export function syncBenefit(input) {
|
|
38
|
+
const chance = 1 - Math.exp(-input.perSecond * input.tags * Math.max(0, input.ageSec));
|
|
39
|
+
return chance * input.policy.staleBytes - input.policy.syncBytes;
|
|
40
|
+
}
|
|
41
|
+
const keyOf = (need) => need ? Object.entries(need).sort().join('|') : '*';
|
|
42
|
+
/**
|
|
43
|
+
* Runs syncs for the whole origin. The worker is one instance for every tab, so it is the single
|
|
44
|
+
* sync leader: a required sync, a background sync and a second tab's request join the one already
|
|
45
|
+
* running when it covers them, and otherwise wait for it, so two syncs are never in flight at once.
|
|
46
|
+
* A full sync covers any `need`. A deferred sync waits for a network moment that is happening
|
|
47
|
+
* anyway and runs only when its expected benefit is positive.
|
|
48
|
+
*/
|
|
49
|
+
export class Scheduler {
|
|
50
|
+
#deps;
|
|
51
|
+
#inflight = new Map();
|
|
52
|
+
#policy;
|
|
53
|
+
#pending = {};
|
|
54
|
+
#pendingAge = 0;
|
|
55
|
+
/** the rate the benefit uses; moves as syncs report what changed */
|
|
56
|
+
rate;
|
|
57
|
+
constructor(deps) {
|
|
58
|
+
this.#deps = deps;
|
|
59
|
+
this.#policy = deps.policy ?? DEFAULT_POLICY;
|
|
60
|
+
this.rate = new ChangeRate(this.#policy.changesPerDay);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Syncs now, or joins the sync already running. Resolves true when the replica is current to
|
|
64
|
+
* the server for what was asked and false when the request failed. With nothing stored there is
|
|
65
|
+
* nothing to validate, so a full sync makes no request. `ageSec` is how long the asked-about ids
|
|
66
|
+
* went unchecked, which is the exposure the change rate learns from.
|
|
67
|
+
*/
|
|
68
|
+
sync(need, ageSec) {
|
|
69
|
+
const full = this.#inflight.get('*');
|
|
70
|
+
if (full)
|
|
71
|
+
return full;
|
|
72
|
+
const key = keyOf(need);
|
|
73
|
+
const same = this.#inflight.get(key);
|
|
74
|
+
if (same)
|
|
75
|
+
return same;
|
|
76
|
+
const before = [...this.#inflight.values()];
|
|
77
|
+
const started = Promise.allSettled(before)
|
|
78
|
+
.then(() => this.#run(need, ageSec))
|
|
79
|
+
.finally(() => {
|
|
80
|
+
if (this.#inflight.get(key) === started)
|
|
81
|
+
this.#inflight.delete(key);
|
|
82
|
+
});
|
|
83
|
+
this.#inflight.set(key, started);
|
|
84
|
+
return started;
|
|
85
|
+
}
|
|
86
|
+
/** a sync nobody waits on: the soft contract's single refresh after a local view */
|
|
87
|
+
background(need, ageSec) {
|
|
88
|
+
return this.sync(need, ageSec).then(() => undefined, () => undefined);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Queues a sync of `need` for the next network moment when {@link syncBenefit} is positive after
|
|
92
|
+
* `ageSec` without a check. Returns whether it was queued; a refused request is asked again on the
|
|
93
|
+
* next view, when the age is longer.
|
|
94
|
+
*/
|
|
95
|
+
defer(need, ageSec) {
|
|
96
|
+
const tags = Object.keys(need).length;
|
|
97
|
+
if (tags === 0)
|
|
98
|
+
return false;
|
|
99
|
+
const benefit = syncBenefit({
|
|
100
|
+
ageSec,
|
|
101
|
+
perSecond: this.rate.perSecond(),
|
|
102
|
+
tags,
|
|
103
|
+
policy: this.#policy
|
|
104
|
+
});
|
|
105
|
+
if (benefit <= 0)
|
|
106
|
+
return false;
|
|
107
|
+
for (const [id, version] of Object.entries(need)) {
|
|
108
|
+
this.#pending[id] = Math.min(version, this.#pending[id] ?? Infinity);
|
|
109
|
+
}
|
|
110
|
+
this.#pendingAge = Math.max(this.#pendingAge, ageSec);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
/** ids queued by {@link Scheduler.defer} */
|
|
114
|
+
get pending() {
|
|
115
|
+
return Object.keys(this.#pending).sort();
|
|
116
|
+
}
|
|
117
|
+
/** runs the queued syncs as one request; true when nothing was queued */
|
|
118
|
+
drain() {
|
|
119
|
+
const need = this.#pending;
|
|
120
|
+
if (Object.keys(need).length === 0)
|
|
121
|
+
return Promise.resolve(true);
|
|
122
|
+
const age = this.#pendingAge;
|
|
123
|
+
this.#pending = {};
|
|
124
|
+
this.#pendingAge = 0;
|
|
125
|
+
return this.sync(need, age);
|
|
126
|
+
}
|
|
127
|
+
async #run(need, ageSec) {
|
|
128
|
+
const { replica, fetchSync, onNetwork } = this.#deps;
|
|
129
|
+
const from = replica.cursor;
|
|
130
|
+
if (from === null && !need)
|
|
131
|
+
return true;
|
|
132
|
+
try {
|
|
133
|
+
const response = await fetchSync(from ?? 0, need);
|
|
134
|
+
if (need && ageSec !== undefined && !response.reset) {
|
|
135
|
+
const asked = Object.keys(need);
|
|
136
|
+
const moved = asked.filter((tag) => Object.hasOwn(response.changed, tag)).length;
|
|
137
|
+
this.rate.observe(asked.length, ageSec, moved);
|
|
138
|
+
}
|
|
139
|
+
await replica.applySync(response);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
onNetwork?.();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Retention } from '../protocol/route.js';
|
|
2
|
+
import type { ReplicaStorage } from './storage.js';
|
|
3
|
+
/** where the keys that seal a partition live */
|
|
4
|
+
export interface KeyVault {
|
|
5
|
+
/** the key of `retention` for `partition`; with `create` false, undefined when none exists */
|
|
6
|
+
get(partition: string, retention: Retention, create: boolean): Promise<CryptoKey | undefined>;
|
|
7
|
+
/** deletes every key of `partition`; what it sealed cannot be read afterwards */
|
|
8
|
+
shred(partition: string): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
/** keys held in memory only, both retentions; for tests and for a worker with no IndexedDB */
|
|
11
|
+
export declare function memoryVault(): KeyVault;
|
|
12
|
+
/** what {@link sealedStorage} needs to decide a key and to bind a value to its place */
|
|
13
|
+
export type SealOptions = {
|
|
14
|
+
partition: string;
|
|
15
|
+
vault: KeyVault;
|
|
16
|
+
/** the retention of the route that owns artifact `id` */
|
|
17
|
+
retentionOf(id: string): Retention;
|
|
18
|
+
/** the retention for records, projections and the bookkeeping, which no one route owns */
|
|
19
|
+
shared: Retention;
|
|
20
|
+
};
|
|
21
|
+
/** a partition's storage that can be shut for good */
|
|
22
|
+
export type SealedStorage = ReplicaStorage & {
|
|
23
|
+
kill(): void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Wraps a partition's storage so artifacts, records, projections and the replica meta are AES-GCM
|
|
27
|
+
* ciphertext at rest. The ids and versions that serve as keys stay readable; everything else,
|
|
28
|
+
* dependency tags included, is sealed. Each value is bound to its partition, kind and id as
|
|
29
|
+
* additional data, so a row copied into another slot or another partition does not decrypt. A row
|
|
30
|
+
* whose key is gone (a shredded partition, or a session key lost with the worker) reads as absent.
|
|
31
|
+
* Once `kill` runs, writes do nothing and reads find nothing, so a request that outlives a wipe
|
|
32
|
+
* cannot bring the store back.
|
|
33
|
+
*
|
|
34
|
+
* This is crypto-shredding for data at rest. Script running with the origin's privileges can ask
|
|
35
|
+
* the worker for the same data the page can, so it is not a defence against injected script.
|
|
36
|
+
*/
|
|
37
|
+
export declare function sealedStorage(inner: ReplicaStorage, options: SealOptions): SealedStorage;
|
package/dist/sw/seal.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
const decoder = new TextDecoder();
|
|
3
|
+
const generate = () => crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
|
|
4
|
+
/** keys held in memory only, both retentions; for tests and for a worker with no IndexedDB */
|
|
5
|
+
export function memoryVault() {
|
|
6
|
+
const keys = new Map();
|
|
7
|
+
let tail = Promise.resolve();
|
|
8
|
+
const serial = (fn) => {
|
|
9
|
+
const run = tail.then(fn, fn);
|
|
10
|
+
tail = run.catch(() => undefined);
|
|
11
|
+
return run;
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
get: (partition, retention, create) => serial(async () => {
|
|
15
|
+
const id = `${partition}\u0000${retention}`;
|
|
16
|
+
let key = keys.get(id);
|
|
17
|
+
if (!key && create) {
|
|
18
|
+
key = await generate();
|
|
19
|
+
keys.set(id, key);
|
|
20
|
+
}
|
|
21
|
+
return key;
|
|
22
|
+
}),
|
|
23
|
+
shred: (partition) => serial(async () => {
|
|
24
|
+
for (const id of [...keys.keys()])
|
|
25
|
+
if (id.startsWith(`${partition}\u0000`))
|
|
26
|
+
keys.delete(id);
|
|
27
|
+
})
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async function seal(key, aad, value, at) {
|
|
31
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
32
|
+
const data = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: encoder.encode(aad) }, key, encoder.encode(JSON.stringify(value)));
|
|
33
|
+
return { sealed: { iv, data, at } };
|
|
34
|
+
}
|
|
35
|
+
async function unseal(vault, partition, aad, row) {
|
|
36
|
+
const sealed = row?.sealed;
|
|
37
|
+
if (!sealed)
|
|
38
|
+
return undefined;
|
|
39
|
+
const key = await vault.get(partition, sealed.at, false);
|
|
40
|
+
if (!key)
|
|
41
|
+
return undefined;
|
|
42
|
+
try {
|
|
43
|
+
const plain = await crypto.subtle.decrypt({
|
|
44
|
+
name: 'AES-GCM',
|
|
45
|
+
iv: sealed.iv,
|
|
46
|
+
additionalData: encoder.encode(aad)
|
|
47
|
+
}, key, sealed.data);
|
|
48
|
+
return JSON.parse(decoder.decode(plain));
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Wraps a partition's storage so artifacts, records, projections and the replica meta are AES-GCM
|
|
56
|
+
* ciphertext at rest. The ids and versions that serve as keys stay readable; everything else,
|
|
57
|
+
* dependency tags included, is sealed. Each value is bound to its partition, kind and id as
|
|
58
|
+
* additional data, so a row copied into another slot or another partition does not decrypt. A row
|
|
59
|
+
* whose key is gone (a shredded partition, or a session key lost with the worker) reads as absent.
|
|
60
|
+
* Once `kill` runs, writes do nothing and reads find nothing, so a request that outlives a wipe
|
|
61
|
+
* cannot bring the store back.
|
|
62
|
+
*
|
|
63
|
+
* This is crypto-shredding for data at rest. Script running with the origin's privileges can ask
|
|
64
|
+
* the worker for the same data the page can, so it is not a defence against injected script.
|
|
65
|
+
*/
|
|
66
|
+
export function sealedStorage(inner, options) {
|
|
67
|
+
const { partition, vault } = options;
|
|
68
|
+
let dead = false;
|
|
69
|
+
const aad = (kind, id) => `${partition}\u0000${kind}\u0000${id}`;
|
|
70
|
+
async function sealWith(retention, kind, id, value) {
|
|
71
|
+
const key = await vault.get(partition, retention, true);
|
|
72
|
+
if (!key)
|
|
73
|
+
throw new Error('no key');
|
|
74
|
+
return seal(key, aad(kind, id), value, retention);
|
|
75
|
+
}
|
|
76
|
+
const open = (kind, id, row) => unseal(vault, partition, aad(kind, id), row);
|
|
77
|
+
return {
|
|
78
|
+
kill: () => void (dead = true),
|
|
79
|
+
async getArtifact(id) {
|
|
80
|
+
if (dead)
|
|
81
|
+
return undefined;
|
|
82
|
+
const row = await inner.getArtifact(id);
|
|
83
|
+
if (!row)
|
|
84
|
+
return undefined;
|
|
85
|
+
const value = await open('artifact', id, row);
|
|
86
|
+
if (!value)
|
|
87
|
+
await inner.deleteArtifacts([id]);
|
|
88
|
+
return value;
|
|
89
|
+
},
|
|
90
|
+
async putArtifact(stored) {
|
|
91
|
+
if (dead)
|
|
92
|
+
return;
|
|
93
|
+
const id = stored.artifact.id;
|
|
94
|
+
const row = await sealWith(options.retentionOf(id), 'artifact', id, stored);
|
|
95
|
+
if (dead)
|
|
96
|
+
return;
|
|
97
|
+
await inner.putArtifact({ artifact: { id }, ...row });
|
|
98
|
+
},
|
|
99
|
+
deleteArtifacts: (ids) => (dead ? Promise.resolve() : inner.deleteArtifacts(ids)),
|
|
100
|
+
async allArtifacts() {
|
|
101
|
+
if (dead)
|
|
102
|
+
return [];
|
|
103
|
+
const out = [];
|
|
104
|
+
const lost = [];
|
|
105
|
+
for (const row of await inner.allArtifacts()) {
|
|
106
|
+
const id = row.artifact.id;
|
|
107
|
+
const value = await open('artifact', id, row);
|
|
108
|
+
if (value)
|
|
109
|
+
out.push(value);
|
|
110
|
+
else
|
|
111
|
+
lost.push(id);
|
|
112
|
+
}
|
|
113
|
+
if (lost.length > 0)
|
|
114
|
+
await inner.deleteArtifacts(lost);
|
|
115
|
+
return out;
|
|
116
|
+
},
|
|
117
|
+
clearArtifacts: () => (dead ? Promise.resolve() : inner.clearArtifacts()),
|
|
118
|
+
async getMeta() {
|
|
119
|
+
if (dead)
|
|
120
|
+
return undefined;
|
|
121
|
+
return open('meta', 'replica', await inner.getMeta());
|
|
122
|
+
},
|
|
123
|
+
async setMeta(meta) {
|
|
124
|
+
if (dead)
|
|
125
|
+
return;
|
|
126
|
+
const row = await sealWith(options.shared, 'meta', 'replica', meta);
|
|
127
|
+
if (!dead)
|
|
128
|
+
await inner.setMeta(row);
|
|
129
|
+
},
|
|
130
|
+
addBeacon: (event) => inner.addBeacon(event),
|
|
131
|
+
readBeacons: () => inner.readBeacons(),
|
|
132
|
+
removeBeacons: (keys) => inner.removeBeacons(keys),
|
|
133
|
+
getFlush: () => inner.getFlush(),
|
|
134
|
+
setFlush: (state) => inner.setFlush(state),
|
|
135
|
+
async putRecords(entries) {
|
|
136
|
+
if (dead)
|
|
137
|
+
return;
|
|
138
|
+
const rows = await Promise.all(entries.map(async (e) => ({
|
|
139
|
+
id: e.id,
|
|
140
|
+
version: e.version,
|
|
141
|
+
...(await sealWith(options.shared, 'record', e.id, e))
|
|
142
|
+
})));
|
|
143
|
+
if (!dead)
|
|
144
|
+
await inner.putRecords(rows);
|
|
145
|
+
},
|
|
146
|
+
async allRecords() {
|
|
147
|
+
if (dead)
|
|
148
|
+
return [];
|
|
149
|
+
const out = [];
|
|
150
|
+
for (const row of await inner.allRecords()) {
|
|
151
|
+
const value = await open('record', row.id, row);
|
|
152
|
+
if (value)
|
|
153
|
+
out.push(value);
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
},
|
|
157
|
+
deleteRecords: (ids) => (dead ? Promise.resolve() : inner.deleteRecords(ids)),
|
|
158
|
+
async putProjections(entries) {
|
|
159
|
+
if (dead)
|
|
160
|
+
return;
|
|
161
|
+
const rows = await Promise.all(entries.map(async (e) => ({
|
|
162
|
+
id: e.id,
|
|
163
|
+
version: e.version,
|
|
164
|
+
...(await sealWith(options.shared, 'projection', e.id, e))
|
|
165
|
+
})));
|
|
166
|
+
if (!dead)
|
|
167
|
+
await inner.putProjections(rows);
|
|
168
|
+
},
|
|
169
|
+
async allProjections() {
|
|
170
|
+
if (dead)
|
|
171
|
+
return [];
|
|
172
|
+
const out = [];
|
|
173
|
+
for (const row of await inner.allProjections()) {
|
|
174
|
+
const value = await open('projection', row.id, row);
|
|
175
|
+
if (value)
|
|
176
|
+
out.push(value);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
},
|
|
180
|
+
deleteProjections: (ids) => (dead ? Promise.resolve() : inner.deleteProjections(ids)),
|
|
181
|
+
clearGraph: () => (dead ? Promise.resolve() : inner.clearGraph())
|
|
182
|
+
};
|
|
183
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SwConfig } from '../protocol/config.js';
|
|
2
|
+
import type { Renderers } from './render.js';
|
|
3
|
+
/** the runtime version of a worker that has no server; a new value empties what an old one stored */
|
|
4
|
+
export declare const STATIC_RUNTIME = "static-1";
|
|
5
|
+
/** the file a static site serves beside `spool-sw.js` */
|
|
6
|
+
export declare const MANIFEST_FILE = "spool.json";
|
|
7
|
+
/** the config of a worker with no spool server: its routes come from `spool.json` next to `workerUrl` */
|
|
8
|
+
export declare function manifestConfig(workerUrl: string): SwConfig;
|
|
9
|
+
/**
|
|
10
|
+
* Starts the worker: registers the install, activate, fetch and message handlers on the worker
|
|
11
|
+
* scope. `renderers` replaces the built-in render for the route patterns it names. The shared
|
|
12
|
+
* core calls this with the config the server prepended; a site that bundles its own worker calls
|
|
13
|
+
* it directly.
|
|
14
|
+
*/
|
|
15
|
+
export declare function serve(config: SwConfig, renderers?: Renderers): void;
|
package/dist/sw/serve.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { PROTOCOL_VERSION } from '../protocol/artifact.js';
|
|
2
|
+
import { idbStorage } from './idb.js';
|
|
3
|
+
import { idbPartitions } from './partition.js';
|
|
4
|
+
import { createRuntime } from './runtime.js';
|
|
5
|
+
/** the runtime version of a worker that has no server; a new value empties what an old one stored */
|
|
6
|
+
export const STATIC_RUNTIME = 'static-1';
|
|
7
|
+
/** the file a static site serves beside `spool-sw.js` */
|
|
8
|
+
export const MANIFEST_FILE = 'spool.json';
|
|
9
|
+
/** the config of a worker with no spool server: its routes come from `spool.json` next to `workerUrl` */
|
|
10
|
+
export function manifestConfig(workerUrl) {
|
|
11
|
+
return {
|
|
12
|
+
protocol: PROTOCOL_VERSION,
|
|
13
|
+
version: STATIC_RUNTIME,
|
|
14
|
+
routes: [],
|
|
15
|
+
templates: {},
|
|
16
|
+
beacons: [],
|
|
17
|
+
clientTag: '',
|
|
18
|
+
views: false,
|
|
19
|
+
manifest: new URL(MANIFEST_FILE, workerUrl).href
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Starts the worker: registers the install, activate, fetch and message handlers on the worker
|
|
24
|
+
* scope. `renderers` replaces the built-in render for the route patterns it names. The shared
|
|
25
|
+
* core calls this with the config the server prepended; a site that bundles its own worker calls
|
|
26
|
+
* it directly.
|
|
27
|
+
*/
|
|
28
|
+
export function serve(config, renderers) {
|
|
29
|
+
const scope = self;
|
|
30
|
+
const runtime = createRuntime({
|
|
31
|
+
config,
|
|
32
|
+
storage: idbStorage(),
|
|
33
|
+
fetch: (input, init) => fetch(input, init),
|
|
34
|
+
caches,
|
|
35
|
+
origin: scope.location.origin,
|
|
36
|
+
renderers,
|
|
37
|
+
partitions: typeof indexedDB === 'undefined' ? undefined : idbPartitions(),
|
|
38
|
+
broadcast: (message) => void scope.clients
|
|
39
|
+
.matchAll({ type: 'window', includeUncontrolled: true })
|
|
40
|
+
.then((all) => all.forEach((client) => client.postMessage(message)))
|
|
41
|
+
});
|
|
42
|
+
scope.addEventListener('install', (event) => event.waitUntil(Promise.all([
|
|
43
|
+
scope.skipWaiting(),
|
|
44
|
+
runtime.install(event)
|
|
45
|
+
])));
|
|
46
|
+
scope.addEventListener('activate', (event) => event.waitUntil(Promise.all([runtime.activate(), scope.clients.claim()])));
|
|
47
|
+
scope.addEventListener('fetch', (event) => {
|
|
48
|
+
const response = runtime.handle(event.request, (promise) => event.waitUntil(promise));
|
|
49
|
+
if (response)
|
|
50
|
+
event.respondWith(response);
|
|
51
|
+
});
|
|
52
|
+
scope.addEventListener('message', (event) => {
|
|
53
|
+
event.waitUntil(runtime.message(event.data).then((reply) => {
|
|
54
|
+
if (!reply)
|
|
55
|
+
return;
|
|
56
|
+
const port = event.ports[0];
|
|
57
|
+
if (port)
|
|
58
|
+
port.postMessage(reply);
|
|
59
|
+
else
|
|
60
|
+
event.source?.postMessage(reply);
|
|
61
|
+
}));
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SwConfig } from '../protocol/config.js';
|
|
2
|
+
import type { SerializedRoute } from '../protocol/route.js';
|
|
3
|
+
import type { Mode } from '../protocol/view.js';
|
|
4
|
+
/** the mode a route runs in: its own, else the server's, else `serve` */
|
|
5
|
+
export declare function modeFor(config: SwConfig, route: SerializedRoute): Mode;
|
|
6
|
+
/**
|
|
7
|
+
* Whether this view runs in shadow: always on a shadow route, and on `shadowSample` of the views
|
|
8
|
+
* of a serve route. A sampled view goes to the network like a shadow one, which is what the sample costs.
|
|
9
|
+
*/
|
|
10
|
+
export declare function runsShadow(config: SwConfig, route: SerializedRoute, random: () => number): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Whether the network's answer differs from the local render after normalisation. Anything but a
|
|
13
|
+
* 200 counts as a difference, since local would have shown a page where the origin did not. Reads
|
|
14
|
+
* the body, so pass a clone.
|
|
15
|
+
*/
|
|
16
|
+
export declare function differs(local: string, network: Response): Promise<boolean>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { equivalent } from '../protocol/normalise.js';
|
|
2
|
+
/** the mode a route runs in: its own, else the server's, else `serve` */
|
|
3
|
+
export function modeFor(config, route) {
|
|
4
|
+
return route.mode ?? config.mode ?? 'serve';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Whether this view runs in shadow: always on a shadow route, and on `shadowSample` of the views
|
|
8
|
+
* of a serve route. A sampled view goes to the network like a shadow one, which is what the sample costs.
|
|
9
|
+
*/
|
|
10
|
+
export function runsShadow(config, route, random) {
|
|
11
|
+
if (route.strategy === 'static')
|
|
12
|
+
return false;
|
|
13
|
+
if (modeFor(config, route) === 'shadow')
|
|
14
|
+
return true;
|
|
15
|
+
const sample = config.shadowSample ?? 0;
|
|
16
|
+
return sample > 0 && random() < sample;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Whether the network's answer differs from the local render after normalisation. Anything but a
|
|
20
|
+
* 200 counts as a difference, since local would have shown a page where the origin did not. Reads
|
|
21
|
+
* the body, so pass a clone.
|
|
22
|
+
*/
|
|
23
|
+
export async function differs(local, network) {
|
|
24
|
+
if (network.status !== 200)
|
|
25
|
+
return true;
|
|
26
|
+
return !equivalent(local, await network.text()).equal;
|
|
27
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** prefix of the Cache that holds static bytes; the runtime version completes the name */
|
|
2
|
+
export declare const STATIC_CACHE = "spool-static-";
|
|
3
|
+
/** largest file the worker keeps; a bigger response goes to the network every time */
|
|
4
|
+
export declare const MAX_STATIC_BYTES: number;
|
|
5
|
+
/** the headers to store with a file: everything but {@link TRANSFER} */
|
|
6
|
+
export declare function replayHeaders(headers: Headers): Headers;
|
|
7
|
+
/**
|
|
8
|
+
* Reads a body up to `cap` bytes. A longer body, announced by `content-length` or found by
|
|
9
|
+
* counting, gives null and the rest is not read.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readCapped(response: Response, cap: number): Promise<Uint8Array | null>;
|
|
12
|
+
/**
|
|
13
|
+
* Whether `bytes` are the file a manifest `version` names. A `sha256-`, `sha384-` or `sha512-`
|
|
14
|
+
* version must equal the digest of the bytes, and an entity tag must equal the response's `ETag`.
|
|
15
|
+
* Any other version is a label the site chose, which nothing can check.
|
|
16
|
+
*/
|
|
17
|
+
export declare function matchesVersion(version: string, bytes: Uint8Array, headers: Headers): Promise<boolean>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** prefix of the Cache that holds static bytes; the runtime version completes the name */
|
|
2
|
+
export const STATIC_CACHE = 'spool-static-';
|
|
3
|
+
/** largest file the worker keeps; a bigger response goes to the network every time */
|
|
4
|
+
export const MAX_STATIC_BYTES = 4 * 1024 * 1024;
|
|
5
|
+
/** response headers that describe the transfer or the connection, not the file, so they are not replayed */
|
|
6
|
+
const TRANSFER = [
|
|
7
|
+
'content-encoding',
|
|
8
|
+
'content-length',
|
|
9
|
+
'transfer-encoding',
|
|
10
|
+
'connection',
|
|
11
|
+
'keep-alive',
|
|
12
|
+
'set-cookie',
|
|
13
|
+
'age',
|
|
14
|
+
'date',
|
|
15
|
+
'vary'
|
|
16
|
+
];
|
|
17
|
+
/** the headers to store with a file: everything but {@link TRANSFER} */
|
|
18
|
+
export function replayHeaders(headers) {
|
|
19
|
+
const out = new Headers(headers);
|
|
20
|
+
for (const name of TRANSFER)
|
|
21
|
+
out.delete(name);
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Reads a body up to `cap` bytes. A longer body, announced by `content-length` or found by
|
|
26
|
+
* counting, gives null and the rest is not read.
|
|
27
|
+
*/
|
|
28
|
+
export async function readCapped(response, cap) {
|
|
29
|
+
if (Number(response.headers.get('content-length')) > cap)
|
|
30
|
+
return null;
|
|
31
|
+
const reader = response.body?.getReader();
|
|
32
|
+
if (!reader)
|
|
33
|
+
return new Uint8Array();
|
|
34
|
+
const chunks = [];
|
|
35
|
+
let total = 0;
|
|
36
|
+
for (;;) {
|
|
37
|
+
const { done, value } = await reader.read();
|
|
38
|
+
if (done)
|
|
39
|
+
break;
|
|
40
|
+
total += value.byteLength;
|
|
41
|
+
if (total > cap) {
|
|
42
|
+
// not awaited: a cancelled tee branch settles only once the other branch is read or cancelled
|
|
43
|
+
void reader.cancel().catch(() => undefined);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
chunks.push(value);
|
|
47
|
+
}
|
|
48
|
+
const out = new Uint8Array(total);
|
|
49
|
+
let at = 0;
|
|
50
|
+
for (const chunk of chunks) {
|
|
51
|
+
out.set(chunk, at);
|
|
52
|
+
at += chunk.byteLength;
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
const HASH = /^sha(256|384|512)-([A-Za-z0-9+/]+={0,2})$/;
|
|
57
|
+
const TAG = /^(?:W\/)?"/;
|
|
58
|
+
function base64(bytes) {
|
|
59
|
+
let text = '';
|
|
60
|
+
for (const byte of bytes)
|
|
61
|
+
text += String.fromCharCode(byte);
|
|
62
|
+
return btoa(text);
|
|
63
|
+
}
|
|
64
|
+
const stripWeak = (tag) => tag.replace(/^W\//, '');
|
|
65
|
+
/**
|
|
66
|
+
* Whether `bytes` are the file a manifest `version` names. A `sha256-`, `sha384-` or `sha512-`
|
|
67
|
+
* version must equal the digest of the bytes, and an entity tag must equal the response's `ETag`.
|
|
68
|
+
* Any other version is a label the site chose, which nothing can check.
|
|
69
|
+
*/
|
|
70
|
+
export async function matchesVersion(version, bytes, headers) {
|
|
71
|
+
const hash = HASH.exec(version);
|
|
72
|
+
if (hash) {
|
|
73
|
+
const digest = await crypto.subtle.digest(`SHA-${hash[1]}`, bytes);
|
|
74
|
+
return base64(new Uint8Array(digest)) === hash[2];
|
|
75
|
+
}
|
|
76
|
+
if (TAG.test(version)) {
|
|
77
|
+
const tag = headers.get('etag');
|
|
78
|
+
return tag !== null && stripWeak(tag) === stripWeak(version);
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|