@k8ordo/framework 3.0.0 → 4.0.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 (55) hide show
  1. package/README.md +6 -4
  2. package/dist/cli.mjs +3 -3
  3. package/dist/generated.d.mts +12 -13
  4. package/dist/index.d.mts +3 -3
  5. package/dist/index.mjs +1 -1
  6. package/dist/paths-type-DXpmjaOr.d.mts +43 -0
  7. package/dist/runtime/app-router.mjs +1 -1
  8. package/dist/runtime/browser-payloads.mjs +1 -1
  9. package/dist/runtime/cookies.d.mts +2 -0
  10. package/dist/runtime/cookies.mjs +2 -1
  11. package/dist/runtime/ended.d.mts +7 -0
  12. package/dist/runtime/ended.mjs +14 -0
  13. package/dist/runtime/entry.browser.mjs +1 -1
  14. package/dist/runtime/entry.rsc.d.mts +17 -7
  15. package/dist/runtime/entry.rsc.mjs +42 -33
  16. package/dist/runtime/entry.ssr.d.mts +1 -0
  17. package/dist/runtime/entry.ssr.mjs +4 -3
  18. package/dist/runtime/guard.d.mts +15 -10
  19. package/dist/runtime/guard.mjs +8 -2
  20. package/dist/runtime/mount.mjs +1 -1
  21. package/dist/runtime/page-boundary.mjs +2 -2
  22. package/dist/runtime/params.d.mts +5 -4
  23. package/dist/runtime/params.mjs +2 -2
  24. package/dist/runtime/redirect.d.mts +13 -7
  25. package/dist/runtime/redirect.mjs +25 -9
  26. package/dist/runtime/request-scope.d.mts +37 -2
  27. package/dist/runtime/request-scope.mjs +60 -3
  28. package/dist/runtime/request.d.mts +6 -1
  29. package/dist/runtime/request.mjs +7 -2
  30. package/dist/runtime/route.d.mts +4 -2
  31. package/dist/runtime/route.mjs +13 -5
  32. package/dist/runtime/server.d.mts +5 -5
  33. package/dist/runtime/server.mjs +2 -2
  34. package/dist/runtime/shell.d.mts +2 -1
  35. package/dist/serve.d.mts +4 -0
  36. package/dist/serve.mjs +26 -13
  37. package/dist/{static-c-nX9Juk.d.mts → static-Be3xIqd8.d.mts} +19 -12
  38. package/dist/{static-file-BEk_xO5y.mjs → static-file-BS-WEiwu.mjs} +180 -136
  39. package/dist/{static-host-CyAmsL_7.mjs → static-host-tIDLqV8c.mjs} +1 -1
  40. package/dist/vercel.d.mts +18 -1
  41. package/dist/vercel.mjs +104 -7
  42. package/dist/vite.d.mts +6 -3
  43. package/dist/vite.mjs +155 -65
  44. package/docs/GUIDE.md +149 -21
  45. package/docs/llms.txt +102 -47
  46. package/docs/references/actions.md +14 -5
  47. package/docs/references/boundaries.md +23 -2
  48. package/docs/references/csp.md +91 -2
  49. package/docs/references/deploy.md +85 -16
  50. package/docs/references/errors.md +18 -10
  51. package/docs/references/guards.md +100 -29
  52. package/docs/references/params.md +91 -22
  53. package/docs/references/routing.md +171 -31
  54. package/package.json +15 -10
  55. package/dist/paths-type-DtsL8JuC.d.mts +0 -19
package/dist/vercel.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import "./static-c-nX9Juk.mjs";
1
+ import "./static-Be3xIqd8.mjs";
2
2
  import { Plugin } from "vite";
3
3
  /**
4
4
  * A route as Vercel's Build Output API (v3) reads it, written to
@@ -37,6 +37,17 @@ type VercelOptions = {
37
37
  * answered, before `404.html`.
38
38
  */
39
39
  readonly beforeNotFound?: readonly VercelRoute[];
40
+ /**
41
+ * Under `mode: 'server'`, how long the function may run for one request,
42
+ * in seconds, or `'max'` for the plan's maximum. Without it, Vercel's
43
+ * default for the plan.
44
+ */
45
+ readonly maxDuration?: number | 'max';
46
+ /**
47
+ * Under `mode: 'server'`, the Vercel regions the function is deployed to
48
+ * (`['hnd1']`). Without it, the project's own setting.
49
+ */
50
+ readonly regions?: readonly string[];
40
51
  };
