@krate/runtime 0.5.0-beta.1 → 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 (134) hide show
  1. package/dist/actions.js +2 -1
  2. package/dist/assets-static.d.ts +5 -0
  3. package/dist/assets-static.js +31 -0
  4. package/dist/boundary.d.ts +12 -0
  5. package/dist/boundary.js +53 -13
  6. package/dist/cache-adapter.d.ts +113 -0
  7. package/dist/cache-adapter.js +217 -0
  8. package/dist/cache-adapter.test.d.ts +1 -0
  9. package/dist/cache-adapter.test.js +244 -0
  10. package/dist/cache-redis.d.ts +27 -0
  11. package/dist/cache-redis.js +110 -0
  12. package/dist/cache-scope.d.ts +25 -9
  13. package/dist/cache-scope.js +60 -12
  14. package/dist/cache.d.ts +1 -0
  15. package/dist/cache.js +3 -3
  16. package/dist/class-component.js +6 -0
  17. package/dist/class-component.test.js +53 -2
  18. package/dist/dev-overlay.js +10 -10
  19. package/dist/dom.d.ts +8 -0
  20. package/dist/dom.js +28 -0
  21. package/dist/effect-errors.test.d.ts +1 -0
  22. package/dist/effect-errors.test.js +163 -0
  23. package/dist/effects.d.ts +8 -0
  24. package/dist/effects.js +13 -0
  25. package/dist/error-overlay.js +4 -4
  26. package/dist/handler-html.d.ts +63 -0
  27. package/dist/handler-html.js +285 -0
  28. package/dist/handler-html.test.d.ts +1 -0
  29. package/dist/handler-html.test.js +256 -0
  30. package/dist/handler-routing.d.ts +83 -0
  31. package/dist/handler-routing.js +579 -0
  32. package/dist/handler-routing.test.d.ts +1 -0
  33. package/dist/handler-routing.test.js +99 -0
  34. package/dist/handler-segments.d.ts +43 -0
  35. package/dist/handler-segments.js +115 -0
  36. package/dist/handler-segments.test.d.ts +1 -0
  37. package/dist/handler-segments.test.js +217 -0
  38. package/dist/head-tags.d.ts +51 -0
  39. package/dist/head-tags.js +329 -0
  40. package/dist/head-tags.test.d.ts +1 -0
  41. package/dist/head-tags.test.js +131 -0
  42. package/dist/highlight-bind.d.ts +42 -0
  43. package/dist/highlight-bind.js +176 -0
  44. package/dist/highlight-bind.test.d.ts +1 -0
  45. package/dist/highlight-bind.test.js +124 -0
  46. package/dist/highlight.d.ts +93 -0
  47. package/dist/highlight.js +426 -0
  48. package/dist/highlight.test.d.ts +1 -0
  49. package/dist/highlight.test.js +56 -0
  50. package/dist/hints.d.ts +19 -0
  51. package/dist/hints.js +97 -0
  52. package/dist/index.d.ts +3 -0
  53. package/dist/index.js +2 -0
  54. package/dist/jsx-types.d.ts +11 -0
  55. package/dist/krate-core.js +1 -1
  56. package/dist/krate-dev.js +10 -10
  57. package/dist/krate-f-actions.js +1 -1
  58. package/dist/krate-f-classes.js +5 -5
  59. package/dist/krate-f-highlight.js +2 -0
  60. package/dist/krate-f-list.js +1 -1
  61. package/dist/krate-f-navigation.js +2 -0
  62. package/dist/krate-f-router.js +1 -1
  63. package/dist/krate-features.json +254 -0
  64. package/dist/krate-hydrate-lite.js +5 -5
  65. package/dist/krate-hydrate.js +5 -5
  66. package/dist/krate-runtime.js +5 -5
  67. package/dist/list.js +4 -2
  68. package/dist/metadata-routes.d.ts +75 -0
  69. package/dist/metadata-routes.js +76 -0
  70. package/dist/metadata.d.ts +222 -0
  71. package/dist/metadata.js +452 -0
  72. package/dist/metadata.test.d.ts +1 -0
  73. package/dist/metadata.test.js +167 -0
  74. package/dist/navigation-server.d.ts +27 -0
  75. package/dist/navigation-server.js +90 -0
  76. package/dist/navigation.d.ts +37 -0
  77. package/dist/navigation.js +195 -0
  78. package/dist/navigation.test.d.ts +1 -0
  79. package/dist/navigation.test.js +130 -0
  80. package/dist/node-server.d.ts +22 -0
  81. package/dist/node-server.js +436 -0
  82. package/dist/node-server.test.d.ts +1 -0
  83. package/dist/node-server.test.js +492 -0
  84. package/dist/og-files.d.ts +2 -0
  85. package/dist/og-files.js +19 -0
  86. package/dist/og.d.ts +56 -0
  87. package/dist/og.js +156 -0
  88. package/dist/react.d.ts +47 -0
  89. package/dist/react.js +59 -1
  90. package/dist/react19.test.d.ts +1 -0
  91. package/dist/react19.test.js +176 -0
  92. package/dist/request-context.d.ts +23 -0
  93. package/dist/request-context.js +45 -0
  94. package/dist/resp.d.ts +55 -0
  95. package/dist/resp.js +239 -0
  96. package/dist/router-layout.test.js +70 -0
  97. package/dist/router-nav.test.js +27 -0
  98. package/dist/router.js +59 -4
  99. package/dist/segmented-lru.d.ts +19 -0
  100. package/dist/segmented-lru.js +87 -0
  101. package/dist/server-async.test.js +23 -1
  102. package/dist/server-handler.d.ts +165 -0
  103. package/dist/server-handler.js +1826 -0
  104. package/dist/server-handler.test.d.ts +1 -0
  105. package/dist/server-handler.test.js +522 -0
  106. package/dist/server-highlight.test.d.ts +1 -0
  107. package/dist/server-highlight.test.js +32 -0
  108. package/dist/server-renderer.js +146 -788
  109. package/dist/server-renderer.test.d.ts +1 -0
  110. package/dist/server-renderer.test.js +266 -0
  111. package/dist/server.d.ts +12 -1
  112. package/dist/server.js +42 -6
  113. package/dist/sha256.d.ts +1 -0
  114. package/dist/sha256.js +66 -0
  115. package/dist/sha256.test.d.ts +1 -0
  116. package/dist/sha256.test.js +13 -0
  117. package/dist/shared-isr.d.ts +31 -0
  118. package/dist/shared-isr.js +142 -0
  119. package/dist/signal.js +63 -8
  120. package/dist/ssr-core-behavior.test.d.ts +1 -0
  121. package/dist/ssr-core-behavior.test.js +620 -0
  122. package/dist/ssr-core.d.ts +175 -0
  123. package/dist/ssr-core.js +940 -0
  124. package/dist/ssr-core.test.d.ts +1 -0
  125. package/dist/ssr-core.test.js +324 -0
  126. package/dist/static-memory.d.ts +3 -0
  127. package/dist/static-memory.js +47 -0
  128. package/dist/static-memory.test.d.ts +1 -0
  129. package/dist/static-memory.test.js +26 -0
  130. package/dist/test-redis.d.ts +12 -0
  131. package/dist/test-redis.js +131 -0
  132. package/dist/use-id.d.ts +8 -0
  133. package/dist/use-id.js +40 -0
  134. package/package.json +29 -1
