@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.
Files changed (162) hide show
  1. package/README.md +206 -1756
  2. package/dist/angular/index.d.ts +13 -0
  3. package/dist/angular/index.js +25 -0
  4. package/dist/angular/pages.d.ts +28 -0
  5. package/dist/angular/pages.js +85 -0
  6. package/dist/cdn/client.js +1 -1
  7. package/dist/cdn/location.d.ts +6 -0
  8. package/dist/cdn/location.js +8 -0
  9. package/dist/cdn/manifest.json +7 -7
  10. package/dist/cdn/spool-sw.js +1 -1
  11. package/dist/client/bootstrap.d.ts +57 -0
  12. package/dist/client/bootstrap.js +307 -0
  13. package/dist/client/contact.d.ts +24 -0
  14. package/dist/client/contact.js +66 -0
  15. package/dist/client/declared.d.ts +23 -0
  16. package/dist/client/declared.js +68 -0
  17. package/dist/client/entry.js +91 -2
  18. package/dist/client/gate.d.ts +1 -0
  19. package/dist/client/gate.js +148 -0
  20. package/dist/client/index.d.ts +25 -0
  21. package/dist/client/index.js +1 -0
  22. package/dist/client/live.d.ts +66 -0
  23. package/dist/client/live.js +225 -0
  24. package/dist/client/navigate-entry.d.ts +1 -0
  25. package/dist/client/navigate-entry.js +4 -0
  26. package/dist/client/navigate.d.ts +54 -0
  27. package/dist/client/navigate.js +272 -0
  28. package/dist/client/persist.d.ts +2 -0
  29. package/dist/client/persist.js +9 -0
  30. package/dist/client/register.d.ts +11 -1
  31. package/dist/client/register.js +19 -8
  32. package/dist/express/index.d.ts +11 -0
  33. package/dist/express/index.js +209 -0
  34. package/dist/hono/index.d.ts +19 -0
  35. package/dist/hono/index.js +49 -0
  36. package/dist/next/index.d.ts +33 -0
  37. package/dist/next/index.js +41 -0
  38. package/dist/next/pages.d.ts +22 -0
  39. package/dist/next/pages.js +39 -0
  40. package/dist/node/listener.d.ts +3 -0
  41. package/dist/node/listener.js +57 -2
  42. package/dist/nuxt/index.d.ts +22 -0
  43. package/dist/nuxt/index.js +104 -0
  44. package/dist/nuxt/pages.d.ts +26 -0
  45. package/dist/nuxt/pages.js +48 -0
  46. package/dist/nuxt/runtime/index.d.ts +24 -0
  47. package/dist/nuxt/runtime/index.js +58 -0
  48. package/dist/plugins/coverage.d.ts +14 -0
  49. package/dist/plugins/coverage.js +27 -0
  50. package/dist/plugins/mode.d.ts +19 -0
  51. package/dist/plugins/mode.js +20 -0
  52. package/dist/plugins/path.d.ts +16 -0
  53. package/dist/plugins/path.js +139 -0
  54. package/dist/plugins/report.d.ts +22 -0
  55. package/dist/plugins/report.js +18 -0
  56. package/dist/plugins/routes.d.ts +38 -0
  57. package/dist/plugins/routes.js +52 -0
  58. package/dist/plugins/static.d.ts +42 -0
  59. package/dist/plugins/static.js +97 -0
  60. package/dist/protocol/bundle.d.ts +111 -0
  61. package/dist/protocol/bundle.js +428 -0
  62. package/dist/protocol/config.d.ts +65 -1
  63. package/dist/protocol/config.js +28 -0
  64. package/dist/protocol/dataset.d.ts +6 -0
  65. package/dist/protocol/dataset.js +6 -0
  66. package/dist/protocol/envelope.d.ts +33 -1
  67. package/dist/protocol/envelope.js +15 -1
  68. package/dist/protocol/errors.d.ts +1 -1
  69. package/dist/protocol/index-rows.d.ts +22 -0
  70. package/dist/protocol/index-rows.js +53 -0
  71. package/dist/protocol/index.d.ts +2 -0
  72. package/dist/protocol/index.js +2 -0
  73. package/dist/protocol/normalise.d.ts +4 -4
  74. package/dist/protocol/normalise.js +9 -5
  75. package/dist/protocol/ring.d.ts +58 -0
  76. package/dist/protocol/ring.js +6 -0
  77. package/dist/protocol/route.d.ts +57 -11
  78. package/dist/protocol/route.js +95 -3
  79. package/dist/protocol/tag.d.ts +22 -2
  80. package/dist/protocol/tag.js +25 -4
  81. package/dist/protocol/view.d.ts +2 -0
  82. package/dist/protocol/view.js +8 -2
  83. package/dist/protocol/views.d.ts +6 -0
  84. package/dist/protocol/views.js +15 -0
  85. package/dist/protocol/write.d.ts +92 -4
  86. package/dist/protocol/write.js +251 -9
  87. package/dist/proxy/eligible.d.ts +22 -1
  88. package/dist/proxy/eligible.js +62 -15
  89. package/dist/proxy/proxy.js +28 -18
  90. package/dist/react-router/index.d.ts +20 -0
  91. package/dist/react-router/index.js +60 -0
  92. package/dist/react-router/pages.d.ts +34 -0
  93. package/dist/react-router/pages.js +83 -0
  94. package/dist/report/demand.d.ts +49 -0
  95. package/dist/report/demand.js +60 -0
  96. package/dist/report/economics.d.ts +55 -0
  97. package/dist/report/economics.js +189 -0
  98. package/dist/report/headline.d.ts +158 -0
  99. package/dist/report/headline.js +473 -0
  100. package/dist/report/index.d.ts +2 -0
  101. package/dist/report/index.js +2 -0
  102. package/dist/report/inputs.d.ts +79 -0
  103. package/dist/report/inputs.js +115 -0
  104. package/dist/report/measurement.d.ts +1 -1
  105. package/dist/report/measurement.js +5 -1
  106. package/dist/report/traffic.d.ts +8 -0
  107. package/dist/report/traffic.js +7 -0
  108. package/dist/server/bundle.d.ts +55 -0
  109. package/dist/server/bundle.js +172 -0
  110. package/dist/server/dataset.d.ts +8 -2
  111. package/dist/server/dataset.js +51 -4
  112. package/dist/server/embedded.d.ts +7 -3
  113. package/dist/server/embedded.js +7 -3
  114. package/dist/server/index.d.ts +5 -1
  115. package/dist/server/index.js +5 -1
  116. package/dist/server/live-sockets.d.ts +54 -0
  117. package/dist/server/live-sockets.js +131 -0
  118. package/dist/server/live.d.ts +146 -0
  119. package/dist/server/live.js +228 -0
  120. package/dist/server/ring.d.ts +77 -0
  121. package/dist/server/ring.js +311 -0
  122. package/dist/server/spool.d.ts +139 -4
  123. package/dist/server/spool.js +457 -28
  124. package/dist/server/static.d.ts +42 -0
  125. package/dist/server/static.js +29 -0
  126. package/dist/server/stub.d.ts +43 -5
  127. package/dist/server/stub.js +126 -8
  128. package/dist/server/write.d.ts +23 -1
  129. package/dist/server/write.js +96 -0
  130. package/dist/stores/durable-object.d.ts +46 -0
  131. package/dist/stores/durable-object.js +114 -0
  132. package/dist/sw/analytics.d.ts +1 -1
  133. package/dist/sw/analytics.js +2 -2
  134. package/dist/sw/coverage.d.ts +6 -0
  135. package/dist/sw/coverage.js +34 -0
  136. package/dist/sw/family.d.ts +15 -0
  137. package/dist/sw/family.js +48 -0
  138. package/dist/sw/idb.d.ts +2 -0
  139. package/dist/sw/idb.js +2 -2
  140. package/dist/sw/index.d.ts +2 -0
  141. package/dist/sw/index.js +1 -0
  142. package/dist/sw/pack/store.d.ts +128 -0
  143. package/dist/sw/pack/store.js +737 -0
  144. package/dist/sw/policy.d.ts +2 -0
  145. package/dist/sw/policy.js +11 -0
  146. package/dist/sw/probe.d.ts +16 -0
  147. package/dist/sw/probe.js +25 -0
  148. package/dist/sw/router.d.ts +3 -2
  149. package/dist/sw/router.js +15 -2
  150. package/dist/sw/runtime.d.ts +48 -1
  151. package/dist/sw/runtime.js +548 -68
  152. package/dist/sw/scheduler.d.ts +6 -1
  153. package/dist/sw/scheduler.js +22 -5
  154. package/dist/sw/write.d.ts +41 -1
  155. package/dist/sw/write.js +188 -5
  156. package/dist/version.d.ts +1 -1
  157. package/dist/version.js +1 -2
  158. package/dist/vite/index.d.ts +26 -0
  159. package/dist/vite/index.js +66 -0
  160. package/dist/vue/index.d.ts +36 -0
  161. package/dist/vue/index.js +53 -0
  162. package/package.json +64 -4