41
52
  /**
42
53
  * Deploys the application to Vercel: after `vite build`, the build is also
@@ -51,6 +62,12 @@ type VercelOptions = {
51
62
  * own routes around them, written as given: `beforeFiles` ahead of every
52
63
  * file, `beforeNotFound` ahead of `404.html`. A file a `route.ts` wrote is
53
64
  * served with the `Content-Type` its `GET` answered.
65
+ *
66
+ * Vercel stops a function once its answer is sent, unless told through
67
+ * `@vercel/functions`. So when the application imports `waitUntil` from
68
+ * `@k8ordo/framework/server`, the function hands the handler that package's
69
+ * `waitUntil`, bundled in from the application's own install of it — and
70
+ * without one the build stops, naming the modules that import it.
54
71
  */
55
72
  export declare const vercel: (options?: VercelOptions) => Plugin;
56
73
  export type { VercelOptions, VercelRoute };
package/dist/vercel.mjs CHANGED
@@ -1,8 +1,68 @@
1
- import { t as isFile, u as isParamSegment } from "./static-file-BEk_xO5y.mjs";
2
1
  import { a as sharedDir, t as declaredHeaders } from "./headers-BxB_jJVk.mjs";
2
+ import { d as isParamSegment, t as isFile } from "./static-file-BS-WEiwu.mjs";
3
3
  import { withBase } from "@k8ordo/router";
4
4
  import { cp, rm, writeFile } from "node:fs/promises";
5
5
  import path from "node:path";
6
+ import { build, parseSync } from "vite";
7
+ // src/vercel-wait-until.ts
8
+ /** The entry `waitUntil` is imported from. */
9
+ const SERVER_ENTRY = "@k8ordo/framework/server";
10
+ /** The package Vercel keeps a function running through. */
11
+ const VERCEL_FUNCTIONS = "@vercel/functions";
12
+ const BUNDLE = "virtual:k8ordo/vercel-wait-until";
13
+ /** The file in the function that is `@vercel/functions`' `waitUntil`. */
14
+ const WAIT_UNTIL_FILE = "wait-until.mjs";
15
+ /**
16
+ * Whether a compiled module takes `waitUntil` from `@k8ordo/framework/server`:
17
+ * imported or re-exported by name, or possibly, through `import * as` or
18
+ * `export *`, which name nothing — counted, since the other mistake is a
19
+ * function Vercel stops with the work unfinished. A type-only import is gone
20
+ * by the time a module is compiled.
21
+ */
22
+ const importsWaitUntil = (code) => {
23
+ if (!code.includes(SERVER_ENTRY)) return false;
24
+ const { errors, module } = parseSync("module.js", code, { sourceType: "module" });
25
+ if (errors.length > 0) return false;
26
+ return module.staticImports.some(({ moduleRequest, entries }) => moduleRequest.value === SERVER_ENTRY && entries.some(({ importName }) => importName.kind === "NamespaceObject" || importName.name === "waitUntil")) || module.staticExports.some(({ entries }) => entries.some(({ moduleRequest, importName }) => moduleRequest?.value === SERVER_ENTRY && (importName.kind === "All" || importName.kind === "AllButDefault" || importName.name === "waitUntil")));
27
+ };
28
+ const missingFunctions = (users) => `the build for Vercel cannot keep the function running after its answer — on Vercel waitUntil() goes through ${VERCEL_FUNCTIONS}, which is not installed here, and ${users.length === 1 ? "this imports" : "these import"} waitUntil from ${SERVER_ENTRY}:\n${users.map((file) => ` ${file}`).join("\n")}\ninstall ${VERCEL_FUNCTIONS} in this application`;
29
+ /**
30
+ * `@vercel/functions`' `waitUntil`, as the application at `root` resolves
31
+ * the package, bundled into `WAIT_UNTIL_FILE` of the function at `func`: a
32
+ * function holds nothing but its own directory. A build of its own, as
33
+ * `dist/server.js` is, because what it bundles is not the application's code
34
+ * and takes none of its config. Without the package it stops, naming
35
+ * `users`, the modules that import `waitUntil`.
36
+ */
37
+ const bundleWaitUntil = async (root, func, users) => {
38
+ await build({
39
+ root,
40
+ configFile: false,
41
+ logLevel: "warn",
42
+ publicDir: false,
43
+ plugins: [{
44
+ name: "k8ordo:vercel-wait-until",
45
+ async resolveId(id) {
46
+ if (id === BUNDLE) return `\0${BUNDLE}`;
47
+ if (id !== VERCEL_FUNCTIONS) return null;
48
+ const found = await this.resolve(id, path.join(root, "package.json"), { skipSelf: true });
49
+ if (found === null) throw new Error(missingFunctions(users));
50
+ return found;
51
+ },
52
+ load: (id) => id === `\0${BUNDLE}` ? `export { waitUntil } from ${JSON.stringify(VERCEL_FUNCTIONS)};\n` : null
53
+ }],
54
+ environments: { ssr: { resolve: { noExternal: true } } },
55
+ build: {
56
+ ssr: true,
57
+ outDir: func,
58
+ emptyOutDir: false,
59
+ rolldownOptions: {
60
+ input: BUNDLE,
61
+ output: { entryFileNames: WAIT_UNTIL_FILE }
62
+ }
63
+ }
64
+ });
65
+ };
6
66
  // src/vercel-output.ts
