@ngrok/app-server 0.4.1 → 0.5.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 (119) hide show
  1. package/README.md +223 -64
  2. package/dist/brotli-only-BVAagKt3.js +63 -0
  3. package/dist/build-id-CwMbED1n.js +245 -0
  4. package/dist/{build-manifest-DBMWe3Lx.js → build-manifest-CoBztw_6.js} +13 -4
  5. package/dist/bun-BCz6K_Xv.js +1 -2
  6. package/dist/check-count-DQl5z_qe.js +17 -0
  7. package/dist/cli.js +9 -6
  8. package/dist/client-browser.d.ts +2 -3
  9. package/dist/client-browser.js +1 -1
  10. package/dist/client.d.ts +5 -4
  11. package/dist/client.js +9 -6
  12. package/dist/{compress-Cp5NfXCC.js → compress-2w3jqBh7.js} +2 -3
  13. package/dist/{compression-CT2mWlKM.js → compression-uJxu0JVb.js} +32 -5
  14. package/dist/content-types-Le-5BNEC.js +10 -0
  15. package/dist/{create-server-Bp1yILQk.js → create-server-DVTZun6D.js} +617 -306
  16. package/dist/create-server-PA0qniaK.js +3 -0
  17. package/dist/{create-server-X2GR-U6t.d.ts → create-server-UVsHFQyK.d.ts} +116 -54
  18. package/dist/deno-aqhikNMG.js +1 -2
  19. package/dist/{entry-BoxCqc6w.js → entry-CMhX3yf8.js} +85 -65
  20. package/dist/entry-CvZ4U0ia.js +3 -0
  21. package/dist/{entry-DZTCaL3d.d.ts → entry-DBq_3s8X.d.ts} +10 -36
  22. package/dist/entry-protocol-C30UPXzy.js +14 -0
  23. package/dist/entry-source-DSE5NAJu.js +70 -0
  24. package/dist/entry-source-SHoE-DUP.js +3 -0
  25. package/dist/exit-Hh-O3gp4.js +83 -0
  26. package/dist/{header-hook-Vzw9J9BY.js → header-hook-JfqaTt34.js} +69 -23
  27. package/dist/{index-B8LsM6Mh.d.ts → index-gzGs53Ue.d.ts} +43 -38
  28. package/dist/index.d.ts +88 -44
  29. package/dist/index.js +216 -84
  30. package/dist/listen-options-7TaXjyrk.js +1 -2
  31. package/dist/{load-entry-CCPWbsrj.js → load-entry-Bgq1_HDV.js} +11 -12
  32. package/dist/{log-DQAS5gGT.js → log-DfC9D5_T.js} +70 -49
  33. package/dist/{node-D6m6e6EI.js → node-DQPrMFyc.js} +20 -8
  34. package/dist/node-DyKDFII0.js +263 -0
  35. package/dist/node-vite.d.ts +2 -3
  36. package/dist/node-vite.js +1 -1
  37. package/dist/node.d.ts +41 -64
  38. package/dist/node.js +3 -3
  39. package/dist/react-router-vite.d.ts +2 -3
  40. package/dist/react-router-vite.js +1 -1
  41. package/dist/react-router.d.ts +6 -17
  42. package/dist/react-router.js +9 -14
  43. package/dist/request-context-Cslbrscf.js +163 -0
  44. package/dist/{response-edge-CU8ZvL_F.js → response-edge-Ow5Pi5qF.js} +13 -10
  45. package/dist/{response-writer-D7KHfr8n.js → response-writer-Dd4Ey99f.js} +57 -21
  46. package/dist/{script-hashes-CU7P9oZU.js → script-hashes-BdvJ0QI_.js} +2 -3
  47. package/dist/serve-AatMo9k6.js +26 -0
  48. package/dist/{stale-build-navigation-rg5xxmTr.js → stale-build-navigation-Qf_Lem39.js} +4 -5
  49. package/dist/{stale-build-navigation-ypV7Ow4o.d.ts → stale-build-navigation-WjhD8zHi.d.ts} +4 -4
  50. package/dist/start-DYFyGKsM.js +470 -0
  51. package/dist/static-files-BdTPam5b.js +438 -0
  52. package/dist/static-pages-6IBHdYQc.d.ts +30 -0
  53. package/dist/tanstack-start-vite.d.ts +2 -3
  54. package/dist/tanstack-start-vite.js +1 -1
  55. package/dist/tanstack-start.d.ts +3 -4
  56. package/dist/tanstack-start.js +2 -3
  57. package/dist/tcp-3v4pmnYU.js +58 -0
  58. package/dist/testing.d.ts +8 -6
  59. package/dist/testing.js +19 -17
  60. package/dist/{trailing-slash-D2QWKWEu.d.ts → trailing-slash-C-yBKCib.d.ts} +2 -3
  61. package/dist/{types-MgFtkNQW.d.ts → types-KsKRDwAv.d.ts} +97 -46
  62. package/dist/vercel-pages.d.ts +3 -16
  63. package/dist/vercel-pages.js +1 -2
  64. package/dist/vercel-vite.d.ts +4 -5
  65. package/dist/vercel-vite.js +48 -44
  66. package/dist/vercel.d.ts +13 -10
  67. package/dist/vercel.js +48 -35
  68. package/dist/{vite-D4hfj3ph.js → vite-Brb8YR9I.js} +2 -7
  69. package/dist/{vite-B-GfswoH.js → vite-CHppJWp_.js} +5 -7
  70. package/dist/{vite-DyrhyKEr.js → vite-DjlHdyTM.js} +2 -7
  71. package/dist/vite.d.ts +2 -2
  72. package/dist/vite.js +885 -2
  73. package/package.json +13 -12
  74. package/dist/brotli-only-DrA29Ecw.js +0 -97
  75. package/dist/brotli-only-DrA29Ecw.js.map +0 -1
  76. package/dist/build-id-B0448vZH.js +0 -151
  77. package/dist/build-id-B0448vZH.js.map +0 -1
  78. package/dist/build-manifest-DBMWe3Lx.js.map +0 -1
  79. package/dist/bun-BCz6K_Xv.js.map +0 -1
  80. package/dist/cli.js.map +0 -1
  81. package/dist/client.js.map +0 -1
  82. package/dist/compress-Cp5NfXCC.js.map +0 -1
  83. package/dist/compression-CT2mWlKM.js.map +0 -1
  84. package/dist/create-server-Bp1yILQk.js.map +0 -1
  85. package/dist/deno-aqhikNMG.js.map +0 -1
  86. package/dist/entry-BoxCqc6w.js.map +0 -1
  87. package/dist/header-hook-Vzw9J9BY.js.map +0 -1
  88. package/dist/index.js.map +0 -1
  89. package/dist/listen-options-7TaXjyrk.js.map +0 -1
  90. package/dist/load-entry-CCPWbsrj.js.map +0 -1
  91. package/dist/log-DQAS5gGT.js.map +0 -1
  92. package/dist/node-CVVdk2N6.js +0 -334
  93. package/dist/node-CVVdk2N6.js.map +0 -1
  94. package/dist/node-D6m6e6EI.js.map +0 -1
  95. package/dist/react-router.js.map +0 -1
  96. package/dist/request-context-Bxdbk5P1.js +0 -80
  97. package/dist/request-context-Bxdbk5P1.js.map +0 -1
  98. package/dist/response-edge-CU8ZvL_F.js.map +0 -1
  99. package/dist/response-writer-D7KHfr8n.js.map +0 -1
  100. package/dist/script-hashes-CU7P9oZU.js.map +0 -1
  101. package/dist/serve-W4WacdOw.js +0 -29
  102. package/dist/serve-W4WacdOw.js.map +0 -1
  103. package/dist/stale-build-navigation-rg5xxmTr.js.map +0 -1
  104. package/dist/start-j0VlxeiI.js +0 -324
  105. package/dist/start-j0VlxeiI.js.map +0 -1
  106. package/dist/static-files-C2ea8KQ6.js +0 -310
  107. package/dist/static-files-C2ea8KQ6.js.map +0 -1
  108. package/dist/tanstack-start.js.map +0 -1
  109. package/dist/tcp-BgDXbjOG.js +0 -42
  110. package/dist/tcp-BgDXbjOG.js.map +0 -1
  111. package/dist/testing.js.map +0 -1
  112. package/dist/vercel-pages.js.map +0 -1
  113. package/dist/vercel-vite.js.map +0 -1
  114. package/dist/vercel.js.map +0 -1
  115. package/dist/vite-B-GfswoH.js.map +0 -1
  116. package/dist/vite-D4hfj3ph.js.map +0 -1
  117. package/dist/vite-DPiSKHDn.js +0 -870
  118. package/dist/vite-DPiSKHDn.js.map +0 -1
  119. package/dist/vite-DyrhyKEr.js.map +0 -1
