@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
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { BUNDLE_DELTA_PREFIX, COOKIES } from '../protocol/config.js';
|
|
2
|
+
import { DELTA_ELEMENT_ID, RING_ELEMENT_ID } from '../protocol/ring.js';
|
|
3
|
+
import { engineOf } from '../protocol/view.js';
|
|
4
|
+
import { viewStamps } from '../protocol/views.js';
|
|
5
|
+
import { askPersistence } from './persist.js';
|
|
6
|
+
const VIEWS_KEY = 'spool:views';
|
|
7
|
+
const STAMPS_KEY = 'spool:stamps';
|
|
8
|
+
const LIFETIMES_KEY = 'spool:lifetimes';
|
|
9
|
+
const BOOT_KEY = 'spool:boot';
|
|
10
|
+
/** the cookie outlives a browser restart, so the first navigation after one still carries its version */
|
|
11
|
+
const HELD_MAX_AGE = 604_800;
|
|
12
|
+
function isPack(value) {
|
|
13
|
+
const pack = value;
|
|
14
|
+
return (typeof pack === 'object' &&
|
|
15
|
+
pack !== null &&
|
|
16
|
+
typeof pack.url === 'string' &&
|
|
17
|
+
Number.isSafeInteger(pack.bytes));
|
|
18
|
+
}
|
|
19
|
+
/** the ring a document carries, or null when the text is missing, not JSON or not a ring */
|
|
20
|
+
export function parseRing(text) {
|
|
21
|
+
if (!text)
|
|
22
|
+
return null;
|
|
23
|
+
let value;
|
|
24
|
+
try {
|
|
25
|
+
value = JSON.parse(text);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
31
|
+
return null;
|
|
32
|
+
const ring = value;
|
|
33
|
+
const ok = ring['protocol'] === 1 &&
|
|
34
|
+
Number.isSafeInteger(ring['version']) &&
|
|
35
|
+
typeof ring['digest'] === 'string' &&
|
|
36
|
+
(ring['routes'] === undefined || Array.isArray(ring['routes'])) &&
|
|
37
|
+
(ring['templates'] === undefined ||
|
|
38
|
+
(typeof ring['templates'] === 'object' &&
|
|
39
|
+
ring['templates'] !== null &&
|
|
40
|
+
!Array.isArray(ring['templates']))) &&
|
|
41
|
+
typeof ring['tables'] === 'object' &&
|
|
42
|
+
ring['tables'] !== null &&
|
|
43
|
+
!Array.isArray(ring['tables']) &&
|
|
44
|
+
typeof ring['complete'] === 'boolean' &&
|
|
45
|
+
(ring['bootstrap'] === 'first-view' || ring['bootstrap'] === 'second-view') &&
|
|
46
|
+
(ring['worker'] === 'auto' || ring['worker'] === 'never') &&
|
|
47
|
+
(ring['pack'] === undefined ? ring['complete'] === true : isPack(ring['pack'])) &&
|
|
48
|
+
(ring['writes'] === undefined || Array.isArray(ring['writes'])) &&
|
|
49
|
+
(ring['lease'] === undefined || Number.isSafeInteger(ring['lease']));
|
|
50
|
+
return ok ? value : null;
|
|
51
|
+
}
|
|
52
|
+
/** adds one to the count at `key`; a throwing storage or a non-count value starts at 1 */
|
|
53
|
+
function bump(storage, key) {
|
|
54
|
+
try {
|
|
55
|
+
const seen = Number(storage?.getItem(key) ?? 0);
|
|
56
|
+
const next = (Number.isSafeInteger(seen) && seen >= 0 ? seen : 0) + 1;
|
|
57
|
+
storage?.setItem(key, String(next));
|
|
58
|
+
return next;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* the views this page counts: the ones a lease period still covers when the ring names a lease, else
|
|
66
|
+
* the lifetime count; a throwing storage counts one, as `bump` does
|
|
67
|
+
*/
|
|
68
|
+
function windowed(storage, lifetime, lease) {
|
|
69
|
+
if (!((lease ?? 0) > 0))
|
|
70
|
+
return lifetime;
|
|
71
|
+
try {
|
|
72
|
+
const stamps = viewStamps(storage?.getItem(STAMPS_KEY) ?? '', Date.now(), (lease ?? 0) * 1000);
|
|
73
|
+
storage?.setItem(STAMPS_KEY, stamps.join());
|
|
74
|
+
return stamps.length;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** the text of the ring element; null where there is no document to read it from */
|
|
81
|
+
function ringText(doc) {
|
|
82
|
+
try {
|
|
83
|
+
return (doc ?? document).getElementById(RING_ELEMENT_ID)?.textContent;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** the delta a document carries for a page that holds an older version; no text is a pointer to fetch */
|
|
90
|
+
function heldDelta(doc) {
|
|
91
|
+
try {
|
|
92
|
+
const element = (doc ?? document).getElementById(DELTA_ELEMENT_ID);
|
|
93
|
+
if (!element)
|
|
94
|
+
return undefined;
|
|
95
|
+
const from = Number(element.getAttribute('data-from'));
|
|
96
|
+
const to = Number(element.getAttribute('data-to'));
|
|
97
|
+
if (!Number.isSafeInteger(from) || !Number.isSafeInteger(to) || to <= from)
|
|
98
|
+
return undefined;
|
|
99
|
+
return { from, to, text: element.textContent ?? '' };
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* whether the page must not ask for a worker: the ring says so, or the page tag does when there is
|
|
107
|
+
* no ring. A ring page on WebKit asks only when the site chose `always`; the engine is read from the
|
|
108
|
+
* user agent.
|
|
109
|
+
*/
|
|
110
|
+
function neverPage(doc, ring, userAgent) {
|
|
111
|
+
if (ring) {
|
|
112
|
+
return (ring.worker === 'never' ||
|
|
113
|
+
(ring.webkitWorker !== true && engineOf(userAgent) === 'webkit'));
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
return (doc ?? document).querySelector?.('script[data-spool-worker="never"]') != null;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function localStore() {
|
|
123
|
+
try {
|
|
124
|
+
return localStorage;
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** sends the pack count to the worker that controls this page, if any */
|
|
131
|
+
function reportLifetimes(storage) {
|
|
132
|
+
try {
|
|
133
|
+
const count = Number(storage?.getItem(LIFETIMES_KEY) ?? 0);
|
|
134
|
+
if (Number.isSafeInteger(count) && count > 0) {
|
|
135
|
+
navigator.serviceWorker?.controller?.postMessage({ type: 'spool:lifetimes', count });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// a throwing storage or a page without a worker API keeps the view as it was
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* What a view does. A `second-view` ring fetches the pack from view 2 (view 1 only writes the ring)
|
|
144
|
+
* and registers the worker from view 3, where `view` counts the views in one lease period; `first-view`
|
|
145
|
+
* does both on view 1. A document with no ring
|
|
146
|
+
* keeps the old path: the worker registers at once, unless the page names no worker. A pack the
|
|
147
|
+
* boot script carried at the ring's version installs on any view, since it costs no request. A
|
|
148
|
+
* `router` view, one a client router's click loaded the boot script for, registers the worker too.
|
|
149
|
+
*/
|
|
150
|
+
function gate(ring, view, never, carried, router) {
|
|
151
|
+
if (!ring)
|
|
152
|
+
return { pack: false, worker: !never };
|
|
153
|
+
const late = ring.bootstrap === 'second-view';
|
|
154
|
+
return {
|
|
155
|
+
pack: !ring.complete && (carried || view >= (late ? 2 : 1)),
|
|
156
|
+
worker: !never && (router || view >= (late ? 3 : 1))
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
async function* chunks(body) {
|
|
160
|
+
const reader = body.getReader();
|
|
161
|
+
for (;;) {
|
|
162
|
+
const { done, value } = await reader.read();
|
|
163
|
+
if (done)
|
|
164
|
+
return;
|
|
165
|
+
yield value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function* fromBoot(data) {
|
|
169
|
+
const text = atob(data);
|
|
170
|
+
const bytes = new Uint8Array(text.length);
|
|
171
|
+
for (let i = 0; i < text.length; i++)
|
|
172
|
+
bytes[i] = text.charCodeAt(i);
|
|
173
|
+
yield bytes;
|
|
174
|
+
}
|
|
175
|
+
/** writes the ring, then the pack when the gate asks; returns the version the store now holds in full */
|
|
176
|
+
async function install(options, storage, ring, fetchPack) {
|
|
177
|
+
const { store } = options;
|
|
178
|
+
try {
|
|
179
|
+
// a partial head holds a ring, not a pack, so it does not count as held
|
|
180
|
+
const head = (await store.head());
|
|
181
|
+
if (head && head.partial !== true && head.version >= ring.version) {
|
|
182
|
+
// routes and templates have no version, so the same version refreshes them
|
|
183
|
+
if (head.version === ring.version && ring.routes) {
|
|
184
|
+
await store.putRoutes(ring.routes, ring.templates);
|
|
185
|
+
}
|
|
186
|
+
return head.version;
|
|
187
|
+
}
|
|
188
|
+
const placed = await store.installRing(ring);
|
|
189
|
+
if (ring.complete)
|
|
190
|
+
return placed?.installed ? ring.version : undefined;
|
|
191
|
+
if (!fetchPack || !ring.pack)
|
|
192
|
+
return undefined;
|
|
193
|
+
const fit = await store.estimateBeforeInstall(ring.pack.bytes);
|
|
194
|
+
if (!fit.fits)
|
|
195
|
+
return undefined;
|
|
196
|
+
let source;
|
|
197
|
+
if (options.pack?.version === ring.version) {
|
|
198
|
+
source = fromBoot(options.pack.data);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const response = await (options.fetch ?? fetch)(ring.pack.url);
|
|
202
|
+
if (!response.ok || !response.body)
|
|
203
|
+
return undefined;
|
|
204
|
+
source = chunks(response.body);
|
|
205
|
+
}
|
|
206
|
+
const outcome = await store.installPack(source, { bytes: ring.pack.bytes });
|
|
207
|
+
if (!outcome.installed)
|
|
208
|
+
return undefined;
|
|
209
|
+
if (ring.routes)
|
|
210
|
+
await store.putRoutes(ring.routes, ring.templates);
|
|
211
|
+
bump(storage, LIFETIMES_KEY);
|
|
212
|
+
// Chromium decides by heuristics and does not prompt; Firefox asks the user, so it is left out
|
|
213
|
+
if (engineOf(options.userAgent ?? navigator.userAgent) === 'chromium') {
|
|
214
|
+
await askPersistence(options.manager ?? navigator.storage);
|
|
215
|
+
}
|
|
216
|
+
return ring.version;
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
// the page keeps working from the network and the ring
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** the ring with the routes and templates this page can trust: its own, or the boot script's when the ids match */
|
|
224
|
+
function withRoutes(ring, shape) {
|
|
225
|
+
if (ring.routes && ring.templates)
|
|
226
|
+
return ring;
|
|
227
|
+
const id = /[?&]r=([0-9a-f]+)/.exec(ring.boot ?? '')?.[1];
|
|
228
|
+
if (!shape || id === undefined || shape.id !== id)
|
|
229
|
+
return undefined;
|
|
230
|
+
return { ...ring, routes: shape.routes, templates: shape.templates };
|
|
231
|
+
}
|
|
232
|
+
/** applies a delta to the complete head it starts from; returns the version the store now holds */
|
|
233
|
+
async function applyHeld(options, delta) {
|
|
234
|
+
const { store } = options;
|
|
235
|
+
const head = (await store.head());
|
|
236
|
+
if (!head || head.partial === true || head.version !== delta.from)
|
|
237
|
+
return undefined;
|
|
238
|
+
let body = delta.text ? JSON.parse(delta.text) : undefined;
|
|
239
|
+
if (!body) {
|
|
240
|
+
const response = await (options.fetch ?? fetch)(`${BUNDLE_DELTA_PREFIX}${delta.from}/${delta.to}`);
|
|
241
|
+
if (!response.ok)
|
|
242
|
+
return undefined;
|
|
243
|
+
body = (await response.json());
|
|
244
|
+
}
|
|
245
|
+
await store.applyDelta(body);
|
|
246
|
+
return delta.to;
|
|
247
|
+
}
|
|
248
|
+
/** keeps the version this page holds in a cookie, so the next document navigation can carry its delta */
|
|
249
|
+
export function holdCookie(options, version) {
|
|
250
|
+
const value = `${COOKIES.held}=${version}; path=/; max-age=${HELD_MAX_AGE}; samesite=lax`;
|
|
251
|
+
try {
|
|
252
|
+
if (options.cookie)
|
|
253
|
+
options.cookie(value);
|
|
254
|
+
else
|
|
255
|
+
document.cookie = value;
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
// without the cookie the next navigation carries no delta and the page is still correct
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/** applies the delta and the ring, then holds the version a no-worker page ends on */
|
|
262
|
+
async function settle(options, storage, ring, fetchPack, never) {
|
|
263
|
+
let version;
|
|
264
|
+
try {
|
|
265
|
+
const delta = heldDelta(options.doc);
|
|
266
|
+
if (delta)
|
|
267
|
+
version = await applyHeld(options, delta);
|
|
268
|
+
if (ring)
|
|
269
|
+
version = (await install(options, storage, ring, fetchPack)) ?? version;
|
|
270
|
+
if (version === undefined) {
|
|
271
|
+
const head = (await options.store.head());
|
|
272
|
+
if (head && head.partial !== true)
|
|
273
|
+
version = head.version;
|
|
274
|
+
}
|
|
275
|
+
if (never && version !== undefined)
|
|
276
|
+
holdCookie(options, version);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
// the page keeps working from the network and the ring
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Counts this view, reads the ring from `doc` and starts the install the view calls for. `worker`
|
|
284
|
+
* says whether the page registers the worker now; `installed` settles once the ring, any delta and
|
|
285
|
+
* any pack are in the store. Never throws.
|
|
286
|
+
*/
|
|
287
|
+
export function bootstrap(options) {
|
|
288
|
+
const storage = options.storage === undefined ? localStore() : options.storage;
|
|
289
|
+
const lifetime = bump(storage, VIEWS_KEY);
|
|
290
|
+
reportLifetimes(storage);
|
|
291
|
+
const ring = parseRing(ringText(options.doc));
|
|
292
|
+
const trusted = ring && withRoutes(ring, options.shape);
|
|
293
|
+
const current = ring === null || trusted !== undefined;
|
|
294
|
+
if (!current) {
|
|
295
|
+
try {
|
|
296
|
+
// the next click loads the boot script the ring names, which holds the current routes
|
|
297
|
+
storage?.setItem(BOOT_KEY, '');
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
// the stored boot script is checked again on the next document
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const never = neverPage(options.doc, ring, options.userAgent ?? (typeof navigator === 'undefined' ? '' : navigator.userAgent));
|
|
304
|
+
const decision = gate(ring, windowed(storage, lifetime, ring?.lease), never, ring !== null && options.pack?.version === ring.version, options.router === true);
|
|
305
|
+
const installed = settle(options, storage, trusted ?? null, decision.pack, never);
|
|
306
|
+
return { worker: decision.worker, installed, current };
|
|
307
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** the localStorage key the time of the last contact with the server is kept under */
|
|
2
|
+
export declare const CONTACT_KEY = "spool:contact";
|
|
3
|
+
/** whether the page's copy of the last server contact is older than the worker script's lease */
|
|
4
|
+
export type LeaseClock = {
|
|
5
|
+
/** true when no contact is known, the last one is past the lease, or the clock moved back */
|
|
6
|
+
due(): boolean;
|
|
7
|
+
/** records a server contact at `at`, in milliseconds since the epoch */
|
|
8
|
+
mark(at: number): void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Keeps the time of the last server contact for the lease of `leaseSec` seconds. The page reads it
|
|
12
|
+
* from localStorage; when the store is missing or fails, the page's own record stands in for it, so
|
|
13
|
+
* a failing store asks the server once per page load and never throws.
|
|
14
|
+
*/
|
|
15
|
+
export declare function leaseClock(leaseSec: number, storage?: Pick<Storage, 'getItem' | 'setItem'> | undefined, now?: () => number): LeaseClock;
|
|
16
|
+
/** starts the page's lease clock once the worker has reported its lease */
|
|
17
|
+
export declare function holdLease(registration: ServiceWorkerRegistration, leaseSec: number): void;
|
|
18
|
+
/** records a contact the worker saw; a page without a lease ignores it */
|
|
19
|
+
export declare function markContact(at: number): void;
|
|
20
|
+
/**
|
|
21
|
+
* Asks the browser for the worker script once the lease has passed. The mark comes first, so
|
|
22
|
+
* views inside the next lease make no request. Called on each page load and each in-page view.
|
|
23
|
+
*/
|
|
24
|
+
export declare function checkLease(): void;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** the localStorage key the time of the last contact with the server is kept under */
|
|
2
|
+
export const CONTACT_KEY = 'spool:contact';
|
|
3
|
+
function localStorageOrUndefined() {
|
|
4
|
+
try {
|
|
5
|
+
return globalThis.localStorage;
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Keeps the time of the last server contact for the lease of `leaseSec` seconds. The page reads it
|
|
13
|
+
* from localStorage; when the store is missing or fails, the page's own record stands in for it, so
|
|
14
|
+
* a failing store asks the server once per page load and never throws.
|
|
15
|
+
*/
|
|
16
|
+
export function leaseClock(leaseSec, storage = localStorageOrUndefined(), now = Date.now) {
|
|
17
|
+
let memory = 0;
|
|
18
|
+
const read = () => {
|
|
19
|
+
let stored = 0;
|
|
20
|
+
try {
|
|
21
|
+
stored = Number(storage?.getItem(CONTACT_KEY));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// the page's own record stands in
|
|
25
|
+
}
|
|
26
|
+
const at = Math.max(Number.isFinite(stored) ? stored : 0, memory);
|
|
27
|
+
return at > 0 ? at : undefined;
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
due() {
|
|
31
|
+
const at = read();
|
|
32
|
+
if (at === undefined)
|
|
33
|
+
return true;
|
|
34
|
+
const age = now() - at;
|
|
35
|
+
return !(age >= 0 && age < leaseSec * 1000);
|
|
36
|
+
},
|
|
37
|
+
mark(at) {
|
|
38
|
+
memory = Math.max(memory, at);
|
|
39
|
+
try {
|
|
40
|
+
storage?.setItem(CONTACT_KEY, String(memory));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// the page's own record covers this page load
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
let lease;
|
|
49
|
+
/** starts the page's lease clock once the worker has reported its lease */
|
|
50
|
+
export function holdLease(registration, leaseSec) {
|
|
51
|
+
lease = { clock: leaseClock(leaseSec), registration };
|
|
52
|
+
}
|
|
53
|
+
/** records a contact the worker saw; a page without a lease ignores it */
|
|
54
|
+
export function markContact(at) {
|
|
55
|
+
lease?.clock.mark(at);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Asks the browser for the worker script once the lease has passed. The mark comes first, so
|
|
59
|
+
* views inside the next lease make no request. Called on each page load and each in-page view.
|
|
60
|
+
*/
|
|
61
|
+
export function checkLease() {
|
|
62
|
+
if (!lease || !lease.clock.due())
|
|
63
|
+
return;
|
|
64
|
+
lease.clock.mark(Date.now());
|
|
65
|
+
void lease.registration.update().catch(() => undefined);
|
|
66
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ResourceWrite } from '../protocol/write.js';
|
|
2
|
+
import type { PackStore } from '../sw/pack/store.js';
|
|
3
|
+
/** what `declaredFetch` takes; `origin` is the page's own origin */
|
|
4
|
+
export type DeclaredFetchOptions = {
|
|
5
|
+
store: PackStore;
|
|
6
|
+
/** the declared resource writes of the site, from the ring or the writes element */
|
|
7
|
+
writes: readonly ResourceWrite[];
|
|
8
|
+
origin: string;
|
|
9
|
+
/** sends the request; default `fetch` */
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
/** writes the held-version cookie; default sets `document.cookie` */
|
|
12
|
+
cookie?: (value: string) => void;
|
|
13
|
+
};
|
|
14
|
+
/** the request function `spool.fetch` is: `fetch` for every request, settling declared writes */
|
|
15
|
+
export type SpoolFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
16
|
+
/**
|
|
17
|
+
* A `fetch` for a page without a worker. A request that matches a declared resource write in the ring
|
|
18
|
+
* goes out with `Spool-Have` naming the held version, and its answer settles into the store the way the
|
|
19
|
+
* worker settles it. Every other request is passed to `fetch` unchanged.
|
|
20
|
+
*/
|
|
21
|
+
/** the declared writes a writes element holds; anything but a JSON list reads as none */
|
|
22
|
+
export declare function parseWrites(text: string | null | undefined): ResourceWrite[];
|
|
23
|
+
export declare function declaredFetch(options: DeclaredFetchOptions): SpoolFetch;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { BUNDLE_DELTA_PREFIX } from '../protocol/config.js';
|
|
2
|
+
import { HEADERS } from '../protocol/envelope.js';
|
|
3
|
+
import { writeFor } from '../protocol/write.js';
|
|
4
|
+
import { settleWrite } from '../sw/write.js';
|
|
5
|
+
import { holdCookie } from './bootstrap.js';
|
|
6
|
+
/**
|
|
7
|
+
* A `fetch` for a page without a worker. A request that matches a declared resource write in the ring
|
|
8
|
+
* goes out with `Spool-Have` naming the held version, and its answer settles into the store the way the
|
|
9
|
+
* worker settles it. Every other request is passed to `fetch` unchanged.
|
|
10
|
+
*/
|
|
11
|
+
/** the declared writes a writes element holds; anything but a JSON list reads as none */
|
|
12
|
+
export function parseWrites(text) {
|
|
13
|
+
if (!text)
|
|
14
|
+
return [];
|
|
15
|
+
try {
|
|
16
|
+
const value = JSON.parse(text);
|
|
17
|
+
return Array.isArray(value) ? value : [];
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function declaredFetch(options) {
|
|
24
|
+
const send = options.fetch ?? fetch;
|
|
25
|
+
const { store, writes, origin } = options;
|
|
26
|
+
const fetchDelta = async (from, to) => {
|
|
27
|
+
const response = await send(`${origin}${BUNDLE_DELTA_PREFIX}${from}/${to}`, {
|
|
28
|
+
credentials: 'same-origin'
|
|
29
|
+
});
|
|
30
|
+
if (!response.ok)
|
|
31
|
+
throw new Error(`delta answered ${response.status}`);
|
|
32
|
+
return (await response.json());
|
|
33
|
+
};
|
|
34
|
+
return async (input, init) => {
|
|
35
|
+
const target = new URL(input instanceof Request ? input.url : String(input), origin);
|
|
36
|
+
const base = input instanceof Request ? input : undefined;
|
|
37
|
+
const method = (init?.method ?? base?.method ?? 'GET').toUpperCase();
|
|
38
|
+
const declared = writeFor(writes, method, target, origin);
|
|
39
|
+
if (!declared)
|
|
40
|
+
return send(input, init);
|
|
41
|
+
const head = (await store.head());
|
|
42
|
+
const headers = new Headers(init?.headers ?? base?.headers);
|
|
43
|
+
if (head && head.partial !== true)
|
|
44
|
+
headers.set(HEADERS.have, String(head.version));
|
|
45
|
+
const text = typeof init?.body === 'string' ? init.body : undefined;
|
|
46
|
+
const response = await send(target.href, {
|
|
47
|
+
...init,
|
|
48
|
+
method,
|
|
49
|
+
headers,
|
|
50
|
+
body: text,
|
|
51
|
+
cache: 'no-store',
|
|
52
|
+
credentials: 'same-origin'
|
|
53
|
+
});
|
|
54
|
+
if (response.ok) {
|
|
55
|
+
const body = text === undefined ? undefined : JSON.parse(text);
|
|
56
|
+
await settleWrite(response, store, fetchDelta, declared.origin ?? origin, {
|
|
57
|
+
write: declared,
|
|
58
|
+
url: target,
|
|
59
|
+
body,
|
|
60
|
+
routes: await store.getRoutes()
|
|
61
|
+
}).catch(() => undefined);
|
|
62
|
+
const after = (await store.head());
|
|
63
|
+
if (after && after.partial !== true)
|
|
64
|
+
holdCookie(options, after.version);
|
|
65
|
+
}
|
|
66
|
+
return response;
|
|
67
|
+
};
|
|
68
|
+
}
|
package/dist/client/entry.js
CHANGED
|
@@ -1,9 +1,121 @@
|
|
|
1
|
+
import { navigatePath } from '../protocol/config.js';
|
|
2
|
+
import { RING_ELEMENT_ID, WRITES_ELEMENT_ID } from '../protocol/ring.js';
|
|
1
3
|
import { CLIENT_TAG_ATTR } from '../protocol/tag.js';
|
|
4
|
+
import { createPackStore } from '../sw/pack/store.js';
|
|
5
|
+
import { VERSION } from '../version.js';
|
|
2
6
|
import { browserActivityHost, createActivity } from './activity.js';
|
|
7
|
+
import { bootstrap, parseRing } from './bootstrap.js';
|
|
8
|
+
import { declaredFetch, parseWrites } from './declared.js';
|
|
9
|
+
import { browserLiveDeps, startLive } from './live.js';
|
|
3
10
|
import { crossOriginLoads, crossOriginOrigins } from './loads.js';
|
|
4
11
|
import { register } from './register.js';
|
|
5
|
-
|
|
6
|
-
|
|
12
|
+
function openStore() {
|
|
13
|
+
try {
|
|
14
|
+
return createPackStore();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** the navigation script is in the boot script, or loads here and sets `spoolNavigate` */
|
|
21
|
+
function loadNavigation() {
|
|
22
|
+
const navigate = () => globalThis.spoolNavigate;
|
|
23
|
+
const present = navigate();
|
|
24
|
+
if (present)
|
|
25
|
+
return Promise.resolve(present.enableNavigation);
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const script = document.createElement('script');
|
|
28
|
+
script.src = navigatePath(VERSION);
|
|
29
|
+
script.onload = () => {
|
|
30
|
+
const loaded = navigate();
|
|
31
|
+
if (loaded)
|
|
32
|
+
resolve(loaded.enableNavigation);
|
|
33
|
+
else
|
|
34
|
+
reject(new Error('navigation script did not register'));
|
|
35
|
+
};
|
|
36
|
+
script.onerror = () => reject(new Error('navigation script failed to load'));
|
|
37
|
+
document.head.append(script);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const carried = globalThis.__SPOOL_BOOT__;
|
|
41
|
+
// the click the gate held back, loaded as a document if the held pack cannot answer it
|
|
42
|
+
const pending = carried?.to;
|
|
43
|
+
const leave = () => (pending ? location.assign(pending) : undefined);
|
|
44
|
+
// no IndexedDB leaves the page on the old path: the worker registers at once
|
|
45
|
+
const store = openStore();
|
|
46
|
+
const boot = store
|
|
47
|
+
? bootstrap({
|
|
48
|
+
store,
|
|
49
|
+
...(carried?.pack ? { pack: carried.pack } : {}),
|
|
50
|
+
...(carried?.shape ? { shape: carried.shape } : {}),
|
|
51
|
+
router: carried?.router === true
|
|
52
|
+
})
|
|
53
|
+
: { worker: true, installed: Promise.resolve(), current: true };
|
|
54
|
+
const ready = (boot.worker ? register() : Promise.resolve(null)).catch(() => null);
|
|
55
|
+
// in-page navigation needs a held site pack; a page without one navigates as it always did
|
|
56
|
+
void boot.installed
|
|
57
|
+
.then(async () => {
|
|
58
|
+
// the routes held are older than this page's, so it navigates by document until a click reloads them
|
|
59
|
+
if (!store || !boot.current || !(await store.head()))
|
|
60
|
+
return leave();
|
|
61
|
+
const enable = await loadNavigation();
|
|
62
|
+
const templates = await store.getTemplates();
|
|
63
|
+
const tag = document.querySelector(`script[${CLIENT_TAG_ATTR}]`);
|
|
64
|
+
const navigation = enable({ store, clientTag: tag?.outerHTML ?? '', templates });
|
|
65
|
+
if (pending)
|
|
66
|
+
navigation.go(new URL(pending));
|
|
67
|
+
})
|
|
68
|
+
.catch(leave);
|
|
69
|
+
const gone = {
|
|
70
|
+
ok: false,
|
|
71
|
+
code: 'unavailable',
|
|
72
|
+
message: 'there is no worker on this page'
|
|
73
|
+
};
|
|
74
|
+
let queued = false;
|
|
75
|
+
/** the write calls, each answered with a refusal when the page has no worker */
|
|
76
|
+
const writes = {
|
|
77
|
+
write: async (...args) => {
|
|
78
|
+
const result = await ready.then((client) => client?.write(...args) ?? gone);
|
|
79
|
+
if (result.ok)
|
|
80
|
+
queued = true;
|
|
81
|
+
return result;
|
|
82
|
+
},
|
|
83
|
+
flush: () => ready.then((client) => client?.flush() ?? gone),
|
|
84
|
+
writes: () => ready.then((client) => client?.writes() ?? gone),
|
|
85
|
+
resolve: (...args) => ready.then((client) => client?.resolve(...args) ?? gone)
|
|
86
|
+
};
|
|
87
|
+
/** the writes a no-worker page declares: its ring's, or the element a page with no ring carries */
|
|
88
|
+
function declaredWrites() {
|
|
89
|
+
const ring = parseRing(document.getElementById(RING_ELEMENT_ID)?.textContent);
|
|
90
|
+
if (ring)
|
|
91
|
+
return ring.writes ?? [];
|
|
92
|
+
return parseWrites(document.getElementById(WRITES_ELEMENT_ID)?.textContent);
|
|
93
|
+
}
|
|
94
|
+
// a page with a worker sends writes through fetch, where the worker settles them
|
|
95
|
+
const declared = store && !boot.worker ? declaredWrites() : [];
|
|
96
|
+
const spoolFetch = store && declared.length > 0
|
|
97
|
+
? declaredFetch({ store, writes: declared, origin: location.origin })
|
|
98
|
+
: (input, init) => fetch(input, init);
|
|
99
|
+
globalThis.spool = {
|
|
100
|
+
ready,
|
|
101
|
+
activity: createActivity(browserActivityHost()),
|
|
102
|
+
fetch: spoolFetch,
|
|
103
|
+
...writes
|
|
104
|
+
};
|
|
105
|
+
// queued values go out when the page is hidden, which is a moment the browser gives us
|
|
106
|
+
const flushQueued = () => {
|
|
107
|
+
if (queued)
|
|
108
|
+
void writes.flush();
|
|
109
|
+
};
|
|
110
|
+
void ready.then((client) => {
|
|
111
|
+
if (!client)
|
|
112
|
+
return;
|
|
113
|
+
addEventListener('pagehide', flushQueued);
|
|
114
|
+
document.addEventListener('visibilitychange', () => {
|
|
115
|
+
if (document.visibilityState === 'hidden')
|
|
116
|
+
flushQueued();
|
|
117
|
+
});
|
|
118
|
+
});
|
|
7
119
|
void ready.then(async (client) => {
|
|
8
120
|
const id = await client?.view().catch(() => null);
|
|
9
121
|
if (!client || !id)
|
|
@@ -24,3 +136,12 @@ void ready.then(async (client) => {
|
|
|
24
136
|
report();
|
|
25
137
|
});
|
|
26
138
|
});
|
|
139
|
+
// the stream a visible page holds for the principal lease; a wipe or a switch reopens it for the new partition
|
|
140
|
+
void ready.then((client) => {
|
|
141
|
+
const deps = client && browserLiveDeps(client);
|
|
142
|
+
if (!deps)
|
|
143
|
+
return;
|
|
144
|
+
const live = startLive(deps);
|
|
145
|
+
addEventListener('spool:cleared', live.reconcile);
|
|
146
|
+
addEventListener('spool:want-live', live.arm);
|
|
147
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|