@krate/runtime 0.5.0-rc.5 → 0.5.0-rc.6

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 (78) hide show
  1. package/dist/actions.js +10 -1
  2. package/dist/cache-scope.d.ts +2 -0
  3. package/dist/cache-scope.js +6 -1
  4. package/dist/context.d.ts +5 -0
  5. package/dist/context.js +28 -17
  6. package/dist/flight-props.d.ts +7 -0
  7. package/dist/flight-props.js +105 -0
  8. package/dist/hints.d.ts +25 -2
  9. package/dist/hints.js +148 -74
  10. package/dist/index.d.ts +1 -0
  11. package/dist/krate-core.js +1 -1
  12. package/dist/krate-f-actions.js +1 -1
  13. package/dist/krate-f-list.js +1 -1
  14. package/dist/krate-f-navigation.js +1 -1
  15. package/dist/krate-features.json +24 -0
  16. package/dist/krate-hydrate-lite.js +5 -5
  17. package/dist/krate-hydrate.js +5 -5
  18. package/dist/krate-runtime.js +5 -5
  19. package/dist/list.d.ts +14 -4
  20. package/dist/list.js +4 -5
  21. package/dist/navigation-react.d.ts +13 -0
  22. package/dist/navigation-react.js +86 -0
  23. package/dist/navigation.d.ts +19 -0
  24. package/dist/navigation.js +35 -0
  25. package/dist/react-compat.d.ts +26 -0
  26. package/dist/react-compat.js +292 -0
  27. package/dist/server-handler.js +74 -6
  28. package/dist/server.d.ts +17 -10
  29. package/dist/server.js +400 -94
  30. package/package.json +15 -2
  31. package/dist/actions.test.d.ts +0 -1
  32. package/dist/actions.test.js +0 -75
  33. package/dist/after.test.d.ts +0 -1
  34. package/dist/after.test.js +0 -58
  35. package/dist/builtin-tags.test.d.ts +0 -1
  36. package/dist/builtin-tags.test.js +0 -12
  37. package/dist/cache-adapter.test.d.ts +0 -1
  38. package/dist/cache-adapter.test.js +0 -45
  39. package/dist/cache.test.d.ts +0 -1
  40. package/dist/cache.test.js +0 -37
  41. package/dist/events.test.d.ts +0 -1
  42. package/dist/events.test.js +0 -42
  43. package/dist/handler-html.test.d.ts +0 -1
  44. package/dist/handler-html.test.js +0 -55
  45. package/dist/handler-segments.test.d.ts +0 -1
  46. package/dist/handler-segments.test.js +0 -25
  47. package/dist/head-tags.test.d.ts +0 -1
  48. package/dist/head-tags.test.js +0 -24
  49. package/dist/inline-action-token.test.d.ts +0 -1
  50. package/dist/inline-action-token.test.js +0 -31
  51. package/dist/jsx-svg-types.test.d.ts +0 -1
  52. package/dist/jsx-svg-types.test.js +0 -58
  53. package/dist/link-base-path.test.d.ts +0 -1
  54. package/dist/link-base-path.test.js +0 -28
  55. package/dist/node-server.test.d.ts +0 -1
  56. package/dist/node-server.test.js +0 -138
  57. package/dist/platform.test.d.ts +0 -1
  58. package/dist/platform.test.js +0 -44
  59. package/dist/router-nav.test.d.ts +0 -1
  60. package/dist/router-nav.test.js +0 -130
  61. package/dist/server-handler.test.d.ts +0 -1
  62. package/dist/server-handler.test.js +0 -338
  63. package/dist/server-renderer.test.d.ts +0 -1
  64. package/dist/server-renderer.test.js +0 -162
  65. package/dist/server.test.d.ts +0 -1
  66. package/dist/server.test.js +0 -91
  67. package/dist/sha256.test.d.ts +0 -1
  68. package/dist/sha256.test.js +0 -13
  69. package/dist/signal.test.d.ts +0 -1
  70. package/dist/signal.test.js +0 -68
  71. package/dist/site-config.test.d.ts +0 -1
  72. package/dist/site-config.test.js +0 -30
  73. package/dist/slot.test.d.ts +0 -1
  74. package/dist/slot.test.js +0 -25
  75. package/dist/ssr-core-behavior.test.d.ts +0 -1
  76. package/dist/ssr-core-behavior.test.js +0 -108
  77. package/dist/testing.test.d.ts +0 -1
  78. package/dist/testing.test.js +0 -73
