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.
Files changed (54) hide show
  1. package/README.md +496 -83
  2. package/dist/client-XnpTQZHw.d.ts +85 -0
  3. package/dist/client-XnpTQZHw.d.ts.map +1 -0
  4. package/dist/client.d.ts +5 -0
  5. package/dist/client.js +343 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/context.d.ts +2 -0
  8. package/dist/context.js +33 -0
  9. package/dist/context.js.map +1 -0
  10. package/dist/diagnostics.d.ts +2 -0
  11. package/dist/diagnostics.js +59 -0
  12. package/dist/diagnostics.js.map +1 -0
  13. package/dist/{index.d.mts → index.d.ts} +96 -25
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/{index.mjs → index.js} +247 -108
  16. package/dist/index.js.map +1 -0
  17. package/dist/keep.d.ts +34 -0
  18. package/dist/keep.d.ts.map +1 -0
  19. package/dist/keep.js +80 -0
  20. package/dist/keep.js.map +1 -0
  21. package/dist/listen-DaUPRKVR.d.ts +71 -0
  22. package/dist/listen-DaUPRKVR.d.ts.map +1 -0
  23. package/dist/listen.d.ts +2 -0
  24. package/dist/listen.js +187 -0
  25. package/dist/listen.js.map +1 -0
  26. package/dist/requestContext-D9nsbGSJ.d.ts +21 -0
  27. package/dist/requestContext-D9nsbGSJ.d.ts.map +1 -0
  28. package/dist/requestContext.d.ts +2 -0
  29. package/dist/requestContext.js +48 -0
  30. package/dist/requestContext.js.map +1 -0
  31. package/dist/resumable-BPN_qQHU.d.ts +107 -0
  32. package/dist/resumable-BPN_qQHU.d.ts.map +1 -0
  33. package/dist/resumable.d.ts +2 -0
  34. package/dist/resumable.js +401 -0
  35. package/dist/resumable.js.map +1 -0
  36. package/dist/{client-DkwAniQQ.d.mts → shared-DbupL3vO.d.ts} +233 -194
  37. package/dist/shared-DbupL3vO.d.ts.map +1 -0
  38. package/dist/shared.d.ts +2 -0
  39. package/dist/shared.js +347 -0
  40. package/dist/shared.js.map +1 -0
  41. package/dist/{vite.d.mts → vite.d.ts} +3 -5
  42. package/dist/vite.d.ts.map +1 -0
  43. package/dist/{vite.mjs → vite.js} +3 -4
  44. package/dist/vite.js.map +1 -0
  45. package/package.json +18 -18
  46. package/dist/client-DVG9_3rL.mjs +0 -1113
  47. package/dist/client-DVG9_3rL.mjs.map +0 -1
  48. package/dist/client-DkwAniQQ.d.mts.map +0 -1
  49. package/dist/client.d.mts +0 -2
  50. package/dist/client.mjs +0 -1
  51. package/dist/index.d.mts.map +0 -1
  52. package/dist/index.mjs.map +0 -1
  53. package/dist/vite.d.mts.map +0 -1
  54. package/dist/vite.mjs.map +0 -1
