@drupflare/spool 0.4.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 +206 -1756
- 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 +91 -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 +11 -1
- package/dist/client/register.js +19 -8
- 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 +65 -1
- package/dist/protocol/config.js +28 -0
- package/dist/protocol/dataset.d.ts +6 -0
- package/dist/protocol/dataset.js +6 -0
- package/dist/protocol/envelope.d.ts +33 -1
- package/dist/protocol/envelope.js +15 -1
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/index-rows.d.ts +22 -0
- package/dist/protocol/index-rows.js +53 -0
- package/dist/protocol/index.d.ts +2 -0
- package/dist/protocol/index.js +2 -0
- package/dist/protocol/normalise.d.ts +4 -4
- package/dist/protocol/normalise.js +9 -5
- package/dist/protocol/ring.d.ts +58 -0
- package/dist/protocol/ring.js +6 -0
- package/dist/protocol/route.d.ts +57 -11
- package/dist/protocol/route.js +95 -3
- 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 +92 -4
- package/dist/protocol/write.js +251 -9
- 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/demand.d.ts +49 -0
- package/dist/report/demand.js +60 -0
- package/dist/report/economics.d.ts +55 -0
- package/dist/report/economics.js +189 -0
- package/dist/report/headline.d.ts +158 -0
- package/dist/report/headline.js +473 -0
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +2 -0
- package/dist/report/inputs.d.ts +79 -0
- package/dist/report/inputs.js +115 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +5 -1
- package/dist/report/traffic.d.ts +8 -0
- package/dist/report/traffic.js +7 -0
- package/dist/server/bundle.d.ts +55 -0
- package/dist/server/bundle.js +172 -0
- package/dist/server/dataset.d.ts +8 -2
- package/dist/server/dataset.js +51 -4
- package/dist/server/embedded.d.ts +7 -3
- package/dist/server/embedded.js +7 -3
- package/dist/server/index.d.ts +5 -1
- package/dist/server/index.js +5 -1
- 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 +139 -4
- package/dist/server/spool.js +457 -28
- 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 +23 -1
- package/dist/server/write.js +96 -0
- package/dist/stores/durable-object.d.ts +46 -0
- package/dist/stores/durable-object.js +114 -0
- package/dist/sw/analytics.d.ts +1 -1
- package/dist/sw/analytics.js +2 -2
- package/dist/sw/coverage.d.ts +6 -0
- package/dist/sw/coverage.js +34 -0
- package/dist/sw/family.d.ts +15 -0
- package/dist/sw/family.js +48 -0
- package/dist/sw/idb.d.ts +2 -0
- package/dist/sw/idb.js +2 -2
- 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/policy.d.ts +2 -0
- package/dist/sw/policy.js +11 -0
- package/dist/sw/probe.d.ts +16 -0
- package/dist/sw/probe.js +25 -0
- package/dist/sw/router.d.ts +3 -2
- package/dist/sw/router.js +15 -2
- package/dist/sw/runtime.d.ts +48 -1
- package/dist/sw/runtime.js +548 -68
- package/dist/sw/scheduler.d.ts +6 -1
- package/dist/sw/scheduler.js +22 -5
- package/dist/sw/write.d.ts +41 -1
- package/dist/sw/write.js +188 -5
- 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 +64 -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,12 +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 } from '../protocol/write.js';
|
|
13
|
+
import { ifMatch, WRITE_HEADERS, writeFor } from '../protocol/write.js';
|
|
14
14
|
import { BeaconQueue, LOGOUT_SEND_MS } from './analytics.js';
|
|
15
15
|
import { collectorAnswer } from './collect.js';
|
|
16
16
|
import { principalGuard, syncDataset } from './dataset-sync.js';
|
|
17
|
+
import { collectionPage, familyPage } from './family.js';
|
|
17
18
|
import { verdict } from './freshness.js';
|
|
18
|
-
import {
|
|
19
|
+
import { createPackStore } from './pack/store.js';
|
|
20
|
+
import { allowedPolicy, policyHeaders } from './policy.js';
|
|
21
|
+
import { probeCache, resourceTiming, shouldForward } from './probe.js';
|
|
19
22
|
import { Ledger, plan, priorFrom } from './provision.js';
|
|
20
23
|
import { renderLocal } from './render.js';
|
|
21
24
|
import { Replica } from './replica.js';
|
|
@@ -25,7 +28,7 @@ import { Scheduler } from './scheduler.js';
|
|
|
25
28
|
import { differs, modeFor, runsShadow } from './shadow.js';
|
|
26
29
|
import { branch, matchesVersion, MAX_STATIC_BYTES, readCapped, replayHeaders, STATIC_CACHE } from './static.js';
|
|
27
30
|
import { THIRD_CACHE, THIRD_HEADERS, versionClass } from './thirdparty.js';
|
|
28
|
-
import { WriteQueue } from './write.js';
|
|
31
|
+
import { settleWrite, WriteQueue } from './write.js';
|
|
29
32
|
/** the strategies a catalogue entry may have: data for a template or renderer the device holds */
|
|
30
33
|
const ENTRY_STRATEGIES = ['template', 'projection', 'data'];
|
|
31
34
|
const ASSET_CACHE = 'spool-assets-';
|
|
@@ -35,6 +38,8 @@ const DAY_MS = 86_400_000;
|
|
|
35
38
|
const encoder = new TextEncoder();
|
|
36
39
|
/** a static file's key is its pathname plus query string; this is the pathname a route matches */
|
|
37
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);
|
|
38
43
|
function immutable(response) {
|
|
39
44
|
const control = response.headers.get('cache-control') ?? '';
|
|
40
45
|
if (/(?:^|,)\s*immutable\b/i.test(control))
|
|
@@ -50,28 +55,38 @@ export function createRuntime(deps) {
|
|
|
50
55
|
const random = deps.random ?? Math.random;
|
|
51
56
|
const engine = engineOf(deps.userAgent ?? (typeof navigator === 'undefined' ? '' : navigator.userAgent));
|
|
52
57
|
const prior = priorFrom(config.provision);
|
|
53
|
-
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);
|
|
54
62
|
const ledger = new Ledger();
|
|
55
63
|
const background = new Set();
|
|
56
64
|
const provisioning = new Map();
|
|
57
65
|
const declined = new Map();
|
|
66
|
+
// a 404 or a decline holds for the version it came back at
|
|
67
|
+
const unavailable = new Map();
|
|
58
68
|
const wants = new Set();
|
|
59
69
|
const asked = new Set();
|
|
60
70
|
let currentPath = null;
|
|
61
71
|
const latestView = new Map();
|
|
62
|
-
const assetCache = `${ASSET_CACHE}${
|
|
63
|
-
const staticCache = `${STATIC_CACHE}${
|
|
72
|
+
const assetCache = `${ASSET_CACHE}${codeVersion}`;
|
|
73
|
+
const staticCache = `${STATIC_CACHE}${codeVersion}`;
|
|
64
74
|
const keyOf = (path) => new Request(`${origin}${path}`);
|
|
65
75
|
const startedAt = now();
|
|
66
76
|
const assetMode = config.assets ?? 'cache';
|
|
67
77
|
let routed = false;
|
|
68
78
|
let lastViewAt = null;
|
|
79
|
+
let lifetimes;
|
|
69
80
|
let loaded;
|
|
70
81
|
let manifestLoad;
|
|
71
82
|
let partitionLoad;
|
|
72
83
|
let principal = null;
|
|
73
84
|
let exclusive = Promise.resolve();
|
|
74
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;
|
|
75
90
|
const manifestPending = () => config.manifest !== undefined && config.routes.length === 0;
|
|
76
91
|
const handles = new Map();
|
|
77
92
|
/** opens a cache once and keeps the handle, so a hot request does not reopen it */
|
|
@@ -122,6 +137,102 @@ export function createRuntime(deps) {
|
|
|
122
137
|
await early(decodeEnvelopeOrClear(response.headers), true);
|
|
123
138
|
return parseSyncResponse(await response.json());
|
|
124
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
|
+
}
|
|
125
236
|
const policy = {
|
|
126
237
|
changesPerDay: prior.changesPerDay,
|
|
127
238
|
staleBytes: prior.avoidedBytes,
|
|
@@ -140,6 +251,8 @@ export function createRuntime(deps) {
|
|
|
140
251
|
const hit = matchRoute(config.routes, path);
|
|
141
252
|
if (!hit || hit.route.scope !== 'public' || hit.route.strategy === 'static')
|
|
142
253
|
continue;
|
|
254
|
+
if (hit.route.family || hit.route.origin !== undefined)
|
|
255
|
+
continue;
|
|
143
256
|
hot.push({
|
|
144
257
|
id: path,
|
|
145
258
|
kind: 'route',
|
|
@@ -158,7 +271,15 @@ export function createRuntime(deps) {
|
|
|
158
271
|
}
|
|
159
272
|
}
|
|
160
273
|
/** the page the last view was on, when its route declares a catalogue; the queue skips a recent ask */
|
|
161
|
-
|
|
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
|
+
};
|
|
162
283
|
/**
|
|
163
284
|
* Stores the entries a flush brought, each marked unopened. An entry is kept only for a route
|
|
164
285
|
* whose strategy and scope match it. A principal entry is kept only when the answer names the
|
|
@@ -263,7 +384,9 @@ export function createRuntime(deps) {
|
|
|
263
384
|
scheduler: makeScheduler(replica),
|
|
264
385
|
deferred: new Set(),
|
|
265
386
|
loaded: Promise.resolve(),
|
|
266
|
-
dead: false
|
|
387
|
+
dead: false,
|
|
388
|
+
covered: false,
|
|
389
|
+
leaseUntil: 0
|
|
267
390
|
};
|
|
268
391
|
const scheduler = publicSide.scheduler;
|
|
269
392
|
// #region datasets
|
|
@@ -376,14 +499,16 @@ export function createRuntime(deps) {
|
|
|
376
499
|
: 'session';
|
|
377
500
|
function makeSide(id) {
|
|
378
501
|
const sealed = host.open(id, retentionOf, sharedRetention(), config.principalDatasets !== undefined);
|
|
379
|
-
const target = new Replica(sealed,
|
|
502
|
+
const target = new Replica(sealed, codeVersion, now);
|
|
380
503
|
return {
|
|
381
504
|
id,
|
|
382
505
|
replica: target,
|
|
383
506
|
scheduler: makeScheduler(target),
|
|
384
507
|
deferred: new Set(),
|
|
385
508
|
loaded: target.load(),
|
|
386
|
-
dead: false
|
|
509
|
+
dead: false,
|
|
510
|
+
covered: false,
|
|
511
|
+
leaseUntil: 0
|
|
387
512
|
};
|
|
388
513
|
}
|
|
389
514
|
async function loadPartition() {
|
|
@@ -407,10 +532,12 @@ export function createRuntime(deps) {
|
|
|
407
532
|
for (const name of principalLimits().keys())
|
|
408
533
|
datasetRefused.delete(name);
|
|
409
534
|
}
|
|
410
|
-
/** shuts the principal store, shreds its keys and deletes it; true when there was one */
|
|
411
|
-
function wipePartition(reason) {
|
|
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) {
|
|
412
537
|
return serial(async () => {
|
|
413
538
|
await ready();
|
|
539
|
+
if (only !== undefined && principal?.id !== only)
|
|
540
|
+
return false;
|
|
414
541
|
const old = principal;
|
|
415
542
|
principal = null;
|
|
416
543
|
old?.writes?.kill();
|
|
@@ -432,6 +559,7 @@ export function createRuntime(deps) {
|
|
|
432
559
|
if (old?.id === id)
|
|
433
560
|
return;
|
|
434
561
|
principal = makeSide(id);
|
|
562
|
+
swept = false;
|
|
435
563
|
old?.writes?.kill();
|
|
436
564
|
if (old)
|
|
437
565
|
retire(old);
|
|
@@ -442,6 +570,97 @@ export function createRuntime(deps) {
|
|
|
442
570
|
}
|
|
443
571
|
});
|
|
444
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
|
+
}
|
|
445
664
|
/**
|
|
446
665
|
* What a response says about who is signed in, applied before the response is handed back so no
|
|
447
666
|
* later view reads a store the response outdated. `Spool-Clear` wipes everything or one
|
|
@@ -450,6 +669,9 @@ export function createRuntime(deps) {
|
|
|
450
669
|
* required sync after a sign-in the worker never saw swaps the store before its result is used.
|
|
451
670
|
*/
|
|
452
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();
|
|
453
675
|
if (!host || !envelope || envelope.protocol !== PROTOCOL_VERSION)
|
|
454
676
|
return;
|
|
455
677
|
await ready();
|
|
@@ -484,7 +706,7 @@ export function createRuntime(deps) {
|
|
|
484
706
|
});
|
|
485
707
|
async function sendWrite(record) {
|
|
486
708
|
const query = `name=${encodeURIComponent(record.name)}&key=${encodeURIComponent(record.key)}`;
|
|
487
|
-
const
|
|
709
|
+
const request = await withHave(new Request(`${origin}${PATHS.write}?${query}`, {
|
|
488
710
|
method: 'PUT',
|
|
489
711
|
headers: {
|
|
490
712
|
'content-type': 'application/json',
|
|
@@ -494,7 +716,10 @@ export function createRuntime(deps) {
|
|
|
494
716
|
body: JSON.stringify({ value: record.value }),
|
|
495
717
|
cache: 'no-store',
|
|
496
718
|
credentials: 'same-origin'
|
|
497
|
-
});
|
|
719
|
+
}));
|
|
720
|
+
const response = await deps.fetch(request);
|
|
721
|
+
// the write answer carries no envelope, so the answer itself is the contact
|
|
722
|
+
contacted();
|
|
498
723
|
const body = await response.json().catch(() => undefined);
|
|
499
724
|
return { status: response.status, body };
|
|
500
725
|
}
|
|
@@ -506,7 +731,8 @@ export function createRuntime(deps) {
|
|
|
506
731
|
classes: () => config.writes ?? [],
|
|
507
732
|
now,
|
|
508
733
|
send: sendWrite,
|
|
509
|
-
onFlush: (flush) => deps.broadcast?.({ type: 'spool:written', ...flush })
|
|
734
|
+
onFlush: (flush) => deps.broadcast?.({ type: 'spool:written', ...flush }),
|
|
735
|
+
timers: deps.timers
|
|
510
736
|
});
|
|
511
737
|
return side.writes;
|
|
512
738
|
}
|
|
@@ -524,8 +750,26 @@ export function createRuntime(deps) {
|
|
|
524
750
|
if (queue)
|
|
525
751
|
await queue.flush();
|
|
526
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
|
+
}
|
|
527
763
|
// #endregion
|
|
528
|
-
|
|
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) {
|
|
529
773
|
const side = route.scope === 'principal' ? await principalSide() : publicSide;
|
|
530
774
|
if (!side || (route.scope === 'principal' && side.id !== partition))
|
|
531
775
|
return;
|
|
@@ -534,9 +778,12 @@ export function createRuntime(deps) {
|
|
|
534
778
|
const response = await deps.fetch(`${origin}${PATHS.artifact}?path=${encodeURIComponent(path)}${query}`, { cache: 'no-store', credentials: 'same-origin' });
|
|
535
779
|
if (response.status === 204 && response.headers.get(HEADERS.provision) === 'declined') {
|
|
536
780
|
declined.set(path, response.headers.get(HEADERS.provisionReason));
|
|
781
|
+
unavailable.set(path, version);
|
|
537
782
|
return;
|
|
538
783
|
}
|
|
539
784
|
if (!response.ok) {
|
|
785
|
+
if (response.status === 404)
|
|
786
|
+
unavailable.set(path, version);
|
|
540
787
|
if (route.scope === 'principal' && [401, 403, 404].includes(response.status)) {
|
|
541
788
|
await side.replica.drop([path]);
|
|
542
789
|
}
|
|
@@ -556,6 +803,22 @@ export function createRuntime(deps) {
|
|
|
556
803
|
}
|
|
557
804
|
if (await side.replica.put(artifact, route.persist)) {
|
|
558
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);
|
|
559
822
|
}
|
|
560
823
|
}
|
|
561
824
|
async function drainDeferred(side) {
|
|
@@ -565,6 +828,24 @@ export function createRuntime(deps) {
|
|
|
565
828
|
for (const path of paths)
|
|
566
829
|
await side.replica.validate(path);
|
|
567
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
|
+
}
|
|
568
849
|
async function observe(headers, envelope, sent, navigation, moment, provisionable, keep) {
|
|
569
850
|
await keep();
|
|
570
851
|
if (envelope && envelope.protocol === PROTOCOL_VERSION) {
|
|
@@ -580,18 +861,24 @@ export function createRuntime(deps) {
|
|
|
580
861
|
const own = advertised.scope === 'principal' ? await principalSide() : publicSide;
|
|
581
862
|
const fits = hit &&
|
|
582
863
|
hit.route.strategy !== 'static' &&
|
|
864
|
+
hit.route.provisionable !== false &&
|
|
583
865
|
hit.route.scope === advertised.scope &&
|
|
584
866
|
!declined.has(advertised.id) &&
|
|
867
|
+
unavailable.get(advertised.id) !== advertised.version &&
|
|
585
868
|
own &&
|
|
586
869
|
(advertised.scope === 'public' || own.id === envelope.partition);
|
|
587
|
-
if (hit &&
|
|
870
|
+
if (hit &&
|
|
871
|
+
own &&
|
|
872
|
+
fits &&
|
|
873
|
+
!(await packRenders(advertised.id, hit.route)) &&
|
|
874
|
+
!(await own.replica.get(advertised.id))) {
|
|
588
875
|
if (advertised.scope === 'public' && modeFor(config, hit.route) === 'shadow') {
|
|
589
876
|
wants.add(advertised.id);
|
|
590
877
|
}
|
|
591
878
|
else {
|
|
592
879
|
const key = `${advertised.scope}:${advertised.id}`;
|
|
593
880
|
const pending = provisioning.get(key) ??
|
|
594
|
-
provision(advertised.id, hit.route, envelope.partition);
|
|
881
|
+
provision(advertised.id, hit.route, advertised.version, envelope.partition);
|
|
595
882
|
provisioning.set(key, pending);
|
|
596
883
|
try {
|
|
597
884
|
await pending;
|
|
@@ -671,6 +958,7 @@ export function createRuntime(deps) {
|
|
|
671
958
|
outgoing = request;
|
|
672
959
|
}
|
|
673
960
|
}
|
|
961
|
+
outgoing = await withHave(outgoing);
|
|
674
962
|
const response = await deps.fetch(outgoing);
|
|
675
963
|
const envelope = decodeEnvelopeOrClear(response.headers);
|
|
676
964
|
await early(envelope, navigation, after?.signedOut);
|
|
@@ -680,6 +968,7 @@ export function createRuntime(deps) {
|
|
|
680
968
|
const kept = typeof clone === 'function' ? clone(response, envelope) : clone;
|
|
681
969
|
const { visitor, copy } = kept ? branch(response) : { visitor: response, copy: null };
|
|
682
970
|
track((async () => {
|
|
971
|
+
await applyServerDelta(response);
|
|
683
972
|
try {
|
|
684
973
|
await after?.run(copy, envelope);
|
|
685
974
|
}
|
|
@@ -690,10 +979,14 @@ export function createRuntime(deps) {
|
|
|
690
979
|
})());
|
|
691
980
|
return visitor;
|
|
692
981
|
}
|
|
693
|
-
/**
|
|
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
|
+
*/
|
|
694
986
|
async function decide(path, route, dry, search = '') {
|
|
987
|
+
const principal = route.scope === 'principal';
|
|
695
988
|
let side = publicSide;
|
|
696
|
-
if (
|
|
989
|
+
if (principal) {
|
|
697
990
|
if (!host)
|
|
698
991
|
return { reason: 'principal', staleSec: null };
|
|
699
992
|
const own = await principalSide();
|
|
@@ -702,9 +995,49 @@ export function createRuntime(deps) {
|
|
|
702
995
|
side = own;
|
|
703
996
|
}
|
|
704
997
|
const { replica: rep, scheduler: sched } = side;
|
|
705
|
-
|
|
706
|
-
|
|
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
|
+
}
|
|
707
1039
|
return { reason: 'cold', staleSec: null };
|
|
1040
|
+
}
|
|
708
1041
|
const staleSec = () => stored ? Math.max(0, Math.floor((now() - rep.validatedAt(stored)) / 1000)) : null;
|
|
709
1042
|
if (!rep.isCurrent(stored.artifact)) {
|
|
710
1043
|
const age = staleSec();
|
|
@@ -721,10 +1054,27 @@ export function createRuntime(deps) {
|
|
|
721
1054
|
// a page answers from the rows the device holds, so their age bounds the answer too
|
|
722
1055
|
const queried = route.query?.dataset;
|
|
723
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;
|
|
724
1073
|
const dsNames = [
|
|
725
1074
|
...new Set([
|
|
726
|
-
...(queried ? [queried] : []),
|
|
1075
|
+
...(queried && listRows === undefined ? [queried] : []),
|
|
727
1076
|
...joined,
|
|
1077
|
+
...(principal && route.family ? [route.family.dataset] : []),
|
|
728
1078
|
...(await rep.datasetsOf(stored.artifact))
|
|
729
1079
|
])
|
|
730
1080
|
];
|
|
@@ -742,22 +1092,35 @@ export function createRuntime(deps) {
|
|
|
742
1092
|
seedDataset(name, side);
|
|
743
1093
|
return { reason: 'refused', staleSec: staleSec() };
|
|
744
1094
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
1095
|
+
// held principal rows answer only while the last confirmed sync is inside maxStaleMs
|
|
1096
|
+
const pastLimit = () => limits
|
|
1097
|
+
? dsNames.filter((name, i) => {
|
|
748
1098
|
const age = now() - appliedAt[i];
|
|
749
1099
|
return !(age >= 0 && age < limits.get(name));
|
|
750
|
-
})
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
}
|
|
757
|
-
}
|
|
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() };
|
|
758
1106
|
const known = (at) => at.filter((v) => v !== undefined);
|
|
759
1107
|
const fromCatalogue = stored.catalogue === true;
|
|
760
|
-
|
|
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
|
+
}
|
|
761
1124
|
const age = Math.max(staleSec(), ...known(appliedAt).map((at) => Math.floor((now() - at) / 1000)));
|
|
762
1125
|
const base = rep.needFor(stored.artifact, route.sync === 'deferred' || route.scope === 'principal');
|
|
763
1126
|
// a principal sync also names the catalogue entries no page has opened, so a revoked one is dropped
|
|
@@ -767,18 +1130,28 @@ export function createRuntime(deps) {
|
|
|
767
1130
|
reason = 'hard-sync';
|
|
768
1131
|
if (!dry) {
|
|
769
1132
|
const synced = await sched.sync(need, age);
|
|
770
|
-
|
|
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);
|
|
771
1143
|
if (!(synced && dsSynced) && route.offline === 'refuse')
|
|
772
1144
|
return { reason: 'hard-refused', staleSec: age };
|
|
773
1145
|
if (dsNames.length > 0) {
|
|
774
1146
|
appliedAt = await heldAt();
|
|
775
|
-
if (appliedAt.includes(undefined))
|
|
1147
|
+
if (appliedAt.includes(undefined) || pastLimit().length > 0) {
|
|
776
1148
|
return { reason: 'refused', staleSec: age };
|
|
1149
|
+
}
|
|
777
1150
|
}
|
|
778
1151
|
if (synced) {
|
|
779
1152
|
if (need)
|
|
780
1153
|
await rep.validate(path);
|
|
781
|
-
stored = await
|
|
1154
|
+
stored = await page();
|
|
782
1155
|
if (!stored || !rep.isCurrent(stored.artifact)) {
|
|
783
1156
|
return { reason: 'stale-after-sync', staleSec: age };
|
|
784
1157
|
}
|
|
@@ -788,26 +1161,22 @@ export function createRuntime(deps) {
|
|
|
788
1161
|
}
|
|
789
1162
|
}
|
|
790
1163
|
else if (action === 'serve-then-sync') {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
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)
|
|
794
1172
|
side.deferred.add(path);
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
(side === publicSide ? datasetsDeferred : principalDeferred).add(name);
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
else {
|
|
802
|
-
reason = 'soft-sync';
|
|
803
|
-
if (!dry) {
|
|
804
|
-
track(sched.sync(need, age).then(async (ok) => {
|
|
805
|
-
if (ok && need)
|
|
806
|
-
await rep.validate(path);
|
|
807
|
-
}));
|
|
808
|
-
for (const name of dsNames)
|
|
809
|
-
track(syncDatasetOnce(name, side));
|
|
1173
|
+
for (const name of dsNames) {
|
|
1174
|
+
(side === publicSide ? datasetsDeferred : principalDeferred).add(name);
|
|
810
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();
|
|
811
1180
|
}
|
|
812
1181
|
}
|
|
813
1182
|
if (route.strategy === 'static') {
|
|
@@ -818,9 +1187,9 @@ export function createRuntime(deps) {
|
|
|
818
1187
|
}
|
|
819
1188
|
return { reason, response: kept, staleSec: age };
|
|
820
1189
|
}
|
|
821
|
-
//
|
|
1190
|
+
// a page answers only with the policy the site sent with it; an API family is no page
|
|
822
1191
|
const headers = rep.policyOf(path) ?? replica.sitePolicy;
|
|
823
|
-
if (!headers && !dry)
|
|
1192
|
+
if (!headers && !dry && !isDataFamily(route))
|
|
824
1193
|
return { reason: 'refused', staleSec: age };
|
|
825
1194
|
const plain = await rep.materialize(stored.artifact);
|
|
826
1195
|
if (!plain.ok) {
|
|
@@ -836,8 +1205,15 @@ export function createRuntime(deps) {
|
|
|
836
1205
|
return { reason: 'refused', staleSec: age };
|
|
837
1206
|
}
|
|
838
1207
|
const planned = route.query?.plan !== undefined;
|
|
839
|
-
|
|
840
|
-
|
|
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)
|
|
841
1217
|
return { reason: 'refused', staleSec: age };
|
|
842
1218
|
const tables = queried !== undefined && planned
|
|
843
1219
|
? await rep.datasets.tables(queried, joined)
|
|
@@ -875,6 +1251,9 @@ export function createRuntime(deps) {
|
|
|
875
1251
|
}
|
|
876
1252
|
async function navigate(path, route, request) {
|
|
877
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;
|
|
878
1257
|
const at = now();
|
|
879
1258
|
const gapMs = lastViewAt === null ? null : at - lastViewAt;
|
|
880
1259
|
lastViewAt = at;
|
|
@@ -899,7 +1278,8 @@ export function createRuntime(deps) {
|
|
|
899
1278
|
mismatch,
|
|
900
1279
|
swAgeMs: at - startedAt,
|
|
901
1280
|
gapMs,
|
|
902
|
-
...(from ? { from } : {})
|
|
1281
|
+
...(from ? { from } : {}),
|
|
1282
|
+
...(lifetimes === undefined ? {} : { lifetimes })
|
|
903
1283
|
});
|
|
904
1284
|
const record = async (mismatch) => {
|
|
905
1285
|
if (config.views !== false)
|
|
@@ -955,15 +1335,36 @@ export function createRuntime(deps) {
|
|
|
955
1335
|
if (side)
|
|
956
1336
|
await side.replica.drop([path]);
|
|
957
1337
|
}
|
|
958
|
-
/**
|
|
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
|
+
*/
|
|
959
1345
|
async function dataFile(request, path, route) {
|
|
960
|
-
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);
|
|
961
1356
|
if (decision.html !== undefined) {
|
|
962
1357
|
ledger.serve(path, decision.reason === 'catalogue');
|
|
963
1358
|
const headers = new Headers(decision.headers);
|
|
964
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
|
+
}
|
|
965
1364
|
return new Response(decision.html, { headers });
|
|
966
1365
|
}
|
|
1366
|
+
if (remote || bearer)
|
|
1367
|
+
return deps.fetch(request);
|
|
967
1368
|
const response = await passthrough(request, false, {
|
|
968
1369
|
provision: true,
|
|
969
1370
|
route,
|
|
@@ -1047,6 +1448,7 @@ export function createRuntime(deps) {
|
|
|
1047
1448
|
function derivable(path, route, response, envelope) {
|
|
1048
1449
|
const advertised = envelope?.artifact;
|
|
1049
1450
|
return ((route.strategy === 'assembly' || route.strategy === 'template') &&
|
|
1451
|
+
route.family === undefined &&
|
|
1050
1452
|
route.scope === 'public' &&
|
|
1051
1453
|
envelope?.protocol === PROTOCOL_VERSION &&
|
|
1052
1454
|
advertised?.hash !== undefined &&
|
|
@@ -1274,6 +1676,9 @@ export function createRuntime(deps) {
|
|
|
1274
1676
|
// #endregion
|
|
1275
1677
|
async function run(request) {
|
|
1276
1678
|
await ready();
|
|
1679
|
+
const declared = writeFor(config.resourceWrites ?? [], request.method, new URL(request.url), origin);
|
|
1680
|
+
if (declared)
|
|
1681
|
+
return declaredWrite(request, declared);
|
|
1277
1682
|
const route = classify(config, request, origin);
|
|
1278
1683
|
track(analytics.flushIfDue());
|
|
1279
1684
|
if (route.kind === 'logout') {
|
|
@@ -1304,15 +1709,32 @@ export function createRuntime(deps) {
|
|
|
1304
1709
|
return asset(request);
|
|
1305
1710
|
return passthrough(request, request.mode === 'navigate');
|
|
1306
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
|
+
}
|
|
1307
1725
|
return {
|
|
1308
1726
|
replica,
|
|
1309
1727
|
ledger,
|
|
1310
1728
|
handle(request, wait) {
|
|
1729
|
+
const pending = sweepWrites();
|
|
1730
|
+
if (pending)
|
|
1731
|
+
wait(pending);
|
|
1311
1732
|
const kind = classify(config, request, origin).kind;
|
|
1312
1733
|
if (kind === 'ignore')
|
|
1313
1734
|
return undefined;
|
|
1314
|
-
if (kind === 'asset' && assetMode !== 'cache')
|
|
1315
|
-
return undefined;
|
|
1735
|
+
if (kind === 'asset' && assetMode !== 'cache') {
|
|
1736
|
+
return forwardsAssets && shouldForward(cachePath) ? deps.fetch(request) : undefined;
|
|
1737
|
+
}
|
|
1316
1738
|
// a worker in shadow touches only navigations: subresources and beacons keep the browser's own path
|
|
1317
1739
|
if (observing &&
|
|
1318
1740
|
kind !== 'navigation' &&
|
|
@@ -1321,6 +1743,14 @@ export function createRuntime(deps) {
|
|
|
1321
1743
|
return undefined;
|
|
1322
1744
|
}
|
|
1323
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
|
+
}
|
|
1324
1754
|
const work = kind === 'asset' && !manifestPending() ? asset(request) : run(request);
|
|
1325
1755
|
return work
|
|
1326
1756
|
.catch(() => deps.fetch(request))
|
|
@@ -1329,9 +1759,24 @@ export function createRuntime(deps) {
|
|
|
1329
1759
|
async install(event) {
|
|
1330
1760
|
if (assetMode === 'static-routing')
|
|
1331
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();
|
|
1332
1769
|
},
|
|
1333
1770
|
async activate() {
|
|
1771
|
+
const pending = sweepWrites();
|
|
1772
|
+
if (pending)
|
|
1773
|
+
track(pending);
|
|
1334
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
|
+
}
|
|
1335
1780
|
for (const name of await deps.caches.keys()) {
|
|
1336
1781
|
const old = CACHE_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
1337
1782
|
if (old && name !== assetCache && name !== staticCache)
|
|
@@ -1339,13 +1784,18 @@ export function createRuntime(deps) {
|
|
|
1339
1784
|
}
|
|
1340
1785
|
},
|
|
1341
1786
|
async message(data) {
|
|
1787
|
+
const pending = sweepWrites();
|
|
1788
|
+
if (pending)
|
|
1789
|
+
track(pending);
|
|
1342
1790
|
const message = (data ?? {});
|
|
1343
1791
|
if (message.type === 'spool:hello') {
|
|
1344
1792
|
return {
|
|
1345
1793
|
type: 'spool:hello',
|
|
1346
1794
|
protocol: PROTOCOL_VERSION,
|
|
1347
1795
|
version: config.version,
|
|
1348
|
-
compatible: message.protocol === PROTOCOL_VERSION
|
|
1796
|
+
compatible: message.protocol === PROTOCOL_VERSION,
|
|
1797
|
+
...(config.lease === undefined ? {} : { lease: config.lease }),
|
|
1798
|
+
...(lastContact === 0 ? {} : { contact: lastContact })
|
|
1349
1799
|
};
|
|
1350
1800
|
}
|
|
1351
1801
|
if (message.type === 'spool:status') {
|
|
@@ -1378,6 +1828,13 @@ export function createRuntime(deps) {
|
|
|
1378
1828
|
}
|
|
1379
1829
|
return undefined;
|
|
1380
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
|
+
}
|
|
1381
1838
|
if (message.type === 'spool:graph') {
|
|
1382
1839
|
await ready();
|
|
1383
1840
|
await replica.held();
|
|
@@ -1412,6 +1869,29 @@ export function createRuntime(deps) {
|
|
|
1412
1869
|
datasets: await side.replica.datasets.list()
|
|
1413
1870
|
};
|
|
1414
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
|
+
}
|
|
1415
1895
|
if (message.type === 'spool:assets') {
|
|
1416
1896
|
return { type: 'spool:assets', mode: assetMode, routed };
|
|
1417
1897
|
}
|