foldkit-ssr 0.8.2 → 0.10.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 +496 -83
- package/dist/client-XnpTQZHw.d.ts +85 -0
- package/dist/client-XnpTQZHw.d.ts.map +1 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +343 -0
- package/dist/client.js.map +1 -0
- package/dist/context.d.ts +2 -0
- package/dist/context.js +33 -0
- package/dist/context.js.map +1 -0
- package/dist/diagnostics.d.ts +2 -0
- package/dist/diagnostics.js +59 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/{index.d.mts → index.d.ts} +96 -25
- package/dist/index.d.ts.map +1 -0
- package/dist/{index.mjs → index.js} +247 -108
- package/dist/index.js.map +1 -0
- package/dist/keep.d.ts +34 -0
- package/dist/keep.d.ts.map +1 -0
- package/dist/keep.js +80 -0
- package/dist/keep.js.map +1 -0
- package/dist/listen-DaUPRKVR.d.ts +71 -0
- package/dist/listen-DaUPRKVR.d.ts.map +1 -0
- package/dist/listen.d.ts +2 -0
- package/dist/listen.js +187 -0
- package/dist/listen.js.map +1 -0
- package/dist/requestContext-D9nsbGSJ.d.ts +21 -0
- package/dist/requestContext-D9nsbGSJ.d.ts.map +1 -0
- package/dist/requestContext.d.ts +2 -0
- package/dist/requestContext.js +48 -0
- package/dist/requestContext.js.map +1 -0
- package/dist/resumable-BPN_qQHU.d.ts +107 -0
- package/dist/resumable-BPN_qQHU.d.ts.map +1 -0
- package/dist/resumable.d.ts +2 -0
- package/dist/resumable.js +401 -0
- package/dist/resumable.js.map +1 -0
- package/dist/{client-DkwAniQQ.d.mts → shared-DbupL3vO.d.ts} +233 -194
- package/dist/shared-DbupL3vO.d.ts.map +1 -0
- package/dist/shared.d.ts +2 -0
- package/dist/shared.js +347 -0
- package/dist/shared.js.map +1 -0
- package/dist/{vite.d.mts → vite.d.ts} +3 -5
- package/dist/vite.d.ts.map +1 -0
- package/dist/{vite.mjs → vite.js} +3 -4
- package/dist/vite.js.map +1 -0
- package/package.json +18 -18
- package/dist/client-DVG9_3rL.mjs +0 -1113
- package/dist/client-DVG9_3rL.mjs.map +0 -1
- package/dist/client-DkwAniQQ.d.mts.map +0 -1
- package/dist/client.d.mts +0 -2
- package/dist/client.mjs +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/vite.d.mts.map +0 -1
- package/dist/vite.mjs.map +0 -1
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { withContext } from "./context.js";
|
|
2
|
+
import { handlerUnnamed, pageFailed, reportingTo } from "./diagnostics.js";
|
|
3
|
+
import { BINDING_ATTRIBUTE, FALLBACK_DEPTH_FIELD, FALLBACK_FIELD, KEEP_ATTRIBUTE, SLOT_ATTRIBUTE } from "./resumable.js";
|
|
4
|
+
import { FOLDKIT_APP_ATTRIBUTE, META_ATTRIBUTE, RESUME_ATTRIBUTE, ResumeRefused, STATIC_ATTRIBUTE, STYLES_MARKER, allowedTags, attributeOf, codecOf, metaMarkup, modelFrom, pathKey, plan, projectionsOf, resume, routeOf, serializeJsonScript, serving, startingFrom, staticRegion, tagOf } from "./shared.js";
|
|
5
|
+
import { Diagnostics, Resume, SSR as SSR$1, UNTIL_LIMIT_MS } from "./client.js";
|
|
6
|
+
import { requestContext } from "./requestContext.js";
|
|
2
7
|
import { Cause, Context, Effect, Exit, Layer, Option, Result, Schema } from "effect";
|
|
8
|
+
import { Diagnostics as Diagnostics$1, Metadata } from "foldkit-surface";
|
|
3
9
|
import { FOLDKIT_FLAGS_ATTRIBUTE, Rendered, Responded, injectIntoTemplate, renderToString } from "foldkit/experimental/server";
|
|
4
|
-
|
|
5
|
-
//#region src/index.ts
|
|
10
|
+
// src/index.ts
|
|
6
11
|
/**
|
|
7
12
|
* `foldkit-ssr`: what crosses from the server to the browser when a page is
|
|
8
13
|
* rendered on one and resumed on the other.
|
|
@@ -58,9 +63,8 @@ const envelopeOf = (resume, payload, options) => serializeJsonScript({
|
|
|
58
63
|
* Foldkit's own app and build stamps. `injectIntoTemplate` accepts the
|
|
59
64
|
* envelope there, as part of the root, and refuses it beside the root, so a
|
|
60
65
|
* page the host injects carries the handover without the entry owning the
|
|
61
|
-
* template.
|
|
62
|
-
*
|
|
63
|
-
* boots.
|
|
66
|
+
* template. The browser reads the envelope, then takes the attribute off
|
|
67
|
+
* as the page boots, before Foldkit adopts the server's nodes around it.
|
|
64
68
|
*/
|
|
65
69
|
const withEnvelopeAttribute = (html, json) => {
|
|
66
70
|
const stamp = "data-foldkit-app=\"";
|
|
@@ -92,10 +96,11 @@ const isInRootTag = (html, at) => {
|
|
|
92
96
|
return true;
|
|
93
97
|
};
|
|
94
98
|
/**
|
|
95
|
-
* The rendered markup with `extra` as the root's last
|
|
96
|
-
* paint is styled before any script runs where no
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
+
* The rendered markup with `extra` as the root's last children, after the
|
|
100
|
+
* styles marker, so the first paint is styled before any script runs where no
|
|
101
|
+
* template head can take it; `SSR.hydrate` moves them into the head. A void
|
|
102
|
+
* root holds no children: styles for one are refused, naming the tag, rather
|
|
103
|
+
* than served where no browser could keep them.
|
|
99
104
|
*/
|
|
100
105
|
const withInRoot = (html, extra) => {
|
|
101
106
|
const tag = /^<([A-Za-z][A-Za-z0-9-]*)/.exec(html)?.[1];
|
|
@@ -104,7 +109,7 @@ const withInRoot = (html, extra) => {
|
|
|
104
109
|
reason: "VoidRootWithStyles",
|
|
105
110
|
message: `styles need a root element that can hold them, but the view returned <${tag ?? "?"}>: put the styles in the template's head with SSR.generate, or return an element root`
|
|
106
111
|
}));
|
|
107
|
-
return Result.succeed(`${html.slice(0, at)}
|
|
112
|
+
return Result.succeed(`${html.slice(0, at)}<!--${STYLES_MARKER}-->${extra}${html.slice(at)}`);
|
|
108
113
|
};
|
|
109
114
|
/**
|
|
110
115
|
* The Model the browser will start from when the server's Model is `model`:
|
|
@@ -139,14 +144,31 @@ const inspect = (plan, model) => {
|
|
|
139
144
|
/** How a plan covers its Surfaces, given the server's Model and the browser's. */
|
|
140
145
|
const coverage = (plan, model, browser) => {
|
|
141
146
|
const covered = new Set(plan.parts.flatMap((part) => part.covers));
|
|
147
|
+
const families = /* @__PURE__ */ new Map();
|
|
148
|
+
for (const family of plan.surfaces.flatMap((source) => Option.toArray(familyOf(source)))) {
|
|
149
|
+
const siblings = families.get(family.from);
|
|
150
|
+
if (siblings === void 0) families.set(family.from, [family]);
|
|
151
|
+
else siblings.push(family);
|
|
152
|
+
}
|
|
153
|
+
const shown = /* @__PURE__ */ new Set();
|
|
142
154
|
return {
|
|
143
155
|
id: plan.id,
|
|
144
156
|
state: plan.state.dependencies.map(pathOf),
|
|
145
157
|
local: plan.local.map(pathOf),
|
|
146
158
|
parts: plan.parts.map((part) => part.id),
|
|
147
|
-
surfaces: plan.surfaces.flatMap((source) =>
|
|
159
|
+
surfaces: plan.surfaces.flatMap((source) => {
|
|
160
|
+
const surface = Option.match(familyOf(source), {
|
|
161
|
+
onNone: () => source,
|
|
162
|
+
onSome: (family) => family.from
|
|
163
|
+
});
|
|
164
|
+
if (shown.has(surface)) return [];
|
|
165
|
+
shown.add(surface);
|
|
166
|
+
return coverSurface(plan, covered, surface, families.get(surface) ?? [], model, browser);
|
|
167
|
+
})
|
|
148
168
|
};
|
|
149
169
|
};
|
|
170
|
+
/** The plan entry as a family with a parent; none for a lone Surface. */
|
|
171
|
+
const familyOf = (source) => source.from !== void 0 && typeof source.instancesOf === "function" ? Option.some(source) : Option.none();
|
|
150
172
|
/** One coverage row: a lone Surface, a family's parent, or one family instance. */
|
|
151
173
|
const coverRow = (plan, covered, entry, served, browserServed) => ({
|
|
152
174
|
name: entry.name,
|
|
@@ -165,33 +187,30 @@ const coverRow = (plan, covered, entry, served, browserServed) => ({
|
|
|
165
187
|
sameInBrowser: readsKey(served) === readsKey(browserServed)
|
|
166
188
|
});
|
|
167
189
|
/**
|
|
168
|
-
* A
|
|
169
|
-
*
|
|
170
|
-
* covered; each instance row says which key it is and which
|
|
171
|
-
* it, so a gap names the instance (`Member[u1]`), not just the
|
|
190
|
+
* A Surface's coverage rows: its own row, then, while it is active, one row
|
|
191
|
+
* per instance of each family it is the parent of. The parent row keeps the
|
|
192
|
+
* parent's reads covered; each instance row says which key it is and which
|
|
193
|
+
* parent reveals it, so a gap names the instance (`Member[u1]`), not just the
|
|
194
|
+
* Surface.
|
|
172
195
|
*/
|
|
173
|
-
const
|
|
174
|
-
const
|
|
175
|
-
if (
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
} }
|
|
192
|
-
}
|
|
193
|
-
}, instance.projection, browserInstances.find((other) => other.key === instance.key)?.projection));
|
|
194
|
-
return rows;
|
|
196
|
+
const coverSurface = (plan, covered, surface, families, model, browser) => {
|
|
197
|
+
const own = coverRow(plan, covered, surface, Option.getOrUndefined(surface.projectionOf(model)), Option.getOrUndefined(surface.projectionOf(browser)));
|
|
198
|
+
if (!own.active) return [own];
|
|
199
|
+
const via = {
|
|
200
|
+
surface: surface.name,
|
|
201
|
+
...surface.activation === void 0 ? {} : { activation: {
|
|
202
|
+
path: pathOf(surface.activation.path),
|
|
203
|
+
cover: coverOf(surface.activation.path, plan)
|
|
204
|
+
} }
|
|
205
|
+
};
|
|
206
|
+
return [own, ...families.flatMap((family) => {
|
|
207
|
+
const browserInstances = new Map(family.instancesOf(browser).map((instance) => [instance.key, instance.projection]));
|
|
208
|
+
return family.instancesOf(model).map((instance) => coverRow(plan, covered, {
|
|
209
|
+
name: family.name,
|
|
210
|
+
key: instance.key,
|
|
211
|
+
via
|
|
212
|
+
}, instance.projection, browserInstances.get(instance.key)));
|
|
213
|
+
})];
|
|
195
214
|
};
|
|
196
215
|
/** A coverage row as a shortfall names it: `Member[u1]` for an instance, the name alone otherwise. */
|
|
197
216
|
const displayOf = (surface) => surface.key === void 0 ? surface.name : `${surface.name}[${surface.key}]`;
|
|
@@ -215,7 +234,8 @@ var ResumeUnsafe = class extends Schema.TaggedError()("ResumeUnsafe", {
|
|
|
215
234
|
"EagerStartRequired",
|
|
216
235
|
"UndeclaredSurfaces",
|
|
217
236
|
"BindingInStaticRegion",
|
|
218
|
-
"VoidRootWithStyles"
|
|
237
|
+
"VoidRootWithStyles",
|
|
238
|
+
"UnkeepableField"
|
|
219
239
|
]),
|
|
220
240
|
message: Schema.String
|
|
221
241
|
}) {};
|
|
@@ -224,6 +244,24 @@ const unlistedBindings = (plan, model, bindings) => {
|
|
|
224
244
|
const allowed = allowedTags(plan, model);
|
|
225
245
|
return bindings.filter((binding) => !allowed.has(tagOf(binding.message))).map((binding) => `the ${binding.event} binding on ${binding.element} dispatches ${tagOf(binding.message)}, which no active Surface lists in its messages`);
|
|
226
246
|
};
|
|
247
|
+
/**
|
|
248
|
+
* A line for each `KeepTyped` the render drew on an element the resumable
|
|
249
|
+
* builder did not build, whose marker still holds its server-render value:
|
|
250
|
+
* nothing marks which binding replays such a field, so every browser would
|
|
251
|
+
* refuse the page. Foldkit escapes `<` in text and attribute values, so the
|
|
252
|
+
* last `<` before the marker opens its element.
|
|
253
|
+
*/
|
|
254
|
+
const unplacedKeeps = (html) => {
|
|
255
|
+
const marker = `${KEEP_ATTRIBUTE}="?"`;
|
|
256
|
+
const found = [];
|
|
257
|
+
for (let at = html.indexOf(marker); at !== -1; at = html.indexOf(marker, at + 1)) {
|
|
258
|
+
const open = html.lastIndexOf("<", at);
|
|
259
|
+
const tag = /^<([A-Za-z][A-Za-z0-9._-]*)/.exec(html.slice(open))?.[1] ?? "an element";
|
|
260
|
+
const id = /\sid="([^"]*)"/.exec(html.slice(open, html.indexOf(">", at)))?.[1];
|
|
261
|
+
found.push(`KeepTyped on ${id === void 0 ? tag : `${tag}#${id}`}, which Resume.builder did not build, so nothing marks the binding its typing replays as: build the element with the resumable builder (rh.input), not h`);
|
|
262
|
+
}
|
|
263
|
+
return found;
|
|
264
|
+
};
|
|
227
265
|
/** Loads every lazy bundle's bodies, so the view renders whole. */
|
|
228
266
|
const loadLazy = (config) => Effect.promise(() => Promise.all((config.lazy ?? []).map((bundle) => bundle.load())));
|
|
229
267
|
/** Foldkit's own Flags script, which a resumed page never carries. */
|
|
@@ -294,7 +332,7 @@ const eagerEntries = (config, plan, model) => {
|
|
|
294
332
|
* Foldkit would rebuild that part of the page silently, so this is the one
|
|
295
333
|
* place it shows.
|
|
296
334
|
*/
|
|
297
|
-
const render = (config, plan, options) => renderMatching(config, plan, options, "full");
|
|
335
|
+
const render = (config, plan, options) => Effect.map(renderMatching(config, plan, options, "full"), (served) => served.page);
|
|
298
336
|
const renderMatching = (config, plan, options, match) => Effect.gen(function* () {
|
|
299
337
|
yield* loadLazy(config);
|
|
300
338
|
let served;
|
|
@@ -304,6 +342,7 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
304
342
|
const browserBindings = [];
|
|
305
343
|
const inStatic = [];
|
|
306
344
|
const unnamed = [];
|
|
345
|
+
const unkeepable = [];
|
|
307
346
|
const fallback = fallbackEncoder(plan);
|
|
308
347
|
const capturing = withContext({
|
|
309
348
|
...config,
|
|
@@ -318,9 +357,11 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
318
357
|
depth: 0,
|
|
319
358
|
region: void 0,
|
|
320
359
|
inStatic,
|
|
321
|
-
unnamed
|
|
360
|
+
unnamed,
|
|
361
|
+
unkeepable
|
|
322
362
|
});
|
|
323
363
|
const full = yield* renderToString(capturing, options);
|
|
364
|
+
unkeepable.push(...unplacedKeeps(full.html));
|
|
324
365
|
if (duplicates.size > 0) return yield* new ResumeUnsafe({
|
|
325
366
|
reason: "DuplicateStaticRegion",
|
|
326
367
|
message: `two static regions share the id ${[...duplicates].map((id) => `"${id}"`).join(", ")}: the browser could adopt only one`
|
|
@@ -329,6 +370,10 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
329
370
|
reason: "BindingInStaticRegion",
|
|
330
371
|
message: `a static region is the server's alone, and these would never run: ${inStatic.map(({ region, element, event }) => `a ${event} handler on ${element} in "${region}"`).join(", ")}. What a Message changes belongs in a Surface`
|
|
331
372
|
});
|
|
373
|
+
if (unkeepable.length > 0) return yield* new ResumeUnsafe({
|
|
374
|
+
reason: "UnkeepableField",
|
|
375
|
+
message: `the page cannot keep what is typed into these fields before it boots:\n- ${unkeepable.join("\n- ")}`
|
|
376
|
+
});
|
|
332
377
|
const started = served;
|
|
333
378
|
const commands = started.commands ?? [];
|
|
334
379
|
if (commands.length > 0 && plan.boot === void 0) return yield* new ResumeUnsafe({
|
|
@@ -390,13 +435,16 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
390
435
|
const styled = options.styles === void 0 ? Result.succeed(rendered.html) : withInRoot(rendered.html, options.styles(rendered));
|
|
391
436
|
if (Result.isFailure(styled)) return yield* styled.failure;
|
|
392
437
|
return {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
438
|
+
page: {
|
|
439
|
+
rendered: {
|
|
440
|
+
...rendered,
|
|
441
|
+
html: withEnvelopeAttribute(styled.success, envelope)
|
|
442
|
+
},
|
|
443
|
+
meta,
|
|
444
|
+
envelope,
|
|
445
|
+
unnamed
|
|
396
446
|
},
|
|
397
|
-
|
|
398
|
-
envelope,
|
|
399
|
-
unnamed
|
|
447
|
+
model: started.model
|
|
400
448
|
};
|
|
401
449
|
});
|
|
402
450
|
/**
|
|
@@ -483,25 +531,26 @@ const robots = (options) => [
|
|
|
483
531
|
`Sitemap: ${new URL(options.sitemap ?? "/sitemap.xml", options.origin).href}`,
|
|
484
532
|
""
|
|
485
533
|
].join("\n");
|
|
486
|
-
const
|
|
534
|
+
const reportedUnnamed = /* @__PURE__ */ new Set();
|
|
487
535
|
/**
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
536
|
+
* Reports, once per process, each handler that keeps a page from answering
|
|
537
|
+
* before boot. Only a page that waits to boot is held up: one started
|
|
538
|
+
* `'now'`, with no lazy bundle to load, boots before any event.
|
|
491
539
|
*/
|
|
492
|
-
const
|
|
493
|
-
if (plan.start === "now" && (config.lazy ?? []).length === 0) return;
|
|
494
|
-
|
|
540
|
+
const reportUnnamed = (config, plan, unnamed) => Effect.suspend(() => {
|
|
541
|
+
if (plan.start === "now" && (config.lazy ?? []).length === 0) return Effect.void;
|
|
542
|
+
const first = unnamed.filter(({ element, event }) => {
|
|
495
543
|
const key = JSON.stringify([
|
|
496
544
|
plan.id,
|
|
497
545
|
element,
|
|
498
546
|
event
|
|
499
547
|
]);
|
|
500
|
-
if (
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
}
|
|
504
|
-
};
|
|
548
|
+
if (reportedUnnamed.has(key)) return false;
|
|
549
|
+
reportedUnnamed.add(key);
|
|
550
|
+
return true;
|
|
551
|
+
});
|
|
552
|
+
return Effect.forEach(first, ({ element, event }) => Diagnostics$1.report(handlerUnnamed(plan.id, element, event)), { discard: true });
|
|
553
|
+
});
|
|
505
554
|
/**
|
|
506
555
|
* Renders pages at build time, one per path, each as `SSR.render` and
|
|
507
556
|
* `SSR.page` would, to be written as files. `origin` makes each path the full
|
|
@@ -527,18 +576,15 @@ const generate = (config, plan, options) => Effect.gen(function* () {
|
|
|
527
576
|
});
|
|
528
577
|
files.set(fileOf(path), path);
|
|
529
578
|
}
|
|
530
|
-
return yield* Effect.forEach(options.paths, (path) =>
|
|
579
|
+
return yield* Effect.forEach(options.paths, (path) => renderMatching(config, plan, {
|
|
531
580
|
buildId: options.buildId,
|
|
532
581
|
url: new URL(path, options.origin).href,
|
|
533
582
|
...options.flags === void 0 ? {} : { flags: options.flags(path) }
|
|
534
|
-
}, "path"), (result) => {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
html: page(options.template, result, { head: options.head })
|
|
540
|
-
};
|
|
541
|
-
}));
|
|
583
|
+
}, "path").pipe(Effect.map((served) => served.page), Effect.tap((result) => reportUnnamed(config, plan, result.unnamed)), Effect.map((result) => ({
|
|
584
|
+
path,
|
|
585
|
+
file: fileOf(path),
|
|
586
|
+
html: page(options.template, result, { head: options.head })
|
|
587
|
+
}))));
|
|
542
588
|
});
|
|
543
589
|
/**
|
|
544
590
|
* The server entry for Foldkit's pipeline: the `renderPage` a dev server,
|
|
@@ -547,15 +593,18 @@ const generate = (config, plan, options) => Effect.gen(function* () {
|
|
|
547
593
|
* served by Node and Workers alike.
|
|
548
594
|
*
|
|
549
595
|
* `GET` and `HEAD` render the page against the plan, with the request's URL
|
|
550
|
-
* and, when the application has Flags, `flags(request)
|
|
596
|
+
* and, when the application has Flags, `flags(request)`; `prepare(model,
|
|
597
|
+
* request)` turns the Model `init` gave into the one rendered, a read made
|
|
598
|
+
* before the render (`Data.satisfy`). `POST` goes to
|
|
551
599
|
* `SSR.handle` when the plan has a server fallback, and is answered `400` when
|
|
552
600
|
* the post is unusable. `OPTIONS` is answered `204`, and any other method
|
|
553
601
|
* `405`, each with the methods the entry answers in `allow` (`handleRequest`
|
|
554
602
|
* passes every method through). A render that fails, or a plan the render
|
|
555
|
-
* refuses, is answered `500` with the reason
|
|
556
|
-
* browser cannot resume. `headers(request)`
|
|
557
|
-
*
|
|
558
|
-
* a
|
|
603
|
+
* refuses, is answered `500` with the reason reported (`diagnostics`), never with a page the
|
|
604
|
+
* browser cannot resume. `headers(request, model)` is set over Foldkit's own
|
|
605
|
+
* on every answer: a cache policy, a CORS answer to a preflight, or a cookie
|
|
606
|
+
* written from the Model a post reached. `status(model, request)` gives a page
|
|
607
|
+
* its status from that Model, `404` for a route not found.
|
|
559
608
|
*
|
|
560
609
|
* The page is answered as `Rendered`: the rendered application carries the
|
|
561
610
|
* envelope in its root, so the host injects it into its own template with
|
|
@@ -570,27 +619,45 @@ const generate = (config, plan, options) => Effect.gen(function* () {
|
|
|
570
619
|
*/
|
|
571
620
|
const entry = (config, plan, options) => {
|
|
572
621
|
if (plan.meta !== void 0) throw new Error(`foldkit-ssr: plan "${plan.id}" says what the page says of itself (meta), which is written into the template's head, and the template is the host's: serve it with SSR.generate, not SSR.entry`);
|
|
573
|
-
|
|
622
|
+
const listed = options.origins === "same-origin" ? [] : options.origins ?? [];
|
|
623
|
+
for (const origin of listed) if (!URL.canParse(origin) || new URL(origin).origin !== origin) throw new Error(`foldkit-ssr: ${JSON.stringify(origin)} in origins is not an origin`);
|
|
624
|
+
if (options.answerPost !== void 0 && plan.fallback !== "server") throw new Error(`foldkit-ssr: plan "${plan.id}" has no server fallback, so no form is posted to answer (answerPost)`);
|
|
574
625
|
if (plan.fallback === "server" && options.origins === void 0) throw new Error(`foldkit-ssr: plan "${plan.id}" handles posted forms, which run as whoever posted; name the origins a form may be posted from (origins)`);
|
|
575
|
-
const
|
|
626
|
+
for (const path of options.prerendered ?? []) if (!path.startsWith("/") || /[?#]/.test(path)) throw new Error(`foldkit-ssr: "${path}" in prerendered is not a path a file can be served at: start it with / and leave out any query or fragment`);
|
|
627
|
+
const prerendered = new Set((options.prerendered ?? []).map(pathKey));
|
|
628
|
+
const matchOf = (url) => {
|
|
629
|
+
const route = routeOf(url);
|
|
630
|
+
return !route.includes("?") && prerendered.has(pathKey(route)) ? "path" : "full";
|
|
631
|
+
};
|
|
632
|
+
const origins = new Set(listed);
|
|
633
|
+
const originsOf = (request) => options.origins === "same-origin" ? /* @__PURE__ */ new Set([new URL(request.url).origin]) : origins;
|
|
576
634
|
const allow = plan.fallback === "server" ? "GET, HEAD, POST, OPTIONS" : "GET, HEAD, OPTIONS";
|
|
577
635
|
const merge = (response, extra) => {
|
|
578
636
|
for (const name of new Set(extra.keys())) response.headers.delete(name);
|
|
579
637
|
extra.forEach((value, name) => response.headers.append(name, value));
|
|
580
638
|
return response;
|
|
581
639
|
};
|
|
640
|
+
const headersOf = (request, model) => {
|
|
641
|
+
if (options.headers === void 0) return void 0;
|
|
642
|
+
const headers = new Headers(options.headers(request, model));
|
|
643
|
+
for (const name of headers.keys()) if (ENTRY_HEADERS.has(name)) throw new Error(`headers set ${name}, which the entry writes and an application may not`);
|
|
644
|
+
return headers;
|
|
645
|
+
};
|
|
582
646
|
const answer = async (request) => {
|
|
583
647
|
const method = request.method.toUpperCase();
|
|
584
648
|
const posting = method === "POST" && plan.fallback === "server";
|
|
585
|
-
const
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
extra
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
649
|
+
const { diagnostics } = options;
|
|
650
|
+
const reporting = reportingTo(diagnostics === void 0 ? void 0 : (diagnostic) => diagnostics(diagnostic, request));
|
|
651
|
+
const responded = async (response, { reported = false } = {}) => {
|
|
652
|
+
let extra;
|
|
653
|
+
try {
|
|
654
|
+
extra = headersOf(request, Option.none());
|
|
655
|
+
} catch (error) {
|
|
656
|
+
if (!reported) await Effect.runPromise(reporting(Diagnostics$1.report(pageFailed(request.url, Cause.die(error)))));
|
|
657
|
+
return Responded(plainText(500, "The page could not be rendered."));
|
|
658
|
+
}
|
|
659
|
+
return Responded(extra === void 0 ? response : merge(response, extra));
|
|
660
|
+
};
|
|
594
661
|
if (method === "OPTIONS") return responded(new Response(null, {
|
|
595
662
|
status: 204,
|
|
596
663
|
headers: { allow }
|
|
@@ -599,37 +666,89 @@ const entry = (config, plan, options) => {
|
|
|
599
666
|
status: 405,
|
|
600
667
|
headers: { allow }
|
|
601
668
|
}));
|
|
602
|
-
if (posting && !postedFrom(request,
|
|
669
|
+
if (posting && !postedFrom(request, originsOf(request))) return responded(plainText(403, "The form was not posted from this site."));
|
|
603
670
|
const flagsOf = options.flags;
|
|
671
|
+
const statusOf = options.status;
|
|
672
|
+
const { prepare } = options;
|
|
604
673
|
const styled = options.styles === void 0 ? {} : { styles: options.styles };
|
|
605
674
|
const exit = await Effect.runPromiseExit(Effect.gen(function* () {
|
|
606
675
|
const flags = flagsOf === void 0 ? void 0 : yield* Effect.tryPromise(async () => flagsOf(request));
|
|
607
676
|
const flagged = flagsOf === void 0 ? {} : { flags };
|
|
608
|
-
|
|
677
|
+
const prepared = prepare === void 0 ? {} : { prepare };
|
|
678
|
+
const post = () => ({
|
|
609
679
|
buildId: options.buildId,
|
|
610
680
|
...flagged,
|
|
611
681
|
...styled,
|
|
682
|
+
...prepared,
|
|
612
683
|
...options.resources === void 0 ? {} : { resources: options.resources(request) }
|
|
613
684
|
});
|
|
614
|
-
|
|
685
|
+
if (posting && options.answerPost === "redirect") {
|
|
686
|
+
const model = yield* postedModel(request, config, plan, post());
|
|
687
|
+
const extra = yield* Effect.sync(() => headersOf(request, Option.some(model)));
|
|
688
|
+
const redirect = new Response(null, {
|
|
689
|
+
status: 303,
|
|
690
|
+
headers: { location: sameAddress(request.url) }
|
|
691
|
+
});
|
|
692
|
+
return Responded(extra === void 0 ? redirect : merge(redirect, extra));
|
|
693
|
+
}
|
|
694
|
+
const served = posting ? yield* handleServed(request, config, plan, post()) : yield* renderMatching(prepare === void 0 ? config : yield* preparedStart(request, config, {
|
|
695
|
+
url: request.url,
|
|
696
|
+
...flagged
|
|
697
|
+
}, prepare), plan, {
|
|
615
698
|
buildId: options.buildId,
|
|
616
699
|
url: request.url,
|
|
617
700
|
...flagged,
|
|
618
701
|
...styled
|
|
702
|
+
}, matchOf(request.url));
|
|
703
|
+
const extra = yield* Effect.sync(() => headersOf(request, Option.some(served.model)));
|
|
704
|
+
const status = Option.map(Option.fromUndefinedOr(statusOf), (of) => of(served.model, request));
|
|
705
|
+
const unanswerable = Option.filter(status, (given) => !Number.isInteger(given) || given < 200 || given > 599);
|
|
706
|
+
if (Option.isSome(unanswerable)) return yield* Effect.die(/* @__PURE__ */ new Error(`status gave ${unanswerable.value} for the page, which is no status between 200 and 599`));
|
|
707
|
+
yield* reportUnnamed(config, plan, served.page.unnamed);
|
|
708
|
+
return Rendered(served.page.rendered, {
|
|
709
|
+
...extra === void 0 ? {} : { headers: extra },
|
|
710
|
+
...Option.match(status, {
|
|
711
|
+
onNone: () => ({}),
|
|
712
|
+
onSome: (status) => ({ status })
|
|
713
|
+
})
|
|
619
714
|
});
|
|
620
|
-
}));
|
|
715
|
+
}).pipe(reporting));
|
|
621
716
|
if (Exit.isFailure(exit)) {
|
|
622
717
|
const refused = Cause.findErrorOption(exit.cause).pipe(Option.filter((error) => error instanceof FallbackRefused));
|
|
623
718
|
if (Option.isSome(refused)) return responded(plainText(400, `The form could not be handled: ${refused.value.message}`));
|
|
624
|
-
|
|
625
|
-
return responded(plainText(500, "The page could not be rendered."));
|
|
719
|
+
await Effect.runPromise(reporting(Diagnostics$1.report(pageFailed(request.url, exit.cause))));
|
|
720
|
+
return responded(plainText(500, "The page could not be rendered."), { reported: true });
|
|
626
721
|
}
|
|
627
|
-
|
|
628
|
-
return extra === void 0 ? Rendered(exit.value.rendered) : Rendered(exit.value.rendered, { headers: extra });
|
|
722
|
+
return exit.value;
|
|
629
723
|
};
|
|
630
724
|
return { renderPage: answer };
|
|
631
725
|
};
|
|
632
726
|
/**
|
|
727
|
+
* Where a post's answer sends the browser: the address it posted to, its path
|
|
728
|
+
* and query alone. Relative, so the browser resolves it against that address
|
|
729
|
+
* and it names no other origin, whatever `Host` the request claimed; leading
|
|
730
|
+
* slashes collapse to one, since `//elsewhere.test/` would name another host.
|
|
731
|
+
*/
|
|
732
|
+
const sameAddress = (url) => {
|
|
733
|
+
const { pathname, search } = new URL(url);
|
|
734
|
+
return `${pathname.replace(/^\/+/, "/")}${search}`;
|
|
735
|
+
};
|
|
736
|
+
/**
|
|
737
|
+
* What `headers` may not set: what frames the response, which the host's
|
|
738
|
+
* server writes, and `location`, which only the entry writes.
|
|
739
|
+
*/
|
|
740
|
+
const ENTRY_HEADERS = /* @__PURE__ */ new Set([
|
|
741
|
+
"connection",
|
|
742
|
+
"content-length",
|
|
743
|
+
"keep-alive",
|
|
744
|
+
"location",
|
|
745
|
+
"proxy-connection",
|
|
746
|
+
"te",
|
|
747
|
+
"trailer",
|
|
748
|
+
"transfer-encoding",
|
|
749
|
+
"upgrade"
|
|
750
|
+
]);
|
|
751
|
+
/**
|
|
633
752
|
* Whether a post came from one of `origins`, by what the browser sends and no
|
|
634
753
|
* page or form can set: its `Origin`, or, where that is missing or opaque
|
|
635
754
|
* (`Origin: null`, which a page served with `Referrer-Policy: no-referrer`
|
|
@@ -710,7 +829,11 @@ const startOf = (config, options) => Effect.gen(function* () {
|
|
|
710
829
|
let started;
|
|
711
830
|
yield* renderToString({
|
|
712
831
|
...config,
|
|
713
|
-
init: (...args) => started = config.init(...args)
|
|
832
|
+
init: (...args) => started = config.init(...args),
|
|
833
|
+
view: (_model, h) => ({
|
|
834
|
+
title: "",
|
|
835
|
+
body: h.div([], [])
|
|
836
|
+
})
|
|
714
837
|
}, {
|
|
715
838
|
buildId: "x",
|
|
716
839
|
...options
|
|
@@ -718,15 +841,34 @@ const startOf = (config, options) => Effect.gen(function* () {
|
|
|
718
841
|
return started;
|
|
719
842
|
});
|
|
720
843
|
/**
|
|
844
|
+
* The config a request starts from: `init`'s answer, its Model prepared. A
|
|
845
|
+
* failed `prepare` is a defect, answered 500 and reported as a failed render.
|
|
846
|
+
*/
|
|
847
|
+
const preparedStart = (request, config, options, prepare) => Effect.gen(function* () {
|
|
848
|
+
const started = yield* startOf(config, options);
|
|
849
|
+
const model = yield* Effect.orDie(prepare(started.model, request));
|
|
850
|
+
return startingFrom(config, {
|
|
851
|
+
...started,
|
|
852
|
+
model
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
/**
|
|
721
856
|
* Answers a form posted by a page rendered with `fallback: 'server'`: the
|
|
722
857
|
* posted Message is decoded through the plan's Message Schema, with posted
|
|
723
858
|
* fields of the same names as its own overriding them, and must be one the
|
|
724
859
|
* Surfaces active for the request's Model may send. The server then rebuilds
|
|
725
|
-
* the Model the browser had, `init` and then the plan's `boot` with every
|
|
860
|
+
* the Model the browser had, `init`, `prepare` and then the plan's `boot` with every
|
|
726
861
|
* Command it leads to, runs `update` with the Message and every Command that
|
|
727
862
|
* follows, and renders the result as a fresh page.
|
|
728
863
|
*/
|
|
729
|
-
const handle = (request, config, plan, options) => Effect.
|
|
864
|
+
const handle = (request, config, plan, options) => Effect.map(handleServed(request, config, plan, options), (served) => served.page);
|
|
865
|
+
const handleServed = (request, config, plan, options) => Effect.flatMap(postedModel(request, config, plan, options), (model) => renderMatching(startingFrom(config, { model }), plan, {
|
|
866
|
+
buildId: options.buildId,
|
|
867
|
+
url: request.url,
|
|
868
|
+
...options.styles === void 0 ? {} : { styles: options.styles }
|
|
869
|
+
}, "full"));
|
|
870
|
+
/** The Model a posted form reaches: its Message decoded and checked, then `update` and its Commands. */
|
|
871
|
+
const postedModel = (request, config, plan, options) => Effect.scoped(Effect.gen(function* () {
|
|
730
872
|
if (plan.fallback !== "server" || plan.Message === void 0) return yield* refuseFallback("NoFallback", `plan "${plan.id}" has no server fallback`);
|
|
731
873
|
const form = yield* Effect.tryPromise({
|
|
732
874
|
try: () => request.formData(),
|
|
@@ -768,16 +910,13 @@ const handle = (request, config, plan, options) => Effect.scoped(Effect.gen(func
|
|
|
768
910
|
url: request.url,
|
|
769
911
|
...options
|
|
770
912
|
});
|
|
771
|
-
|
|
772
|
-
const
|
|
913
|
+
const { prepare } = options;
|
|
914
|
+
const start = prepare === void 0 ? started.model : yield* Effect.orDie(prepare(started.model, request));
|
|
915
|
+
if (!allowedTags(plan, start).has(tagOf(message))) return yield* refuseFallback("Unlisted", `the posted ${tagOf(message)} is not one an active Surface lists in its messages`);
|
|
916
|
+
const boot = plan.boot?.(start) ?? [];
|
|
773
917
|
const post = options.resources === void 0 ? Context.empty() : yield* Layer.build(options.resources);
|
|
774
|
-
const booted = yield* fold(config, post,
|
|
775
|
-
|
|
776
|
-
return yield* render(startingFrom(config, { model }), plan, {
|
|
777
|
-
buildId: options.buildId,
|
|
778
|
-
url: request.url,
|
|
779
|
-
...options.styles === void 0 ? {} : { styles: options.styles }
|
|
780
|
-
});
|
|
918
|
+
const booted = yield* fold(config, post, start, [], boot);
|
|
919
|
+
return yield* fold(config, post, booted, [message], []);
|
|
781
920
|
}));
|
|
782
921
|
const SSR = {
|
|
783
922
|
plan,
|
|
@@ -794,9 +933,9 @@ const SSR = {
|
|
|
794
933
|
handle,
|
|
795
934
|
serializeJsonScript,
|
|
796
935
|
sitemap,
|
|
797
|
-
robots
|
|
936
|
+
robots,
|
|
937
|
+
requestContext
|
|
798
938
|
};
|
|
799
|
-
|
|
800
|
-
export { BINDING_ATTRIBUTE, FALLBACK_FIELD, FOLDKIT_APP_ATTRIBUTE, FallbackRefused, META_ATTRIBUTE, RESUME_ATTRIBUTE, Resume, ResumeRefused, ResumeUnsafe, SLOT_ATTRIBUTE, SSR, STATIC_ATTRIBUTE, UNTIL_LIMIT_MS, metaMarkup };
|
|
939
|
+
export { BINDING_ATTRIBUTE, Diagnostics, FALLBACK_FIELD, FOLDKIT_APP_ATTRIBUTE, FallbackRefused, KEEP_ATTRIBUTE, META_ATTRIBUTE, RESUME_ATTRIBUTE, Resume, ResumeRefused, ResumeUnsafe, SLOT_ATTRIBUTE, SSR, STATIC_ATTRIBUTE, UNTIL_LIMIT_MS, metaMarkup };
|
|
801
940
|
|
|
802
|
-
//# sourceMappingURL=index.
|
|
941
|
+
//# sourceMappingURL=index.js.map
|