@@ -1,8 +1,11 @@
1
- import { C as UnnamedHandler, D as SLOT_ATTRIBUTE, E as ResumableBuilder, O as DecodedBinding, S as staticRegion, T as FALLBACK_FIELD, _ as metaMarkup, a as Loadable, b as serializeJsonScript, c as PlanSurface, d as ResumePart, f as ResumePlan, g as Start, h as STATIC_ATTRIBUTE, i as FOLDKIT_APP_ATTRIBUTE, k as EncodedBinding, l as RESUME_ATTRIBUTE, m as RouteMatch, o as META_ATTRIBUTE, p as ResumeRefused, r as UNTIL_LIMIT_MS, s as Meta, t as Resume, u as ResumableConfig, v as plan, w as BINDING_ATTRIBUTE, x as serving, y as resume } from "./client-DkwAniQQ.mjs";
2
- import { Cause, Effect, Layer, Schema } from "effect";
1
+ import { n as DecodedBinding, r as EncodedBinding } from "./listen-DaUPRKVR.js";
2
+ import { a as FALLBACK_FIELD, d as SLOT_ATTRIBUTE, n as BINDING_ATTRIBUTE, s as KEEP_ATTRIBUTE, u as ResumableBuilder } from "./resumable-BPN_qQHU.js";
3
+ import { C as metaMarkup, E as plan, G as PageContained, J as StaticRegionMissing, K as PageFailed, M as serving, P as staticRegion, U as BundleUnloaded, W as HandlerUnnamed, c as ResumableConfig, d as ResumeRefused, f as RouteMatch, h as Start, i as Meta, j as serializeJsonScript, k as resume, l as ResumePart, n as Loadable, o as PlanSurface, p as STATIC_ATTRIBUTE, q as SsrDiagnostic, r as META_ATTRIBUTE, s as RESUME_ATTRIBUTE, t as FOLDKIT_APP_ATTRIBUTE, u as ResumePlan, z as UnnamedHandler } from "./shared-DbupL3vO.js";
4
+ import { i as Resume, n as Diagnostics, o as UNTIL_LIMIT_MS, r as ReportDiagnostic, t as Diagnostic } from "./client-XnpTQZHw.js";
5
+ import { n as requestContext, t as RequestContext } from "./requestContext-D9nsbGSJ.js";
6
+ import { Cause, Effect, Layer, Option, Schema } from "effect";
7
+ import { Diagnostic as Diagnostic$1, MetadataSummary } from "foldkit-surface";
3
8
  import { EntryModule, RenderError, RenderedApplication } from "foldkit/experimental/server";
4
- import { MetadataSummary } from "foldkit-surface";
5
- //#region src/index.d.ts
6
9
  interface EnvelopeOptions {
7
10
  readonly route?: string | undefined;
8
11
  readonly match?: RouteMatch | undefined;
@@ -72,7 +75,7 @@ export interface PlanInspection {
72
75
  */
73
76
  declare const inspect: <Model, Fields extends Schema.Struct.Fields>(plan: ResumePlan<Model, Fields>, model: Model) => PlanInspection;
74
77
  declare const ResumeUnsafe_base: Schema.Class<ResumeUnsafe, Schema.TaggedStruct<"ResumeUnsafe", {
75
- readonly reason: Schema.Literals<readonly ["UndeclaredStartup", "Uncovered", "ViewDependsOnUnsentState", "DuplicateStaticRegion", "UngeneratablePath", "UnrestorablePart", "UnencodableBinding", "EagerStartRequired", "UndeclaredSurfaces", "BindingInStaticRegion", "VoidRootWithStyles"]>;
78
+ readonly reason: Schema.Literals<readonly ["UndeclaredStartup", "Uncovered", "ViewDependsOnUnsentState", "DuplicateStaticRegion", "UngeneratablePath", "UnrestorablePart", "UnencodableBinding", "EagerStartRequired", "UndeclaredSurfaces", "BindingInStaticRegion", "VoidRootWithStyles", "UnkeepableField"]>;
76
79
  readonly message: Schema.String;
77
80
  }>, Cause.YieldableError>;
78
81
  /** Why a server refused to render a page against its plan. */
@@ -114,9 +117,9 @@ declare const render: <Model, Fields extends Schema.Struct.Fields, Message = any
114
117
  readonly flags?: unknown;
115
118
  /**
116
119
  * Markup for the first paint, given what was rendered, carried as the
117
- * root's last child: keep it to `<style>`, which the browser drops on
118
- * hydration. Anything a head owns (`meta`, links) stays with `SSR.page`
119
- * and `SSR.generate`.
120
+ * root's last children: `<style>` (or a stylesheet `<link>`), which
121
+ * `SSR.hydrate` moves into the head before adopting the page. Anything
122
+ * else a head owns (`meta`) stays with `SSR.page` and `SSR.generate`.
120
123
  */
121
124
  readonly styles?: Head | undefined;
122
125
  }) => Effect.Effect<RenderedPage, RenderError | ResumeUnsafe>;
@@ -198,15 +201,18 @@ declare const generate: <Model, Fields extends Schema.Struct.Fields, const Paths
198
201
  * served by Node and Workers alike.