@@ -1,7 +1,8 @@
1
- import { cdnClientUrl } from '../cdn/location.js';
1
+ import { cdnClientUrl, cdnWorkerUrl } from '../cdn/location.js';
2
2
  import { parseArtifact, PROTOCOL_VERSION } from '../protocol/artifact.js';
3
+ import { BLOCK_ROWS, MAX_BUNDLE_FRAME_BYTES } from '../protocol/bundle.js';
3
4
  import { checkCollectors } from '../protocol/collector.js';
4
- import { clientPath, PATHS, SPOOL_PREFIX } from '../protocol/config.js';
5
+ import { bootPath, BUNDLE_DELTA_PREFIX, BUNDLE_PREFIX, clientPath, LIVE_PATH, navigatePath, PATHS, SPOOL_PREFIX } from '../protocol/config.js';
5
6
  import { HEADERS, writeEnvelope } from '../protocol/envelope.js';
6
7
  import { ProtocolError } from '../protocol/errors.js';
7
8
  import { collectRefs, createCollector, resolveRefs, splitRowId } from '../protocol/graph.js';
@@ -10,40 +11,81 @@ import { escapeInline, inlineElement, placeInline } from '../protocol/inline.js'
10
11
  import { checkPattern } from '../protocol/pattern.js';
11
12
  import { benefit, chooseEntries, DEFAULT_CATALOGUE_BYTES, priorFrom } from '../protocol/provision.js';
12
13
  import { keyField, verifyProof } from '../protocol/query.js';
14
+ import { DELTA_ELEMENT_ID, RING_ELEMENT_ID, WRITES_ELEMENT_ID } from '../protocol/ring.js';
13
15
  import { matchRoute, serializeRoute } from '../protocol/route.js';
14
16
  import { artifactTag, MAX_HAVE, parseHeld } from '../protocol/sync.js';
15
- import { clientTag, injectClientTag } from '../protocol/tag.js';
16
- import { serializeWrite } from '../protocol/write.js';
17
+ import { clientTag, gateTag, injectClientTag } from '../protocol/tag.js';
18
+ import { serializeWrite, writeFor } from '../protocol/write.js';
19
+ import { recordDecline, responseDecline } from '../proxy/eligible.js';
20
+ import { POLICY_HEADERS, policyHeaders } from '../sw/policy.js';
17
21
  import { VERSION } from '../version.js';
