@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.
Files changed (206) hide show
  1. package/README.md +279 -1607
  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 +123 -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 +34 -2
  31. package/dist/client/register.js +64 -7
  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 +80 -1
  63. package/dist/protocol/config.js +31 -1
  64. package/dist/protocol/dataset.d.ts +189 -0
  65. package/dist/protocol/dataset.js +212 -0
  66. package/dist/protocol/envelope.d.ts +34 -1
  67. package/dist/protocol/envelope.js +16 -1
  68. package/dist/protocol/errors.d.ts +1 -1
  69. package/dist/protocol/graph.d.ts +23 -4
  70. package/dist/protocol/graph.js +34 -3
  71. package/dist/protocol/index-rows.d.ts +22 -0
  72. package/dist/protocol/index-rows.js +53 -0
  73. package/dist/protocol/index.d.ts +5 -0
  74. package/dist/protocol/index.js +5 -0
  75. package/dist/protocol/normalise.d.ts +4 -4
  76. package/dist/protocol/normalise.js +9 -5
  77. package/dist/protocol/plan.d.ts +100 -0
  78. package/dist/protocol/plan.js +661 -0
  79. package/dist/protocol/provision.d.ts +46 -0
  80. package/dist/protocol/provision.js +38 -0
  81. package/dist/protocol/query.d.ts +66 -5
  82. package/dist/protocol/query.js +103 -4
  83. package/dist/protocol/ring.d.ts +58 -0
  84. package/dist/protocol/ring.js +6 -0
  85. package/dist/protocol/route.d.ts +63 -13
  86. package/dist/protocol/route.js +126 -6
  87. package/dist/protocol/tag.d.ts +22 -2
  88. package/dist/protocol/tag.js +25 -4
  89. package/dist/protocol/view.d.ts +2 -0
  90. package/dist/protocol/view.js +8 -2
  91. package/dist/protocol/views.d.ts +6 -0
  92. package/dist/protocol/views.js +15 -0
  93. package/dist/protocol/write.d.ts +234 -0
  94. package/dist/protocol/write.js +343 -0
  95. package/dist/proxy/eligible.d.ts +22 -1
  96. package/dist/proxy/eligible.js +62 -15
  97. package/dist/proxy/proxy.js +28 -18
  98. package/dist/react-router/index.d.ts +20 -0
  99. package/dist/react-router/index.js +60 -0
  100. package/dist/react-router/pages.d.ts +34 -0
  101. package/dist/react-router/pages.js +83 -0
  102. package/dist/report/dataset.d.ts +48 -0
  103. package/dist/report/dataset.js +93 -0
  104. package/dist/report/demand.d.ts +49 -0
  105. package/dist/report/demand.js +60 -0
  106. package/dist/report/derive.d.ts +285 -0
  107. package/dist/report/derive.js +676 -1
  108. package/dist/report/economics.d.ts +66 -1
  109. package/dist/report/economics.js +208 -1
  110. package/dist/report/headline.d.ts +158 -0
  111. package/dist/report/headline.js +473 -0
  112. package/dist/report/index.d.ts +4 -0
  113. package/dist/report/index.js +4 -0
  114. package/dist/report/inputs.d.ts +176 -0
  115. package/dist/report/inputs.js +248 -0
  116. package/dist/report/institution.d.ts +188 -1
  117. package/dist/report/institution.js +146 -0
  118. package/dist/report/loops.d.ts +73 -0
  119. package/dist/report/loops.js +101 -0
  120. package/dist/report/measurement.d.ts +1 -1
  121. package/dist/report/measurement.js +7 -1
  122. package/dist/report/model.d.ts +2 -0
  123. package/dist/report/model.js +4 -0
  124. package/dist/report/thirdparty.d.ts +57 -0
  125. package/dist/report/thirdparty.js +111 -0
  126. package/dist/report/traffic.d.ts +8 -0
  127. package/dist/report/traffic.js +7 -0
  128. package/dist/report/trim.d.ts +56 -0
  129. package/dist/report/trim.js +83 -1
  130. package/dist/server/bundle.d.ts +55 -0
  131. package/dist/server/bundle.js +172 -0
  132. package/dist/server/dataset.d.ts +54 -0
  133. package/dist/server/dataset.js +355 -0
  134. package/dist/server/embedded.d.ts +7 -3
  135. package/dist/server/embedded.js +7 -3
  136. package/dist/server/index.d.ts +6 -0
  137. package/dist/server/index.js +6 -0
  138. package/dist/server/live-sockets.d.ts +54 -0
  139. package/dist/server/live-sockets.js +131 -0
  140. package/dist/server/live.d.ts +146 -0
  141. package/dist/server/live.js +228 -0
  142. package/dist/server/ring.d.ts +77 -0
  143. package/dist/server/ring.js +311 -0
  144. package/dist/server/spool.d.ts +158 -3
  145. package/dist/server/spool.js +578 -41
  146. package/dist/server/static.d.ts +42 -0
  147. package/dist/server/static.js +29 -0
  148. package/dist/server/stub.d.ts +43 -5
  149. package/dist/server/stub.js +126 -8
  150. package/dist/server/write.d.ts +49 -0
  151. package/dist/server/write.js +223 -0
  152. package/dist/stores/durable-object.d.ts +46 -0
  153. package/dist/stores/durable-object.js +114 -0
  154. package/dist/sw/analytics.d.ts +7 -1
  155. package/dist/sw/analytics.js +16 -2
  156. package/dist/sw/coverage.d.ts +6 -0
  157. package/dist/sw/coverage.js +34 -0
  158. package/dist/sw/dataset-sync.d.ts +76 -0
  159. package/dist/sw/dataset-sync.js +149 -0
  160. package/dist/sw/dataset.d.ts +91 -0
  161. package/dist/sw/dataset.js +318 -0
  162. package/dist/sw/family.d.ts +15 -0
  163. package/dist/sw/family.js +48 -0
  164. package/dist/sw/graph/index.d.ts +31 -1
  165. package/dist/sw/graph/index.js +57 -5
  166. package/dist/sw/graph/invalidate.d.ts +15 -3
  167. package/dist/sw/graph/invalidate.js +42 -4
  168. package/dist/sw/idb.d.ts +2 -0
  169. package/dist/sw/idb.js +80 -11
  170. package/dist/sw/index.d.ts +2 -0
  171. package/dist/sw/index.js +1 -0
  172. package/dist/sw/pack/store.d.ts +128 -0
  173. package/dist/sw/pack/store.js +737 -0
  174. package/dist/sw/partition.d.ts +6 -1
  175. package/dist/sw/partition.js +21 -2
  176. package/dist/sw/policy.d.ts +2 -0
  177. package/dist/sw/policy.js +11 -0
  178. package/dist/sw/principal-dataset.d.ts +64 -0
  179. package/dist/sw/principal-dataset.js +254 -0
  180. package/dist/sw/probe.d.ts +16 -0
  181. package/dist/sw/probe.js +25 -0
  182. package/dist/sw/render.d.ts +7 -2
  183. package/dist/sw/render.js +19 -5
  184. package/dist/sw/replica.d.ts +12 -3
  185. package/dist/sw/replica.js +40 -5
  186. package/dist/sw/router.d.ts +3 -2
  187. package/dist/sw/router.js +15 -2
  188. package/dist/sw/runtime.d.ts +90 -2
  189. package/dist/sw/runtime.js +857 -61
  190. package/dist/sw/scheduler.d.ts +6 -1
  191. package/dist/sw/scheduler.js +22 -5
  192. package/dist/sw/seal.d.ts +13 -0
  193. package/dist/sw/seal.js +13 -4
  194. package/dist/sw/static.d.ts +18 -0
  195. package/dist/sw/static.js +84 -1
  196. package/dist/sw/storage.d.ts +51 -1
  197. package/dist/sw/storage.js +33 -1
  198. package/dist/sw/write.d.ts +147 -0
  199. package/dist/sw/write.js +625 -0
  200. package/dist/version.d.ts +1 -1
  201. package/dist/version.js +1 -2
  202. package/dist/vite/index.d.ts +26 -0
  203. package/dist/vite/index.js +66 -0
  204. package/dist/vue/index.d.ts +36 -0
  205. package/dist/vue/index.js +53 -0
  206. package/package.json +65 -4
