@ngrok/app-server 0.2.0 → 0.3.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.
- package/README.md +652 -77
- package/dist/build-id-C6sYO9Yp.js +126 -0
- package/dist/build-id-C6sYO9Yp.js.map +1 -0
- package/dist/{build-manifest-8NfD1wnO.js → build-manifest-UDNk81Ce.js} +24 -6
- package/dist/build-manifest-UDNk81Ce.js.map +1 -0
- package/dist/cli.js +45 -25
- package/dist/cli.js.map +1 -1
- package/dist/client-browser.d.ts +12 -0
- package/dist/client-browser.js +3 -0
- package/dist/client.d.ts +21 -0
- package/dist/client.js +27 -0
- package/dist/client.js.map +1 -0
- package/dist/compression-7_gToa8R.js +97 -0
- package/dist/compression-7_gToa8R.js.map +1 -0
- package/dist/create-server-DkTeIJ8L.js +1409 -0
- package/dist/create-server-DkTeIJ8L.js.map +1 -0
- package/dist/{deno-DNRgzHrk.js → deno-aqhikNMG.js} +2 -2
- package/dist/deno-aqhikNMG.js.map +1 -0
- package/dist/entry-CM6Ma-Tv.d.ts +626 -0
- package/dist/entry-CenmvESk.js +116 -0
- package/dist/entry-CenmvESk.js.map +1 -0
- package/dist/gzip-Doi4wqjX.js +294 -0
- package/dist/gzip-Doi4wqjX.js.map +1 -0
- package/dist/header-hook-Vzw9J9BY.js +159 -0
- package/dist/header-hook-Vzw9J9BY.js.map +1 -0
- package/dist/{index-oSE-tYRW.d.ts → index-Bo5G9LuQ.d.ts} +53 -14
- package/dist/index.d.ts +99 -269
- package/dist/index.js +47 -59
- package/dist/index.js.map +1 -1
- package/dist/listen-options-7TaXjyrk.js.map +1 -1
- package/dist/{load-entry-C03qqmhr.js → load-entry-CmkdlmDX.js} +8 -4
- package/dist/{load-entry-C03qqmhr.js.map → load-entry-CmkdlmDX.js.map} +1 -1
- package/dist/log-BP6xXXZl.js +122 -0
- package/dist/log-BP6xXXZl.js.map +1 -0
- package/dist/node-BFH82eeL.js +333 -0
- package/dist/node-BFH82eeL.js.map +1 -0
- package/dist/node-tBVQPjQv.js +41 -0
- package/dist/node-tBVQPjQv.js.map +1 -0
- package/dist/node-vite.d.ts +5 -4
- package/dist/node-vite.js +2 -17
- package/dist/node.d.ts +23 -4
- package/dist/node.js +2 -2
- package/dist/react-router-vite.d.ts +4 -3
- package/dist/react-router-vite.js +2 -20
- package/dist/react-router.d.ts +1 -1
- package/dist/react-router.js +8 -7
- package/dist/react-router.js.map +1 -1
- package/dist/request-context-BtQ87VYb.js +40 -0
- package/dist/request-context-BtQ87VYb.js.map +1 -0
- package/dist/response-edge-Bc1-C_Cx.js +327 -0
- package/dist/response-edge-Bc1-C_Cx.js.map +1 -0
- package/dist/response-writer-B7FoJjEE.js +289 -0
- package/dist/response-writer-B7FoJjEE.js.map +1 -0
- package/dist/script-hashes-hqUk6kG7.js +53 -0
- package/dist/script-hashes-hqUk6kG7.js.map +1 -0
- package/dist/{serve-Cleo8RMO.js → serve-Cc-ADdhZ.js} +3 -3
- package/dist/{serve-Cleo8RMO.js.map → serve-Cc-ADdhZ.js.map} +1 -1
- package/dist/stale-build-navigation-PnMLtTF8.js +339 -0
- package/dist/stale-build-navigation-PnMLtTF8.js.map +1 -0
- package/dist/stale-build-navigation-ypV7Ow4o.d.ts +141 -0
- package/dist/{start-CINALHxy.js → start-Cb3kknlF.js} +14 -19
- package/dist/start-Cb3kknlF.js.map +1 -0
- package/dist/static-files-Beju9riB.js +302 -0
- package/dist/static-files-Beju9riB.js.map +1 -0
- package/dist/tanstack-start-vite.d.ts +6 -5
- package/dist/tanstack-start-vite.js +2 -23
- package/dist/tanstack-start.d.ts +19 -2
- package/dist/tanstack-start.js +5 -14
- package/dist/tanstack-start.js.map +1 -1
- package/dist/{tcp-C-9AmFuM.js → tcp-Dd1Mt6sN.js} +6 -5
- package/dist/{tcp-C-9AmFuM.js.map → tcp-Dd1Mt6sN.js.map} +1 -1
- package/dist/trailing-slash-DX7NB88H.d.ts +36 -0
- package/dist/types-DLYxYS6M.d.ts +392 -0
- package/dist/vercel-vite.d.ts +35 -0
- package/dist/vercel-vite.js +142 -0
- package/dist/vercel-vite.js.map +1 -0
- package/dist/vercel.d.ts +35 -0
- package/dist/vercel.js +89 -0
- package/dist/vercel.js.map +1 -0
- package/dist/vite-B-GfswoH.js +22 -0
- package/dist/vite-B-GfswoH.js.map +1 -0
- package/dist/vite-D4hfj3ph.js +19 -0
- package/dist/vite-D4hfj3ph.js.map +1 -0
- package/dist/vite-DyrhyKEr.js +25 -0
- package/dist/vite-DyrhyKEr.js.map +1 -0
- package/dist/vite-iioAoxSt.js +752 -0
- package/dist/vite-iioAoxSt.js.map +1 -0
- package/dist/vite.d.ts +2 -2
- package/dist/vite.js +1 -1
- package/package.json +25 -4
- package/dist/build-manifest-8NfD1wnO.js.map +0 -1
- package/dist/deno-DNRgzHrk.js.map +0 -1
- package/dist/entry-BkdUub2S.js +0 -638
- package/dist/entry-BkdUub2S.js.map +0 -1
- package/dist/log-CsjaKM8U.js +0 -59
- package/dist/log-CsjaKM8U.js.map +0 -1
- package/dist/node-CG7qXuWZ.js +0 -58
- package/dist/node-CG7qXuWZ.js.map +0 -1
- package/dist/node-YWPGmzAk.js +0 -215
- package/dist/node-YWPGmzAk.js.map +0 -1
- package/dist/node-ZMowKZDB.js +0 -3
- package/dist/node-vite.js.map +0 -1
- package/dist/react-router-vite.js.map +0 -1
- package/dist/start-CINALHxy.js.map +0 -1
- package/dist/tanstack-start-vite.js.map +0 -1
- package/dist/types-BfCkS7kQ.d.ts +0 -203
- package/dist/vite-mYZ2Y69u.js +0 -144
- package/dist/vite-mYZ2Y69u.js.map +0 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { o as withHeaderHook } from "./header-hook-Vzw9J9BY.js";
|
|
2
|
+
|
|
3
|
+
// src/core/plugin-options.ts
|
|
4
|
+
/**
|
|
5
|
+
* The option of `defaultPlugins()` that places each plugin, by plugin
|
|
6
|
+
* object. Why the object, not the name: an app plugin can have any name.
|
|
7
|
+
*/
|
|
8
|
+
const options = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
/**
|
|
10
|
+
* Marks `plugin` as the plugin of the `option` of `defaultPlugins()`, so
|
|
11
|
+
* `createServer` finds it in the plugin list.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* return optionPlugin("noindex", { name: "noindex", middleware });
|
|
15
|
+
*/
|
|
16
|
+
function optionPlugin(option, plugin) {
|
|
17
|
+
options.set(plugin, option);
|
|
18
|
+
return plugin;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Stops startup when `plugins` has two plugins of one option of
|
|
22
|
+
* `defaultPlugins()`, such as two `buildId()`. Why: two copies do not fail.
|
|
23
|
+
* One sets its headers over the other, so a value that the app gave is lost.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* checkOptionPlugins([...defaultPlugins(), buildId({ value: "v42" })]); // throws
|
|
27
|
+
*/
|
|
28
|
+
function checkOptionPlugins(plugins) {
|
|
29
|
+
const seen = /* @__PURE__ */ new Set();
|
|
30
|
+
for (const plugin of plugins) {
|
|
31
|
+
const option = options.get(plugin);
|
|
32
|
+
if (option != null && seen.has(option)) throw new Error(`The plugin list has the ${plugin.name} plugin two times, so one replaces the other. Keep one: set the "${option}" option of defaultPlugins(), and do not also add the plugin.`);
|
|
33
|
+
if (option != null) seen.add(option);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/core/plugins/build-id.ts
|
|
38
|
+
/** The header that {@link buildId} sets by default. */
|
|
39
|
+
const BUILD_ID_HEADER = "X-Build-Id";
|
|
40
|
+
/**
|
|
41
|
+
* The `Server-Timing` metric that holds the build of a response. Why
|
|
42
|
+
* `Server-Timing`: a script can read it from the performance entries, with no
|
|
43
|
+
* request and no change to the document's HTML.
|
|
44
|
+
*/
|
|
45
|
+
const BUILD_ID_METRIC = "build";
|
|
46
|
+
/**
|
|
47
|
+
* Whether a cache can use the response again without the server: it has a
|
|
48
|
+
* fresh time, a stale time, or a date that a cache can guess a fresh time from.
|
|
49
|
+
*/
|
|
50
|
+
function isReusable(response) {
|
|
51
|
+
const cacheControl = response.get("Cache-Control") ?? "";
|
|
52
|
+
if (/\b(?:no-store|no-cache)\b/iu.test(cacheControl)) return false;
|
|
53
|
+
return /\b(?:max-age|s-maxage|stale-while-revalidate|stale-if-error)\s*=\s*"?0*[1-9]/iu.test(cacheControl) || /\bpublic\b/iu.test(cacheControl) || Date.parse(response.get("Expires") ?? "") > Date.now() || response.has("Last-Modified");
|
|
54
|
+
}
|
|
55
|
+
/** The metrics of a `Server-Timing` header, such as `build;desc="v42"`. */
|
|
56
|
+
function serverTimingMetrics(header) {
|
|
57
|
+
return (header?.match(/(?:[^",]|"(?:[^"\\]|\\.)*")+/gu) ?? []).map((metric) => metric.trim()).filter((metric) => metric !== "");
|
|
58
|
+
}
|
|
59
|
+
/** Whether a `Server-Timing` metric is a build. */
|
|
60
|
+
function isBuildMetric(metric) {
|
|
61
|
+
return metric.split(";")[0]?.trim().toLowerCase() === BUILD_ID_METRIC;
|
|
62
|
+
}
|
|
63
|
+
/** The `Server-Timing` metrics of `header` that are not a build. */
|
|
64
|
+
function otherMetrics(header) {
|
|
65
|
+
return serverTimingMetrics(header).filter((metric) => !isBuildMetric(metric));
|
|
66
|
+
}
|
|
67
|
+
/** The data check before `setup` runs. */
|
|
68
|
+
function noDataRequests() {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
/** A `Server-Timing` quoted string with the characters of `value`. */
|
|
72
|
+
function quoted(value) {
|
|
73
|
+
return `"${value.replaceAll(/["\\]/gu, String.raw`\$&`)}"`;
|
|
74
|
+
}
|
|
75
|
+
/** The `build` metric of `value`, or `undefined` for no build. */
|
|
76
|
+
function buildMetric(value) {
|
|
77
|
+
return value == null ? void 0 : `${BUILD_ID_METRIC};desc=${quoted(value)}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Sets a build ID header on documents and on the framework's data responses,
|
|
81
|
+
* such as React Router `.data` files, so a client can see that a deploy
|
|
82
|
+
* happened under it. Each of them also gets the build as
|
|
83
|
+
* `Server-Timing: build;desc="<value>"`. The `paths` option adds the metric
|
|
84
|
+
* to more routes, such as API proxy routes. The plugin removes the build
|
|
85
|
+
* header and the `build` metric of an upstream API from each other
|
|
86
|
+
* response, so the client reads only this app's build.
|
|
87
|
+
* `staleBuildReactRouter()` and `staleBuildTanStack()` from `@ngrok/app-server/client` read the metric
|
|
88
|
+
* from the document, and from a response that the browser fetches with no
|
|
89
|
+
* script, such as a prefetch.
|
|
90
|
+
*
|
|
91
|
+
* With no `value`, it sends the build ID that the `appServer` Vite plugin
|
|
92
|
+
* derives from the client build. Development and a build with no
|
|
93
|
+
* `.app-server.json` then get no build.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* buildId({ value: process.env.IMAGE_TAG });
|
|
97
|
+
*/
|
|
98
|
+
function buildId({ header = BUILD_ID_HEADER, value: given, paths } = {}) {
|
|
99
|
+
const explicit = given === "" ? void 0 : given;
|
|
100
|
+
let value = explicit;
|
|
101
|
+
let timing = buildMetric(value);
|
|
102
|
+
let isDataRequest = noDataRequests;
|
|
103
|
+
return optionPlugin("buildId", {
|
|
104
|
+
name: "build-id",
|
|
105
|
+
setup: (info) => {
|
|
106
|
+
({isDataRequest} = info);
|
|
107
|
+
value = explicit ?? info.buildId;
|
|
108
|
+
timing = buildMetric(value);
|
|
109
|
+
},
|
|
110
|
+
etagPart: () => value,
|
|
111
|
+
middleware: async (context, next) => withHeaderHook(context, next, (response) => {
|
|
112
|
+
const { path } = context.req;
|
|
113
|
+
const isRouter = (response.get("Content-Type")?.startsWith("text/html") ?? false) || isDataRequest(path);
|
|
114
|
+
const isStamped = isRouter || paths?.(path) === true && !isReusable(response);
|
|
115
|
+
const before = response.get("Server-Timing");
|
|
116
|
+
const metrics = [...otherMetrics(before), ...isStamped && timing != null ? [timing] : []].join(", ");
|
|
117
|
+
if (metrics === "") response.delete("Server-Timing");
|
|
118
|
+
else if (metrics !== before) response.set("Server-Timing", metrics);
|
|
119
|
+
if (isRouter && value != null) response.set(header, value);
|
|
120
|
+
else response.delete(header);
|
|
121
|
+
})
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { serverTimingMetrics as a, isBuildMetric as i, BUILD_ID_METRIC as n, checkOptionPlugins as o, buildId as r, optionPlugin as s, BUILD_ID_HEADER as t };
|
|
126
|
+
//# sourceMappingURL=build-id-C6sYO9Yp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-id-C6sYO9Yp.js","names":[],"sources":["../src/core/plugin-options.ts","../src/core/plugins/build-id.ts"],"sourcesContent":["import type { Plugin } from \"./types.js\";\n\n/**\n * The option of `defaultPlugins()` that places each plugin, by plugin\n * object. Why the object, not the name: an app plugin can have any name.\n */\nconst options = new WeakMap<Plugin, string>();\n\n/**\n * Marks `plugin` as the plugin of the `option` of `defaultPlugins()`, so\n * `createServer` finds it in the plugin list.\n *\n * @example\n * return optionPlugin(\"noindex\", { name: \"noindex\", middleware });\n */\nexport function optionPlugin<P extends Plugin>(option: string, plugin: P): P {\n\toptions.set(plugin, option);\n\treturn plugin;\n}\n\n/**\n * Stops startup when `plugins` has two plugins of one option of\n * `defaultPlugins()`, such as two `buildId()`. Why: two copies do not fail.\n * One sets its headers over the other, so a value that the app gave is lost.\n *\n * @example\n * checkOptionPlugins([...defaultPlugins(), buildId({ value: \"v42\" })]); // throws\n */\nexport function checkOptionPlugins(plugins: readonly Plugin[]): void {\n\tconst seen = new Set<string>();\n\tfor (const plugin of plugins) {\n\t\tconst option = options.get(plugin);\n\t\tif (option != null && seen.has(option)) {\n\t\t\tthrow new Error(\n\t\t\t\t`The plugin list has the ${plugin.name} plugin two times, so one replaces the other. Keep one: set the \"${option}\" option of defaultPlugins(), and do not also add the plugin.`,\n\t\t\t);\n\t\t}\n\t\tif (option != null) {\n\t\t\tseen.add(option);\n\t\t}\n\t}\n}\n","import { withHeaderHook } from \"../header-hook.js\";\nimport { optionPlugin } from \"../plugin-options.js\";\nimport type { Plugin, ResponseHeaders, SetupInfo } from \"../types.js\";\n\n/** The header that {@link buildId} sets by default. */\nexport const BUILD_ID_HEADER = \"X-Build-Id\";\n\n/**\n * The `Server-Timing` metric that holds the build of a response. Why\n * `Server-Timing`: a script can read it from the performance entries, with no\n * request and no change to the document's HTML.\n */\nexport const BUILD_ID_METRIC = \"build\";\n\n/** Options for {@link buildId}. */\nexport type BuildIdOptions = {\n\t/** @default \"X-Build-Id\" */\n\theader?: string;\n\t/**\n\t * The deployed build, such as the image tag. With no value or `\"\"`, the\n\t * plugin sends the ID that the `appServer` Vite plugin derives from the\n\t * client build. In development, it then sends no build.\n\t * @default the `buildId` of `.app-server.json` in the client build\n\t */\n\tvalue?: string | undefined;\n\t/**\n\t * More request paths whose responses carry the build in `Server-Timing`,\n\t * such as the app's proxy routes to its API. A tab then finds a new build\n\t * in the requests that it already sends. They get no build header, because\n\t * the header marks the router's own responses. A response that a cache can\n\t * use again without the server gets no build, so a cached copy never\n\t * reports an old build. It runs in a header hook, so it can run more than\n\t * once for one request.\n\t *\n\t * @example\n\t * buildId({ paths: (path) => path.startsWith(\"/api/\") });\n\t */\n\tpaths?: (path: string) => boolean;\n};\n\n/**\n * Whether a cache can use the response again without the server: it has a\n * fresh time, a stale time, or a date that a cache can guess a fresh time from.\n */\nfunction isReusable(response: ResponseHeaders): boolean {\n\tconst cacheControl = response.get(\"Cache-Control\") ?? \"\";\n\tif (/\\b(?:no-store|no-cache)\\b/iu.test(cacheControl)) {\n\t\treturn false;\n\t}\n\treturn (\n\t\t/\\b(?:max-age|s-maxage|stale-while-revalidate|stale-if-error)\\s*=\\s*\"?0*[1-9]/iu.test(\n\t\t\tcacheControl,\n\t\t) ||\n\t\t/\\bpublic\\b/iu.test(cacheControl) ||\n\t\tDate.parse(response.get(\"Expires\") ?? \"\") > Date.now() ||\n\t\tresponse.has(\"Last-Modified\")\n\t);\n}\n\n/** The metrics of a `Server-Timing` header, such as `build;desc=\"v42\"`. */\nexport function serverTimingMetrics(header: string | null): string[] {\n\t// Why match the quoted strings: a comma or an escaped quote in a description does not end a metric.\n\treturn (header?.match(/(?:[^\",]|\"(?:[^\"\\\\]|\\\\.)*\")+/gu) ?? [])\n\t\t.map((metric) => metric.trim())\n\t\t.filter((metric) => metric !== \"\");\n}\n\n/** Whether a `Server-Timing` metric is a build. */\nexport function isBuildMetric(metric: string): boolean {\n\treturn metric.split(\";\")[0]?.trim().toLowerCase() === BUILD_ID_METRIC;\n}\n\n/** The `Server-Timing` metrics of `header` that are not a build. */\nfunction otherMetrics(header: string | null): string[] {\n\treturn serverTimingMetrics(header).filter((metric) => !isBuildMetric(metric));\n}\n\n/** The data check before `setup` runs. */\nfunction noDataRequests(): boolean {\n\treturn false;\n}\n\n/** A `Server-Timing` quoted string with the characters of `value`. */\nfunction quoted(value: string): string {\n\treturn `\"${value.replaceAll(/[\"\\\\]/gu, String.raw`\\$&`)}\"`;\n}\n\n/** The `build` metric of `value`, or `undefined` for no build. */\nfunction buildMetric(value: string | undefined): string | undefined {\n\treturn value == null ? undefined : `${BUILD_ID_METRIC};desc=${quoted(value)}`;\n}\n\n/**\n * Sets a build ID header on documents and on the framework's data responses,\n * such as React Router `.data` files, so a client can see that a deploy\n * happened under it. Each of them also gets the build as\n * `Server-Timing: build;desc=\"<value>\"`. The `paths` option adds the metric\n * to more routes, such as API proxy routes. The plugin removes the build\n * header and the `build` metric of an upstream API from each other\n * response, so the client reads only this app's build.\n * `staleBuildReactRouter()` and `staleBuildTanStack()` from `@ngrok/app-server/client` read the metric\n * from the document, and from a response that the browser fetches with no\n * script, such as a prefetch.\n *\n * With no `value`, it sends the build ID that the `appServer` Vite plugin\n * derives from the client build. Development and a build with no\n * `.app-server.json` then get no build.\n *\n * @example\n * buildId({ value: process.env.IMAGE_TAG });\n */\nexport function buildId({\n\theader = BUILD_ID_HEADER,\n\tvalue: given,\n\tpaths,\n}: BuildIdOptions = {}): Plugin {\n\t// Why `\"\"` too: a deploy can set the variable with no value, such as `IMAGE_TAG=`.\n\tconst explicit = given === \"\" ? undefined : given;\n\tlet value = explicit;\n\tlet timing = buildMetric(value);\n\tlet isDataRequest: SetupInfo[\"isDataRequest\"] = noDataRequests;\n\treturn optionPlugin(\"buildId\", {\n\t\tname: \"build-id\",\n\t\tsetup: (info) => {\n\t\t\t({ isDataRequest } = info);\n\t\t\tvalue = explicit ?? info.buildId;\n\t\t\ttiming = buildMetric(value);\n\t\t},\n\t\t// Why: a 304 has no `Content-Type`, so a document that did not change on disk must still\n\t\t// answer 200 when the build ID changes.\n\t\tetagPart: () => value,\n\t\tmiddleware: async (context, next) =>\n\t\t\twithHeaderHook(context, next, (response) => {\n\t\t\t\tconst { path } = context.req;\n\t\t\t\tconst isRouter =\n\t\t\t\t\t(response.get(\"Content-Type\")?.startsWith(\"text/html\") ?? false) || isDataRequest(path);\n\t\t\t\tconst isStamped = isRouter || (paths?.(path) === true && !isReusable(response));\n\t\t\t\t// Why remove the build of an upstream API: the client must read only this app's build.\n\t\t\t\tconst before = response.get(\"Server-Timing\");\n\t\t\t\tconst metrics = [\n\t\t\t\t\t...otherMetrics(before),\n\t\t\t\t\t...(isStamped && timing != null ? [timing] : []),\n\t\t\t\t].join(\", \");\n\t\t\t\tif (metrics === \"\") {\n\t\t\t\t\tresponse.delete(\"Server-Timing\");\n\t\t\t\t} else if (metrics !== before) {\n\t\t\t\t\tresponse.set(\"Server-Timing\", metrics);\n\t\t\t\t}\n\t\t\t\tif (isRouter && value != null) {\n\t\t\t\t\tresponse.set(header, value);\n\t\t\t\t} else {\n\t\t\t\t\tresponse.delete(header);\n\t\t\t\t}\n\t\t\t}),\n\t});\n}\n"],"mappings":";;;;;;;AAMA,MAAM,0BAAU,IAAI,QAAwB;;;;;;;;AAS5C,SAAgB,aAA+B,QAAgB,QAAc;CAC5E,QAAQ,IAAI,QAAQ,MAAM;CAC1B,OAAO;AACR;;;;;;;;;AAUA,SAAgB,mBAAmB,SAAkC;CACpE,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,UAAU,SAAS;EAC7B,MAAM,SAAS,QAAQ,IAAI,MAAM;EACjC,IAAI,UAAU,QAAQ,KAAK,IAAI,MAAM,GACpC,MAAM,IAAI,MACT,2BAA2B,OAAO,KAAK,mEAAmE,OAAO,8DAClH;EAED,IAAI,UAAU,MACb,KAAK,IAAI,MAAM;CAEjB;AACD;;;;ACpCA,MAAa,kBAAkB;;;;;;AAO/B,MAAa,kBAAkB;;;;;AAgC/B,SAAS,WAAW,UAAoC;CACvD,MAAM,eAAe,SAAS,IAAI,eAAe,KAAK;CACtD,IAAI,8BAA8B,KAAK,YAAY,GAClD,OAAO;CAER,OACC,iFAAiF,KAChF,YACD,KACA,eAAe,KAAK,YAAY,KAChC,KAAK,MAAM,SAAS,IAAI,SAAS,KAAK,EAAE,IAAI,KAAK,IAAI,KACrD,SAAS,IAAI,eAAe;AAE9B;;AAGA,SAAgB,oBAAoB,QAAiC;CAEpE,QAAQ,QAAQ,MAAM,gCAAgC,KAAK,CAAC,EAAC,CAC3D,KAAK,WAAW,OAAO,KAAK,CAAC,CAAC,CAC9B,QAAQ,WAAW,WAAW,EAAE;AACnC;;AAGA,SAAgB,cAAc,QAAyB;CACtD,OAAO,OAAO,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,YAAY,MAAM;AACvD;;AAGA,SAAS,aAAa,QAAiC;CACtD,OAAO,oBAAoB,MAAM,CAAC,CAAC,QAAQ,WAAW,CAAC,cAAc,MAAM,CAAC;AAC7E;;AAGA,SAAS,iBAA0B;CAClC,OAAO;AACR;;AAGA,SAAS,OAAO,OAAuB;CACtC,OAAO,IAAI,MAAM,WAAW,WAAW,OAAO,GAAG,KAAK,EAAE;AACzD;;AAGA,SAAS,YAAY,OAA+C;CACnE,OAAO,SAAS,OAAO,SAAY,GAAG,gBAAgB,QAAQ,OAAO,KAAK;AAC3E;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,QAAQ,EACvB,SAAS,iBACT,OAAO,OACP,UACmB,CAAC,GAAW;CAE/B,MAAM,WAAW,UAAU,KAAK,SAAY;CAC5C,IAAI,QAAQ;CACZ,IAAI,SAAS,YAAY,KAAK;CAC9B,IAAI,gBAA4C;CAChD,OAAO,aAAa,WAAW;EAC9B,MAAM;EACN,QAAQ,SAAS;GAChB,CAAC,CAAE,iBAAkB;GACrB,QAAQ,YAAY,KAAK;GACzB,SAAS,YAAY,KAAK;EAC3B;EAGA,gBAAgB;EAChB,YAAY,OAAO,SAAS,SAC3B,eAAe,SAAS,OAAO,aAAa;GAC3C,MAAM,EAAE,SAAS,QAAQ;GACzB,MAAM,YACJ,SAAS,IAAI,cAAc,CAAC,EAAE,WAAW,WAAW,KAAK,UAAU,cAAc,IAAI;GACvF,MAAM,YAAY,YAAa,QAAQ,IAAI,MAAM,QAAQ,CAAC,WAAW,QAAQ;GAE7E,MAAM,SAAS,SAAS,IAAI,eAAe;GAC3C,MAAM,UAAU,CACf,GAAG,aAAa,MAAM,GACtB,GAAI,aAAa,UAAU,OAAO,CAAC,MAAM,IAAI,CAAC,CAC/C,CAAC,CAAC,KAAK,IAAI;GACX,IAAI,YAAY,IACf,SAAS,OAAO,eAAe;QACzB,IAAI,YAAY,QACtB,SAAS,IAAI,iBAAiB,OAAO;GAEtC,IAAI,YAAY,SAAS,MACxB,SAAS,IAAI,QAAQ,KAAK;QAE1B,SAAS,OAAO,MAAM;EAExB,CAAC;CACH,CAAC;AACF"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// src/core/build-manifest.ts
|
|
2
2
|
/**
|
|
3
|
-
* The file under the client build that the Vite plugin writes
|
|
4
|
-
* prerender pass. It is a dotfile, so the
|
|
3
|
+
* The file under the client build that the Vite plugin writes after the
|
|
4
|
+
* client build and again after the prerender pass. It is a dotfile, so the
|
|
5
|
+
* static stages never serve it.
|
|
5
6
|
*/
|
|
6
7
|
const BUILD_MANIFEST_FILE = ".app-server.json";
|
|
7
8
|
/**
|
|
@@ -18,6 +19,15 @@ function manifestKey(pathname) {
|
|
|
18
19
|
function isStringRecord(value) {
|
|
19
20
|
return typeof value === "object" && value != null && !Array.isArray(value) && Object.values(value).every((entry) => typeof entry === "string");
|
|
20
21
|
}
|
|
22
|
+
function isStringList(value) {
|
|
23
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
24
|
+
}
|
|
25
|
+
/** The `buildId` of a parsed manifest, which can have none. A value that is not a string throws. */
|
|
26
|
+
function buildIdOf(manifest) {
|
|
27
|
+
if (!("buildId" in manifest)) return {};
|
|
28
|
+
if (typeof manifest.buildId !== "string") throw new TypeError(`${BUILD_MANIFEST_FILE}: the "buildId" must be a string.`);
|
|
29
|
+
return { buildId: manifest.buildId };
|
|
30
|
+
}
|
|
21
31
|
/**
|
|
22
32
|
* Reads the build manifest of a client build. A build with no file has an
|
|
23
33
|
* empty one. A file in any other shape throws, so a bad build stops startup.
|
|
@@ -26,20 +36,28 @@ async function readBuildManifest(files) {
|
|
|
26
36
|
const path = `/${BUILD_MANIFEST_FILE}`;
|
|
27
37
|
if ((await files.stat(path))?.kind !== "file") return {
|
|
28
38
|
prerendered: [],
|
|
29
|
-
headers: {}
|
|
39
|
+
headers: {},
|
|
40
|
+
scriptHashes: {}
|
|
30
41
|
};
|
|
31
42
|
const parsed = await new Response(await files.open(path)).json();
|
|
32
|
-
if (typeof parsed !== "object" || parsed == null || !("prerendered" in parsed) || !
|
|
43
|
+
if (typeof parsed !== "object" || parsed == null || !("prerendered" in parsed) || !isStringList(parsed.prerendered) || !("headers" in parsed) || typeof parsed.headers !== "object" || parsed.headers == null || !("scriptHashes" in parsed) || typeof parsed.scriptHashes !== "object" || parsed.scriptHashes == null) throw new Error(`${BUILD_MANIFEST_FILE} must hold a "prerendered" list and the "headers" and "scriptHashes" objects.`);
|
|
33
44
|
const headers = {};
|
|
34
45
|
for (const [key, value] of Object.entries(parsed.headers)) {
|
|
35
46
|
if (!isStringRecord(value)) throw new Error(`${BUILD_MANIFEST_FILE}: the headers for ${key} must be strings.`);
|
|
36
47
|
headers[key] = value;
|
|
37
48
|
}
|
|
49
|
+
const scriptHashes = {};
|
|
50
|
+
for (const [key, value] of Object.entries(parsed.scriptHashes)) {
|
|
51
|
+
if (!isStringList(value)) throw new Error(`${BUILD_MANIFEST_FILE}: the script hashes for ${key} must be strings.`);
|
|
52
|
+
scriptHashes[key] = value;
|
|
53
|
+
}
|
|
38
54
|
return {
|
|
55
|
+
...buildIdOf(parsed),
|
|
39
56
|
prerendered: parsed.prerendered,
|
|
40
|
-
headers
|
|
57
|
+
headers,
|
|
58
|
+
scriptHashes
|
|
41
59
|
};
|
|
42
60
|
}
|
|
43
61
|
|
|
44
62
|
export { manifestKey as n, readBuildManifest as r, BUILD_MANIFEST_FILE as t };
|
|
45
|
-
//# sourceMappingURL=build-manifest-
|
|
63
|
+
//# sourceMappingURL=build-manifest-UDNk81Ce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-manifest-UDNk81Ce.js","names":[],"sources":["../src/core/build-manifest.ts"],"sourcesContent":["import type { StaticFiles } from \"./types.js\";\n\n/**\n * The file under the client build that the Vite plugin writes after the\n * client build and again after the prerender pass. It is a dotfile, so the\n * static stages never serve it.\n */\nexport const BUILD_MANIFEST_FILE = \".app-server.json\";\n\n/** What the Vite plugin records about the client build. */\nexport type BuildManifest = {\n\t/**\n\t * The ID of the client build: a hash of the files that the client build\n\t * writes. The `buildId` plugin sends it by default.\n\t */\n\tbuildId?: string;\n\t/** The URL paths that answered with a 2xx, sorted. */\n\tprerendered: string[];\n\t/** The headers of each prerendered file that is not an HTML document, by URL path. */\n\theaders: Record<string, Record<string, string>>;\n\t/**\n\t * The base64 `SHA-256` of each inline script of each HTML file in the client\n\t * build, by the file's URL path, such as `/about/index.html`. A CSP trusts\n\t * the scripts of a prerendered page by these hashes.\n\t */\n\tscriptHashes: Record<string, string[]>;\n};\n\n/**\n * The key for a URL path in {@link BuildManifest}: the path with no trailing\n * slash, so `/a/` and `/a` share headers. The root stays `/`.\n *\n * @example\n * manifestKey(\"/components/button/\"); // => \"/components/button\"\n */\nexport function manifestKey(pathname: string): string {\n\tconst trimmed = pathname.replace(/\\/+$/u, \"\");\n\treturn trimmed === \"\" ? \"/\" : trimmed;\n}\n\nfunction isStringRecord(value: unknown): value is Record<string, string> {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue != null &&\n\t\t!Array.isArray(value) &&\n\t\tObject.values(value).every((entry) => typeof entry === \"string\")\n\t);\n}\n\nfunction isStringList(value: unknown): value is string[] {\n\treturn Array.isArray(value) && value.every((entry) => typeof entry === \"string\");\n}\n\n/** The `buildId` of a parsed manifest, which can have none. A value that is not a string throws. */\nfunction buildIdOf(manifest: object): Pick<BuildManifest, \"buildId\"> {\n\tif (!(\"buildId\" in manifest)) {\n\t\treturn {};\n\t}\n\tif (typeof manifest.buildId !== \"string\") {\n\t\tthrow new TypeError(`${BUILD_MANIFEST_FILE}: the \"buildId\" must be a string.`);\n\t}\n\treturn { buildId: manifest.buildId };\n}\n\n/**\n * Reads the build manifest of a client build. A build with no file has an\n * empty one. A file in any other shape throws, so a bad build stops startup.\n */\nexport async function readBuildManifest(files: StaticFiles): Promise<BuildManifest> {\n\tconst path = `/${BUILD_MANIFEST_FILE}`;\n\tif ((await files.stat(path))?.kind !== \"file\") {\n\t\treturn { prerendered: [], headers: {}, scriptHashes: {} };\n\t}\n\tconst parsed: unknown = await new Response(await files.open(path)).json();\n\tif (\n\t\ttypeof parsed !== \"object\" ||\n\t\tparsed == null ||\n\t\t!(\"prerendered\" in parsed) ||\n\t\t!isStringList(parsed.prerendered) ||\n\t\t!(\"headers\" in parsed) ||\n\t\ttypeof parsed.headers !== \"object\" ||\n\t\tparsed.headers == null ||\n\t\t!(\"scriptHashes\" in parsed) ||\n\t\ttypeof parsed.scriptHashes !== \"object\" ||\n\t\tparsed.scriptHashes == null\n\t) {\n\t\tthrow new Error(\n\t\t\t`${BUILD_MANIFEST_FILE} must hold a \"prerendered\" list and the \"headers\" and \"scriptHashes\" objects.`,\n\t\t);\n\t}\n\tconst headers: Record<string, Record<string, string>> = {};\n\tfor (const [key, value] of Object.entries(parsed.headers)) {\n\t\tif (!isStringRecord(value)) {\n\t\t\tthrow new Error(`${BUILD_MANIFEST_FILE}: the headers for ${key} must be strings.`);\n\t\t}\n\t\theaders[key] = value;\n\t}\n\tconst scriptHashes: Record<string, string[]> = {};\n\tfor (const [key, value] of Object.entries(parsed.scriptHashes)) {\n\t\tif (!isStringList(value)) {\n\t\t\tthrow new Error(`${BUILD_MANIFEST_FILE}: the script hashes for ${key} must be strings.`);\n\t\t}\n\t\tscriptHashes[key] = value;\n\t}\n\treturn {\n\t\t...buildIdOf(parsed),\n\t\tprerendered: parsed.prerendered,\n\t\theaders,\n\t\tscriptHashes,\n\t};\n}\n"],"mappings":";;;;;;AAOA,MAAa,sBAAsB;;;;;;;;AA4BnC,SAAgB,YAAY,UAA0B;CACrD,MAAM,UAAU,SAAS,QAAQ,SAAS,EAAE;CAC5C,OAAO,YAAY,KAAK,MAAM;AAC/B;AAEA,SAAS,eAAe,OAAiD;CACxE,OACC,OAAO,UAAU,YACjB,SAAS,QACT,CAAC,MAAM,QAAQ,KAAK,KACpB,OAAO,OAAO,KAAK,CAAC,CAAC,OAAO,UAAU,OAAO,UAAU,QAAQ;AAEjE;AAEA,SAAS,aAAa,OAAmC;CACxD,OAAO,MAAM,QAAQ,KAAK,KAAK,MAAM,OAAO,UAAU,OAAO,UAAU,QAAQ;AAChF;;AAGA,SAAS,UAAU,UAAkD;CACpE,IAAI,EAAE,aAAa,WAClB,OAAO,CAAC;CAET,IAAI,OAAO,SAAS,YAAY,UAC/B,MAAM,IAAI,UAAU,GAAG,oBAAoB,kCAAkC;CAE9E,OAAO,EAAE,SAAS,SAAS,QAAQ;AACpC;;;;;AAMA,eAAsB,kBAAkB,OAA4C;CACnF,MAAM,OAAO,IAAI;CACjB,KAAK,MAAM,MAAM,KAAK,IAAI,EAAC,EAAG,SAAS,QACtC,OAAO;EAAE,aAAa,CAAC;EAAG,SAAS,CAAC;EAAG,cAAc,CAAC;CAAE;CAEzD,MAAM,SAAkB,MAAM,IAAI,SAAS,MAAM,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK;CACxE,IACC,OAAO,WAAW,YAClB,UAAU,QACV,EAAE,iBAAiB,WACnB,CAAC,aAAa,OAAO,WAAW,KAChC,EAAE,aAAa,WACf,OAAO,OAAO,YAAY,YAC1B,OAAO,WAAW,QAClB,EAAE,kBAAkB,WACpB,OAAO,OAAO,iBAAiB,YAC/B,OAAO,gBAAgB,MAEvB,MAAM,IAAI,MACT,GAAG,oBAAoB,8EACxB;CAED,MAAM,UAAkD,CAAC;CACzD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,OAAO,GAAG;EAC1D,IAAI,CAAC,eAAe,KAAK,GACxB,MAAM,IAAI,MAAM,GAAG,oBAAoB,oBAAoB,IAAI,kBAAkB;EAElF,QAAQ,OAAO;CAChB;CACA,MAAM,eAAyC,CAAC;CAChD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,YAAY,GAAG;EAC/D,IAAI,CAAC,aAAa,KAAK,GACtB,MAAM,IAAI,MAAM,GAAG,oBAAoB,0BAA0B,IAAI,kBAAkB;EAExF,aAAa,OAAO;CACrB;CACA,OAAO;EACN,GAAG,UAAU,MAAM;EACnB,aAAa,OAAO;EACpB;EACA;CACD;AACD"}
|
package/dist/cli.js
CHANGED
|
@@ -1,35 +1,55 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as parsePort } from "./tcp-
|
|
2
|
+
import { t as parsePort } from "./tcp-Dd1Mt6sN.js";
|
|
3
3
|
import process from "node:process";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
|
|
6
6
|
// src/cli/cli.ts
|
|
7
7
|
const USAGE = "Usage: app-server start [--port <port>] | app-server serve [--port <port>] [build/server/app-server.js]";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
});
|
|
13
|
-
const [command, buildPath] = positionals;
|
|
14
|
-
if (values.port != null) process.env.PORT = String(parsePort(values.port));
|
|
15
|
-
switch (command) {
|
|
16
|
-
case "start": {
|
|
17
|
-
process.env.NODE_ENV ??= "development";
|
|
18
|
-
const { start } = await import("./start-CINALHxy.js");
|
|
19
|
-
await start();
|
|
20
|
-
break;
|
|
21
|
-
}
|
|
22
|
-
case "serve": {
|
|
23
|
-
process.env.NODE_ENV ??= "production";
|
|
24
|
-
if (typeof process.setSourceMapsEnabled === "function") process.setSourceMapsEnabled(true);
|
|
25
|
-
const { serve } = await import("./serve-Cleo8RMO.js");
|
|
26
|
-
await serve({ buildPath });
|
|
27
|
-
break;
|
|
28
|
-
}
|
|
29
|
-
default:
|
|
30
|
-
process.stderr.write(`${USAGE}\n`);
|
|
31
|
-
process.exitCode = 1;
|
|
8
|
+
/** Writes `message` and the usage to stderr, and sets a failed exit code. */
|
|
9
|
+
function fail(message) {
|
|
10
|
+
process.stderr.write(`${message}\n${USAGE}\n`);
|
|
11
|
+
process.exitCode = 1;
|
|
32
12
|
}
|
|
13
|
+
/** The parsed arguments. A bad argument throws. */
|
|
14
|
+
function parse() {
|
|
15
|
+
const { positionals, values } = parseArgs({
|
|
16
|
+
allowPositionals: true,
|
|
17
|
+
strict: true,
|
|
18
|
+
options: {
|
|
19
|
+
port: {
|
|
20
|
+
type: "string",
|
|
21
|
+
short: "p"
|
|
22
|
+
},
|
|
23
|
+
help: {
|
|
24
|
+
type: "boolean",
|
|
25
|
+
short: "h"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (values.port != null) process.env.PORT = String(parsePort(values.port));
|
|
30
|
+
return {
|
|
31
|
+
command: positionals[0],
|
|
32
|
+
buildPath: positionals[1],
|
|
33
|
+
help: values.help === true
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
let args;
|
|
37
|
+
try {
|
|
38
|
+
args = parse();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
fail(String(error instanceof Error ? error.message : error).replace(/^PORT\b/u, "--port"));
|
|
41
|
+
}
|
|
42
|
+
if (args?.help === true) process.stdout.write(`${USAGE}\n`);
|
|
43
|
+
else if (args?.command === "start") {
|
|
44
|
+
process.env.NODE_ENV ??= "development";
|
|
45
|
+
const { start } = await import("./start-Cb3kknlF.js");
|
|
46
|
+
await start();
|
|
47
|
+
} else if (args?.command === "serve") {
|
|
48
|
+
process.env.NODE_ENV ??= "production";
|
|
49
|
+
if (typeof process.setSourceMapsEnabled === "function") process.setSourceMapsEnabled(true);
|
|
50
|
+
const { serve } = await import("./serve-Cc-ADdhZ.js");
|
|
51
|
+
await serve({ buildPath: args.buildPath });
|
|
52
|
+
} else if (args != null) fail(args.command == null ? "Give a command." : `Unknown command: ${args.command}.`);
|
|
33
53
|
|
|
34
54
|
export { };
|
|
35
55
|
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n// The `app-server` command: `app-server start` or `app-server serve [build/server/app-server.js]`,\n// each with an optional `--port`.\nimport process from \"node:process\";\nimport { parseArgs } from \"node:util\";\nimport { parsePort } from \"../node/tcp.js\";\n\nconst USAGE =\n\t\"Usage: app-server start [--port <port>] | app-server serve [--port <port>] [build/server/app-server.js]\";\n\
|
|
1
|
+
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n// The `app-server` command: `app-server start` or `app-server serve [build/server/app-server.js]`,\n// each with an optional `--port`.\nimport process from \"node:process\";\nimport { parseArgs } from \"node:util\";\nimport { parsePort } from \"../node/tcp.js\";\n\nconst USAGE =\n\t\"Usage: app-server start [--port <port>] | app-server serve [--port <port>] [build/server/app-server.js]\";\n\n/** Writes `message` and the usage to stderr, and sets a failed exit code. */\nfunction fail(message: string): void {\n\tprocess.stderr.write(`${message}\\n${USAGE}\\n`);\n\tprocess.exitCode = 1;\n}\n\n/** The parsed arguments. A bad argument throws. */\nfunction parse() {\n\tconst { positionals, values } = parseArgs({\n\t\tallowPositionals: true,\n\t\tstrict: true,\n\t\toptions: { port: { type: \"string\", short: \"p\" }, help: { type: \"boolean\", short: \"h\" } },\n\t});\n\t// Why set `PORT`: the copy of this package in the build reads it, so the flag needs no change to\n\t// the entry protocol.\n\tif (values.port != null) {\n\t\tprocess.env.PORT = String(parsePort(values.port));\n\t}\n\treturn { command: positionals[0], buildPath: positionals[1], help: values.help === true };\n}\n\nlet args: ReturnType<typeof parse> | undefined;\ntry {\n\targs = parse();\n} catch (error) {\n\t// Why no stack: a bad argument is a usage error, not a crash.\n\tfail(String(error instanceof Error ? error.message : error).replace(/^PORT\\b/u, \"--port\"));\n}\n\n// Why dynamic imports: React reads `NODE_ENV` when it loads, and a static import runs first.\nif (args?.help === true) {\n\tprocess.stdout.write(`${USAGE}\\n`);\n} else if (args?.command === \"start\") {\n\tprocess.env.NODE_ENV ??= \"development\";\n\tconst { start } = await import(\"./start.js\");\n\tawait start();\n} else if (args?.command === \"serve\") {\n\tprocess.env.NODE_ENV ??= \"production\";\n\tif (typeof process.setSourceMapsEnabled === \"function\") {\n\t\tprocess.setSourceMapsEnabled(true);\n\t}\n\tconst { serve } = await import(\"./serve.js\");\n\tawait serve({ buildPath: args.buildPath });\n} else if (args != null) {\n\tfail(args.command == null ? \"Give a command.\" : `Unknown command: ${args.command}.`);\n}\n"],"mappings":";;;;;;AAOA,MAAM,QACL;;AAGD,SAAS,KAAK,SAAuB;CACpC,QAAQ,OAAO,MAAM,GAAG,QAAQ,IAAI,MAAM,GAAG;CAC7C,QAAQ,WAAW;AACpB;;AAGA,SAAS,QAAQ;CAChB,MAAM,EAAE,aAAa,WAAW,UAAU;EACzC,kBAAkB;EAClB,QAAQ;EACR,SAAS;GAAE,MAAM;IAAE,MAAM;IAAU,OAAO;GAAI;GAAG,MAAM;IAAE,MAAM;IAAW,OAAO;GAAI;EAAE;CACxF,CAAC;CAGD,IAAI,OAAO,QAAQ,MAClB,QAAQ,IAAI,OAAO,OAAO,UAAU,OAAO,IAAI,CAAC;CAEjD,OAAO;EAAE,SAAS,YAAY;EAAI,WAAW,YAAY;EAAI,MAAM,OAAO,SAAS;CAAK;AACzF;AAEA,IAAI;AACJ,IAAI;CACH,OAAO,MAAM;AACd,SAAS,OAAO;CAEf,KAAK,OAAO,iBAAiB,QAAQ,MAAM,UAAU,KAAK,CAAC,CAAC,QAAQ,YAAY,QAAQ,CAAC;AAC1F;AAGA,IAAI,MAAM,SAAS,MAClB,QAAQ,OAAO,MAAM,GAAG,MAAM,GAAG;KAC3B,IAAI,MAAM,YAAY,SAAS;CACrC,QAAQ,IAAI,aAAa;CACzB,MAAM,EAAE,UAAU,MAAM,OAAO;CAC/B,MAAM,MAAM;AACb,OAAO,IAAI,MAAM,YAAY,SAAS;CACrC,QAAQ,IAAI,aAAa;CACzB,IAAI,OAAO,QAAQ,yBAAyB,YAC3C,QAAQ,qBAAqB,IAAI;CAElC,MAAM,EAAE,UAAU,MAAM,OAAO;CAC/B,MAAM,MAAM,EAAE,WAAW,KAAK,UAAU,CAAC;AAC1C,OAAO,IAAI,QAAQ,MAClB,KAAK,KAAK,WAAW,OAAO,oBAAoB,oBAAoB,KAAK,QAAQ,EAAE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { a as staleBuildReactRouter, o as staleBuildTanStack } from "./stale-build-navigation-ypV7Ow4o.js";
|
|
2
|
+
/**
|
|
3
|
+
* The CSP nonce of the document's first element with a nonce, or `undefined`
|
|
4
|
+
* when it has none. The browser build of `@ngrok/app-server/client` exports it
|
|
5
|
+
* as `cspNonce()`, so the browser bundle holds no server code.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* documentNonce(); // => "d2hhdCBhIG5vbmNl"
|
|
9
|
+
*/
|
|
10
|
+
declare function documentNonce(): string | undefined;
|
|
11
|
+
export { documentNonce as cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
|
12
|
+
//# sourceMappingURL=client-browser.d.ts.map
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { a as staleBuildReactRouter, i as StaleBuildReactRouter, n as RouterFetchContext, o as staleBuildTanStack, r as StaleBuildOptions, t as NavigationEvents } from "./stale-build-navigation-ypV7Ow4o.js";
|
|
2
|
+
/**
|
|
3
|
+
* The CSP nonce of the document, for the framework's render. It gives the
|
|
4
|
+
* same value on the server and in the browser, so the hydration matches.
|
|
5
|
+
*
|
|
6
|
+
* - On the server, the nonce of the request that runs, from `requestContext()`.
|
|
7
|
+
* - In the browser, the nonce of the document's first element with a nonce.
|
|
8
|
+
* Why: React Router's `<HydratedRouter>` takes no nonce, so the app reads it
|
|
9
|
+
* from the document.
|
|
10
|
+
*
|
|
11
|
+
* It gives `undefined` when the page has no nonce: with no
|
|
12
|
+
* `contentSecurityPolicy` plugin, during a prerender, and on a prerendered page.
|
|
13
|
+
* A bundler that resolves the `browser` condition, such as Vite for the
|
|
14
|
+
* client, gets a build with no server code.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* <Scripts nonce={cspNonce()} />
|
|
18
|
+
*/
|
|
19
|
+
export declare function cspNonce(): string | undefined;
|
|
20
|
+
export { type NavigationEvents, type RouterFetchContext, type StaleBuildOptions, type StaleBuildReactRouter, staleBuildReactRouter, staleBuildTanStack };
|
|
21
|
+
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { t as requestContext } from "./request-context-BtQ87VYb.js";
|
|
2
|
+
import { n as staleBuildTanStack, r as documentNonce, t as staleBuildReactRouter } from "./stale-build-navigation-PnMLtTF8.js";
|
|
3
|
+
|
|
4
|
+
// src/client/csp-nonce.ts
|
|
5
|
+
/**
|
|
6
|
+
* The CSP nonce of the document, for the framework's render. It gives the
|
|
7
|
+
* same value on the server and in the browser, so the hydration matches.
|
|
8
|
+
*
|
|
9
|
+
* - On the server, the nonce of the request that runs, from `requestContext()`.
|
|
10
|
+
* - In the browser, the nonce of the document's first element with a nonce.
|
|
11
|
+
* Why: React Router's `<HydratedRouter>` takes no nonce, so the app reads it
|
|
12
|
+
* from the document.
|
|
13
|
+
*
|
|
14
|
+
* It gives `undefined` when the page has no nonce: with no
|
|
15
|
+
* `contentSecurityPolicy` plugin, during a prerender, and on a prerendered page.
|
|
16
|
+
* A bundler that resolves the `browser` condition, such as Vite for the
|
|
17
|
+
* client, gets a build with no server code.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* <Scripts nonce={cspNonce()} />
|
|
21
|
+
*/
|
|
22
|
+
function cspNonce() {
|
|
23
|
+
return typeof document === "undefined" ? requestContext()?.cspNonce : documentNonce();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
|
27
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","names":[],"sources":["../src/client/csp-nonce.ts"],"sourcesContent":["import { requestContext } from \"../core/request-context.js\";\nimport { documentNonce } from \"./document-nonce.js\";\n\n/**\n * The CSP nonce of the document, for the framework's render. It gives the\n * same value on the server and in the browser, so the hydration matches.\n *\n * - On the server, the nonce of the request that runs, from `requestContext()`.\n * - In the browser, the nonce of the document's first element with a nonce.\n * Why: React Router's `<HydratedRouter>` takes no nonce, so the app reads it\n * from the document.\n *\n * It gives `undefined` when the page has no nonce: with no\n * `contentSecurityPolicy` plugin, during a prerender, and on a prerendered page.\n * A bundler that resolves the `browser` condition, such as Vite for the\n * client, gets a build with no server code.\n *\n * @example\n * <Scripts nonce={cspNonce()} />\n */\nexport function cspNonce(): string | undefined {\n\treturn typeof document === \"undefined\" ? requestContext()?.cspNonce : documentNonce();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,WAA+B;CAC9C,OAAO,OAAO,aAAa,cAAc,eAAe,CAAC,EAAE,WAAW,cAAc;AACrF"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { parseAccept } from "hono/utils/accept";
|
|
2
|
+
|
|
3
|
+
// src/core/compression.ts
|
|
4
|
+
const COMPRESSIBLE = /^(?:text\/|application\/(?:json|javascript|xml|schema\+json|manifest\+json|x-ndjson|connect\+json)|image\/svg\+xml)/u;
|
|
5
|
+
/**
|
|
6
|
+
* The smallest body that the server or the build compresses. Why: gzip
|
|
7
|
+
* makes a smaller body larger, and each gzip stream allocates about 300
|
|
8
|
+
* kibibytes (KiB) of zlib state. `hono/compress` uses the same value.
|
|
9
|
+
*/
|
|
10
|
+
const MIN_COMPRESS_BYTES = 1024;
|
|
11
|
+
/**
|
|
12
|
+
* The encodings of the copies, in the order that the server prefers them.
|
|
13
|
+
* Why brotli first: its copy is smaller, such as 45 KB in place of 56 KB
|
|
14
|
+
* for a docs page of 417 KB.
|
|
15
|
+
*/
|
|
16
|
+
const COPY_ENCODINGS = ["br", "gzip"];
|
|
17
|
+
const COPY_EXTENSIONS = {
|
|
18
|
+
br: "br",
|
|
19
|
+
gzip: "gz"
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The name of the `encoding` copy of the file `name`, which the build writes
|
|
23
|
+
* next to it.
|
|
24
|
+
*
|
|
25
|
+
* Why a dotfile: the static stages never serve a dotfile, so the copy has no
|
|
26
|
+
* URL of its own. A request gets the copy only through the URL of its file,
|
|
27
|
+
* which `skip` and an auth check can block. The build also never replaces or
|
|
28
|
+
* removes a `.gz` or `.br` file of the app.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* copyName("app-abc.js", "br"); // => ".app-abc.js.br"
|
|
32
|
+
*/
|
|
33
|
+
function copyName(name, encoding) {
|
|
34
|
+
return `.${name}.${COPY_EXTENSIONS[encoding]}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether the server compresses a body of the content type `type`.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* isCompressible("text/html; charset=utf-8"); // => true
|
|
41
|
+
* isCompressible("image/png"); // => false
|
|
42
|
+
*/
|
|
43
|
+
function isCompressible(type) {
|
|
44
|
+
return COMPRESSIBLE.test(type);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The encoding in `offered` that an `Accept-Encoding` header prefers, or
|
|
48
|
+
* `undefined` for the body as it is. Each encoding gets the `q` of its own
|
|
49
|
+
* entry, else of `*`. The highest `q` above 0 wins, and a tie goes to the
|
|
50
|
+
* first encoding in `offered`. An `identity` entry with a higher `q` keeps
|
|
51
|
+
* the body as it is.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* preferredEncoding("gzip, deflate, br", ["br", "gzip"]); // => "br"
|
|
55
|
+
* preferredEncoding("br;q=0, gzip", ["br", "gzip"]); // => "gzip"
|
|
56
|
+
*/
|
|
57
|
+
function preferredEncoding(header, offered) {
|
|
58
|
+
const entries = parseAccept(header ?? "");
|
|
59
|
+
const quality = (coding) => (entries.find((entry) => entry.type.toLowerCase() === coding) ?? entries.find((entry) => entry.type === "*"))?.q ?? 0;
|
|
60
|
+
let best;
|
|
61
|
+
let bestQuality = 0;
|
|
62
|
+
for (const encoding of offered) {
|
|
63
|
+
const value = quality(encoding);
|
|
64
|
+
if (value > bestQuality) {
|
|
65
|
+
best = encoding;
|
|
66
|
+
bestQuality = value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const identity = entries.find((entry) => entry.type.toLowerCase() === "identity")?.q;
|
|
70
|
+
return identity != null && identity > bestQuality ? void 0 : best;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Whether a `Cache-Control` value forbids a change to the body, such as gzip.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* forbidsTransform("public, no-transform"); // => true
|
|
77
|
+
* forbidsTransform("public, max-age=3600"); // => false
|
|
78
|
+
*/
|
|
79
|
+
function forbidsTransform(cacheControl) {
|
|
80
|
+
return /\bno-transform\b/u.test(cacheControl ?? "");
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The next result if `reader` has it queued, or the read that is still open.
|
|
84
|
+
* It never waits for the source.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const next = await readIfQueued(reader); // => { result: { done: false, value } } or { pending }
|
|
88
|
+
*/
|
|
89
|
+
async function readIfQueued(reader) {
|
|
90
|
+
const pending = reader.read();
|
|
91
|
+
pending.catch(() => {});
|
|
92
|
+
const result = await Promise.race([pending, Promise.resolve().then(async () => void 0)]);
|
|
93
|
+
return result == null ? { pending } : { result };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { isCompressible as a, forbidsTransform as i, MIN_COMPRESS_BYTES as n, preferredEncoding as o, copyName as r, readIfQueued as s, COPY_ENCODINGS as t };
|
|
97
|
+
//# sourceMappingURL=compression-7_gToa8R.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compression-7_gToa8R.js","names":[],"sources":["../src/core/compression.ts"],"sourcesContent":["import { parseAccept } from \"hono/utils/accept\";\n\nconst COMPRESSIBLE =\n\t/^(?:text\\/|application\\/(?:json|javascript|xml|schema\\+json|manifest\\+json|x-ndjson|connect\\+json)|image\\/svg\\+xml)/u;\n\n/**\n * The smallest body that the server or the build compresses. Why: gzip\n * makes a smaller body larger, and each gzip stream allocates about 300\n * kibibytes (KiB) of zlib state. `hono/compress` uses the same value.\n */\nexport const MIN_COMPRESS_BYTES = 1024;\n\n/** A content coding of a compressed copy that the build writes. */\nexport type Encoding = \"br\" | \"gzip\";\n\n/**\n * The encodings of the copies, in the order that the server prefers them.\n * Why brotli first: its copy is smaller, such as 45 KB in place of 56 KB\n * for a docs page of 417 KB.\n */\nexport const COPY_ENCODINGS: readonly Encoding[] = [\"br\", \"gzip\"];\n\nconst COPY_EXTENSIONS: Readonly<Record<Encoding, string>> = { br: \"br\", gzip: \"gz\" };\n\n/**\n * The name of the `encoding` copy of the file `name`, which the build writes\n * next to it.\n *\n * Why a dotfile: the static stages never serve a dotfile, so the copy has no\n * URL of its own. A request gets the copy only through the URL of its file,\n * which `skip` and an auth check can block. The build also never replaces or\n * removes a `.gz` or `.br` file of the app.\n *\n * @example\n * copyName(\"app-abc.js\", \"br\"); // => \".app-abc.js.br\"\n */\nexport function copyName(name: string, encoding: Encoding): string {\n\treturn `.${name}.${COPY_EXTENSIONS[encoding]}`;\n}\n\n/**\n * Whether the server compresses a body of the content type `type`.\n *\n * @example\n * isCompressible(\"text/html; charset=utf-8\"); // => true\n * isCompressible(\"image/png\"); // => false\n */\nexport function isCompressible(type: string): boolean {\n\treturn COMPRESSIBLE.test(type);\n}\n\n/**\n * The encoding in `offered` that an `Accept-Encoding` header prefers, or\n * `undefined` for the body as it is. Each encoding gets the `q` of its own\n * entry, else of `*`. The highest `q` above 0 wins, and a tie goes to the\n * first encoding in `offered`. An `identity` entry with a higher `q` keeps\n * the body as it is.\n *\n * @example\n * preferredEncoding(\"gzip, deflate, br\", [\"br\", \"gzip\"]); // => \"br\"\n * preferredEncoding(\"br;q=0, gzip\", [\"br\", \"gzip\"]); // => \"gzip\"\n */\nexport function preferredEncoding(\n\theader: string | undefined,\n\toffered: readonly Encoding[],\n): Encoding | undefined {\n\tconst entries = parseAccept(header ?? \"\");\n\tconst quality = (coding: string) =>\n\t\t(\n\t\t\tentries.find((entry) => entry.type.toLowerCase() === coding) ??\n\t\t\tentries.find((entry) => entry.type === \"*\")\n\t\t)?.q ?? 0;\n\tlet best: Encoding | undefined;\n\tlet bestQuality = 0;\n\tfor (const encoding of offered) {\n\t\tconst value = quality(encoding);\n\t\tif (value > bestQuality) {\n\t\t\tbest = encoding;\n\t\t\tbestQuality = value;\n\t\t}\n\t}\n\t// Why only an `identity` entry: with no entry, identity is acceptable, but the client prefers a coding that it names.\n\tconst identity = entries.find((entry) => entry.type.toLowerCase() === \"identity\")?.q;\n\treturn identity != null && identity > bestQuality ? undefined : best;\n}\n\n/**\n * Whether a `Cache-Control` value forbids a change to the body, such as gzip.\n *\n * @example\n * forbidsTransform(\"public, no-transform\"); // => true\n * forbidsTransform(\"public, max-age=3600\"); // => false\n */\nexport function forbidsTransform(cacheControl: string | null): boolean {\n\treturn /\\bno-transform\\b/u.test(cacheControl ?? \"\");\n}\n\n/** The result of one read of a byte stream. */\nexport type ReadResult = ReadableStreamReadResult<Uint8Array>;\n\n/**\n * The next result if `reader` has it queued, or the read that is still open.\n * It never waits for the source.\n *\n * @example\n * const next = await readIfQueued(reader); // => { result: { done: false, value } } or { pending }\n */\nexport async function readIfQueued(\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n): Promise<{ result: ReadResult } | { pending: Promise<ReadResult> }> {\n\tconst pending = reader.read();\n\t// Why: a client that leaves before the body is read must not cause an unhandled rejection.\n\tpending.catch(() => {});\n\tconst result = await Promise.race([pending, Promise.resolve().then(async () => undefined)]);\n\treturn result == null ? { pending } : { result };\n}\n"],"mappings":";;;AAEA,MAAM,eACL;;;;;;AAOD,MAAa,qBAAqB;;;;;;AAUlC,MAAa,iBAAsC,CAAC,MAAM,MAAM;AAEhE,MAAM,kBAAsD;CAAE,IAAI;CAAM,MAAM;AAAK;;;;;;;;;;;;;AAcnF,SAAgB,SAAS,MAAc,UAA4B;CAClE,OAAO,IAAI,KAAK,GAAG,gBAAgB;AACpC;;;;;;;;AASA,SAAgB,eAAe,MAAuB;CACrD,OAAO,aAAa,KAAK,IAAI;AAC9B;;;;;;;;;;;;AAaA,SAAgB,kBACf,QACA,SACuB;CACvB,MAAM,UAAU,YAAY,UAAU,EAAE;CACxC,MAAM,WAAW,YAEf,QAAQ,MAAM,UAAU,MAAM,KAAK,YAAY,MAAM,MAAM,KAC3D,QAAQ,MAAM,UAAU,MAAM,SAAS,GAAG,EAAC,EACzC,KAAK;CACT,IAAI;CACJ,IAAI,cAAc;CAClB,KAAK,MAAM,YAAY,SAAS;EAC/B,MAAM,QAAQ,QAAQ,QAAQ;EAC9B,IAAI,QAAQ,aAAa;GACxB,OAAO;GACP,cAAc;EACf;CACD;CAEA,MAAM,WAAW,QAAQ,MAAM,UAAU,MAAM,KAAK,YAAY,MAAM,UAAU,CAAC,EAAE;CACnF,OAAO,YAAY,QAAQ,WAAW,cAAc,SAAY;AACjE;;;;;;;;AASA,SAAgB,iBAAiB,cAAsC;CACtE,OAAO,oBAAoB,KAAK,gBAAgB,EAAE;AACnD;;;;;;;;AAYA,eAAsB,aACrB,QACqE;CACrE,MAAM,UAAU,OAAO,KAAK;CAE5B,QAAQ,YAAY,CAAC,CAAC;CACtB,MAAM,SAAS,MAAM,QAAQ,KAAK,CAAC,SAAS,QAAQ,QAAQ,CAAC,CAAC,KAAK,YAAY,MAAS,CAAC,CAAC;CAC1F,OAAO,UAAU,OAAO,EAAE,QAAQ,IAAI,EAAE,OAAO;AAChD"}
|