18
22
  import { respondBeacon } from './beacon.js';
19
- import { respondDataset } from './dataset.js';
20
- import { CLIENT_CORE } from './embedded.js';
23
+ import { buildBundle, respondBoot, respondBundle } from './bundle.js';
24
+ import { respondDataset, viewOf } from './dataset.js';
25
+ import { CLIENT_CORE, GATE_CORE, NAVIGATE_CORE, SW_CORE } from './embedded.js';
21
26
  import { partitionId } from './identity.js';
27
+ import { frameText, LIVE_HEADERS, liveSettings, openingFrames, parseLiveQuery, publishing } from './live.js';
28
+ import { bootShape, buildRing, deltaElement, gatePaths, heldVersion, placeRing, RING_BYTES, ringElement, ringPacks, writesElement } from './ring.js';
22
29
  import { compressedSize } from './size.js';
23
- import { workerResponse, workerScript } from './stub.js';
30
+ import { staticHeaders } from './static.js';
31
+ import { checkCore, packReader, respondDelta, STUB_DELTAS, workerEtag, workerImport, workerReply, workerResponse, workerScript } from './stub.js';
24
32
  import { json, respondSync } from './sync.js';
25
- import { createWriteServer } from './write.js';
33
+ import { createResourceWrites, createWriteServer, DELTA_HEADER_BYTES } from './write.js';
26
34
  /** most changed tags a response carries inline before the worker is left to ask `/.spool/sync` */
27
35
  export const MAX_PIGGYBACK = 32;
28
36
  /** most pages one catalogue request makes the server build, whatever its route suggests */
29
37
  export const MAX_CATALOGUE_BUILD = 32;
38
+ /** the lease of the worker script in the browser's cache, in seconds: one day */
39
+ const STUB_MAX_AGE = 86_400;
30
40
  /** the strategies whose artifact is data for a template or projection the device already holds */
31
41
  const ENTRY_STRATEGIES = ['template', 'projection', 'data'];
32
42
  function isHtml(response) {
33
43
  return (response.headers.get('content-type') ?? '').toLowerCase().includes('text/html');
34
44
  }
45
+ const ELIDED = new Set(['', '...', '…']);
46
+ /**
47
+ * Why a template route's source cannot be a real page, or null. Two shapes only, because nothing
48
+ * else can be known without the page: a source with no text or only an ellipsis, and a source with
49
+ * no field, loop or condition under a pattern that has parameters (one constant page for every
50
+ * value of the parameter). A constant page that really is the same for every value belongs on an
51
+ * `assembly` or `static` route.
52
+ */
53
+ function placeholderOf(route) {
54
+ const nodes = route.template?.nodes;
55
+ if (route.strategy !== 'template' || nodes === undefined)
56
+ return null;
57
+ const text = nodes.every((node) => node.t === 'text')
58
+ ? nodes.map((node) => (node.t === 'text' ? node.v : '')).join('')
59
+ : null;
60
+ if (text !== null && ELIDED.has(text.trim()))
61
+ return 'it has no content';
62
+ if (text !== null && route.pattern.includes(':')) {
63
+ return 'it prints no field, so every value of the route parameter would show the same page';
64
+ }
65
+ return null;
66
+ }
35
67
  /** wires routes, a version store and the endpoints into one handler set */