@@ -1,45 +1,159 @@
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
- import { collectRefs, createCollector } from '../protocol/graph.js';
8
+ import { collectRefs, createCollector, resolveRefs, splitRowId } from '../protocol/graph.js';
8
9
  import { shellHash } from '../protocol/hash.js';
9
10
  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
- import { verifyProof } from '../protocol/query.js';
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';
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';
16
21
  import { VERSION } from '../version.js';
17
22
  import { respondBeacon } from './beacon.js';
18
- 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';
19
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';
20
29
  import { compressedSize } from './size.js';
21
- 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';
22
32
  import { json, respondSync } from './sync.js';
33
+ import { createResourceWrites, createWriteServer, DELTA_HEADER_BYTES } from './write.js';
23
34
  /** most changed tags a response carries inline before the worker is left to ask `/.spool/sync` */
24
35
  export const MAX_PIGGYBACK = 32;
25
36
  /** most pages one catalogue request makes the server build, whatever its route suggests */
26
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;
27
40
  /** the strategies whose artifact is data for a template or projection the device already holds */
28
41
  const ENTRY_STRATEGIES = ['template', 'projection', 'data'];
29
42
  function isHtml(response) {
30
43
  return (response.headers.get('content-type') ?? '').toLowerCase().includes('text/html');
31
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
+ }
32
67
  /** wires routes, a version store and the endpoints into one handler set */