@@ -0,0 +1,3 @@
1
+ import { t as createEntry } from "./entry-CMhX3yf8.js";
2
+
3
+ export { createEntry };
@@ -1,37 +1,9 @@
1
- import { _ as Mode, a as FrameworkRuntime, c as HostRuntime, d as ListenerBindings, r as FetchHandler, t as AppServerEnv } from "./types-MgFtkNQW.js";
2
- import { t as AppServer } from "./create-server-X2GR-U6t.js";
1
+ import { t as AppServer } from "./create-server-UVsHFQyK.js";
2
+ import { n as StaticPage } from "./static-pages-6IBHdYQc.js";
3
+ import { a as FrameworkRuntime, g as Mode, r as FetchHandler, s as HostRuntime, t as AppServerEnv, u as ListenerBindings } from "./types-KsKRDwAv.js";
3
4
  import { MiddlewareHandler } from "hono";
4
- /** What a content delivery network (CDN) sends for a prerendered path that it can serve with no server. */
5
- type CdnPage = {
6
- /** The URL path of the client build's file that the answer comes from, such as `/about/index.html`. */
7
- file: string;
8
- /** The body that the pipeline sent. It can differ from the file, such as with `runtimeEnv` values. */
9
- body: Uint8Array;
10
- /**
11
- * The response headers for each case of the presence headers. `present`
12
- * lists the headers that the request has. With no presence headers, there
13
- * is one case.
14
- */
15
- answers: {
16
- present: readonly string[];
17
- headers: Readonly<Record<string, string>>;
18
- }[];
19
- /**
20
- * The path of the page's markdown twin. A request whose `Accept` names
21
- * `text/markdown` must reach the server, which negotiates the answer.
22
- */
23
- twin?: string;
24
- };
25
- /** A prerendered path, with its CDN answer, or with the reason that the server must answer it. */
26
- type StaticPage = {
27
- path: string;
28
- } & ({
29
- cdn: CdnPage;
30
- } | {
31
- reason: string;
32
- });
33
5
  /** The version of the contract between the CLI and the `appServer` export of a build. */