@@ -467,15 +467,23 @@ export function createKrateHandler(options) {
467
467
  let next = false;
468
468
  let rewrite = "";
469
469
  const hdrs = [];
470
+ let keep = null;
471
+ const vals = new Map();
470
472
  for (const [k, v] of headerPairs(result.headers)) {
471
473
  if (k === "x-middleware-next")
472
474
  next = true;
473
475
  else if (k === "x-middleware-rewrite")
474
476
  rewrite = v;
477
+ else if (k === "x-middleware-override-headers")
478
+ keep = new Set(v.split(",").map((n) => n.trim().toLowerCase()).filter(Boolean));
479
+ else if (k.startsWith("x-middleware-request-"))
480
+ vals.set(k.slice("x-middleware-request-".length), v);
475
481
  else
476
482
  hdrs.push([k, v]);
477
483
  }
478
484
  if (next || rewrite) {
485
+ if (keep)
486
+ overrideRequestHeaders(rs, keep, vals);
479
487
  writeHeaderPairs(rs.res, hdrs);
480
488
  if (rewrite)
481
489
  applyRewrite(rs, rewrite);
@@ -488,6 +496,23 @@ export function createKrateHandler(options) {
488
496
  status = 500;
489
497
  return finish(rs, status, rs.method === "HEAD" || !body ? null : body);
490
498
  }
499
+ /** NextResponse.next({ request: { headers } }): the downstream request keeps only the listed headers, with the given values. */
500
+ function overrideRequestHeaders(rs, keep, vals) {
501
+ const edit = (h) => {
502
+ const out = new Headers();
503
+ h.forEach((v, k) => {
504
+ if (keep.has(k.toLowerCase()))
505
+ out.set(k, v);
506
+ });
507
+ for (const [k, v] of vals)
508
+ if (keep.has(k) && k !== "host")
509
+ out.set(k, v);
510
+ return out;
511
+ };
512
+ const shared = rs.headers === rs.request.headers;
513
+ rs.request = new Request(rs.request, { headers: edit(rs.request.headers) });
514
+ rs.headers = shared ? rs.request.headers : edit(rs.headers);
515
+ }
491
516
  function middlewareFailed(rs, err) {
492
517
  console.error(`[krate] middleware error: ${rs.method} ${rs.path}: ${(err && err.stack) || err}`);
493
518
  // Fail closed: continuing would turn a broken auth check into an unauthenticated request.
@@ -651,8 +676,8 @@ export function createKrateHandler(options) {
651
676
  return notFoundText(rs, true);
652
677
  }
653
678
  /** trailingSlash "never": a page URL ending in a slash redirects to the slashless URL. */
654
- function redirectToSlashless(rs) {
655
- const p = rs.path;
679
+ function redirectToSlashless(rs, requested = rs.path) {
680
+ const p = requested;
656
681
  if (slashMode !== "never" || p.length < 2 || !p.endsWith("/") || notFoundPageURL(p))
657
682
  return null;
658
683
  if (rs.method !== "GET" && rs.method !== "HEAD")
@@ -1492,7 +1517,7 @@ export function createKrateHandler(options) {
1492
1517
  }), basePath);
1493
1518
  const scope = { kind: "api", paths: [], tags: [] };
1494
1519
  const work = async () => {
1495
- const res = await revalidateStore.run(scope, async () => toResponse(await fn(request, { params: apiRouteParams(file, rs.path) })));
1520
+ const res = await revalidateStore.run(scope, () => runRoute(request, async () => toResponse(await fn(request, { params: apiRouteParams(file, rs.path) }))));
1496
1521
  await flushRevalidate(scope);
1497
1522
  return res;
1498
1523
  };
@@ -1629,6 +1654,29 @@ export function createKrateHandler(options) {
1629
1654
  };
1630
1655
  return { actionId: id, url: urlPath, method: "POST", headers, cookies, setCookies };
1631
1656
  }
1657
+ // Runs an API route inside the action store so headers(), cookies() and
1658
+ // redirect() work in it like in a server action: cookies it sets are added to
1659
+ // the response and a redirect() signal becomes a 307.
1660
+ async function runRoute(request, run) {
1661
+ const ctx = makeActionContext(request.headers, "", new URL(request.url).pathname);
1662
+ ctx.method = request.method;
1663
+ let out;
1664
+ try {
1665
+ out = await actionStore.run(ctx, run);
1666
+ }
1667
+ catch (e) {
1668
+ if (!e || typeof e.__krateRedirect !== "string")
1669
+ throw e;
1670
+ out = new Response(null, { status: 307, headers: { location: e.__krateRedirect, "cache-control": "no-store" } });
1671
+ }
1672
+ if (ctx.setCookies.length > 0) {
1673
+ const h = new Headers(out.headers);
1674
+ for (const c of ctx.setCookies)
1675
+ h.append("set-cookie", c);
1676
+ out = new Response(out.body, { status: out.status, statusText: out.statusText, headers: h });
1677
+ }
1678
+ return out;
1679
+ }
1632
1680
  function safeActionRedirect(value) {
1633
1681
  if (typeof value !== "string")
1634
1682
  return "";
@@ -1639,6 +1687,20 @@ export function createKrateHandler(options) {
1639
1687
  return "";
1640
1688
  return to;
1641
1689
  }
1690
+ // An absolute http(s) URL from a redirect() signal that marked itself external
1691
+ // (the OAuth redirect of @krate/auth signIn()). A redirect returned in an
1692
+ // action's result is never external.
1693
+ function safeExternalRedirect(value) {
1694
+ if (typeof value !== "string" || /[\u0000-\u001f\u007f\\]/.test(value))
1695
+ return "";
1696
+ try {
1697
+ const u = new URL(value.trim());
1698
+ return u.protocol === "https:" || u.protocol === "http:" ? u.href : "";
1699
+ }
1700
+ catch {
1701
+ return "";
1702
+ }
1703
+ }
1642
1704
  function sanitizeActionReturn(value, rs) {
1643
1705
  value = value.trim();
1644
1706
  if (!value)
@@ -1831,14 +1893,17 @@ export function createKrateHandler(options) {
1831
1893
  const scope = { kind: "action", paths: [], tags: [] };
1832
1894
  let result;
1833
1895
  let redirect = "";
1896
+ let external = false;
1834
1897
  let failure = null;
1835
1898
  let fallback = 0;
1836
1899
  try {
1837
1900
  result = await revalidateStore.run(scope, () => actionStore.run(ctx, () => fn(...args)));
1838
1901
  }
1839
1902
  catch (e) {
1840
- if (e && typeof e.__krateRedirect === "string")
1903
+ if (e && typeof e.__krateRedirect === "string") {
1841
1904
  redirect = e.__krateRedirect;
1905
+ external = e.__krateRedirectExternal === true;
1906
+ }
1842
1907
  else if ((fallback = httpFallbackStatus(e)))
1843
1908
  failure = null;
1844
1909
  else
@@ -1854,7 +1919,7 @@ export function createKrateHandler(options) {
1854
1919
  }
1855
1920
  if (!redirect && result && typeof result === "object" && typeof result.redirect === "string")
1856
1921
  redirect = result.redirect;
1857
- redirect = safeActionRedirect(redirect);
1922
+ redirect = external ? safeExternalRedirect(redirect) : safeActionRedirect(redirect);
1858
1923
  if (fallback && !failure)
1859
1924
  return withHeaders(rs, actionFallback(rs, fallback, fetchMode, ctx.setCookies, reqHeaders.get("x-krate-return") || ""));
1860
1925
  let body = "";
@@ -1862,6 +1927,8 @@ export function createKrateHandler(options) {
1862
1927
  const envelope = { ok: true, result: result === undefined ? null : result };
1863
1928
  if (redirect)
1864
1929
  envelope.redirect = redirect;
1930
+ if (redirect && external)
1931
+ envelope.external = true;
1865
1932
  if (revalidation) {
1866
1933
  envelope.revalidated = true;
1867
1934
  envelope.revalidation = revalidation;
@@ -1964,11 +2031,12 @@ export function createKrateHandler(options) {
1964
2031
  }
1965
2032
  async function pageChain(rs) {
1966
2033
  return flashNotice(rs, async (rs2) => {
2034
+ const requested = rs2.path;
1967
2035
  const blocked = await runMiddleware(rs2);
1968
2036
  if (blocked)
1969
2037
  return blocked;
1970
2038
  // Checked on the requested URL, before a rewrite changes the internal path.
1971
- const slashless = redirectToSlashless(rs2);
2039
+ const slashless = redirectToSlashless(rs2, requested);
1972
2040
  if (slashless)
1973
2041
  return slashless;
1974
2042
  return redirectRewriteHandler(rs2);
package/dist/server.d.ts CHANGED
@@ -30,14 +30,14 @@ interface RawHTML {
30
30
  export declare function goComponent(name: string, tier: string, baked: Record<string, string> | null | undefined, props: Record<string, any> | undefined, revalidate?: number): JSXNode;
31
31
  type JSXNode = string | number | boolean | null | undefined | JSXNode[] | JSXElement | RawHTML;
32
32
  interface JSXElement {
33
- type: string | Function;
33
+ type: string | Function | object | symbol;
34
34
  props: Record<string, any>;
35
35
  children: JSXNode[];
36
36
  }
37
37
  export declare function Fragment(_props: {
38
38
  children?: JSXNode;
39
39
  }): JSXNode;
40
- export declare function jsx(type: string | Function, props: Record<string, any>, _key?: string): JSXElement;
40
+ export declare function jsx(type: string | Function | object | symbol, props: Record<string, any>, _key?: string): JSXElement;
41
41
  export declare const jsxs: typeof jsx;
42
42
  /**
43
43
  * ServerErrorBoundary is the request-time form of a segment's error.tsx under
@@ -97,18 +97,25 @@ export declare function lazy<P = Record<string, any>>(loader: () => Promise<{
97
97
  } | ((props: P) => unknown)>): (props: P & {
98
98
  children?: JSXNode;
99
99
  }) => Promise<JSXNode>;
100
- type ClientRender = (component: unknown, props: Record<string, unknown>, childrenHTML: string | null) => string;
100
+ type ClientRender = (component: unknown, props: Record<string, unknown>, childrenHTML: string | null, fwd?: SlotValues, capture?: Record<string, SlotValues>) => string;
101
+ type SlotValues = {
102
+ k: string;
103
+ v: unknown;
104
+ }[];
101
105
  /**
102
106
  * clientBoundary wraps an export of a `"use client"` module, or a component
103
107
  * of a React package, for the server bundle of a page that renders server
104
108
  * components. The server renders the component's initial HTML (render) and,
105
- * when it hydrates, puts it in a <krate-island> marker carrying its
106
- * serialized props; the per-module client chunk at src hydrates it in the
107
- * browser. A component without state or effects (hydrate false, an icon)
108
- * is plain HTML. Children are rendered on the server and passed as HTML, or,
109
- * when they hold client components of their own, as a tree that keeps those
110
- * components in the same Preact tree. A namespace export (Radix's Dialog) is
111
- * wrapped member by member. With `flight` the children always travel as a
109
+ * when it hydrates, puts it in a <krate-island> marker preceded by a JSON
110
+ * script with its serialized props; the per-module client chunk at src
111
+ * hydrates it in the browser. A component without state or effects (hydrate
112
+ * false, an icon) is plain HTML. Children are rendered on the server into the
113
+ * component's HTML and adopted there by the browser (see DeferredSlot), or,
114
+ * when they hold client components of their own, sent as a tree that keeps
115
+ * those components in the same Preact tree. The children of an asChild component
116
+ * always travel as a tree, with a server component at the top of a child run
117
+ * to the element it renders, so the component's Slot can clone it. A
118
+ * namespace export (Radix's Dialog) is wrapped member by member. With `flight` the children always travel as a
112
119
  * tree (a real-React island rebuilds them as elements, not HTML).
113
120
  */
114
121
  export declare function clientBoundary(moduleId: string, exportName: string, src: string, impl: unknown, render: ClientRender, hydrate?: boolean, flight?: boolean): unknown;