@drupflare/spool 0.3.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +279 -1607
- package/dist/angular/index.d.ts +13 -0
- package/dist/angular/index.js +25 -0
- package/dist/angular/pages.d.ts +28 -0
- package/dist/angular/pages.js +85 -0
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/location.d.ts +6 -0
- package/dist/cdn/location.js +8 -0
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/bootstrap.d.ts +57 -0
- package/dist/client/bootstrap.js +307 -0
- package/dist/client/contact.d.ts +24 -0
- package/dist/client/contact.js +66 -0
- package/dist/client/declared.d.ts +23 -0
- package/dist/client/declared.js +68 -0
- package/dist/client/entry.js +123 -2
- package/dist/client/gate.d.ts +1 -0
- package/dist/client/gate.js +148 -0
- package/dist/client/index.d.ts +25 -0
- package/dist/client/index.js +1 -0
- package/dist/client/live.d.ts +66 -0
- package/dist/client/live.js +225 -0
- package/dist/client/navigate-entry.d.ts +1 -0
- package/dist/client/navigate-entry.js +4 -0
- package/dist/client/navigate.d.ts +54 -0
- package/dist/client/navigate.js +272 -0
- package/dist/client/persist.d.ts +2 -0
- package/dist/client/persist.js +9 -0
- package/dist/client/register.d.ts +34 -2
- package/dist/client/register.js +64 -7
- package/dist/express/index.d.ts +11 -0
- package/dist/express/index.js +209 -0
- package/dist/hono/index.d.ts +19 -0
- package/dist/hono/index.js +49 -0
- package/dist/next/index.d.ts +33 -0
- package/dist/next/index.js +41 -0
- package/dist/next/pages.d.ts +22 -0
- package/dist/next/pages.js +39 -0
- package/dist/node/listener.d.ts +3 -0
- package/dist/node/listener.js +57 -2
- package/dist/nuxt/index.d.ts +22 -0
- package/dist/nuxt/index.js +104 -0
- package/dist/nuxt/pages.d.ts +26 -0
- package/dist/nuxt/pages.js +48 -0
- package/dist/nuxt/runtime/index.d.ts +24 -0
- package/dist/nuxt/runtime/index.js +58 -0
- package/dist/plugins/coverage.d.ts +14 -0
- package/dist/plugins/coverage.js +27 -0
- package/dist/plugins/mode.d.ts +19 -0
- package/dist/plugins/mode.js +20 -0
- package/dist/plugins/path.d.ts +16 -0
- package/dist/plugins/path.js +139 -0
- package/dist/plugins/report.d.ts +22 -0
- package/dist/plugins/report.js +18 -0
- package/dist/plugins/routes.d.ts +38 -0
- package/dist/plugins/routes.js +52 -0
- package/dist/plugins/static.d.ts +42 -0
- package/dist/plugins/static.js +97 -0
- package/dist/protocol/bundle.d.ts +111 -0
- package/dist/protocol/bundle.js +428 -0
- package/dist/protocol/config.d.ts +80 -1
- package/dist/protocol/config.js +31 -1
- package/dist/protocol/dataset.d.ts +189 -0
- package/dist/protocol/dataset.js +212 -0
- package/dist/protocol/envelope.d.ts +34 -1
- package/dist/protocol/envelope.js +16 -1
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/graph.d.ts +23 -4
- package/dist/protocol/graph.js +34 -3
- package/dist/protocol/index-rows.d.ts +22 -0
- package/dist/protocol/index-rows.js +53 -0
- package/dist/protocol/index.d.ts +5 -0
- package/dist/protocol/index.js +5 -0
- package/dist/protocol/normalise.d.ts +4 -4
- package/dist/protocol/normalise.js +9 -5
- package/dist/protocol/plan.d.ts +100 -0
- package/dist/protocol/plan.js +661 -0
- package/dist/protocol/provision.d.ts +46 -0
- package/dist/protocol/provision.js +38 -0
- package/dist/protocol/query.d.ts +66 -5
- package/dist/protocol/query.js +103 -4
- package/dist/protocol/ring.d.ts +58 -0
- package/dist/protocol/ring.js +6 -0
- package/dist/protocol/route.d.ts +63 -13
- package/dist/protocol/route.js +126 -6
- package/dist/protocol/tag.d.ts +22 -2
- package/dist/protocol/tag.js +25 -4
- package/dist/protocol/view.d.ts +2 -0
- package/dist/protocol/view.js +8 -2
- package/dist/protocol/views.d.ts +6 -0
- package/dist/protocol/views.js +15 -0
- package/dist/protocol/write.d.ts +234 -0
- package/dist/protocol/write.js +343 -0
- package/dist/proxy/eligible.d.ts +22 -1
- package/dist/proxy/eligible.js +62 -15
- package/dist/proxy/proxy.js +28 -18
- package/dist/react-router/index.d.ts +20 -0
- package/dist/react-router/index.js +60 -0
- package/dist/react-router/pages.d.ts +34 -0
- package/dist/react-router/pages.js +83 -0
- package/dist/report/dataset.d.ts +48 -0
- package/dist/report/dataset.js +93 -0
- package/dist/report/demand.d.ts +49 -0
- package/dist/report/demand.js +60 -0
- package/dist/report/derive.d.ts +285 -0
- package/dist/report/derive.js +676 -1
- package/dist/report/economics.d.ts +66 -1
- package/dist/report/economics.js +208 -1
- package/dist/report/headline.d.ts +158 -0
- package/dist/report/headline.js +473 -0
- package/dist/report/index.d.ts +4 -0
- package/dist/report/index.js +4 -0
- package/dist/report/inputs.d.ts +176 -0
- package/dist/report/inputs.js +248 -0
- package/dist/report/institution.d.ts +188 -1
- package/dist/report/institution.js +146 -0
- package/dist/report/loops.d.ts +73 -0
- package/dist/report/loops.js +101 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +7 -1
- package/dist/report/model.d.ts +2 -0
- package/dist/report/model.js +4 -0
- package/dist/report/thirdparty.d.ts +57 -0
- package/dist/report/thirdparty.js +111 -0
- package/dist/report/traffic.d.ts +8 -0
- package/dist/report/traffic.js +7 -0
- package/dist/report/trim.d.ts +56 -0
- package/dist/report/trim.js +83 -1
- package/dist/server/bundle.d.ts +55 -0
- package/dist/server/bundle.js +172 -0
- package/dist/server/dataset.d.ts +54 -0
- package/dist/server/dataset.js +355 -0
- package/dist/server/embedded.d.ts +7 -3
- package/dist/server/embedded.js +7 -3
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +6 -0
- package/dist/server/live-sockets.d.ts +54 -0
- package/dist/server/live-sockets.js +131 -0
- package/dist/server/live.d.ts +146 -0
- package/dist/server/live.js +228 -0
- package/dist/server/ring.d.ts +77 -0
- package/dist/server/ring.js +311 -0
- package/dist/server/spool.d.ts +158 -3
- package/dist/server/spool.js +578 -41
- package/dist/server/static.d.ts +42 -0
- package/dist/server/static.js +29 -0
- package/dist/server/stub.d.ts +43 -5
- package/dist/server/stub.js +126 -8
- package/dist/server/write.d.ts +49 -0
- package/dist/server/write.js +223 -0
- package/dist/stores/durable-object.d.ts +46 -0
- package/dist/stores/durable-object.js +114 -0
- package/dist/sw/analytics.d.ts +7 -1
- package/dist/sw/analytics.js +16 -2
- package/dist/sw/coverage.d.ts +6 -0
- package/dist/sw/coverage.js +34 -0
- package/dist/sw/dataset-sync.d.ts +76 -0
- package/dist/sw/dataset-sync.js +149 -0
- package/dist/sw/dataset.d.ts +91 -0
- package/dist/sw/dataset.js +318 -0
- package/dist/sw/family.d.ts +15 -0
- package/dist/sw/family.js +48 -0
- package/dist/sw/graph/index.d.ts +31 -1
- package/dist/sw/graph/index.js +57 -5
- package/dist/sw/graph/invalidate.d.ts +15 -3
- package/dist/sw/graph/invalidate.js +42 -4
- package/dist/sw/idb.d.ts +2 -0
- package/dist/sw/idb.js +80 -11
- package/dist/sw/index.d.ts +2 -0
- package/dist/sw/index.js +1 -0
- package/dist/sw/pack/store.d.ts +128 -0
- package/dist/sw/pack/store.js +737 -0
- package/dist/sw/partition.d.ts +6 -1
- package/dist/sw/partition.js +21 -2
- package/dist/sw/policy.d.ts +2 -0
- package/dist/sw/policy.js +11 -0
- package/dist/sw/principal-dataset.d.ts +64 -0
- package/dist/sw/principal-dataset.js +254 -0
- package/dist/sw/probe.d.ts +16 -0
- package/dist/sw/probe.js +25 -0
- package/dist/sw/render.d.ts +7 -2
- package/dist/sw/render.js +19 -5
- package/dist/sw/replica.d.ts +12 -3
- package/dist/sw/replica.js +40 -5
- package/dist/sw/router.d.ts +3 -2
- package/dist/sw/router.js +15 -2
- package/dist/sw/runtime.d.ts +90 -2
- package/dist/sw/runtime.js +857 -61
- package/dist/sw/scheduler.d.ts +6 -1
- package/dist/sw/scheduler.js +22 -5
- package/dist/sw/seal.d.ts +13 -0
- package/dist/sw/seal.js +13 -4
- package/dist/sw/static.d.ts +18 -0
- package/dist/sw/static.js +84 -1
- package/dist/sw/storage.d.ts +51 -1
- package/dist/sw/storage.js +33 -1
- package/dist/sw/write.d.ts +147 -0
- package/dist/sw/write.js +625 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -2
- package/dist/vite/index.d.ts +26 -0
- package/dist/vite/index.js +66 -0
- package/dist/vue/index.d.ts +36 -0
- package/dist/vue/index.js +53 -0
- package/package.json +65 -4
package/dist/sw/runtime.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { parseArtifact, PROTOCOL_VERSION } from '../protocol/artifact.js';
|
|
2
2
|
import { MAX_WANT } from '../protocol/beacon.js';
|
|
3
3
|
import { applyStamp } from '../protocol/collector.js';
|
|
4
|
-
import { PATHS } from '../protocol/config.js';
|
|
5
|
-
import { decodeEnvelope, decodeEnvelopeOrClear, HEADERS } from '../protocol/envelope.js';
|
|
4
|
+
import { BUNDLE_DELTA_PREFIX, clientPath, PATHS } from '../protocol/config.js';
|
|
5
|
+
import { decodeEnvelope, decodeEnvelopeOrClear, HEADERS, parseLiveDelta } from '../protocol/envelope.js';
|
|
6
6
|
import { shellHash } from '../protocol/hash.js';
|
|
7
7
|
import { readInline } from '../protocol/inline.js';
|
|
8
8
|
import { manifestRoutes, parseManifest } from '../protocol/manifest.js';
|
|
@@ -10,10 +10,15 @@ import { PROOF_MISMATCH } from '../protocol/query.js';
|
|
|
10
10
|
import { matchRoute } from '../protocol/route.js';
|
|
11
11
|
import { encodeHeld, parseSyncResponse } from '../protocol/sync.js';
|
|
12
12
|
import { engineOf, FROM_EXTERNAL, loadsToBeacon, REASON_TIER, viewToBeacon } from '../protocol/view.js';
|
|
13
|
+
import { ifMatch, WRITE_HEADERS, writeFor } from '../protocol/write.js';
|
|
13
14
|
import { BeaconQueue, LOGOUT_SEND_MS } from './analytics.js';
|
|
14
15
|
import { collectorAnswer } from './collect.js';
|
|
16
|
+
import { principalGuard, syncDataset } from './dataset-sync.js';
|
|
17
|
+
import { collectionPage, familyPage } from './family.js';
|
|
15
18
|
import { verdict } from './freshness.js';
|
|
16
|
-
import {
|
|
19
|
+
import { createPackStore } from './pack/store.js';
|
|
20
|
+
import { allowedPolicy, policyHeaders } from './policy.js';
|
|
21
|
+
import { probeCache, resourceTiming, shouldForward } from './probe.js';
|
|
17
22
|
import { Ledger, plan, priorFrom } from './provision.js';
|
|
18
23
|
import { renderLocal } from './render.js';
|
|
19
24
|
import { Replica } from './replica.js';
|
|
@@ -21,8 +26,9 @@ import { classify } from './router.js';
|
|
|
21
26
|
import { installRoutes } from './routing.js';
|
|
22
27
|
import { Scheduler } from './scheduler.js';
|
|
23
28
|
import { differs, modeFor, runsShadow } from './shadow.js';
|
|
24
|
-
import { matchesVersion, MAX_STATIC_BYTES, readCapped, replayHeaders, STATIC_CACHE } from './static.js';
|
|
29
|
+
import { branch, matchesVersion, MAX_STATIC_BYTES, readCapped, replayHeaders, STATIC_CACHE } from './static.js';
|
|
25
30
|
import { THIRD_CACHE, THIRD_HEADERS, versionClass } from './thirdparty.js';
|
|
31
|
+
import { settleWrite, WriteQueue } from './write.js';
|
|
26
32
|
/** the strategies a catalogue entry may have: data for a template or renderer the device holds */
|
|
27
33
|
const ENTRY_STRATEGIES = ['template', 'projection', 'data'];
|
|
28
34
|
const ASSET_CACHE = 'spool-assets-';
|
|
@@ -32,6 +38,8 @@ const DAY_MS = 86_400_000;
|
|
|
32
38
|
const encoder = new TextEncoder();
|
|
33
39
|
/** a static file's key is its pathname plus query string; this is the pathname a route matches */
|
|
34
40
|
const pathOf = (key) => key.split('?', 1)[0];
|
|
41
|
+
/** a data route answered from held rows: an item family, or a collection of a cross-origin API */
|
|
42
|
+
const isDataFamily = (route) => route.strategy === 'data' && (route.family !== undefined || route.origin !== undefined);
|
|
35
43
|
function immutable(response) {
|
|
36
44
|
const control = response.headers.get('cache-control') ?? '';
|
|
37
45
|
if (/(?:^|,)\s*immutable\b/i.test(control))
|
|
@@ -47,28 +55,38 @@ export function createRuntime(deps) {
|
|
|
47
55
|
const random = deps.random ?? Math.random;
|
|
48
56
|
const engine = engineOf(deps.userAgent ?? (typeof navigator === 'undefined' ? '' : navigator.userAgent));
|
|
49
57
|
const prior = priorFrom(config.provision);
|
|
50
|
-
const
|
|
58
|
+
const pack = deps.pack ?? (typeof indexedDB === 'undefined' ? undefined : createPackStore());
|
|
59
|
+
// only a new runtime empties what the device holds; a new content version keeps it
|
|
60
|
+
const codeVersion = config.runtime ?? config.version;
|
|
61
|
+
const replica = new Replica(storage, codeVersion, now);
|
|
51
62
|
const ledger = new Ledger();
|
|
52
63
|
const background = new Set();
|
|
53
64
|
const provisioning = new Map();
|
|
54
|
-
const declined = new
|
|
65
|
+
const declined = new Map();
|
|
66
|
+
// a 404 or a decline holds for the version it came back at
|
|
67
|
+
const unavailable = new Map();
|
|
55
68
|
const wants = new Set();
|
|
56
69
|
const asked = new Set();
|
|
57
70
|
let currentPath = null;
|
|
58
71
|
const latestView = new Map();
|
|
59
|
-
const assetCache = `${ASSET_CACHE}${
|
|
60
|
-
const staticCache = `${STATIC_CACHE}${
|
|
72
|
+
const assetCache = `${ASSET_CACHE}${codeVersion}`;
|
|
73
|
+
const staticCache = `${STATIC_CACHE}${codeVersion}`;
|
|
61
74
|
const keyOf = (path) => new Request(`${origin}${path}`);
|
|
62
75
|
const startedAt = now();
|
|
63
76
|
const assetMode = config.assets ?? 'cache';
|
|
64
77
|
let routed = false;
|
|
65
78
|
let lastViewAt = null;
|
|
79
|
+
let lifetimes;
|
|
66
80
|
let loaded;
|
|
67
81
|
let manifestLoad;
|
|
68
82
|
let partitionLoad;
|
|
69
83
|
let principal = null;
|
|
70
84
|
let exclusive = Promise.resolve();
|
|
71
85
|
const observing = config.mode === 'shadow';
|
|
86
|
+
// unanswered subresources are forwarded only after the probe finds the disk cache
|
|
87
|
+
const forwardsAssets = assetMode !== 'cache' && !observing;
|
|
88
|
+
let cachePath;
|
|
89
|
+
let probed = false;
|
|
72
90
|
const manifestPending = () => config.manifest !== undefined && config.routes.length === 0;
|
|
73
91
|
const handles = new Map();
|
|
74
92
|
/** opens a cache once and keeps the handle, so a hot request does not reopen it */
|
|
@@ -93,9 +111,11 @@ export function createRuntime(deps) {
|
|
|
93
111
|
background.add(settled);
|
|
94
112
|
void settled.then(() => background.delete(settled));
|
|
95
113
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
114
|
+
// only work begun after `before` is waited for, not an earlier request's held body
|
|
115
|
+
const settleBackground = async (before) => {
|
|
116
|
+
const fresh = () => [...background].filter((p) => !before.has(p));
|
|
117
|
+
while (fresh().length > 0)
|
|
118
|
+
await Promise.allSettled(fresh());
|
|
99
119
|
};
|
|
100
120
|
/** runs `fn` after every earlier partition change, so two responses never swap the store at once */
|
|
101
121
|
const serial = (fn) => {
|
|
@@ -117,6 +137,102 @@ export function createRuntime(deps) {
|
|
|
117
137
|
await early(decodeEnvelopeOrClear(response.headers), true);
|
|
118
138
|
return parseSyncResponse(await response.json());
|
|
119
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* A declared write goes to the server once. A 2xx answer brings the held pack to the version the
|
|
142
|
+
* server reached before the page gets the answer, so the next local read sees the change.
|
|
143
|
+
*/
|
|
144
|
+
async function declaredWrite(request, write) {
|
|
145
|
+
const body = await request
|
|
146
|
+
.clone()
|
|
147
|
+
.json()
|
|
148
|
+
.catch(() => undefined);
|
|
149
|
+
const response = await deps.fetch(await withHave(request));
|
|
150
|
+
const envelope = decodeEnvelopeOrClear(response.headers);
|
|
151
|
+
await early(envelope, false);
|
|
152
|
+
const expected = write.origin ?? origin;
|
|
153
|
+
if (response.ok && pack) {
|
|
154
|
+
await settleWrite(response, pack, fetchDelta, expected, {
|
|
155
|
+
write,
|
|
156
|
+
url: new URL(request.url),
|
|
157
|
+
body,
|
|
158
|
+
routes: config.routes
|
|
159
|
+
}).catch(() => undefined);
|
|
160
|
+
}
|
|
161
|
+
track(observe(response.headers, envelope, null, false, true, false, async () => undefined));
|
|
162
|
+
return response;
|
|
163
|
+
}
|
|
164
|
+
/** the delta from `from` to `to`, which the server answers for any two packs it holds */
|
|
165
|
+
async function fetchDelta(from, to) {
|
|
166
|
+
const response = await deps.fetch(`${origin}${BUNDLE_DELTA_PREFIX}${from}/${to}`, {
|
|
167
|
+
credentials: 'same-origin'
|
|
168
|
+
});
|
|
169
|
+
if (!response.ok)
|
|
170
|
+
throw new Error(`delta answered ${response.status}`);
|
|
171
|
+
return (await response.json());
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Moves a held pack to the version the worker script carries. A pack behind it takes the inline
|
|
175
|
+
* delta from its version, or one fetched delta when the inline ones do not reach it. The store
|
|
176
|
+
* checks the digest before it writes, so a refused delta leaves the pack as it was.
|
|
177
|
+
*/
|
|
178
|
+
async function adoptPack(pack, target) {
|
|
179
|
+
// the replica loads first: applyEnvelope would save its meta over the stored one
|
|
180
|
+
await ready();
|
|
181
|
+
const head = await pack.head();
|
|
182
|
+
if (!head || head.version >= target.version)
|
|
183
|
+
return;
|
|
184
|
+
const inline = target.deltas.find((delta) => delta.from === head.version);
|
|
185
|
+
await moveTo(pack, inline ?? (await fetchDelta(head.version, target.version)));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Applies a pack delta, then tells the replica which tags it moved, so an artifact built on an
|
|
189
|
+
* older row is dropped whichever path delivered the delta. A refused delta throws before either
|
|
190
|
+
* step.
|
|
191
|
+
*/
|
|
192
|
+
async function moveTo(store, delta) {
|
|
193
|
+
await store.applyDelta(delta);
|
|
194
|
+
const moved = Object.keys(delta.tables).map((tag) => [tag, delta.to]);
|
|
195
|
+
await replica.applyEnvelope({ invalidate: Object.fromEntries(moved) }, null, false);
|
|
196
|
+
}
|
|
197
|
+
/** the request with `Spool-Have` naming the pack the device holds; same-origin requests only */
|
|
198
|
+
async function withHave(request) {
|
|
199
|
+
if (!pack || new URL(request.url).origin !== origin)
|
|
200
|
+
return request;
|
|
201
|
+
let version;
|
|
202
|
+
try {
|
|
203
|
+
version = (await pack.head())?.version;
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return request;
|
|
207
|
+
}
|
|
208
|
+
if (version === undefined)
|
|
209
|
+
return request;
|
|
210
|
+
try {
|
|
211
|
+
const headers = new Headers(request.headers);
|
|
212
|
+
headers.set(HEADERS.have, String(version));
|
|
213
|
+
return new Request(request, { headers });
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
return request;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Applies the `Spool-Delta` a same-origin answer carries to the held pack, then tells the replica
|
|
221
|
+
* which tags moved, so an artifact built on an older row is dropped. A refused delta changes nothing.
|
|
222
|
+
*/
|
|
223
|
+
async function applyServerDelta(response) {
|
|
224
|
+
const text = response.headers.get(HEADERS.delta);
|
|
225
|
+
if (text === null || !pack || !response.url)
|
|
226
|
+
return;
|
|
227
|
+
if (new URL(response.url).origin !== origin)
|
|
228
|
+
return;
|
|
229
|
+
try {
|
|
230
|
+
await moveTo(pack, JSON.parse(text));
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
// a refused delta leaves the pack as it was
|
|
234
|
+
}
|
|
235
|
+
}
|
|
120
236
|
const policy = {
|
|
121
237
|
changesPerDay: prior.changesPerDay,
|
|
122
238
|
staleBytes: prior.avoidedBytes,
|
|
@@ -135,6 +251,8 @@ export function createRuntime(deps) {
|
|
|
135
251
|
const hit = matchRoute(config.routes, path);
|
|
136
252
|
if (!hit || hit.route.scope !== 'public' || hit.route.strategy === 'static')
|
|
137
253
|
continue;
|
|
254
|
+
if (hit.route.family || hit.route.origin !== undefined)
|
|
255
|
+
continue;
|
|
138
256
|
hot.push({
|
|
139
257
|
id: path,
|
|
140
258
|
kind: 'route',
|
|
@@ -153,7 +271,15 @@ export function createRuntime(deps) {
|
|
|
153
271
|
}
|
|
154
272
|
}
|
|
155
273
|
/** the page the last view was on, when its route declares a catalogue; the queue skips a recent ask */
|
|
156
|
-
|
|
274
|
+
// a complete held pack already has every page the catalogue would name, so no ask is sent
|
|
275
|
+
const catalogueAsk = async () => {
|
|
276
|
+
if (currentPath === null)
|
|
277
|
+
return undefined;
|
|
278
|
+
const head = await pack?.head().catch(() => undefined);
|
|
279
|
+
if (head && !head.partial)
|
|
280
|
+
return undefined;
|
|
281
|
+
return { path: currentPath, sweep: true };
|
|
282
|
+
};
|
|
157
283
|
/**
|
|
158
284
|
* Stores the entries a flush brought, each marked unopened. An entry is kept only for a route
|
|
159
285
|
* whose strategy and scope match it. A principal entry is kept only when the answer names the
|
|
@@ -257,23 +383,132 @@ export function createRuntime(deps) {
|
|
|
257
383
|
replica,
|
|
258
384
|
scheduler: makeScheduler(replica),
|
|
259
385
|
deferred: new Set(),
|
|
260
|
-
loaded: Promise.resolve()
|
|
386
|
+
loaded: Promise.resolve(),
|
|
387
|
+
dead: false,
|
|
388
|
+
covered: false,
|
|
389
|
+
leaseUntil: 0
|
|
261
390
|
};
|
|
262
391
|
const scheduler = publicSide.scheduler;
|
|
392
|
+
// #region datasets
|
|
393
|
+
const datasetRuns = new Map();
|
|
394
|
+
const datasetCounts = {};
|
|
395
|
+
const datasetRefused = new Map();
|
|
396
|
+
const datasetsDeferred = new Set();
|
|
397
|
+
/** principal datasets whose held rows are past maxStaleMs; the next network moment syncs them */
|
|
398
|
+
const principalDeferred = new Set();
|
|
399
|
+
let persistAsked = false;
|
|
400
|
+
let persisted = null;
|
|
401
|
+
const estimate = async () => {
|
|
402
|
+
if (deps.estimate)
|
|
403
|
+
return deps.estimate();
|
|
404
|
+
return ((typeof navigator === 'undefined'
|
|
405
|
+
? undefined
|
|
406
|
+
: await navigator.storage?.estimate?.()) ?? {});
|
|
407
|
+
};
|
|
408
|
+
/** the bytes of rows one dataset may take: the headroom times the declared share; unknown when the browser will not say */
|
|
409
|
+
const datasetBudget = async () => {
|
|
410
|
+
try {
|
|
411
|
+
const { quota, usage } = await estimate();
|
|
412
|
+
if (quota === undefined || usage === undefined)
|
|
413
|
+
return undefined;
|
|
414
|
+
return Math.max(0, (quota - usage) * (config.datasetShare ?? 1));
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
return undefined;
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
const tally = (outcome) => {
|
|
421
|
+
const key = outcome.status === 'refused'
|
|
422
|
+
? `${outcome.name}:refused:${outcome.reason}`
|
|
423
|
+
: `${outcome.name}:${outcome.status}`;
|
|
424
|
+
datasetCounts[key] = (datasetCounts[key] ?? 0) + 1;
|
|
425
|
+
if (outcome.status === 'refused') {
|
|
426
|
+
datasetRefused.set(outcome.name, outcome.reason);
|
|
427
|
+
if (outcome.reason === 'declined')
|
|
428
|
+
declined.set(outcome.name, outcome.by ?? null);
|
|
429
|
+
}
|
|
430
|
+
else if (outcome.status !== 'offline')
|
|
431
|
+
datasetRefused.delete(outcome.name);
|
|
432
|
+
const held = outcome.status !== 'offline' && outcome.status !== 'refused';
|
|
433
|
+
if (held && !persistAsked) {
|
|
434
|
+
persistAsked = true;
|
|
435
|
+
deps.broadcast?.({ type: 'spool:persist' });
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
/** the principal datasets the server declared, with how long each may answer after a confirmed sync */
|
|
439
|
+
const principalLimits = () => new Map((config.principalDatasets ?? []).map((d) => [d.name, d.maxStaleMs]));
|
|
440
|
+
/**
|
|
441
|
+
* A principal dataset's request, read for who it answers. A late answer for a partition the
|
|
442
|
+
* worker has left is returned untouched, since the sync discards it; a session that ended wipes
|
|
443
|
+
* the store, and an answer for another partition swaps it, exactly as a navigation does.
|
|
444
|
+
*/
|
|
445
|
+
async function principalFetch(side, url, init) {
|
|
446
|
+
const response = await deps.fetch(url, init);
|
|
447
|
+
if (side.dead || principal !== side)
|
|
448
|
+
return response;
|
|
449
|
+
if (response.status === 401)
|
|
450
|
+
await wipePartition('session-ended');
|
|
451
|
+
else if (response.ok) {
|
|
452
|
+
if (response.headers.has(HEADERS.partition)) {
|
|
453
|
+
await early(decodeEnvelopeOrClear(response.headers), true);
|
|
454
|
+
}
|
|
455
|
+
else
|
|
456
|
+
await wipePartition('session-ended');
|
|
457
|
+
}
|
|
458
|
+
return response;
|
|
459
|
+
}
|
|
460
|
+
/** brings one dataset current; a second caller joins the run in flight, for the same side */
|
|
461
|
+
function syncDatasetOnce(name, side = publicSide) {
|
|
462
|
+
const key = `${side.id ?? ''}\u0000${name}`;
|
|
463
|
+
const running = datasetRuns.get(key);
|
|
464
|
+
if (running)
|
|
465
|
+
return running;
|
|
466
|
+
const partition = side.id;
|
|
467
|
+
const run = syncDataset({
|
|
468
|
+
store: side.replica.datasets,
|
|
469
|
+
fetch: partition === null
|
|
470
|
+
? (url, init) => deps.fetch(url, init)
|
|
471
|
+
: (url, init) => principalFetch(side, url, init),
|
|
472
|
+
origin,
|
|
473
|
+
epoch: () => (side.dead ? -1 : side.replica.epoch),
|
|
474
|
+
budget: datasetBudget,
|
|
475
|
+
...(partition === null ? {} : { guard: principalGuard(partition) })
|
|
476
|
+
}, name)
|
|
477
|
+
.then((outcome) => {
|
|
478
|
+
tally(outcome);
|
|
479
|
+
return outcome;
|
|
480
|
+
})
|
|
481
|
+
.finally(() => datasetRuns.delete(key));
|
|
482
|
+
datasetRuns.set(key, run);
|
|
483
|
+
return run;
|
|
484
|
+
}
|
|
485
|
+
const datasetCurrent = (outcome) => outcome.status !== 'offline' && outcome.status !== 'refused';
|
|
486
|
+
/** seeds a dataset in the background unless this run already learned it will not be held */
|
|
487
|
+
function seedDataset(name, side = publicSide) {
|
|
488
|
+
if (side !== publicSide && !principalLimits().has(name))
|
|
489
|
+
return;
|
|
490
|
+
const key = `${side.id ?? ''}\u0000${name}`;
|
|
491
|
+
if (!datasetRefused.has(name) && !datasetRuns.has(key))
|
|
492
|
+
track(syncDatasetOnce(name, side));
|
|
493
|
+
}
|
|
494
|
+
// #endregion
|
|
263
495
|
// #region partitions
|
|
264
496
|
const retentionOf = (id) => matchRoute(config.routes, id)?.route.retention ?? 'session';
|
|
265
497
|
const sharedRetention = () => config.routes.some((r) => r.scope === 'principal' && r.retention === 'persistent')
|
|
266
498
|
? 'persistent'
|
|
267
499
|
: 'session';
|
|
268
500
|
function makeSide(id) {
|
|
269
|
-
const sealed = host.open(id, retentionOf, sharedRetention());
|
|
270
|
-
const target = new Replica(sealed,
|
|
501
|
+
const sealed = host.open(id, retentionOf, sharedRetention(), config.principalDatasets !== undefined);
|
|
502
|
+
const target = new Replica(sealed, codeVersion, now);
|
|
271
503
|
return {
|
|
272
504
|
id,
|
|
273
505
|
replica: target,
|
|
274
506
|
scheduler: makeScheduler(target),
|
|
275
507
|
deferred: new Set(),
|
|
276
|
-
loaded: target.load()
|
|
508
|
+
loaded: target.load(),
|
|
509
|
+
dead: false,
|
|
510
|
+
covered: false,
|
|
511
|
+
leaseUntil: 0
|
|
277
512
|
};
|
|
278
513
|
}
|
|
279
514
|
async function loadPartition() {
|
|
@@ -290,12 +525,24 @@ export function createRuntime(deps) {
|
|
|
290
525
|
await side.loaded;
|
|
291
526
|
return side;
|
|
292
527
|
}
|
|
293
|
-
/**
|
|
294
|
-
function
|
|
528
|
+
/** marks a side left for good, and forgets what the next principal must not inherit */
|
|
529
|
+
function retire(side) {
|
|
530
|
+
side.dead = true;
|
|
531
|
+
principalDeferred.clear();
|
|
532
|
+
for (const name of principalLimits().keys())
|
|
533
|
+
datasetRefused.delete(name);
|
|
534
|
+
}
|
|
535
|
+
/** shuts the principal store, shreds its keys and deletes it; true when there was one. `only` limits it to that partition */
|
|
536
|
+
function wipePartition(reason, only) {
|
|
295
537
|
return serial(async () => {
|
|
296
538
|
await ready();
|
|
539
|
+
if (only !== undefined && principal?.id !== only)
|
|
540
|
+
return false;
|
|
297
541
|
const old = principal;
|
|
298
542
|
principal = null;
|
|
543
|
+
old?.writes?.kill();
|
|
544
|
+
if (old)
|
|
545
|
+
retire(old);
|
|
299
546
|
if (!old || !host)
|
|
300
547
|
return false;
|
|
301
548
|
await host.setCurrent(null);
|
|
@@ -312,6 +559,10 @@ export function createRuntime(deps) {
|
|
|
312
559
|
if (old?.id === id)
|
|
313
560
|
return;
|
|
314
561
|
principal = makeSide(id);
|
|
562
|
+
swept = false;
|
|
563
|
+
old?.writes?.kill();
|
|
564
|
+
if (old)
|
|
565
|
+
retire(old);
|
|
315
566
|
await host.setCurrent(id);
|
|
316
567
|
if (old) {
|
|
317
568
|
await host.destroy(old.id);
|
|
@@ -319,6 +570,97 @@ export function createRuntime(deps) {
|
|
|
319
570
|
}
|
|
320
571
|
});
|
|
321
572
|
}
|
|
573
|
+
// #region live stream
|
|
574
|
+
/** what the page needs to open a stream: whether the site has one, the partition held and the cursor to start from */
|
|
575
|
+
async function liveState() {
|
|
576
|
+
const side = await principalSide();
|
|
577
|
+
const cursors = [replica.cursor, side?.replica.cursor ?? null].filter((c) => c !== null);
|
|
578
|
+
return {
|
|
579
|
+
type: 'spool:live-state',
|
|
580
|
+
live: config.live ?? null,
|
|
581
|
+
partition: side?.id ?? null,
|
|
582
|
+
have: cursors.length > 0 ? Math.min(...cursors) : null
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Applies what the page read from its stream. The partition names the stream's own, so a late event
|
|
587
|
+
* for a partition this worker has left changes nothing. A delta is applied to a side only when it
|
|
588
|
+
* starts at or below that side's cursor; a gap ends the side's lease and tells the page to connect
|
|
589
|
+
* again. A `revoked` event wipes the partition exactly as a logout does.
|
|
590
|
+
*/
|
|
591
|
+
/** lease renewals paid in this visible session; the second asks a page for the stream instead */
|
|
592
|
+
let liveRenewals = 0;
|
|
593
|
+
const liveWaiters = new Set();
|
|
594
|
+
/** asks the pages to open the stream; true when a lease covered `side` before the wait ran out */
|
|
595
|
+
function wantLive() {
|
|
596
|
+
if (!deps.broadcast)
|
|
597
|
+
return Promise.resolve(false);
|
|
598
|
+
return new Promise((resolve) => {
|
|
599
|
+
const ready = () => {
|
|
600
|
+
clearTimeout(timer);
|
|
601
|
+
resolve(true);
|
|
602
|
+
};
|
|
603
|
+
const timer = setTimeout(() => {
|
|
604
|
+
liveWaiters.delete(ready);
|
|
605
|
+
resolve(false);
|
|
606
|
+
}, deps.liveWaitMs ?? 3000);
|
|
607
|
+
liveWaiters.add(ready);
|
|
608
|
+
deps.broadcast?.({ type: 'spool:want-live' });
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
async function liveEvent(message) {
|
|
612
|
+
if (message.event === 'session') {
|
|
613
|
+
liveRenewals = 0;
|
|
614
|
+
return { type: 'spool:live', ok: true };
|
|
615
|
+
}
|
|
616
|
+
if (message.event === 'revoked') {
|
|
617
|
+
if (message.partition !== null)
|
|
618
|
+
await wipePartition('revoked', message.partition);
|
|
619
|
+
return { type: 'spool:live', ok: true };
|
|
620
|
+
}
|
|
621
|
+
return serial(async () => {
|
|
622
|
+
await ready();
|
|
623
|
+
const own = principal && principal.id === message.partition ? principal : null;
|
|
624
|
+
const publicLive = config.live?.public === true;
|
|
625
|
+
const sides = [...(own ? [own] : []), ...(publicLive ? [publicSide] : [])];
|
|
626
|
+
if (message.event === 'lease') {
|
|
627
|
+
const until = now() + message.leaseMs;
|
|
628
|
+
for (const side of sides)
|
|
629
|
+
if (side.covered)
|
|
630
|
+
side.leaseUntil = until;
|
|
631
|
+
if (own?.covered) {
|
|
632
|
+
for (const ready of [...liveWaiters])
|
|
633
|
+
ready();
|
|
634
|
+
liveWaiters.clear();
|
|
635
|
+
}
|
|
636
|
+
return { type: 'spool:live', ok: true };
|
|
637
|
+
}
|
|
638
|
+
let ok = true;
|
|
639
|
+
for (const side of sides) {
|
|
640
|
+
if (side.dead)
|
|
641
|
+
continue;
|
|
642
|
+
const cursor = side.replica.cursor;
|
|
643
|
+
if (cursor === null)
|
|
644
|
+
continue;
|
|
645
|
+
if (cursor < message.delta.from) {
|
|
646
|
+
side.covered = false;
|
|
647
|
+
side.leaseUntil = 0;
|
|
648
|
+
ok = false;
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
await side.replica.applySync(message.delta);
|
|
652
|
+
side.covered = true;
|
|
653
|
+
}
|
|
654
|
+
return { type: 'spool:live', ok };
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
// #endregion
|
|
658
|
+
// a page that loads after a navigation contact never hears its broadcast, so the hello carries it
|
|
659
|
+
let lastContact = 0;
|
|
660
|
+
function contacted() {
|
|
661
|
+
lastContact = now();
|
|
662
|
+
deps.broadcast?.({ type: 'spool:contact', at: lastContact });
|
|
663
|
+
}
|
|
322
664
|
/**
|
|
323
665
|
* What a response says about who is signed in, applied before the response is handed back so no
|
|
324
666
|
* later view reads a store the response outdated. `Spool-Clear` wipes everything or one
|
|
@@ -327,11 +669,15 @@ export function createRuntime(deps) {
|
|
|
327
669
|
* required sync after a sign-in the worker never saw swaps the store before its result is used.
|
|
328
670
|
*/
|
|
329
671
|
async function early(envelope, navigation, signedOut = false) {
|
|
672
|
+
// a response from the server is a contact; the page's lease clock resets on it
|
|
673
|
+
if (envelope?.protocol === PROTOCOL_VERSION)
|
|
674
|
+
contacted();
|
|
330
675
|
if (!host || !envelope || envelope.protocol !== PROTOCOL_VERSION)
|
|
331
676
|
return;
|
|
332
677
|
await ready();
|
|
333
678
|
const clear = envelope.clear ?? [];
|
|
334
679
|
if (clear.includes('*') || (principal && clear.includes(principal.id))) {
|
|
680
|
+
await analytics.dropSameOrigin();
|
|
335
681
|
await wipePartition('clear');
|
|
336
682
|
return;
|
|
337
683
|
}
|
|
@@ -339,14 +685,91 @@ export function createRuntime(deps) {
|
|
|
339
685
|
if (signedOut)
|
|
340
686
|
return;
|
|
341
687
|
if (envelope.partition !== undefined) {
|
|
342
|
-
if (principal?.id !== envelope.partition)
|
|
688
|
+
if (principal?.id !== envelope.partition) {
|
|
689
|
+
await analytics.dropSameOrigin();
|
|
343
690
|
await switchTo(envelope.partition);
|
|
691
|
+
}
|
|
344
692
|
}
|
|
345
|
-
else if (navigation && principal)
|
|
693
|
+
else if (navigation && principal) {
|
|
694
|
+
await analytics.dropSameOrigin();
|
|
346
695
|
await wipePartition('session-ended');
|
|
696
|
+
}
|
|
347
697
|
}
|
|
348
698
|
// #endregion
|
|
349
|
-
|
|
699
|
+
// #region writes
|
|
700
|
+
// a worker in shadow mode only observes, so it queues nothing
|
|
701
|
+
const writesOn = () => config.writes !== undefined && config.mode !== 'shadow';
|
|
702
|
+
const writeRefusal = (code, message) => ({
|
|
703
|
+
ok: false,
|
|
704
|
+
code,
|
|
705
|
+
message
|
|
706
|
+
});
|
|
707
|
+
async function sendWrite(record) {
|
|
708
|
+
const query = `name=${encodeURIComponent(record.name)}&key=${encodeURIComponent(record.key)}`;
|
|
709
|
+
const request = await withHave(new Request(`${origin}${PATHS.write}?${query}`, {
|
|
710
|
+
method: 'PUT',
|
|
711
|
+
headers: {
|
|
712
|
+
'content-type': 'application/json',
|
|
713
|
+
'if-match': ifMatch(record.base),
|
|
714
|
+
[WRITE_HEADERS.id]: record.id
|
|
715
|
+
},
|
|
716
|
+
body: JSON.stringify({ value: record.value }),
|
|
717
|
+
cache: 'no-store',
|
|
718
|
+
credentials: 'same-origin'
|
|
719
|
+
}));
|
|
720
|
+
const response = await deps.fetch(request);
|
|
721
|
+
// the write answer carries no envelope, so the answer itself is the contact
|
|
722
|
+
contacted();
|
|
723
|
+
const body = await response.json().catch(() => undefined);
|
|
724
|
+
return { status: response.status, body };
|
|
725
|
+
}
|
|
726
|
+
function queueOf(side) {
|
|
727
|
+
if (!writesOn() || !host?.openWrites)
|
|
728
|
+
return null;
|
|
729
|
+
side.writes ??= new WriteQueue({
|
|
730
|
+
store: host.openWrites(side.id),
|
|
731
|
+
classes: () => config.writes ?? [],
|
|
732
|
+
now,
|
|
733
|
+
send: sendWrite,
|
|
734
|
+
onFlush: (flush) => deps.broadcast?.({ type: 'spool:written', ...flush }),
|
|
735
|
+
timers: deps.timers
|
|
736
|
+
});
|
|
737
|
+
return side.writes;
|
|
738
|
+
}
|
|
739
|
+
async function pageQueue() {
|
|
740
|
+
if (!writesOn())
|
|
741
|
+
return writeRefusal('writes-off', 'declared writes are off');
|
|
742
|
+
const side = await principalSide();
|
|
743
|
+
if (!side)
|
|
744
|
+
return writeRefusal('no-partition', 'no signed-in principal is held');
|
|
745
|
+
return queueOf(side) ?? writeRefusal('unavailable', 'this worker has no partition storage');
|
|
746
|
+
}
|
|
747
|
+
/** sends the signed-in partition's queue at a network moment that is happening anyway */
|
|
748
|
+
async function flushWrites() {
|
|
749
|
+
const queue = principal ? queueOf(principal) : null;
|
|
750
|
+
if (queue)
|
|
751
|
+
await queue.flush();
|
|
752
|
+
}
|
|
753
|
+
// a worker that was stopped leaves its deadlines behind; the first contact of each start checks them
|
|
754
|
+
let swept = false;
|
|
755
|
+
function sweepWrites() {
|
|
756
|
+
if (swept)
|
|
757
|
+
return undefined;
|
|
758
|
+
swept = true;
|
|
759
|
+
return principalSide()
|
|
760
|
+
.then((side) => (side ? queueOf(side)?.flushDue() : undefined))
|
|
761
|
+
.then(() => undefined, () => undefined);
|
|
762
|
+
}
|
|
763
|
+
// #endregion
|
|
764
|
+
/** whether the held pack renders a public family page, so no artifact for it is needed */
|
|
765
|
+
async function packRenders(path, route) {
|
|
766
|
+
if (route.family === undefined || route.scope !== 'public')
|
|
767
|
+
return false;
|
|
768
|
+
const dataset = route.family.dataset;
|
|
769
|
+
const held = async (key) => (pack ? pack.getRow(dataset, key) : undefined);
|
|
770
|
+
return (await familyPage(held, route, path, now())) !== undefined;
|
|
771
|
+
}
|
|
772
|
+
async function provision(path, route, version, partition) {
|
|
350
773
|
const side = route.scope === 'principal' ? await principalSide() : publicSide;
|
|
351
774
|
if (!side || (route.scope === 'principal' && side.id !== partition))
|
|
352
775
|
return;
|
|
@@ -354,10 +777,13 @@ export function createRuntime(deps) {
|
|
|
354
777
|
const query = Object.keys(have).length > 0 ? `&have=${encodeURIComponent(encodeHeld(have))}` : '';
|
|
355
778
|
const response = await deps.fetch(`${origin}${PATHS.artifact}?path=${encodeURIComponent(path)}${query}`, { cache: 'no-store', credentials: 'same-origin' });
|
|
356
779
|
if (response.status === 204 && response.headers.get(HEADERS.provision) === 'declined') {
|
|
357
|
-
declined.
|
|
780
|
+
declined.set(path, response.headers.get(HEADERS.provisionReason));
|
|
781
|
+
unavailable.set(path, version);
|
|
358
782
|
return;
|
|
359
783
|
}
|
|
360
784
|
if (!response.ok) {
|
|
785
|
+
if (response.status === 404)
|
|
786
|
+
unavailable.set(path, version);
|
|
361
787
|
if (route.scope === 'principal' && [401, 403, 404].includes(response.status)) {
|
|
362
788
|
await side.replica.drop([path]);
|
|
363
789
|
}
|
|
@@ -377,6 +803,22 @@ export function createRuntime(deps) {
|
|
|
377
803
|
}
|
|
378
804
|
if (await side.replica.put(artifact, route.persist)) {
|
|
379
805
|
ledger.spend(path, { bytes: encoder.encode(text).length, requests: 1 });
|
|
806
|
+
// a persistent key outlives the worker, so rows seeded now are readable at the next return
|
|
807
|
+
if (route.scope === 'principal' && route.retention === 'persistent') {
|
|
808
|
+
await seedOwnRows(side, route).catch(() => undefined);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
/** seeds the principal datasets `route` reads that this side does not hold */
|
|
813
|
+
async function seedOwnRows(side, route) {
|
|
814
|
+
const names = new Set([
|
|
815
|
+
...(route.family ? [route.family.dataset] : []),
|
|
816
|
+
...(route.query?.dataset ? [route.query.dataset] : []),
|
|
817
|
+
...(route.query?.plan?.join ?? []).map((j) => j.dataset)
|
|
818
|
+
]);
|
|
819
|
+
for (const name of names) {
|
|
820
|
+
if (!(await side.replica.datasets.get(name)))
|
|
821
|
+
seedDataset(name, side);
|
|
380
822
|
}
|
|
381
823
|
}
|
|
382
824
|
async function drainDeferred(side) {
|
|
@@ -386,6 +828,24 @@ export function createRuntime(deps) {
|
|
|
386
828
|
for (const path of paths)
|
|
387
829
|
await side.replica.validate(path);
|
|
388
830
|
}
|
|
831
|
+
let leaseDrain;
|
|
832
|
+
/** sends the public side's queued syncs without waiting for a network moment; one run at a time */
|
|
833
|
+
function drainAtLease() {
|
|
834
|
+
if (leaseDrain)
|
|
835
|
+
return;
|
|
836
|
+
leaseDrain = (async () => {
|
|
837
|
+
await drainDeferred(publicSide);
|
|
838
|
+
const names = [...datasetsDeferred];
|
|
839
|
+
datasetsDeferred.clear();
|
|
840
|
+
for (const name of names)
|
|
841
|
+
await syncDatasetOnce(name);
|
|
842
|
+
})()
|
|
843
|
+
.catch(() => undefined)
|
|
844
|
+
.finally(() => {
|
|
845
|
+
leaseDrain = undefined;
|
|
846
|
+
});
|
|
847
|
+
track(leaseDrain);
|
|
848
|
+
}
|
|
389
849
|
async function observe(headers, envelope, sent, navigation, moment, provisionable, keep) {
|
|
390
850
|
await keep();
|
|
391
851
|
if (envelope && envelope.protocol === PROTOCOL_VERSION) {
|
|
@@ -401,18 +861,24 @@ export function createRuntime(deps) {
|
|
|
401
861
|
const own = advertised.scope === 'principal' ? await principalSide() : publicSide;
|
|
402
862
|
const fits = hit &&
|
|
403
863
|
hit.route.strategy !== 'static' &&
|
|
864
|
+
hit.route.provisionable !== false &&
|
|
404
865
|
hit.route.scope === advertised.scope &&
|
|
405
866
|
!declined.has(advertised.id) &&
|
|
867
|
+
unavailable.get(advertised.id) !== advertised.version &&
|
|
406
868
|
own &&
|
|
407
869
|
(advertised.scope === 'public' || own.id === envelope.partition);
|
|
408
|
-
if (hit &&
|
|
870
|
+
if (hit &&
|
|
871
|
+
own &&
|
|
872
|
+
fits &&
|
|
873
|
+
!(await packRenders(advertised.id, hit.route)) &&
|
|
874
|
+
!(await own.replica.get(advertised.id))) {
|
|
409
875
|
if (advertised.scope === 'public' && modeFor(config, hit.route) === 'shadow') {
|
|
410
876
|
wants.add(advertised.id);
|
|
411
877
|
}
|
|
412
878
|
else {
|
|
413
879
|
const key = `${advertised.scope}:${advertised.id}`;
|
|
414
880
|
const pending = provisioning.get(key) ??
|
|
415
|
-
provision(advertised.id, hit.route, envelope.partition);
|
|
881
|
+
provision(advertised.id, hit.route, advertised.version, envelope.partition);
|
|
416
882
|
provisioning.set(key, pending);
|
|
417
883
|
try {
|
|
418
884
|
await pending;
|
|
@@ -431,7 +897,18 @@ export function createRuntime(deps) {
|
|
|
431
897
|
await drainDeferred(publicSide);
|
|
432
898
|
if (principal)
|
|
433
899
|
await drainDeferred(principal);
|
|
900
|
+
const names = [...datasetsDeferred];
|
|
901
|
+
datasetsDeferred.clear();
|
|
902
|
+
for (const name of names)
|
|
903
|
+
await syncDatasetOnce(name);
|
|
904
|
+
const owed = [...principalDeferred];
|
|
905
|
+
principalDeferred.clear();
|
|
906
|
+
const side = principal;
|
|
907
|
+
if (side)
|
|
908
|
+
for (const name of owed)
|
|
909
|
+
await syncDatasetOnce(name, side);
|
|
434
910
|
await analytics.flushAtMoment();
|
|
911
|
+
await flushWrites();
|
|
435
912
|
await revalidateThirdParty();
|
|
436
913
|
}
|
|
437
914
|
}
|
|
@@ -481,16 +958,17 @@ export function createRuntime(deps) {
|
|
|
481
958
|
outgoing = request;
|
|
482
959
|
}
|
|
483
960
|
}
|
|
961
|
+
outgoing = await withHave(outgoing);
|
|
484
962
|
const response = await deps.fetch(outgoing);
|
|
485
963
|
const envelope = decodeEnvelopeOrClear(response.headers);
|
|
486
964
|
await early(envelope, navigation, after?.signedOut);
|
|
487
965
|
// a cacheable GET may be answered by the HTTP cache with no radio wake, so only these count
|
|
488
966
|
const moment = navigation || request.method !== 'GET';
|
|
489
967
|
const clone = after?.clone;
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
: null;
|
|
968
|
+
const kept = typeof clone === 'function' ? clone(response, envelope) : clone;
|
|
969
|
+
const { visitor, copy } = kept ? branch(response) : { visitor: response, copy: null };
|
|
493
970
|
track((async () => {
|
|
971
|
+
await applyServerDelta(response);
|
|
494
972
|
try {
|
|
495
973
|
await after?.run(copy, envelope);
|
|
496
974
|
}
|
|
@@ -499,12 +977,16 @@ export function createRuntime(deps) {
|
|
|
499
977
|
}
|
|
500
978
|
await observe(response.headers, envelope, sent, navigation, moment, after?.provision ?? navigation, () => keepPolicy(request, response, envelope, navigation, after));
|
|
501
979
|
})());
|
|
502
|
-
return
|
|
980
|
+
return visitor;
|
|
503
981
|
}
|
|
504
|
-
/**
|
|
982
|
+
/**
|
|
983
|
+
* `search` is the request's query string; the stored artifact is always the path's own. A
|
|
984
|
+
* principal route answers from the signed-in partition and a public one from the public side.
|
|
985
|
+
*/
|
|
505
986
|
async function decide(path, route, dry, search = '') {
|
|
987
|
+
const principal = route.scope === 'principal';
|
|
506
988
|
let side = publicSide;
|
|
507
|
-
if (
|
|
989
|
+
if (principal) {
|
|
508
990
|
if (!host)
|
|
509
991
|
return { reason: 'principal', staleSec: null };
|
|
510
992
|
const own = await principalSide();
|
|
@@ -513,9 +995,49 @@ export function createRuntime(deps) {
|
|
|
513
995
|
side = own;
|
|
514
996
|
}
|
|
515
997
|
const { replica: rep, scheduler: sched } = side;
|
|
516
|
-
|
|
517
|
-
|
|
998
|
+
// a signed-in read takes its row from the partition's dataset; a public one from the pack
|
|
999
|
+
const heldRow = (dataset) => async (key) => principal
|
|
1000
|
+
? (await rep.datasets.get(dataset))?.rows.get(key)
|
|
1001
|
+
: await pack?.getRow(dataset, key);
|
|
1002
|
+
const page = () => {
|
|
1003
|
+
if (route.family && route.family.key !== 'viewer.id') {
|
|
1004
|
+
return familyPage(heldRow(route.family.dataset), route, path, now());
|
|
1005
|
+
}
|
|
1006
|
+
if (route.origin !== undefined)
|
|
1007
|
+
return collectionPage(path, now());
|
|
1008
|
+
return rep.get(path);
|
|
1009
|
+
};
|
|
1010
|
+
// a same-origin collection answers from the held pack's rows, so a write the pack holds shows at once
|
|
1011
|
+
const packed = route.query?.dataset;
|
|
1012
|
+
if (packed !== undefined &&
|
|
1013
|
+
!principal &&
|
|
1014
|
+
pack &&
|
|
1015
|
+
route.strategy === 'data' &&
|
|
1016
|
+
route.family === undefined &&
|
|
1017
|
+
route.origin === undefined &&
|
|
1018
|
+
route.query?.plan === undefined) {
|
|
1019
|
+
const head = await pack.head();
|
|
1020
|
+
if (head && !head.partial && head.tables.includes(packed)) {
|
|
1021
|
+
const rows = (await pack.scanTable(packed)).map(([, row]) => row);
|
|
1022
|
+
const rendered = await renderLocal(config, route, collectionPage(path, now()).artifact, renderers, search, rows);
|
|
1023
|
+
const headers = rep.policyOf(path) ?? replica.sitePolicy;
|
|
1024
|
+
if (!rendered.ok || (!headers && !dry && !isDataFamily(route))) {
|
|
1025
|
+
return { reason: 'refused', staleSec: null };
|
|
1026
|
+
}
|
|
1027
|
+
return { reason: 'fresh', html: rendered.html, headers, staleSec: 0 };
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
let stored = await page();
|
|
1031
|
+
if (!stored) {
|
|
1032
|
+
// a signed-in family miss seeds its dataset; this view still goes to the network
|
|
1033
|
+
if (principal &&
|
|
1034
|
+
route.family &&
|
|
1035
|
+
!dry &&
|
|
1036
|
+
!(await rep.datasets.get(route.family.dataset))) {
|
|
1037
|
+
seedDataset(route.family.dataset, side);
|
|
1038
|
+
}
|
|
518
1039
|
return { reason: 'cold', staleSec: null };
|
|
1040
|
+
}
|
|
519
1041
|
const staleSec = () => stored ? Math.max(0, Math.floor((now() - rep.validatedAt(stored)) / 1000)) : null;
|
|
520
1042
|
if (!rep.isCurrent(stored.artifact)) {
|
|
521
1043
|
const age = staleSec();
|
|
@@ -529,9 +1051,77 @@ export function createRuntime(deps) {
|
|
|
529
1051
|
await rep.drop([path]);
|
|
530
1052
|
return { reason: 'invalidated', staleSec: age };
|
|
531
1053
|
}
|
|
1054
|
+
// a page answers from the rows the device holds, so their age bounds the answer too
|
|
1055
|
+
const queried = route.query?.dataset;
|
|
1056
|
+
const joined = (route.query?.plan?.join ?? []).map((j) => j.dataset);
|
|
1057
|
+
// a public list query reads the rows of a whole table the held pack carries
|
|
1058
|
+
const listHead = queried !== undefined &&
|
|
1059
|
+
search !== '' &&
|
|
1060
|
+
!principal &&
|
|
1061
|
+
route.scope === 'public' &&
|
|
1062
|
+
route.query?.plan === undefined &&
|
|
1063
|
+
pack
|
|
1064
|
+
? await pack.head()
|
|
1065
|
+
: undefined;
|
|
1066
|
+
const listRows = queried !== undefined &&
|
|
1067
|
+
listHead &&
|
|
1068
|
+
!listHead.partial &&
|
|
1069
|
+
listHead.tables.includes(queried) &&
|
|
1070
|
+
pack
|
|
1071
|
+
? (await pack.scanTable(queried)).map(([, row]) => row)
|
|
1072
|
+
: undefined;
|
|
1073
|
+
const dsNames = [
|
|
1074
|
+
...new Set([
|
|
1075
|
+
...(queried && listRows === undefined ? [queried] : []),
|
|
1076
|
+
...joined,
|
|
1077
|
+
...(principal && route.family ? [route.family.dataset] : []),
|
|
1078
|
+
...(await rep.datasetsOf(stored.artifact))
|
|
1079
|
+
])
|
|
1080
|
+
];
|
|
1081
|
+
// a principal side answers only from the principal datasets the server declared
|
|
1082
|
+
const limits = side === publicSide ? undefined : principalLimits();
|
|
1083
|
+
if (limits && dsNames.some((name) => !limits.has(name))) {
|
|
1084
|
+
return { reason: 'refused', staleSec: staleSec() };
|
|
1085
|
+
}
|
|
1086
|
+
const heldAt = async () => Promise.all(dsNames.map(async (name) => (await rep.datasets.get(name))?.meta.appliedAt));
|
|
1087
|
+
let appliedAt = await heldAt();
|
|
1088
|
+
const missing = dsNames.filter((_, i) => appliedAt[i] === undefined);
|
|
1089
|
+
if (missing.length > 0) {
|
|
1090
|
+
if (!dry)
|
|
1091
|
+
for (const name of missing)
|
|
1092
|
+
seedDataset(name, side);
|
|
1093
|
+
return { reason: 'refused', staleSec: staleSec() };
|
|
1094
|
+
}
|
|
1095
|
+
// held principal rows answer only while the last confirmed sync is inside maxStaleMs
|
|
1096
|
+
const pastLimit = () => limits
|
|
1097
|
+
? dsNames.filter((name, i) => {
|
|
1098
|
+
const age = now() - appliedAt[i];
|
|
1099
|
+
return !(age >= 0 && age < limits.get(name));
|
|
1100
|
+
})
|
|
1101
|
+
: [];
|
|
1102
|
+
// rows past that bound are refreshed before the page shows, as a hard route does
|
|
1103
|
+
const refresh = pastLimit().length > 0;
|
|
1104
|
+
if (refresh && dry)
|
|
1105
|
+
return { reason: 'refused', staleSec: staleSec() };
|
|
1106
|
+
const known = (at) => at.filter((v) => v !== undefined);
|
|
532
1107
|
const fromCatalogue = stored.catalogue === true;
|
|
533
|
-
|
|
534
|
-
|
|
1108
|
+
// a lease the stream renews stands in for the bound; `isCurrent` and the dataset bounds above still apply
|
|
1109
|
+
let action = refresh
|
|
1110
|
+
? 'sync-first'
|
|
1111
|
+
: side.covered && now() < side.leaseUntil
|
|
1112
|
+
? 'serve'
|
|
1113
|
+
: verdict(route.freshness, Math.min(rep.validatedAt(stored), ...known(appliedAt)), now());
|
|
1114
|
+
// adaptive: the first renewal of a visible session is paid, the second becomes the stream
|
|
1115
|
+
if (action === 'sync-first' &&
|
|
1116
|
+
!refresh &&
|
|
1117
|
+
!dry &&
|
|
1118
|
+
side !== publicSide &&
|
|
1119
|
+
config.live?.mode === 'adaptive' &&
|
|
1120
|
+
(liveRenewals += 1) === 2 &&
|
|
1121
|
+
(await wantLive())) {
|
|
1122
|
+
action = 'serve';
|
|
1123
|
+
}
|
|
1124
|
+
const age = Math.max(staleSec(), ...known(appliedAt).map((at) => Math.floor((now() - at) / 1000)));
|
|
535
1125
|
const base = rep.needFor(stored.artifact, route.sync === 'deferred' || route.scope === 'principal');
|
|
536
1126
|
// a principal sync also names the catalogue entries no page has opened, so a revoked one is dropped
|
|
537
1127
|
const need = (route.scope === 'principal' && base ? await rep.sweep(base) : base) ?? undefined;
|
|
@@ -540,34 +1130,53 @@ export function createRuntime(deps) {
|
|
|
540
1130
|
reason = 'hard-sync';
|
|
541
1131
|
if (!dry) {
|
|
542
1132
|
const synced = await sched.sync(need, age);
|
|
543
|
-
|
|
1133
|
+
// a sync that ended the session wiped this side, so its rows are not asked for again
|
|
1134
|
+
if (side.dead)
|
|
1135
|
+
return { reason: 'refused', staleSec: age };
|
|
1136
|
+
// a dataset applied inside both the route's bound and its own is as current as the page may be
|
|
1137
|
+
const late = new Set(pastLimit());
|
|
1138
|
+
const due = dsNames.filter((name, i) => {
|
|
1139
|
+
const since = now() - appliedAt[i];
|
|
1140
|
+
return (late.has(name) || !(since >= 0 && since <= route.freshness.maxStale * 1000));
|
|
1141
|
+
});
|
|
1142
|
+
const dsSynced = (await Promise.all(due.map((name) => syncDatasetOnce(name, side)))).every(datasetCurrent);
|
|
1143
|
+
if (!(synced && dsSynced) && route.offline === 'refuse')
|
|
544
1144
|
return { reason: 'hard-refused', staleSec: age };
|
|
1145
|
+
if (dsNames.length > 0) {
|
|
1146
|
+
appliedAt = await heldAt();
|
|
1147
|
+
if (appliedAt.includes(undefined) || pastLimit().length > 0) {
|
|
1148
|
+
return { reason: 'refused', staleSec: age };
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
545
1151
|
if (synced) {
|
|
546
1152
|
if (need)
|
|
547
1153
|
await rep.validate(path);
|
|
548
|
-
stored = await
|
|
1154
|
+
stored = await page();
|
|
549
1155
|
if (!stored || !rep.isCurrent(stored.artifact)) {
|
|
550
1156
|
return { reason: 'stale-after-sync', staleSec: age };
|
|
551
1157
|
}
|
|
552
1158
|
}
|
|
553
|
-
|
|
1159
|
+
if (!(synced && dsSynced))
|
|
554
1160
|
reason = 'offline-stale';
|
|
555
1161
|
}
|
|
556
1162
|
}
|
|
557
1163
|
else if (action === 'serve-then-sync') {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
1164
|
+
// the sync waits for a network moment: a view made from the device's own copy sends nothing
|
|
1165
|
+
reason = route.sync === 'deferred' ? 'soft-deferred' : 'soft-sync';
|
|
1166
|
+
if (!dry) {
|
|
1167
|
+
// a deferred route syncs only the ids it names; a soft one syncs the whole replica when it names none
|
|
1168
|
+
const queued = route.sync === 'deferred'
|
|
1169
|
+
? need !== undefined && sched.defer(need, age)
|
|
1170
|
+
: sched.queue(need, age);
|
|
1171
|
+
if (queued && need)
|
|
561
1172
|
side.deferred.add(path);
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
reason = 'soft-sync';
|
|
565
|
-
if (!dry) {
|
|
566
|
-
track(sched.sync(need, age).then(async (ok) => {
|
|
567
|
-
if (ok && need)
|
|
568
|
-
await rep.validate(path);
|
|
569
|
-
}));
|
|
1173
|
+
for (const name of dsNames) {
|
|
1174
|
+
(side === publicSide ? datasetsDeferred : principalDeferred).add(name);
|
|
570
1175
|
}
|
|
1176
|
+
// a queue that has waited a lease is sent now, so a device that reads only locally still syncs
|
|
1177
|
+
const waited = config.lease !== undefined && age >= config.lease;
|
|
1178
|
+
if (side === publicSide && waited && (queued || dsNames.length > 0))
|
|
1179
|
+
drainAtLease();
|
|
571
1180
|
}
|
|
572
1181
|
}
|
|
573
1182
|
if (route.strategy === 'static') {
|
|
@@ -578,17 +1187,40 @@ export function createRuntime(deps) {
|
|
|
578
1187
|
}
|
|
579
1188
|
return { reason, response: kept, staleSec: age };
|
|
580
1189
|
}
|
|
581
|
-
//
|
|
1190
|
+
// a page answers only with the policy the site sent with it; an API family is no page
|
|
582
1191
|
const headers = rep.policyOf(path) ?? replica.sitePolicy;
|
|
583
|
-
if (!headers && !dry)
|
|
1192
|
+
if (!headers && !dry && !isDataFamily(route))
|
|
584
1193
|
return { reason: 'refused', staleSec: age };
|
|
585
1194
|
const plain = await rep.materialize(stored.artifact);
|
|
586
1195
|
if (!plain.ok) {
|
|
1196
|
+
// rows of a dataset not held yet: the page is sound, so keep it and seed the dataset
|
|
1197
|
+
if (plain.datasets) {
|
|
1198
|
+
if (!dry)
|
|
1199
|
+
for (const name of plain.datasets)
|
|
1200
|
+
seedDataset(name, side);
|
|
1201
|
+
return { reason: 'refused', staleSec: age };
|
|
1202
|
+
}
|
|
587
1203
|
// a reference with nothing behind it: drop the page so the next network view fetches it whole
|
|
588
1204
|
await rep.drop([path]);
|
|
589
1205
|
return { reason: 'refused', staleSec: age };
|
|
590
1206
|
}
|
|
591
|
-
const
|
|
1207
|
+
const planned = route.query?.plan !== undefined;
|
|
1208
|
+
// a bare public HTML list URL renders the stored page; other reads use the held rows
|
|
1209
|
+
const bareHtml = search === '' && route.strategy !== 'data' && route.scope === 'public';
|
|
1210
|
+
// stored data is the load output, not the origin's page; a bare plan goes to the network
|
|
1211
|
+
if (planned && bareHtml)
|
|
1212
|
+
return { reason: 'refused', staleSec: age };
|
|
1213
|
+
const rows = queried === undefined || planned || bareHtml
|
|
1214
|
+
? undefined
|
|
1215
|
+
: (listRows ?? (await rep.datasets.rows(queried)));
|
|
1216
|
+
if (queried !== undefined && !planned && !bareHtml && !rows)
|
|
1217
|
+
return { reason: 'refused', staleSec: age };
|
|
1218
|
+
const tables = queried !== undefined && planned
|
|
1219
|
+
? await rep.datasets.tables(queried, joined)
|
|
1220
|
+
: undefined;
|
|
1221
|
+
if (queried !== undefined && planned && !tables)
|
|
1222
|
+
return { reason: 'refused', staleSec: age };
|
|
1223
|
+
const rendered = await renderLocal(config, route, plain.artifact, renderers, search, rows, tables);
|
|
592
1224
|
if (!rendered.ok) {
|
|
593
1225
|
// a collection that fails its proof is dropped, so the next network view fetches it whole
|
|
594
1226
|
if (rendered.reason === PROOF_MISMATCH)
|
|
@@ -619,6 +1251,9 @@ export function createRuntime(deps) {
|
|
|
619
1251
|
}
|
|
620
1252
|
async function navigate(path, route, request) {
|
|
621
1253
|
const shadow = runsShadow(config, route, random);
|
|
1254
|
+
// a top-level load is a new page lifetime; a frame belongs to a page that stays open
|
|
1255
|
+
if (request.destination === '' || request.destination === 'document')
|
|
1256
|
+
liveRenewals = 0;
|
|
622
1257
|
const at = now();
|
|
623
1258
|
const gapMs = lastViewAt === null ? null : at - lastViewAt;
|
|
624
1259
|
lastViewAt = at;
|
|
@@ -643,7 +1278,8 @@ export function createRuntime(deps) {
|
|
|
643
1278
|
mismatch,
|
|
644
1279
|
swAgeMs: at - startedAt,
|
|
645
1280
|
gapMs,
|
|
646
|
-
...(from ? { from } : {})
|
|
1281
|
+
...(from ? { from } : {}),
|
|
1282
|
+
...(lifetimes === undefined ? {} : { lifetimes })
|
|
647
1283
|
});
|
|
648
1284
|
const record = async (mismatch) => {
|
|
649
1285
|
if (config.views !== false)
|
|
@@ -699,15 +1335,36 @@ export function createRuntime(deps) {
|
|
|
699
1335
|
if (side)
|
|
700
1336
|
await side.replica.drop([path]);
|
|
701
1337
|
}
|
|
702
|
-
/**
|
|
1338
|
+
/**
|
|
1339
|
+
* answers a GET for a `data` route from the stored value; the page makes no request. A
|
|
1340
|
+
* principal route answers from the signed-in partition, and a cross-origin one only a request
|
|
1341
|
+
* with an `Authorization` header. A public route never answers one. A cross-origin miss goes
|
|
1342
|
+
* out untouched: a spool header on it would force a CORS preflight.
|
|
1343
|
+
* A bearer read of a public route goes out the same way, so the public store never sees it.
|
|
1344
|
+
*/
|
|
703
1345
|
async function dataFile(request, path, route) {
|
|
704
|
-
const
|
|
1346
|
+
const url = new URL(request.url);
|
|
1347
|
+
const remote = url.origin !== origin;
|
|
1348
|
+
const signed = request.headers.has('authorization');
|
|
1349
|
+
// a public route holds no copy per viewer, so a bearer read never takes the store
|
|
1350
|
+
const bearer = signed && route.scope === 'public';
|
|
1351
|
+
// the API answers a cross-origin read with no header as anonymous, so the partition never does
|
|
1352
|
+
const anonymous = remote && route.scope === 'principal' && !signed;
|
|
1353
|
+
const decision = bearer || anonymous
|
|
1354
|
+
? { reason: 'refused', staleSec: null }
|
|
1355
|
+
: await decide(path, route, false, url.search);
|
|
705
1356
|
if (decision.html !== undefined) {
|
|
706
1357
|
ledger.serve(path, decision.reason === 'catalogue');
|
|
707
1358
|
const headers = new Headers(decision.headers);
|
|
708
1359
|
headers.set('content-type', route.type ?? 'application/json');
|
|
1360
|
+
if (remote) {
|
|
1361
|
+
headers.set('access-control-allow-origin', origin);
|
|
1362
|
+
headers.set('vary', 'origin');
|
|
1363
|
+
}
|
|
709
1364
|
return new Response(decision.html, { headers });
|
|
710
1365
|
}
|
|
1366
|
+
if (remote || bearer)
|
|
1367
|
+
return deps.fetch(request);
|
|
711
1368
|
const response = await passthrough(request, false, {
|
|
712
1369
|
provision: true,
|
|
713
1370
|
route,
|
|
@@ -729,8 +1386,11 @@ export function createRuntime(deps) {
|
|
|
729
1386
|
* does not describe; the next view asks the network again.
|
|
730
1387
|
*/
|
|
731
1388
|
async function capture(path, asked, copy) {
|
|
732
|
-
if (!copy || copy.status !== 200 || copy.redirected)
|
|
1389
|
+
if (!copy || copy.status !== 200 || copy.redirected) {
|
|
1390
|
+
// an unread tee branch holds every chunk the visitor reads
|
|
1391
|
+
void copy?.body?.cancel().catch(() => undefined);
|
|
733
1392
|
return;
|
|
1393
|
+
}
|
|
734
1394
|
// a sync in the same view may have replaced the manifest, so the route is read again
|
|
735
1395
|
const route = config.manifest ? matchRoute(config.routes, pathOf(path))?.route : asked;
|
|
736
1396
|
if (route?.strategy !== 'static')
|
|
@@ -788,6 +1448,7 @@ export function createRuntime(deps) {
|
|
|
788
1448
|
function derivable(path, route, response, envelope) {
|
|
789
1449
|
const advertised = envelope?.artifact;
|
|
790
1450
|
return ((route.strategy === 'assembly' || route.strategy === 'template') &&
|
|
1451
|
+
route.family === undefined &&
|
|
791
1452
|
route.scope === 'public' &&
|
|
792
1453
|
envelope?.protocol === PROTOCOL_VERSION &&
|
|
793
1454
|
advertised?.hash !== undefined &&
|
|
@@ -1015,6 +1676,9 @@ export function createRuntime(deps) {
|
|
|
1015
1676
|
// #endregion
|
|
1016
1677
|
async function run(request) {
|
|
1017
1678
|
await ready();
|
|
1679
|
+
const declared = writeFor(config.resourceWrites ?? [], request.method, new URL(request.url), origin);
|
|
1680
|
+
if (declared)
|
|
1681
|
+
return declaredWrite(request, declared);
|
|
1018
1682
|
const route = classify(config, request, origin);
|
|
1019
1683
|
track(analytics.flushIfDue());
|
|
1020
1684
|
if (route.kind === 'logout') {
|
|
@@ -1045,15 +1709,32 @@ export function createRuntime(deps) {
|
|
|
1045
1709
|
return asset(request);
|
|
1046
1710
|
return passthrough(request, request.mode === 'navigate');
|
|
1047
1711
|
}
|
|
1712
|
+
// a cold worker otherwise loses the document to the network while it starts
|
|
1713
|
+
async function navigationRoute(event) {
|
|
1714
|
+
if (typeof event.addRoutes !== 'function')
|
|
1715
|
+
return;
|
|
1716
|
+
try {
|
|
1717
|
+
await event.addRoutes([
|
|
1718
|
+
{ condition: { requestMode: 'navigate' }, source: 'fetch-event' }
|
|
1719
|
+
]);
|
|
1720
|
+
}
|
|
1721
|
+
catch {
|
|
1722
|
+
// a refused route leaves the browser's default race in place
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1048
1725
|
return {
|
|
1049
1726
|
replica,
|
|
1050
1727
|
ledger,
|
|
1051
1728
|
handle(request, wait) {
|
|
1729
|
+
const pending = sweepWrites();
|
|
1730
|
+
if (pending)
|
|
1731
|
+
wait(pending);
|
|
1052
1732
|
const kind = classify(config, request, origin).kind;
|
|
1053
1733
|
if (kind === 'ignore')
|
|
1054
1734
|
return undefined;
|
|
1055
|
-
if (kind === 'asset' && assetMode !== 'cache')
|
|
1056
|
-
return undefined;
|
|
1735
|
+
if (kind === 'asset' && assetMode !== 'cache') {
|
|
1736
|
+
return forwardsAssets && shouldForward(cachePath) ? deps.fetch(request) : undefined;
|
|
1737
|
+
}
|
|
1057
1738
|
// a worker in shadow touches only navigations: subresources and beacons keep the browser's own path
|
|
1058
1739
|
if (observing &&
|
|
1059
1740
|
kind !== 'navigation' &&
|
|
@@ -1061,15 +1742,41 @@ export function createRuntime(deps) {
|
|
|
1061
1742
|
request.mode !== 'navigate') {
|
|
1062
1743
|
return undefined;
|
|
1063
1744
|
}
|
|
1745
|
+
const before = new Set(background);
|
|
1746
|
+
if (forwardsAssets && request.mode === 'navigate' && !probed) {
|
|
1747
|
+
probed = true;
|
|
1748
|
+
const url = new URL(clientPath(config.version), origin).href;
|
|
1749
|
+
const probe = probeCache(url, (input, init) => deps.fetch(input, init), resourceTiming);
|
|
1750
|
+
track(probe.then((result) => {
|
|
1751
|
+
cachePath = result;
|
|
1752
|
+
}));
|
|
1753
|
+
}
|
|
1064
1754
|
const work = kind === 'asset' && !manifestPending() ? asset(request) : run(request);
|
|
1065
|
-
return work
|
|
1755
|
+
return work
|
|
1756
|
+
.catch(() => deps.fetch(request))
|
|
1757
|
+
.finally(() => wait(settleBackground(before)));
|
|
1066
1758
|
},
|
|
1067
1759
|
async install(event) {
|
|
1068
1760
|
if (assetMode === 'static-routing')
|
|
1069
1761
|
routed = await installRoutes(event, config, origin);
|
|
1762
|
+
else
|
|
1763
|
+
await navigationRoute(event);
|
|
1764
|
+
if (config.pack && pack)
|
|
1765
|
+
await adoptPack(pack, config.pack);
|
|
1766
|
+
// the stub is the server's answer, so an install that reads it is a contact
|
|
1767
|
+
if (config.stamp !== undefined || config.pack !== undefined)
|
|
1768
|
+
contacted();
|
|
1070
1769
|
},
|
|
1071
1770
|
async activate() {
|
|
1771
|
+
const pending = sweepWrites();
|
|
1772
|
+
if (pending)
|
|
1773
|
+
track(pending);
|
|
1072
1774
|
await ready();
|
|
1775
|
+
if (config.policy !== undefined && replica.sitePolicy === undefined) {
|
|
1776
|
+
const policy = allowedPolicy(config.policy);
|
|
1777
|
+
if (policy)
|
|
1778
|
+
await replica.keepPolicy(null, policy);
|
|
1779
|
+
}
|
|
1073
1780
|
for (const name of await deps.caches.keys()) {
|
|
1074
1781
|
const old = CACHE_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
1075
1782
|
if (old && name !== assetCache && name !== staticCache)
|
|
@@ -1077,13 +1784,18 @@ export function createRuntime(deps) {
|
|
|
1077
1784
|
}
|
|
1078
1785
|
},
|
|
1079
1786
|
async message(data) {
|
|
1787
|
+
const pending = sweepWrites();
|
|
1788
|
+
if (pending)
|
|
1789
|
+
track(pending);
|
|
1080
1790
|
const message = (data ?? {});
|
|
1081
1791
|
if (message.type === 'spool:hello') {
|
|
1082
1792
|
return {
|
|
1083
1793
|
type: 'spool:hello',
|
|
1084
1794
|
protocol: PROTOCOL_VERSION,
|
|
1085
1795
|
version: config.version,
|
|
1086
|
-
compatible: message.protocol === PROTOCOL_VERSION
|
|
1796
|
+
compatible: message.protocol === PROTOCOL_VERSION,
|
|
1797
|
+
...(config.lease === undefined ? {} : { lease: config.lease }),
|
|
1798
|
+
...(lastContact === 0 ? {} : { contact: lastContact })
|
|
1087
1799
|
};
|
|
1088
1800
|
}
|
|
1089
1801
|
if (message.type === 'spool:status') {
|
|
@@ -1116,6 +1828,13 @@ export function createRuntime(deps) {
|
|
|
1116
1828
|
}
|
|
1117
1829
|
return undefined;
|
|
1118
1830
|
}
|
|
1831
|
+
if (message.type === 'spool:lifetimes') {
|
|
1832
|
+
const count = message.count;
|
|
1833
|
+
if (typeof count === 'number' && Number.isSafeInteger(count) && count >= 0) {
|
|
1834
|
+
lifetimes = count;
|
|
1835
|
+
}
|
|
1836
|
+
return undefined;
|
|
1837
|
+
}
|
|
1119
1838
|
if (message.type === 'spool:graph') {
|
|
1120
1839
|
await ready();
|
|
1121
1840
|
await replica.held();
|
|
@@ -1136,7 +1855,8 @@ export function createRuntime(deps) {
|
|
|
1136
1855
|
partition: null,
|
|
1137
1856
|
artifacts: [],
|
|
1138
1857
|
records: [],
|
|
1139
|
-
projections: []
|
|
1858
|
+
projections: [],
|
|
1859
|
+
datasets: []
|
|
1140
1860
|
};
|
|
1141
1861
|
}
|
|
1142
1862
|
await side.replica.held();
|
|
@@ -1145,12 +1865,88 @@ export function createRuntime(deps) {
|
|
|
1145
1865
|
partition: side.id,
|
|
1146
1866
|
artifacts: await side.replica.ids(),
|
|
1147
1867
|
records: side.replica.graph.records.ids(),
|
|
1148
|
-
projections: side.replica.graph.projections.ids()
|
|
1868
|
+
projections: side.replica.graph.projections.ids(),
|
|
1869
|
+
datasets: await side.replica.datasets.list()
|
|
1149
1870
|
};
|
|
1150
1871
|
}
|
|
1872
|
+
if (message.type === 'spool:live-state')
|
|
1873
|
+
return liveState();
|
|
1874
|
+
if (message.type === 'spool:live') {
|
|
1875
|
+
const { partition, event, delta, leaseMs } = message;
|
|
1876
|
+
if (partition !== null && typeof partition !== 'string')
|
|
1877
|
+
return undefined;
|
|
1878
|
+
if (event === 'revoked')
|
|
1879
|
+
return liveEvent({ partition, event });
|
|
1880
|
+
if (event === 'session')
|
|
1881
|
+
return liveEvent({ partition: null, event });
|
|
1882
|
+
if (event === 'lease' && typeof leaseMs === 'number' && leaseMs > 0) {
|
|
1883
|
+
return liveEvent({ partition, event, leaseMs });
|
|
1884
|
+
}
|
|
1885
|
+
if (event === 'delta') {
|
|
1886
|
+
try {
|
|
1887
|
+
return liveEvent({ partition, event, delta: parseLiveDelta(delta) });
|
|
1888
|
+
}
|
|
1889
|
+
catch {
|
|
1890
|
+
return undefined;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
return undefined;
|
|
1894
|
+
}
|
|
1151
1895
|
if (message.type === 'spool:assets') {
|
|
1152
1896
|
return { type: 'spool:assets', mode: assetMode, routed };
|
|
1153
1897
|
}
|
|
1898
|
+
if (message.type === 'spool:datasets') {
|
|
1899
|
+
await ready();
|
|
1900
|
+
return {
|
|
1901
|
+
type: 'spool:datasets',
|
|
1902
|
+
datasets: await replica.datasets.list(),
|
|
1903
|
+
outcomes: { ...datasetCounts },
|
|
1904
|
+
refused: Object.fromEntries(datasetRefused),
|
|
1905
|
+
declined: Object.fromEntries(declined),
|
|
1906
|
+
evicted: replica.datasets.evicted,
|
|
1907
|
+
persisted
|
|
1908
|
+
};
|
|
1909
|
+
}
|
|
1910
|
+
if (message.type === 'spool:persisted') {
|
|
1911
|
+
persisted = message.granted === true;
|
|
1912
|
+
return undefined;
|
|
1913
|
+
}
|
|
1914
|
+
if (message.type === 'spool:write') {
|
|
1915
|
+
const { name, key, value, version } = message;
|
|
1916
|
+
const queue = await pageQueue();
|
|
1917
|
+
return {
|
|
1918
|
+
type: 'spool:write',
|
|
1919
|
+
result: queue instanceof WriteQueue
|
|
1920
|
+
? await queue.write(typeof name === 'string' ? name : '', typeof key === 'string' ? key : '', value, version)
|
|
1921
|
+
: queue
|
|
1922
|
+
};
|
|
1923
|
+
}
|
|
1924
|
+
if (message.type === 'spool:flush') {
|
|
1925
|
+
const queue = await pageQueue();
|
|
1926
|
+
return {
|
|
1927
|
+
type: 'spool:flush',
|
|
1928
|
+
result: queue instanceof WriteQueue ? { ok: true, ...(await queue.flush()) } : queue
|
|
1929
|
+
};
|
|
1930
|
+
}
|
|
1931
|
+
if (message.type === 'spool:writes') {
|
|
1932
|
+
const queue = await pageQueue();
|
|
1933
|
+
return {
|
|
1934
|
+
type: 'spool:writes',
|
|
1935
|
+
result: queue instanceof WriteQueue
|
|
1936
|
+
? { ok: true, writes: await queue.list() }
|
|
1937
|
+
: queue
|
|
1938
|
+
};
|
|
1939
|
+
}
|
|
1940
|
+
if (message.type === 'spool:resolve') {
|
|
1941
|
+
const { name, key, use } = message;
|
|
1942
|
+
const queue = await pageQueue();
|
|
1943
|
+
return {
|
|
1944
|
+
type: 'spool:resolve',
|
|
1945
|
+
result: queue instanceof WriteQueue
|
|
1946
|
+
? await queue.resolve(typeof name === 'string' ? name : '', typeof key === 'string' ? key : '', use)
|
|
1947
|
+
: queue
|
|
1948
|
+
};
|
|
1949
|
+
}
|
|
1154
1950
|
if (message.type === 'spool:held') {
|
|
1155
1951
|
const cache = await cacheNamed(THIRD_CACHE);
|
|
1156
1952
|
return {
|