34
- declare const ENTRY_PROTOCOL = 2;
6
+ declare const ENTRY_PROTOCOL = 3;
35
7
  /**
36
8
  * What a server build exports as `appServer`. The CLI calls it, so the copy
37
9
  * of this package in the build runs the server, and the CLI's copy runs
@@ -52,8 +24,11 @@ type AppServerEntry = {
52
24
  * listen. The first call runs each plugin's `setup`.
53
25
  */
54
26
  fetch: FetchHandler;
55
- /** Development: answers one request with the dev server inside the pipeline. */
56
- fetchDev: (request: Request, bindings: ListenerBindings, devServer: MiddlewareHandler<AppServerEnv>) => Promise<Response>;
27
+ /** Development: answers one request with `devServer` inside the pipeline. */
28
+ fetchDev: (request: Request, options: {
29
+ devServer: MiddlewareHandler<AppServerEnv>;
30
+ bindings?: ListenerBindings;
31
+ }) => Promise<Response>;
57
32
  /**
58
33
  * Production, for a build step: each prerendered path, with the answer
59
34
  * that a content delivery network (CDN) can send with no server, or the
@@ -90,5 +65,4 @@ type CreateEntryOptions = {
90
65
  * export const appServer = createEntry({ app, host, framework: (mode) => runtime(build, { mode }) });
91
66
  */
92
67
  declare function createEntry({ app, host, framework, clientDirectory }: CreateEntryOptions): AppServerEntry;
