@ogygia/postrender 0.12.0 → 0.13.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 +11 -2
- package/dist/adapters/akamai.js +36 -26
- package/dist/adapters/cloudflare-kv.d.ts +12 -2
- package/dist/adapters/cloudflare-kv.js +47 -42
- package/dist/adapters/cloudflare.d.ts +11 -2
- package/dist/adapters/cloudflare.js +38 -27
- package/dist/adapters/cloudfront.d.ts +11 -2
- package/dist/adapters/cloudfront.js +30 -21
- package/dist/adapters/upstash.d.ts +12 -2
- package/dist/adapters/upstash.js +76 -68
- package/dist/adapters/valkey.d.ts +12 -2
- package/dist/adapters/valkey.js +65 -51
- package/dist/handle.js +2 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +10 -10
- package/dist/key.d.ts +3 -0
- package/dist/key.js +6 -1
- package/dist/memory-store.d.ts +13 -2
- package/dist/memory-store.js +39 -38
- package/dist/messages.d.ts +72 -0
- package/dist/messages.js +146 -0
- package/dist/registry.d.ts +52 -5
- package/dist/registry.js +190 -37
- package/dist/server.d.ts +4 -3
- package/dist/server.js +104 -51
- package/package.json +13 -5
package/dist/registry.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { normalize_prefix } from "./key.js";
|
|
1
|
+
import { MemoryStore } from "./memory-store.js";
|
|
2
|
+
import { in_subtree, normalize_prefix } from "./key.js";
|
|
3
3
|
import { postrender_app } from "./app.js";
|
|
4
|
+
import { OgygiaError, warn } from "./messages.js";
|
|
5
|
+
import { flights, machine, outbox, policy } from "@ogygia/machine";
|
|
4
6
|
import { sourceTag } from "@ogygia/core/plugin/utils";
|
|
5
7
|
// src/registry.ts
|
|
6
8
|
const slot = globalThis[Symbol.for("ogygia.postrender.v1")] ??= {
|
|
@@ -18,10 +20,10 @@ function install(options) {
|
|
|
18
20
|
const edges = [...options.edge ?? []];
|
|
19
21
|
if (edges.length && !options.store && !warned_replica_blindspot) {
|
|
20
22
|
warned_replica_blindspot = true;
|
|
21
|
-
|
|
23
|
+
warn("postrender_edges_without_shared_store");
|
|
22
24
|
}
|
|
23
25
|
const runtime = {
|
|
24
|
-
store: options.store ??
|
|
26
|
+
store: options.store ?? new MemoryStore(),
|
|
25
27
|
edges
|
|
26
28
|
};
|
|
27
29
|
slot.installed = runtime;
|
|
@@ -42,7 +44,7 @@ function installed_runtime() {
|
|
|
42
44
|
/** This server's runtime: its handle's, else the memory default. */
|
|
43
45
|
function current_runtime() {
|
|
44
46
|
return slot.installed ?? (slot.fallback ??= {
|
|
45
|
-
store:
|
|
47
|
+
store: new MemoryStore(),
|
|
46
48
|
edges: []
|
|
47
49
|
});
|
|
48
50
|
}
|
|
@@ -52,56 +54,178 @@ function reset_for_tests() {
|
|
|
52
54
|
slot.handled = /* @__PURE__ */ new WeakMap();
|
|
53
55
|
slot.fallback = null;
|
|
54
56
|
warned_replica_blindspot = false;
|
|
57
|
+
store_health = health_def.create();
|
|
58
|
+
renders.drop(() => true);
|
|
59
|
+
writes.clear();
|
|
55
60
|
}
|
|
56
61
|
function notify_thaw(tags) {
|
|
57
|
-
postrender_app()?.notify.invalidated(tags);
|
|
62
|
+
postrender_app()?.notify.invalidated(tags).catch(() => {});
|
|
58
63
|
}
|
|
59
64
|
/** Evict every stored page carrying ANY of `tags` (origin + every edge). The receiving half of a
|
|
60
65
|
* thaw notice; also what `invalidate(fn, args)` does for one receipt tag. */
|
|
61
66
|
async function invalidate_tags(tags, runtime = current_runtime()) {
|
|
67
|
+
return (await evict_tags(tags, runtime)).keys;
|
|
68
|
+
}
|
|
69
|
+
/** `invalidate_tags`, with what the store failed to evict: the shared hook (a peer's thaw) must not
|
|
70
|
+
* answer "done" for a tag that is still stored (#287). */
|
|
71
|
+
async function evict_tags(tags, runtime = current_runtime()) {
|
|
72
|
+
stale_flights("all");
|
|
62
73
|
const { store, edges } = runtime;
|
|
63
74
|
const evict_by_tag = store.evictByTag?.bind(store);
|
|
64
75
|
if (!evict_by_tag) {
|
|
65
|
-
|
|
66
|
-
return
|
|
76
|
+
warn("postrender_store_no_evict_by_tag");
|
|
77
|
+
return {
|
|
78
|
+
keys: [],
|
|
79
|
+
failed: []
|
|
80
|
+
};
|
|
67
81
|
}
|
|
68
82
|
const keys = /* @__PURE__ */ new Set();
|
|
83
|
+
const failed = [];
|
|
69
84
|
for (const tag of tags) try {
|
|
70
85
|
for (const key of await evict_by_tag(tag)) keys.add(key);
|
|
71
86
|
} catch (e) {
|
|
72
|
-
|
|
87
|
+
warn("postrender_tag_evict_failed", { tag }, e);
|
|
88
|
+
failed.push(e);
|
|
73
89
|
}
|
|
74
90
|
const jobs = [];
|
|
75
91
|
for (const key of keys) for (const edge of edges) jobs.push(edge_job(edge, (e) => e.purgeUrl(key)));
|
|
76
92
|
await settle_all("invalidate(tags)", jobs);
|
|
77
|
-
return
|
|
93
|
+
return {
|
|
94
|
+
keys: [...keys],
|
|
95
|
+
failed
|
|
96
|
+
};
|
|
78
97
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
const health_def = machine()({
|
|
99
|
+
name: "postrender store",
|
|
100
|
+
initial: "ok",
|
|
101
|
+
steps: {
|
|
102
|
+
ok: ["failing"],
|
|
103
|
+
failing: ["failing", "ok"]
|
|
84
104
|
}
|
|
105
|
+
});
|
|
106
|
+
let store_health = health_def.create();
|
|
107
|
+
function store_failed(op, key, error) {
|
|
108
|
+
const was = store_health.now.kind;
|
|
109
|
+
store_health.go("failing");
|
|
110
|
+
if (was === "ok") warn("postrender_store_failed", {
|
|
111
|
+
op,
|
|
112
|
+
key
|
|
113
|
+
}, error);
|
|
85
114
|
}
|
|
86
|
-
|
|
115
|
+
/** A store call that started in `mark` answered: the streak ends, unless a call failed since. */
|
|
116
|
+
function store_answered(mark) {
|
|
117
|
+
if (store_health.now === mark && mark.kind === "failing") store_health.go("ok");
|
|
118
|
+
}
|
|
119
|
+
/** `work` against the store: its failure is said and answered `fallback`. */
|
|
120
|
+
async function guarded(op, key, work, fallback) {
|
|
121
|
+
const mark = store_health.now;
|
|
87
122
|
try {
|
|
88
|
-
await
|
|
89
|
-
|
|
123
|
+
const out = await work();
|
|
124
|
+
store_answered(mark);
|
|
125
|
+
return out;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
store_failed(op, key, error);
|
|
128
|
+
return fallback;
|
|
129
|
+
}
|
|
90
130
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return inflight.get(key) ?? null;
|
|
131
|
+
function store_get(runtime, key) {
|
|
132
|
+
return guarded("get", key, () => runtime.store.get(key), null);
|
|
94
133
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
134
|
+
function store_put(runtime, key, entry, options) {
|
|
135
|
+
return guarded("put", key, () => runtime.store.put(key, entry, options), void 0);
|
|
136
|
+
}
|
|
137
|
+
const writes = /* @__PURE__ */ new Set();
|
|
138
|
+
/**
|
|
139
|
+
* Store a render's page. An invalidation that starts while the write is out ends the render's
|
|
140
|
+
* flight (it may have read what the invalidation changes), and its evict may run before the write
|
|
141
|
+
* lands: the page is then taken back out, through the same tried-again evict a post waits for.
|
|
142
|
+
* True when it stays stored.
|
|
143
|
+
*
|
|
144
|
+
* From the moment its flight goes stale until that evict ends, the page is RETRACTING: what the
|
|
145
|
+
* store holds for it may be the page from before the invalidation, so no hit is served from it
|
|
146
|
+
* (#332).
|
|
147
|
+
*/
|
|
148
|
+
async function store_render(runtime, key, entry, options, flight) {
|
|
149
|
+
const write = {
|
|
150
|
+
key,
|
|
151
|
+
flight
|
|
104
152
|
};
|
|
153
|
+
writes.add(write);
|
|
154
|
+
try {
|
|
155
|
+
await store_put(runtime, key, entry, options);
|
|
156
|
+
if (!flight.stale) return true;
|
|
157
|
+
const mark = store_health.now;
|
|
158
|
+
if (await self_evicts.put({
|
|
159
|
+
runtime,
|
|
160
|
+
key
|
|
161
|
+
})) store_answered(mark);
|
|
162
|
+
return false;
|
|
163
|
+
} finally {
|
|
164
|
+
writes.delete(write);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** A stale render's page is being taken back out of the store: a hit for it is a miss. */
|
|
168
|
+
function retracting(key) {
|
|
169
|
+
for (const w of writes) if (w.key === key && w.flight.stale) return true;
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* One page's render, opened by the request that renders it: that request settles it exactly once,
|
|
174
|
+
* however it ends (the plugin's `done` settles one nothing else did), with what it stored, or null.
|
|
175
|
+
* An invalidation that would have evicted its page ends it while it renders (`stale`: what it read
|
|
176
|
+
* may be the page from before the publish): it is not stored, and the requests that joined it are
|
|
177
|
+
* let go at once to ask again (#194).
|
|
178
|
+
*/
|
|
179
|
+
var Flight = class {
|
|
180
|
+
key;
|
|
181
|
+
outcome;
|
|
182
|
+
#settle;
|
|
183
|
+
#settled = false;
|
|
184
|
+
/** the flight's own signal: it aborts when an invalidation ends the flight */
|
|
185
|
+
#signal = null;
|
|
186
|
+
constructor(key) {
|
|
187
|
+
this.key = key;
|
|
188
|
+
this.outcome = new Promise((settle) => this.#settle = settle);
|
|
189
|
+
}
|
|
190
|
+
/** An invalidation ended it while it rendered. */
|
|
191
|
+
get stale() {
|
|
192
|
+
return !this.#settled && this.#signal?.aborted === true;
|
|
193
|
+
}
|
|
194
|
+
/** Settle it (once: a second settle does nothing). */
|
|
195
|
+
settle(outcome) {
|
|
196
|
+
if (this.#settled) return;
|
|
197
|
+
this.#settled = true;
|
|
198
|
+
this.#settle(outcome);
|
|
199
|
+
}
|
|
200
|
+
/** The flight's work: its outcome, once its request settles it. */
|
|
201
|
+
_fly(signal) {
|
|
202
|
+
this.#signal = signal;
|
|
203
|
+
return this.outcome;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
/** The renders in progress, one per page: nothing is kept (the store keeps what they stored). The
|
|
207
|
+
* request that opens one holds it until it settles it. */
|
|
208
|
+
const renders = flights.keyed()({
|
|
209
|
+
name: "postrender flight",
|
|
210
|
+
key: ["key"],
|
|
211
|
+
ignore: { _fly: "the request that opens a page render runs it: the others join it" },
|
|
212
|
+
keep: 0,
|
|
213
|
+
run: (flight, signal) => flight._fly(signal)
|
|
214
|
+
});
|
|
215
|
+
/** The render of `key` in progress: join it (its outcome, `STALE` when an invalidation ended it),
|
|
216
|
+
* or, with none out, open it (`opened`: this request renders the page, and settles it however it
|
|
217
|
+
* ends). */
|
|
218
|
+
function enter_flight(key) {
|
|
219
|
+
const flight = new Flight(key);
|
|
220
|
+
const opens = renders.peek({ key }) === void 0;
|
|
221
|
+
const outcome = renders.get(flight);
|
|
222
|
+
return opens ? { opened: flight } : { joined: outcome };
|
|
223
|
+
}
|
|
224
|
+
/** An invalidation is starting: a render still out for a page it evicts may have read what it
|
|
225
|
+
* changes. It ends the renders whose page it would have evicted, had it been stored; the others go
|
|
226
|
+
* on and store. */
|
|
227
|
+
function stale_flights(evicts) {
|
|
228
|
+
renders.drop(({ key }) => evicts === "all" || ("url" in evicts ? key === evicts.url : in_subtree(key, evicts.prefix)));
|
|
105
229
|
}
|
|
106
230
|
/** A call as a promise, a synchronous throw included. */
|
|
107
231
|
const attempt = (call) => Promise.resolve().then(call);
|
|
@@ -115,7 +239,10 @@ const edge_job = (edge, call) => ({
|
|
|
115
239
|
});
|
|
116
240
|
async function settle_all(label, jobs) {
|
|
117
241
|
(await Promise.allSettled(jobs.map((job) => job.run))).forEach((r, i) => {
|
|
118
|
-
if (r.status === "rejected")
|
|
242
|
+
if (r.status === "rejected") warn("postrender_fanout_failed", {
|
|
243
|
+
label,
|
|
244
|
+
what: jobs[i].what
|
|
245
|
+
}, r.reason);
|
|
119
246
|
});
|
|
120
247
|
}
|
|
121
248
|
/**
|
|
@@ -128,6 +255,7 @@ async function settle_all(label, jobs) {
|
|
|
128
255
|
*/
|
|
129
256
|
async function invalidate(target, args) {
|
|
130
257
|
if (typeof target === "string") {
|
|
258
|
+
stale_flights({ url: target });
|
|
131
259
|
record({
|
|
132
260
|
domain: "server",
|
|
133
261
|
name: "server.postrender",
|
|
@@ -142,7 +270,7 @@ async function invalidate(target, args) {
|
|
|
142
270
|
return;
|
|
143
271
|
}
|
|
144
272
|
const tag = sourceTag(target, args ?? []);
|
|
145
|
-
if (!tag) throw new
|
|
273
|
+
if (!tag) throw new OgygiaError("postrender_invalidate_not_source");
|
|
146
274
|
await invalidate_tags([tag]);
|
|
147
275
|
notify_thaw([tag]);
|
|
148
276
|
}
|
|
@@ -151,6 +279,7 @@ async function invalidate(target, args) {
|
|
|
151
279
|
* Cloudflare: prefix purge). */
|
|
152
280
|
async function invalidateWhere(filter) {
|
|
153
281
|
const prefix = normalize_prefix(filter.prefix);
|
|
282
|
+
stale_flights({ prefix });
|
|
154
283
|
record({
|
|
155
284
|
domain: "server",
|
|
156
285
|
name: "server.postrender",
|
|
@@ -163,15 +292,39 @@ async function invalidateWhere(filter) {
|
|
|
163
292
|
await settle_all("invalidateWhere", jobs);
|
|
164
293
|
notify_thaw("all");
|
|
165
294
|
}
|
|
166
|
-
/**
|
|
167
|
-
*
|
|
168
|
-
|
|
295
|
+
/** How the origin store is tried again for an evict a response waits for: three quick tries within
|
|
296
|
+
* 2 s. The post's answer waits for it, so a store that is down costs the visitor that, not more. */
|
|
297
|
+
const STORE_POLICY = policy({
|
|
298
|
+
attempts: 3,
|
|
299
|
+
backoff: [50, 250],
|
|
300
|
+
jitter: .2,
|
|
301
|
+
deadline: 2e3
|
|
302
|
+
});
|
|
303
|
+
/** A post's evicts: tried again, kept alive by the platform past the response when the post does
|
|
304
|
+
* not wait (it does: `self_evict`'s caller), and said with the store's failure when given up. */
|
|
305
|
+
const self_evicts = outbox({
|
|
306
|
+
name: "postrender self-evict",
|
|
307
|
+
send: (e) => e.runtime.store.evict(e.key),
|
|
308
|
+
retry: STORE_POLICY,
|
|
309
|
+
giveUp: (e, error) => store_failed("evict", e.key, error),
|
|
310
|
+
waitUntil: (work) => postrender_app()?.keepAlive(work)
|
|
311
|
+
});
|
|
312
|
+
/** Action self-evict: a successful non-GET on a page URL evicts that URL (origin only — the edge
|
|
313
|
+
* copy follows via its s-maxage or the app's explicit invalidate). Settles once the store took it
|
|
314
|
+
* out, or gave up (it never rejects): the response waits for it, so the redirect's GET right after
|
|
315
|
+
* cannot be answered with the page from before the post (#283). */
|
|
316
|
+
async function self_evict(runtime, pathname) {
|
|
317
|
+
stale_flights({ url: pathname });
|
|
169
318
|
record({
|
|
170
319
|
domain: "server",
|
|
171
320
|
name: "server.postrender",
|
|
172
321
|
op: "self-evict",
|
|
173
322
|
url: pathname
|
|
174
323
|
});
|
|
175
|
-
|
|
324
|
+
const mark = store_health.now;
|
|
325
|
+
if (await self_evicts.put({
|
|
326
|
+
runtime,
|
|
327
|
+
key: pathname
|
|
328
|
+
})) store_answered(mark);
|
|
176
329
|
}
|
|
177
|
-
export {
|
|
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 };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Flight, Runtime } from "./registry.js";
|
|
2
2
|
import { Observation } from "./observe.js";
|
|
3
3
|
import { PostrenderEntry } from "./types.js";
|
|
4
4
|
import { ServerPlugin } from "@ogygia/core/plugin";
|
|
@@ -13,8 +13,9 @@ export interface PostrenderServerOptions {
|
|
|
13
13
|
interface State {
|
|
14
14
|
/** the store + edges this request reads and writes (its `postrender.handle()`'s) */
|
|
15
15
|
runtime: Runtime | null;
|
|
16
|
-
/**
|
|
17
|
-
|
|
16
|
+
/** the stampede flight this request opened, until it settled it (registry.ts: `stale` once a
|
|
17
|
+
* publish landed while it rendered, so it is not stored) */
|
|
18
|
+
flight: Flight | null;
|
|
18
19
|
/** the personalization observation (eligible requests only) */
|
|
19
20
|
obs: Observation | null;
|
|
20
21
|
/** the request was eligible to be stored */
|
package/dist/server.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { current_runtime, enter_flight, evict_tags, handled_runtime, installed_runtime, retracting, self_evict, 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 } from "@ogygia/machine";
|
|
5
7
|
import { createHash } from "node:crypto";
|
|
6
8
|
import { building, dev } from "$app/env";
|
|
7
9
|
import { STORED_DOCUMENT_META, defineServer } from "@ogygia/core/plugin";
|
|
@@ -153,44 +155,66 @@ function dev_postrender_note(response, ctx, obs) {
|
|
|
153
155
|
}
|
|
154
156
|
if (dev_noted_paths.has(ctx.path)) return;
|
|
155
157
|
dev_noted_paths.add(ctx.path);
|
|
156
|
-
|
|
158
|
+
warn("postrender_stays_per_request", {
|
|
159
|
+
path: ctx.path,
|
|
160
|
+
refusal
|
|
161
|
+
});
|
|
157
162
|
}
|
|
163
|
+
/** A refusal made before the body was read: the original Response is still whole. */
|
|
164
|
+
const REFUSED = {
|
|
165
|
+
stored: false,
|
|
166
|
+
response: null
|
|
167
|
+
};
|
|
158
168
|
/**
|
|
159
169
|
* The write-path verdict + store. Returns the stored entry AND a fresh Response (reading the
|
|
160
|
-
* body consumes the original)
|
|
170
|
+
* body consumes the original), or a refusal when the page must stay per-request. Pre-body guards
|
|
161
171
|
* run FIRST so an ineligible response's stream is never consumed.
|
|
162
172
|
*/
|
|
163
|
-
async function capture(response, ctx, runtime, obs, ttl) {
|
|
173
|
+
async function capture(response, ctx, runtime, obs, ttl, flight) {
|
|
164
174
|
const { path, facts } = ctx;
|
|
165
175
|
const tags = facts.sources ?? [];
|
|
166
|
-
|
|
176
|
+
/** Store `entry`, unless an invalidation started while the store was being written to (the
|
|
177
|
+
* 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
|
+
const put = (entry) => store_render(runtime, path, entry, {
|
|
180
|
+
ttl,
|
|
181
|
+
tags
|
|
182
|
+
}, flight);
|
|
183
|
+
if (obs.disqualified_by || obs.wrote_cookie) return REFUSED;
|
|
167
184
|
if (response.status === 301 || response.status === 308) {
|
|
168
185
|
const location = response.headers.get("location");
|
|
169
|
-
if (!location) return
|
|
186
|
+
if (!location) return REFUSED;
|
|
170
187
|
const entry = {
|
|
171
188
|
kind: "redirect",
|
|
172
189
|
status: response.status,
|
|
173
190
|
location,
|
|
174
191
|
created: Date.now()
|
|
175
192
|
};
|
|
176
|
-
await
|
|
177
|
-
ttl,
|
|
178
|
-
tags
|
|
179
|
-
});
|
|
193
|
+
if (!await put(entry)) return REFUSED;
|
|
180
194
|
return {
|
|
195
|
+
stored: true,
|
|
181
196
|
entry,
|
|
182
197
|
response
|
|
183
198
|
};
|
|
184
199
|
}
|
|
185
|
-
if (response.status !== 200) return
|
|
186
|
-
if (response.headers.has("set-cookie")) return
|
|
200
|
+
if (response.status !== 200) return REFUSED;
|
|
201
|
+
if (response.headers.has("set-cookie")) return REFUSED;
|
|
187
202
|
const content_type = response.headers.get("content-type") ?? "";
|
|
188
|
-
if (!content_type.includes("text/html")) return
|
|
203
|
+
if (!content_type.includes("text/html")) return REFUSED;
|
|
189
204
|
const app_cc = response.headers.get("cache-control") ?? "";
|
|
190
|
-
if (app_cc.includes("private") || app_cc.includes("no-store")) return
|
|
191
|
-
if (facts.streamed) return
|
|
205
|
+
if (app_cc.includes("private") || app_cc.includes("no-store")) return REFUSED;
|
|
206
|
+
if (facts.streamed) return REFUSED;
|
|
192
207
|
const html = await response.text();
|
|
193
|
-
|
|
208
|
+
/** The page as Kit answered it, again (its body was read above). */
|
|
209
|
+
const answered = () => new Response(html, {
|
|
210
|
+
status: response.status,
|
|
211
|
+
statusText: response.statusText,
|
|
212
|
+
headers: response.headers
|
|
213
|
+
});
|
|
214
|
+
if (KIT_DEFERRED_BOOT_RE.test(html)) return {
|
|
215
|
+
stored: false,
|
|
216
|
+
response: answered()
|
|
217
|
+
};
|
|
194
218
|
const created = Date.now();
|
|
195
219
|
const modes = stitch_modes(html);
|
|
196
220
|
const has_serve = modes.serve;
|
|
@@ -219,11 +243,12 @@ async function capture(response, ctx, runtime, obs, ttl) {
|
|
|
219
243
|
created,
|
|
220
244
|
...has_serve ? { stitch: true } : {}
|
|
221
245
|
};
|
|
222
|
-
await
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
246
|
+
if (!await put(entry)) return {
|
|
247
|
+
stored: false,
|
|
248
|
+
response: answered()
|
|
249
|
+
};
|
|
226
250
|
return {
|
|
251
|
+
stored: true,
|
|
227
252
|
entry,
|
|
228
253
|
response: postrender_response(entry, "stored")
|
|
229
254
|
};
|
|
@@ -242,7 +267,7 @@ const server = defineServer({
|
|
|
242
267
|
},
|
|
243
268
|
state: () => ({
|
|
244
269
|
runtime: null,
|
|
245
|
-
|
|
270
|
+
flight: null,
|
|
246
271
|
obs: null,
|
|
247
272
|
eligible: false
|
|
248
273
|
}),
|
|
@@ -256,7 +281,7 @@ const server = defineServer({
|
|
|
256
281
|
return;
|
|
257
282
|
}
|
|
258
283
|
const hit = await store_get(runtime, path);
|
|
259
|
-
if (hit && within_ttl(hit, ctx.options.ttl)) {
|
|
284
|
+
if (hit && within_ttl(hit, ctx.options.ttl) && !retracting(path)) {
|
|
260
285
|
ctx.app.devtools({
|
|
261
286
|
domain: "server",
|
|
262
287
|
name: "server.postrender",
|
|
@@ -265,9 +290,13 @@ const server = defineServer({
|
|
|
265
290
|
});
|
|
266
291
|
return serve(hit, "hit", ctx);
|
|
267
292
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const outcome = await flight;
|
|
293
|
+
let flight = enter_flight(path);
|
|
294
|
+
while ("joined" in flight) {
|
|
295
|
+
const outcome = await flight.joined;
|
|
296
|
+
if (outcome === STALE) {
|
|
297
|
+
flight = enter_flight(path);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
271
300
|
if (outcome.stored) {
|
|
272
301
|
ctx.app.devtools({
|
|
273
302
|
domain: "server",
|
|
@@ -279,7 +308,7 @@ const server = defineServer({
|
|
|
279
308
|
}
|
|
280
309
|
return;
|
|
281
310
|
}
|
|
282
|
-
state.
|
|
311
|
+
state.flight = flight.opened;
|
|
283
312
|
state.obs = observe_event(event, facts);
|
|
284
313
|
facts.storable = true;
|
|
285
314
|
facts.sources ??= [];
|
|
@@ -287,33 +316,57 @@ const server = defineServer({
|
|
|
287
316
|
async response(ctx, response) {
|
|
288
317
|
const { event, path, state } = ctx;
|
|
289
318
|
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
|
-
|
|
313
|
-
|
|
319
|
+
if (event.request.method.toUpperCase() !== "GET" && response.status < 400) await self_evict(runtime, path);
|
|
320
|
+
const { flight, obs } = state;
|
|
321
|
+
if (flight && obs) {
|
|
322
|
+
state.flight = null;
|
|
323
|
+
const settle = (stored) => flight.settle({ stored });
|
|
324
|
+
if (flight.stale) {
|
|
325
|
+
stamp_no_store(response);
|
|
326
|
+
settle(null);
|
|
327
|
+
ctx.app.devtools({
|
|
328
|
+
domain: "server",
|
|
329
|
+
name: "server.postrender",
|
|
330
|
+
op: "skip",
|
|
331
|
+
url: path,
|
|
332
|
+
reason: "invalidated-while-rendering"
|
|
333
|
+
});
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
const captured = await capture(response, ctx, runtime, obs, ctx.options.ttl, flight);
|
|
338
|
+
settle(captured.stored ? captured.entry : null);
|
|
339
|
+
ctx.app.devtools(captured.stored ? {
|
|
340
|
+
domain: "server",
|
|
341
|
+
name: "server.postrender",
|
|
342
|
+
op: "stored",
|
|
343
|
+
url: path,
|
|
344
|
+
bytes: captured.entry.kind === "page" ? captured.entry.html.length : 0
|
|
345
|
+
} : {
|
|
346
|
+
domain: "server",
|
|
347
|
+
name: "server.postrender",
|
|
348
|
+
op: "skip",
|
|
349
|
+
url: path,
|
|
350
|
+
reason: obs.disqualified_by ?? "response-ineligible"
|
|
351
|
+
});
|
|
352
|
+
if (captured.stored) return captured.entry.kind === "page" && captured.entry.stitch ? await serve_stitched(captured.entry, "stored", ctx) : captured.response;
|
|
353
|
+
stamp_no_store(captured.response ?? response);
|
|
354
|
+
if (captured.response) return captured.response;
|
|
355
|
+
} catch {
|
|
356
|
+
settle(null);
|
|
357
|
+
}
|
|
358
|
+
} else if (dev && obs && state.eligible) dev_postrender_note(response, ctx, obs);
|
|
359
|
+
},
|
|
360
|
+
done(ctx) {
|
|
361
|
+
const { state } = ctx;
|
|
362
|
+
if (!state.flight) return;
|
|
363
|
+
state.flight.settle({ stored: null });
|
|
364
|
+
state.flight = null;
|
|
314
365
|
},
|
|
315
366
|
async invalidate(tags) {
|
|
316
|
-
await
|
|
367
|
+
const { failed } = await evict_tags(tags);
|
|
368
|
+
if (failed.length === 1) throw failed[0];
|
|
369
|
+
if (failed.length > 1) throw new AggregateError(failed, `${failed.length} tags failed to evict`);
|
|
317
370
|
},
|
|
318
371
|
async lastBuild(version) {
|
|
319
372
|
const { store } = current_runtime();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ogygia/postrender",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Prerender at request time for ogygia: a page whose render read nothing personal is stored on its first request and served as bytes",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
@@ -31,15 +31,23 @@
|
|
|
31
31
|
"./vite": {
|
|
32
32
|
"types": "./dist/vite.d.ts",
|
|
33
33
|
"default": "./dist/vite.js"
|
|
34
|
+
},
|
|
35
|
+
"./internal/messages": {
|
|
36
|
+
"types": "./dist/messages.d.ts",
|
|
37
|
+
"default": "./dist/messages.js"
|
|
34
38
|
}
|
|
35
39
|
},
|
|
36
40
|
"publishConfig": {
|
|
37
41
|
"access": "public"
|
|
38
42
|
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@ogygia/machine": "^0.13.0"
|
|
45
|
+
},
|
|
39
46
|
"devDependencies": {
|
|
40
|
-
"@ogygia/
|
|
41
|
-
"@ogygia/
|
|
42
|
-
"@ogygia/
|
|
47
|
+
"@ogygia/config": "0.0.0",
|
|
48
|
+
"@ogygia/core": "0.13.0",
|
|
49
|
+
"@ogygia/flags": "0.13.0",
|
|
50
|
+
"@ogygia/router": "0.13.0",
|
|
43
51
|
"@ogygia/tooling": "0.0.0",
|
|
44
52
|
"@playwright/test": "1.62.1",
|
|
45
53
|
"@sveltejs/kit": "^2.70.2",
|
|
@@ -50,7 +58,7 @@
|
|
|
50
58
|
"vitest": "^4.1.12"
|
|
51
59
|
},
|
|
52
60
|
"peerDependencies": {
|
|
53
|
-
"@ogygia/core": "^0.
|
|
61
|
+
"@ogygia/core": "^0.13.0",
|
|
54
62
|
"@sveltejs/kit": ">=2.70.2 <4"
|
|
55
63
|
},
|
|
56
64
|
"scripts": {
|