33
68
  export function createSpool(options) {
34
- const { store, sink } = options;
35
- 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
+ ];
36
76
  const seen = new Set();
37
77
  for (const route of routes) {
38
78
  if (seen.has(route.pattern)) {
39
79
  throw new ProtocolError('bad-route', `${route.pattern}: declared twice`);
40
80
  }
41
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
+ }
86
+ }
87
+ const datasets = new Map();
88
+ const families = new Set(routes.flatMap((route) => (route.family ? [route.family.dataset] : [])));
89
+ const principalOn = options.principalDatasets === true;
90
+ for (const dataset of options.datasets ?? []) {
91
+ if (datasets.has(dataset.name)) {
92
+ throw new ProtocolError('bad-dataset', `${dataset.name}: declared twice`);
93
+ }
94
+ if (dataset.scope === 'principal') {
95
+ if (!principalOn) {
96
+ throw new ProtocolError('bad-dataset', `${dataset.name}: a principal dataset needs the principal datasets flag: set the principalDatasets option of createSpool`);
97
+ }
98
+ if (!options.identity) {
99
+ throw new ProtocolError('bad-dataset', `${dataset.name}: a principal dataset needs an identity option`);
100
+ }
101
+ }
102
+ datasets.set(dataset.name, dataset);
103
+ }
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
+ }
111
+ const name = route.query?.dataset;
112
+ if (name !== undefined) {
113
+ const dataset = datasets.get(name);
114
+ if (!dataset) {
115
+ throw new ProtocolError('bad-route', `${route.pattern}: query.dataset "${name}" is not declared`);
116
+ }
117
+ if (dataset.scope !== route.scope) {
118
+ throw new ProtocolError('bad-route', `${route.pattern}: a ${route.scope} route cannot query the ${dataset.scope} dataset "${name}"`);
119
+ }
120
+ }
121
+ for (const join of route.query?.plan?.join ?? []) {
122
+ if (!datasets.has(join.dataset)) {
123
+ throw new ProtocolError('bad-route', `${route.pattern}: query.plan join dataset "${join.dataset}" is not declared`);
124
+ }
125
+ if (datasets.get(join.dataset)?.scope !== route.scope) {
126
+ throw new ProtocolError('bad-route', `${route.pattern}: query.plan join dataset "${join.dataset}" must be ${route.scope}`);
127
+ }
128
+ }
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
+ }
136
+ const writes = [...(options.writes?.classes ?? [])];
137
+ const writeNames = new Set();
138
+ for (const write of writes) {
139
+ if (writeNames.has(write.name)) {
140
+ throw new ProtocolError('bad-write', `${write.name}: declared twice`);
141
+ }
142
+ writeNames.add(write.name);
143
+ const target = routes.find((route) => route.pattern === write.path);
144
+ if (!target || target.scope !== 'principal') {
145
+ throw new ProtocolError('bad-write', `${write.name}: path ${write.path} must be a declared principal route`);
146
+ }
42
147
  }
148
+ if (writes.length > 0 && !options.identity) {
149
+ throw new ProtocolError('bad-write', 'writes need an identity option');
150
+ }
151
+ const writesOn = options.writes?.enabled === true && writes.length > 0;
152
+ const writeServer = createWriteServer({
153
+ enabled: writesOn,
154
+ writes,
155
+ ...(options.writes?.remember !== undefined ? { remember: options.writes.remember } : {})
156
+ });
43
157
  const mode = options.mode ?? 'serve';
