@ogygia/postrender 0.13.0 → 0.15.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/dist/adapters/akamai.d.ts +6 -0
- package/dist/adapters/akamai.js +11 -15
- package/dist/adapters/cloudflare-kv.d.ts +12 -0
- package/dist/adapters/cloudflare-kv.js +12 -0
- package/dist/adapters/cloudflare.d.ts +6 -0
- package/dist/adapters/cloudflare.js +8 -1
- package/dist/adapters/cloudfront.js +5 -4
- package/dist/adapters/upstash.d.ts +7 -0
- package/dist/adapters/upstash.js +11 -4
- package/dist/adapters/valkey.d.ts +7 -0
- package/dist/adapters/valkey.js +9 -4
- package/dist/app.js +8 -3
- package/dist/handle.d.ts +16 -0
- package/dist/handle.js +19 -5
- package/dist/index.d.ts +25 -0
- package/dist/index.js +3 -4
- package/dist/key.d.ts +0 -8
- package/dist/key.js +3 -11
- package/dist/memory-store.js +4 -3
- package/dist/messages.d.ts +6 -0
- package/dist/messages.js +14 -10
- package/dist/observe.d.ts +6 -10
- package/dist/observe.js +10 -59
- package/dist/registry.d.ts +2 -0
- package/dist/registry.js +45 -27
- package/dist/server.d.ts +2 -5
- package/dist/server.js +183 -60
- 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 +9 -6
package/dist/observe.js
CHANGED
|
@@ -1,77 +1,28 @@
|
|
|
1
1
|
// src/observe.ts
|
|
2
|
-
/** Personalization-bearing request headers: reading one during an eligible render disqualifies
|
|
3
|
-
* the page. Everything else (host, sec-fetch-*, accept-encoding, …) is delivery plumbing. */
|
|
4
|
-
const DISQUALIFYING_HEADERS = /* @__PURE__ */ new Set([
|
|
5
|
-
"cookie",
|
|
6
|
-
"authorization",
|
|
7
|
-
"accept-language",
|
|
8
|
-
"user-agent",
|
|
9
|
-
"referer",
|
|
10
|
-
"x-user",
|
|
11
|
-
"x-user-id",
|
|
12
|
-
"x-session",
|
|
13
|
-
"x-session-id"
|
|
14
|
-
]);
|
|
15
|
-
/** Default-valued reads still store: undefined/null/''/false, empty plain object, empty array. */
|
|
16
|
-
function is_default_value(value) {
|
|
17
|
-
if (value === void 0 || value === null || value === "" || value === false) return true;
|
|
18
|
-
if (Array.isArray(value)) return value.length === 0;
|
|
19
|
-
if (typeof value === "object") {
|
|
20
|
-
const proto = Object.getPrototypeOf(value);
|
|
21
|
-
if (proto === Object.prototype || proto === null) return Object.keys(value).length === 0;
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
2
|
/**
|
|
26
|
-
*
|
|
27
|
-
* Returns the live observation the verdict reads after the render.
|
|
28
|
-
* (the event dies with the request) — nothing to restore.
|
|
3
|
+
* Note the event's personal reads from now on, and record its cookie writes (patched in place, per
|
|
4
|
+
* request: the event dies with it). Returns the live observation the verdict reads after the render.
|
|
29
5
|
*/
|
|
30
|
-
function observe_event(
|
|
6
|
+
function observe_event(ctx) {
|
|
7
|
+
const { event, facts } = ctx;
|
|
8
|
+
ctx.app.observeReads(event);
|
|
31
9
|
const obs = {
|
|
32
10
|
get disqualified_by() {
|
|
33
|
-
return facts.perVisitor;
|
|
11
|
+
return facts.reads.perVisitor;
|
|
34
12
|
},
|
|
35
13
|
wrote_cookie: false
|
|
36
14
|
};
|
|
37
|
-
const disqualify = (label) => {
|
|
38
|
-
if (facts.perVisitor === null) facts.perVisitor = label;
|
|
39
|
-
};
|
|
40
15
|
const cookies = event.cookies;
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const orig_set = cookies.set.bind(cookies);
|
|
44
|
-
const orig_delete = cookies.delete.bind(cookies);
|
|
45
|
-
cookies.get = ((name, opts) => {
|
|
46
|
-
const value = orig_get(name, opts);
|
|
47
|
-
if (!is_default_value(value)) disqualify(`cookie:${name}`);
|
|
48
|
-
return value;
|
|
49
|
-
});
|
|
50
|
-
cookies.getAll = ((opts) => {
|
|
51
|
-
const all = orig_get_all(opts);
|
|
52
|
-
if (all.length > 0) disqualify("cookies.getAll");
|
|
53
|
-
return all;
|
|
54
|
-
});
|
|
16
|
+
const set = cookies.set.bind(cookies);
|
|
17
|
+
const drop = cookies.delete.bind(cookies);
|
|
55
18
|
cookies.set = ((...args) => {
|
|
56
19
|
obs.wrote_cookie = true;
|
|
57
|
-
return
|
|
20
|
+
return set(...args);
|
|
58
21
|
});
|
|
59
22
|
cookies.delete = ((...args) => {
|
|
60
23
|
obs.wrote_cookie = true;
|
|
61
|
-
return
|
|
62
|
-
});
|
|
63
|
-
const headers = event.request.headers;
|
|
64
|
-
const orig_header_get = headers.get.bind(headers);
|
|
65
|
-
headers.get = ((name) => {
|
|
66
|
-
const value = orig_header_get(name);
|
|
67
|
-
if (value !== null && DISQUALIFYING_HEADERS.has(name.toLowerCase())) disqualify(`header:${name.toLowerCase()}`);
|
|
68
|
-
return value;
|
|
24
|
+
return drop(...args);
|
|
69
25
|
});
|
|
70
|
-
event.locals = new Proxy(event.locals, { get(target, prop, receiver) {
|
|
71
|
-
const value = Reflect.get(target, prop, receiver);
|
|
72
|
-
if (typeof prop === "string" && !is_default_value(value) && typeof value !== "function") disqualify(`locals.${prop}`);
|
|
73
|
-
return value;
|
|
74
|
-
} });
|
|
75
26
|
return obs;
|
|
76
27
|
}
|
|
77
28
|
export { observe_event };
|
package/dist/registry.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ export declare function evict_tags(tags: readonly string[], runtime?: Runtime):
|
|
|
27
27
|
keys: string[];
|
|
28
28
|
failed: unknown[];
|
|
29
29
|
}>;
|
|
30
|
+
/** `work` against the store: its failure is said, then thrown (for a caller that tries again). */
|
|
31
|
+
export declare function store_call<T>(op: string, key: string, work: () => Promise<T>): Promise<T>;
|
|
30
32
|
export declare function store_get(runtime: Runtime, key: string): Promise<PostrenderEntry | null>;
|
|
31
33
|
export declare function store_put(runtime: Runtime, key: string, entry: PostrenderEntry, options: PostrenderPutOptions): Promise<void>;
|
|
32
34
|
/**
|
package/dist/registry.js
CHANGED
|
@@ -2,61 +2,67 @@ import { MemoryStore } from "./memory-store.js";
|
|
|
2
2
|
import { in_subtree, normalize_prefix } from "./key.js";
|
|
3
3
|
import { postrender_app } from "./app.js";
|
|
4
4
|
import { OgygiaError, warn } from "./messages.js";
|
|
5
|
-
import { flights, machine, outbox, policy } from "@ogygia/machine";
|
|
5
|
+
import { buggify, flights, machine, outbox, policy, registry, say, scope, slot } from "@ogygia/machine";
|
|
6
6
|
import { sourceTag } from "@ogygia/core/plugin/utils";
|
|
7
7
|
// src/registry.ts
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
/** Dev and rules builds (a buggify point); folded out of any other. */
|
|
9
|
+
const RULES = import.meta.env?.DEV || (typeof __OGYGIA_RULES__ !== "undefined" ? __OGYGIA_RULES__ : false);
|
|
10
|
+
const slot$1 = slot({
|
|
11
|
+
name: "ogygia.postrender",
|
|
12
|
+
version: 1,
|
|
13
|
+
make: () => ({
|
|
14
|
+
installed: null,
|
|
15
|
+
handled: /* @__PURE__ */ new WeakMap(),
|
|
16
|
+
fallback: null
|
|
17
|
+
})
|
|
18
|
+
});
|
|
13
19
|
/** A server event into the current request's devtools stream (a no-op unless devtools is on). */
|
|
14
20
|
function record(event) {
|
|
15
21
|
postrender_app()?.devtools(event);
|
|
16
22
|
}
|
|
17
|
-
|
|
23
|
+
/** Said once for the process (a test's reset says it again). */
|
|
24
|
+
const replica_blindspot = say({ max: 1 });
|
|
25
|
+
const BLINDSPOT = "postrender_edges_without_shared_store";
|
|
18
26
|
/** `postrender.handle(options)`'s first half: make these settings this server's runtime. */
|
|
19
27
|
function install(options) {
|
|
20
28
|
const edges = [...options.edge ?? []];
|
|
21
|
-
if (edges.length && !options.store
|
|
22
|
-
warned_replica_blindspot = true;
|
|
23
|
-
warn("postrender_edges_without_shared_store");
|
|
24
|
-
}
|
|
29
|
+
if (edges.length && !options.store) replica_blindspot.once(BLINDSPOT, () => warn("postrender_edges_without_shared_store"));
|
|
25
30
|
const runtime = {
|
|
26
31
|
store: options.store ?? new MemoryStore(),
|
|
27
32
|
edges
|
|
28
33
|
};
|
|
29
|
-
slot.installed = runtime;
|
|
34
|
+
slot$1.installed = runtime;
|
|
30
35
|
return runtime;
|
|
31
36
|
}
|
|
32
37
|
/** The handle's per-request half: this request came through it, with these settings. */
|
|
33
38
|
function mark_handled(event, runtime) {
|
|
34
|
-
slot.handled.set(event.request, runtime);
|
|
39
|
+
slot$1.handled.set(event.request, runtime);
|
|
35
40
|
}
|
|
36
41
|
/** The runtime of the `postrender.handle()` that saw this request first, or null. */
|
|
37
42
|
function handled_runtime(event) {
|
|
38
|
-
return slot.handled.get(event.request) ?? null;
|
|
43
|
+
return slot$1.handled.get(event.request) ?? null;
|
|
39
44
|
}
|
|
40
45
|
/** The latest `postrender.handle()` made in this process, or null when hooks.server.ts has none. */
|
|
41
46
|
function installed_runtime() {
|
|
42
|
-
return slot.installed;
|
|
47
|
+
return slot$1.installed;
|
|
43
48
|
}
|
|
44
49
|
/** This server's runtime: its handle's, else the memory default. */
|
|
45
50
|
function current_runtime() {
|
|
46
|
-
return slot.installed ?? (slot.fallback ??= {
|
|
51
|
+
return slot$1.installed ?? (slot$1.fallback ??= {
|
|
47
52
|
store: new MemoryStore(),
|
|
48
53
|
edges: []
|
|
49
54
|
});
|
|
50
55
|
}
|
|
51
56
|
/** Harness/test escape hatch: no handle made, a fresh memory default. */
|
|
52
57
|
function reset_for_tests() {
|
|
53
|
-
slot.installed = null;
|
|
54
|
-
slot.handled = /* @__PURE__ */ new WeakMap();
|
|
55
|
-
slot.fallback = null;
|
|
56
|
-
|
|
58
|
+
slot$1.installed = null;
|
|
59
|
+
slot$1.handled = /* @__PURE__ */ new WeakMap();
|
|
60
|
+
slot$1.fallback = null;
|
|
61
|
+
replica_blindspot.ok(BLINDSPOT);
|
|
57
62
|
store_health = health_def.create();
|
|
58
63
|
renders.drop(() => true);
|
|
59
|
-
|
|
64
|
+
writes_life.close();
|
|
65
|
+
writes_life = scope();
|
|
60
66
|
}
|
|
61
67
|
function notify_thaw(tags) {
|
|
62
68
|
postrender_app()?.notify.invalidated(tags).catch(() => {});
|
|
@@ -116,15 +122,24 @@ function store_failed(op, key, error) {
|
|
|
116
122
|
function store_answered(mark) {
|
|
117
123
|
if (store_health.now === mark && mark.kind === "failing") store_health.go("ok");
|
|
118
124
|
}
|
|
119
|
-
/** `work` against the store: its failure is said
|
|
120
|
-
async function
|
|
125
|
+
/** `work` against the store: its failure is said, then thrown (for a caller that tries again). */
|
|
126
|
+
async function store_call(op, key, work) {
|
|
121
127
|
const mark = store_health.now;
|
|
122
128
|
try {
|
|
129
|
+
if (RULES && buggify.fires("postrender.store-down")) throw new Error("store down (buggify)");
|
|
123
130
|
const out = await work();
|
|
124
131
|
store_answered(mark);
|
|
125
132
|
return out;
|
|
126
133
|
} catch (error) {
|
|
127
134
|
store_failed(op, key, error);
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** `work` against the store: its failure is said and answered `fallback`. */
|
|
139
|
+
async function guarded(op, key, work, fallback) {
|
|
140
|
+
try {
|
|
141
|
+
return await store_call(op, key, work);
|
|
142
|
+
} catch {
|
|
128
143
|
return fallback;
|
|
129
144
|
}
|
|
130
145
|
}
|
|
@@ -134,7 +149,10 @@ function store_get(runtime, key) {
|
|
|
134
149
|
function store_put(runtime, key, entry, options) {
|
|
135
150
|
return guarded("put", key, () => runtime.store.put(key, entry, options), void 0);
|
|
136
151
|
}
|
|
137
|
-
|
|
152
|
+
/** The writes out now, each held from its start until it settles (and all of them until a test
|
|
153
|
+
* harness reset ends `writes_life`). */
|
|
154
|
+
const writes = registry();
|
|
155
|
+
let writes_life = scope();
|
|
138
156
|
/**
|
|
139
157
|
* Store a render's page. An invalidation that starts while the write is out ends the render's
|
|
140
158
|
* flight (it may have read what the invalidation changes), and its evict may run before the write
|
|
@@ -150,7 +168,7 @@ async function store_render(runtime, key, entry, options, flight) {
|
|
|
150
168
|
key,
|
|
151
169
|
flight
|
|
152
170
|
};
|
|
153
|
-
writes.add(write);
|
|
171
|
+
const settled = writes.add(write, writes_life);
|
|
154
172
|
try {
|
|
155
173
|
await store_put(runtime, key, entry, options);
|
|
156
174
|
if (!flight.stale) return true;
|
|
@@ -161,7 +179,7 @@ async function store_render(runtime, key, entry, options, flight) {
|
|
|
161
179
|
})) store_answered(mark);
|
|
162
180
|
return false;
|
|
163
181
|
} finally {
|
|
164
|
-
|
|
182
|
+
settled();
|
|
165
183
|
}
|
|
166
184
|
}
|
|
167
185
|
/** A stale render's page is being taken back out of the store: a hit for it is a miss. */
|
|
@@ -327,4 +345,4 @@ async function self_evict(runtime, pathname) {
|
|
|
327
345
|
key: pathname
|
|
328
346
|
})) store_answered(mark);
|
|
329
347
|
}
|
|
330
|
-
export { Flight, current_runtime, enter_flight, evict_tags, handled_runtime, install, installed_runtime, invalidate, invalidateWhere, invalidate_tags, mark_handled, reset_for_tests, retracting, self_evict, store_get, store_put, store_render };
|
|
348
|
+
export { Flight, current_runtime, enter_flight, evict_tags, handled_runtime, install, installed_runtime, invalidate, invalidateWhere, invalidate_tags, mark_handled, reset_for_tests, retracting, self_evict, store_call, store_get, store_put, store_render };
|
package/dist/server.d.ts
CHANGED
|
@@ -20,14 +20,11 @@ interface State {
|
|
|
20
20
|
obs: Observation | null;
|
|
21
21
|
/** the request was eligible to be stored */
|
|
22
22
|
eligible: boolean;
|
|
23
|
+
/** its render is not stored: the request carried the visitor's credentials */
|
|
24
|
+
credentials: boolean;
|
|
23
25
|
}
|
|
24
26
|
export declare const MISSING_HANDLE: string;
|
|
25
27
|
export declare const MISORDERED_HANDLE: string;
|
|
26
|
-
/** Rebuild a Response from a stored page. `via` rides a debug header the harness asserts on.
|
|
27
|
-
* With the incoming `request`, a matching validator answers 304 — plain-HTTP conditional
|
|
28
|
-
* requests (RFC 9110), which is what makes a CDN or browser revalidation of a stored page cost
|
|
29
|
-
* zero body bytes. Validators live on the ENTRY, never in edge adapters: they are the render's
|
|
30
|
-
* identity, not a CDN dialect. */
|
|
31
28
|
export declare function postrender_response(entry: PostrenderEntry, via: "hit" | "join" | "stored", request?: Request): Response;
|
|
32
29
|
declare const server: ServerPlugin<PostrenderServerOptions, State>;
|
|
33
30
|
export { server as default };
|
package/dist/server.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { current_runtime, enter_flight, evict_tags, handled_runtime, installed_runtime, retracting, self_evict, store_get, store_render } from "./registry.js";
|
|
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
5
|
import { warn } from "./messages.js";
|
|
6
|
-
import { STALE } from "@ogygia/machine";
|
|
6
|
+
import { STALE, clock, flights, say } from "@ogygia/machine";
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
|
-
import { building, dev } from "$app/env";
|
|
8
|
+
import { building, dev, version } from "$app/env";
|
|
9
9
|
import { STORED_DOCUMENT_META, defineServer } from "@ogygia/core/plugin";
|
|
10
|
+
import { head_end } from "@ogygia/core/internal/server";
|
|
10
11
|
// src/server.ts
|
|
11
12
|
/**
|
|
12
13
|
* The postrender plugin's server half (imported by the build through
|
|
13
|
-
* `
|
|
14
|
+
* `ogygia:server/server-plugins`, with the `postrender({ ttl, default })` options).
|
|
14
15
|
*
|
|
15
16
|
* `page`: the read path. A GET, no query string, not prerendering, on a route that opts in: a stored
|
|
16
17
|
* copy answers — Kit, loads and Svelte never run. A concurrent cold miss JOINS the in-flight render
|
|
@@ -37,10 +38,54 @@ const BUILD_MARKER_KEY = "\0og:build";
|
|
|
37
38
|
* store saw), not a page, so the pages' TTL backstop must not apply to it: a deploy a month after
|
|
38
39
|
* the last one still has to find it. */
|
|
39
40
|
const BUILD_MARKER_TTL_SEC = 31536e4;
|
|
40
|
-
/** The
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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();
|
|
44
89
|
return Number.isFinite(entry.created) && now - entry.created < ttl * 1e3;
|
|
45
90
|
}
|
|
46
91
|
/** The two setup mistakes the read path can see, with the line that fixes each. */
|
|
@@ -70,6 +115,26 @@ function runtime_of(ctx) {
|
|
|
70
115
|
* requests (RFC 9110), which is what makes a CDN or browser revalidation of a stored page cost
|
|
71
116
|
* zero body bytes. Validators live on the ENTRY, never in edge adapters: they are the render's
|
|
72
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
|
+
}
|
|
73
138
|
function postrender_response(entry, via, request) {
|
|
74
139
|
if (entry.kind === "redirect") return new Response(null, {
|
|
75
140
|
status: entry.status,
|
|
@@ -83,7 +148,7 @@ function postrender_response(entry, via, request) {
|
|
|
83
148
|
const inm = request.headers.get("if-none-match");
|
|
84
149
|
const ims = request.headers.get("if-modified-since");
|
|
85
150
|
const last_modified = entry.headers["last-modified"];
|
|
86
|
-
if (!!etag && !!inm && inm
|
|
151
|
+
if (!!etag && !!inm && none_match(inm, etag) || !inm && !!ims && !!last_modified && Date.parse(ims) >= Date.parse(last_modified)) {
|
|
87
152
|
const headers = { [VIA_HEADER]: via };
|
|
88
153
|
for (const h of [
|
|
89
154
|
"etag",
|
|
@@ -110,7 +175,7 @@ function postrender_response(entry, via, request) {
|
|
|
110
175
|
* entry written before the field existed) — never a `replace` over a multi-MB page per hit. */
|
|
111
176
|
function served_html(entry, via) {
|
|
112
177
|
if (via === "stored") return entry.html;
|
|
113
|
-
const at = entry.head_end ?? entry.html
|
|
178
|
+
const at = entry.head_end ?? head_end(entry.html);
|
|
114
179
|
return at === -1 ? entry.html : entry.html.slice(0, at) + STORED_DOCUMENT_META + entry.html.slice(at);
|
|
115
180
|
}
|
|
116
181
|
/** Serve-time stitching: fill every `stitch` hole in the stored shell with a fresh SERVER render
|
|
@@ -142,23 +207,45 @@ function stamp_no_store(response) {
|
|
|
142
207
|
response.headers.set("cache-control", "private, no-store");
|
|
143
208
|
} catch {}
|
|
144
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
|
+
}
|
|
145
229
|
/** DEV teaching leg: the verdict runs but never stores/serves. Eligible → a `would-store`
|
|
146
230
|
* header to read in the network panel; refused → ONE console note per path naming the read. */
|
|
147
|
-
const dev_noted_paths =
|
|
231
|
+
const dev_noted_paths = say({ max: 500 });
|
|
148
232
|
function dev_postrender_note(response, ctx, obs) {
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
233
|
+
const why = refusal(response, ctx.facts, obs);
|
|
234
|
+
if (why === null) {
|
|
151
235
|
try {
|
|
152
236
|
response.headers.set(VIA_HEADER, "would-store");
|
|
153
237
|
} catch {}
|
|
154
238
|
return;
|
|
155
239
|
}
|
|
156
|
-
|
|
157
|
-
dev_noted_paths.add(ctx.path);
|
|
158
|
-
warn("postrender_stays_per_request", {
|
|
240
|
+
dev_noted_paths.once(ctx.path, () => warn("postrender_stays_per_request", {
|
|
159
241
|
path: ctx.path,
|
|
160
|
-
refusal
|
|
161
|
-
});
|
|
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));
|
|
162
249
|
}
|
|
163
250
|
/** A refusal made before the body was read: the original Response is still whole. */
|
|
164
251
|
const REFUSED = {
|
|
@@ -172,23 +259,27 @@ const REFUSED = {
|
|
|
172
259
|
*/
|
|
173
260
|
async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
174
261
|
const { path, facts } = ctx;
|
|
175
|
-
const tags = facts.sources ?? [];
|
|
262
|
+
const tags = [...facts.sources ?? [], build_tag(version)];
|
|
176
263
|
/** Store `entry`, unless an invalidation started while the store was being written to (the
|
|
177
264
|
* flight went stale; its evict may have run first): then the entry comes back out, and the
|
|
178
|
-
* page stays per-request (no hit is served from it meanwhile, `retracting`).
|
|
179
|
-
|
|
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
|
+
}, {
|
|
180
271
|
ttl,
|
|
181
272
|
tags
|
|
182
273
|
}, flight);
|
|
183
|
-
if (
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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) {
|
|
187
278
|
const entry = {
|
|
188
279
|
kind: "redirect",
|
|
189
|
-
status:
|
|
280
|
+
status: redirect,
|
|
190
281
|
location,
|
|
191
|
-
created:
|
|
282
|
+
created: clock.wall()
|
|
192
283
|
};
|
|
193
284
|
if (!await put(entry)) return REFUSED;
|
|
194
285
|
return {
|
|
@@ -197,13 +288,7 @@ async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
|
197
288
|
response
|
|
198
289
|
};
|
|
199
290
|
}
|
|
200
|
-
if (response.status !== 200) return REFUSED;
|
|
201
|
-
if (response.headers.has("set-cookie")) return REFUSED;
|
|
202
291
|
const content_type = response.headers.get("content-type") ?? "";
|
|
203
|
-
if (!content_type.includes("text/html")) return REFUSED;
|
|
204
|
-
const app_cc = response.headers.get("cache-control") ?? "";
|
|
205
|
-
if (app_cc.includes("private") || app_cc.includes("no-store")) return REFUSED;
|
|
206
|
-
if (facts.streamed) return REFUSED;
|
|
207
292
|
const html = await response.text();
|
|
208
293
|
/** The page as Kit answered it, again (its body was read above). */
|
|
209
294
|
const answered = () => new Response(html, {
|
|
@@ -211,11 +296,24 @@ async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
|
211
296
|
statusText: response.statusText,
|
|
212
297
|
headers: response.headers
|
|
213
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;
|
|
214
312
|
if (KIT_DEFERRED_BOOT_RE.test(html)) return {
|
|
215
313
|
stored: false,
|
|
216
314
|
response: answered()
|
|
217
315
|
};
|
|
218
|
-
const created =
|
|
316
|
+
const created = clock.wall();
|
|
219
317
|
const modes = stitch_modes(html);
|
|
220
318
|
const has_serve = modes.serve;
|
|
221
319
|
const stored_html = !has_serve && modes.edge ? esi_rewrite(html, path) : html;
|
|
@@ -238,7 +336,7 @@ async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
|
238
336
|
const entry = {
|
|
239
337
|
kind: "page",
|
|
240
338
|
html: stored_html,
|
|
241
|
-
head_end: stored_html
|
|
339
|
+
head_end: head_end(stored_html),
|
|
242
340
|
headers,
|
|
243
341
|
created,
|
|
244
342
|
...has_serve ? { stitch: true } : {}
|
|
@@ -254,13 +352,18 @@ async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
|
254
352
|
};
|
|
255
353
|
}
|
|
256
354
|
/** This request's value for the `postrender` key of Kit's `config` (the route's, merged from its
|
|
257
|
-
* layouts), or the app-wide default when no level sets
|
|
355
|
+
* layouts), or the app-wide default when no level sets one. */
|
|
258
356
|
function opted_in(ctx) {
|
|
259
357
|
const declared = ctx.routeConfig("postrender");
|
|
260
|
-
if (typeof declared === "boolean") return declared;
|
|
261
|
-
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.`);
|
|
262
360
|
return ctx.options.default;
|
|
263
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
|
+
}
|
|
264
367
|
const server = defineServer({
|
|
265
368
|
setup(app) {
|
|
266
369
|
set_postrender_app(app);
|
|
@@ -269,19 +372,23 @@ const server = defineServer({
|
|
|
269
372
|
runtime: null,
|
|
270
373
|
flight: null,
|
|
271
374
|
obs: null,
|
|
272
|
-
eligible: false
|
|
375
|
+
eligible: false,
|
|
376
|
+
credentials: false
|
|
273
377
|
}),
|
|
274
378
|
async page(ctx) {
|
|
275
379
|
const { event, path, facts, state } = ctx;
|
|
276
380
|
const runtime = state.runtime = runtime_of(ctx);
|
|
277
|
-
|
|
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);
|
|
278
384
|
state.eligible = true;
|
|
279
385
|
if (dev) {
|
|
280
|
-
state.obs = observe_event(
|
|
386
|
+
state.obs = observe_event(ctx);
|
|
281
387
|
return;
|
|
282
388
|
}
|
|
389
|
+
deploy_check(runtime, version).then(void 0, () => {});
|
|
283
390
|
const hit = await store_get(runtime, path);
|
|
284
|
-
if (hit &&
|
|
391
|
+
if (hit && servable(hit, ctx.options.ttl) && !retracting(path)) {
|
|
285
392
|
ctx.app.devtools({
|
|
286
393
|
domain: "server",
|
|
287
394
|
name: "server.postrender",
|
|
@@ -290,6 +397,17 @@ const server = defineServer({
|
|
|
290
397
|
});
|
|
291
398
|
return serve(hit, "hit", ctx);
|
|
292
399
|
}
|
|
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
|
+
}
|
|
293
411
|
let flight = enter_flight(path);
|
|
294
412
|
while ("joined" in flight) {
|
|
295
413
|
const outcome = await flight.joined;
|
|
@@ -309,7 +427,7 @@ const server = defineServer({
|
|
|
309
427
|
return;
|
|
310
428
|
}
|
|
311
429
|
state.flight = flight.opened;
|
|
312
|
-
state.obs = observe_event(
|
|
430
|
+
state.obs = observe_event(ctx);
|
|
313
431
|
facts.storable = true;
|
|
314
432
|
facts.sources ??= [];
|
|
315
433
|
},
|
|
@@ -336,6 +454,11 @@ const server = defineServer({
|
|
|
336
454
|
try {
|
|
337
455
|
const captured = await capture(response, ctx, runtime, obs, ctx.options.ttl, flight);
|
|
338
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
|
+
}
|
|
339
462
|
ctx.app.devtools(captured.stored ? {
|
|
340
463
|
domain: "server",
|
|
341
464
|
name: "server.postrender",
|
|
@@ -349,13 +472,24 @@ const server = defineServer({
|
|
|
349
472
|
url: path,
|
|
350
473
|
reason: obs.disqualified_by ?? "response-ineligible"
|
|
351
474
|
});
|
|
352
|
-
if (captured.stored)
|
|
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
|
+
}
|
|
353
485
|
stamp_no_store(captured.response ?? response);
|
|
354
486
|
if (captured.response) return captured.response;
|
|
355
|
-
} catch {
|
|
487
|
+
} catch (failed) {
|
|
356
488
|
settle(null);
|
|
489
|
+
capture_failed(path, failed);
|
|
357
490
|
}
|
|
358
|
-
} else if (
|
|
491
|
+
} else if (state.credentials) stamp_no_store(response);
|
|
492
|
+
else if (dev && obs && state.eligible) dev_postrender_note(response, ctx, obs);
|
|
359
493
|
},
|
|
360
494
|
done(ctx) {
|
|
361
495
|
const { state } = ctx;
|
|
@@ -368,20 +502,9 @@ const server = defineServer({
|
|
|
368
502
|
if (failed.length === 1) throw failed[0];
|
|
369
503
|
if (failed.length > 1) throw new AggregateError(failed, `${failed.length} tags failed to evict`);
|
|
370
504
|
},
|
|
371
|
-
async lastBuild(
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
const seen = marker?.kind === "page" ? marker.html : void 0;
|
|
375
|
-
await store.put(BUILD_MARKER_KEY, {
|
|
376
|
-
kind: "page",
|
|
377
|
-
html: version,
|
|
378
|
-
headers: {},
|
|
379
|
-
created: Date.now()
|
|
380
|
-
}, {
|
|
381
|
-
ttl: BUILD_MARKER_TTL_SEC,
|
|
382
|
-
tags: []
|
|
383
|
-
});
|
|
384
|
-
return seen;
|
|
505
|
+
async lastBuild(build) {
|
|
506
|
+
const seen = await deploy_check(current_runtime(), build);
|
|
507
|
+
return seen === STALE ? void 0 : seen;
|
|
385
508
|
}
|
|
386
509
|
});
|
|
387
510
|
export { MISORDERED_HANDLE, MISSING_HANDLE, server as default, postrender_response };
|