@ngrok/app-server 0.1.0 → 0.2.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 (70) hide show
  1. package/README.md +77 -22
  2. package/dist/build-manifest-8NfD1wnO.js +45 -0
  3. package/dist/build-manifest-8NfD1wnO.js.map +1 -0
  4. package/dist/{bun-DCGnDEPn.js → bun-BCz6K_Xv.js} +12 -8
  5. package/dist/bun-BCz6K_Xv.js.map +1 -0
  6. package/dist/cli.js +15 -13
  7. package/dist/cli.js.map +1 -1
  8. package/dist/{deno-BnMfk2n1.js → deno-DNRgzHrk.js} +4 -4
  9. package/dist/deno-DNRgzHrk.js.map +1 -0
  10. package/dist/{entry-2XXp2Ukh.js → entry-BkdUub2S.js} +179 -334
  11. package/dist/entry-BkdUub2S.js.map +1 -0
  12. package/dist/index-oSE-tYRW.d.ts +88 -0
  13. package/dist/index.d.ts +212 -27
  14. package/dist/index.js +26 -14
  15. package/dist/index.js.map +1 -1
  16. package/dist/{listen-options-DHWmSWGi.js → listen-options-7TaXjyrk.js} +2 -2
  17. package/dist/listen-options-7TaXjyrk.js.map +1 -0
  18. package/dist/load-entry-C03qqmhr.js +33 -0
  19. package/dist/load-entry-C03qqmhr.js.map +1 -0
  20. package/dist/log-CsjaKM8U.js +59 -0
  21. package/dist/log-CsjaKM8U.js.map +1 -0
  22. package/dist/{node-79my3Uix.js → node-CG7qXuWZ.js} +4 -4
  23. package/dist/node-CG7qXuWZ.js.map +1 -0
  24. package/dist/node-YWPGmzAk.js +215 -0
  25. package/dist/node-YWPGmzAk.js.map +1 -0
  26. package/dist/node-ZMowKZDB.js +3 -0
  27. package/dist/node-vite.d.ts +10 -0
  28. package/dist/node-vite.js +18 -0
  29. package/dist/node-vite.js.map +1 -0
  30. package/dist/node.d.ts +67 -0
  31. package/dist/node.js +4 -0
  32. package/dist/react-router-vite.d.ts +9 -0
  33. package/dist/react-router-vite.js +21 -0
  34. package/dist/react-router-vite.js.map +1 -0
  35. package/dist/react-router.d.ts +11 -24
  36. package/dist/react-router.js +15 -25
  37. package/dist/react-router.js.map +1 -1
  38. package/dist/serve-Cleo8RMO.js +29 -0
  39. package/dist/serve-Cleo8RMO.js.map +1 -0
  40. package/dist/start-CINALHxy.js +230 -0
  41. package/dist/start-CINALHxy.js.map +1 -0
  42. package/dist/tanstack-start-vite.d.ts +11 -0
  43. package/dist/tanstack-start-vite.js +24 -0
  44. package/dist/tanstack-start-vite.js.map +1 -0
  45. package/dist/tanstack-start.d.ts +17 -0
  46. package/dist/tanstack-start.js +50 -0
  47. package/dist/tanstack-start.js.map +1 -0
  48. package/dist/tcp-C-9AmFuM.js +41 -0
  49. package/dist/tcp-C-9AmFuM.js.map +1 -0
  50. package/dist/types-BfCkS7kQ.d.ts +203 -0
  51. package/dist/vite-mYZ2Y69u.js +144 -0
  52. package/dist/vite-mYZ2Y69u.js.map +1 -0
  53. package/dist/vite.d.ts +2 -27
  54. package/dist/vite.js +2 -2
  55. package/package.json +28 -2
  56. package/dist/bun-DCGnDEPn.js.map +0 -1
  57. package/dist/captured-headers-ewL_t3MQ.js +0 -48
  58. package/dist/captured-headers-ewL_t3MQ.js.map +0 -1
  59. package/dist/deno-BnMfk2n1.js.map +0 -1
  60. package/dist/dev-DCytOhpM.js +0 -132
  61. package/dist/dev-DCytOhpM.js.map +0 -1
  62. package/dist/entry-2XXp2Ukh.js.map +0 -1
  63. package/dist/entry-CIpwaayZ.d.ts +0 -293
  64. package/dist/listen-options-DHWmSWGi.js.map +0 -1
  65. package/dist/node-2wWBMFeO.js +0 -3
  66. package/dist/node-79my3Uix.js.map +0 -1
  67. package/dist/start-kxw_NM4q.js +0 -16
  68. package/dist/start-kxw_NM4q.js.map +0 -1
  69. package/dist/vite-CrNqpoeK.js +0 -102
  70. package/dist/vite-CrNqpoeK.js.map +0 -1