36
68
  export function createSpool(options) {
37
- const { store, sink } = options;
38
- const routes = [...options.routes];
69
+ const { sink } = options;
70
+ const stream = liveSettings(options);
71
+ const store = stream ? publishing(options.store, stream.hub) : options.store;
72
+ const routes = [
73
+ ...options.routes,
74
+ ...(options.resources ?? []).flatMap((resource) => resource.routes)
75
+ ];
39
76
  const seen = new Set();
40
77
  for (const route of routes) {
41
78
  if (seen.has(route.pattern)) {
42
79
  throw new ProtocolError('bad-route', `${route.pattern}: declared twice`);
43
80
  }
44
81
  seen.add(route.pattern);
82
+ const placeholder = placeholderOf(route);
83
+ if (placeholder) {
84
+ throw new ProtocolError('bad-template', `${route.pattern}: the template is a placeholder, ${placeholder}`);
85
+ }
45
86
  }
46
87
  const datasets = new Map();
88
+ const families = new Set(routes.flatMap((route) => (route.family ? [route.family.dataset] : [])));
47
89
  const principalOn = options.principalDatasets === true;
48
90
  for (const dataset of options.datasets ?? []) {
49
91
  if (datasets.has(dataset.name)) {
@@ -60,6 +102,12 @@ export function createSpool(options) {
60
102
  datasets.set(dataset.name, dataset);
61
103
  }
62
104
  for (const route of routes) {
105
+ const family = route.family && datasets.get(route.family.dataset);
106
+ if (family) {
107
+ if (family.scope !== route.scope) {
108
+ throw new ProtocolError('bad-route', `${route.pattern}: a ${route.scope} route cannot read the ${family.scope} dataset "${family.name}"`);
109
+ }
110
+ }
63
111
  const name = route.query?.dataset;
64
112
  if (name !== undefined) {
65
113
  const dataset = datasets.get(name);
@@ -79,6 +127,12 @@ export function createSpool(options) {
79
127
  }
80
128
  }
81
129
  }
130
+ const resourceWrites = (options.resources ?? []).flatMap((resource) => resource.writes);
131
+ for (const write of resourceWrites) {
132
+ if (datasets.get(write.dataset)?.scope !== 'public') {
133
+ throw new ProtocolError('bad-write', `${write.pattern}: dataset "${write.dataset}" must be a declared public dataset`);
134
+ }
135
+ }
82
136
  const writes = [...(options.writes?.classes ?? [])];
83
137
  const writeNames = new Set();
84
138
  for (const write of writes) {
@@ -140,6 +194,62 @@ export function createSpool(options) {
140
194
  if (!(Number.isFinite(inlineMax) && inlineMax >= 0)) {
141
195
  throw new ProtocolError('bad-route', 'inlineMaxBytes must be a non-negative number');
142
196
  }
197
+ const policyNames = POLICY_HEADERS;
198
+ const policySet = (policy) => {
199
+ for (const [name, value] of Object.entries(policy ?? {})) {
200
+ if (!policyNames.includes(name)) {
201
+ throw new ProtocolError('bad-route', `policy names "${name}", which is not a policy header`);
202
+ }
203
+ if (typeof value !== 'string')
204
+ throw new ProtocolError('bad-route', `policy ${name} is not a string`);
205
+ }
206
+ return (policy && Object.entries(policy).map(([name, value]) => [name, value]));
207
+ };
208
+ const policyOption = policySet(options.policy);
209
+ let lastPolicy;
210
+ const ringBytes = options.bundle?.ringBytes ?? RING_BYTES;
211
+ if (!(Number.isSafeInteger(ringBytes) && ringBytes >= 0)) {
212
+ throw new ProtocolError('bad-route', 'bundle.ringBytes must be a non-negative integer');
213
+ }
214
+ const blockRows = options.bundle?.blockRows ?? BLOCK_ROWS;
215
+ if (!(Number.isSafeInteger(blockRows) && blockRows >= 1)) {
216
+ throw new ProtocolError('bad-route', 'bundle.blockRows must be a positive integer');
217
+ }
218
+ const maxBytes = options.bundle?.maxBytes ?? MAX_BUNDLE_FRAME_BYTES;
219
+ if (!(Number.isSafeInteger(maxBytes) && maxBytes >= 1 && maxBytes <= MAX_BUNDLE_FRAME_BYTES)) {
220
+ throw new ProtocolError('bad-route', `bundle.maxBytes must be 1 to ${MAX_BUNDLE_FRAME_BYTES}`);
221
+ }
222
+ const bootstrap = options.bootstrap ?? 'second-view';
223
+ if (bootstrap !== 'first-view' && bootstrap !== 'second-view') {
224
+ throw new ProtocolError('bad-route', `unknown bootstrap "${String(bootstrap)}"`);
225
+ }
226
+ const workerMode = options.workerMode ?? 'auto';
227
+ if (workerMode !== 'auto' && workerMode !== 'never' && workerMode !== 'always') {
228
+ throw new ProtocolError('bad-route', `unknown workerMode "${String(workerMode)}"`);
229
+ }
230
+ const clientMode = options.clientTag ?? 'inline';
231
+ if (clientMode !== 'inline' && clientMode !== 'script') {
232
+ throw new ProtocolError('bad-route', `unknown clientTag "${String(clientMode)}"`);
233
+ }
234
+ const tagOptions = options.cdn
235
+ ? {
236
+ src: options.cdn.url ?? cdnClientUrl(VERSION),
237
+ integrity: options.cdn.integrity,
238
+ worker: workerMode
239
+ }
240
+ : { src: clientPath(VERSION), worker: workerMode };
241
+ const gated = bootstrap === 'second-view' && clientMode === 'inline';
242
+ // the boot script holds the runtime itself, so a site that serves it from a CDN keeps its own tag
243
+ const bootOn = gated && !options.cdn && options.bundles !== undefined;
244
+ const bootPrefix = bootPath(VERSION, 0).slice(0, -'0.js'.length);
245
+ const bootCode = `${NAVIGATE_CORE}\n${CLIENT_CORE}`;
246
+ // the routes and templates ride in the boot script, once; the ring names it by their id
247
+ let shaped;
248
+ const shapeOf = () => (shaped ??= bootShape(routes));
249
+ const maxAge = options.freshness?.maxAge ?? STUB_MAX_AGE;
250
+ if (!(Number.isSafeInteger(maxAge) && maxAge >= 0)) {
251
+ throw new ProtocolError('bad-route', 'freshness.maxAge must be a non-negative integer');
252
+ }
143
253
  const config = {
144
254
  protocol: PROTOCOL_VERSION,
145
255
  version: VERSION,
@@ -148,12 +258,9 @@ export function createSpool(options) {
148
258
  beacons,
149
259
  mode,
150
260
  shadowSample,
151
- clientTag: clientTag(options.cdn
152
- ? {
153
- src: options.cdn.url ?? cdnClientUrl(VERSION),
154
- integrity: options.cdn.integrity
155
- }
156
- : { src: clientPath(VERSION) })
261
+ clientTag: gated
262
+ ? gateTag({ ...tagOptions, held: bootOn ? gatePaths(routes) : undefined, lease: maxAge }, GATE_CORE)
263
+ : clientTag(tagOptions)
157
264
  };
158
265
  if (options.provision)
159
266
  config.provision = options.provision;
@@ -167,13 +274,88 @@ export function createSpool(options) {
167
274
  config.thirdParty = options.thirdParty;
168
275
  if (writesOn)
169
276
  config.writes = writes.map(serializeWrite);
277
+ if (resourceWrites.length > 0)
278
+ config.resourceWrites = resourceWrites;
170
279
  const principalSets = [...datasets.values()].flatMap((d) => d.scope === 'principal' ? [{ name: d.name, maxStaleMs: d.maxStaleMs }] : []);
171
280
  if (principalOn && principalSets.length > 0)
172
281
  config.principalDatasets = principalSets;
173
- if (options.views === false)
282
+ if (!(options.views ?? sink !== undefined))
174
283
  config.views = false;
175
- let script;
176
- const stub = () => workerResponse((script ??= workerScript(config, options.worker)));
284
+ if (stream) {
285
+ config.live = {
286
+ public: stream.public,
287
+ mode: stream.mode,
288
+ ...(stream.hub.sockets ? { socket: true } : {})
289
+ };
290
+ }
291
+ config.lease = maxAge;
292
+ const packs = options.bundles ? packReader(options.bundles) : undefined;
293
+ const ringPack = options.bundles ? ringPacks(options.bundles) : undefined;
294
+ const writeThrough = options.bundles
295
+ ? createResourceWrites({
296
+ datasets,
297
+ store,
298
+ bundles: options.bundles,
299
+ routes,
300
+ blockRows,
301
+ maxBytes
302
+ })
303
+ : undefined;
304
+ const core = options.worker ?? SW_CORE;
305
+ const runtime = shellHash(core);
306
+ const workerCdn = options.cdn?.worker || undefined;
307
+ const workerUrl = typeof workerCdn === 'object' ? workerCdn.url : undefined;
308
+ if (workerUrl !== undefined && !/^https?:\/\//i.test(workerUrl)) {
309
+ throw new ProtocolError('bad-route', 'cdn.worker.url must be an absolute http or https URL');
310
+ }
311
+ if (workerCdn === true && options.worker !== undefined) {
312
+ throw new ProtocolError('bad-route', 'a custom worker needs cdn.worker.url');
313
+ }
314
+ // what follows the config line: the core itself, or the one line that imports it
315
+ const body = async () => workerCdn ? workerImport(workerUrl ?? cdnWorkerUrl(VERSION, await runtime)) : core;
316
+ // the script carries the pack of the store cursor; one text is kept for the cursor it was built at
317
+ let built;
318
+ const stampOf = () => {
319
+ const stamp = typeof options.stamp === 'function' ? options.stamp() : options.stamp;
320
+ if (stamp !== undefined && !(Number.isSafeInteger(stamp) && stamp >= 0)) {
321
+ throw new ProtocolError('bad-route', 'stamp must be a non-negative integer');
322
+ }
323
+ return stamp;
324
+ };
325
+ stampOf();
326
+ const scriptBuilt = async (given) => {
327
+ const cursor = await store.cursor();
328
+ // undefined is unknown: no option and no page seen yet, so the stub names no policy
329
+ const policy = given ?? policyOption ?? lastPolicy;
330
+ const stamp = stampOf();
331
+ const key = `${cursor}\n${JSON.stringify(policy ?? null)}\n${stamp ?? ''}`;
332
+ if (built?.key !== key) {
333
+ const text = (async () => {
334
+ const pack = await packs?.stub(cursor);
335
+ return workerScript({
336
+ ...config,
337
+ runtime: await runtime,
338
+ ...(pack ? { pack } : {}),
339
+ ...(policy ? { policy } : {}),
340
+ ...(stamp !== undefined ? { stamp } : {})
341
+ }, await body());
342
+ })();
343
+ built = { key, text };
344
+ }
345
+ return built;
346
+ };
347
+ const scriptText = async (given) => (await scriptBuilt(given)).text;
348
+ // the hash is made on first use, so a text that fails to build rejects only the caller that asked
349
+ const scriptWithEtag = async () => {
350
+ const current = await scriptBuilt();
351
+ const text = await current.text;
352
+ return { text, etag: await (current.etag ??= workerEtag(text)) };
353
+ };
354
+ function stub(request) {
355
+ return request
356
+ ? workerReply(request, scriptWithEtag, maxAge)
357
+ : workerResponse(scriptText, maxAge);
358
+ }
177
359
  const who = async (request) => options.identity ? await options.identity.resolve(request) : null;
178
360
  const context = (request, url, params, identity, collector = createCollector()) => ({
179
361
  request,
@@ -212,6 +394,9 @@ export function createSpool(options) {
212
394
  if (hit.route.strategy === 'static') {
213
395
  return { response: json({ error: 'a static route is not provisioned' }, 404) };
214
396
  }
397
+ if (hit.route.origin !== undefined) {
398
+ return { response: json({ error: 'a cross-origin route is not provisioned' }, 404) };
399
+ }
215
400
  const identity = await who(request);
216
401
  if (hit.route.scope === 'principal' && !identity) {
217
402
  return { response: json({ error: 'sign in' }, 401) };
@@ -220,8 +405,28 @@ export function createSpool(options) {
220
405
  const collector = createCollector();
221
406
  const ctx = context(request, new URL(path, url), hit.params, identity, collector);
222
407
  const dependsOn = [...(await hit.route.depends(ctx))];
223
- const loaded = await hit.route.load(ctx);
224
- const body = hit.route.strategy === 'assembly' ? loaded : { data: loaded };
408
+ const family = hit.route.family;
409
+ const dataset = family && hit.route.scope === 'principal' ? datasets.get(family.dataset) : undefined;
410
+ let loaded = await hit.route.load(ctx);
411
+ if (family?.key === 'viewer.id' && identity && dataset) {
412
+ const [row] = await dataset.read([identity.principal], {
413
+ request,
414
+ url: ctx.url,
415
+ principal: identity
416
+ });
417
+ loaded = row !== undefined && dataset.key(row) === identity.principal ? row : undefined;
418
+ }
419
+ const view = identity && dataset ? viewOf(dataset, identity, true) : undefined;
420
+ if (view && loaded !== undefined)
421
+ loaded = view(loaded);
422
+ if (hit.route.family && loaded === undefined) {
423
+ return { response: json({ error: 'no row for family' }, 404) };
424
+ }
425
+ // a data family answers with the row's JSON, so its artifact holds the row alone
426
+ const framed = hit.route.family !== undefined && hit.route.strategy !== 'data';
427
+ const body = hit.route.strategy === 'assembly'
428
+ ? loaded
429
+ : { data: framed ? { params: hit.params, row: loaded } : loaded };
225
430
  const collected = collector.collect(vector);
226
431
  const stored = new Set([...collected.records.map((r) => r.id)]);
227
432
  const known = new Set(collected.projections.map((p) => p.id));
@@ -446,9 +651,51 @@ export function createSpool(options) {
446
651
  }
447
652
  }
448
653
  }
654
+ /**
655
+ * The stream. A page that holds a partition the request's session does not own gets `revoked`
656
+ * and nothing else; a stream with no partition is a public one and exists only where the site
657
+ * opted in. The delta in the stream is the tag diff `/.spool/sync` already gives anyone.
658
+ */
659
+ async function respondLive(request, url) {
660
+ if (!stream || request.method !== 'GET')
661
+ return json({ error: 'not found' }, 404);
662
+ const { have, partition } = parseLiveQuery(url);
663
+ const identity = await who(request);
664
+ const secret = options.identity?.secret;
665
+ const owner = async () => identity && secret !== undefined ? await partitionId(secret, identity.principal) : null;
666
+ if (partition !== null && (await owner()) !== partition) {
667
+ const gone = frameText({ event: 'revoked', data: {} });
668
+ return new Response(gone, { headers: LIVE_HEADERS });
669
+ }
670
+ if (partition === null && !stream.public)
671
+ return new Response(null, { status: 204 });
672
+ return await stream.hub.open({
673
+ partition,
674
+ first: await openingFrames(store, have),
675
+ leaseMs: stream.leaseMs,
676
+ keepaliveMs: stream.keepaliveMs,
677
+ maxAgeMs: stream.maxAgeMs,
678
+ signal: request.signal,
679
+ ...(request.headers.get('upgrade')?.toLowerCase() === 'websocket'
680
+ ? { socket: true }
681
+ : {}),
682
+ ...(partition === null
683
+ ? {}
684
+ : {
685
+ valid: async () => {
686
+ const again = await who(request);
687
+ return (again !== null &&
688
+ secret !== undefined &&
689
+ (await partitionId(secret, again.principal)) === partition);
690
+ }
691
+ })
692
+ });
693
+ }
449
694
  async function answer(request, url) {
450
695
  try {
451
696
  const get = request.method === 'GET' || request.method === 'HEAD';
697
+ if (url.pathname === LIVE_PATH)
698
+ return await respondLive(request, url);
452
699
  if (url.pathname === PATHS.sync && get) {
453
700
  const identity = await who(request);
454
701
  const partition = identity && options.identity
@@ -458,6 +705,12 @@ export function createSpool(options) {
458
705
  }
459
706
  if (url.pathname === PATHS.artifact && get)
460
707
  return await respondArtifact(request, url);
708
+ if (url.pathname.startsWith(BUNDLE_DELTA_PREFIX) && get) {
709
+ return await respondDelta(packs, url.pathname.slice(BUNDLE_DELTA_PREFIX.length));
710
+ }
711
+ if (url.pathname.startsWith(BUNDLE_PREFIX) && get) {
712
+ return await respondBundle(options.bundles, url.pathname.slice(BUNDLE_PREFIX.length));
713
+ }
461
714
  if (url.pathname === PATHS.dataset && get) {
462
715
  return await respondDataset(request, url, datasets, { prior, stats: datasetStats }, async () => {
463
716
  const identity = await who(request);
@@ -467,7 +720,7 @@ export function createSpool(options) {
467
720
  identity,
468
721
  partition: await partitionId(options.identity.secret, identity.principal)
469
722
  };
470
- });
723
+ }, families);
471
724
  }
472
725
  if (url.pathname === PATHS.write) {
473
726
  return await writeServer.respond(request, writesOn ? await who(request) : null);
@@ -475,8 +728,16 @@ export function createSpool(options) {
475
728
  if (url.pathname === PATHS.beacon) {
476
729
  return await respondBeacon(request, sink, provide(request), catalogue(request));
477
730
  }
478
- if (url.pathname === clientPath(VERSION) && get) {
479
- return new Response(CLIENT_CORE, {
731
+ if (get &&
732
+ bootOn &&
733
+ url.pathname.startsWith(bootPrefix) &&
734
+ url.pathname.endsWith('.js')) {
735
+ return await respondBoot(options.bundles, url.pathname.slice(bootPrefix.length, -'.js'.length), bootCode, await shapeOf());
736
+ }
737
+ if (get &&
738
+ (url.pathname === clientPath(VERSION) || url.pathname === navigatePath(VERSION))) {
739
+ const script = url.pathname === clientPath(VERSION) ? CLIENT_CORE : NAVIGATE_CORE;
740
+ return new Response(script, {
480
741
  headers: {
481
742
  'content-type': 'text/javascript; charset=utf-8',
482
743
  'cache-control': 'public, max-age=31536000, immutable'
@@ -491,6 +752,40 @@ export function createSpool(options) {
491
752
  return json({ error: 'internal error' }, 500);
492
753
  }
493
754
  }
755
+ /** the ring element for a family page at `version`, or undefined when no pack or row is held */
756
+ async function ringFor(url, version) {
757
+ const pack = await ringPack?.(version);
758
+ if (!pack)
759
+ return undefined;
760
+ const ring = buildRing({
761
+ path: url.pathname,
762
+ version,
763
+ digest: pack.digest,
764
+ routes,
765
+ tables: pack.tables,
766
+ packBytes: pack.bytes,
767
+ ringBytes,
768
+ bootstrap,
769
+ worker: workerMode,
770
+ boot: bootOn ? `${bootPath(VERSION, version)}?r=${(await shapeOf()).id}` : undefined,
771
+ slim: bootOn,
772
+ writes: resourceWrites,
773
+ lease: maxAge
774
+ });
775
+ return ring && ringElement(ring);
776
+ }
777
+ /** the delta a page that holds an older version needs, or undefined; a pointer when it is past the header threshold */
778
+ async function heldElement(request, version) {
779
+ const held = heldVersion(request.headers.get('cookie'));
780
+ if (held === undefined || held >= version || !packs)
781
+ return undefined;
782
+ const delta = await packs.delta(held, version);
783
+ if (!delta)
784
+ return undefined;
785
+ const text = JSON.stringify(delta);
786
+ const fits = new TextEncoder().encode(text).length <= DELTA_HEADER_BYTES;
787
+ return deltaElement(held, version, fits ? text : undefined);
788
+ }
494
789
  async function decorate(request, response, decorateOptions = {}) {
495
790
  if (response.status === 101)
496
791
  return response;
@@ -529,6 +824,16 @@ export function createSpool(options) {
529
824
  envelope.invalidate = diff.changed;
530
825
  envelope.vector = diff.cursor;
531
826
  }
827
+ const haveHeader = request.headers.get(HEADERS.have);
828
+ if (haveHeader !== null && !headers.has(HEADERS.delta)) {
829
+ headers.append('vary', HEADERS.have);
830
+ const have = /^\d{1,15}$/.test(haveHeader) ? Number(haveHeader) : -1;
831
+ const delta = have >= 0 && have < before ? await packs?.delta(have, before) : undefined;
832
+ const text = delta && JSON.stringify(delta);
833
+ if (text && new TextEncoder().encode(text).length <= DELTA_HEADER_BYTES) {
834
+ headers.set(HEADERS.delta, text);
835
+ }
836
+ }
532
837
  if (decorateOptions.clear)
533
838
  envelope.clear = [...decorateOptions.clear];
534
839
  const identity = options.identity ? await who(request) : null;
@@ -536,6 +841,13 @@ export function createSpool(options) {
536
841
  envelope.partition = await partitionId(options.identity.secret, identity.principal);
537
842
  }
538
843
  const hit = read && response.status === 200 ? matchRoute(routes, url.pathname) : null;
844
+ const declined = !decorateOptions.judged && hit?.route.scope === 'public'
845
+ ? responseDecline(response.headers, new Set(), false)
846
+ : null;
847
+ if (hit && declined) {
848
+ await recordDecline(sink, declined, hit.route.pattern, 'gate');
849
+ return response;
850
+ }
539
851
  if (hit) {
540
852
  headers.set('x-spool-route', hit.route.pattern);
541
853
  headers.set('x-spool-mode', hit.route.mode ?? mode);
@@ -546,8 +858,13 @@ export function createSpool(options) {
546
858
  isHtml(response) &&
547
859
  !response.headers.has('content-encoding');
548
860
  const html = tag ? await response.text() : null;
861
+ if (tag)
862
+ lastPolicy = policyHeaders(response.headers);
549
863
  let inline = null;
550
- if (hit && kept && (hit.route.scope === 'public' || identity)) {
864
+ if (hit &&
865
+ kept &&
866
+ hit.route.provisionable !== false &&
867
+ (hit.route.scope === 'public' || identity)) {
551
868
  const ctx = context(request, url, hit.params, identity);
552
869
  envelope.artifact = {
553
870
  id: url.pathname,
@@ -580,6 +897,34 @@ export function createSpool(options) {
580
897
  let tagged = injectClientTag(html, config.clientTag);
581
898
  if (inline !== null)
582
899
  tagged = placeInline(tagged, inlineElement(inline));
900
+ // a gate page names the boot script on every public page, since a visitor may start on any
901
+ const ringed = bootOn
902
+ ? hit === null || hit.route.scope === 'public'
903
+ : hit?.route.family !== undefined && hit.route.scope === 'public';
904
+ if (tag && ringed) {
905
+ const element = await ringFor(url, before);
906
+ if (element !== undefined && !tagged.includes(`id="${RING_ELEMENT_ID}"`)) {
907
+ tagged = placeRing(tagged, element);
908
+ }
909
+ }
910
+ if (tag &&
911
+ resourceWrites.length > 0 &&
912
+ (hit === null || hit.route.scope === 'public') &&
913
+ !tagged.includes(`id="${RING_ELEMENT_ID}"`) &&
914
+ !tagged.includes(`id="${WRITES_ELEMENT_ID}"`)) {
915
+ tagged = placeRing(tagged, writesElement(resourceWrites));
916
+ }
917
+ // the held cookie comes from a page with no worker: any in never mode, a WebKit one in auto mode
918
+ const cookie = request.headers.get('cookie');
919
+ if (tag &&
920
+ cookie !== null &&
921
+ (workerMode === 'never' || (workerMode === 'auto' && heldVersion(cookie) !== undefined))) {
922
+ headers.append('vary', 'cookie');
923
+ const element = await heldElement(request, before);
924
+ if (element !== undefined && !tagged.includes(`id="${DELTA_ELEMENT_ID}"`)) {
925
+ tagged = placeRing(tagged, element);
926
+ }
927
+ }
583
928
  if (tagged !== html) {
584
929
  headers.delete('content-length');
585
930
  const etag = headers.get('etag');
@@ -592,27 +937,111 @@ export function createSpool(options) {
592
937
  headers
593
938
  });
594
939
  }
940
+ async function publish(url) {
941
+ const bundles = options.bundles;
942
+ if (!bundles)
943
+ throw new ProtocolError('bad-bundle', 'publish needs the bundles option');
944
+ const version = await store.cursor();
945
+ const built = await buildBundle({
946
+ store: bundles,
947
+ datasets: [...datasets.values()],
948
+ routes,
949
+ version,
950
+ url,
951
+ blockRows,
952
+ maxBytes
953
+ });
954
+ return { version, bytes: built.bytes.byteLength };
955
+ }
956
+ /** the runtime, the stub, the packs held for the cursor and the deltas to it, and the headers */
957
+ async function publishStatic(sink, given = {}) {
958
+ const call = policySet(given.policy);
959
+ const fallback = policySet(given.fallbackPolicy);
960
+ const chosen = call ?? policyOption ?? lastPolicy ?? fallback;
961
+ const policy = call
962
+ ? 'call'
963
+ : policyOption
964
+ ? 'option'
965
+ : lastPolicy
966
+ ? 'page'
967
+ : fallback
968
+ ? 'fallback'
969
+ : 'none';
970
+ if (given.verifyCore) {
971
+ if (!workerCdn)
972
+ throw new ProtocolError('bad-route', 'verifyCore needs cdn.worker');
973
+ const fetcher = given.verifyCore === true ? fetch : given.verifyCore;
974
+ await checkCore(workerUrl ?? cdnWorkerUrl(VERSION, await runtime), await runtime, fetcher);
975
+ }
976
+ if (given.url && options.bundles)
977
+ await publish(given.url);
978
+ const version = await store.cursor();
979
+ const paths = [];
980
+ const put = async (path, bytes, contentType, immutable) => {
981
+ if (immutable && (await sink.has?.(path)))
982
+ return;
983
+ await sink.put(path, bytes, contentType);
984
+ paths.push(path);
985
+ };
986
+ const js = 'text/javascript; charset=utf-8';
987
+ await put(clientPath(VERSION), new TextEncoder().encode(CLIENT_CORE), js, true);
988
+ await put(navigatePath(VERSION), new TextEncoder().encode(NAVIGATE_CORE), js, true);
989
+ await put(PATHS.worker, new TextEncoder().encode(await scriptText(chosen)), js, false);
990
+ if (options.bundles && packs) {
991
+ const bundles = options.bundles;
992
+ for (let back = 0; back <= STUB_DELTAS; back++) {
993
+ const from = version - back;
994
+ const bytes = from >= 0 ? await bundles.get(from) : undefined;
995
+ if (bytes === undefined)
996
+ continue;
997
+ await put(`${BUNDLE_PREFIX}${from}`, bytes, 'application/octet-stream', true);
998
+ if (bootOn) {
999
+ const boot = await respondBoot(bundles, String(from), bootCode, await shapeOf());
1000
+ // the current pack's boot script is rewritten: a deploy can change the routes and templates it carries
1001
+ await put(bootPath(VERSION, from), new Uint8Array(await boot.arrayBuffer()), js, from < version);
1002
+ }
1003
+ const delta = from < version ? await packs.delta(from, version) : undefined;
1004
+ if (delta) {
1005
+ const text = new TextEncoder().encode(JSON.stringify(delta));
1006
+ await put(`${BUNDLE_DELTA_PREFIX}${from}/${version}`, text, 'application/json', true);
1007
+ }
1008
+ }
1009
+ }
1010
+ const headers = new TextEncoder().encode(staticHeaders(maxAge));
1011
+ await put('/_headers', headers, 'text/plain; charset=utf-8', false);
1012
+ return { version, paths, policy };
1013
+ }
595
1014
  return {
596
1015
  config,
597
1016
  stub,
1017
+ publishStatic,
598
1018
  partition: async (principal) => {
599
1019
  if (!options.identity) {
600
1020
  throw new ProtocolError('bad-route', 'partition needs an identity option');
601
1021
  }
602
1022
  return partitionId(options.identity.secret, principal);
603
1023
  },
1024
+ async revoke(principal) {
1025
+ if (!stream || !options.identity)
1026
+ return;
1027
+ await stream.hub.revoke(await partitionId(options.identity.secret, principal));
1028
+ },
604
1029
  respond,
605
1030
  decorate,
1031
+ publish,
606
1032
  async handle(request, next) {
607
1033
  const early = await respond(request);
608
1034
  if (early)
609
1035
  return early;
610
1036
  const url = new URL(request.url);
611
- if (url.pathname === PATHS.worker && request.method === 'GET')
612
- return stub();
1037
+ if (url.pathname === PATHS.worker && request.method === 'GET') {
1038
+ return workerMode === 'never' ? json({ error: 'no worker' }, 404) : stub(request);
1039
+ }
613
1040
  const began = work ? meter() : 0;
614
1041
  const vector = await store.cursor();
615
- const response = await decorate(request, await next(request), { vector });
1042
+ const declared = writeFor(resourceWrites, request.method, url, url.origin);
1043
+ const answer = declared && writeThrough ? await writeThrough(request, next) : await next(request);
1044
+ const response = await decorate(request, answer, { vector });
616
1045
  if (work) {
617
1046
  const hit = matchRoute(routes, url.pathname);
618
1047
  work.record(hit ? hit.route.pattern : 'other', meter() - began);