@krate/runtime 0.5.0-alpha.4 → 0.5.0-beta.2

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 (165) hide show
  1. package/dist/action-context.d.ts +5 -1
  2. package/dist/action-context.js +33 -3
  3. package/dist/action-context.test.js +26 -1
  4. package/dist/actions.d.ts +12 -1
  5. package/dist/actions.js +84 -4
  6. package/dist/actions.test.js +61 -1
  7. package/dist/assets-static.d.ts +5 -0
  8. package/dist/assets-static.js +31 -0
  9. package/dist/boundary.d.ts +12 -0
  10. package/dist/boundary.js +53 -13
  11. package/dist/cache-adapter.d.ts +113 -0
  12. package/dist/cache-adapter.js +217 -0
  13. package/dist/cache-adapter.test.d.ts +1 -0
  14. package/dist/cache-adapter.test.js +244 -0
  15. package/dist/cache-redis.d.ts +27 -0
  16. package/dist/cache-redis.js +110 -0
  17. package/dist/cache-scope.d.ts +25 -9
  18. package/dist/cache-scope.js +64 -13
  19. package/dist/cache.d.ts +1 -0
  20. package/dist/cache.js +3 -3
  21. package/dist/cache.test.js +16 -0
  22. package/dist/class-component.js +6 -0
  23. package/dist/class-component.test.js +53 -2
  24. package/dist/class-helpers.d.ts +4 -4
  25. package/dist/class-helpers.js +357 -32
  26. package/dist/class-helpers.test.d.ts +1 -0
  27. package/dist/class-helpers.test.js +169 -0
  28. package/dist/context.d.ts +16 -2
  29. package/dist/context.js +44 -3
  30. package/dist/dev-overlay.js +85 -12
  31. package/dist/dev-overlay.test.js +22 -0
  32. package/dist/dom.d.ts +15 -0
  33. package/dist/dom.js +97 -3
  34. package/dist/effect-errors.test.d.ts +1 -0
  35. package/dist/effect-errors.test.js +163 -0
  36. package/dist/effects.d.ts +8 -0
  37. package/dist/effects.js +13 -0
  38. package/dist/error-overlay.js +7 -7
  39. package/dist/handler-html.d.ts +63 -0
  40. package/dist/handler-html.js +285 -0
  41. package/dist/handler-html.test.d.ts +1 -0
  42. package/dist/handler-html.test.js +256 -0
  43. package/dist/handler-routing.d.ts +83 -0
  44. package/dist/handler-routing.js +579 -0
  45. package/dist/handler-routing.test.d.ts +1 -0
  46. package/dist/handler-routing.test.js +99 -0
  47. package/dist/handler-segments.d.ts +43 -0
  48. package/dist/handler-segments.js +115 -0
  49. package/dist/handler-segments.test.d.ts +1 -0
  50. package/dist/handler-segments.test.js +217 -0
  51. package/dist/head-tags.d.ts +51 -0
  52. package/dist/head-tags.js +329 -0
  53. package/dist/head-tags.test.d.ts +1 -0
  54. package/dist/head-tags.test.js +131 -0
  55. package/dist/highlight-bind.d.ts +42 -0
  56. package/dist/highlight-bind.js +176 -0
  57. package/dist/highlight-bind.test.d.ts +1 -0
  58. package/dist/highlight-bind.test.js +124 -0
  59. package/dist/highlight.d.ts +93 -0
  60. package/dist/highlight.js +426 -0
  61. package/dist/highlight.test.d.ts +1 -0
  62. package/dist/highlight.test.js +56 -0
  63. package/dist/hints.d.ts +19 -0
  64. package/dist/hints.js +97 -0
  65. package/dist/hook-args.d.ts +7 -0
  66. package/dist/hook-args.js +10 -0
  67. package/dist/htmlspec-attributes.d.ts +1 -0
  68. package/dist/htmlspec-attributes.js +159 -0
  69. package/dist/index.d.ts +4 -0
  70. package/dist/index.js +3 -0
  71. package/dist/jsx-types.d.ts +11 -0
  72. package/dist/krate-core.js +2 -0
  73. package/dist/krate-dev.js +75 -12
  74. package/dist/krate-f-actions.js +2 -0
  75. package/dist/krate-f-classes.js +8 -0
  76. package/dist/krate-f-helpers.js +2 -0
  77. package/dist/krate-f-highlight.js +2 -0
  78. package/dist/krate-f-list.js +2 -0
  79. package/dist/krate-f-navigation.js +2 -0
  80. package/dist/krate-f-router-lite.js +2 -0
  81. package/dist/krate-f-router.js +2 -0
  82. package/dist/krate-features.json +493 -0
  83. package/dist/krate-hydrate-lite.js +5 -5
  84. package/dist/krate-hydrate.js +5 -5
  85. package/dist/krate-runtime.js +5 -5
  86. package/dist/list.js +4 -2
  87. package/dist/metadata-routes.d.ts +75 -0
  88. package/dist/metadata-routes.js +76 -0
  89. package/dist/metadata.d.ts +222 -0
  90. package/dist/metadata.js +452 -0
  91. package/dist/metadata.test.d.ts +1 -0
  92. package/dist/metadata.test.js +167 -0
  93. package/dist/navigation-server.d.ts +27 -0
  94. package/dist/navigation-server.js +90 -0
  95. package/dist/navigation.d.ts +37 -0
  96. package/dist/navigation.js +195 -0
  97. package/dist/navigation.test.d.ts +1 -0
  98. package/dist/navigation.test.js +130 -0
  99. package/dist/node-server.d.ts +22 -0
  100. package/dist/node-server.js +436 -0
  101. package/dist/node-server.test.d.ts +1 -0
  102. package/dist/node-server.test.js +492 -0
  103. package/dist/og-files.d.ts +2 -0
  104. package/dist/og-files.js +19 -0
  105. package/dist/og.d.ts +56 -0
  106. package/dist/og.js +156 -0
  107. package/dist/patterns.test.d.ts +1 -0
  108. package/dist/patterns.test.js +98 -0
  109. package/dist/react.d.ts +47 -0
  110. package/dist/react.js +59 -1
  111. package/dist/react19.test.d.ts +1 -0
  112. package/dist/react19.test.js +176 -0
  113. package/dist/reactivity-core.test.d.ts +1 -0
  114. package/dist/reactivity-core.test.js +152 -0
  115. package/dist/reconcile.js +4 -2
  116. package/dist/request-context.d.ts +23 -0
  117. package/dist/request-context.js +45 -0
  118. package/dist/resp.d.ts +55 -0
  119. package/dist/resp.js +239 -0
  120. package/dist/router-layout.test.js +70 -0
  121. package/dist/router-nav.test.js +65 -0
  122. package/dist/router-speculation.test.d.ts +1 -0
  123. package/dist/router-speculation.test.js +25 -0
  124. package/dist/router-styles.test.d.ts +1 -0
  125. package/dist/router-styles.test.js +98 -0
  126. package/dist/router.js +175 -36
  127. package/dist/segmented-lru.d.ts +19 -0
  128. package/dist/segmented-lru.js +87 -0
  129. package/dist/server-async.test.js +43 -1
  130. package/dist/server-handler.d.ts +165 -0
  131. package/dist/server-handler.js +1826 -0
  132. package/dist/server-handler.test.d.ts +1 -0
  133. package/dist/server-handler.test.js +522 -0
  134. package/dist/server-highlight.test.d.ts +1 -0
  135. package/dist/server-highlight.test.js +32 -0
  136. package/dist/server-renderer.js +165 -720
  137. package/dist/server-renderer.test.d.ts +1 -0
  138. package/dist/server-renderer.test.js +266 -0
  139. package/dist/server.d.ts +13 -1
  140. package/dist/server.js +131 -16
  141. package/dist/server.test.js +20 -0
  142. package/dist/sha256.d.ts +1 -0
  143. package/dist/sha256.js +66 -0
  144. package/dist/sha256.test.d.ts +1 -0
  145. package/dist/sha256.test.js +13 -0
  146. package/dist/shared-isr.d.ts +31 -0
  147. package/dist/shared-isr.js +142 -0
  148. package/dist/signal.d.ts +3 -2
  149. package/dist/signal.js +163 -23
  150. package/dist/slots.js +12 -1
  151. package/dist/ssr-core-behavior.test.d.ts +1 -0
  152. package/dist/ssr-core-behavior.test.js +620 -0
  153. package/dist/ssr-core.d.ts +175 -0
  154. package/dist/ssr-core.js +940 -0
  155. package/dist/ssr-core.test.d.ts +1 -0
  156. package/dist/ssr-core.test.js +324 -0
  157. package/dist/static-memory.d.ts +3 -0
  158. package/dist/static-memory.js +47 -0
  159. package/dist/static-memory.test.d.ts +1 -0
  160. package/dist/static-memory.test.js +26 -0
  161. package/dist/test-redis.d.ts +12 -0
  162. package/dist/test-redis.js +131 -0
  163. package/dist/use-id.d.ts +8 -0
  164. package/dist/use-id.js +40 -0
  165. package/package.json +30 -2