199
202
  *
200
203
  * `GET` and `HEAD` render the page against the plan, with the request's URL
201
- * and, when the application has Flags, `flags(request)`. `POST` goes to
204
+ * and, when the application has Flags, `flags(request)`; `prepare(model,
205
+ * request)` turns the Model `init` gave into the one rendered, a read made
206
+ * before the render (`Data.satisfy`). `POST` goes to
202
207
  * `SSR.handle` when the plan has a server fallback, and is answered `400` when
203
208
  * the post is unusable. `OPTIONS` is answered `204`, and any other method
204
209
  * `405`, each with the methods the entry answers in `allow` (`handleRequest`
205
210
  * passes every method through). A render that fails, or a plan the render
206
- * refuses, is answered `500` with the reason logged, never with a page the
207
- * browser cannot resume. `headers(request)` rides on the `Rendered` result, so
208
- * Foldkit's `toResponse` sets it over its own on the page: a cache policy, or
209
- * a CORS answer to a preflight.
211
+ * refuses, is answered `500` with the reason reported (`diagnostics`), never with a page the
212
+ * browser cannot resume. `headers(request, model)` is set over Foldkit's own
213
+ * on every answer: a cache policy, a CORS answer to a preflight, or a cookie
214
+ * written from the Model a post reached. `status(model, request)` gives a page
215
+ * its status from that Model, `404` for a route not found.
210
216
  *
211
217
  * The page is answered as `Rendered`: the rendered application carries the
212
218
  * envelope in its root, so the host injects it into its own template with
