@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.
- package/README.md +223 -64
- package/dist/brotli-only-BVAagKt3.js +63 -0
- package/dist/build-id-CwMbED1n.js +245 -0
- package/dist/{build-manifest-DBMWe3Lx.js → build-manifest-CoBztw_6.js} +13 -4
- package/dist/bun-BCz6K_Xv.js +1 -2
- package/dist/check-count-DQl5z_qe.js +17 -0
- package/dist/cli.js +9 -6
- package/dist/client-browser.d.ts +2 -3
- package/dist/client-browser.js +1 -1
- package/dist/client.d.ts +5 -4
- package/dist/client.js +9 -6
- package/dist/{compress-Cp5NfXCC.js → compress-2w3jqBh7.js} +2 -3
- package/dist/{compression-CT2mWlKM.js → compression-uJxu0JVb.js} +32 -5
- package/dist/content-types-Le-5BNEC.js +10 -0
- package/dist/{create-server-Bp1yILQk.js → create-server-DVTZun6D.js} +617 -306
- package/dist/create-server-PA0qniaK.js +3 -0
- package/dist/{create-server-X2GR-U6t.d.ts → create-server-UVsHFQyK.d.ts} +116 -54
- package/dist/deno-aqhikNMG.js +1 -2
- package/dist/{entry-BoxCqc6w.js → entry-CMhX3yf8.js} +85 -65
- package/dist/entry-CvZ4U0ia.js +3 -0
- package/dist/{entry-DZTCaL3d.d.ts → entry-DBq_3s8X.d.ts} +10 -36
- package/dist/entry-protocol-C30UPXzy.js +14 -0
- package/dist/entry-source-DSE5NAJu.js +70 -0
- package/dist/entry-source-SHoE-DUP.js +3 -0
- package/dist/exit-Hh-O3gp4.js +83 -0
- package/dist/{header-hook-Vzw9J9BY.js → header-hook-JfqaTt34.js} +69 -23
- package/dist/{index-B8LsM6Mh.d.ts → index-gzGs53Ue.d.ts} +43 -38
- package/dist/index.d.ts +88 -44
- package/dist/index.js +216 -84
- package/dist/listen-options-7TaXjyrk.js +1 -2
- package/dist/{load-entry-CCPWbsrj.js → load-entry-Bgq1_HDV.js} +11 -12
- package/dist/{log-DQAS5gGT.js → log-DfC9D5_T.js} +70 -49
- package/dist/{node-D6m6e6EI.js → node-DQPrMFyc.js} +20 -8
- package/dist/node-DyKDFII0.js +263 -0
- package/dist/node-vite.d.ts +2 -3
- package/dist/node-vite.js +1 -1
- package/dist/node.d.ts +41 -64
- package/dist/node.js +3 -3
- package/dist/react-router-vite.d.ts +2 -3
- package/dist/react-router-vite.js +1 -1
- package/dist/react-router.d.ts +6 -17
- package/dist/react-router.js +9 -14
- package/dist/request-context-Cslbrscf.js +163 -0
- package/dist/{response-edge-CU8ZvL_F.js → response-edge-Ow5Pi5qF.js} +13 -10
- package/dist/{response-writer-D7KHfr8n.js → response-writer-Dd4Ey99f.js} +57 -21
- package/dist/{script-hashes-CU7P9oZU.js → script-hashes-BdvJ0QI_.js} +2 -3
- package/dist/serve-AatMo9k6.js +26 -0
- package/dist/{stale-build-navigation-rg5xxmTr.js → stale-build-navigation-Qf_Lem39.js} +4 -5
- package/dist/{stale-build-navigation-ypV7Ow4o.d.ts → stale-build-navigation-WjhD8zHi.d.ts} +4 -4
- package/dist/start-DYFyGKsM.js +470 -0
- package/dist/static-files-BdTPam5b.js +438 -0
- package/dist/static-pages-6IBHdYQc.d.ts +30 -0
- package/dist/tanstack-start-vite.d.ts +2 -3
- package/dist/tanstack-start-vite.js +1 -1
- package/dist/tanstack-start.d.ts +3 -4
- package/dist/tanstack-start.js +2 -3
- package/dist/tcp-3v4pmnYU.js +58 -0
- package/dist/testing.d.ts +8 -6
- package/dist/testing.js +19 -17
- package/dist/{trailing-slash-D2QWKWEu.d.ts → trailing-slash-C-yBKCib.d.ts} +2 -3
- package/dist/{types-MgFtkNQW.d.ts → types-KsKRDwAv.d.ts} +97 -46
- package/dist/vercel-pages.d.ts +3 -16
- package/dist/vercel-pages.js +1 -2
- package/dist/vercel-vite.d.ts +4 -5
- package/dist/vercel-vite.js +48 -44
- package/dist/vercel.d.ts +13 -10
- package/dist/vercel.js +48 -35
- package/dist/{vite-D4hfj3ph.js → vite-Brb8YR9I.js} +2 -7
- package/dist/{vite-B-GfswoH.js → vite-CHppJWp_.js} +5 -7
- package/dist/{vite-DyrhyKEr.js → vite-DjlHdyTM.js} +2 -7
- package/dist/vite.d.ts +2 -2
- package/dist/vite.js +885 -2
- package/package.json +13 -12
- package/dist/brotli-only-DrA29Ecw.js +0 -97
- package/dist/brotli-only-DrA29Ecw.js.map +0 -1
- package/dist/build-id-B0448vZH.js +0 -151
- package/dist/build-id-B0448vZH.js.map +0 -1
- package/dist/build-manifest-DBMWe3Lx.js.map +0 -1
- package/dist/bun-BCz6K_Xv.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/compress-Cp5NfXCC.js.map +0 -1
- package/dist/compression-CT2mWlKM.js.map +0 -1
- package/dist/create-server-Bp1yILQk.js.map +0 -1
- package/dist/deno-aqhikNMG.js.map +0 -1
- package/dist/entry-BoxCqc6w.js.map +0 -1
- package/dist/header-hook-Vzw9J9BY.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/listen-options-7TaXjyrk.js.map +0 -1
- package/dist/load-entry-CCPWbsrj.js.map +0 -1
- package/dist/log-DQAS5gGT.js.map +0 -1
- package/dist/node-CVVdk2N6.js +0 -334
- package/dist/node-CVVdk2N6.js.map +0 -1
- package/dist/node-D6m6e6EI.js.map +0 -1
- package/dist/react-router.js.map +0 -1
- package/dist/request-context-Bxdbk5P1.js +0 -80
- package/dist/request-context-Bxdbk5P1.js.map +0 -1
- package/dist/response-edge-CU8ZvL_F.js.map +0 -1
- package/dist/response-writer-D7KHfr8n.js.map +0 -1
- package/dist/script-hashes-CU7P9oZU.js.map +0 -1
- package/dist/serve-W4WacdOw.js +0 -29
- package/dist/serve-W4WacdOw.js.map +0 -1
- package/dist/stale-build-navigation-rg5xxmTr.js.map +0 -1
- package/dist/start-j0VlxeiI.js +0 -324
- package/dist/start-j0VlxeiI.js.map +0 -1
- package/dist/static-files-C2ea8KQ6.js +0 -310
- package/dist/static-files-C2ea8KQ6.js.map +0 -1
- package/dist/tanstack-start.js.map +0 -1
- package/dist/tcp-BgDXbjOG.js +0 -42
- package/dist/tcp-BgDXbjOG.js.map +0 -1
- package/dist/testing.js.map +0 -1
- package/dist/vercel-pages.js.map +0 -1
- package/dist/vercel-vite.js.map +0 -1
- package/dist/vercel.js.map +0 -1
- package/dist/vite-B-GfswoH.js.map +0 -1
- package/dist/vite-D4hfj3ph.js.map +0 -1
- package/dist/vite-DPiSKHDn.js +0 -870
- package/dist/vite-DPiSKHDn.js.map +0 -1
- package/dist/vite-DyrhyKEr.js.map +0 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { i as copyPath } from "./compression-uJxu0JVb.js";
|
|
2
|
+
import { createBrotliDecompress } from "node:zlib";
|
|
3
|
+
import { Duplex } from "node:stream";
|
|
4
|
+
|
|
5
|
+
// src/vercel/brotli-only.ts
|
|
6
|
+
/**
|
|
7
|
+
* The URL path of the list that `vercel()` writes in the function's client
|
|
8
|
+
* build: a {@link FunctionFiles}. Why a dotfile: the static stages never
|
|
9
|
+
* serve one.
|
|
10
|
+
*/
|
|
11
|
+
const FILE_LIST = "/.app-server-files.json";
|
|
12
|
+
/** The bytes of `range` in `stream`. It cancels `stream` after the last byte. */
|
|
13
|
+
function sliced(stream, { start, end }) {
|
|
14
|
+
let position = 0;
|
|
15
|
+
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
16
|
+
const from = Math.max(start - position, 0);
|
|
17
|
+
const to = Math.min(end + 1 - position, chunk.byteLength);
|
|
18
|
+
position += chunk.byteLength;
|
|
19
|
+
if (to > from) controller.enqueue(chunk.subarray(from, to));
|
|
20
|
+
if (position > end) controller.terminate();
|
|
21
|
+
} }));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The bytes of the stream that `open` gives, with `open` called on the first
|
|
25
|
+
* read. Why: a pipe reads its source at once, and a stage can drop a response
|
|
26
|
+
* that no one reads.
|
|
27
|
+
*/
|
|
28
|
+
function decodedOnRead(open) {
|
|
29
|
+
let reader;
|
|
30
|
+
return new ReadableStream({
|
|
31
|
+
async pull(controller) {
|
|
32
|
+
reader ??= open().then((stream) => stream.getReader());
|
|
33
|
+
const { done, value } = await (await reader).read();
|
|
34
|
+
if (done) controller.close();
|
|
35
|
+
else controller.enqueue(value);
|
|
36
|
+
},
|
|
37
|
+
async cancel(reason) {
|
|
38
|
+
await (await reader)?.cancel(reason);
|
|
39
|
+
}
|
|
40
|
+
}, { highWaterMark: 0 });
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* An `open` that reads each file of `brotliOnly` from its brotli copy, which
|
|
44
|
+
* it decompresses, and each other file with `open`. Why: a function then
|
|
45
|
+
* keeps one copy of the file, not three, and a client that accepts brotli, as
|
|
46
|
+
* each browser does, gets the copy with no work.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* brotliOnlyOpen(nodeFiles("build/client").open, new Set(["/blog/index.html"]))("/blog/index.html");
|
|
50
|
+
*/
|
|
51
|
+
function brotliOnlyOpen(open, brotliOnly) {
|
|
52
|
+
return async (path, options) => {
|
|
53
|
+
if (!brotliOnly.has(path)) return open(path, options);
|
|
54
|
+
return decodedOnRead(async () => {
|
|
55
|
+
const decompress = Duplex.toWeb(createBrotliDecompress());
|
|
56
|
+
const decoded = (await open(copyPath(path, "br"))).pipeThrough(decompress);
|
|
57
|
+
const range = options?.range;
|
|
58
|
+
return range == null ? decoded : sliced(decoded, range);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { brotliOnlyOpen as n, FILE_LIST as t };
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { s as withBuiltInHeaderHook } from "./header-hook-JfqaTt34.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
|
+
* The options whose plugins must see the prerendered pages. Why a list
|
|
22
|
+
* here: `prerendered()` answers a page with no later stage, so a plugin of
|
|
23
|
+
* one of these options that comes after it changes only the rendered pages.
|
|
24
|
+
*/
|
|
25
|
+
const SEES_PRERENDERED_PAGES = /* @__PURE__ */ new Set([
|
|
26
|
+
"securityHeaders",
|
|
27
|
+
"noindex",
|
|
28
|
+
"buildId",
|
|
29
|
+
"contentSecurityPolicy",
|
|
30
|
+
"env"
|
|
31
|
+
]);
|
|
32
|
+
/**
|
|
33
|
+
* Stops startup for a plugin list that puts a plugin of an option of
|
|
34
|
+
* `defaultPlugins()` where it does not do its job:
|
|
35
|
+
*
|
|
36
|
+
* - Two plugins of one option, such as two `bodyLimit()`. Why: two copies do
|
|
37
|
+
* not fail, and they do not act as one. A value that the app gave is lost,
|
|
38
|
+
* or both act, such as two trailing-slash redirects that loop.
|
|
39
|
+
* - A plugin of `securityHeaders`, `noindex`, `buildId`,
|
|
40
|
+
* `contentSecurityPolicy`, or `env` after `prerendered()`. Why: the
|
|
41
|
+
* prerendered pages then do not get it. Development serves no prerendered
|
|
42
|
+
* page, so only production shows the failure.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* checkOptionPlugins([...defaultPlugins(), bodyLimit({ maxBytes: 1024 })]); // throws
|
|
46
|
+
* checkOptionPlugins([...defaultPlugins(), noindex()]); // throws
|
|
47
|
+
*/
|
|
48
|
+
function checkOptionPlugins(plugins) {
|
|
49
|
+
const seen = /* @__PURE__ */ new Set();
|
|
50
|
+
for (const plugin of plugins) {
|
|
51
|
+
const option = options.get(plugin);
|
|
52
|
+
if (option != null && seen.has(option)) throw new Error(`The plugin list has the ${plugin.name} plugin two times. Keep one: set the "${option}" option of defaultPlugins(), and do not also add the plugin. To put your own copy at another place, take the default out of the list first, such as with defaultPlugins().filter((plugin) => plugin.name !== "${plugin.name}").`);
|
|
53
|
+
if (option != null && seen.has("prerendered") && SEES_PRERENDERED_PAGES.has(option)) throw new Error(`The ${plugin.name} plugin comes after prerendered(), so the prerendered pages do not get it. Set the "${option}" option of defaultPlugins(), or put the plugin before prerendered().`);
|
|
54
|
+
if (option != null) seen.add(option);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** The request headers whose presence alone changes the answer of each plugin, by plugin object. */
|
|
58
|
+
const presence = /* @__PURE__ */ new WeakMap();
|
|
59
|
+
/**
|
|
60
|
+
* Marks `plugin` as one whose changes to a response depend only on whether
|
|
61
|
+
* each of `headers` is present in the request. Why: the build can then put
|
|
62
|
+
* one static answer for each case on a CDN, which picks the answer by the
|
|
63
|
+
* same headers.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* return presenceHeaders(["x-ngrok-edge"], { name: "noindex", middleware });
|
|
67
|
+
*/
|
|
68
|
+
function presenceHeaders(headers, plugin) {
|
|
69
|
+
presence.set(plugin, headers);
|
|
70
|
+
return plugin;
|
|
71
|
+
}
|
|
72
|
+
/** The headers that `marks` holds for `plugins`, in lowercase, once each. */
|
|
73
|
+
function markedHeaders(marks, plugins) {
|
|
74
|
+
return [...new Set(plugins.flatMap((plugin) => marks.get(plugin) ?? []).map((name) => name.toLowerCase()))];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The headers that {@link presenceHeaders} marked on `plugins`, in lowercase,
|
|
78
|
+
* once each.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* presenceHeadersOf(defaultPlugins({ noindex: { unlessHeader: "X-Ngrok-Edge" } })); // => ["x-ngrok-edge"]
|
|
82
|
+
*/
|
|
83
|
+
function presenceHeadersOf(plugins) {
|
|
84
|
+
return markedHeaders(presence, plugins);
|
|
85
|
+
}
|
|
86
|
+
/** The response headers that each plugin gives a new value for each request, by plugin object. */
|
|
87
|
+
const perRequest = /* @__PURE__ */ new WeakMap();
|
|
88
|
+
/**
|
|
89
|
+
* Marks `plugin` as one that gives each of the response headers `headers` a
|
|
90
|
+
* new value for each request. Why: the build's check of the CDN pages drops
|
|
91
|
+
* these headers, so they do not keep a prerendered page off the CDN.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* return perRequestHeaders(["X-Request-Id"], { name: "request-id", middleware });
|
|
95
|
+
*/
|
|
96
|
+
function perRequestHeaders(headers, plugin) {
|
|
97
|
+
perRequest.set(plugin, headers);
|
|
98
|
+
return plugin;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The headers that {@link perRequestHeaders} marked on `plugins`, in
|
|
102
|
+
* lowercase, once each.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* perRequestHeadersOf(defaultPlugins({ requestId: { headerName: "X-Correlation-Id" } })); // => ["x-correlation-id"]
|
|
106
|
+
*/
|
|
107
|
+
function perRequestHeadersOf(plugins) {
|
|
108
|
+
return markedHeaders(perRequest, plugins);
|
|
109
|
+
}
|
|
110
|
+
/** An HTTP header name: a token of RFC 9110. */
|
|
111
|
+
const HEADER_NAME = /^[\w!#$%&'*+.^`|~-]+$/u;
|
|
112
|
+
/**
|
|
113
|
+
* A character that a header value cannot hold: a control character other
|
|
114
|
+
* than a tab, or a character above U+00FF. Why this set: it is the rule of
|
|
115
|
+
* the HTTP writer of Node.js, which is the strictest of the hosts.
|
|
116
|
+
*/
|
|
117
|
+
const NOT_IN_HEADER_VALUE = /[^\t\u0020-\u007E\u0080-\u00FF]/u;
|
|
118
|
+
/**
|
|
119
|
+
* Throws when `name`, the `option` of the plugin factory `plugin`, is not an
|
|
120
|
+
* HTTP header name, so a bad option stops startup. Why: such a name starts
|
|
121
|
+
* with no error, and each response that gets the header is then a 500.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* checkHeaderName({ plugin: "requestId", option: "headerName", name: "X Request Id" }); // throws
|
|
125
|
+
*/
|
|
126
|
+
function checkHeaderName({ plugin, option, name }) {
|
|
127
|
+
if (!HEADER_NAME.test(name)) throw new TypeError(`The ${option} of ${plugin} must be an HTTP header name, not ${JSON.stringify(name)}. Use only letters, digits, and "-".`);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Throws when `value`, the `option` of the plugin factory `plugin`, is not a
|
|
131
|
+
* value that a header can hold, so a bad option stops startup. Why: such a
|
|
132
|
+
* value starts with no error, and each response that gets the header is then
|
|
133
|
+
* a 500.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* checkHeaderValue({ plugin: "buildId", option: "value", value: "v42\n" }); // throws
|
|
137
|
+
*/
|
|
138
|
+
function checkHeaderValue({ plugin, option, value }) {
|
|
139
|
+
if (NOT_IN_HEADER_VALUE.test(value)) throw new TypeError(`The ${option} of ${plugin} must be a value that a header can hold, not ${JSON.stringify(value)}. Remove each control character, such as a line break, and each character above U+00FF.`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/core/plugins/build-id.ts
|
|
143
|
+
/** The header that {@link buildId} sets by default. */
|
|
144
|
+
const BUILD_ID_HEADER = "X-Build-Id";
|
|
145
|
+
/**
|
|
146
|
+
* The `Server-Timing` metric that holds the build of a response. Why
|
|
147
|
+
* `Server-Timing`: a script can read it from the performance entries, with no
|
|
148
|
+
* request and no change to the document's HTML.
|
|
149
|
+
*/
|
|
150
|
+
const BUILD_ID_METRIC = "build";
|
|
151
|
+
/**
|
|
152
|
+
* Whether a cache can use the response again without the server: it has a
|
|
153
|
+
* fresh time, a stale time, or a date that a cache can guess a fresh time from.
|
|
154
|
+
*/
|
|
155
|
+
function isReusable(response) {
|
|
156
|
+
const cacheControl = response.get("Cache-Control") ?? "";
|
|
157
|
+
if (/\b(?:no-store|no-cache)\b/iu.test(cacheControl)) return false;
|
|
158
|
+
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");
|
|
159
|
+
}
|
|
160
|
+
/** The metrics of a `Server-Timing` header, such as `build;desc="v42"`. */
|
|
161
|
+
function serverTimingMetrics(header) {
|
|
162
|
+
return (header?.match(/(?:[^",]|"(?:[^"\\]|\\.)*")+/gu) ?? []).map((metric) => metric.trim()).filter((metric) => metric !== "");
|
|
163
|
+
}
|
|
164
|
+
/** Whether a `Server-Timing` metric is a build. */
|
|
165
|
+
function isBuildMetric(metric) {
|
|
166
|
+
return metric.split(";")[0]?.trim().toLowerCase() === BUILD_ID_METRIC;
|
|
167
|
+
}
|
|
168
|
+
/** The `Server-Timing` metrics of `header` that are not a build. */
|
|
169
|
+
function otherMetrics(header) {
|
|
170
|
+
return serverTimingMetrics(header).filter((metric) => !isBuildMetric(metric));
|
|
171
|
+
}
|
|
172
|
+
/** The data check before `setup` runs. */
|
|
173
|
+
function noDataRequests() {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
/** A `Server-Timing` quoted string with the characters of `value`. */
|
|
177
|
+
function quoted(value) {
|
|
178
|
+
return `"${value.replaceAll(/["\\]/gu, String.raw`\$&`)}"`;
|
|
179
|
+
}
|
|
180
|
+
/** The `build` metric of `value`, or `undefined` for no build. */
|
|
181
|
+
function buildMetric(value) {
|
|
182
|
+
return value == null ? void 0 : `${BUILD_ID_METRIC};desc=${quoted(value)}`;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Sets a build ID header on documents and on the framework's data responses,
|
|
186
|
+
* such as React Router `.data` files, so a client can see that a deploy
|
|
187
|
+
* happened under it. Each of them also gets the build as
|
|
188
|
+
* `Server-Timing: build;desc="<value>"`. The `paths` option adds the metric
|
|
189
|
+
* to more routes, such as API proxy routes. The plugin removes the build
|
|
190
|
+
* header and the `build` metric of an upstream API from each other
|
|
191
|
+
* response, so the client reads only this app's build.
|
|
192
|
+
* `staleBuildReactRouter()` and `staleBuildTanStack()` from `@ngrok/app-server/client` read the metric
|
|
193
|
+
* from the document, and from a response that the browser fetches with no
|
|
194
|
+
* script, such as a prefetch.
|
|
195
|
+
*
|
|
196
|
+
* With no `value`, it sends the build ID that the `appServer` Vite plugin
|
|
197
|
+
* derives from the client build. Development and a build with no
|
|
198
|
+
* `.app-server.json` then get no build.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* buildId({ value: process.env.IMAGE_TAG });
|
|
202
|
+
*/
|
|
203
|
+
function buildId({ headerName = BUILD_ID_HEADER, value: given, paths } = {}) {
|
|
204
|
+
const explicit = given === "" ? void 0 : given;
|
|
205
|
+
checkHeaderName({
|
|
206
|
+
plugin: "buildId",
|
|
207
|
+
option: "headerName",
|
|
208
|
+
name: headerName
|
|
209
|
+
});
|
|
210
|
+
if (explicit != null) checkHeaderValue({
|
|
211
|
+
plugin: "buildId",
|
|
212
|
+
option: "value",
|
|
213
|
+
value: explicit
|
|
214
|
+
});
|
|
215
|
+
let value = explicit;
|
|
216
|
+
let timing = buildMetric(value);
|
|
217
|
+
let isDataRequest = noDataRequests;
|
|
218
|
+
return optionPlugin("buildId", {
|
|
219
|
+
name: "build-id",
|
|
220
|
+
setup: (info) => {
|
|
221
|
+
({isDataRequest} = info);
|
|
222
|
+
value = explicit ?? info.buildId;
|
|
223
|
+
timing = buildMetric(value);
|
|
224
|
+
},
|
|
225
|
+
etagPart: () => value,
|
|
226
|
+
middleware: (context, next) => withBuiltInHeaderHook({
|
|
227
|
+
context,
|
|
228
|
+
next,
|
|
229
|
+
hook: (response) => {
|
|
230
|
+
const { path } = context.req;
|
|
231
|
+
const isRouter = (response.get("Content-Type")?.startsWith("text/html") ?? false) || isDataRequest(path);
|
|
232
|
+
const isStamped = isRouter || paths?.(path) === true && !isReusable(response);
|
|
233
|
+
const before = response.get("Server-Timing");
|
|
234
|
+
const stamp = isStamped && timing != null ? timing : "";
|
|
235
|
+
const metrics = before == null ? stamp : [...otherMetrics(before), ...stamp === "" ? [] : [stamp]].join(", ");
|
|
236
|
+
if (metrics === "") response.delete("Server-Timing");
|
|
237
|
+
else if (metrics !== before) response.set("Server-Timing", metrics);
|
|
238
|
+
if (isRouter && value != null) response.set(headerName, value);
|
|
239
|
+
else response.delete(headerName);
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { serverTimingMetrics as a, checkOptionPlugins as c, perRequestHeadersOf as d, presenceHeaders as f, isBuildMetric as i, optionPlugin as l, BUILD_ID_METRIC as n, checkHeaderName as o, presenceHeadersOf as p, buildId as r, checkHeaderValue as s, BUILD_ID_HEADER as t, perRequestHeaders as u };
|
|
@@ -5,8 +5,15 @@
|
|
|
5
5
|
* static stages never serve it.
|
|
6
6
|
*/
|
|
7
7
|
const BUILD_MANIFEST_FILE = ".app-server.json";
|
|
8
|
+
/**
|
|
9
|
+
* The file name of the server entry in the server build. Each host gets this
|
|
10
|
+
* name, and `app-server serve` looks for it.
|
|
11
|
+
*/
|
|
12
|
+
const SERVER_ENTRY_FILE = "app-server.js";
|
|
8
13
|
/** The URL path of the hashed files of a build that records none: Vite's default `build.assetsDir`. */
|
|
9
14
|
const DEFAULT_ASSETS_PATH = "/assets/";
|
|
15
|
+
/** The `Cache-Control` of a hashed file. Why one year: the URL of the file changes with its bytes. */
|
|
16
|
+
const IMMUTABLE = "public, max-age=31536000, immutable";
|
|
10
17
|
/**
|
|
11
18
|
* The key for a URL path in {@link BuildManifest}: the path with no trailing
|
|
12
19
|
* slash, so `/a/` and `/a` share headers. The root stays `/`.
|
|
@@ -39,7 +46,8 @@ function assetsPathOf(manifest) {
|
|
|
39
46
|
}
|
|
40
47
|
/**
|
|
41
48
|
* Reads the build manifest of a client build. A build with no file has an
|
|
42
|
-
* empty one. A file in any other shape throws, so a
|
|
49
|
+
* empty one. A file that is not JSON, or one in any other shape, throws, so a
|
|
50
|
+
* bad build stops startup.
|
|
43
51
|
*/
|
|
44
52
|
async function readBuildManifest(files) {
|
|
45
53
|
const path = `/${BUILD_MANIFEST_FILE}`;
|
|
@@ -48,7 +56,9 @@ async function readBuildManifest(files) {
|
|
|
48
56
|
headers: {},
|
|
49
57
|
scriptHashes: {}
|
|
50
58
|
};
|
|
51
|
-
const parsed = await new Response(await files.open(path)).json()
|
|
59
|
+
const parsed = await new Response(await files.open(path)).json().catch((error) => {
|
|
60
|
+
throw new Error(`The server cannot read ${BUILD_MANIFEST_FILE} as JSON. Build the app again.`, { cause: error });
|
|
61
|
+
});
|
|
52
62
|
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.`);
|
|
53
63
|
const headers = {};
|
|
54
64
|
for (const [key, value] of Object.entries(parsed.headers)) {
|
|
@@ -69,5 +79,4 @@ async function readBuildManifest(files) {
|
|
|
69
79
|
};
|
|
70
80
|
}
|
|
71
81
|
|
|
72
|
-
export {
|
|
73
|
-
//# sourceMappingURL=build-manifest-DBMWe3Lx.js.map
|
|
82
|
+
export { manifestKey as a, SERVER_ENTRY_FILE as i, DEFAULT_ASSETS_PATH as n, readBuildManifest as o, IMMUTABLE as r, BUILD_MANIFEST_FILE as t };
|
package/dist/bun-BCz6K_Xv.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/core/check-count.ts
|
|
2
|
+
/** Why: `setTimeout` turns a longer delay into 1 ms. */
|
|
3
|
+
const MAX_TIMER_MS = 2147483647;
|
|
4
|
+
/**
|
|
5
|
+
* Throws for a value that is not an integer from 0 to `max`, so a bad option stops startup.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* checkCount("shutdownDeadlineMs", 10_000, MAX_TIMER_MS);
|
|
9
|
+
*/
|
|
10
|
+
function checkCount(name, value, max = Number.MAX_SAFE_INTEGER) {
|
|
11
|
+
if (!Number.isInteger(value) || value < 0 || value > max) {
|
|
12
|
+
const range = max === Number.MAX_SAFE_INTEGER ? "of 0 or more" : `from 0 to ${max}`;
|
|
13
|
+
throw new Error(`${name} must be an integer ${range}, not ${value}.`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { checkCount as n, MAX_TIMER_MS as t };
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as parsePort } from "./tcp-
|
|
2
|
+
import { t as parsePort } from "./tcp-3v4pmnYU.js";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
3
4
|
import process from "node:process";
|
|
4
5
|
import { parseArgs } from "node:util";
|
|
5
6
|
|
|
@@ -42,14 +43,16 @@ try {
|
|
|
42
43
|
if (args?.help === true) process.stdout.write(`${USAGE}\n`);
|
|
43
44
|
else if (args?.command === "start") {
|
|
44
45
|
process.env.NODE_ENV ??= "development";
|
|
45
|
-
const { start } = await import("./start-
|
|
46
|
+
const { start } = await import("./start-DYFyGKsM.js");
|
|
46
47
|
await start();
|
|
47
48
|
} else if (args?.command === "serve") {
|
|
48
49
|
process.env.NODE_ENV ??= "production";
|
|
49
50
|
if (typeof process.setSourceMapsEnabled === "function") process.setSourceMapsEnabled(true);
|
|
50
|
-
const { serve } = await import("./serve-
|
|
51
|
-
|
|
51
|
+
const { DEFAULT_BUILD_PATHS, serve } = await import("./serve-AatMo9k6.js");
|
|
52
|
+
const buildPath = args.buildPath ?? DEFAULT_BUILD_PATHS.find((each) => existsSync(each));
|
|
53
|
+
if (buildPath == null) fail(`No server build found. Build the app, or pass its path. Looked for: ${DEFAULT_BUILD_PATHS.join(", ")}.`);
|
|
54
|
+
else if (existsSync(buildPath)) await serve({ buildPath });
|
|
55
|
+
else fail(`No server build at ${buildPath}. Build the app, or pass its path.`);
|
|
52
56
|
} else if (args != null) fail(args.command == null ? "Give a command." : `Unknown command: ${args.command}.`);
|
|
53
57
|
|
|
54
|
-
export { };
|
|
55
|
-
//# sourceMappingURL=cli.js.map
|
|
58
|
+
export { };
|
package/dist/client-browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as staleBuildReactRouter, o as staleBuildTanStack } from "./stale-build-navigation-
|
|
1
|
+
import { a as staleBuildReactRouter, o as staleBuildTanStack } from "./stale-build-navigation-WjhD8zHi.js";
|
|
2
2
|
/**
|
|
3
3
|
* The CSP nonce of the document's first element with a nonce, or `undefined`
|
|
4
4
|
* when it has none. The browser build of `@ngrok/app-server/client` exports it
|
|
@@ -8,5 +8,4 @@ import { a as staleBuildReactRouter, o as staleBuildTanStack } from "./stale-bui
|
|
|
8
8
|
* documentNonce(); // => "d2hhdCBhIG5vbmNl"
|
|
9
9
|
*/
|
|
10
10
|
declare function documentNonce(): string | undefined;
|
|
11
|
-
export { documentNonce as cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
|
12
|
-
//# sourceMappingURL=client-browser.d.ts.map
|
|
11
|
+
export { documentNonce as cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
package/dist/client-browser.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { n as staleBuildTanStack, r as documentNonce, t as staleBuildReactRouter } from "./stale-build-navigation-
|
|
1
|
+
import { n as staleBuildTanStack, r as documentNonce, t as staleBuildReactRouter } from "./stale-build-navigation-Qf_Lem39.js";
|
|
2
2
|
|
|
3
3
|
export { documentNonce as cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { a as staleBuildReactRouter, i as StaleBuildReactRouter, n as RouterFetchContext, o as staleBuildTanStack, r as StaleBuildOptions, t as NavigationEvents } from "./stale-build-navigation-
|
|
1
|
+
import { a as staleBuildReactRouter, i as StaleBuildReactRouter, n as RouterFetchContext, o as staleBuildTanStack, r as StaleBuildOptions, t as NavigationEvents } from "./stale-build-navigation-WjhD8zHi.js";
|
|
2
2
|
/**
|
|
3
3
|
* The CSP nonce of the document, for the framework's render. It gives the
|
|
4
4
|
* same value on the server and in the browser, so the hydration matches.
|
|
5
5
|
*
|
|
6
|
-
* -
|
|
6
|
+
* - In a server request, the nonce of the request, from `requestContext()`.
|
|
7
|
+
* Why the request first: a test environment, such as jsdom, and a DOM
|
|
8
|
+
* polyfill give the server a `document`.
|
|
7
9
|
* - In the browser, the nonce of the document's first element with a nonce.
|
|
8
10
|
* Why: React Router's `<HydratedRouter>` takes no nonce, so the app reads it
|
|
9
11
|
* from the document.
|
|
@@ -17,5 +19,4 @@ import { a as staleBuildReactRouter, i as StaleBuildReactRouter, n as RouterFetc
|
|
|
17
19
|
* <Scripts nonce={cspNonce()} />
|
|
18
20
|
*/
|
|
19
21
|
export declare function cspNonce(): string | undefined;
|
|
20
|
-
export { type NavigationEvents, type RouterFetchContext, type StaleBuildOptions, type StaleBuildReactRouter, staleBuildReactRouter, staleBuildTanStack };
|
|
21
|
-
//# sourceMappingURL=client.d.ts.map
|
|
22
|
+
export { type NavigationEvents, type RouterFetchContext, type StaleBuildOptions, type StaleBuildReactRouter, staleBuildReactRouter, staleBuildTanStack };
|
package/dist/client.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as staleBuildTanStack, r as documentNonce, t as staleBuildReactRouter } from "./stale-build-navigation-
|
|
1
|
+
import { a as requestContext } from "./request-context-Cslbrscf.js";
|
|
2
|
+
import { n as staleBuildTanStack, r as documentNonce, t as staleBuildReactRouter } from "./stale-build-navigation-Qf_Lem39.js";
|
|
3
3
|
|
|
4
4
|
// src/client/csp-nonce.ts
|
|
5
5
|
/**
|
|
6
6
|
* The CSP nonce of the document, for the framework's render. It gives the
|
|
7
7
|
* same value on the server and in the browser, so the hydration matches.
|
|
8
8
|
*
|
|
9
|
-
* -
|
|
9
|
+
* - In a server request, the nonce of the request, from `requestContext()`.
|
|
10
|
+
* Why the request first: a test environment, such as jsdom, and a DOM
|
|
11
|
+
* polyfill give the server a `document`.
|
|
10
12
|
* - In the browser, the nonce of the document's first element with a nonce.
|
|
11
13
|
* Why: React Router's `<HydratedRouter>` takes no nonce, so the app reads it
|
|
12
14
|
* from the document.
|
|
@@ -20,8 +22,9 @@ import { n as staleBuildTanStack, r as documentNonce, t as staleBuildReactRouter
|
|
|
20
22
|
* <Scripts nonce={cspNonce()} />
|
|
21
23
|
*/
|
|
22
24
|
function cspNonce() {
|
|
23
|
-
|
|
25
|
+
const context = requestContext();
|
|
26
|
+
if (context != null) return context.cspNonce;
|
|
27
|
+
return typeof document === "undefined" ? void 0 : documentNonce();
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
export { cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
|
27
|
-
//# sourceMappingURL=client.js.map
|
|
30
|
+
export { cspNonce, staleBuildReactRouter, staleBuildTanStack };
|
|
@@ -153,7 +153,7 @@ function take(pool, size) {
|
|
|
153
153
|
let best = -1;
|
|
154
154
|
let bestSize = -1;
|
|
155
155
|
for (let index = idle.length - 1; index >= 0; index -= 1) {
|
|
156
|
-
const each = idle[index]?.size ??
|
|
156
|
+
const each = idle[index]?.size ?? 1e400;
|
|
157
157
|
if (each <= size && each > bestSize) {
|
|
158
158
|
best = index;
|
|
159
159
|
bestSize = each;
|
|
@@ -491,5 +491,4 @@ function brotliStream(source) {
|
|
|
491
491
|
return zlibStream(source, BROTLI);
|
|
492
492
|
}
|
|
493
493
|
|
|
494
|
-
export { gzipStream as n, waitsForZlib as r, brotliStream as t };
|
|
495
|
-
//# sourceMappingURL=compress-Cp5NfXCC.js.map
|
|
494
|
+
export { gzipStream as n, waitsForZlib as r, brotliStream as t };
|
|
@@ -41,6 +41,16 @@ function copyName(name, encoding) {
|
|
|
41
41
|
return `.${name}.${COPY_EXTENSIONS[encoding]}`;
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
|
+
* The URL path of the `encoding` copy of the file at the URL path `path`.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* copyPath("/assets/app-abc.js", "br"); // => "/assets/.app-abc.js.br"
|
|
48
|
+
*/
|
|
49
|
+
function copyPath(path, encoding) {
|
|
50
|
+
const name = path.lastIndexOf("/") + 1;
|
|
51
|
+
return `${path.slice(0, name)}${copyName(path.slice(name), encoding)}`;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
44
54
|
* Whether the server compresses a body of the content type `type`.
|
|
45
55
|
*
|
|
46
56
|
* @example
|
|
@@ -87,18 +97,35 @@ function forbidsTransform(cacheControl) {
|
|
|
87
97
|
return /\bno-transform\b/u.test(cacheControl ?? "");
|
|
88
98
|
}
|
|
89
99
|
/**
|
|
100
|
+
* Puts what `pending` settles with in `settled`. Why it never rejects: a
|
|
101
|
+
* client that leaves before the body is read must not cause an unhandled
|
|
102
|
+
* rejection.
|
|
103
|
+
*/
|
|
104
|
+
async function watch(pending, settled) {
|
|
105
|
+
try {
|
|
106
|
+
settled.result = await pending;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
settled.failure = { error };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
90
112
|
* The next result if `reader` has it queued, or the read that is still open.
|
|
91
|
-
* It never waits for the source.
|
|
113
|
+
* It never waits for the source. It gives the read a fixed count of turns of
|
|
114
|
+
* the microtask queue: a read that settles within three turns is a result,
|
|
115
|
+
* or it throws when the read fails.
|
|
92
116
|
*
|
|
93
117
|
* @example
|
|
94
118
|
* const next = await readIfQueued(reader); // => { result: { done: false, value } } or { pending }
|
|
95
119
|
*/
|
|
96
120
|
async function readIfQueued(reader) {
|
|
97
121
|
const pending = reader.read();
|
|
98
|
-
|
|
99
|
-
|
|
122
|
+
const settled = {};
|
|
123
|
+
watch(pending, settled);
|
|
124
|
+
for (let turn = 0; turn < 4 && settled.result == null && settled.failure == null; turn += 1) await Promise.resolve();
|
|
125
|
+
const { result, failure } = settled;
|
|
126
|
+
await Promise.resolve();
|
|
127
|
+
if (failure != null) throw failure.error;
|
|
100
128
|
return result == null ? { pending } : { result };
|
|
101
129
|
}
|
|
102
130
|
|
|
103
|
-
export {
|
|
104
|
-
//# sourceMappingURL=compression-CT2mWlKM.js.map
|
|
131
|
+
export { forbidsTransform as a, readIfQueued as c, copyPath as i, MIN_COMPRESS_BYTES as n, isCompressible as o, copyName as r, preferredEncoding as s, COPY_ENCODINGS as t };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/react-router/content-types.ts
|
|
2
|
+
/**
|
|
3
|
+
* The content types of React Router's files that `mime` does not know, by
|
|
4
|
+
* file extension. Why one constant: the server sends a file with this type,
|
|
5
|
+
* and the build writes the compressed copies of a file by this type, so the
|
|
6
|
+
* two must agree.
|
|
7
|
+
*/
|
|
8
|
+
const CONTENT_TYPES = { ".data": "text/x-script" };
|
|
9
|
+
|
|
10
|
+
export { CONTENT_TYPES as t };
|