@@ -1,6 +1,6 @@
1
1
  import { afterEach, describe, expect, it } from 'vitest';
2
2
  import { cookies, headers } from './action-context.js';
3
- import { getRequestContext, isNotFoundSignal, notFound, parseCookieHeader, redirectTarget, thenableRecord, } from './request-context.js';
3
+ import { getRequestContext, isNotFoundSignal, notFound, forbidden, unauthorized, accessDeniedStatus, httpFallbackStatus, parseCookieHeader, redirectTarget, thenableRecord, } from './request-context.js';
4
4
  import { Link, Suspense, clientBoundary, jsx, renderToString, renderToStringAsync } from './server.js';
5
5
  const REQUEST_KEY = Symbol.for('krate.request.store');
6
6
  function withRequestStore(ctx) {
@@ -74,6 +74,26 @@ describe('renderToStringAsync', () => {
74
74
  }
75
75
  await expect(renderToStringAsync(jsx('div', { children: jsx(Boom, {}) }))).rejects.toThrow('db down');
76
76
  });
77
+ it('renders a class error boundary around a failing async server component', async () => {
78
+ class Boundary {
79
+ constructor(props) {
80
+ this.state = { error: null };
81
+ this.props = props;
82
+ }
83
+ static getDerivedStateFromError(error) { return { error }; }
84
+ render() {
85
+ if (this.state.error)
86
+ return jsx('p', { children: 'fallback: ' + this.state.error.message });
87
+ return this.props.children;
88
+ }
89
+ }
90
+ async function Boom() {
91
+ await tick();
92
+ throw new Error('server boom');
93
+ }
94
+ const html = await renderToStringAsync(jsx(Boundary, { children: jsx(Boom, {}) }));
95
+ expect(html).toContain('fallback: server boom');
96
+ });
77
97
  });