@@ -222,7 +228,26 @@ declare const generate: <Model, Fields extends Schema.Struct.Fields, const Paths
222
228
  declare const entry: <Model, Fields extends Schema.Struct.Fields, Message = any>(config: ResumableConfig<Model, Message>, plan: ResumePlan<Model, Fields>, options: {
223
229
  readonly buildId: string;
224
230
  readonly flags?: ((request: Request) => unknown | PromiseLike<unknown>) | undefined;
225
- readonly headers?: ((request: Request) => HeadersInit) | undefined;
231
+ /**
232
+ * The Model a request starts from, from the one `init` gave: what the page
233
+ * shows read first (`Data.satisfy`), or what the request carries (a
234
+ * cookie). Run for `GET`, `HEAD` and a posted form, before the post's
235
+ * Message is checked or `update` runs, so the entry is made once. A
236
+ * failure is answered `500` and reported; to draw the page unread
237
+ * instead, recover inside the Effect.
238
+ */
239
+ readonly prepare?: Prepare<Model> | undefined;
240
+ /**
241
+ * Headers set over the entry's own on every answer. `model` is the Model
242
+ * the server reached for a rendered page, a posted form's after `update`;
243
+ * it is `none` for an answer that rendered nothing (`OPTIONS`, `405`, a
244
+ * refused post, a `500`). So a cookie kept from the Model is written here.
245
+ * A name or value no `Headers` takes (a CR or LF in a value), a header that
246
+ * frames the response (`content-length`, `transfer-encoding`, `connection`
247
+ * and the like) or `location`, which only the entry writes, is answered
248
+ * `500` and reported, as `headers` that throw are.
249
+ */
250
+ readonly headers?: ((request: Request, model: Option.Option<Model>) => HeadersInit) | undefined;
226
251
  /**
227
252
  * A posted form's own services, from its request (who is asking, a
228
253
  * client bound to them), for the Commands the fallback runs; where one is
@@ -240,15 +265,54 @@ declare const entry: <Model, Fields extends Schema.Struct.Fields, Message = any>
240
265
  * form cannot carry a CSRF header; this is its check. A post with no
241
266
  * `Origin` or `Origin: null` (a page served with `Referrer-Policy:
242
267
  * no-referrer`) is taken when `Sec-Fetch-Site` says `same-origin`. Each must be an origin exactly as
243
- * a browser sends it (`https://example.com`).
268
+ * a browser sends it (`https://example.com`). `'same-origin'` takes a
269
+ * post from the origin the request was addressed to and no other, for a
270
+ * site served at addresses not known when the entry is made (a dev
271
+ * server's port). It compares with the request's own URL, so behind a
272
+ * proxy that terminates TLS (the server sees `http://`, the browser
273
+ * sends `Origin: https://`) or rewrites the host, every post is refused
274
+ * 403: there, name the public origin (`['https://example.com']`).
244
275
  */
245
- readonly origins?: ReadonlyArray<string> | undefined;
276
+ readonly origins?: ReadonlyArray<string> | 'same-origin' | undefined;
246
277
  /**
247
- * Markup for the first paint, carried as the rendered root's last child.
248
- * The host owns the template in dynamic serving, so this is where
249
- * per-page styles go; anything a head owns stays with `SSR.generate`.
278
+ * Markup for the first paint, carried as the rendered root's last
279
+ * children, which `SSR.hydrate` moves into the head. The host owns the
280
+ * template in dynamic serving, so this is where per-page styles go;
281
+ * anything else a head owns stays with `SSR.generate`.
250
282
  */
251
283
  readonly styles?: Head | undefined;
284
+ /**
285
+ * The status a rendered page is answered with, from the Model the server
286
+ * reached and the request: `404` for a route the Model says is not found.
287
+ * Omitted, every page is `200`. One outside `200`–`599`, or a `status`
288
+ * that throws, is answered `500` and reported, as a failed render is.
289
+ */
290
+ readonly status?: ((model: Model, request: Request) => number) | undefined;
291
+ /**
292
+ * How a posted form is answered. `'page'`, the default, is the page
293
+ * `update` reached, at the address the form posted to, so a reload posts
294
+ * again. `'redirect'` is a `303` to that address, which the browser then
295
+ * asks for with a `GET` (Post/Redirect/Get): what the post changed must
296
+ * outlive it where the next request reads it, a cookie `headers` writes
297
+ * from the Model the post reached. A refused post is still a `400`.
298
+ */
299
+ readonly answerPost?: 'page' | 'redirect' | undefined;
300
+ /**
301
+ * The paths a build prerenders through this entry (Foldkit's plugin,
302
+ * `ssr.build.prerender`), whose files a static host serves for every
303
+ * query. A request for one of them with no query records its path alone,
304
+ * as `SSR.generate` does, so the file resumes at `/?q=…` and the
305
+ * application starts from that URL. Export the same list as the entry
306
+ * module's `prerenderPaths`, which the plugin reads.
307
+ */
308
+ readonly prerendered?: ReadonlyArray<string> | undefined;
309
+ /**
310
+ * Where this request's diagnostics go, with the request: a request
311
+ * answered 500 (`PageFailed`), a handler the page cannot name, and what
312
+ * a posted form's Commands report. The reporter in the Effect context
313
+ * (`Diagnostics.Reporter`) when not given, by default the log.
314
+ */
315
+ readonly diagnostics?: ((diagnostic: Diagnostic$1, request: Request) => Effect.Effect<void>) | undefined;
252
316
  }) => EntryModule;
253
317
  declare const FallbackRefused_base: Schema.Class<FallbackRefused, Schema.TaggedStruct<"FallbackRefused", {
254
318
  readonly reason: Schema.Literals<readonly ["Missing", "Unreadable", "Invalid", "Unlisted", "NoFallback"]>;
@@ -256,26 +320,31 @@ declare const FallbackRefused_base: Schema.Class<FallbackRefused, Schema.TaggedS
256
320
  }>, Cause.YieldableError>;
257
321
  /** Why a posted form was not handled: the request, not the page or the plan, is at fault. */
258
322
  export declare class FallbackRefused extends FallbackRefused_base {}
323
+ /** Turns the Model `init` gave into the one a request starts from. */
324
+ export type Prepare<Model> = (model: Model, request: Request) => Effect.Effect<Model, unknown>;
259
325
  /**
260
326
  * Answers a form posted by a page rendered with `fallback: 'server'`: the
261
327
  * posted Message is decoded through the plan's Message Schema, with posted
262
328
  * fields of the same names as its own overriding them, and must be one the
263
329
  * Surfaces active for the request's Model may send. The server then rebuilds
264
- * the Model the browser had, `init` and then the plan's `boot` with every
330
+ * the Model the browser had, `init`, `prepare` and then the plan's `boot` with every
265
331
  * Command it leads to, runs `update` with the Message and every Command that
266
332
  * follows, and renders the result as a fresh page.
267
333
  */
