@k8ordo/framework 1.4.4 → 2.3.1

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 (55) hide show
  1. package/README.md +1 -1
  2. package/dist/generated.d.mts +23 -2
  3. package/dist/runtime/app-router.d.mts +10 -10
  4. package/dist/runtime/app-router.mjs +119 -83
  5. package/dist/runtime/browser-payloads.mjs +2 -1
  6. package/dist/runtime/client-files.d.mts +11 -0
  7. package/dist/runtime/client-files.mjs +42 -0
  8. package/dist/runtime/client-ready.d.mts +3 -2
  9. package/dist/runtime/document-root.mjs +1 -1
  10. package/dist/runtime/entry.browser.d.mts +1 -1
  11. package/dist/runtime/entry.browser.mjs +8 -12
  12. package/dist/runtime/entry.rsc.mjs +148 -102
  13. package/dist/runtime/entry.ssr.d.mts +21 -1
  14. package/dist/runtime/entry.ssr.mjs +10 -13
  15. package/dist/runtime/head-scripts.d.mts +18 -0
  16. package/dist/runtime/head-scripts.mjs +159 -0
  17. package/dist/runtime/hydrating-pathname.d.mts +16 -0
  18. package/dist/runtime/hydrating-pathname.mjs +26 -0
  19. package/dist/runtime/mount.d.mts +16 -11
  20. package/dist/runtime/mount.mjs +29 -18
  21. package/dist/runtime/payload.d.mts +22 -23
  22. package/dist/runtime/payload.mjs +11 -16
  23. package/dist/runtime/prefetch.d.mts +60 -25
  24. package/dist/runtime/prefetch.mjs +166 -38
  25. package/dist/runtime/recover.d.mts +2 -0
  26. package/dist/runtime/recover.mjs +11 -7
  27. package/dist/runtime/redirect.d.mts +20 -14
  28. package/dist/runtime/redirect.mjs +42 -15
  29. package/dist/runtime/request-scope.d.mts +1 -1
  30. package/dist/runtime/request-scope.mjs +11 -23
  31. package/dist/runtime/server.d.mts +6 -0
  32. package/dist/runtime/server.mjs +3 -0
  33. package/dist/runtime/stylesheets.d.mts +13 -0
  34. package/dist/runtime/stylesheets.mjs +15 -0
  35. package/dist/serve.d.mts +1 -0
  36. package/dist/serve.mjs +5 -11
  37. package/dist/static-file-BAjwBMMl.mjs +1274 -0
  38. package/dist/{static-host-C6D_11Gp.mjs → static-host-B-1fAfAw.mjs} +49 -74
  39. package/dist/vercel.d.mts +0 -1
  40. package/dist/vercel.mjs +20 -20
  41. package/dist/vite.mjs +228 -1518
  42. package/docs/GUIDE.md +3 -2
  43. package/docs/llms.txt +8 -4
  44. package/docs/references/csp.md +11 -9
  45. package/docs/references/deploy.md +17 -20
  46. package/docs/references/errors.md +0 -34
  47. package/docs/references/params.md +5 -5
  48. package/docs/references/routing.md +94 -31
  49. package/package.json +6 -6
  50. package/dist/runtime/redirect-file.d.mts +0 -20
  51. package/dist/runtime/redirect-file.mjs +0 -42
  52. package/dist/runtime/tree-pathname.d.mts +0 -2
  53. package/dist/runtime/tree-pathname.mjs +0 -11
  54. package/dist/server.d.mts +0 -110
  55. package/dist/server.mjs +0 -97
@@ -1,19 +1,8 @@
1
+ import { normalizePathname } from "@k8ordo/router";
1
2
  // src/runtime/redirect.ts