@@ -0,0 +1,436 @@
1
+ // @krate/runtime/node-server - runs the portable Krate handler under plain Node.
2
+ //
3
+ // The `node` deploy target bundles this with the build data into
4
+ // dist/.krate/server/server.mjs. It needs no Go binary: it binds PORT/HOST,
5
+ // serves the built static files from dist with the same hardening as
6
+ // `krate start` (no dotfiles, no directory listings, Krate's server-only
7
+ // output hidden, hashed assets immutable, precompressed .br/.gz siblings,
8
+ // Range, Last-Modified), hands everything else to the handler, and drains
9
+ // in-flight requests on SIGTERM/SIGINT.
10
+ import fs from "node:fs";
11
+ import fsp from "node:fs/promises";
12
+ import http from "node:http";
13
+ import path from "node:path";
14
+ import zlib from "node:zlib";
15
+ import { Readable } from "node:stream";
16
+ import { fileURLToPath } from "node:url";
17
+ import { hasFlashCookie } from "./handler-html.js";
18
+ import { extName, isHiddenPath, isInternalOutputPath } from "./handler-routing.js";
19
+ const MIME = {
20
+ ".html": "text/html; charset=utf-8",
21
+ ".htm": "text/html; charset=utf-8",
22
+ ".css": "text/css; charset=utf-8",
23
+ ".js": "text/javascript; charset=utf-8",
24
+ ".mjs": "text/javascript; charset=utf-8",
25
+ ".cjs": "text/javascript; charset=utf-8",
26
+ ".json": "application/json",
27
+ ".map": "application/json; charset=utf-8",
28
+ ".webmanifest": "application/manifest+json",
29
+ ".svg": "image/svg+xml",
30
+ ".png": "image/png",
31
+ ".jpg": "image/jpeg",
32
+ ".jpeg": "image/jpeg",
33
+ ".gif": "image/gif",
34
+ ".webp": "image/webp",
35
+ ".avif": "image/avif",
36
+ ".ico": "image/vnd.microsoft.icon",
37
+ ".woff": "font/woff",
38
+ ".woff2": "font/woff2",
39
+ ".ttf": "font/ttf",
40
+ ".otf": "font/otf",
41
+ ".txt": "text/plain; charset=utf-8",
42
+ ".xml": "text/xml; charset=utf-8",
43
+ ".rss": "application/xml; charset=utf-8",
44
+ ".atom": "application/xml; charset=utf-8",
45
+ ".wasm": "application/wasm",
46
+ ".pdf": "application/pdf",
47
+ ".zip": "application/zip",
48
+ ".mp4": "video/mp4",
49
+ ".webm": "video/webm",
50
+ ".mp3": "audio/mpeg",
51
+ ".wav": "audio/wav",
52
+ ".ogg": "audio/ogg",
53
+ ".md": "text/markdown; charset=utf-8",
54
+ };
55
+ function safeDecode(p) {
56
+ try {
57
+ return decodeURIComponent(p);
58
+ }
59
+ catch {
60
+ return p;
61
+ }
62
+ }
63
+ function acceptsEncoding(header, coding) {
64
+ if (!header)
65
+ return false;
66
+ let wildcard = -1;
67
+ for (let part of header.split(",")) {
68
+ part = part.trim();
69
+ if (!part)
70
+ continue;
71
+ let token = part;
72
+ let q = 1;
73
+ const semi = part.indexOf(";");
74
+ if (semi >= 0) {
75
+ token = part.slice(0, semi).trim();
76
+ for (const param of part.slice(semi + 1).split(";")) {
77
+ const t = param.trim();
78
+ if (t.startsWith("q=")) {
79
+ const v = parseFloat(t.slice(2));
80
+ if (!Number.isNaN(v))
81
+ q = v;
82
+ }
83
+ }
84
+ }
85
+ if (token.toLowerCase() === coding)
86
+ return q > 0;
87
+ if (token === "*")
88
+ wildcard = q;
89
+ }
90
+ return wildcard > 0;
91
+ }
92
+ function compressible(ct) {
93
+ ct = ct.toLowerCase();
94
+ return ct.startsWith("text/") || ct.includes("javascript") || ct.includes("json") || ct.includes("svg") || ct.includes("xml");
95
+ }
96
+ /** Static files from the built dist directory, with the hardening of `krate start`. */
97
+ export function createNodeStaticLayer(distDir) {
98
+ const root = path.resolve(distDir);
99
+ let realRoot = null;
100
+ const existsCache = new Map();
101
+ async function rootReal() {
102
+ if (!realRoot)
103
+ realRoot = await fsp.realpath(root);
104
+ return realRoot;
105
+ }
106
+ /** Maps a URL path to a file path under root; null for traversal or hidden/internal paths. */
107
+ async function resolveFile(urlPath) {
108
+ if (isInternalOutputPath(urlPath) || isHiddenPath(urlPath))
109
+ return null;
110
+ if (urlPath.includes("\0"))
111
+ return null;
112
+ const rel = path.posix.normalize("/" + urlPath.replace(/\\/g, "/")).replace(/^\/+/, "");
113
+ const full = path.join(root, ...rel.split("/").filter(Boolean));
114
+ if (full !== root && !full.startsWith(root + path.sep))
115
+ return null;
116
+ try {
117
+ const real = await fsp.realpath(full);
118
+ const rr = await rootReal();
119
+ if (real !== rr && !real.startsWith(rr + path.sep))
120
+ return null;
121
+ }
122
+ catch {
123
+ return full;
124
+ }
125
+ return full;
126
+ }
127
+ async function statFile(p) {
128
+ try {
129
+ return await fsp.stat(p);
130
+ }
131
+ catch {
132
+ return null;
133
+ }
134
+ }
135
+ function memo(key, v) {
136
+ if (existsCache.size > 20000)
137
+ existsCache.clear();
138
+ existsCache.set(key, v);
139
+ return v;
140
+ }
141
+ return {
142
+ async exists(urlPath) {
143
+ const cached = existsCache.get("e:" + urlPath);
144
+ if (cached !== undefined)
145
+ return cached;
146
+ const rel = urlPath.replace(/^\/+|\/+$/g, "");
147
+ const candidates = rel === "" ? ["index.html"] : [rel, rel + "/index.html"];
148
+ for (const c of candidates) {
149
+ const f = await resolveFile("/" + c);
150
+ if (!f)
151
+ continue;
152
+ const st = await statFile(f);
153
+ if (st && !st.isDirectory())
154
+ return memo("e:" + urlPath, true);
155
+ }
156
+ return memo("e:" + urlPath, false);
157
+ },
158
+ async isAsset(urlPath) {
159
+ const cached = existsCache.get("a:" + urlPath);
160
+ if (cached !== undefined)
161
+ return cached;
162
+ const ext = extName(safeDecode(urlPath)).toLowerCase();
163
+ if (ext === "" || ext === ".html")
164
+ return memo("a:" + urlPath, false);
165
+ const f = await resolveFile(urlPath);
166
+ if (!f)
167
+ return memo("a:" + urlPath, false);
168
+ const st = await statFile(f);
169
+ return memo("a:" + urlPath, !!st && !st.isDirectory());
170
+ },
171
+ async serve(request, urlPath) {
172
+ const method = request.method.toUpperCase();
173
+ if (method !== "GET" && method !== "HEAD")
174
+ return null;
175
+ const file = await resolveFile(urlPath);
176
+ if (!file)
177
+ return null;
178
+ let target = file;
179
+ let st = await statFile(target);
180
+ if (!st)
181
+ return null;
182
+ const url = new URL(request.url);
183
+ if (st.isDirectory()) {
184
+ // /dir without a slash is the canonical redirect to /dir/.
185
+ if (!urlPath.endsWith("/")) {
186
+ const base = path.posix.basename(urlPath) + "/";
187
+ return new Response(null, { status: 301, headers: { Location: base + url.search } });
188
+ }
189
+ target = path.join(target, "index.html");
190
+ st = await statFile(target);
191
+ if (!st || st.isDirectory())
192
+ return null;
193
+ }
194
+ else if (urlPath.endsWith("/index.html")) {
195
+ return new Response(null, { status: 301, headers: { Location: "./" + url.search } });
196
+ }
197
+ else if (urlPath.endsWith("/")) {
198
+ return null;
199
+ }
200
+ const ext = path.extname(target).toLowerCase();
201
+ let contentType = MIME[ext] || "application/octet-stream";
202
+ const headers = new Headers();
203
+ const ae = request.headers.get("accept-encoding");
204
+ let encoding = "";
205
+ let source = target;
206
+ let size = st.size;
207
+ let mtime = st.mtime;
208
+ // Build-time precompressed siblings, never for a page carrying flash state.
209
+ if (!request.headers.get("range") && !hasFlashCookie(request.headers.get("cookie"))) {
210
+ for (const [enc, suffix] of [["br", ".br"], ["gzip", ".gz"]]) {
211
+ if (!acceptsEncoding(ae, enc === "gzip" ? "gzip" : "br"))
212
+ continue;
213
+ const sib = await statFile(target + suffix);
214
+ if (sib && !sib.isDirectory()) {
215
+ encoding = enc;
216
+ source = target + suffix;
217
+ size = sib.size;
218
+ mtime = sib.mtime;
219
+ break;
220
+ }
221
+ }
222
+ }
223
+ if (compressible(contentType) || encoding)
224
+ headers.append("Vary", "Accept-Encoding");
225
+ if (encoding)
226
+ headers.set("Content-Encoding", encoding);
227
+ headers.set("Content-Type", contentType);
228
+ headers.set("Last-Modified", mtime.toUTCString());
229
+ headers.set("Accept-Ranges", "bytes");
230
+ const ims = request.headers.get("if-modified-since");
231
+ if (ims && Math.floor(mtime.getTime() / 1000) <= Math.floor(new Date(ims).getTime() / 1000)) {
232
+ return new Response(null, { status: 304, headers });
233
+ }
234
+ let status = 200;
235
+ let start = 0;
236
+ let end = size - 1;
237
+ const range = request.headers.get("range");
238
+ if (range && /^bytes=/.test(range) && !range.includes(",")) {
239
+ const m = /^bytes=(\d*)-(\d*)$/.exec(range.trim());
240
+ if (m && (m[1] !== "" || m[2] !== "")) {
241
+ if (m[1] === "") {
242
+ const n = parseInt(m[2], 10);
243
+ start = Math.max(0, size - n);
244
+ }
245
+ else {
246
+ start = parseInt(m[1], 10);
247
+ if (m[2] !== "")
248
+ end = Math.min(end, parseInt(m[2], 10));
249
+ }
250
+ if (start > end || start >= size) {
251
+ headers.set("Content-Range", `bytes */${size}`);
252
+ return new Response(null, { status: 416, headers });
253
+ }
254
+ status = 206;
255
+ headers.set("Content-Range", `bytes ${start}-${end}/${size}`);
256
+ }
257
+ }
258
+ headers.set("Content-Length", String(end - start + 1));
259
+ if (method === "HEAD")
260
+ return new Response(null, { status, headers });
261
+ const stream = Readable.toWeb(fs.createReadStream(source, { start, end }));
262
+ return new Response(stream, { status, headers });
263
+ },
264
+ };
265
+ }
266
+ export function requestFromNode(req, trustProxy) {
267
+ const encrypted = !!req.socket.encrypted;
268
+ const host = req.headers.host || "localhost";
269
+ const headers = new Headers();
270
+ for (const [k, v] of Object.entries(req.headers)) {
271
+ if (v === undefined)
272
+ continue;
273
+ if (Array.isArray(v))
274
+ for (const x of v)
275
+ headers.append(k, x);
276
+ else
277
+ headers.set(k, v);
278
+ }
279
+ if (!trustProxy) {
280
+ // Client-supplied forwarding headers are replaced by what the server observed.
281
+ headers.delete("forwarded");
282
+ headers.delete("x-real-ip");
283
+ headers.set("x-forwarded-for", req.socket.remoteAddress || "");
284
+ headers.set("x-forwarded-host", host);
285
+ headers.set("x-forwarded-proto", encrypted ? "https" : "http");
286
+ }
287
+ const method = (req.method || "GET").toUpperCase();
288
+ const init = { method, headers };
289
+ if (method !== "GET" && method !== "HEAD") {
290
+ init.body = Readable.toWeb(req);
291
+ init.duplex = "half";
292
+ }
293
+ return new Request((encrypted ? "https://" : "http://") + host + (req.url || "/"), init);
294
+ }
295
+ export async function writeResponse(req, res, response) {
296
+ res.statusCode = response.status;
297
+ const cookies = typeof response.headers.getSetCookie === "function" ? response.headers.getSetCookie() : [];
298
+ response.headers.forEach((value, key) => {
299
+ if (key.toLowerCase() !== "set-cookie")
300
+ res.setHeader(key, value);
301
+ });
302
+ if (cookies.length)
303
+ res.setHeader("set-cookie", cookies);
304
+ const ct = String(res.getHeader("content-type") || "");
305
+ const wantsGzip = !req.headers.range && acceptsEncoding(String(req.headers["accept-encoding"] || ""), "gzip");
306
+ let gzip = null;
307
+ if (compressible(ct)) {
308
+ res.setHeader("Vary", appendVary(res.getHeader("vary"), "Accept-Encoding"));
309
+ if (wantsGzip && !res.getHeader("content-encoding") && response.status !== 206 && !res.getHeader("content-range") && response.body) {
310
+ res.removeHeader("content-length");
311
+ res.setHeader("Content-Encoding", "gzip");
312
+ gzip = zlib.createGzip();
313
+ gzip.pipe(res);
314
+ }
315
+ }
316
+ const socket = res.socket;
317
+ if (socket && typeof socket.setNoDelay === "function")
318
+ socket.setNoDelay(true);
319
+ if (req.method === "HEAD" || !response.body) {
320
+ res.end();
321
+ return;
322
+ }
323
+ const out = gzip || res;
324
+ const reader = response.body.getReader();
325
+ res.on("close", () => {
326
+ reader.cancel().catch(() => { });
327
+ });
328
+ try {
329
+ for (;;) {
330
+ const r = await reader.read();
331
+ if (r.done)
332
+ break;
333
+ if (!out.write(r.value))
334
+ await new Promise((ok) => out.once("drain", ok));
335
+ // Streaming pages flush each chunk as soon as it exists.
336
+ if (gzip)
337
+ gzip.flush();
338
+ }
339
+ }
340
+ catch (err) {
341
+ if (!res.headersSent)
342
+ throw err;
343
+ res.destroy(err);
344
+ return;
345
+ }
346
+ out.end();
347
+ }
348
+ function appendVary(cur, value) {
349
+ const parts = String(cur || "").split(",").map((s) => s.trim()).filter(Boolean);
350
+ if (!parts.some((p) => p.toLowerCase() === value.toLowerCase()))
351
+ parts.push(value);
352
+ return parts.join(", ");
353
+ }
354
+ /** Starts the HTTP server; resolves with the server once it listens. */
355
+ export async function startNodeServer(opts) {
356
+ const port = parseInt(process.env.PORT || "", 10) || opts.port || 3000;
357
+ const host = process.env.HOST || process.env.HOSTNAME || "0.0.0.0";
358
+ const server = http.createServer(async (req, res) => {
359
+ try {
360
+ const request = requestFromNode(req, opts.trustProxy);
361
+ const response = await opts.handler(request);
362
+ await writeResponse(req, res, response);
363
+ }
364
+ catch (err) {
365
+ console.error("[krate] request failed:", err);
366
+ if (!res.headersSent) {
367
+ res.statusCode = 500;
368
+ res.setHeader("Content-Type", "text/plain; charset=utf-8");
369
+ res.end("Internal Server Error\n");
370
+ }
371
+ else {
372
+ res.destroy();
373
+ }
374
+ }
375
+ });
376
+ server.headersTimeout = 10000;
377
+ server.requestTimeout = 30000;
378
+ server.keepAliveTimeout = 120000;
379
+ let shuttingDown = false;
380
+ const shutdown = (signal) => {
381
+ if (shuttingDown) {
382
+ console.error(`[krate] second ${signal}, exiting immediately`);
383
+ process.exit(130);
384
+ }
385
+ shuttingDown = true;
386
+ console.log(`[krate] ${signal}: draining in-flight requests`);
387
+ const grace = (opts.shutdownTimeout && opts.shutdownTimeout > 0 ? opts.shutdownTimeout : 15) * 1000;
388
+ const timer = setTimeout(() => {
389
+ console.error(`[krate] requests still running after ${grace / 1000}s, forcing close`);
390
+ server.closeAllConnections();
391
+ process.exit(0);
392
+ }, grace);
393
+ timer.unref();
394
+ server.close(() => process.exit(0));
395
+ server.closeIdleConnections();
396
+ };
397
+ process.on("SIGTERM", () => shutdown("SIGTERM"));
398
+ process.on("SIGINT", () => shutdown("SIGINT"));
399
+ await new Promise((resolve, reject) => {
400
+ server.once("error", reject);
401
+ server.listen(port, host, () => {
402
+ server.off("error", reject);
403
+ resolve();
404
+ });
405
+ });
406
+ const addr = server.address();
407
+ const shown = addr && typeof addr === "object" ? addr.port : port;
408
+ console.log(`[krate] node server listening on http://${host === "0.0.0.0" || host === "::" ? "localhost" : host}:${shown}`);
409
+ return server;
410
+ }
411
+ /** Directory of the built site, relative to the bundle (dist/.krate/server/server.mjs). */
412
+ export function defaultDistDir(metaUrl) {
413
+ if (process.env.KRATE_DIST)
414
+ return path.resolve(process.env.KRATE_DIST);
415
+ return path.resolve(path.dirname(fileURLToPath(metaUrl)), "..", "..");
416
+ }
417
+ /** A node:http style (req, res) function that serves the handler; used by the Vercel function. */
418
+ export function nodeRequestListener(handler, trustProxy) {
419
+ return async (req, res) => {
420
+ try {
421
+ const response = await handler(requestFromNode(req, trustProxy));
422
+ await writeResponse(req, res, response);
423
+ }
424
+ catch (err) {
425
+ console.error("[krate] request failed:", err);
426
+ if (!res.headersSent) {
427
+ res.statusCode = 500;
428
+ res.setHeader("Content-Type", "text/plain; charset=utf-8");
429
+ res.end("Internal Server Error\n");
430
+ }
431
+ else {
432
+ res.destroy();
433
+ }
434
+ }
435
+ };
436
+ }
@@ -0,0 +1 @@
1
+ export {};