7
67
  /** The function every request that names no file is rewritten to. */
8
68
  const FUNCTION = "handler";
@@ -120,28 +180,41 @@ const isCompressedCopy = async (file) => {
120
180
  };
121
181
  const json = (value) => `${JSON.stringify(value, null, 2)}\n`;
122
182
  /**
183
+ * The function's entry: the handler as `fetch`, and with `waitUntil`, the
184
+ * host's handed to it beside every request.
185
+ */
186
+ const entryFor = (handler, waitUntil) => waitUntil ? `import handler from './${handler}';\nimport { waitUntil } from './${WAIT_UNTIL_FILE}';\n\nexport default { fetch: (request) => handler(request, { waitUntil }) };\n` : `import handler from './${handler}';\n\nexport default { fetch: handler };\n`;
187
+ /**
123
188
  * The build laid out as Vercel's Build Output API (v3) reads it, under
124
189
  * `<root>/.vercel/output`: the client build as static files at Vite's `base`,
125
190
  * and the request handler as the one function behind them. Only `output/` is
126
191
  * replaced — `vercel pull` keeps the project's link and settings beside it.
192
+ * An application that calls `waitUntil()` gets `@vercel/functions`' in the
193
+ * function, and without the package installed nothing is written but the
194
+ * refusal.
127
195
  */