2
- /**
3
- * A redirect thrown from a Server Action. Branded with a registry symbol
4
- * rather than a class: the framework bundles this module once into the
5
- * entry an application imports `redirect()` from and once into the runtime
6
- * it copies beside it (where the handler catches it), and two copies of a
7
- * class are two classes. `Symbol.for` is the one identity both share.
8
- */
9
- const BRAND = Symbol.for("k8ordo.redirect");
10
- /**
11
- * An `Error` for the linter's and the stack's sake, recognised by the brand
12
- * and never by `instanceof`.
13
- */
3
+ /** A redirect thrown from a Server Action. */
14
4
  var Redirect = class extends Error {
15
5
  to;
16
- [BRAND] = true;
17
6
  constructor(to) {
18
7
  super(`redirect to ${to}`);
19
8
  this.to = to;
@@ -30,5 +19,43 @@ var Redirect = class extends Error {
30
19
  const redirect = (to) => {
31
20
  throw new Redirect(to);
32
21
  };
33
- const isRedirect = (value) => typeof value === "object" && value !== null && value[BRAND] === true;
34
- export { Redirect, isRedirect, redirect };
22
+ const resolveTarget = (target, segments) => {
23
+ const { to, permanent } = typeof target === "string" ? {
24
+ to: target,
25
+ permanent: false
26
+ } : target;
27
+ return {
28
+ to: to.split("/").map((segment) => {
29
+ if (!segment.startsWith(":")) return segment;
30
+ const value = segments[segment.slice(1)];
31
+ if (value === void 0) throw new TypeError(`redirect target "${to}" needs a value for "${segment}"`);
32
+ return value;
33
+ }).join("/"),
34
+ permanent: permanent ?? false
35
+ };
36
+ };
37
+ /**
38
+ * Where a `redirect.ts` sends a pathname the table matched to its pattern,
39
+ * with the matched params filled into the target, so `/:locale/legacy` can
40
+ * send to `/:locale/new`; `null` for a pattern no redirect declares. Which
41
+ * pattern answers is the table's to say — the redirect holds its place there
42
+ * — so this only reads the params again: the router hands them decoded, and a
43
+ * target moves a segment as the URL spelled it. The pathname is normalized as
44
+ * the table normalizes it, so `/old/` is `/old` here too.
45
+ */
46
+ const resolveRedirects = (declared) => {
47
+ const matchers = new Map(Object.entries(declared).map(([pattern, target]) => [pattern, {
48
+ matcher: new URLPattern({ pathname: pattern }),
49
+ target
50
+ }]));
51
+ return (pattern, pathname) => {
52
+ const declaredHere = matchers.get(pattern);
53
+ if (declaredHere === void 0) return null;
54
+ const result = declaredHere.matcher.exec({ pathname: normalizePathname(pathname) });
55
+ if (result === null) return null;
56
+ const segments = {};
57
+ for (const [name, value] of Object.entries(result.pathname.groups)) if (!/^\d+$/u.test(name) && value !== void 0) segments[name] = value;
58
+ return resolveTarget(declaredHere.target, segments);
59
+ };
60
+ };
61
+ export { Redirect, redirect, resolveRedirects };
@@ -48,7 +48,7 @@ export declare const requestHeaders: () => Headers;
48
48
  /**
49
49
  * The nonce this answer's inline scripts carry: the framework's own — the
50
50
  * payload written into the HTML, React's — and any the application signs
51
- * with it, as `<ColorSchemeProvider nonce={nonce()}>`. A new one per
51
+ * with it, as `<ColorSchemeScript nonce={nonce()} />`. A new one per
52
52
  * request. Readable wherever the request is in progress, the render
53
53
  * included: signing a script is not writing the response. A guard names it
54
54
  * in the `Content-Security-Policy` it writes; a build into files turns what
@@ -2,24 +2,12 @@ import { createCookies } from "./cookies.mjs";
2
2
  import { parseCookies } from "./request.mjs";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
4
  // src/runtime/request-scope.ts
5
- /**
6
- * The framework bundles this module twice — into the runtime the handler
7
- * is built from, and into the entry an application imports the response API
8
- * from — and both copies have to find the one request in progress. So the
9
- * storage lives on `globalThis` under a registry symbol, as `@k8ordo/i18n`'s
10
- * locale storage does, rather than in either copy.
11
- */
12
- const KEY = Symbol.for("k8ordo.request");
13
- const storage = () => {
14
- const global = globalThis;
15
- global[KEY] ??= new AsyncLocalStorage();
16
- return global[KEY];
17
- };
5
+ const storage = new AsyncLocalStorage();
18
6
  const freshNonce = () => btoa(String.fromCodePoint(...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))));
19
7
  /** Runs `fn` as the handling of `request`, starting in the render. */
20
8
  const withRequest = (request, fn) => {
21
9
  const jar = createCookies(parseCookies(request.headers.get("cookie")), new URL(request.url));
22
- return storage().run({
10
+ return storage.run({
23
11
  request,
24
12
  phase: "render",
25
13
  cookies: jar.cookies,
@@ -32,9 +20,9 @@ const withRequest = (request, fn) => {
32
20
  };
33
21
  /** Runs `fn` as `phase` of the request in progress. */
34
22
  const inPhase = (phase, fn) => {
35
- const current = storage().getStore();
23
+ const current = storage.getStore();
36
24
  if (current === void 0) return fn();
37
- return storage().run({
25
+ return storage.run({
38
26
  ...current,
39
27
  phase
40
28
  }, fn);
@@ -46,9 +34,9 @@ const inPhase = (phase, fn) => {
46
34
  * never compiles is only seen here, when it is called.
47
35
  */
48
36
  const writingFile = (fn) => {
49
- const current = storage().getStore();
37
+ const current = storage.getStore();
50
38
  if (current === void 0) return fn();
51
- return storage().run({
39
+ return storage.run({
52
40
  ...current,
53
41
  file: true
54
42
  }, fn);
@@ -60,7 +48,7 @@ const fileRefusal = (caller) => /* @__PURE__ */ new Error(`${caller} answers a r
60
48
  * and a fetched response hand out headers that cannot be written.
61
49
  */
62
50
  const answer = (response) => {
63
- const outgoing = storage().getStore()?.outgoing;
51
+ const outgoing = storage.getStore()?.outgoing;
64
52
  const cookies = outgoing?.cookies() ?? [];
65
53
  if (outgoing === void 0 || [...outgoing.headers].length === 0 && cookies.length === 0) return response;
66
54
  const headers = new Headers(response.headers);
@@ -78,7 +66,7 @@ const answer = (response) => {
78
66
  * something that answers it runs.
79
67
  */
80
68
  const answering = (caller) => {
81
- const scope = storage().getStore();
69
+ const scope = storage.getStore();
82
70
  if (scope === void 0) throw new Error(`${caller} needs a request — call it from a guard.ts, a route.ts or a Server Action, while the request is being answered`);
83
71
  if (scope.file === true) throw fileRefusal(caller);
84
72
  if (scope.phase === "render") throw new Error(`${caller} belongs to what answers the request, and a page is a render — call it from a guard.ts, a route.ts or a Server Action; a page reads the request from its props`);
@@ -109,14 +97,14 @@ const requestHeaders = () => answering("requestHeaders()").request.headers;
109
97
  /**
110
98
  * The nonce this answer's inline scripts carry: the framework's own — the
111
99
  * payload written into the HTML, React's — and any the application signs
112
- * with it, as `<ColorSchemeProvider nonce={nonce()}>`. A new one per
100
+ * with it, as `<ColorSchemeScript nonce={nonce()} />`. A new one per
113
101
  * request. Readable wherever the request is in progress, the render
114
102
  * included: signing a script is not writing the response. A guard names it
115
103
  * in the `Content-Security-Policy` it writes; a build into files turns what
116
104
  * carries it into hashes.
117
105
  */
118
106
  const nonce = () => {
119
- const scope = storage().getStore();
107
+ const scope = storage.getStore();
120
108
  if (scope === void 0) throw new Error("nonce() needs a request — call it while one is answered: from a guard.ts, a layout or page, or a Server Action");
121
109
  if (scope.file === true) throw fileRefusal("nonce()");
122
110
  return scope.nonce;
@@ -126,7 +114,7 @@ const nonce = () => {
126
114
  * build into files turns what it signed into hashes.
127
115
  */
128
116
  const signingNonce = () => {
129
- const scope = storage().getStore();
117
+ const scope = storage.getStore();
130
118
  if (scope === void 0) throw new Error("the handler signs its scripts while a request is answered");
131
119
  return scope.nonce;
132
120
  };
@@ -0,0 +1,6 @@
1
+ import { CookieOptions, CookieScope, Cookies } from "./cookies.mjs";
2
+ import { Guard, GuardContext } from "./guard.mjs";
3
+ import { RedirectTarget, redirect } from "./redirect.mjs";
4
+ import { RouteRequest } from "./request.mjs";
5
+ import { cookies, nonce, requestHeaders, responseHeaders } from "./request-scope.mjs";
6
+ export { type CookieOptions, type CookieScope, type Cookies, type Guard, type GuardContext, type RedirectTarget, type RouteRequest, cookies, nonce, redirect, requestHeaders, responseHeaders };
@@ -0,0 +1,3 @@
1
+ import { cookies, nonce, requestHeaders, responseHeaders } from "./request-scope.mjs";
2
+ import { redirect } from "./redirect.mjs";
3
+ export { cookies, nonce, redirect, requestHeaders, responseHeaders };
@@ -0,0 +1,13 @@
1
+ /** The precedence the RSC plugin links a Server Component's stylesheet with. */
2
+ export declare const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
3
+ /** The precedence the RSC plugin links a client component's stylesheet with. */
4
+ export declare const CLIENT_STYLESHEETS = "vite-rsc/client-reference";
5
+ /**
6
+ * The `href` of the one `<style>` the HTML render writes itself, empty, under
7
+ * `SERVER_STYLESHEETS`. React orders the precedences in `<head>` as each is
8
+ * first asked for, and puts a stylesheet loaded later after the last of its
9
+ * own; asked for first, this keeps the Server Components' stylesheets before
10
+ * the client components'. Under mode: 'static' it carries no nonce, and the
11
+ * build names it by hash where the policy decides a style element.
12
+ */
13
+ export declare const STYLESHEET_ORDER_HREF = "k8ordo-stylesheet-order";
@@ -0,0 +1,15 @@
1
+ // src/runtime/stylesheets.ts
2
+ /** The precedence the RSC plugin links a Server Component's stylesheet with. */
3
+ const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
4
+ /** The precedence the RSC plugin links a client component's stylesheet with. */
5
+ const CLIENT_STYLESHEETS = "vite-rsc/client-reference";
6
+ /**
7
+ * The `href` of the one `<style>` the HTML render writes itself, empty, under
8
+ * `SERVER_STYLESHEETS`. React orders the precedences in `<head>` as each is
9
+ * first asked for, and puts a stylesheet loaded later after the last of its
10
+ * own; asked for first, this keeps the Server Components' stylesheets before
11
+ * the client components'. Under mode: 'static' it carries no nonce, and the
12
+ * build names it by hash where the policy decides a style element.
13
+ */
14
+ const STYLESHEET_ORDER_HREF = "k8ordo-stylesheet-order";
15
+ export { CLIENT_STYLESHEETS, SERVER_STYLESHEETS, STYLESHEET_ORDER_HREF };
package/dist/serve.d.mts CHANGED
@@ -8,6 +8,7 @@ export type ServeOptions = {
8
8
  /** What `serve` hands back: where it listens, and how to stop it. */
9
9
  export type Server = {
10
10
  readonly port: number;
11
+ /** The address it listens on, which `localhost` may have made `[::1]`. */
11
12
  readonly url: string;
12
13
  readonly close: () => Promise<void>;
13
14
  };
package/dist/serve.mjs CHANGED
@@ -1,11 +1,12 @@
1
- import { d as ENCODINGS, f as SUFFIX, h as streamingCompressor, m as negotiateEncoding, n as resolveStatic, p as isCompressible, r as safeJoin, t as readRules } from "./static-host-C6D_11Gp.mjs";
1
+ import { n as safeJoin, t as isFile } from "./static-file-BAjwBMMl.mjs";
2
+ import { f as ENCODINGS, g as streamingCompressor, h as negotiateEncoding, m as isCompressible, n as resolveStatic, p as SUFFIX, t as readRules } from "./static-host-B-1fAfAw.mjs";
2
3
  import { withoutBase } from "@k8ordo/router";
3
4
  import { pathToFileURL } from "node:url";
4
5
  import { stat } from "node:fs/promises";
5
6
  import path from "node:path";
7
+ import { createReadStream } from "node:fs";
6
8
  import { contentType } from "mime-types";
7
9
  import { createHash } from "node:crypto";
8
- import { createReadStream } from "node:fs";
9
10
  import { createServer } from "node:http";
10
11
  import { Readable } from "node:stream";
11
12
  import { pipeline } from "node:stream/promises";
@@ -61,13 +62,6 @@ const urlOf = (incoming) => {
61
62
  if (!target.startsWith("/") || !AUTHORITY.test(host)) return null;
62
63
  return URL.parse(`http://${host}${target}`);
63
64
  };
64
- const isFile = async (candidate) => {
65
- try {
66
- return (await stat(candidate)).isFile();
67
- } catch {
68
- return false;
69
- }
70
- };
71
65
  const fileFor = async (root, pathname) => {
72
66
  const resolved = safeJoin(root, pathname);
73
67
  if (resolved === null) return null;
@@ -260,8 +254,8 @@ const serve = async (options = {}) => {
260
254
  await new Promise((resolve) => {
261
255
  server.listen(options.port ?? 3e3, host, resolve);
262
256
  });
263
- const { port } = server.address();
264
- const url = `http://${host}:${String(port)}`;
257
+ const { address, family, port } = server.address();
258
+ const url = `http://${family === "IPv6" ? `[${address}]` : address}:${String(port)}`;
265
259
  console.warn(`k8ordo: serving ${dist} on ${url}`);
266
260
  return {
267
261
  port,