@ogygia/postrender 0.12.0 → 0.14.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 +5 -2
- package/dist/adapters/akamai.d.ts +17 -2
- package/dist/adapters/akamai.js +47 -41
- package/dist/adapters/cloudflare-kv.d.ts +24 -2
- package/dist/adapters/cloudflare-kv.js +59 -42
- package/dist/adapters/cloudflare.d.ts +17 -2
- package/dist/adapters/cloudflare.js +45 -27
- package/dist/adapters/cloudfront.d.ts +11 -2
- package/dist/adapters/cloudfront.js +34 -24
- package/dist/adapters/upstash.d.ts +19 -2
- package/dist/adapters/upstash.js +85 -70
- package/dist/adapters/valkey.d.ts +19 -2
- package/dist/adapters/valkey.js +74 -55
- package/dist/app.js +8 -3
- package/dist/handle.d.ts +16 -0
- package/dist/handle.js +20 -5
- package/dist/index.d.ts +33 -8
- package/dist/index.js +13 -14
- package/dist/key.d.ts +3 -8
- package/dist/key.js +9 -12
- package/dist/memory-store.d.ts +13 -2
- package/dist/memory-store.js +40 -38
- package/dist/messages.d.ts +78 -0
- package/dist/messages.js +150 -0
- package/dist/observe.d.ts +6 -10
- package/dist/observe.js +10 -59
- package/dist/registry.d.ts +54 -5
- package/dist/registry.js +225 -54
- package/dist/server.d.ts +6 -8
- package/dist/server.js +268 -92
- package/dist/stitch.d.ts +8 -22
- package/dist/stitch.js +42 -66
- package/dist/types.d.ts +5 -12
- package/dist/vite.d.ts +17 -1
- package/dist/vite.js +18 -11
- package/package.json +16 -5
package/dist/server.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { current_runtime, enter_flight, evict_tags, handled_runtime, installed_runtime, retracting, self_evict, store_call, store_get, store_render } from "./registry.js";
|
|
2
2
|
import { observe_event } from "./observe.js";
|
|
3
3
|
import { esi_rewrite, stitch_html, stitch_modes } from "./stitch.js";
|
|
4
4
|
import { set_postrender_app } from "./app.js";
|
|
5
|
+
import { warn } from "./messages.js";
|
|
6
|
+
import { STALE, clock, flights, say } from "@ogygia/machine";
|
|
5
7
|
import { createHash } from "node:crypto";
|
|
6
|
-
import { building, dev } from "$app/env";
|
|
8
|
+
import { building, dev, version } from "$app/env";
|
|
7
9
|
import { STORED_DOCUMENT_META, defineServer } from "@ogygia/core/plugin";
|
|
10
|
+
import { head_end } from "@ogygia/core/internal/server";
|
|
8
11
|
// src/server.ts
|
|
9
12
|
/**
|
|
10
13
|
* The postrender plugin's server half (imported by the build through
|
|
11
|
-
* `
|
|
14
|
+
* `ogygia:server/server-plugins`, with the `postrender({ ttl, default })` options).
|
|
12
15
|
*
|
|
13
16
|
* `page`: the read path. A GET, no query string, not prerendering, on a route that opts in: a stored
|
|
14
17
|
* copy answers — Kit, loads and Svelte never run. A concurrent cold miss JOINS the in-flight render
|
|
@@ -35,10 +38,54 @@ const BUILD_MARKER_KEY = "\0og:build";
|
|
|
35
38
|
* store saw), not a page, so the pages' TTL backstop must not apply to it: a deploy a month after
|
|
36
39
|
* the last one still has to find it. */
|
|
37
40
|
const BUILD_MARKER_TTL_SEC = 31536e4;
|
|
38
|
-
/** The
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
/** The tag every page carries for the build that stored it: a new build drops the old one's. */
|
|
42
|
+
const build_tag = (build) => `b:${build}`;
|
|
43
|
+
/** The deploy checks, per store runtime and build: one per boot, shared by the page path and
|
|
44
|
+
* federation's `lastBuild` ask (whichever asks first moves the marker; both hear one answer). A
|
|
45
|
+
* failed check is not kept: the next request tries again. */
|
|
46
|
+
const deploys = /* @__PURE__ */ new WeakMap();
|
|
47
|
+
/**
|
|
48
|
+
* A deploy is a write (internal/notes/freeze.md: prerender is the special case where the only write
|
|
49
|
+
* is a deploy). What makes it one is the entry's build label: a page is served only to the build that
|
|
50
|
+
* stored it ({@link servable}), so no build ever serves another's, a rolling deploy's old instances
|
|
51
|
+
* included. This check is the early cleanup beside it: the build the store saw before this one, whose
|
|
52
|
+
* pages are dropped (origin and edges: an edge never asks the label), then the marker moves to this
|
|
53
|
+
* build. The evict goes first, so one that fails leaves the marker where it was and the next request
|
|
54
|
+
* tries again. test/deploy.test.ts (#947).
|
|
55
|
+
*/
|
|
56
|
+
function deploy_check(runtime, build) {
|
|
57
|
+
let checks = deploys.get(runtime);
|
|
58
|
+
if (!checks) {
|
|
59
|
+
checks = flights({ run: async (build) => {
|
|
60
|
+
const { store } = runtime;
|
|
61
|
+
const marker = await store_call("get", BUILD_MARKER_KEY, () => store.get(BUILD_MARKER_KEY));
|
|
62
|
+
const seen = marker?.kind === "page" ? marker.html : void 0;
|
|
63
|
+
if (seen !== void 0 && seen !== build) {
|
|
64
|
+
const { failed } = await evict_tags([build_tag(seen)], runtime);
|
|
65
|
+
if (failed.length) throw failed[0];
|
|
66
|
+
}
|
|
67
|
+
await store_call("put", BUILD_MARKER_KEY, () => store.put(BUILD_MARKER_KEY, {
|
|
68
|
+
kind: "page",
|
|
69
|
+
html: build,
|
|
70
|
+
headers: {},
|
|
71
|
+
created: clock.wall()
|
|
72
|
+
}, {
|
|
73
|
+
ttl: BUILD_MARKER_TTL_SEC,
|
|
74
|
+
tags: []
|
|
75
|
+
}));
|
|
76
|
+
return seen;
|
|
77
|
+
} });
|
|
78
|
+
deploys.set(runtime, checks);
|
|
79
|
+
}
|
|
80
|
+
return checks.get(build);
|
|
81
|
+
}
|
|
82
|
+
/** A stored entry this build may serve: one it stored itself (another build's, or one with no build,
|
|
83
|
+
* is a miss: the label is the deploy's truth), within the TTL backstop (an older one is a miss: a
|
|
84
|
+
* store with native expiry has dropped it already, one without still lets its pages expire; one
|
|
85
|
+
* with no usable creation time cannot be aged, and is a miss too). */
|
|
86
|
+
function servable(entry, ttl) {
|
|
87
|
+
if (entry.build !== version) return false;
|
|
88
|
+
const now = clock.wall();
|
|
42
89
|
return Number.isFinite(entry.created) && now - entry.created < ttl * 1e3;
|
|
43
90
|
}
|
|
44
91
|
/** The two setup mistakes the read path can see, with the line that fixes each. */
|
|
@@ -68,6 +115,26 @@ function runtime_of(ctx) {
|
|
|
68
115
|
* requests (RFC 9110), which is what makes a CDN or browser revalidation of a stored page cost
|
|
69
116
|
* zero body bytes. Validators live on the ENTRY, never in edge adapters: they are the render's
|
|
70
117
|
* identity, not a CDN dialect. */
|
|
118
|
+
/** One entity tag of an If-None-Match list (`[W/]"opaque"`), and the comma or end after it. */
|
|
119
|
+
const ENTITY_TAG_RE = /\s*(?:W\/)?"([^"]*)"\s*(?:,|$)/y;
|
|
120
|
+
/**
|
|
121
|
+
* Does an If-None-Match value match the stored `etag`? RFC 9110 §13.1.2: `*` matches any stored
|
|
122
|
+
* page, and each listed tag is compared WEAKLY (`W/"x"` matches `"x"`: a compressing CDN weakens the
|
|
123
|
+
* etag the browser then revalidates with). A list that does not read as entity tags matches nothing
|
|
124
|
+
* (test/property-conditional.test.ts).
|
|
125
|
+
*/
|
|
126
|
+
function none_match(inm, etag) {
|
|
127
|
+
if (inm.trim() === "*") return true;
|
|
128
|
+
const want = etag.startsWith("W/") ? etag.slice(2) : etag;
|
|
129
|
+
ENTITY_TAG_RE.lastIndex = 0;
|
|
130
|
+
for (;;) {
|
|
131
|
+
while (inm[ENTITY_TAG_RE.lastIndex] === "," || inm[ENTITY_TAG_RE.lastIndex] === " ") ENTITY_TAG_RE.lastIndex++;
|
|
132
|
+
if (ENTITY_TAG_RE.lastIndex >= inm.length) return false;
|
|
133
|
+
const m = ENTITY_TAG_RE.exec(inm);
|
|
134
|
+
if (!m) return false;
|
|
135
|
+
if (`"${m[1]}"` === want) return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
71
138
|
function postrender_response(entry, via, request) {
|
|
72
139
|
if (entry.kind === "redirect") return new Response(null, {
|
|
73
140
|
status: entry.status,
|
|
@@ -81,7 +148,7 @@ function postrender_response(entry, via, request) {
|
|
|
81
148
|
const inm = request.headers.get("if-none-match");
|
|
82
149
|
const ims = request.headers.get("if-modified-since");
|
|
83
150
|
const last_modified = entry.headers["last-modified"];
|
|
84
|
-
if (!!etag && !!inm && inm
|
|
151
|
+
if (!!etag && !!inm && none_match(inm, etag) || !inm && !!ims && !!last_modified && Date.parse(ims) >= Date.parse(last_modified)) {
|
|
85
152
|
const headers = { [VIA_HEADER]: via };
|
|
86
153
|
for (const h of [
|
|
87
154
|
"etag",
|
|
@@ -108,7 +175,7 @@ function postrender_response(entry, via, request) {
|
|
|
108
175
|
* entry written before the field existed) — never a `replace` over a multi-MB page per hit. */
|
|
109
176
|
function served_html(entry, via) {
|
|
110
177
|
if (via === "stored") return entry.html;
|
|
111
|
-
const at = entry.head_end ?? entry.html
|
|
178
|
+
const at = entry.head_end ?? head_end(entry.html);
|
|
112
179
|
return at === -1 ? entry.html : entry.html.slice(0, at) + STORED_DOCUMENT_META + entry.html.slice(at);
|
|
113
180
|
}
|
|
114
181
|
/** Serve-time stitching: fill every `stitch` hole in the stored shell with a fresh SERVER render
|
|
@@ -140,58 +207,113 @@ function stamp_no_store(response) {
|
|
|
140
207
|
response.headers.set("cache-control", "private, no-store");
|
|
141
208
|
} catch {}
|
|
142
209
|
}
|
|
210
|
+
/** A permanent redirect: stored as its status and location, never its body. */
|
|
211
|
+
const permanent_redirect = (response) => response.status === 301 || response.status === 308 ? response.status : null;
|
|
212
|
+
/**
|
|
213
|
+
* THE VERDICT before the body is read: why this response stays per-request, or null when it may be
|
|
214
|
+
* stored. `capture` stores by it and the dev note teaches by it, so the two never disagree
|
|
215
|
+
* (test/dev-note.test.ts). The checks that need the body stay in `capture`.
|
|
216
|
+
*/
|
|
217
|
+
function refusal(response, facts, obs) {
|
|
218
|
+
if (obs.disqualified_by) return obs.disqualified_by;
|
|
219
|
+
if (obs.wrote_cookie) return "a cookie write";
|
|
220
|
+
if (permanent_redirect(response) !== null) return response.headers.get("location") ? null : "a redirect with no location";
|
|
221
|
+
if (response.status !== 200) return `status ${response.status}`;
|
|
222
|
+
if (response.headers.has("set-cookie")) return "a set-cookie response header";
|
|
223
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/html")) return "a non-html response";
|
|
224
|
+
const app_cc = response.headers.get("cache-control") ?? "";
|
|
225
|
+
if (app_cc.includes("private") || app_cc.includes("no-store")) return `the app's cache-control (${app_cc})`;
|
|
226
|
+
if (facts.streamed) return "streamed load promises";
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
143
229
|
/** DEV teaching leg: the verdict runs but never stores/serves. Eligible → a `would-store`
|
|
144
230
|
* header to read in the network panel; refused → ONE console note per path naming the read. */
|
|
145
|
-
const dev_noted_paths =
|
|
231
|
+
const dev_noted_paths = say({ max: 500 });
|
|
146
232
|
function dev_postrender_note(response, ctx, obs) {
|
|
147
|
-
const
|
|
148
|
-
if (
|
|
233
|
+
const why = refusal(response, ctx.facts, obs);
|
|
234
|
+
if (why === null) {
|
|
149
235
|
try {
|
|
150
236
|
response.headers.set(VIA_HEADER, "would-store");
|
|
151
237
|
} catch {}
|
|
152
238
|
return;
|
|
153
239
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
240
|
+
dev_noted_paths.once(ctx.path, () => warn("postrender_stays_per_request", {
|
|
241
|
+
path: ctx.path,
|
|
242
|
+
refusal: why
|
|
243
|
+
}));
|
|
244
|
+
}
|
|
245
|
+
/** A failed capture, said once per page (the store's own failures are said by the registry). */
|
|
246
|
+
const capture_said = say({ max: 256 });
|
|
247
|
+
function capture_failed(path, error) {
|
|
248
|
+
capture_said.once(path, () => warn("postrender_capture_failed", { path }, error));
|
|
157
249
|
}
|
|
250
|
+
/** A refusal made before the body was read: the original Response is still whole. */
|
|
251
|
+
const REFUSED = {
|
|
252
|
+
stored: false,
|
|
253
|
+
response: null
|
|
254
|
+
};
|
|
158
255
|
/**
|
|
159
256
|
* The write-path verdict + store. Returns the stored entry AND a fresh Response (reading the
|
|
160
|
-
* body consumes the original)
|
|
257
|
+
* body consumes the original), or a refusal when the page must stay per-request. Pre-body guards
|
|
161
258
|
* run FIRST so an ineligible response's stream is never consumed.
|
|
162
259
|
*/
|
|
163
|
-
async function capture(response, ctx, runtime, obs, ttl) {
|
|
260
|
+
async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
164
261
|
const { path, facts } = ctx;
|
|
165
|
-
const tags = facts.sources ?? [];
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
262
|
+
const tags = [...facts.sources ?? [], build_tag(version)];
|
|
263
|
+
/** Store `entry`, unless an invalidation started while the store was being written to (the
|
|
264
|
+
* flight went stale; its evict may have run first): then the entry comes back out, and the
|
|
265
|
+
* page stays per-request (no hit is served from it meanwhile, `retracting`). Labeled with this
|
|
266
|
+
* build: it is served to this build only ({@link servable}). */
|
|
267
|
+
const put = (entry) => store_render(runtime, path, {
|
|
268
|
+
...entry,
|
|
269
|
+
build: version
|
|
270
|
+
}, {
|
|
271
|
+
ttl,
|
|
272
|
+
tags
|
|
273
|
+
}, flight);
|
|
274
|
+
if (refusal(response, facts, obs) !== null) return REFUSED;
|
|
275
|
+
const redirect = permanent_redirect(response);
|
|
276
|
+
const location = response.headers.get("location");
|
|
277
|
+
if (redirect !== null && location) {
|
|
170
278
|
const entry = {
|
|
171
279
|
kind: "redirect",
|
|
172
|
-
status:
|
|
280
|
+
status: redirect,
|
|
173
281
|
location,
|
|
174
|
-
created:
|
|
282
|
+
created: clock.wall()
|
|
175
283
|
};
|
|
176
|
-
await
|
|
177
|
-
ttl,
|
|
178
|
-
tags
|
|
179
|
-
});
|
|
284
|
+
if (!await put(entry)) return REFUSED;
|
|
180
285
|
return {
|
|
286
|
+
stored: true,
|
|
181
287
|
entry,
|
|
182
288
|
response
|
|
183
289
|
};
|
|
184
290
|
}
|
|
185
|
-
if (response.status !== 200) return null;
|
|
186
|
-
if (response.headers.has("set-cookie")) return null;
|
|
187
291
|
const content_type = response.headers.get("content-type") ?? "";
|
|
188
|
-
if (!content_type.includes("text/html")) return null;
|
|
189
|
-
const app_cc = response.headers.get("cache-control") ?? "";
|
|
190
|
-
if (app_cc.includes("private") || app_cc.includes("no-store")) return null;
|
|
191
|
-
if (facts.streamed) return null;
|
|
192
292
|
const html = await response.text();
|
|
193
|
-
|
|
194
|
-
const
|
|
293
|
+
/** The page as Kit answered it, again (its body was read above). */
|
|
294
|
+
const answered = () => new Response(html, {
|
|
295
|
+
status: response.status,
|
|
296
|
+
statusText: response.statusText,
|
|
297
|
+
headers: response.headers
|
|
298
|
+
});
|
|
299
|
+
try {
|
|
300
|
+
return await capture_page(html, answered, content_type, put, ctx, runtime, ttl);
|
|
301
|
+
} catch (failed) {
|
|
302
|
+
return {
|
|
303
|
+
stored: false,
|
|
304
|
+
response: answered(),
|
|
305
|
+
failed
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/** The read page's verdict and store (`capture`, past the body read). */
|
|
310
|
+
async function capture_page(html, answered, content_type, put, ctx, runtime, ttl) {
|
|
311
|
+
const { path } = ctx;
|
|
312
|
+
if (KIT_DEFERRED_BOOT_RE.test(html)) return {
|
|
313
|
+
stored: false,
|
|
314
|
+
response: answered()
|
|
315
|
+
};
|
|
316
|
+
const created = clock.wall();
|
|
195
317
|
const modes = stitch_modes(html);
|
|
196
318
|
const has_serve = modes.serve;
|
|
197
319
|
const stored_html = !has_serve && modes.edge ? esi_rewrite(html, path) : html;
|
|
@@ -214,49 +336,59 @@ async function capture(response, ctx, runtime, obs, ttl) {
|
|
|
214
336
|
const entry = {
|
|
215
337
|
kind: "page",
|
|
216
338
|
html: stored_html,
|
|
217
|
-
head_end: stored_html
|
|
339
|
+
head_end: head_end(stored_html),
|
|
218
340
|
headers,
|
|
219
341
|
created,
|
|
220
342
|
...has_serve ? { stitch: true } : {}
|
|
221
343
|
};
|
|
222
|
-
await
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
344
|
+
if (!await put(entry)) return {
|
|
345
|
+
stored: false,
|
|
346
|
+
response: answered()
|
|
347
|
+
};
|
|
226
348
|
return {
|
|
349
|
+
stored: true,
|
|
227
350
|
entry,
|
|
228
351
|
response: postrender_response(entry, "stored")
|
|
229
352
|
};
|
|
230
353
|
}
|
|
231
354
|
/** This request's value for the `postrender` key of Kit's `config` (the route's, merged from its
|
|
232
|
-
* layouts), or the app-wide default when no level sets
|
|
355
|
+
* layouts), or the app-wide default when no level sets one. */
|
|
233
356
|
function opted_in(ctx) {
|
|
234
357
|
const declared = ctx.routeConfig("postrender");
|
|
235
|
-
if (typeof declared === "boolean") return declared;
|
|
236
|
-
if (declared !== void 0) ctx.app.warn(`\`config.postrender\` must be true or
|
|
358
|
+
if (typeof declared === "boolean" || declared === "with-credentials") return declared;
|
|
359
|
+
if (declared !== void 0) ctx.app.warn(`\`config.postrender\` must be true, false or 'with-credentials' (got ${JSON.stringify(declared)}); the app-wide default applies.`);
|
|
237
360
|
return ctx.options.default;
|
|
238
361
|
}
|
|
362
|
+
/** The request carries the visitor's credentials: a cookie or an `authorization` header. Read
|
|
363
|
+
* before the render's reads are watched (they are not the render's). */
|
|
364
|
+
function credentialed(request) {
|
|
365
|
+
return request.headers.has("cookie") || request.headers.has("authorization");
|
|
366
|
+
}
|
|
239
367
|
const server = defineServer({
|
|
240
368
|
setup(app) {
|
|
241
369
|
set_postrender_app(app);
|
|
242
370
|
},
|
|
243
371
|
state: () => ({
|
|
244
372
|
runtime: null,
|
|
245
|
-
|
|
373
|
+
flight: null,
|
|
246
374
|
obs: null,
|
|
247
|
-
eligible: false
|
|
375
|
+
eligible: false,
|
|
376
|
+
credentials: false
|
|
248
377
|
}),
|
|
249
378
|
async page(ctx) {
|
|
250
379
|
const { event, path, facts, state } = ctx;
|
|
251
380
|
const runtime = state.runtime = runtime_of(ctx);
|
|
252
|
-
|
|
381
|
+
const storing = !ctx.forPage && !facts.private && !building && event.request.method.toUpperCase() === "GET" && event.url.search === "" && !path.endsWith("__data.json") && !facts.answerForm && opted_in(ctx);
|
|
382
|
+
if (!storing) return;
|
|
383
|
+
const may_store = storing === "with-credentials" || !credentialed(event.request);
|
|
253
384
|
state.eligible = true;
|
|
254
385
|
if (dev) {
|
|
255
|
-
state.obs = observe_event(
|
|
386
|
+
state.obs = observe_event(ctx);
|
|
256
387
|
return;
|
|
257
388
|
}
|
|
389
|
+
deploy_check(runtime, version).then(void 0, () => {});
|
|
258
390
|
const hit = await store_get(runtime, path);
|
|
259
|
-
if (hit &&
|
|
391
|
+
if (hit && servable(hit, ctx.options.ttl) && !retracting(path)) {
|
|
260
392
|
ctx.app.devtools({
|
|
261
393
|
domain: "server",
|
|
262
394
|
name: "server.postrender",
|
|
@@ -265,9 +397,24 @@ const server = defineServer({
|
|
|
265
397
|
});
|
|
266
398
|
return serve(hit, "hit", ctx);
|
|
267
399
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
400
|
+
if (!may_store) {
|
|
401
|
+
state.credentials = true;
|
|
402
|
+
ctx.app.devtools({
|
|
403
|
+
domain: "server",
|
|
404
|
+
name: "server.postrender",
|
|
405
|
+
op: "skip",
|
|
406
|
+
url: path,
|
|
407
|
+
reason: "credentials"
|
|
408
|
+
});
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
let flight = enter_flight(path);
|
|
412
|
+
while ("joined" in flight) {
|
|
413
|
+
const outcome = await flight.joined;
|
|
414
|
+
if (outcome === STALE) {
|
|
415
|
+
flight = enter_flight(path);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
271
418
|
if (outcome.stored) {
|
|
272
419
|
ctx.app.devtools({
|
|
273
420
|
domain: "server",
|
|
@@ -279,56 +426,85 @@ const server = defineServer({
|
|
|
279
426
|
}
|
|
280
427
|
return;
|
|
281
428
|
}
|
|
282
|
-
state.
|
|
283
|
-
state.obs = observe_event(
|
|
429
|
+
state.flight = flight.opened;
|
|
430
|
+
state.obs = observe_event(ctx);
|
|
284
431
|
facts.storable = true;
|
|
285
432
|
facts.sources ??= [];
|
|
286
433
|
},
|
|
287
434
|
async response(ctx, response) {
|
|
288
435
|
const { event, path, state } = ctx;
|
|
289
436
|
const runtime = state.runtime ?? current_runtime();
|
|
290
|
-
if (event.request.method.toUpperCase() !== "GET" && response.status < 400) self_evict(runtime, path);
|
|
291
|
-
const {
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
-
settle
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
437
|
+
if (event.request.method.toUpperCase() !== "GET" && response.status < 400) await self_evict(runtime, path);
|
|
438
|
+
const { flight, obs } = state;
|
|
439
|
+
if (flight && obs) {
|
|
440
|
+
state.flight = null;
|
|
441
|
+
const settle = (stored) => flight.settle({ stored });
|
|
442
|
+
if (flight.stale) {
|
|
443
|
+
stamp_no_store(response);
|
|
444
|
+
settle(null);
|
|
445
|
+
ctx.app.devtools({
|
|
446
|
+
domain: "server",
|
|
447
|
+
name: "server.postrender",
|
|
448
|
+
op: "skip",
|
|
449
|
+
url: path,
|
|
450
|
+
reason: "invalidated-while-rendering"
|
|
451
|
+
});
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
const captured = await capture(response, ctx, runtime, obs, ctx.options.ttl, flight);
|
|
456
|
+
settle(captured.stored ? captured.entry : null);
|
|
457
|
+
if (!captured.stored && captured.failed !== void 0) {
|
|
458
|
+
capture_failed(path, captured.failed);
|
|
459
|
+
stamp_no_store(captured.response ?? response);
|
|
460
|
+
return captured.response ?? void 0;
|
|
461
|
+
}
|
|
462
|
+
ctx.app.devtools(captured.stored ? {
|
|
463
|
+
domain: "server",
|
|
464
|
+
name: "server.postrender",
|
|
465
|
+
op: "stored",
|
|
466
|
+
url: path,
|
|
467
|
+
bytes: captured.entry.kind === "page" ? captured.entry.html.length : 0
|
|
468
|
+
} : {
|
|
469
|
+
domain: "server",
|
|
470
|
+
name: "server.postrender",
|
|
471
|
+
op: "skip",
|
|
472
|
+
url: path,
|
|
473
|
+
reason: obs.disqualified_by ?? "response-ineligible"
|
|
474
|
+
});
|
|
475
|
+
if (captured.stored) {
|
|
476
|
+
if (captured.entry.kind !== "page" || !captured.entry.stitch) return captured.response;
|
|
477
|
+
try {
|
|
478
|
+
return await serve_stitched(captured.entry, "stored", ctx);
|
|
479
|
+
} catch (failed) {
|
|
480
|
+
capture_failed(path, failed);
|
|
481
|
+
stamp_no_store(captured.response);
|
|
482
|
+
return captured.response;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
stamp_no_store(captured.response ?? response);
|
|
486
|
+
if (captured.response) return captured.response;
|
|
487
|
+
} catch (failed) {
|
|
488
|
+
settle(null);
|
|
489
|
+
capture_failed(path, failed);
|
|
490
|
+
}
|
|
491
|
+
} else if (state.credentials) stamp_no_store(response);
|
|
313
492
|
else if (dev && obs && state.eligible) dev_postrender_note(response, ctx, obs);
|
|
314
493
|
},
|
|
494
|
+
done(ctx) {
|
|
495
|
+
const { state } = ctx;
|
|
496
|
+
if (!state.flight) return;
|
|
497
|
+
state.flight.settle({ stored: null });
|
|
498
|
+
state.flight = null;
|
|
499
|
+
},
|
|
315
500
|
async invalidate(tags) {
|
|
316
|
-
await
|
|
501
|
+
const { failed } = await evict_tags(tags);
|
|
502
|
+
if (failed.length === 1) throw failed[0];
|
|
503
|
+
if (failed.length > 1) throw new AggregateError(failed, `${failed.length} tags failed to evict`);
|
|
317
504
|
},
|
|
318
|
-
async lastBuild(
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
const seen = marker?.kind === "page" ? marker.html : void 0;
|
|
322
|
-
await store.put(BUILD_MARKER_KEY, {
|
|
323
|
-
kind: "page",
|
|
324
|
-
html: version,
|
|
325
|
-
headers: {},
|
|
326
|
-
created: Date.now()
|
|
327
|
-
}, {
|
|
328
|
-
ttl: BUILD_MARKER_TTL_SEC,
|
|
329
|
-
tags: []
|
|
330
|
-
});
|
|
331
|
-
return seen;
|
|
505
|
+
async lastBuild(build) {
|
|
506
|
+
const seen = await deploy_check(current_runtime(), build);
|
|
507
|
+
return seen === STALE ? void 0 : seen;
|
|
332
508
|
}
|
|
333
509
|
});
|
|
334
510
|
export { MISORDERED_HANDLE, MISSING_HANDLE, server as default, postrender_response };
|
package/dist/stitch.d.ts
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* postrender — SERVE-TIME STITCHING (the zero-flash upgrade over `render: 'live'`).
|
|
3
|
-
*
|
|
4
|
-
* A `with { render: 'deferred', stitch: 'serve' }` hole ships in the stored shell as a normal
|
|
5
|
-
* signed hole PLUS a `stitch` attribute. On every serve of that entry the handle renders the
|
|
6
|
-
* hole server-side RIGHT THEN (the visitor's cookies ride the request) and SPLICES the HTML
|
|
7
|
-
* into the stored bytes — one response, personalized first paint, view-source included.
|
|
8
|
-
*
|
|
9
|
-
* Laws: fail-open PER HOLE (a failed/timed-out render keeps the fallback + the client fetch —
|
|
10
|
-
* a slow upstream never blocks the page); the spliced wrapper becomes a neutral
|
|
11
|
-
* `display:contents` div so nested islands inside the stitched HTML are TOP-LEVEL regions that
|
|
12
|
-
* wake themselves (no region ancestor to ride or wait on); stitched entries are per-visitor —
|
|
13
|
-
* the capture stamps them `private, no-store` and mints no validators.
|
|
14
|
-
*
|
|
15
|
-
* Pure string functions (unit-tested); the handle threads its own `#render_capability`.
|
|
16
|
-
*/
|
|
17
|
-
/** Cheap presence probe — the capture uses it to flag entries; serves use it to skip the scan. */
|
|
18
|
-
export declare const STITCH_HOLE_RE: RegExp;
|
|
19
1
|
/** How a stitch hole fills: `'serve'` = the ORIGIN re-renders + splices on every serve (per-visitor
|
|
20
2
|
* page, edge-bypassed) · `'edge'` = the CDN fills an ESI include per request (the shell stays
|
|
21
|
-
* edge-cached; origin renders only the hole).
|
|
3
|
+
* edge-cached; origin renders only the hole). A bare `stitch` attribute (older builds) reads as
|
|
4
|
+
* `'serve'`. */
|
|
22
5
|
export type StitchMode = "serve" | "edge";
|
|
23
6
|
export interface StitchHole {
|
|
24
7
|
/** Absolute span of the WHOLE region block (open tag through matching close). */
|
|
@@ -54,8 +37,11 @@ export declare function esi_rewrite(html: string, page_path?: string): string;
|
|
|
54
37
|
/** Find every TOP-LEVEL stitch-marked region block, balanced across nested regions. */
|
|
55
38
|
export declare function find_stitch_holes(html: string): StitchHole[];
|
|
56
39
|
/**
|
|
57
|
-
* Splice fresh renders into every stitch hole
|
|
58
|
-
*
|
|
59
|
-
*
|
|
40
|
+
* Splice fresh renders into every stitch hole (`stitch: 'serve'`): on each serve of a stored page
|
|
41
|
+
* the handle renders its holes for this visitor and splices them into the stored bytes. `render`
|
|
42
|
+
* resolves an endpoint to HTML or null; null or a throw keeps that hole's block verbatim, so its
|
|
43
|
+
* fallback ships and the client fetches it as an unstitched hole would. Holes render in parallel.
|
|
44
|
+
* A filled hole becomes a `display:contents` div, so the regions inside it are top-level and wake
|
|
45
|
+
* on their own.
|
|
60
46
|
*/
|
|
61
47
|
export declare function stitch_html(html: string, render: (endpoint: string) => Promise<string | null>): Promise<string>;
|