@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,1070 @@
|
|
|
1
|
+
import { parseArtifact, PROTOCOL_VERSION } from '../protocol/artifact.js';
|
|
2
|
+
import { MAX_WANT } from '../protocol/beacon.js';
|
|
3
|
+
import { applyStamp } from '../protocol/collector.js';
|
|
4
|
+
import { PATHS } from '../protocol/config.js';
|
|
5
|
+
import { decodeEnvelope, decodeEnvelopeOrClear, HEADERS } from '../protocol/envelope.js';
|
|
6
|
+
import { manifestRoutes, parseManifest } from '../protocol/manifest.js';
|
|
7
|
+
import { matchRoute } from '../protocol/route.js';
|
|
8
|
+
import { encodeHeld, parseSyncResponse } from '../protocol/sync.js';
|
|
9
|
+
import { engineOf, FROM_EXTERNAL, loadsToBeacon, REASON_TIER, viewToBeacon } from '../protocol/view.js';
|
|
10
|
+
import { BeaconQueue, LOGOUT_SEND_MS } from './analytics.js';
|
|
11
|
+
import { collectorAnswer } from './collect.js';
|
|
12
|
+
import { verdict } from './freshness.js';
|
|
13
|
+
import { policyHeaders } from './policy.js';
|
|
14
|
+
import { Ledger, plan, priorFrom } from './provision.js';
|
|
15
|
+
import { renderLocal } from './render.js';
|
|
16
|
+
import { Replica } from './replica.js';
|
|
17
|
+
import { classify } from './router.js';
|
|
18
|
+
import { installRoutes } from './routing.js';
|
|
19
|
+
import { Scheduler } from './scheduler.js';
|
|
20
|
+
import { differs, modeFor, runsShadow } from './shadow.js';
|
|
21
|
+
import { matchesVersion, MAX_STATIC_BYTES, readCapped, replayHeaders, STATIC_CACHE } from './static.js';
|
|
22
|
+
import { THIRD_CACHE, THIRD_HEADERS, versionClass } from './thirdparty.js';
|
|
23
|
+
/** the strategies a catalogue entry may have: data for a template or renderer the device holds */
|
|
24
|
+
const ENTRY_STRATEGIES = ['template', 'projection', 'data'];
|
|
25
|
+
const ASSET_CACHE = 'spool-assets-';
|
|
26
|
+
const CACHE_PREFIXES = [ASSET_CACHE, STATIC_CACHE];
|
|
27
|
+
const YEAR = 31_536_000;
|
|
28
|
+
const DAY_MS = 86_400_000;
|
|
29
|
+
const encoder = new TextEncoder();
|
|
30
|
+
/** a static file's key is its pathname plus query string; this is the pathname a route matches */
|
|
31
|
+
const pathOf = (key) => key.split('?', 1)[0];
|
|
32
|
+
function immutable(response) {
|
|
33
|
+
const control = response.headers.get('cache-control') ?? '';
|
|
34
|
+
if (/(?:^|,)\s*immutable\b/i.test(control))
|
|
35
|
+
return true;
|
|
36
|
+
const age = /max-age=(\d+)/i.exec(control);
|
|
37
|
+
return age !== null && Number(age[1]) >= YEAR;
|
|
38
|
+
}
|
|
39
|
+
/** builds the request pipeline: local render, sync, provisioning, asset cache and beacon queue */
|
|
40
|
+
export function createRuntime(deps) {
|
|
41
|
+
const { config, storage, origin, renderers } = deps;
|
|
42
|
+
const host = deps.partitions;
|
|
43
|
+
const now = deps.now ?? Date.now;
|
|
44
|
+
const random = deps.random ?? Math.random;
|
|
45
|
+
const engine = engineOf(deps.userAgent ?? (typeof navigator === 'undefined' ? '' : navigator.userAgent));
|
|
46
|
+
const prior = priorFrom(config.provision);
|
|
47
|
+
const replica = new Replica(storage, config.version, now);
|
|
48
|
+
const ledger = new Ledger();
|
|
49
|
+
const background = new Set();
|
|
50
|
+
const provisioning = new Map();
|
|
51
|
+
const declined = new Set();
|
|
52
|
+
const wants = new Set();
|
|
53
|
+
const asked = new Set();
|
|
54
|
+
let currentPath = null;
|
|
55
|
+
const latestView = new Map();
|
|
56
|
+
const assetCache = `${ASSET_CACHE}${config.version}`;
|
|
57
|
+
const staticCache = `${STATIC_CACHE}${config.version}`;
|
|
58
|
+
const keyOf = (path) => new Request(`${origin}${path}`);
|
|
59
|
+
const startedAt = now();
|
|
60
|
+
const assetMode = config.assets ?? 'cache';
|
|
61
|
+
let routed = false;
|
|
62
|
+
let lastViewAt = null;
|
|
63
|
+
let loaded;
|
|
64
|
+
let manifestLoad;
|
|
65
|
+
let partitionLoad;
|
|
66
|
+
let principal = null;
|
|
67
|
+
let exclusive = Promise.resolve();
|
|
68
|
+
const observing = config.mode === 'shadow';
|
|
69
|
+
const manifestPending = () => config.manifest !== undefined && config.routes.length === 0;
|
|
70
|
+
const handles = new Map();
|
|
71
|
+
/** opens a cache once and keeps the handle, so a hot request does not reopen it */
|
|
72
|
+
const cacheNamed = (name) => {
|
|
73
|
+
let handle = handles.get(name);
|
|
74
|
+
if (!handle) {
|
|
75
|
+
handle = deps.caches.open(name);
|
|
76
|
+
handles.set(name, handle);
|
|
77
|
+
handle.catch(() => handles.delete(name));
|
|
78
|
+
}
|
|
79
|
+
return handle;
|
|
80
|
+
};
|
|
81
|
+
const ready = async () => {
|
|
82
|
+
await (loaded ??= replica.load());
|
|
83
|
+
if (manifestPending())
|
|
84
|
+
await (manifestLoad ??= loadManifest());
|
|
85
|
+
if (host)
|
|
86
|
+
await (partitionLoad ??= loadPartition());
|
|
87
|
+
};
|
|
88
|
+
const track = (promise) => {
|
|
89
|
+
const settled = promise.then(() => undefined, () => undefined);
|
|
90
|
+
background.add(settled);
|
|
91
|
+
void settled.then(() => background.delete(settled));
|
|
92
|
+
};
|
|
93
|
+
const settleBackground = async () => {
|
|
94
|
+
while (background.size > 0)
|
|
95
|
+
await Promise.allSettled([...background]);
|
|
96
|
+
};
|
|
97
|
+
/** runs `fn` after every earlier partition change, so two responses never swap the store at once */
|
|
98
|
+
const serial = (fn) => {
|
|
99
|
+
const run = exclusive.then(fn, fn);
|
|
100
|
+
exclusive = run.catch(() => undefined);
|
|
101
|
+
return run;
|
|
102
|
+
};
|
|
103
|
+
async function fetchSync(from, need) {
|
|
104
|
+
if (config.manifest)
|
|
105
|
+
return refreshManifest();
|
|
106
|
+
const query = need ? `need=${encodeURIComponent(encodeHeld(need))}` : `from=${from}`;
|
|
107
|
+
const response = await deps.fetch(`${origin}${PATHS.sync}?${query}`, {
|
|
108
|
+
cache: 'no-store',
|
|
109
|
+
credentials: 'same-origin'
|
|
110
|
+
});
|
|
111
|
+
if (!response.ok)
|
|
112
|
+
throw new Error(`sync answered ${response.status}`);
|
|
113
|
+
// the answer names who the server sees, so a sign-in the worker never saw ends here
|
|
114
|
+
await early(decodeEnvelopeOrClear(response.headers), true);
|
|
115
|
+
return parseSyncResponse(await response.json());
|
|
116
|
+
}
|
|
117
|
+
const policy = {
|
|
118
|
+
changesPerDay: prior.changesPerDay,
|
|
119
|
+
staleBytes: prior.avoidedBytes,
|
|
120
|
+
syncBytes: prior.requestBytes
|
|
121
|
+
};
|
|
122
|
+
/** paths to ask the next flush for: visited routes in shadow, then the hot list, best first */
|
|
123
|
+
async function wantList() {
|
|
124
|
+
const out = [];
|
|
125
|
+
for (const path of wants)
|
|
126
|
+
if (!(await replica.get(path)))
|
|
127
|
+
out.push(path);
|
|
128
|
+
const hot = [];
|
|
129
|
+
for (const path of config.provision?.hot ?? []) {
|
|
130
|
+
if (asked.has(path) || out.includes(path) || (await replica.get(path)))
|
|
131
|
+
continue;
|
|
132
|
+
const hit = matchRoute(config.routes, path);
|
|
133
|
+
if (!hit || hit.route.scope !== 'public' || hit.route.strategy === 'static')
|
|
134
|
+
continue;
|
|
135
|
+
hot.push({
|
|
136
|
+
id: path,
|
|
137
|
+
kind: 'route',
|
|
138
|
+
provision: { bytes: ledger.meanFetchBytes() ?? prior.artifactBytes, requests: 0 },
|
|
139
|
+
avoided: { bytes: prior.avoidedBytes, requests: 1 }
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return [...out, ...plan(hot, prior).map((o) => o.id)].slice(0, MAX_WANT);
|
|
143
|
+
}
|
|
144
|
+
async function accept(artifact, bytes, requests) {
|
|
145
|
+
const hit = matchRoute(config.routes, artifact.id);
|
|
146
|
+
if (!hit || artifact.scope !== 'public' || artifact.strategy !== hit.route.strategy)
|
|
147
|
+
return;
|
|
148
|
+
if (await replica.put(artifact, hit.route.persist)) {
|
|
149
|
+
ledger.spend(artifact.id, { bytes, requests });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** the page the last view was on, when its route declares a catalogue; the queue skips a recent ask */
|
|
153
|
+
const catalogueAsk = () => currentPath !== null ? { path: currentPath, sweep: true } : undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Stores the entries a flush brought, each marked unopened. An entry is kept only for a route
|
|
156
|
+
* whose strategy and scope match it. A principal entry is kept only when the answer names the
|
|
157
|
+
* partition the worker holds, and one the device already holds is left as it is. Every entry is
|
|
158
|
+
* charged to the ledger as received.
|
|
159
|
+
*/
|
|
160
|
+
async function acceptCatalogue(list, partition) {
|
|
161
|
+
if (!Array.isArray(list))
|
|
162
|
+
return;
|
|
163
|
+
for (const raw of list) {
|
|
164
|
+
let artifact;
|
|
165
|
+
try {
|
|
166
|
+
artifact = parseArtifact(raw);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
ledger.spend(artifact.id, { bytes: encoder.encode(JSON.stringify(raw)).length, requests: 0 }, true);
|
|
172
|
+
const hit = matchRoute(config.routes, artifact.id);
|
|
173
|
+
if (!hit || !ENTRY_STRATEGIES.includes(artifact.strategy))
|
|
174
|
+
continue;
|
|
175
|
+
if (artifact.strategy !== hit.route.strategy || artifact.scope !== hit.route.scope)
|
|
176
|
+
continue;
|
|
177
|
+
let side = publicSide;
|
|
178
|
+
if (artifact.scope === 'principal') {
|
|
179
|
+
side = await principalSide();
|
|
180
|
+
if (!side || side.id !== partition)
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (await side.replica.get(artifact.id))
|
|
184
|
+
continue;
|
|
185
|
+
await side.replica.put(artifact, hit.route.persist, true);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async function replay(event) {
|
|
189
|
+
const entry = config.collectors?.find((c) => c.vendor === event.vendor);
|
|
190
|
+
let search = event.search ?? '';
|
|
191
|
+
if (entry?.stamp)
|
|
192
|
+
search = applyStamp(search, entry.stamp, event.at, now());
|
|
193
|
+
const method = event.method ?? 'POST';
|
|
194
|
+
const init = {
|
|
195
|
+
method,
|
|
196
|
+
credentials: event.origin ? 'omit' : 'same-origin'
|
|
197
|
+
};
|
|
198
|
+
if (method === 'POST') {
|
|
199
|
+
init.body = event.body;
|
|
200
|
+
if (event.contentType)
|
|
201
|
+
init.headers = { 'content-type': event.contentType };
|
|
202
|
+
}
|
|
203
|
+
if (event.origin) {
|
|
204
|
+
const type = event.contentType.split(';')[0]?.trim().toLowerCase() ?? '';
|
|
205
|
+
const simple = ['', 'text/plain', 'application/x-www-form-urlencoded'].includes(type);
|
|
206
|
+
init.mode = simple ? 'no-cors' : 'cors';
|
|
207
|
+
}
|
|
208
|
+
const response = await deps.fetch(`${event.origin ?? origin}${event.path}${search}`, init);
|
|
209
|
+
return (response.type === 'opaque' ||
|
|
210
|
+
response.ok ||
|
|
211
|
+
(response.status >= 400 && response.status < 500));
|
|
212
|
+
}
|
|
213
|
+
const analytics = new BeaconQueue({
|
|
214
|
+
storage,
|
|
215
|
+
now,
|
|
216
|
+
want: wantList,
|
|
217
|
+
catalogue: catalogueAsk,
|
|
218
|
+
replay,
|
|
219
|
+
async send(batch) {
|
|
220
|
+
const response = await deps.fetch(`${origin}${PATHS.beacon}`, {
|
|
221
|
+
method: 'POST',
|
|
222
|
+
headers: { 'content-type': 'application/json' },
|
|
223
|
+
body: JSON.stringify(batch),
|
|
224
|
+
credentials: 'same-origin'
|
|
225
|
+
});
|
|
226
|
+
if (response.status === 200 && (batch.want || batch.catalogue)) {
|
|
227
|
+
const body = (await response.json());
|
|
228
|
+
if (batch.want) {
|
|
229
|
+
const list = Array.isArray(body.artifacts) ? body.artifacts : [];
|
|
230
|
+
for (const raw of list) {
|
|
231
|
+
const artifact = parseArtifact(raw);
|
|
232
|
+
await accept(artifact, encoder.encode(JSON.stringify(raw)).length, 0);
|
|
233
|
+
}
|
|
234
|
+
for (const path of batch.want) {
|
|
235
|
+
wants.delete(path);
|
|
236
|
+
asked.add(path);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (batch.catalogue) {
|
|
240
|
+
await acceptCatalogue(body.catalogue, response.headers.get(HEADERS.partition));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return response.ok || (response.status >= 400 && response.status < 500);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
const makeScheduler = (target) => new Scheduler({
|
|
247
|
+
replica: target,
|
|
248
|
+
fetchSync,
|
|
249
|
+
onNetwork: () => track(analytics.flushAtMoment()),
|
|
250
|
+
policy
|
|
251
|
+
});
|
|
252
|
+
const publicSide = {
|
|
253
|
+
id: null,
|
|
254
|
+
replica,
|
|
255
|
+
scheduler: makeScheduler(replica),
|
|
256
|
+
deferred: new Set(),
|
|
257
|
+
loaded: Promise.resolve()
|
|
258
|
+
};
|
|
259
|
+
const scheduler = publicSide.scheduler;
|
|
260
|
+
// #region partitions
|
|
261
|
+
const retentionOf = (id) => matchRoute(config.routes, id)?.route.retention ?? 'session';
|
|
262
|
+
const sharedRetention = () => config.routes.some((r) => r.scope === 'principal' && r.retention === 'persistent')
|
|
263
|
+
? 'persistent'
|
|
264
|
+
: 'session';
|
|
265
|
+
function makeSide(id) {
|
|
266
|
+
const sealed = host.open(id, retentionOf, sharedRetention());
|
|
267
|
+
const target = new Replica(sealed, config.version, now);
|
|
268
|
+
return {
|
|
269
|
+
id,
|
|
270
|
+
replica: target,
|
|
271
|
+
scheduler: makeScheduler(target),
|
|
272
|
+
deferred: new Set(),
|
|
273
|
+
loaded: target.load()
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
async function loadPartition() {
|
|
277
|
+
const id = await host.current();
|
|
278
|
+
if (id && !principal)
|
|
279
|
+
principal = makeSide(id);
|
|
280
|
+
}
|
|
281
|
+
/** the side of the partition in use, loaded; null when no principal is signed in as far as the worker knows */
|
|
282
|
+
async function principalSide() {
|
|
283
|
+
await ready();
|
|
284
|
+
const side = principal;
|
|
285
|
+
if (!side)
|
|
286
|
+
return null;
|
|
287
|
+
await side.loaded;
|
|
288
|
+
return side;
|
|
289
|
+
}
|
|
290
|
+
/** shuts the principal store, shreds its keys and deletes it; true when there was one */
|
|
291
|
+
function wipePartition(reason) {
|
|
292
|
+
return serial(async () => {
|
|
293
|
+
await ready();
|
|
294
|
+
const old = principal;
|
|
295
|
+
principal = null;
|
|
296
|
+
if (!old || !host)
|
|
297
|
+
return false;
|
|
298
|
+
await host.setCurrent(null);
|
|
299
|
+
await host.destroy(old.id);
|
|
300
|
+
deps.broadcast?.({ type: 'spool:cleared', reason });
|
|
301
|
+
return true;
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
/** makes `id` the partition in use; the one before it is destroyed first */
|
|
305
|
+
function switchTo(id) {
|
|
306
|
+
return serial(async () => {
|
|
307
|
+
await ready();
|
|
308
|
+
const old = principal;
|
|
309
|
+
if (old?.id === id)
|
|
310
|
+
return;
|
|
311
|
+
principal = makeSide(id);
|
|
312
|
+
await host.setCurrent(id);
|
|
313
|
+
if (old) {
|
|
314
|
+
await host.destroy(old.id);
|
|
315
|
+
deps.broadcast?.({ type: 'spool:cleared', reason: 'partition' });
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* What a response says about who is signed in, applied before the response is handed back so no
|
|
321
|
+
* later view reads a store the response outdated. `Spool-Clear` wipes everything or one
|
|
322
|
+
* partition; a different `Spool-Partition` replaces the store; a navigation the server decorated
|
|
323
|
+
* with no partition means the session ended. A sync answer is read the same way, so the first
|
|
324
|
+
* required sync after a sign-in the worker never saw swaps the store before its result is used.
|
|
325
|
+
*/
|
|
326
|
+
async function early(envelope, navigation, signedOut = false) {
|
|
327
|
+
if (!host || !envelope || envelope.protocol !== PROTOCOL_VERSION)
|
|
328
|
+
return;
|
|
329
|
+
await ready();
|
|
330
|
+
const clear = envelope.clear ?? [];
|
|
331
|
+
if (clear.includes('*') || (principal && clear.includes(principal.id))) {
|
|
332
|
+
await wipePartition('clear');
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
// the answer to a logout request still names the identity of the request it answered
|
|
336
|
+
if (signedOut)
|
|
337
|
+
return;
|
|
338
|
+
if (envelope.partition !== undefined) {
|
|
339
|
+
if (principal?.id !== envelope.partition)
|
|
340
|
+
await switchTo(envelope.partition);
|
|
341
|
+
}
|
|
342
|
+
else if (navigation && principal)
|
|
343
|
+
await wipePartition('session-ended');
|
|
344
|
+
}
|
|
345
|
+
// #endregion
|
|
346
|
+
async function provision(path, route, partition) {
|
|
347
|
+
const side = route.scope === 'principal' ? await principalSide() : publicSide;
|
|
348
|
+
if (!side || (route.scope === 'principal' && side.id !== partition))
|
|
349
|
+
return;
|
|
350
|
+
const have = await side.replica.held();
|
|
351
|
+
const query = Object.keys(have).length > 0 ? `&have=${encodeURIComponent(encodeHeld(have))}` : '';
|
|
352
|
+
const response = await deps.fetch(`${origin}${PATHS.artifact}?path=${encodeURIComponent(path)}${query}`, { cache: 'no-store', credentials: 'same-origin' });
|
|
353
|
+
if (response.status === 204 && response.headers.get(HEADERS.provision) === 'declined') {
|
|
354
|
+
declined.add(path);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (!response.ok) {
|
|
358
|
+
if (route.scope === 'principal' && [401, 403, 404].includes(response.status)) {
|
|
359
|
+
await side.replica.drop([path]);
|
|
360
|
+
}
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const text = await response.text();
|
|
364
|
+
const artifact = parseArtifact(JSON.parse(text));
|
|
365
|
+
if (artifact.scope !== route.scope || artifact.id !== path)
|
|
366
|
+
return;
|
|
367
|
+
if (artifact.strategy !== route.strategy)
|
|
368
|
+
return;
|
|
369
|
+
if (route.scope === 'principal') {
|
|
370
|
+
if (response.headers.get(HEADERS.partition) !== partition)
|
|
371
|
+
return;
|
|
372
|
+
if (principal !== side)
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (await side.replica.put(artifact, route.persist)) {
|
|
376
|
+
ledger.spend(path, { bytes: encoder.encode(text).length, requests: 1 });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
async function drainDeferred(side) {
|
|
380
|
+
const paths = [...side.deferred];
|
|
381
|
+
side.deferred.clear();
|
|
382
|
+
if (await side.scheduler.drain())
|
|
383
|
+
for (const path of paths)
|
|
384
|
+
await side.replica.validate(path);
|
|
385
|
+
}
|
|
386
|
+
async function observe(headers, envelope, sent, navigation, moment, provisionable, keep) {
|
|
387
|
+
await keep();
|
|
388
|
+
if (envelope && envelope.protocol === PROTOCOL_VERSION) {
|
|
389
|
+
await replica.applyEnvelope(envelope, sent, headers.has('age'));
|
|
390
|
+
const side = principal;
|
|
391
|
+
if (side) {
|
|
392
|
+
await side.loaded;
|
|
393
|
+
await side.replica.applyEnvelope(envelope, null, true);
|
|
394
|
+
}
|
|
395
|
+
const advertised = envelope.artifact;
|
|
396
|
+
if (provisionable && advertised) {
|
|
397
|
+
const hit = matchRoute(config.routes, advertised.id);
|
|
398
|
+
const own = advertised.scope === 'principal' ? await principalSide() : publicSide;
|
|
399
|
+
const fits = hit &&
|
|
400
|
+
hit.route.strategy !== 'static' &&
|
|
401
|
+
hit.route.scope === advertised.scope &&
|
|
402
|
+
!declined.has(advertised.id) &&
|
|
403
|
+
own &&
|
|
404
|
+
(advertised.scope === 'public' || own.id === envelope.partition);
|
|
405
|
+
if (hit && own && fits && !(await own.replica.get(advertised.id))) {
|
|
406
|
+
if (advertised.scope === 'public' && modeFor(config, hit.route) === 'shadow') {
|
|
407
|
+
wants.add(advertised.id);
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
const key = `${advertised.scope}:${advertised.id}`;
|
|
411
|
+
const pending = provisioning.get(key) ??
|
|
412
|
+
provision(advertised.id, hit.route, envelope.partition);
|
|
413
|
+
provisioning.set(key, pending);
|
|
414
|
+
try {
|
|
415
|
+
await pending;
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
// a failed provision leaves the network view as the only one
|
|
419
|
+
}
|
|
420
|
+
finally {
|
|
421
|
+
provisioning.delete(key);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (moment) {
|
|
428
|
+
await drainDeferred(publicSide);
|
|
429
|
+
if (principal)
|
|
430
|
+
await drainDeferred(principal);
|
|
431
|
+
await analytics.flushAtMoment();
|
|
432
|
+
await revalidateThirdParty();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Keeps the security headers of a network view so a local render can replay them: the site-wide
|
|
437
|
+
* set from any HTML navigation, and the set of the route's own path. Errors, redirects and
|
|
438
|
+
* queried URLs are never kept, since no local render answers them.
|
|
439
|
+
*/
|
|
440
|
+
async function keepPolicy(request, response, envelope, navigation, after) {
|
|
441
|
+
if (request.method !== 'GET' || response.status !== 200 || response.redirected)
|
|
442
|
+
return;
|
|
443
|
+
if (after?.signedOut)
|
|
444
|
+
return;
|
|
445
|
+
if (navigation && !/text\/html/i.test(response.headers.get('content-type') ?? ''))
|
|
446
|
+
return;
|
|
447
|
+
const set = policyHeaders(response.headers);
|
|
448
|
+
if (navigation)
|
|
449
|
+
await replica.keepPolicy(null, set);
|
|
450
|
+
const route = after?.route;
|
|
451
|
+
const url = new URL(request.url);
|
|
452
|
+
if (!route || route.strategy === 'static' || url.search !== '')
|
|
453
|
+
return;
|
|
454
|
+
const side = route.scope === 'principal'
|
|
455
|
+
? principal?.id === envelope?.partition
|
|
456
|
+
? principal
|
|
457
|
+
: null
|
|
458
|
+
: publicSide;
|
|
459
|
+
if (!side)
|
|
460
|
+
return;
|
|
461
|
+
await side.loaded;
|
|
462
|
+
await side.replica.keepPolicy(url.pathname, set);
|
|
463
|
+
}
|
|
464
|
+
async function passthrough(request, navigation, after) {
|
|
465
|
+
let outgoing = request;
|
|
466
|
+
let sent = null;
|
|
467
|
+
if (!config.manifest &&
|
|
468
|
+
!navigation &&
|
|
469
|
+
request.method === 'GET' &&
|
|
470
|
+
replica.cursor !== null) {
|
|
471
|
+
try {
|
|
472
|
+
const headers = new Headers(request.headers);
|
|
473
|
+
headers.set(HEADERS.vectorSeen, String(replica.cursor));
|
|
474
|
+
outgoing = new Request(request, { headers });
|
|
475
|
+
sent = replica.cursor;
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
outgoing = request;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const response = await deps.fetch(outgoing);
|
|
482
|
+
const envelope = decodeEnvelopeOrClear(response.headers);
|
|
483
|
+
await early(envelope, navigation, after?.signedOut);
|
|
484
|
+
// a cacheable GET may be answered by the HTTP cache with no radio wake, so only these count
|
|
485
|
+
const moment = navigation || request.method !== 'GET';
|
|
486
|
+
const copy = after?.clone ? response.clone() : null;
|
|
487
|
+
track((async () => {
|
|
488
|
+
try {
|
|
489
|
+
await after?.run(copy);
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
// a view that cannot be recorded must not stop the response being observed
|
|
493
|
+
}
|
|
494
|
+
await observe(response.headers, envelope, sent, navigation, moment, after?.provision ?? navigation, () => keepPolicy(request, response, envelope, navigation, after));
|
|
495
|
+
})());
|
|
496
|
+
return response;
|
|
497
|
+
}
|
|
498
|
+
async function decide(path, route, dry) {
|
|
499
|
+
let side = publicSide;
|
|
500
|
+
if (route.scope === 'principal') {
|
|
501
|
+
if (!host)
|
|
502
|
+
return { reason: 'principal', staleSec: null };
|
|
503
|
+
const own = await principalSide();
|
|
504
|
+
if (!own)
|
|
505
|
+
return { reason: 'cold', staleSec: null };
|
|
506
|
+
side = own;
|
|
507
|
+
}
|
|
508
|
+
const { replica: rep, scheduler: sched } = side;
|
|
509
|
+
let stored = await rep.get(path);
|
|
510
|
+
if (!stored)
|
|
511
|
+
return { reason: 'cold', staleSec: null };
|
|
512
|
+
const staleSec = () => stored ? Math.max(0, Math.floor((now() - rep.validatedAt(stored)) / 1000)) : null;
|
|
513
|
+
if (!rep.isCurrent(stored.artifact)) {
|
|
514
|
+
const age = staleSec();
|
|
515
|
+
await rep.drop([path]);
|
|
516
|
+
return { reason: 'invalidated', staleSec: age };
|
|
517
|
+
}
|
|
518
|
+
if (route.version !== undefined &&
|
|
519
|
+
stored.artifact.strategy === 'static' &&
|
|
520
|
+
stored.artifact.body.version !== route.version) {
|
|
521
|
+
const age = staleSec();
|
|
522
|
+
await rep.drop([path]);
|
|
523
|
+
return { reason: 'invalidated', staleSec: age };
|
|
524
|
+
}
|
|
525
|
+
const fromCatalogue = stored.catalogue === true;
|
|
526
|
+
const action = verdict(route.freshness, rep.validatedAt(stored), now());
|
|
527
|
+
const age = staleSec();
|
|
528
|
+
const base = rep.needFor(stored.artifact, route.sync === 'deferred' || route.scope === 'principal');
|
|
529
|
+
// a principal sync also names the catalogue entries no page has opened, so a revoked one is dropped
|
|
530
|
+
const need = (route.scope === 'principal' && base ? await rep.sweep(base) : base) ?? undefined;
|
|
531
|
+
let reason = 'fresh';
|
|
532
|
+
if (action === 'sync-first') {
|
|
533
|
+
reason = 'hard-sync';
|
|
534
|
+
if (!dry) {
|
|
535
|
+
const synced = await sched.sync(need, age);
|
|
536
|
+
if (!synced && route.offline === 'refuse')
|
|
537
|
+
return { reason: 'hard-refused', staleSec: age };
|
|
538
|
+
if (synced) {
|
|
539
|
+
if (need)
|
|
540
|
+
await rep.validate(path);
|
|
541
|
+
stored = await rep.get(path);
|
|
542
|
+
if (!stored || !rep.isCurrent(stored.artifact)) {
|
|
543
|
+
return { reason: 'stale-after-sync', staleSec: age };
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
else
|
|
547
|
+
reason = 'offline-stale';
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
else if (action === 'serve-then-sync') {
|
|
551
|
+
if (route.sync === 'deferred') {
|
|
552
|
+
reason = 'soft-deferred';
|
|
553
|
+
if (!dry && need && sched.defer(need, age))
|
|
554
|
+
side.deferred.add(path);
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
reason = 'soft-sync';
|
|
558
|
+
if (!dry) {
|
|
559
|
+
track(sched.sync(need, age).then(async (ok) => {
|
|
560
|
+
if (ok && need)
|
|
561
|
+
await rep.validate(path);
|
|
562
|
+
}));
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (route.strategy === 'static') {
|
|
567
|
+
const kept = await readStatic(path);
|
|
568
|
+
if (!kept) {
|
|
569
|
+
await rep.drop([path]);
|
|
570
|
+
return { reason: 'refused', staleSec: age };
|
|
571
|
+
}
|
|
572
|
+
return { reason, response: kept, staleSec: age };
|
|
573
|
+
}
|
|
574
|
+
// never answer a page locally without the policy the site sent with it
|
|
575
|
+
const headers = rep.policyOf(path) ?? replica.sitePolicy;
|
|
576
|
+
if (!headers && !dry)
|
|
577
|
+
return { reason: 'refused', staleSec: age };
|
|
578
|
+
const plain = await rep.materialize(stored.artifact);
|
|
579
|
+
if (!plain.ok) {
|
|
580
|
+
// a reference with nothing behind it: drop the page so the next network view fetches it whole
|
|
581
|
+
await rep.drop([path]);
|
|
582
|
+
return { reason: 'refused', staleSec: age };
|
|
583
|
+
}
|
|
584
|
+
const rendered = await renderLocal(config, route, plain.artifact, renderers);
|
|
585
|
+
if (!rendered.ok)
|
|
586
|
+
return { reason: 'refused', staleSec: age };
|
|
587
|
+
if (fromCatalogue) {
|
|
588
|
+
await rep.open(path);
|
|
589
|
+
if (REASON_TIER[reason] === 'local')
|
|
590
|
+
reason = 'catalogue';
|
|
591
|
+
}
|
|
592
|
+
return { reason, html: rendered.html, headers, staleSec: age };
|
|
593
|
+
}
|
|
594
|
+
/** the route pattern of a same-origin referrer, `external` for another origin, else undefined */
|
|
595
|
+
function referrerRoute(referrer) {
|
|
596
|
+
let url;
|
|
597
|
+
try {
|
|
598
|
+
url = new URL(referrer);
|
|
599
|
+
}
|
|
600
|
+
catch {
|
|
601
|
+
return undefined;
|
|
602
|
+
}
|
|
603
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:')
|
|
604
|
+
return undefined;
|
|
605
|
+
if (url.origin !== origin)
|
|
606
|
+
return FROM_EXTERNAL;
|
|
607
|
+
return matchRoute(config.routes, url.pathname)?.route.pattern;
|
|
608
|
+
}
|
|
609
|
+
async function navigate(path, route, request) {
|
|
610
|
+
const shadow = runsShadow(config, route, random);
|
|
611
|
+
const at = now();
|
|
612
|
+
const gapMs = lastViewAt === null ? null : at - lastViewAt;
|
|
613
|
+
lastViewAt = at;
|
|
614
|
+
currentPath = route.catalogue ? path : null;
|
|
615
|
+
const decision = await decide(path, route, shadow);
|
|
616
|
+
const from = referrerRoute(request.referrer);
|
|
617
|
+
const id = crypto.randomUUID();
|
|
618
|
+
latestView.delete(path);
|
|
619
|
+
latestView.set(path, id);
|
|
620
|
+
if (latestView.size > 64)
|
|
621
|
+
latestView.delete(latestView.keys().next().value);
|
|
622
|
+
const view = (mismatch) => ({
|
|
623
|
+
id,
|
|
624
|
+
crossOriginLoads: null,
|
|
625
|
+
tier: shadow ? 'shadow' : REASON_TIER[decision.reason],
|
|
626
|
+
reason: decision.reason,
|
|
627
|
+
staleSec: decision.staleSec,
|
|
628
|
+
engine,
|
|
629
|
+
scope: route.scope,
|
|
630
|
+
strategy: route.strategy,
|
|
631
|
+
route: route.pattern,
|
|
632
|
+
mismatch,
|
|
633
|
+
swAgeMs: at - startedAt,
|
|
634
|
+
gapMs,
|
|
635
|
+
...(from ? { from } : {})
|
|
636
|
+
});
|
|
637
|
+
const record = async (mismatch) => {
|
|
638
|
+
if (config.views !== false)
|
|
639
|
+
await analytics.record(viewToBeacon(view(mismatch), at));
|
|
640
|
+
};
|
|
641
|
+
if (shadow) {
|
|
642
|
+
const html = decision.html;
|
|
643
|
+
return passthrough(request, true, {
|
|
644
|
+
clone: html !== undefined,
|
|
645
|
+
route,
|
|
646
|
+
run: async (network) => {
|
|
647
|
+
await record(html === undefined || !network ? null : await differs(html, network));
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
if (decision.response) {
|
|
652
|
+
ledger.serve(path);
|
|
653
|
+
track(record(null));
|
|
654
|
+
return decision.response;
|
|
655
|
+
}
|
|
656
|
+
if (decision.html === undefined) {
|
|
657
|
+
const keeps = route.strategy === 'static';
|
|
658
|
+
const response = await passthrough(request, true, {
|
|
659
|
+
clone: keeps,
|
|
660
|
+
route,
|
|
661
|
+
run: async (copy) => {
|
|
662
|
+
if (keeps)
|
|
663
|
+
await capture(path, route, copy);
|
|
664
|
+
await record(null);
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
if (route.scope === 'principal' && refusesSession(response))
|
|
668
|
+
await dropPrincipal(path);
|
|
669
|
+
return response;
|
|
670
|
+
}
|
|
671
|
+
ledger.serve(path, decision.reason === 'catalogue');
|
|
672
|
+
track(record(null));
|
|
673
|
+
const headers = new Headers(decision.headers);
|
|
674
|
+
headers.set('content-type', 'text/html; charset=utf-8');
|
|
675
|
+
return new Response(decision.html, { headers });
|
|
676
|
+
}
|
|
677
|
+
/** whether the server answered a principal page with a refusal or a redirect, as to a signed-out visitor */
|
|
678
|
+
function refusesSession(response) {
|
|
679
|
+
return (response.status === 401 ||
|
|
680
|
+
response.status === 403 ||
|
|
681
|
+
response.type === 'opaqueredirect' ||
|
|
682
|
+
(response.status >= 300 && response.status < 400));
|
|
683
|
+
}
|
|
684
|
+
async function dropPrincipal(path) {
|
|
685
|
+
const side = await principalSide();
|
|
686
|
+
if (side)
|
|
687
|
+
await side.replica.drop([path]);
|
|
688
|
+
}
|
|
689
|
+
/** answers a GET for a `data` route from the stored value; the page makes no request */
|
|
690
|
+
async function dataFile(request, path, route) {
|
|
691
|
+
const decision = await decide(path, route, false);
|
|
692
|
+
if (decision.html !== undefined) {
|
|
693
|
+
ledger.serve(path, decision.reason === 'catalogue');
|
|
694
|
+
const headers = new Headers(decision.headers);
|
|
695
|
+
headers.set('content-type', route.type ?? 'application/json');
|
|
696
|
+
return new Response(decision.html, { headers });
|
|
697
|
+
}
|
|
698
|
+
const response = await passthrough(request, false, {
|
|
699
|
+
provision: true,
|
|
700
|
+
route,
|
|
701
|
+
run: async () => undefined
|
|
702
|
+
});
|
|
703
|
+
if (route.scope === 'principal' && refusesSession(response))
|
|
704
|
+
await dropPrincipal(path);
|
|
705
|
+
return response;
|
|
706
|
+
}
|
|
707
|
+
/** the stored bytes of a static file, with the headers it was fetched with */
|
|
708
|
+
async function readStatic(path) {
|
|
709
|
+
const cache = await cacheNamed(staticCache);
|
|
710
|
+
return cache.match(keyOf(path));
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Keeps the response a static route's network answer produced. The bytes go in the Cache
|
|
714
|
+
* first, then the replica record that makes them servable, so a record always has its bytes.
|
|
715
|
+
* Nothing is kept for a redirect, an error, a file over the cap, or one the manifest's version
|
|
716
|
+
* does not describe; the next view asks the network again.
|
|
717
|
+
*/
|
|
718
|
+
async function capture(path, asked, copy) {
|
|
719
|
+
if (!copy || copy.status !== 200 || copy.redirected)
|
|
720
|
+
return;
|
|
721
|
+
// a sync in the same view may have replaced the manifest, so the route is read again
|
|
722
|
+
const route = config.manifest ? matchRoute(config.routes, pathOf(path))?.route : asked;
|
|
723
|
+
if (route?.strategy !== 'static')
|
|
724
|
+
return;
|
|
725
|
+
const bytes = await readCapped(copy, MAX_STATIC_BYTES);
|
|
726
|
+
if (!bytes)
|
|
727
|
+
return;
|
|
728
|
+
const type = copy.headers.get('content-type') ?? 'application/octet-stream';
|
|
729
|
+
let artifact;
|
|
730
|
+
if (config.manifest) {
|
|
731
|
+
const version = route.version;
|
|
732
|
+
if (version === undefined || !(await matchesVersion(version, bytes, copy.headers))) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
artifact = {
|
|
736
|
+
protocol: PROTOCOL_VERSION,
|
|
737
|
+
id: path,
|
|
738
|
+
scope: 'public',
|
|
739
|
+
strategy: 'static',
|
|
740
|
+
deps: [],
|
|
741
|
+
vector: 0,
|
|
742
|
+
body: { type, bytes: bytes.byteLength, version }
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
let envelope;
|
|
747
|
+
try {
|
|
748
|
+
envelope = decodeEnvelope(copy.headers);
|
|
749
|
+
}
|
|
750
|
+
catch {
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
const advertised = envelope.artifact;
|
|
754
|
+
if (envelope.protocol !== PROTOCOL_VERSION || !advertised)
|
|
755
|
+
return;
|
|
756
|
+
if (advertised.id !== path || advertised.scope !== 'public')
|
|
757
|
+
return;
|
|
758
|
+
artifact = {
|
|
759
|
+
protocol: PROTOCOL_VERSION,
|
|
760
|
+
id: path,
|
|
761
|
+
scope: 'public',
|
|
762
|
+
strategy: 'static',
|
|
763
|
+
deps: advertised.deps,
|
|
764
|
+
vector: advertised.version,
|
|
765
|
+
body: { type, bytes: bytes.byteLength }
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
const cache = await cacheNamed(staticCache);
|
|
769
|
+
await cache.put(keyOf(path), new Response(bytes, { headers: replayHeaders(copy.headers) }));
|
|
770
|
+
if (await replica.put(artifact, true)) {
|
|
771
|
+
ledger.spend(path, { bytes: bytes.byteLength, requests: 0 });
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
async function staticFile(request, path, route) {
|
|
775
|
+
const decision = await decide(path, route, false);
|
|
776
|
+
if (decision.response) {
|
|
777
|
+
ledger.serve(path);
|
|
778
|
+
return decision.response;
|
|
779
|
+
}
|
|
780
|
+
return passthrough(request, false, {
|
|
781
|
+
clone: true,
|
|
782
|
+
run: (copy) => capture(path, route, copy)
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
/** reads the manifest from the Cache, or the network when the Cache has none; leaves the routes empty if neither answers */
|
|
786
|
+
async function loadManifest() {
|
|
787
|
+
try {
|
|
788
|
+
const cache = await cacheNamed(staticCache);
|
|
789
|
+
const kept = await cache.match(new Request(config.manifest));
|
|
790
|
+
if (kept) {
|
|
791
|
+
config.routes = manifestRoutes(parseManifest(JSON.parse(await kept.text())));
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
await refreshManifest();
|
|
795
|
+
}
|
|
796
|
+
catch {
|
|
797
|
+
manifestLoad = undefined;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Fetches the manifest and takes its routes. The sync answer names every stored file whose
|
|
802
|
+
* route is gone or whose version changed, which the replica then drops.
|
|
803
|
+
*/
|
|
804
|
+
async function refreshManifest() {
|
|
805
|
+
const url = config.manifest;
|
|
806
|
+
const response = await deps.fetch(url, { cache: 'no-store', credentials: 'same-origin' });
|
|
807
|
+
if (!response.ok)
|
|
808
|
+
throw new Error(`manifest answered ${response.status}`);
|
|
809
|
+
const text = await response.text();
|
|
810
|
+
const routes = manifestRoutes(parseManifest(JSON.parse(text)));
|
|
811
|
+
config.routes = routes;
|
|
812
|
+
const cache = await cacheNamed(staticCache);
|
|
813
|
+
await cache.put(new Request(url), new Response(text, { headers: { 'content-type': 'application/json' } }));
|
|
814
|
+
const dropped = [];
|
|
815
|
+
for (const id of await replica.ids()) {
|
|
816
|
+
const stored = await replica.get(id);
|
|
817
|
+
if (stored?.artifact.strategy !== 'static')
|
|
818
|
+
continue;
|
|
819
|
+
const hit = matchRoute(routes, pathOf(id));
|
|
820
|
+
if (!hit || stored.artifact.body.version !== hit.route.version)
|
|
821
|
+
dropped.push(id);
|
|
822
|
+
}
|
|
823
|
+
return {
|
|
824
|
+
protocol: PROTOCOL_VERSION,
|
|
825
|
+
cursor: replica.cursor ?? 0,
|
|
826
|
+
changed: {},
|
|
827
|
+
dropped,
|
|
828
|
+
reset: false
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
/** answers an immutable same-origin asset from its Cache entry; the handle is opened once */
|
|
832
|
+
async function asset(request) {
|
|
833
|
+
const cache = await cacheNamed(assetCache);
|
|
834
|
+
const cacheable = !request.headers.has('range');
|
|
835
|
+
if (cacheable) {
|
|
836
|
+
const hit = await cache.match(request);
|
|
837
|
+
if (hit)
|
|
838
|
+
return hit;
|
|
839
|
+
}
|
|
840
|
+
await ready();
|
|
841
|
+
const response = await passthrough(request, false);
|
|
842
|
+
if (cacheable && response.status === 200 && immutable(response)) {
|
|
843
|
+
track(cache.put(request, response.clone()));
|
|
844
|
+
}
|
|
845
|
+
return response;
|
|
846
|
+
}
|
|
847
|
+
/** holds a cross-origin collector hit and answers the page at once */
|
|
848
|
+
async function collect(request, collector) {
|
|
849
|
+
await ready();
|
|
850
|
+
if (!(await analytics.enqueue(request, collector)))
|
|
851
|
+
return deps.fetch(request);
|
|
852
|
+
return collectorAnswer(request);
|
|
853
|
+
}
|
|
854
|
+
// #region third-party files
|
|
855
|
+
const third = config.thirdParty;
|
|
856
|
+
const thirdKey = (request) => new Request(request.url);
|
|
857
|
+
/** answers an allowed origin's GET from a held copy; a first sight goes to the network and may be kept */
|
|
858
|
+
async function thirdParty(request) {
|
|
859
|
+
const cache = await cacheNamed(THIRD_CACHE);
|
|
860
|
+
const hit = await cache.match(thirdKey(request));
|
|
861
|
+
if (hit)
|
|
862
|
+
return hit;
|
|
863
|
+
const response = await deps.fetch(request);
|
|
864
|
+
if (response.status === 200 && (response.type !== 'opaque' || third?.allowOpaque)) {
|
|
865
|
+
const kind = response.type === 'opaque'
|
|
866
|
+
? 'immutable'
|
|
867
|
+
: versionClass(new URL(request.url), response.headers, third?.versioned);
|
|
868
|
+
if (kind)
|
|
869
|
+
track(keepThird(request, response.clone(), kind));
|
|
870
|
+
}
|
|
871
|
+
return response;
|
|
872
|
+
}
|
|
873
|
+
async function keepThird(request, response, kind) {
|
|
874
|
+
const cache = await cacheNamed(THIRD_CACHE);
|
|
875
|
+
let stored = response;
|
|
876
|
+
if (response.type !== 'opaque') {
|
|
877
|
+
const headers = new Headers(response.headers);
|
|
878
|
+
headers.set(THIRD_HEADERS.class, kind);
|
|
879
|
+
headers.set(THIRD_HEADERS.fetched, String(now()));
|
|
880
|
+
stored = new Response(await response.arrayBuffer(), { status: 200, headers });
|
|
881
|
+
}
|
|
882
|
+
await cache.put(thirdKey(request), stored);
|
|
883
|
+
const keys = await cache.keys();
|
|
884
|
+
const limit = third?.maxEntries ?? 64;
|
|
885
|
+
if (keys.length <= limit)
|
|
886
|
+
return;
|
|
887
|
+
const aged = [];
|
|
888
|
+
for (const key of keys) {
|
|
889
|
+
const held = await cache.match(key);
|
|
890
|
+
aged.push({ key, at: Number(held?.headers.get(THIRD_HEADERS.fetched) ?? 0) });
|
|
891
|
+
}
|
|
892
|
+
aged.sort((a, b) => a.at - b.at);
|
|
893
|
+
for (const { key } of aged.slice(0, keys.length - limit))
|
|
894
|
+
await cache.delete(key);
|
|
895
|
+
}
|
|
896
|
+
/** checks the held copies that are only weakly versioned and old, at a network moment */
|
|
897
|
+
async function revalidateThirdParty() {
|
|
898
|
+
if (!third)
|
|
899
|
+
return;
|
|
900
|
+
const cache = await cacheNamed(THIRD_CACHE);
|
|
901
|
+
const after = third.revalidateAfterMs ?? DAY_MS;
|
|
902
|
+
for (const key of await cache.keys()) {
|
|
903
|
+
const held = await cache.match(key);
|
|
904
|
+
if (!held || held.headers.get(THIRD_HEADERS.class) !== 'revalidate')
|
|
905
|
+
continue;
|
|
906
|
+
const fetchedAt = Number(held.headers.get(THIRD_HEADERS.fetched) ?? 0);
|
|
907
|
+
if (now() - fetchedAt < after)
|
|
908
|
+
continue;
|
|
909
|
+
try {
|
|
910
|
+
const fresh = await deps.fetch(new Request(key.url, { cache: 'no-cache', mode: 'cors', credentials: 'omit' }));
|
|
911
|
+
if (fresh.status === 200 && fresh.type !== 'opaque') {
|
|
912
|
+
await keepThird(key, fresh, 'revalidate');
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
catch {
|
|
916
|
+
// the held copy stays until a later moment reaches the origin
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
// #endregion
|
|
921
|
+
async function run(request) {
|
|
922
|
+
await ready();
|
|
923
|
+
const route = classify(config, request, origin);
|
|
924
|
+
track(analytics.flushIfDue());
|
|
925
|
+
if (route.kind === 'logout') {
|
|
926
|
+
// held beacons still carry this session; sent after the logout they would carry the next one
|
|
927
|
+
await analytics.sendSameOrigin(LOGOUT_SEND_MS).catch(() => undefined);
|
|
928
|
+
await wipePartition('logout');
|
|
929
|
+
return passthrough(request, request.mode === 'navigate', {
|
|
930
|
+
signedOut: true,
|
|
931
|
+
run: async () => undefined
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
if (route.kind === 'collector')
|
|
935
|
+
return collect(request, route.collector);
|
|
936
|
+
if (route.kind === 'thirdparty')
|
|
937
|
+
return thirdParty(request);
|
|
938
|
+
if (route.kind === 'beacon') {
|
|
939
|
+
if (await analytics.enqueue(request))
|
|
940
|
+
return new Response(null, { status: 204 });
|
|
941
|
+
return passthrough(request, false);
|
|
942
|
+
}
|
|
943
|
+
if (route.kind === 'navigation')
|
|
944
|
+
return navigate(route.path, route.route, request);
|
|
945
|
+
if (route.kind === 'static')
|
|
946
|
+
return staticFile(request, route.path, route.route);
|
|
947
|
+
if (route.kind === 'data')
|
|
948
|
+
return dataFile(request, route.path, route.route);
|
|
949
|
+
if (route.kind === 'asset')
|
|
950
|
+
return asset(request);
|
|
951
|
+
return passthrough(request, request.mode === 'navigate');
|
|
952
|
+
}
|
|
953
|
+
return {
|
|
954
|
+
replica,
|
|
955
|
+
ledger,
|
|
956
|
+
handle(request, wait) {
|
|
957
|
+
const kind = classify(config, request, origin).kind;
|
|
958
|
+
if (kind === 'ignore')
|
|
959
|
+
return undefined;
|
|
960
|
+
if (kind === 'asset' && assetMode !== 'cache')
|
|
961
|
+
return undefined;
|
|
962
|
+
// a worker in shadow touches only navigations: subresources and beacons keep the browser's own path
|
|
963
|
+
if (observing &&
|
|
964
|
+
kind !== 'navigation' &&
|
|
965
|
+
kind !== 'static' &&
|
|
966
|
+
request.mode !== 'navigate') {
|
|
967
|
+
return undefined;
|
|
968
|
+
}
|
|
969
|
+
const work = kind === 'asset' && !manifestPending() ? asset(request) : run(request);
|
|
970
|
+
return work.catch(() => deps.fetch(request)).finally(() => wait(settleBackground()));
|
|
971
|
+
},
|
|
972
|
+
async install(event) {
|
|
973
|
+
if (assetMode === 'static-routing')
|
|
974
|
+
routed = await installRoutes(event, config, origin);
|
|
975
|
+
},
|
|
976
|
+
async activate() {
|
|
977
|
+
await ready();
|
|
978
|
+
for (const name of await deps.caches.keys()) {
|
|
979
|
+
const old = CACHE_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
980
|
+
if (old && name !== assetCache && name !== staticCache)
|
|
981
|
+
await deps.caches.delete(name);
|
|
982
|
+
}
|
|
983
|
+
},
|
|
984
|
+
async message(data) {
|
|
985
|
+
const message = (data ?? {});
|
|
986
|
+
if (message.type === 'spool:hello') {
|
|
987
|
+
return {
|
|
988
|
+
type: 'spool:hello',
|
|
989
|
+
protocol: PROTOCOL_VERSION,
|
|
990
|
+
version: config.version,
|
|
991
|
+
compatible: message.protocol === PROTOCOL_VERSION
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
if (message.type === 'spool:status') {
|
|
995
|
+
await ready();
|
|
996
|
+
return {
|
|
997
|
+
type: 'spool:status',
|
|
998
|
+
cursor: replica.cursor,
|
|
999
|
+
syncedAt: replica.syncedAt,
|
|
1000
|
+
artifacts: await replica.ids(),
|
|
1001
|
+
beacons: (await storage.readBeacons()).length
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
if (message.type === 'spool:view') {
|
|
1005
|
+
const path = message.path;
|
|
1006
|
+
return {
|
|
1007
|
+
type: 'spool:view',
|
|
1008
|
+
id: (typeof path === 'string' && latestView.get(path)) || null
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
if (message.type === 'spool:loads') {
|
|
1012
|
+
const { view, count, origins } = message;
|
|
1013
|
+
if (typeof view === 'string' &&
|
|
1014
|
+
typeof count === 'number' &&
|
|
1015
|
+
config.views !== false) {
|
|
1016
|
+
await ready();
|
|
1017
|
+
const named = typeof origins === 'object' && origins !== null && !Array.isArray(origins)
|
|
1018
|
+
? origins
|
|
1019
|
+
: undefined;
|
|
1020
|
+
await analytics.record(loadsToBeacon(view, count, now(), named));
|
|
1021
|
+
}
|
|
1022
|
+
return undefined;
|
|
1023
|
+
}
|
|
1024
|
+
if (message.type === 'spool:graph') {
|
|
1025
|
+
await ready();
|
|
1026
|
+
await replica.held();
|
|
1027
|
+
return {
|
|
1028
|
+
type: 'spool:graph',
|
|
1029
|
+
records: replica.graph.records.ids(),
|
|
1030
|
+
projections: replica.graph.projections.ids()
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
if (message.type === 'spool:ledger') {
|
|
1034
|
+
return { type: 'spool:ledger', entries: ledger.entries(), totals: ledger.totals() };
|
|
1035
|
+
}
|
|
1036
|
+
if (message.type === 'spool:partition') {
|
|
1037
|
+
const side = await principalSide();
|
|
1038
|
+
if (!side) {
|
|
1039
|
+
return {
|
|
1040
|
+
type: 'spool:partition',
|
|
1041
|
+
partition: null,
|
|
1042
|
+
artifacts: [],
|
|
1043
|
+
records: [],
|
|
1044
|
+
projections: []
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
await side.replica.held();
|
|
1048
|
+
return {
|
|
1049
|
+
type: 'spool:partition',
|
|
1050
|
+
partition: side.id,
|
|
1051
|
+
artifacts: await side.replica.ids(),
|
|
1052
|
+
records: side.replica.graph.records.ids(),
|
|
1053
|
+
projections: side.replica.graph.projections.ids()
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
if (message.type === 'spool:assets') {
|
|
1057
|
+
return { type: 'spool:assets', mode: assetMode, routed };
|
|
1058
|
+
}
|
|
1059
|
+
if (message.type === 'spool:held') {
|
|
1060
|
+
const cache = await cacheNamed(THIRD_CACHE);
|
|
1061
|
+
return {
|
|
1062
|
+
type: 'spool:held',
|
|
1063
|
+
dropped: Object.fromEntries(analytics.dropped),
|
|
1064
|
+
third: (await cache.keys()).map((k) => k.url).sort()
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
return undefined;
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
}
|