78
98
  describe('renderToString with async components', () => {
79
99
  it('throws instead of rendering an empty string', () => {
@@ -132,6 +152,28 @@ describe('render signals', () => {
132
152
  expect(isNotFoundSignal(caught)).toBe(true);
133
153
  expect(isNotFoundSignal(new Error('other'))).toBe(false);
134
154
  });
155
+ it('forbidden() and unauthorized() throw signals with the Next.js digest', () => {
156
+ const catchOf = (fn) => {
157
+ try {
158
+ fn();
159
+ }
160
+ catch (e) {
161
+ return e;
162
+ }
163
+ };
164
+ const f = catchOf(forbidden);
165
+ const u = catchOf(unauthorized);
166
+ expect(accessDeniedStatus(f)).toBe(403);
167
+ expect(accessDeniedStatus(u)).toBe(401);
168
+ expect(f.digest).toBe('NEXT_HTTP_ERROR_FALLBACK;403');
169
+ expect(u.digest).toBe('NEXT_HTTP_ERROR_FALLBACK;401');
170
+ expect(accessDeniedStatus(Object.assign(new Error('x'), { digest: 'NEXT_HTTP_ERROR_FALLBACK;403' }))).toBe(403);
171
+ expect(accessDeniedStatus(new Error('other'))).toBe(0);
172
+ expect(isNotFoundSignal(f)).toBe(false);
173
+ expect(httpFallbackStatus(catchOf(notFound))).toBe(404);
174
+ expect(httpFallbackStatus(f)).toBe(403);
175
+ expect(httpFallbackStatus(undefined)).toBe(0);
176
+ });
135
177
  it('redirectTarget understands Krate and Next.js redirect errors', () => {
136
178
  expect(redirectTarget(Object.assign(new Error('x'), { __krateRedirect: '/a' }))).toBe('/a');
137
179
  expect(redirectTarget(new Error('NEXT_REDIRECT:/b'))).toBe('/b');
@@ -0,0 +1,165 @@
1
+ import { type IsrStore, type RegionMeta } from "./ssr-core.js";
2
+ import { isDynamicPattern } from "./handler-routing.js";
3
+ import { type SegmentEntry } from "./handler-segments.js";
4
+ import { type CacheAdapter, type CacheSpec } from "./cache-adapter.js";
5
+ export type DeployTarget = "node" | "vercel" | "netlify" | "cloudflare";
6
+ export interface HandlerConfig {
7
+ /** The shared cache (krate.config cache), without its URL; see HandlerOptions.cache. */
8
+ cache?: CacheSpec & {
9
+ urlEnv?: string;
10
+ };
11
+ /** URL prefix the site is mounted under ("" or "/prefix"). */
12
+ basePath: string;
13
+ /**
14
+ * server.trustProxy: true trusts X-Forwarded-Host/Proto from a proxy of
15
+ * yours in front of the platform, false trusts no forwarded header, null
16
+ * (not configured) uses what each platform guarantees (see PLATFORMS).
17
+ */
18
+ trustProxy: boolean | null;
19
+ /** Request body cap for pages and middleware, bytes. */
20
+ maxBodyBytes: number;
21
+ /** Request body cap for API routes and actions, bytes. */
22
+ maxAPIBodyBytes: number;
23
+ redirects: Array<{
24
+ source: string;
25
+ destination: string;
26
+ permanent?: boolean;
27
+ }>;
28
+ rewrites: Array<{
29
+ source: string;
30
+ destination: string;
31
+ }>;
32
+ headers: Array<{
33
+ source: string;
34
+ headers: Record<string, string>;
35
+ }>;
36
+ /** Resolved security headers; an empty string means "do not send". */
37
+ security: {
38
+ nosniff: boolean;
39
+ frameOptions: string;
40
+ referrerPolicy: string;
41
+ permissionsPolicy: string;
42
+ hsts: string;
43
+ csp: string;
44
+ };
45
+ cors: {
46
+ enabled: boolean;
47
+ origins: string[];
48
+ methods: string[];
49
+ headers: string[];
50
+ credentials: boolean;
51
+ maxAge: number;
52
+ };
53
+ ssr: {
54
+ timeout: number;
55
+ maxCacheSize: number;
56
+ ppr: boolean;
57
+ pprRevalidate: number;
58
+ };
59
+ }
60
+ export interface HandlerPage {
61
+ route: string;
62
+ source?: string;
63
+ mode: "ssr" | "isr" | "streaming";
64
+ revalidate?: number;
65
+ bundlePath: string;
66
+ serverComponents?: boolean;
67
+ /** Source directory relative to the pages root, route groups kept (segment page lookup). */
68
+ segDir?: string;
69
+ }
70
+ export interface ActionEntry {
71
+ id: string;
72
+ export: string;
73
+ file: string;
74
+ source?: string;
75
+ }
76
+ /** Build data the compiler inlines into the server bundle. */
77
+ export interface KrateServerData {
78
+ version: 1;
79
+ config: HandlerConfig;
80
+ /** Request-time pages (server-manifest.json). */
81
+ pages: HandlerPage[];
82
+ regions: Record<string, RegionMeta[]>;
83
+ /** Every page of the build with its render mode (for revalidatePath). */
84
+ allPages: Array<{
85
+ route: string;
86
+ mode: "ssg" | "ssr" | "isr" | "streaming";
87
+ segDir?: string;
88
+ }>;
89
+ /** Baked shells of the request-time pages, keyed by manifest route. */
90
+ shells: Record<string, string>;
91
+ /** Dynamic SSG templates; pattern is the output dir ("blog/[slug]"). */
92
+ templates: Array<{
93
+ pattern: string;
94
+ html: string;
95
+ }>;
96
+ staticOnlyRoutes: string[];
97
+ hashedAssets: string[];
98
+ html404: string;
99
+ html500: string;
100
+ /** Directories with their own not-found or error page (manifest segments[]). */
101
+ segments?: SegmentEntry[];
102
+ /** Segment page HTML keyed by its output path ("blog/404.html"). */
103
+ segmentPages?: Record<string, string>;
104
+ /** Prerendered pages (dist-relative path to HTML) the function serves itself after middleware. */
105
+ staticPages?: Record<string, string>;
106
+ /** API route keys: file path under the API dir without extension ("items/[id]"). */
107
+ apiRoutes: string[];
108
+ actions: ActionEntry[];
109
+ hasMiddleware: boolean;
110
+ }
111
+ /** Module namespaces the compiler imports statically into the bundle. */
112
+ export interface KrateServerModules {
113
+ /** Page server bundles keyed by their manifest bundlePath. */
114
+ pages?: Record<string, any>;
115
+ /** Runtime component renderers keyed by region bundlePath. */
116
+ runtime?: Record<string, (props: string | Record<string, any>) => string | Promise<string>>;
117
+ /** API route modules keyed by route key. */
118
+ api?: Record<string, any>;
119
+ /** Server action modules keyed by manifest file. */
120
+ actions?: Record<string, any>;
121
+ middleware?: any;
122
+ }
123
+ /** Where static files come from. Absent on platforms whose CDN serves them first. */
124
+ export interface StaticLayer {
125
+ /** A concrete file or folder index exists for the URL path. */
126
+ exists(urlPath: string): boolean | Promise<boolean>;
127
+ /** A non-HTML file exists for the URL path (a page's script, chunk, image). */
128
+ isAsset(urlPath: string): boolean | Promise<boolean>;
129
+ /** Serves the path like http.FileServer; a 404 Response (or null) when missing. */
130
+ serve(request: Request, urlPath: string): Promise<Response | null>;
131
+ }
132
+ export interface HandlerContext {
133
+ /** Keeps the instance alive for work that outlives the response. */
134
+ waitUntil?(work: Promise<unknown>): void;
135
+ }
136
+ export interface HandlerOptions {
137
+ target: DeployTarget;
138
+ data: KrateServerData;
139
+ modules: KrateServerModules;
140
+ static?: StaticLayer;
141
+ /** Page ISR cache storage. Defaults to a bounded in-memory store per instance. */
142
+ isrStore?: IsrStore;
143
+ /**
144
+ * A shared cache for the ISR page cache and the data cache: a CacheAdapter
145
+ * (your own, or createRedisAdapter from @krate/runtime/cache-redis) or a spec
146
+ * such as { adapter: "redis", url }. Without it each instance keeps its own
147
+ * caches. data.config.cache (from krate.config) is the default; its URL comes
148
+ * from the environment (KRATE_CACHE_URL, or the variable named by urlEnv).
149
+ */
150
+ cache?: CacheSpec | CacheAdapter | Promise<CacheAdapter>;
151
+ /** Overrides data.config.trustProxy. */
152
+ trustProxy?: boolean | null;
153
+ /** Hex sha256 for error digests; defaults to the portable implementation. */
154
+ sha256Hex?(input: string): string;
155
+ /**
156
+ * Key the no-JS flash cookie is signed with. Defaults to the
157
+ * KRATE_FLASH_SECRET environment variable. Without a key the node target
158
+ * signs with a random per-process key and the serverless targets carry no
159
+ * flash state at all; an unsigned flash cookie is never trusted.
160
+ */
161
+ flashSecret?: string;
162
+ }
163
+ export type KrateHandler = (request: Request, ctx?: HandlerContext) => Promise<Response>;
164
+ export declare function createKrateHandler(options: HandlerOptions): KrateHandler;
165
+ export { isDynamicPattern };