44
158
  if (mode !== 'serve' && mode !== 'shadow') {
45
159
  throw new ProtocolError('bad-route', `unknown mode "${String(mode)}"`);
@@ -66,6 +180,7 @@ export function createSpool(options) {
66
180
  const collectors = [...(options.collectors ?? [])];
67
181
  checkCollectors(collectors);
68
182
  const prior = priorFrom(options.provision);
183
+ const datasetStats = new Map();
69
184
  const catalogueBytes = options.catalogueBytes ?? DEFAULT_CATALOGUE_BYTES;
70
185
  if (!(Number.isFinite(catalogueBytes) && catalogueBytes >= 0)) {
71
186
  throw new ProtocolError('bad-route', 'catalogueBytes must be a non-negative number');
@@ -79,6 +194,62 @@ export function createSpool(options) {
79
194
  if (!(Number.isFinite(inlineMax) && inlineMax >= 0)) {
80
195
  throw new ProtocolError('bad-route', 'inlineMaxBytes must be a non-negative number');
81
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
+ }
82
253
  const config = {
83
254
  protocol: PROTOCOL_VERSION,
84
255
  version: VERSION,
@@ -87,12 +258,9 @@ export function createSpool(options) {
87
258
  beacons,
88
259
  mode,
89
260
  shadowSample,
90
- clientTag: clientTag(options.cdn
91
- ? {
92
- src: options.cdn.url ?? cdnClientUrl(VERSION),
93
- integrity: options.cdn.integrity
94
- }
95
- : { src: clientPath(VERSION) })
261
+ clientTag: gated
262
+ ? gateTag({ ...tagOptions, held: bootOn ? gatePaths(routes) : undefined, lease: maxAge }, GATE_CORE)
263
+ : clientTag(tagOptions)
96
264
  };
97
265
  if (options.provision)
98
266
  config.provision = options.provision;
@@ -104,10 +272,90 @@ export function createSpool(options) {
104
272
  config.collectors = collectors;
105
273
  if (options.thirdParty)
106
274
  config.thirdParty = options.thirdParty;
107
- if (options.views === false)
275
+ if (writesOn)
276
+ config.writes = writes.map(serializeWrite);
277
+ if (resourceWrites.length > 0)
278
+ config.resourceWrites = resourceWrites;
279
+ const principalSets = [...datasets.values()].flatMap((d) => d.scope === 'principal' ? [{ name: d.name, maxStaleMs: d.maxStaleMs }] : []);
280
+ if (principalOn && principalSets.length > 0)
281
+ config.principalDatasets = principalSets;
282
+ if (!(options.views ?? sink !== undefined))
108
283
  config.views = false;
109
- let script;
110
- 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
+ }
111
359
  const who = async (request) => options.identity ? await options.identity.resolve(request) : null;
112
360
  const context = (request, url, params, identity, collector = createCollector()) => ({
113
361
  request,
@@ -115,7 +363,8 @@ export function createSpool(options) {
115
363
  params,
116
364
  identity,
117
365
  record: collector.record,
118
- projection: collector.projection
366
+ projection: collector.projection,
367
+ row: collector.row
119
368
  });
120
369
  /** drops the records and projections the worker already holds at a version no change has passed */
121
370
  async function trim(artifact, have) {
@@ -145,6 +394,9 @@ export function createSpool(options) {
145
394
  if (hit.route.strategy === 'static') {
146
395
  return { response: json({ error: 'a static route is not provisioned' }, 404) };
147
396
  }
397
+ if (hit.route.origin !== undefined) {
398
+ return { response: json({ error: 'a cross-origin route is not provisioned' }, 404) };
399
+ }
148
400
  const identity = await who(request);
149
401
  if (hit.route.scope === 'principal' && !identity) {
150
402
  return { response: json({ error: 'sign in' }, 401) };
@@ -153,11 +405,28 @@ export function createSpool(options) {
153
405
  const collector = createCollector();
154
406
  const ctx = context(request, new URL(path, url), hit.params, identity, collector);
155
407
  const dependsOn = [...(await hit.route.depends(ctx))];
156
- const loaded = await hit.route.load(ctx);
157
- if (hit.route.query && !(await verifyProof(hit.route.query, loaded))) {
158
- throw new ProtocolError('bad-artifact', `${path}: a query route's load needs an array at "${hit.route.query.list}" and a matching proof`);
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) };
159
424
  }
160
- const body = hit.route.strategy === 'assembly' ? loaded : { data: loaded };
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 };
161
430
  const collected = collector.collect(vector);
162
431
  const stored = new Set([...collected.records.map((r) => r.id)]);
163
432
  const known = new Set(collected.projections.map((p) => p.id));
@@ -172,6 +441,26 @@ export function createSpool(options) {
172
441
  throw new ProtocolError('bad-artifact', `${path} references projection ${id} it did not register`);
173
442
  }
174
443
  }
444
+ for (const id of refs.rows ?? []) {
445
+ const at = splitRowId(id);
446
+ if (!at || datasets.get(at.dataset)?.scope !== hit.route.scope) {
447
+ throw new ProtocolError('bad-artifact', `${path} references row ${id} of no dataset here`);
448
+ }
449
+ }
450
+ if (hit.route.query && hit.route.query.dataset === undefined) {
451
+ // the worker checks the proof after it resolves refs, so the server checks the same text
452
+ const recordsById = new Map(collected.records.map((r) => [r.id, r]));
453
+ const projectionsById = new Map(collected.projections.map((p) => [p.id, p]));
454
+ const lookup = {
455
+ record: (id) => recordsById.get(id),
456
+ projection: (id) => projectionsById.get(id)
457
+ };
458
+ const resolved = resolveRefs(loaded, lookup);
459
+ if (!resolved.ok ||
460
+ !(await verifyProof(hit.route.query, resolved.value, keyField(hit.route.query)))) {
461
+ throw new ProtocolError('bad-artifact', `${path}: a query route's load needs an array at "${hit.route.query.list}" and a matching proof over its resolved rows`);
462
+ }
463
+ }
175
464
  const artifact = parseArtifact({
176
465
  protocol: PROTOCOL_VERSION,
177
466
  id: path,
@@ -184,18 +473,24 @@ export function createSpool(options) {
184
473
  ...(collected.projections.length > 0 ? { projections: collected.projections } : {})
185
474
  });
186
475
  const trimmed = await trim(artifact, have);
187
- if (weighed && hit.route.scope === 'public') {
476
+ if (weighed) {
188
477
  const bytes = new TextEncoder().encode(JSON.stringify(trimmed)).length;
189
- const mine = { ...prior, ...hit.route.provision };
190
- const data = hit.route.strategy === 'data';
191
- const worth = benefit({
192
- id: path,
193
- kind: 'route',
194
- provision: { bytes, requests: 1 },
195
- avoided: { bytes: data ? bytes : prior.avoidedBytes, requests: 1 }
196
- }, mine);
197
- if (worth <= 0)
198
- return { declined: true };
478
+ // a query.dataset route's rows are bounded by the dataset's maxBytes at its seed
479
+ const budget = hit.route.query?.dataset === undefined ? hit.route.provision?.maxBytes : undefined;
480
+ if (budget !== undefined && bytes > budget)
481
+ return { declined: 'budget' };
482
+ if (hit.route.scope === 'public') {
483
+ const mine = { ...prior, ...hit.route.provision };
484
+ const data = hit.route.strategy === 'data';
485
+ const worth = benefit({
486
+ id: path,
487
+ kind: 'route',
488
+ provision: { bytes, requests: 1 },
489
+ avoided: { bytes: data ? bytes : prior.avoidedBytes, requests: 1 }
490
+ }, mine);
491
+ if (worth <= 0)
492
+ return { declined: 'benefit' };
493
+ }
199
494
  }
200
495
  const out = { artifact: trimmed };
201
496
  if (identity && hit.route.scope === 'principal') {
@@ -226,7 +521,11 @@ export function createSpool(options) {
226
521
  if ('declined' in built) {
227
522
  return new Response(null, {
228
523
  status: 204,
229
- headers: { 'cache-control': 'no-store', [HEADERS.provision]: 'declined' }
524
+ headers: {
525
+ 'cache-control': 'no-store',
526
+ [HEADERS.provision]: 'declined',
527
+ [HEADERS.provisionReason]: built.declined
528
+ }
230
529
  });
231
530
  }
232
531
  const headers = { [HEADERS.protocol]: String(PROTOCOL_VERSION) };
@@ -352,9 +651,51 @@ export function createSpool(options) {
352
651
  }
353
652
  }
354
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
+ }
355
694
  async function answer(request, url) {
356
695
  try {
357
696
  const get = request.method === 'GET' || request.method === 'HEAD';
697
+ if (url.pathname === LIVE_PATH)
698
+ return await respondLive(request, url);
358
699
  if (url.pathname === PATHS.sync && get) {
359
700
  const identity = await who(request);
360
701
  const partition = identity && options.identity
@@ -364,11 +705,39 @@ export function createSpool(options) {
364
705
  }
365
706
  if (url.pathname === PATHS.artifact && get)
366
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
+ }
714
+ if (url.pathname === PATHS.dataset && get) {
715
+ return await respondDataset(request, url, datasets, { prior, stats: datasetStats }, async () => {
716
+ const identity = await who(request);
717
+ if (!identity || !options.identity)
718
+ return null;
719
+ return {
720
+ identity,
721
+ partition: await partitionId(options.identity.secret, identity.principal)
722
+ };
723
+ }, families);
724
+ }
725
+ if (url.pathname === PATHS.write) {
726
+ return await writeServer.respond(request, writesOn ? await who(request) : null);
727
+ }
367
728
  if (url.pathname === PATHS.beacon) {
368
729
  return await respondBeacon(request, sink, provide(request), catalogue(request));
369
730
  }
370
- if (url.pathname === clientPath(VERSION) && get) {
371
- 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, {
372
741
  headers: {
373
742
  'content-type': 'text/javascript; charset=utf-8',
374
743
  'cache-control': 'public, max-age=31536000, immutable'
@@ -383,6 +752,40 @@ export function createSpool(options) {
383
752
  return json({ error: 'internal error' }, 500);
384
753
  }
385
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
+ }
386
789
  async function decorate(request, response, decorateOptions = {}) {
387
790
  if (response.status === 101)
388
791
  return response;
@@ -421,6 +824,16 @@ export function createSpool(options) {
421
824
  envelope.invalidate = diff.changed;
422
825
  envelope.vector = diff.cursor;
423
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
+ }
424
837
  if (decorateOptions.clear)
425
838
  envelope.clear = [...decorateOptions.clear];
426
839
  const identity = options.identity ? await who(request) : null;
@@ -428,6 +841,13 @@ export function createSpool(options) {
428
841
  envelope.partition = await partitionId(options.identity.secret, identity.principal);
429
842
  }
430
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
+ }
431
851
  if (hit) {
432
852
  headers.set('x-spool-route', hit.route.pattern);
433
853
  headers.set('x-spool-mode', hit.route.mode ?? mode);
@@ -438,8 +858,13 @@ export function createSpool(options) {
438
858
  isHtml(response) &&
439
859
  !response.headers.has('content-encoding');
440
860
  const html = tag ? await response.text() : null;
861
+ if (tag)
862
+ lastPolicy = policyHeaders(response.headers);
441
863
  let inline = null;
442
- if (hit && kept && (hit.route.scope === 'public' || identity)) {
864
+ if (hit &&
865
+ kept &&
866
+ hit.route.provisionable !== false &&
867
+ (hit.route.scope === 'public' || identity)) {
443
868
  const ctx = context(request, url, hit.params, identity);
444
869
  envelope.artifact = {
445
870
  id: url.pathname,
@@ -472,6 +897,34 @@ export function createSpool(options) {
472
897
  let tagged = injectClientTag(html, config.clientTag);
473
898
  if (inline !== null)
474
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
+ }
475
928
  if (tagged !== html) {
476
929
  headers.delete('content-length');
477
930
  const etag = headers.get('etag');
@@ -484,27 +937,111 @@ export function createSpool(options) {
484
937
  headers
485
938
  });
486
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
+ }
487
1014
  return {
488
1015
  config,
489
1016
  stub,
1017
+ publishStatic,
490
1018
  partition: async (principal) => {
491
1019
  if (!options.identity) {
492
1020
  throw new ProtocolError('bad-route', 'partition needs an identity option');
493
1021
  }
494
1022
  return partitionId(options.identity.secret, principal);
495
1023
  },
1024
+ async revoke(principal) {
1025
+ if (!stream || !options.identity)
1026
+ return;
1027
+ await stream.hub.revoke(await partitionId(options.identity.secret, principal));
1028
+ },
496
1029
  respond,
497
1030
  decorate,
1031
+ publish,
498
1032
  async handle(request, next) {
499
1033
  const early = await respond(request);
500
1034
  if (early)
501
1035
  return early;
502
1036
  const url = new URL(request.url);
503
- if (url.pathname === PATHS.worker && request.method === 'GET')
504
- return stub();
1037
+ if (url.pathname === PATHS.worker && request.method === 'GET') {
1038
+ return workerMode === 'never' ? json({ error: 'no worker' }, 404) : stub(request);
1039
+ }
505
1040
  const began = work ? meter() : 0;
506
1041
  const vector = await store.cursor();
507
- 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 });
508
1045
  if (work) {
509
1046
  const hit = matchRoute(routes, url.pathname);
510
1047
  work.record(hit ? hit.route.pattern : 'other', meter() - began);