93
- export { CreateEntryOptions as n, createEntry as r, AppServerEntry as t };
94
- //# sourceMappingURL=entry-DZTCaL3d.d.ts.map
68
+ export { CreateEntryOptions as n, createEntry as r, AppServerEntry as t };
@@ -0,0 +1,14 @@
1
+ // src/core/entry-protocol.ts
2
+ /** The version of the contract between the CLI and the `appServer` export of a build. */
3
+ const ENTRY_PROTOCOL = 3;
4
+ /**
5
+ * Whether a value is an entry of this protocol version.
6
+ *
7
+ * @example
8
+ * isAppServerEntry((await import("./build/server/app-server.js")).appServer); // => true
9
+ */
10
+ function isAppServerEntry(value) {
11
+ return typeof value === "object" && value != null && "protocol" in value && value.protocol === 3;
12
+ }
13
+
14
+ export { isAppServerEntry as t };
@@ -0,0 +1,70 @@
1
+ // src/vite/entry-source.ts
2
+ /** The module ID of the server entry that `appServer` generates. */
3
+ const SERVER_ENTRY_ID = "virtual:@ngrok/app-server/entry";
4
+ /** What the plugin resolves {@link SERVER_ENTRY_ID} to. Why `\0`: no other plugin then loads or transforms the ID as a file. */
5
+ const RESOLVED_SERVER_ENTRY_ID = `\0${SERVER_ENTRY_ID}`;
6
+ /**
7
+ * The `createServer` options that the environment gives the default app: the
8
+ * app of a build with no `server.ts`. `TRUST_FORWARDED_PROTO` is `1` or `0`,
9
+ * and `TRUSTED_PROXY_HOPS` is an integer of 0 or more. No value and an empty
10
+ * value keep the default. Each other value throws, so it stops startup.
11
+ *
12
+ * Why no binding from outside its body: {@link serverEntrySource} writes the
13
+ * source of this function into the server entry, so the entry and the CLI
14
+ * share one parser.
15
+ *
16
+ * @example
17
+ * environmentOptions({ TRUST_FORWARDED_PROTO: "1", TRUSTED_PROXY_HOPS: "2" });
18
+ * // => { trustForwardedProto: true, trustedProxyHops: 2 }
19
+ */
20
+ function environmentOptions(env) {
21
+ const proto = env.TRUST_FORWARDED_PROTO ?? "";
22
+ if (proto !== "" && proto !== "0" && proto !== "1") throw new Error(`TRUST_FORWARDED_PROTO must be "1" or "0", not ${JSON.stringify(proto)}. Set TRUST_FORWARDED_PROTO=1 to trust X-Forwarded-Proto, or remove the variable.`);
23
+ const hops = env.TRUSTED_PROXY_HOPS ?? "";
24
+ if (!/^\d*$/u.test(hops) || !Number.isSafeInteger(Number(hops))) throw new Error(`TRUSTED_PROXY_HOPS must be an integer of 0 or more, not ${JSON.stringify(hops)}. Set it to the number of proxies in front of the server, such as TRUSTED_PROXY_HOPS=1, or remove the variable.`);
25
+ return {
26
+ trustForwardedProto: proto === "1",
27
+ trustedProxyHops: Number(hops)
28
+ };
29
+ }
30
+ /**
31
+ * The source of the server entry. When a runtime runs the file as its main
32
+ * module, such as `node build/server/app-server.js`, the entry starts the
33
+ * server. When the CLI imports it, the CLI starts the server.
34
+ *
35
+ * @example
36
+ * serverEntrySource({ framework: reactRouterAdapter(), host: node(), appModule: "/app/server.ts", clientDirectory: '"/app/build/client"' });
37
+ */
38
+ function serverEntrySource({ framework, host, appModule, clientDirectory }) {
39
+ const modules = [
40
+ framework.serverModule,
41
+ "@ngrok/app-server",
42
+ framework.runtimeModule,
43
+ host.runtimeModule,
44
+ ...appModule == null ? [] : [appModule]
45
+ ];
46
+ return [
47
+ "if (import.meta.main) {",
48
+ " process.env.NODE_ENV ??= \"production\";",
49
+ " if (typeof process.setSourceMapsEnabled === \"function\") {",
50
+ " process.setSourceMapsEnabled(true);",
51
+ " }",
52
+ "}",
53
+ `const [serverModule, { createEntry, createServer }, { runtime }, hostModule${appModule == null ? "" : ", { default: app }"}] = await Promise.all([`,
54
+ ...modules.map((module) => `\timport(${JSON.stringify(module)}),`),
55
+ "]);",
56
+ ...appModule == null ? [`const app = createServer((${environmentOptions.toString()})(process.env));`] : [],
57
+ `export const appServer = createEntry({`,
58
+ `\tapp,`,
59
+ `\thost: hostModule.host,`,
60
+ `\tclientDirectory: ${clientDirectory},`,
61
+ `\tframework: (mode) => runtime(serverModule, { ${framework.runtimeOptions == null ? "" : `...${framework.runtimeOptions}, `}mode }),`,
62
+ `});`,
63
+ "export { hostModule };",
64
+ "if (import.meta.main) {",
65
+ " await appServer.start();",
66
+ "}"
67
+ ].join("\n");
68
+ }
69
+
70
+ export { serverEntrySource as i, SERVER_ENTRY_ID as n, environmentOptions as r, RESOLVED_SERVER_ENTRY_ID as t };
@@ -0,0 +1,3 @@
1
+ import { i as serverEntrySource, n as SERVER_ENTRY_ID, r as environmentOptions, t as RESOLVED_SERVER_ENTRY_ID } from "./entry-source-DSE5NAJu.js";
2
+
3
+ export { RESOLVED_SERVER_ENTRY_ID, SERVER_ENTRY_ID, environmentOptions, serverEntrySource };
@@ -0,0 +1,83 @@
1
+ import { setImmediate, setTimeout } from "node:timers";
2
+
3
+ // src/node/stdout.ts
4
+ /** The standard output (stdout) lines that wait for {@link flushLogs}. */
5
+ let pending = "";
6
+ /** Whether the process emits `exit`. Then no turn of the event loop follows, so each line goes to stdout at once. */
7
+ let exiting = false;
8
+ process.on("exit", () => {
9
+ exiting = true;
10
+ process.stdout._handle?.setBlocking?.(true);
11
+ for (const line of pending.match(/[^\n]*\n/gu) ?? []) process.stdout.write(line);
12
+ pending = "";
13
+ });
14
+ /**
15
+ * Writes the stdout lines of {@link logWriter} that wait, in one write. Call
16
+ * it before the process exits.
17
+ */
18
+ function flushLogs() {
19
+ if (pending === "") return;
20
+ const lines = pending;
21
+ pending = "";
22
+ process.stdout.write(lines);
23
+ }
24
+ /**
25
+ * Adds `line` to the next stdout write. Why one write for each turn of the
26
+ * event loop: a write to a pipe, such as stdout in Docker, is a system call,
27
+ * and under load many requests end in one turn.
28
+ */
29
+ function writeStdout(line) {
30
+ if (exiting) {
31
+ process.stdout.write(line);
32
+ return;
33
+ }
34
+ if (pending === "") setImmediate(flushLogs);
35
+ pending += line;
36
+ }
37
+ /**
38
+ * The log writer of a host on Node.js, Bun, or Deno. It writes the lines of
39
+ * one event-loop turn to stdout in one write, at the end of the turn. Each
40
+ * line still reaches stdout when the process exits before that.
41
+ *
42
+ * @example
43
+ * export const host: HostRuntime = { files: nodeFiles, logWriter };
44
+ */
45
+ const logWriter = {
46
+ write: writeStdout,
47
+ flush: flushLogs
48
+ };
49
+
50
+ // src/node/exit.ts
51
+ /** How long the exit waits for stdout and stderr. */
52
+ const FLUSH_TIMEOUT_MS = 1e3;
53
+ /**
54
+ * Exits the process after stdout and stderr write what they hold, so the last
55
+ * log lines are not lost. The exit code is `process.exitCode`, else 0.
56
+ *
57
+ * Why exit at all: when shutdown is done, the server holds nothing open. A
58
+ * timer that the app left, such as the render abort timer in React Router's
59
+ * `entry.server`, would otherwise keep the stopped process alive.
60
+ */
61
+ function exitAfterFlush() {
62
+ flushLogs();
63
+ let pending = 2;
64
+ const done = () => {
65
+ pending -= 1;
66
+ if (pending === 0) process.exit();
67
+ };
68
+ flush(process.stdout, done);
69
+ flush(process.stderr, done);
70
+ setTimeout(() => {
71
+ process.exit();
72
+ }, FLUSH_TIMEOUT_MS).unref();
73
+ }
74
+ /** Calls `done` after `stream` writes what it holds. */
75
+ function flush(stream, done) {
76
+ if ("Bun" in globalThis) {
77
+ stream.end(done);
78
+ return;
79
+ }
80
+ stream.write("", done);
81
+ }
82
+
83
+ export { flushLogs as n, logWriter as r, exitAfterFlush as t };
@@ -59,27 +59,63 @@ function responseHeaders(context) {
59
59
  * server, the hook changes the head once, where the response leaves, so the
60
60
  * response needs no copy. That run comes after the code that each stage runs
61
61
  * after its `next()`, so a stage can decide there what its hook does. A hook
62
- * that throws gives a 500. See {@link HeaderHook} for the order of the hooks
63
- * and the views that also run them.
62
+ * that throws gets the answer of the app's `onError`, as a stage that throws
63
+ * does. See {@link HeaderHook} for the order of the hooks and the views that
64
+ * also run them.
64
65
  *
65
66
  * @example
66
67
  * const frameOptions: Plugin = {
67
68
  * name: "frame-options",
68
- * middleware: async (c, next) =>
69
- * withHeaderHook(c, next, (response) => {
70
- * response.set("X-Frame-Options", "DENY");
69
+ * middleware: async (context, next) =>
70
+ * withHeaderHook({
71
+ * context,
72
+ * next,
73
+ * hook: (response) => {
74
+ * response.set("X-Frame-Options", "DENY");
75
+ * },
71
76
  * }),
72
77
  * };
73
78
  */
74
- async function withHeaderHook(context, next, hook) {
79
+ function withHeaderHook({ context, next, hook }) {
75
80
  const hooks = context.get("headerHooks");
76
- if (hooks == null) {
77
- await next();
78
- hook(responseHeaders(context));
79
- return;
80
- }
81
+ if (hooks == null) return hookAfter({
82
+ context,
83
+ next,
84
+ hook
85
+ });
81
86
  hooks.push(hook);
87
+ return next();
88
+ }
89
+ /**
90
+ * {@link withHeaderHook} for a hook that keeps the body that a stage picked,
91
+ * as each hook of a built-in plugin does: it never adds `no-transform` to
92
+ * `Cache-Control` or removes it, and it never sets or deletes
93
+ * `Content-Encoding` or `Content-Range`. So a stage runs no view for it.
94
+ *
95
+ * @example
96
+ * withBuiltInHeaderHook({
97
+ * context,
98
+ * next,
99
+ * hook: (headers) => {
100
+ * headers.set("X-Robots-Tag", "noindex, nofollow");
101
+ * },
102
+ * });
103
+ */
104
+ function withBuiltInHeaderHook({ context, next, hook }) {
105
+ const hooks = context.get("headerHooks");
106
+ if (hooks == null) return hookAfter({
107
+ context,
108
+ next,
109
+ hook
110
+ });
111
+ hooks.builtIn = (hooks.builtIn ?? 0) + 1;
112
+ hooks.push(hook);
113
+ return next();
114
+ }
115
+ /** {@link withHeaderHook} outside an app server, such as in a plain Hono app, where nothing runs the hook later. */
116
+ async function hookAfter({ context, next, hook }) {
82
117
  await next();
118
+ hook(responseHeaders(context));
83
119
  }
84
120
  /**
85
121
  * The index of the lowercase `name` in a flat `[name, value, …]` header list, or -1.
@@ -136,24 +172,34 @@ function listView(head) {
136
172
  };
137
173
  }
138
174
  /**
139
- * A view of `headers` after the header hook of each stage that the request
140
- * passed, in the order that the server runs them. Why: a stage that picks a
141
- * body by a header, such as `Cache-Control: no-transform`, must see the value
142
- * that a hook sets where the response leaves. So a hook can run more than
143
- * once. Why a view: the hooks change only the view, so `headers` needs no
144
- * copy.
175
+ * The header hooks of the request, when one of them is not from
176
+ * {@link withBuiltInHeaderHook} and so can change the body that a stage
177
+ * picks. With `undefined`, a stage reads the headers as they are.
178
+ *
179
+ * @example
180
+ * const hooks = bodyHooks(context);
181
+ * const sent = hooks == null ? undefined : hookedView(hooks, ["cache-control", "public"]);
182
+ */
183
+ function bodyHooks(context) {
184
+ const hooks = context.get("headerHooks");
185
+ return hooks == null || hooks.length === (hooks.builtIn ?? 0) ? void 0 : hooks;
186
+ }
187
+ /**
188
+ * A view of the flat `[name, value, …]` list `head` after `hooks`, in the
189
+ * order that the server runs them. Why: a stage that picks a body by a
190
+ * header, such as `Cache-Control: no-transform`, must see the value that a
191
+ * hook sets where the response leaves. So a hook can run more than once. The
192
+ * hooks change `head`.
145
193
  *
146
194
  * @example
147
- * forbidsTransform(hookedHeaders(context, headers).get("Cache-Control"));
195
+ * hookedView(hooks, ["cache-control", "public"]).get("Cache-Control");
148
196
  */
149
- function hookedHeaders(context, headers) {
150
- const view = listView(headerList(headers));
151
- const hooks = context.get("headerHooks") ?? [];
197
+ function hookedView(hooks, head) {
198
+ const view = listView(head);
152
199
  for (let index = hooks.length - 1; index >= 0; index -= 1) try {
153
200
  hooks[index]?.(view);
154
201
  } catch {}
155
202
  return view;
156
203
  }
157
204
 
158
- export { replaceResponse as a, listView as i, headerList as n, withHeaderHook as o, hookedHeaders as r, writableHeaders as s, headerIndex as t };
159
- //# sourceMappingURL=header-hook-Vzw9J9BY.js.map
205
+ export { listView as a, withHeaderHook as c, hookedView as i, writableHeaders as l, headerIndex as n, replaceResponse as o, headerList as r, withBuiltInHeaderHook as s, bodyHooks as t };
@@ -1,18 +1,17 @@
1
- import { Plugin, PreviewServer, ResolvedConfig } from "vite";
2
- /** The module ID of the server entry that {@link appServer} generates. */
3
- declare const SERVER_ENTRY_ID = "virtual:@ngrok/app-server/entry";
1
+ import { Logger, Plugin, PreviewServer } from "vite";
2
+ /** A content coding of a compressed copy that the build writes. */
3
+ type Encoding = "br" | "gzip";
4
4
  /**
5
5
  * The build side of a framework adapter. The Vite config imports it, such as
6
6
  * `reactRouterAdapter()` from `@ngrok/app-server/react-router/vite`.
7
7
  */
8
8
  type FrameworkBuild = {
9
- /** A name for logs and errors. */
10
- name: string;
11
9
  /** The Vite ID of the framework's server module, such as `virtual:react-router/server-build`. */
12
10
  serverModule: string;
13
11
  /**
14
- * The module whose `runtime` export makes the run side from the server
15
- * module. It has the type `FrameworkRuntimeFactory`.
12
+ * The module whose `runtime` export makes the run side. The server entry
13
+ * calls `runtime(serverModule, { ...runtimeOptions, mode })`. The call
14
+ * returns a `FrameworkRuntime`.
16
15
  */
17
16
  runtimeModule: string;
18
17
  /**
@@ -21,32 +20,46 @@ type FrameworkBuild = {
21
20
  * it, because the source is part of the server entry.
22
21
  */
23
22
  runtimeOptions?: string;
23
+ /**
24
+ * Content types that `mime` does not know, by file extension, such as
25
+ * `{ ".data": "text/x-script" }`. Give the `contentTypes` of the run side.
26
+ * Why: the build writes the compressed copies of each file with a
27
+ * compressible type. With no copy, the server compresses such a file for
28
+ * each request.
29
+ */
30
+ contentTypes?: Readonly<Record<string, string>>;
24
31
  /** Whether a preview server runs the framework's prerender pass. */
25
32
  isPrerenderPass: (server: PreviewServer) => boolean;
26
- /** Throws for a config that the adapter does not support, so the build fails, not the first request. */
27
- validate?: (config: ResolvedConfig) => void;
28
33
  };
29
34
  /**
30
35
  * The build side of a host adapter. The Vite config imports it, such as
31
36
  * `node()` from `@ngrok/app-server/node/vite`.
32
37
  */
33
38
  type HostBuild = {
34
- /** A name for logs and errors. */
35
- name: string;
36
39
  /** The module whose `host` export is the run side, a `HostRuntime`. */
37
40
  runtimeModule: string;
38
- /** The file name of the server entry in the server build, such as `app-server.js`. */
39
- entryFileName: string;
41
+ /**
42
+ * The encodings of the compressed copies that the host reads for one file
43
+ * of the client build. `path` is the URL path of the file, such as
44
+ * `/assets/app-abc.js`, and `assetsPath` is the URL path of Vite's
45
+ * `build.assetsDir`. The build writes no other copy of the file. Why: a
46
+ * host whose output holds only some copies does not pay for the others.
47
+ * @default each encoding: gzip and brotli
48
+ */
49
+ copies?: (file: {
50
+ path: string;
51
+ assetsPath: string;
52
+ }) => readonly Encoding[];
40
53
  /**
41
54
  * Writes the host's deploy output, such as Vercel's `.vercel/output`. It
42
- * runs after each prerender pass and after the build, so it can run two
43
- * times. Why after the pass too: when `appServer` comes before the
44
- * framework's plugin, the pass runs after the build. Why sync: Vite does
45
- * not wait for the end of that pass.
55
+ * runs one time, after the build and the prerender pass. When `appServer`
56
+ * comes before the framework's plugin, the pass runs after the build. Then
57
+ * `output` runs after the build, with no prerendered file, and again after
58
+ * the pass. Why sync: Vite does not wait for the end of that pass.
46
59
  */
47
60
  output?: (build: HostOutput) => void;
48
61
  };
49
- /** What a host's `output` gets: the absolute paths of the build. */
62
+ /** What a host's `output` gets: the absolute paths of the build, and Vite's logger. */
50
63
  type HostOutput = {
51
64
  /** The Vite root. */
52
65
  root: string;
@@ -59,6 +72,12 @@ type HostOutput = {
59
72
  * `/assets/`. Its files have content hashes in their names.
60
73
  */
61
74
  assetsPath: string;
75
+ /**
76
+ * The logger of the Vite build, so Vite's `logLevel` and `customLogger`
77
+ * apply to what the host prints. After a prerender pass, it is still the
78
+ * logger of the build, not the logger of the framework's preview server.
79
+ */
80
+ logger: Pick<Logger, "info" | "warn">;
62
81
  };
63
82
  /** Options for {@link appServer}. */
64
83
  type AppServerOptions = {
@@ -74,7 +93,8 @@ type AppServerOptions = {
74
93
  * The module whose default export is the app from `createServer`, relative
75
94
  * to the Vite root. When omitted, the plugin uses `server.ts`, else
76
95
  * `server.js`, in the Vite root. When neither exists, the server uses its
77
- * default plugins.
96
+ * default plugins. A path that Vite resolves to no module stops
97
+ * `app-server start` and the build.
78
98
  */
79
99
  app?: string;
80
100
  /**
@@ -98,27 +118,12 @@ type AppServerOptions = {
98
118
  omit?: readonly string[];
99
119
  };
100
120
  };
101
- /** What {@link serverEntrySource} needs. */
102
- type EntrySource = {
103
- framework: Pick<FrameworkBuild, "serverModule" | "runtimeModule" | "runtimeOptions">;
104
- host: Pick<HostBuild, "runtimeModule">;
105
- /** The absolute path of the app module, else `undefined` for the default app. */
106
- appModule: string | undefined;
107
- /** The source of an expression that gives the client build: a path or a `file:` URL. */
108
- clientDirectory: string;
109
- };
110
- /**
111
- * The source of the server entry. When a runtime runs the file as its main
112
- * module, such as `node build/server/app-server.js`, the entry starts the
113
- * server. When the CLI imports it, the CLI starts the server.
114
- */
115
- declare function serverEntrySource({ framework, host, appModule, clientDirectory }: EntrySource): string;
116
121
  /**
117
122
  * The Vite plugin for `@ngrok/app-server`. Put it next to the framework's
118
123
  * plugin.
119
124
  *
120
125
  * - It adds the server entry to the server build, such as
121
- * `build/server/app-server.js` on the node host. The entry bundles the app
126
+ * `build/server/app-server.js`. The entry bundles the app
122
127
  * (the default export of `app`) with the framework's server module, so the
123
128
  * server gets the app's path aliases and its one copy of the framework.
124
129
  * The framework's own entry does not change. When a runtime runs the entry
@@ -141,12 +146,12 @@ declare function serverEntrySource({ framework, host, appModule, clientDirectory
141
146
  * `contentSecurityPolicy` plugin. It also writes a gzip copy (`.<file>.gz`)
142
147
  * and a brotli copy (`.<file>.br`) next to each text file of 1 KiB or more
143
148
  * in the client build, HTML included. The static stages send a copy, so the
144
- * server does not compress these files for each request.
149
+ * server does not compress these files for each request. A host can ask
150
+ * for fewer copies with `copies` of `HostBuild`, as `vercel()` does.
145
151
  *
146
152
  * @example
147
153
  * // vite.config.ts
148
154
  * export default defineConfig({ plugins: [reactRouter(), appServer()] });
149
155
  */
150
156
  declare function appServer(options?: AppServerOptions): Plugin[];
151
- export { SERVER_ENTRY_ID as a, HostOutput as i, FrameworkBuild as n, appServer as o, HostBuild as r, serverEntrySource as s, AppServerOptions as t };
152
- //# sourceMappingURL=index-B8LsM6Mh.d.ts.map
157
+ export { appServer as a, HostOutput as i, FrameworkBuild as n, HostBuild as r, AppServerOptions as t };