268
- declare const handle: <Model, Fields extends Schema.Struct.Fields, Message = any>(request: Request, config: ResumableConfig<Model, Message>, plan: ResumePlan<Model, Fields>, options: {
334
+ declare const handle: <Model, Fields extends Schema.Struct.Fields, Message = any>(request: Request, config: ResumableConfig<Model, Message>, plan: ResumePlan<Model, Fields>, options: HandleOptions<Model>) => Effect.Effect<RenderedPage, RenderError | ResumeUnsafe | FallbackRefused>;
335
+ interface HandleOptions<Model> {
269
336
  readonly buildId: string;
270
337
  readonly flags?: unknown;
271
338
  readonly styles?: Head | undefined;
339
+ /** The Model the post starts from, from `init`'s, as `SSR.entry`'s `prepare`. */
340
+ readonly prepare?: Prepare<Model> | undefined;
272
341
  /**
273
342
  * This post's own services, beside the config's `resources`: who is
274
343
  * asking, a client bound to them. Built once for the post; where both
275
344
  * provide a service, the post's is the one its Commands get.
276
345
  */
277
346
  readonly resources?: Layer.Layer<never, never, never> | undefined;
278
- }) => Effect.Effect<RenderedPage, RenderError | ResumeUnsafe | FallbackRefused>;
347
+ }
279
348
  export declare const SSR: {
280
349
  plan: typeof plan;
281
350
  envelope: typeof envelope;
@@ -288,6 +357,8 @@ export declare const SSR: {
288
357
  readonly otherwise?: 'render' | undefined;
289
358
  readonly until?: ((model: Model) => boolean) | undefined;
290
359
  readonly fresh?: ((version: unknown) => boolean) | undefined;
360
+ readonly prepare?: ((model: Model) => Effect.Effect<Model>) | undefined;
361
+ readonly diagnostics?: import("foldkit-surface").ReportDiagnostic | undefined;
291
362
  }) => void;
292
363
  inspect: typeof inspect;
293
364
  static: typeof staticRegion;
@@ -298,7 +369,7 @@ export declare const SSR: {
298
369
  serializeJsonScript: typeof serializeJsonScript;
299
370
  sitemap: typeof sitemap;
300
371
  robots: typeof robots;
372
+ requestContext: typeof requestContext;
301
373
  };