128
- const writeVercelOutput = async (root, dirs, base, headers = {}) => {
196
+ const writeVercelOutput = async (root, dirs, base, headers = {}, { maxDuration, regions, waitUntil = [] } = {}) => {
129
197
  const output = path.join(root, ".vercel", "output");
130
198
  await rm(output, {
131
199
  recursive: true,
132
200
  force: true
133
201
  });
202
+ const func = path.join(output, "functions", `${FUNCTION}.func`);
203
+ if (waitUntil.length > 0) await bundleWaitUntil(root, func, waitUntil);
134
204
  await cp(dirs.client, path.join(output, "static", base), {
135
205
  recursive: true,
136
206
  filter: async (source) => !await isCompressedCopy(source)
137
207
  });
138
- const func = path.join(output, "functions", `${FUNCTION}.func`);
139
208
  const shared = sharedDir(dirs.rsc, dirs.ssr);
140
209
  await Promise.all([dirs.rsc, dirs.ssr].map((dir) => cp(dir, path.join(func, path.relative(shared, dir)), { recursive: true })));
141
210
  const entry = path.relative(shared, path.join(dirs.rsc, "index.js")).split(path.sep).join("/");
142
- await writeFile(path.join(func, "index.mjs"), `import handler from './${entry}';\n\nexport default { fetch: handler };\n`);
211
+ await writeFile(path.join(func, "index.mjs"), entryFor(entry, waitUntil.length > 0));
143
212
  await writeFile(path.join(func, "package.json"), json({ type: "module" }));
144
- await writeFile(path.join(func, ".vc-config.json"), json(FUNCTION_CONFIG));
213
+ await writeFile(path.join(func, ".vc-config.json"), json({
214
+ ...FUNCTION_CONFIG,
215
+ ...maxDuration === void 0 ? {} : { maxDuration },
216
+ ...regions === void 0 ? {} : { regions }
217
+ }));
145
218
  await writeFile(path.join(output, "config.json"), json(configFor(base, headers)));
146
219
  };
147
220
  /**
@@ -178,15 +251,22 @@ const writeStaticVercelOutput = async ({ root, client, base }, output, routes =
178
251
  * own routes around them, written as given: `beforeFiles` ahead of every
179
252
  * file, `beforeNotFound` ahead of `404.html`. A file a `route.ts` wrote is
180
253
  * served with the `Content-Type` its `GET` answered.
254
+ *
255
+ * Vercel stops a function once its answer is sent, unless told through
256
+ * `@vercel/functions`. So when the application imports `waitUntil` from
257
+ * `@k8ordo/framework/server`, the function hands the handler that package's
258
+ * `waitUntil`, bundled in from the application's own install of it — and
259
+ * without one the build stops, naming the modules that import it.
181
260
  */
182
261
  const vercel = (options = {}) => {
183
- const { beforeFiles, beforeNotFound } = options;
262
+ const { beforeFiles, beforeNotFound, maxDuration, regions } = options;
184
263
  let isStatic = false;
185
264
  let logger;
186
265
  let root = "";
187
266
  let base = "/";
188
267
  let client = "";
189
268
  let headers = {};
269
+ const waitUntilUsers = /* @__PURE__ */ new Set();
190
270
  return {
191
271
  name: "k8ordo:vercel",
192
272
  apply: "build",
@@ -196,6 +276,8 @@ const vercel = (options = {}) => {
196
276
  headers = declaredHeaders(config.plugins);
197
277
  client = path.resolve(root, config.environments.client?.build.outDir ?? "dist/client");
198
278
  if (!isStatic && (beforeFiles !== void 0 || beforeNotFound !== void 0)) throw new Error("vercel() takes beforeFiles and beforeNotFound under mode: 'static' only — under mode: 'server' the handler answers every URL no file names");
279
+ if (isStatic && (maxDuration !== void 0 || regions !== void 0)) throw new Error("vercel() takes maxDuration and regions under mode: 'server' only — under mode: 'static' there is no function for them to configure");
280
+ if (maxDuration !== void 0 && maxDuration !== "max" && !(Number.isInteger(maxDuration) && maxDuration > 0)) throw new Error(`vercel() takes maxDuration as a whole number of seconds or 'max', and got ${String(maxDuration)}`);
199
281
  },
200
282
  api: { async writeStatic(output) {
201
283
  await writeStaticVercelOutput({
@@ -205,6 +287,17 @@ const vercel = (options = {}) => {
205
287
  }, output, options, headers);
206
288
  logger?.info("k8ordo: wrote .vercel/output");
207
289
  } },
290
+ transform: {
291
+ order: "post",
292
+ handler(code, id) {
293
+ if (isStatic || this.environment.name !== "rsc") return null;
294
+ if (importsWaitUntil(code)) {
295
+ const [module = id] = id.split("?");
296
+ waitUntilUsers.add(path.relative(root, module));
297
+ }
298
+ return null;
299
+ }
300
+ },
208
301
  buildApp: {
209
302
  order: "post",
210
303
  async handler(builder) {
@@ -218,7 +311,11 @@ const vercel = (options = {}) => {
218
311
  client: dirOf("client"),
219
312
  rsc: dirOf("rsc"),
220
313
  ssr: dirOf("ssr")
221
- }, builder.config.base, headers);
314
+ }, builder.config.base, headers, {
315
+ maxDuration,
316
+ regions,
317
+ waitUntil: [...waitUntilUsers].toSorted()
318
+ });
222
319
  builder.config.logger.info("k8ordo: wrote .vercel/output");
223
320
  }
224
321
  }
package/dist/vite.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as ContentSecurityPolicy, r as EngineOptions, t as StaticOptions } from "./static-c-nX9Juk.mjs";
1
+ import { i as EngineOptions, n as ContentSecurityPolicy, r as scriptHash, t as StaticOptions } from "./static-Be3xIqd8.mjs";
2
2
  import { PluginOption } from "vite";
3
3
  export type ServerOptions = EngineOptions & {
4
4
  /**
@@ -7,7 +7,10 @@ export type ServerOptions = EngineOptions & {
7
7
  */
8
8
  readonly mode: 'server';
9
9
  };
10
- /** `site` and `csp` describe files, so only `mode: 'static'` takes them. */
10
+ /**
11
+ * `site` and `csp` describe files, so only `mode: 'static'` takes them, and
12
+ * it needs `site`.
13
+ */
11
14
  export type FrameworkOptions = StaticOptions | ServerOptions;
12
15
  /**
13
16
  * The application: `routes/` compiled into a route table and a request
@@ -15,4 +18,4 @@ export type FrameworkOptions = StaticOptions | ServerOptions;
15
18
  * every route, written to files (`'static'`), or per request (`'server'`).
16
19
  */
17
20
  export declare const framework: (options: FrameworkOptions) => PluginOption[];
18
- export type { ContentSecurityPolicy, StaticOptions };
21
+ export { type ContentSecurityPolicy, type StaticOptions, scriptHash };