@@ -1,102 +0,0 @@
1
- import { n as capturedHeadersKey, t as CAPTURED_HEADERS_FILE } from "./captured-headers-ewL_t3MQ.js";
2
- import { join, resolve } from "node:path";
3
- import { writeFileSync } from "node:fs";
4
-
5
- // src/vite.ts
6
- /** The module ID of the server build entry that {@link serverEntry} generates. */
7
- const SERVER_ENTRY_ID = "virtual:@ngrok/app-server/entry";
8
- const RESOLVED_SERVER_ENTRY_ID = `\0${SERVER_ENTRY_ID}`;
9
- /** The header that turns on Node's header store for a response, so `finish` can read every header. */
10
- const CAPTURE_MARKER = "x-app-server-capture";
11
- /**
12
- * Headers that describe one transfer, not the resource. The server sets its
13
- * own, and a cookie must never replay to every visitor. `Vary` is left out
14
- * because the preview server adds `Vary: Origin` to every response.
15
- */
16
- const UNCAPTURED_HEADERS = /* @__PURE__ */ new Set([
17
- CAPTURE_MARKER,
18
- "connection",
19
- "content-encoding",
20
- "content-length",
21
- "date",
22
- "etag",
23
- "keep-alive",
24
- "last-modified",
25
- "set-cookie",
26
- "transfer-encoding",
27
- "vary"
28
- ]);
29
- /** The source of the server build entry. */
30
- function serverEntrySource(appModule) {
31
- return [
32
- `export * from "virtual:react-router/server-build";`,
33
- `import * as build from "virtual:react-router/server-build";`,
34
- `import { appServerEntry } from "@ngrok/app-server/react-router";`,
35
- ...appModule == null ? [`export const appServer = appServerEntry(build);`] : [`import app from ${JSON.stringify(appModule)};`, `export const appServer = appServerEntry(build, app);`]
36
- ].join("\n");
37
- }
38
- /**
39
- * Records the headers of each non-document prerender response while React
40
- * Router's prerender pass runs, and writes them to `.headers.json` in the client build
41
- * when the preview server closes.
42
- */
43
- function captureHeaders(server) {
44
- const { config } = server;
45
- const clientDirectory = resolve(config.root, config.environments.client?.build.outDir ?? config.build.outDir);
46
- const captured = {};
47
- server.middlewares.use((request, response, next) => {
48
- const { pathname } = new URL(request.url ?? "/", "http://localhost");
49
- response.setHeader(CAPTURE_MARKER, "1");
50
- response.once("finish", () => {
51
- const contentType = String(response.getHeader("content-type") ?? "");
52
- if (response.statusCode < 200 || response.statusCode >= 300 || contentType.startsWith("text/html")) return;
53
- const headers = {};
54
- for (const [name, value] of Object.entries(response.getHeaders())) if (!UNCAPTURED_HEADERS.has(name) && value != null) headers[name] = Array.isArray(value) ? value.join(", ") : String(value);
55
- captured[capturedHeadersKey(pathname)] = headers;
56
- });
57
- next();
58
- });
59
- server.httpServer.once("close", () => {
60
- const sorted = Object.fromEntries(Object.entries(captured).toSorted(([first], [second]) => first.localeCompare(second)));
61
- writeFileSync(join(clientDirectory, CAPTURED_HEADERS_FILE), `${JSON.stringify(sorted, null, " ")}\n`);
62
- });
63
- }
64
- /**
65
- * The Vite plugin for `@ngrok/app-server`. Put it next to `reactRouter()`.
66
- *
67
- * - It makes `build/server/index.js` export the app's server (the default
68
- * export of `appModule`) next to the React Router build, so the server is
69
- * bundled with the app, its path aliases, and its one copy of `react-router`.
70
- * The file stays a valid server build.
71
- * - During React Router's prerender pass, it records the headers of each
72
- * prerendered file that is not an HTML document (resource routes and `.data`
73
- * files) into `build/client/.headers.json`. The `prerendered` plugin replays
74
- * them. A document's headers depend on its request, so they are not recorded.
75
- *
76
- * It does not support React Router `serverBundles`.
77
- *
78
- * @param appModule - The module whose default export is the app from `createServer`, relative to the Vite root. When omitted, the server uses its default plugins.
79
- *
80
- * @example
81
- * // vite.config.ts
82
- * export default defineConfig({ plugins: [reactRouter(), serverEntry("./server.ts")] });
83
- */
84
- function serverEntry(appModule) {
85
- let root = process.cwd();
86
- return {
87
- name: "@ngrok/app-server",
88
- enforce: "pre",
89
- config: (_config, { command }) => command === "build" ? { environments: { ssr: { build: { rolldownOptions: { input: SERVER_ENTRY_ID } } } } } : void 0,
90
- configResolved: (config) => {
91
- root = config.root;
92
- },
93
- resolveId: (id) => id === "virtual:@ngrok/app-server/entry" ? RESOLVED_SERVER_ENTRY_ID : void 0,
94
- load: (id) => id === RESOLVED_SERVER_ENTRY_ID ? serverEntrySource(appModule == null ? void 0 : resolve(root, appModule)) : void 0,
95
- configurePreviewServer: (server) => {
96
- if (process.env.IS_RR_BUILD_REQUEST === "yes") captureHeaders(server);
97
- }
98
- };
99
- }
100
-
101
- export { serverEntry as n, serverEntrySource as r, SERVER_ENTRY_ID as t };
102
- //# sourceMappingURL=vite-CrNqpoeK.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vite-CrNqpoeK.js","names":[],"sources":["../src/vite.ts"],"sourcesContent":["import { writeFileSync } from \"node:fs\";\nimport { join, resolve } from \"node:path\";\nimport type { Plugin, PreviewServer } from \"vite\";\nimport {\n\t//,\n\tCAPTURED_HEADERS_FILE,\n\tcapturedHeadersKey,\n\ttype CapturedHeaders,\n} from \"./captured-headers.js\";\n\n/** The module ID of the server build entry that {@link serverEntry} generates. */\nexport const SERVER_ENTRY_ID = \"virtual:@ngrok/app-server/entry\";\nconst RESOLVED_SERVER_ENTRY_ID = `\\0${SERVER_ENTRY_ID}`;\n\n/** The header that turns on Node's header store for a response, so `finish` can read every header. */\nconst CAPTURE_MARKER = \"x-app-server-capture\";\n\n/**\n * Headers that describe one transfer, not the resource. The server sets its\n * own, and a cookie must never replay to every visitor. `Vary` is left out\n * because the preview server adds `Vary: Origin` to every response.\n */\nconst UNCAPTURED_HEADERS = new Set([\n\tCAPTURE_MARKER,\n\t\"connection\",\n\t\"content-encoding\",\n\t\"content-length\",\n\t\"date\",\n\t\"etag\",\n\t\"keep-alive\",\n\t\"last-modified\",\n\t\"set-cookie\",\n\t\"transfer-encoding\",\n\t\"vary\",\n]);\n\n/** The source of the server build entry. */\nexport function serverEntrySource(appModule: string | undefined): string {\n\treturn [\n\t\t`export * from \"virtual:react-router/server-build\";`,\n\t\t`import * as build from \"virtual:react-router/server-build\";`,\n\t\t// Why from the bundle: the CLI calls this export, so the server runs on the bundle's copy of\n\t\t// this package and of `react-router`, the same copies as the app and the routes.\n\t\t`import { appServerEntry } from \"@ngrok/app-server/react-router\";`,\n\t\t...(appModule == null\n\t\t\t? [`export const appServer = appServerEntry(build);`]\n\t\t\t: [\n\t\t\t\t\t`import app from ${JSON.stringify(appModule)};`,\n\t\t\t\t\t`export const appServer = appServerEntry(build, app);`,\n\t\t\t\t]),\n\t].join(\"\\n\");\n}\n\n/**\n * Records the headers of each non-document prerender response while React\n * Router's prerender pass runs, and writes them to `.headers.json` in the client build\n * when the preview server closes.\n */\nfunction captureHeaders(server: PreviewServer): void {\n\tconst { config } = server;\n\tconst clientDirectory = resolve(\n\t\tconfig.root,\n\t\tconfig.environments.client?.build.outDir ?? config.build.outDir,\n\t);\n\tconst captured: CapturedHeaders = {};\n\tserver.middlewares.use((request, response, next) => {\n\t\tconst { pathname } = new URL(request.url ?? \"/\", \"http://localhost\");\n\t\t// Why: Node keeps the headers that `writeHead` gets readable only when a header was set first.\n\t\tresponse.setHeader(CAPTURE_MARKER, \"1\");\n\t\tresponse.once(\"finish\", () => {\n\t\t\t// Why skip documents: `entry.server` sets their headers for each request (a nonce, the\n\t\t\t// host), so the headers of the build's request are wrong for every other one.\n\t\t\tconst contentType = String(response.getHeader(\"content-type\") ?? \"\");\n\t\t\tif (\n\t\t\t\tresponse.statusCode < 200 ||\n\t\t\t\tresponse.statusCode >= 300 ||\n\t\t\t\tcontentType.startsWith(\"text/html\")\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst headers: Record<string, string> = {};\n\t\t\tfor (const [name, value] of Object.entries(response.getHeaders())) {\n\t\t\t\tif (!UNCAPTURED_HEADERS.has(name) && value != null) {\n\t\t\t\t\theaders[name] = Array.isArray(value) ? value.join(\", \") : String(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\tcaptured[capturedHeadersKey(pathname)] = headers;\n\t\t});\n\t\tnext();\n\t});\n\tserver.httpServer.once(\"close\", () => {\n\t\tconst sorted = Object.fromEntries(\n\t\t\tObject.entries(captured).toSorted(([first], [second]) => first.localeCompare(second)),\n\t\t);\n\t\twriteFileSync(\n\t\t\tjoin(clientDirectory, CAPTURED_HEADERS_FILE),\n\t\t\t`${JSON.stringify(sorted, null, \"\\t\")}\\n`,\n\t\t);\n\t});\n}\n\n/**\n * The Vite plugin for `@ngrok/app-server`. Put it next to `reactRouter()`.\n *\n * - It makes `build/server/index.js` export the app's server (the default\n * export of `appModule`) next to the React Router build, so the server is\n * bundled with the app, its path aliases, and its one copy of `react-router`.\n * The file stays a valid server build.\n * - During React Router's prerender pass, it records the headers of each\n * prerendered file that is not an HTML document (resource routes and `.data`\n * files) into `build/client/.headers.json`. The `prerendered` plugin replays\n * them. A document's headers depend on its request, so they are not recorded.\n *\n * It does not support React Router `serverBundles`.\n *\n * @param appModule - The module whose default export is the app from `createServer`, relative to the Vite root. When omitted, the server uses its default plugins.\n *\n * @example\n * // vite.config.ts\n * export default defineConfig({ plugins: [reactRouter(), serverEntry(\"./server.ts\")] });\n */\nexport function serverEntry(appModule?: string): Plugin {\n\tlet root = process.cwd();\n\treturn {\n\t\tname: \"@ngrok/app-server\",\n\t\t// Why `pre`: React Router reads the SSR input from the config that the plugins before it return.\n\t\tenforce: \"pre\",\n\t\tconfig: (_config, { command }) =>\n\t\t\tcommand === \"build\"\n\t\t\t\t? { environments: { ssr: { build: { rolldownOptions: { input: SERVER_ENTRY_ID } } } } }\n\t\t\t\t: undefined,\n\t\tconfigResolved: (config) => {\n\t\t\troot = config.root;\n\t\t},\n\t\tresolveId: (id) => (id === SERVER_ENTRY_ID ? RESOLVED_SERVER_ENTRY_ID : undefined),\n\t\tload: (id) =>\n\t\t\tid === RESOLVED_SERVER_ENTRY_ID\n\t\t\t\t? serverEntrySource(appModule == null ? undefined : resolve(root, appModule))\n\t\t\t\t: undefined,\n\t\tconfigurePreviewServer: (server) => {\n\t\t\t// Why this flag: React Router sets it for the preview server of its prerender pass only.\n\t\t\tif (process.env.IS_RR_BUILD_REQUEST === \"yes\") {\n\t\t\t\tcaptureHeaders(server);\n\t\t\t}\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;AAWA,MAAa,kBAAkB;AAC/B,MAAM,2BAA2B,KAAK;;AAGtC,MAAM,iBAAiB;;;;;;AAOvB,MAAM,qCAAqB,IAAI,IAAI;CAClC;CACA;CAC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