302
- //#endregion
303
- export { BINDING_ATTRIBUTE, type DecodedBinding, FALLBACK_FIELD, FOLDKIT_APP_ATTRIBUTE, type Loadable, META_ATTRIBUTE, type Meta, type PlanSurface, RESUME_ATTRIBUTE, type ResumableBuilder, type ResumableConfig, Resume, type ResumePart, type ResumePlan, ResumeRefused, SLOT_ATTRIBUTE, STATIC_ATTRIBUTE, type Start, UNTIL_LIMIT_MS, type UnnamedHandler, metaMarkup };
304
- //# sourceMappingURL=index.d.mts.map
374
+ export { BINDING_ATTRIBUTE, type BundleUnloaded, type DecodedBinding, type Diagnostic, Diagnostics, FALLBACK_FIELD, FOLDKIT_APP_ATTRIBUTE, type HandlerUnnamed, KEEP_ATTRIBUTE, type Loadable, META_ATTRIBUTE, type Meta, type PageContained, type PageFailed, type PlanSurface, RESUME_ATTRIBUTE, type ReportDiagnostic, type RequestContext, type ResumableBuilder, type ResumableConfig, Resume, type ResumePart, type ResumePlan, ResumeRefused, SLOT_ATTRIBUTE, STATIC_ATTRIBUTE, type SsrDiagnostic, type Start, type StaticRegionMissing, UNTIL_LIMIT_MS, type UnnamedHandler, metaMarkup };
375
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":["Diagnostic"],"sources":["../src/index.ts"],"mappings":";;;;;;;;UAgGU;WACC;WACA,QAAQ;WACR,WAAW,cAAc;;WAEzB,SAAS;;;;;;;;;cAUd,WAAY,OAAO,eAAe,OAAO,OAAO,QAAM,QAClD,WAAW,OAAO,SAAO,OAC1B,OAAK,UACH;;YAgHC;;iBAGK;WACN;;WAEA;;WAEA;;;;;WAKA;aAEM;aACA;eAAwB;eAAuB,OAAO;;;;WAI5D;aAAwB;aAAuB,OAAO;;;WAEtD,OAAO;aAAyB;aAAuB,OAAO;;;;;;WAK9D,WAAW,cAAc;;WAEzB;;;iBAIM;WACN;WACA,OAAO;WACP,OAAO;;WAEP,OAAO;WACP,UAAU,cAAc;;;;;;;cA4C7B,UAAW,OAAO,eAAe,OAAO,OAAO,QAAM,MACnD,WAAW,OAAO,SAAO,OACxB,UACN;;;;;;qBAyKU,qBAAqB;;iBAqJjB;;;;;;WAMN,UAAU;;WAEV;;WAEA;;;;;WAKA,SAAS,cAAc;;;;;;;;;;;;;;;cAgB5B,SAAU,OAAO,eAAe,OAAO,OAAO,QAAQ,eAAa,QAC/D,gBAAgB,OAAO,UAAQ,MACjC,WAAW,OAAO,SAAO;WAEpB;WACA;WACA;;;;;;;WAOA,SAAS;MAEnB,OAAO,OAAO,cAAc,cAAc;;;;;;;YAqLjC,QAAQ,UAAU;;;;;;;cAwDxB,OAAI,kBACQ;WACG,UAAU;WAA8B;GAAc;WACrD,OAAO;;;iBAQZ;WACN;WACA;WACA;;;;;;YAOC,eAAe,cAAc,qCAC7B,WAAW,QAAQ;;;;;;;;cAyBzB,UAAO,OACJ;WAAyB;WAAuB;IAAgC;WACnE;;;;;;cA4BhB,SAAM;WACD;WACA;WACA,WAAW;;;;;;;;;;;;;cAmDhB,WACJ,OACA,eAAe,OAAO,OAAO,cACvB,cAAc,uBACpB,eAAa,QAEL,gBAAgB,OAAO,UAAQ,MACjC,WAAW,OAAO,SAAO;WAEpB;WACA;WACA;WACA,OAAO;WACP,UAAU;WACV,OAAO;MAEjB,OAAO,OAAO,eAAe,QAAQ,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA0EhD,QAAS,OAAO,eAAe,OAAO,OAAO,QAAQ,eAAa,QAC9D,gBAAgB,OAAO,UAAQ,MACjC,WAAW,OAAO,SAAO;WAEpB;WACA,UAAU,SAAS,sBAAsB;;;;;;;;;WASzC,UAAU,QAAQ;;;;;;;;;;;WAWlB,YAAY,SAAS,SAAS,OAAO,OAAO,OAAO,WAAW;;;;;;;;;WAS9D,cAAc,SAAS,YAAY,MAAM;;;;;;;;;;;;;;;;;WAiBzC,UAAU;;;;;;;WAOV,SAAS;;;;;;;WAOT,WAAW,OAAO,OAAO,SAAS;;;;;;;;;WASlC;;;;;;;;;WASA,cAAc;;;;;;;WAOd,gBACH,YAAYA,cAAY,SAAS,YAAY,OAAO;MAG3D;;;;;;qBAmOU,wBAAwB;;YA4GzB,QAAQ,UAAU,OAAO,OAAO,SAAS,YAAY,OAAO,OAAO;;;;;;;;;;cA2BzE,SAAU,OAAO,eAAe,OAAO,OAAO,QAAQ,eAAa,SAC9D,SAAO,QACR,gBAAgB,OAAO,UAAQ,MACjC,WAAW,OAAO,SAAO,SACtB,cAAc,WACtB,OAAO,OAAO,cAAc,cAAc,eAAe;UAGlD,cAAc;WACb;WACA;WACA,SAAS;;WAET,UAAU,QAAQ;;;;;;WAMlB,YAAY,MAAM;;qBAsGhB"}