@ngrok/app-server 0.3.3 → 0.3.4
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 +13 -9
- package/dist/brotli-only-DrA29Ecw.js +97 -0
- package/dist/brotli-only-DrA29Ecw.js.map +1 -0
- package/dist/cli.js +3 -3
- package/dist/compress-JuJf2HaO.js +377 -0
- package/dist/compress-JuJf2HaO.js.map +1 -0
- package/dist/{compression-7_gToa8R.js → compression-CT2mWlKM.js} +9 -2
- package/dist/compression-CT2mWlKM.js.map +1 -0
- package/dist/{create-server-jMl2ltHD.js → create-server-BX2rH6-E.js} +45 -26
- package/dist/{create-server-jMl2ltHD.js.map → create-server-BX2rH6-E.js.map} +1 -1
- package/dist/{create-server-D4-nVOVJ.d.ts → create-server-kxyhCdhv.d.ts} +3 -3
- package/dist/{entry-BPrgE_6K.d.ts → entry-CNL4OsEn.d.ts} +3 -3
- package/dist/{entry-D9H4LA9c.js → entry-ChHgs269.js} +4 -3
- package/dist/{entry-D9H4LA9c.js.map → entry-ChHgs269.js.map} +1 -1
- package/dist/{index-Db7dspq6.d.ts → index-B8LsM6Mh.d.ts} +21 -1
- package/dist/index.d.ts +13 -12
- package/dist/index.js +2 -2
- package/dist/{load-entry-D7r0mjEp.js → load-entry-BOgJPt1I.js} +4 -4
- package/dist/{load-entry-D7r0mjEp.js.map → load-entry-BOgJPt1I.js.map} +1 -1
- package/dist/{node-C1qudLs_.js → node-B0AYf_IA.js} +5 -4
- package/dist/{node-C1qudLs_.js.map → node-B0AYf_IA.js.map} +1 -1
- package/dist/{node-tBVQPjQv.js → node-C0Ad7CVL.js} +2 -2
- package/dist/{node-tBVQPjQv.js.map → node-C0Ad7CVL.js.map} +1 -1
- package/dist/node-vite.d.ts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.js +2 -2
- package/dist/react-router-vite.d.ts +1 -1
- package/dist/react-router.d.ts +1 -1
- package/dist/{response-writer-B7FoJjEE.js → response-writer-DzvUAz9x.js} +3 -3
- package/dist/response-writer-DzvUAz9x.js.map +1 -0
- package/dist/{script-hashes-C6hQ5XnP.js → script-hashes-CU7P9oZU.js} +2 -2
- package/dist/{script-hashes-C6hQ5XnP.js.map → script-hashes-CU7P9oZU.js.map} +1 -1
- package/dist/{serve-E0hSGe6_.js → serve-BUJ7ZonG.js} +2 -2
- package/dist/{serve-E0hSGe6_.js.map → serve-BUJ7ZonG.js.map} +1 -1
- package/dist/{start-CifYPRq1.js → start-DiQf9ttp.js} +6 -6
- package/dist/{start-CifYPRq1.js.map → start-DiQf9ttp.js.map} +1 -1
- package/dist/{static-files-Beju9riB.js → static-files-C2ea8KQ6.js} +11 -3
- package/dist/static-files-C2ea8KQ6.js.map +1 -0
- package/dist/tanstack-start-vite.d.ts +1 -1
- package/dist/tanstack-start.d.ts +2 -2
- package/dist/{tcp-Dd1Mt6sN.js → tcp-Bws6oEau.js} +2 -2
- package/dist/{tcp-Dd1Mt6sN.js.map → tcp-Bws6oEau.js.map} +1 -1
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +3 -3
- package/dist/{trailing-slash-CFSv_Gzx.d.ts → trailing-slash-DeOUxMYo.d.ts} +2 -2
- package/dist/{types-j29vspx_.d.ts → types-BKJxtIuj.d.ts} +10 -3
- package/dist/vercel-vite.d.ts +1 -1
- package/dist/vercel-vite.js +24 -4
- package/dist/vercel-vite.js.map +1 -1
- package/dist/vercel.d.ts +4 -3
- package/dist/vercel.js +7 -5
- package/dist/vercel.js.map +1 -1
- package/dist/{vite-BDREIgiH.js → vite-DPiSKHDn.js} +139 -33
- package/dist/vite-DPiSKHDn.js.map +1 -0
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +1 -1
- package/package.json +2 -1
- package/dist/compression-7_gToa8R.js.map +0 -1
- package/dist/gzip-Doi4wqjX.js +0 -294
- package/dist/gzip-Doi4wqjX.js.map +0 -1
- package/dist/response-writer-B7FoJjEE.js.map +0 -1
- package/dist/static-files-Beju9riB.js.map +0 -1
- package/dist/vite-BDREIgiH.js.map +0 -1
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ How we measured:
|
|
|
36
36
|
- Each page has one URL. A page URL with the other trailing-slash form gets a 308 to it, for rendered and prerendered pages. A prerendered page answers at its own URL with a 200.
|
|
37
37
|
- A prerendered resource route (`llms.txt`, `sitemap.xml`, a JSON API file) and each `.data` file keep the headers their loader sent: `Content-Type`, `Cache-Control`, CORS.
|
|
38
38
|
- A static file gets the brotli or gzip copy that the build wrote. `compression()` compresses each response again for each request.
|
|
39
|
-
- A streamed page under gzip reaches the client chunk by chunk.
|
|
39
|
+
- A streamed page under brotli or gzip reaches the client chunk by chunk.
|
|
40
40
|
- Each request gets one JSON log line in your format, static files included, and an `X-Request-Id`.
|
|
41
41
|
- Shutdown drains open requests with a deadline, and the keep-alive timeout outlasts the proxy's idle pool. `@react-router/serve` closes the server on `SIGTERM`, with no deadline and no readiness probe.
|
|
42
42
|
|
|
@@ -122,11 +122,12 @@ export default defineConfig({
|
|
|
122
122
|
});
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
-
`appServer` has
|
|
125
|
+
`appServer` has four options. Each one has a default:
|
|
126
126
|
|
|
127
127
|
- `framework`: the framework adapter. Default: the adapter of the framework plugin in the Vite config. The plugin of React Router is `reactRouter()`, and the plugin of TanStack Start is `tanstackStart()`. If the config has neither plugin or both, Vite stops with an error. Then pass `reactRouterAdapter()` from `@ngrok/app-server/react-router/vite` or `tanstackStartAdapter()` from `@ngrok/app-server/tanstack-start/vite`.
|
|
128
128
|
- `host`: the host adapter. Default: `node()` from `@ngrok/app-server/node/vite`.
|
|
129
129
|
- `app`: the module whose default export is the app, relative to the Vite root. Default: `server.ts`, else `server.js`, in the Vite root. If neither file exists, the server uses the default plugins.
|
|
130
|
+
- `externals`: how the server build copies the packages that stay external. `whole` lists packages that keep each file, and `omit` lists packages that the build does not copy. Default: no package in either list. See [Deploy](#deploy).
|
|
130
131
|
|
|
131
132
|
An option that you set replaces its default. Set it for a third-party adapter or for an app in another file:
|
|
132
133
|
|
|
@@ -158,7 +159,7 @@ Run it:
|
|
|
158
159
|
|
|
159
160
|
`appServer` adds `server/app-server.js` to the server build, next to the framework's own entry, which does not change. The file bundles `server.ts` with the framework's server module, so the server gets the app's path aliases and its one copy of the framework. The build also bundles the dependencies, so the build directory runs with no install. See [Deploy](#deploy).
|
|
160
161
|
|
|
161
|
-
After the build and after the prerender pass, `appServer` writes a gzip copy (`.<file>.gz`) and a brotli copy (`.<file>.br`) next to each
|
|
162
|
+
After the build and after the prerender pass, `appServer` writes a gzip copy (`.<file>.gz`) and a brotli copy (`.<file>.br`) next to each compressible file of 1 kibibyte (KiB) or more in the client build, HTML pages included. A compressible file has a text type, a type with a `+json` or `+xml` suffix (such as an RSS feed), or another type that compresses well: JSON, JavaScript, WebAssembly, YAML, a shell script, an icon (`.ico`, `.bmp`), or a TrueType, OpenType, or Embedded OpenType font. A WOFF or WOFF2 font is compressed already, so it gets no copy. It writes the gzip copy when it is smaller than the file, and the brotli copy when it is smaller than the gzip copy. `app-server serve` sends the copy that the client's `Accept-Encoding` prefers, brotli before gzip, so it does not compress the file for each request. Each copy is a dotfile, so it has no URL of its own: a `skip` rule or an auth check for the file also blocks its copies. The build does not change a `.gz` or `.br` file of the app. A link, and a file in a linked directory, get no copy, so the build writes only inside the client build. The copies include the prerendered files, in any plugin order. The brotli copies use quality 9, not 11: on a docs build with 70 megabytes (MB) of text, quality 11 adds 46 s to the build, and quality 9 adds 0.6 s for copies that are 9% larger. `appServer` gives a list of Vite plugins, so put it in `plugins` as it is.
|
|
162
163
|
|
|
163
164
|
The build exports `appServer`, and the CLI calls it. So the copy of this package in the build runs the server, and the CLI's copy runs nothing. The CLI refuses a build from a version of this package with another entry protocol.
|
|
164
165
|
|
|
@@ -180,7 +181,9 @@ On Bun, run `bun build/server/app-server.js`, or `bun node_modules/@ngrok/app-se
|
|
|
180
181
|
|
|
181
182
|
- `appServer` bundles each dependency into the server build. For the build, it sets `ssr.noExternal: true`, unless the app sets its own `ssr.noExternal` or sets `ssr.external: true`.
|
|
182
183
|
- Some packages stay external: each package in the app's `ssr.external` or in the bundler's `external` option, the whole package of an external subpath, each peer dependency of a package that the app keeps external, the peers of each peer, each native package, and each package that reads a file next to its own module. So the bundle and an external package use one copy of a package, such as `react` for a component library. For a `RegExp` or a function in the bundler's `external` option, the build tests the name of each package in the `dependencies`, `optionalDependencies`, and `devDependencies` of the app. It keeps the peers of each match external too. A package is native when a `.node` file is in it, or in a package of its `dependencies` or `optionalDependencies`. For example, `oxc-parser` gets its addon from `@oxc-parser/binding-linux-x64-gnu`. A package reads a file next to its module when the file that an import resolves to has `new URL("./…", import.meta.url)`, `fileURLToPath(import.meta.url)`, or `import.meta.dirname`. For example, `@vercel/og` reads `resvg.wasm` so.
|
|
183
|
-
- The build copies each external package,
|
|
184
|
+
- The build copies each external package, with its installed dependencies, into `node_modules` in the server build. Each file stays where its package expects it, so CommonJS, a file read through `__dirname`, and a `.node` addon work.
|
|
185
|
+
- The build then removes each copied file that the server build cannot load, as [`@vercel/nft`](https://github.com/vercel/nft) traces it from the modules of the build, with the export conditions of Node, Bun, and Deno. So a source map, an edge build, or a dependency that a package bundled into its own file does not reach the deploy. For `@vercel/og` 1.0.1, the copy goes from 42.9 MB in 453 files to 23.6 MB. Five kinds of package keep each file: a native package and each package that it depends on (a native package can load a library, such as libvips, that a trace cannot see), each package in `ssr.external`, each package that the app gives to `require.resolve()` or `import.meta.resolve()`, each package that the trace misses files of, with each package that it depends on, and each package in `externals.whole`. The trace misses files of a package when its loaded code has `import.meta.dirname` or `import.meta.filename`, or when it cannot parse a loaded module, such as a TypeScript entry for Bun. It also misses a file that a loaded module gives to `require.resolve()` or `import.meta.resolve()` through the `exports` of a package, so that package keeps each file too. Add a package to `externals.whole` when it reads a file whose path the trace cannot find.
|
|
186
|
+
- The build does not copy a package in `externals.omit`. Use it for an optional dependency that a package loads in a `try`. For example, `@vercel/og` uses `sharp` when it is installed, else `resvg.wasm`. `appServer({ externals: { omit: ["sharp"] } })` makes the `@vercel/og` copy 3.0 MB. The server fails when it loads an omitted package with no `try`.
|
|
184
187
|
- The build copies each package in `ssr.external`, also when no code imports it. It also copies each package that the app's code gives to `require.resolve()` or `import.meta.resolve()` as a string, such as `require.resolve("@fontsource/inter/files/inter-latin-400-normal.woff")`. When the code makes the name at run time, add the package to `ssr.external`.
|
|
185
188
|
- The build writes a `package.json` with `"type": "module"` into the server build, so Node, Bun, and Deno load it as ES modules and find its `node_modules`.
|
|
186
189
|
|
|
@@ -259,7 +262,8 @@ The function differs from a long-lived process:
|
|
|
259
262
|
|
|
260
263
|
- Vercel starts and stops it. Each plugin's `setup` runs on the first request of each instance. If a `setup` throws, that request fails, and the next request runs each `setup` again. `onShutdown` never runs, and `shutdownSignal()` never aborts.
|
|
261
264
|
- The request scheme comes from `X-Forwarded-Proto`, and the client address comes from `X-Real-IP`, because Vercel sets both headers. Do not set `trustForwardedProto` or `trustedProxyHops`. Behind another proxy, such as the ngrok edge, the client address is the proxy's.
|
|
262
|
-
- The function does not
|
|
265
|
+
- The function does not compress a response, because the CDN compresses each response.
|
|
266
|
+
- The function keeps only the brotli copy of a file that has one, so it holds one copy of each text file, not three. For a blog with 265 prerendered pages, that makes the function about 50 MB smaller. A client that accepts brotli, as each browser does, gets the copy. For any other client, the function decompresses the copy, and the CDN compresses the response.
|
|
263
267
|
- The deploy gives each file of the function one fixed modification time. So the function uses the time of the build for each file, and the ETag of each file it sends changes with each build.
|
|
264
268
|
- `app-server serve` cannot run the build. Use `app-server start` for development, as with the node host.
|
|
265
269
|
|
|
@@ -270,7 +274,7 @@ The function differs from a long-lived process:
|
|
|
270
274
|
1. `health()`: the liveness and readiness probes at `/livez` and `/readyz`. A probe skips the access log. `defaultPlugins({ health })` changes them, and `false` turns them off. See [Health checks](#health-checks).
|
|
271
275
|
2. `requestId()`: reuses a valid incoming `X-Request-Id`, else makes a UUID, and echoes it.
|
|
272
276
|
3. `accessLog({ redactQuery })`: one line per request when the response ends, for the response that leaves the server, with Datadog standard attributes (`http.status_code`, `network.bytes_written`, `duration` in nanoseconds). Query values are `REDACTED` by default. A 5xx line has the level `error`, and each other line has the level `info`. With React Router, a line for a request that React Router answers has the route pattern that it matched as `http.route`, such as `/domains/:domainId`. A prerendered page that the server sends from its file has no `http.route`. In development, a request that Vite answers, such as a source module or `/@vite/client`, gets no line. A request of the `vercel` build's check of the CDN pages gets no line.
|
|
273
|
-
4. `streamingCompress()`: gzip that flushes after each chunk. Chunks that the body has queued together go out as one, up to 16 KiB, so a rendered page costs fewer flushes. When the body ends by the time the
|
|
277
|
+
4. `streamingCompress()`: brotli or gzip that flushes after each chunk. A client gets brotli, at quality 4, when its `Accept-Encoding` ranks `br` at least as high as `gzip`. For HTML, brotli at quality 4 is smaller than gzip, for the same CPU time. For example, a page of 149 KiB in 10 chunks takes 0.60 ms and 7.7 KiB as brotli, and 0.68 ms and 10.6 KiB as gzip, on an Apple M3 Max. The `compression` middleware of Express also uses quality 4. Chunks that the body has queued together go out as one, up to 16 KiB, so a rendered page costs fewer flushes. When the body ends by the time the stream finishes its last chunk, that chunk and the end of the stream go out together. On the node host, a body that ends with its first chunk, up to 64 KiB, uses a zlib stream from a pool, so a small page costs no new zlib stream. Each encoding keeps up to 16 streams between responses. The gzip streams hold about 4.3 MiB in all. A gzip stream takes only such a body, and only one at least as long as each earlier body of that stream, so the gzip bytes of a body depend on that body alone. A reset gives a brotli stream a new encoder, so its bytes also depend on the body alone. On Bun and Deno, each brotli body gets a new stream, because a reset there loses the quality or gives no bytes. It skips encoded, partial, and `no-transform` responses, and a body under 1,024 bytes: by its `Content-Length`, or a body from a string. It adds `Accept-Encoding` to `Vary` only on a compressible type, the same types that get a copy from the build. A static file with a compressed copy from the build gets that copy, and this plugin skips it. It follows the `q` values of `Accept-Encoding`, and `q=0` refuses an encoding. The host gives the streams. On a host with neither, the plugin does nothing.
|
|
274
278
|
5. `securityHeaders()`: safe default security headers on each response, prerendered pages included. `defaultPlugins({ securityHeaders })` changes them, and `false` turns them off. See [Security headers](#security-headers).
|
|
275
279
|
6. `bodyLimit({ maxBytes })`: a 413 for a request body over `defaultPlugins({ bodyLimit })`, 10 mebibytes (MiB) by default. `false` turns it off. A `Content-Length` that is too large gets the 413 before the app runs. A streamed body errors when it passes the limit. When the app reads the body before it answers, the response becomes a 413. When the app streams the body into its response, the response stream errors. A `bodyLimit` that is not a whole number of 0 or more stops startup. The body streams. The plugin does not read it into memory.
|
|
276
280
|
7. The plugins of the named options. They see every response, prerendered pages included:
|
|
@@ -282,7 +286,7 @@ The function differs from a long-lived process:
|
|
|
282
286
|
9. `assets()`: the files of Vite's `build.assetsDir` (`/assets/*` by default) with a one-year immutable cache. The `appServer` Vite plugin records the directory in `.app-server.json`. A missing asset gets a plain 404 with `Cache-Control: no-store`. In development, Vite serves here. See [Deploys and open tabs](#deploys-and-open-tabs).
|
|
283
287
|
10. `redirects(rules)`, when `defaultPlugins({ redirects })` has rules: a redirect from each old URL to its new URL. See [Redirects](#redirects).
|
|
284
288
|
11. `trailingSlash({ mode })`: a 308 from a page URL to the same URL in the site's trailing-slash form. See [Trailing slashes](#trailing-slashes).
|
|
285
|
-
12. `prerendered({ headers, rewrite, skip, cacheControl })`: every file in the client build, with an ETag, a 304 for a matching `If-None-Match`, and a 206 for one byte range of a file that is not HTML. An HTML page, a markdown file, and a data file of the framework get `Cache-Control: public, max-age=0, must-revalidate`, and every other file gets `public, max-age=3600, stale-while-revalidate=86400`. A captured header, the `headers` option, and `cacheControl` change it. A file with a compressed copy from the build gets `Vary: Accept-Encoding`. A client gets the copy that its `Accept-Encoding` prefers, brotli before gzip, with no byte ranges and an ETag for that encoding. A `no-transform` response never gets a copy, and an HTML page gets none when `runtimeEnv` adds values to it. A directory answers with its `index.html`. Each file has one URL: `/a//b`, `/a/index.html`, and `/a%2Fb` go on to the framework. Dotfiles stay private, except `/.well-known/`. A path with no file, or a path that `skip` matches, goes on to the framework.
|
|
289
|
+
12. `prerendered({ headers, rewrite, skip, cacheControl })`: every file in the client build, with its content type, an ETag, a 304 for a matching `If-None-Match`, and a 206 for one byte range of a file that is not HTML. An HTML page, a markdown file, and a data file of the framework get `Cache-Control: public, max-age=0, must-revalidate`, and every other file gets `public, max-age=3600, stale-while-revalidate=86400`. A captured header, the `headers` option, and `cacheControl` change it. A file with a compressed copy from the build gets `Vary: Accept-Encoding`. A client gets the copy that its `Accept-Encoding` prefers, brotli before gzip, with no byte ranges and an ETag for that encoding. A `no-transform` response never gets a copy, and an HTML page gets none when `runtimeEnv` adds values to it. A directory answers with its `index.html`. Each file has one URL: `/a//b`, `/a/index.html`, and `/a%2Fb` go on to the framework. Dotfiles stay private, except `/.well-known/`. A path with no file, or a path that `skip` matches, goes on to the framework. The content type comes from the file's extension. Besides the types that Hono knows, `.sh` gets `application/x-sh`, `.yaml` and `.yml` get `text/yaml`, `.rss` gets `application/rss+xml`, `.atom` gets `application/atom+xml`, and `.pem` gets `application/x-pem-file`, as with `express.static`. An unknown extension gets `application/octet-stream`.
|
|
286
290
|
13. `privateDocuments()`: `Cache-Control: private, no-cache` on each HTML document and each framework data response (such as a React Router `.data` request) from the app's routes or the framework that has no `Cache-Control`. A shared cache then never keeps a page or loader data that the server rendered for one user. A response with its own `Cache-Control` keeps it. A resource route that sends per-user JSON sets its own `Cache-Control`.
|
|
287
291
|
|
|
288
292
|
A route that takes a body larger than the limit needs a higher `bodyLimit` for the whole app, because each request passes the limit before it reaches a route.
|
|
@@ -840,7 +844,7 @@ The values whose names start with `PUBLIC_` go into each HTML document at serve
|
|
|
840
844
|
const publicEnv = JSON.parse(document.getElementById("app-server-public-env")?.textContent ?? "{}");
|
|
841
845
|
```
|
|
842
846
|
|
|
843
|
-
When there are public values, the server sends each prerendered page from its file, not from its compressed copy, because the values change the bytes of the page. `streamingCompress()` then
|
|
847
|
+
When there are public values, the server sends each prerendered page from its file, not from its compressed copy, because the values change the bytes of the page. `streamingCompress()` then compresses the page for each request.
|
|
844
848
|
|
|
845
849
|
## Long-lived responses
|
|
846
850
|
|
|
@@ -894,7 +898,7 @@ A framework adapter or a host adapter is two small objects. A third-party adapte
|
|
|
894
898
|
- `FrameworkBuild` (from `@ngrok/app-server/vite`): the framework's server module, the module of the run side, how to detect the prerender pass, and a check of the Vite config.
|
|
895
899
|
- `FrameworkRuntime` (from `@ngrok/app-server`): `fetch(request, context)`, plus, when the framework has them, its client build, its prerendered paths, its data requests, and the content types that `mime` lacks. The run-side module exports `runtime(serverModule, { mode })`. To give the access log `http.route`, `fetch` sets `routePattern` on the Hono context to the route pattern that the request matched.
|
|
896
900
|
- `HostBuild` (from `@ngrok/app-server/vite`): the module of the run side, the file name of the server entry, and an optional `output` that writes the host's deploy output after the build, such as `.vercel/output`. `output` gets the paths of the build and the URL path of Vite's `build.assetsDir`.
|
|
897
|
-
- `HostRuntime` (from `@ngrok/app-server`): the client build's files, the default listeners (a serverless host has none), request storage for `requestContext()`,
|
|
901
|
+
- `HostRuntime` (from `@ngrok/app-server`): the client build's files, the default listeners (a serverless host has none), request storage for `requestContext()`, gzip and brotli streams, and a lifecycle for a long-lived process. The run-side module exports `host`. `files(...).open(path, range)` must honor `range`, because the static stages send those bytes in a 206. An optional `files(...).snapshot()` lists the client build once. The production server calls it at start, answers each lookup from that list, and keeps each file up to 256 KiB in memory after the first read, up to 32 MiB in all. The `.<file>.gz` and `.<file>.br` copies count in that limit. A client that accepts brotli reads only the `.br` copies, so the copies make the memory that a site needs smaller. If clients read each file in all three forms, a large site can need more than 32 MiB. Then the files that the server reads last stream from disk. Restart the server after you change the client build. A file that you remove while it runs gets a 500, unless it is in memory.
|
|
898
902
|
- `ResponseEdge` (from `@ngrok/app-server`): for a listener that writes each response itself. The listener makes one for each request and gives it as the `edge` binding. The server runs the header hooks on a list of the headers and keeps the list in the edge, so it copies no response for them. The listener writes `edge.headersFor(response)` in place of the headers of the response, adds each body byte to `edge.bytes`, gives an error that stops the response to `edge.fail`, and calls `edge.close(status, aborted, head)` once, with the status and the header list that it sent. The access log takes its line from that report. Only the first request that gets an edge uses it: a request that the app sends through itself again with `c.env` changes its own response. The node host's listener on Node.js and the vercel handler give one. Without it, as on Bun and Deno, the server makes one new response with the headers after the hooks and a body that counts its bytes.
|
|
899
903
|
|
|
900
904
|
## Options
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { r as copyName } from "./compression-CT2mWlKM.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: the size of each file whose only copy in the function is its brotli
|
|
9
|
+
* copy, by the URL path of the file. Why a dotfile: the static stages never
|
|
10
|
+
* serve one.
|
|
11
|
+
*/
|
|
12
|
+
const BROTLI_ONLY_LIST = "/.app-server-brotli.json";
|
|
13
|
+
/** The URL path of the brotli copy of the file at `path`. */
|
|
14
|
+
function brotliCopy(path) {
|
|
15
|
+
const slash = path.lastIndexOf("/");
|
|
16
|
+
return `${path.slice(0, slash + 1)}${copyName(path.slice(slash + 1), "br")}`;
|
|
17
|
+
}
|
|
18
|
+
/** The bytes of `range` in `stream`. It cancels `stream` after the last byte. */
|
|
19
|
+
function sliced(stream, { start, end }) {
|
|
20
|
+
let position = 0;
|
|
21
|
+
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
22
|
+
const from = Math.max(start - position, 0);
|
|
23
|
+
const to = Math.min(end + 1 - position, chunk.byteLength);
|
|
24
|
+
position += chunk.byteLength;
|
|
25
|
+
if (to > from) controller.enqueue(chunk.subarray(from, to));
|
|
26
|
+
if (position > end) controller.terminate();
|
|
27
|
+
} }));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The bytes of the stream that `open` gives, with `open` called on the first
|
|
31
|
+
* read. Why: a pipe reads its source at once, and a stage can drop a response
|
|
32
|
+
* that no one reads.
|
|
33
|
+
*/
|
|
34
|
+
function decodedOnRead(open) {
|
|
35
|
+
let reader;
|
|
36
|
+
return new ReadableStream({
|
|
37
|
+
async pull(controller) {
|
|
38
|
+
reader ??= open().then((stream) => stream.getReader());
|
|
39
|
+
const { done, value } = await (await reader).read();
|
|
40
|
+
if (done) controller.close();
|
|
41
|
+
else controller.enqueue(value);
|
|
42
|
+
},
|
|
43
|
+
async cancel(reason) {
|
|
44
|
+
await (await reader)?.cancel(reason);
|
|
45
|
+
}
|
|
46
|
+
}, { highWaterMark: 0 });
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* `files` that also find each file in {@link BROTLI_ONLY_LIST}, with the
|
|
50
|
+
* size from the list and the time of its brotli copy. Each read of such a
|
|
51
|
+
* file decompresses its brotli copy. Why: a function then keeps one copy of
|
|
52
|
+
* the file, not three, and a client that accepts brotli, as each browser
|
|
53
|
+
* does, gets the copy with no work.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* brotliOnlyFiles(nodeFiles("build/client")).open("/blog/index.html");
|
|
57
|
+
*/
|
|
58
|
+
function brotliOnlyFiles(files) {
|
|
59
|
+
let sizes;
|
|
60
|
+
const readSizes = async () => {
|
|
61
|
+
sizes ??= files.stat(BROTLI_ONLY_LIST).then(async (info) => {
|
|
62
|
+
if (info?.kind !== "file") return /* @__PURE__ */ new Map();
|
|
63
|
+
const text = await new Response(await files.open(BROTLI_ONLY_LIST)).text();
|
|
64
|
+
return new Map(Object.entries(JSON.parse(text)));
|
|
65
|
+
});
|
|
66
|
+
return sizes;
|
|
67
|
+
};
|
|
68
|
+
const wrap = (inner) => {
|
|
69
|
+
const { snapshot } = inner;
|
|
70
|
+
return {
|
|
71
|
+
stat: async (path) => {
|
|
72
|
+
const info = await inner.stat(path);
|
|
73
|
+
const size = info == null ? (await readSizes()).get(path) : void 0;
|
|
74
|
+
if (size == null) return info;
|
|
75
|
+
const copy = await inner.stat(brotliCopy(path));
|
|
76
|
+
return copy?.kind === "file" ? {
|
|
77
|
+
kind: "file",
|
|
78
|
+
size,
|
|
79
|
+
mtimeMs: copy.mtimeMs
|
|
80
|
+
} : void 0;
|
|
81
|
+
},
|
|
82
|
+
open: async (path, range) => {
|
|
83
|
+
if (!(await readSizes()).has(path)) return inner.open(path, range);
|
|
84
|
+
return decodedOnRead(async () => {
|
|
85
|
+
const decompress = Duplex.toWeb(createBrotliDecompress());
|
|
86
|
+
const decoded = (await inner.open(brotliCopy(path))).pipeThrough(decompress);
|
|
87
|
+
return range == null ? decoded : sliced(decoded, range);
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
...snapshot == null ? {} : { snapshot: async () => wrap(await snapshot()) }
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
return wrap(files);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { brotliOnlyFiles as n, BROTLI_ONLY_LIST as t };
|
|
97
|
+
//# sourceMappingURL=brotli-only-DrA29Ecw.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"brotli-only-DrA29Ecw.js","names":[],"sources":["../src/vercel/brotli-only.ts"],"sourcesContent":["import { Duplex } from \"node:stream\";\nimport { createBrotliDecompress } from \"node:zlib\";\nimport { copyName } from \"../core/compression.js\";\nimport type { ByteRange, StaticFiles } from \"../core/types.js\";\n\n/**\n * The URL path of the list that `vercel()` writes in the function's client\n * build: the size of each file whose only copy in the function is its brotli\n * copy, by the URL path of the file. Why a dotfile: the static stages never\n * serve one.\n */\nexport const BROTLI_ONLY_LIST = \"/.app-server-brotli.json\";\n\n/** The URL path of the brotli copy of the file at `path`. */\nfunction brotliCopy(path: string): string {\n\tconst slash = path.lastIndexOf(\"/\");\n\treturn `${path.slice(0, slash + 1)}${copyName(path.slice(slash + 1), \"br\")}`;\n}\n\n/** The bytes of `range` in `stream`. It cancels `stream` after the last byte. */\nfunction sliced(stream: ReadableStream<Uint8Array>, { start, end }: ByteRange) {\n\tlet position = 0;\n\treturn stream.pipeThrough(\n\t\tnew TransformStream<Uint8Array, Uint8Array>({\n\t\t\ttransform(chunk, controller) {\n\t\t\t\tconst from = Math.max(start - position, 0);\n\t\t\t\tconst to = Math.min(end + 1 - position, chunk.byteLength);\n\t\t\t\tposition += chunk.byteLength;\n\t\t\t\tif (to > from) {\n\t\t\t\t\tcontroller.enqueue(chunk.subarray(from, to));\n\t\t\t\t}\n\t\t\t\tif (position > end) {\n\t\t\t\t\tcontroller.terminate();\n\t\t\t\t}\n\t\t\t},\n\t\t}),\n\t);\n}\n\n/**\n * The bytes of the stream that `open` gives, with `open` called on the first\n * read. Why: a pipe reads its source at once, and a stage can drop a response\n * that no one reads.\n */\nfunction decodedOnRead(\n\topen: () => Promise<ReadableStream<Uint8Array>>,\n): ReadableStream<Uint8Array> {\n\tlet reader: Promise<ReadableStreamDefaultReader<Uint8Array>> | undefined;\n\treturn new ReadableStream<Uint8Array>(\n\t\t{\n\t\t\tasync pull(controller) {\n\t\t\t\treader ??= open().then((stream) => stream.getReader());\n\t\t\t\tconst { done, value } = await (await reader).read();\n\t\t\t\tif (done) {\n\t\t\t\t\tcontroller.close();\n\t\t\t\t} else {\n\t\t\t\t\tcontroller.enqueue(value);\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait (await reader)?.cancel(reason);\n\t\t\t},\n\t\t},\n\t\t{ highWaterMark: 0 },\n\t);\n}\n\n/**\n * `files` that also find each file in {@link BROTLI_ONLY_LIST}, with the\n * size from the list and the time of its brotli copy. Each read of such a\n * file decompresses its brotli copy. Why: a function then keeps one copy of\n * the file, not three, and a client that accepts brotli, as each browser\n * does, gets the copy with no work.\n *\n * @example\n * brotliOnlyFiles(nodeFiles(\"build/client\")).open(\"/blog/index.html\");\n */\nexport function brotliOnlyFiles(files: StaticFiles): StaticFiles {\n\tlet sizes: Promise<ReadonlyMap<string, number>> | undefined;\n\t// Why once and late: the list does not change while the function runs, and a file found on\n\t// disk does not need it.\n\tconst readSizes = async () => {\n\t\tsizes ??= files.stat(BROTLI_ONLY_LIST).then(async (info) => {\n\t\t\tif (info?.kind !== \"file\") {\n\t\t\t\treturn new Map<string, number>();\n\t\t\t}\n\t\t\tconst text = await new Response(await files.open(BROTLI_ONLY_LIST)).text();\n\t\t\t// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- `vercel()` of this package wrote the file.\n\t\t\treturn new Map(Object.entries(JSON.parse(text) as Record<string, number>));\n\t\t});\n\t\treturn sizes;\n\t};\n\tconst wrap = (inner: StaticFiles): StaticFiles => {\n\t\tconst { snapshot } = inner;\n\t\treturn {\n\t\t\tstat: async (path) => {\n\t\t\t\tconst info = await inner.stat(path);\n\t\t\t\tconst size = info == null ? (await readSizes()).get(path) : undefined;\n\t\t\t\tif (size == null) {\n\t\t\t\t\treturn info;\n\t\t\t\t}\n\t\t\t\tconst copy = await inner.stat(brotliCopy(path));\n\t\t\t\treturn copy?.kind === \"file\" ? { kind: \"file\", size, mtimeMs: copy.mtimeMs } : undefined;\n\t\t\t},\n\t\t\topen: async (path, range) => {\n\t\t\t\tif (!(await readSizes()).has(path)) {\n\t\t\t\t\treturn inner.open(path, range);\n\t\t\t\t}\n\t\t\t\treturn decodedOnRead(async () => {\n\t\t\t\t\t// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Node.js types its web streams apart from the global ones.\n\t\t\t\t\tconst decompress = Duplex.toWeb(createBrotliDecompress()) as ReadableWritablePair<\n\t\t\t\t\t\tUint8Array,\n\t\t\t\t\t\tUint8Array\n\t\t\t\t\t>;\n\t\t\t\t\tconst decoded = (await inner.open(brotliCopy(path))).pipeThrough(decompress);\n\t\t\t\t\treturn range == null ? decoded : sliced(decoded, range);\n\t\t\t\t});\n\t\t\t},\n\t\t\t...(snapshot == null ? {} : { snapshot: async () => wrap(await snapshot()) }),\n\t\t};\n\t};\n\treturn wrap(files);\n}\n"],"mappings":";;;;;;;;;;;AAWA,MAAa,mBAAmB;;AAGhC,SAAS,WAAW,MAAsB;CACzC,MAAM,QAAQ,KAAK,YAAY,GAAG;CAClC,OAAO,GAAG,KAAK,MAAM,GAAG,QAAQ,CAAC,IAAI,SAAS,KAAK,MAAM,QAAQ,CAAC,GAAG,IAAI;AAC1E;;AAGA,SAAS,OAAO,QAAoC,EAAE,OAAO,OAAkB;CAC9E,IAAI,WAAW;CACf,OAAO,OAAO,YACb,IAAI,gBAAwC,EAC3C,UAAU,OAAO,YAAY;EAC5B,MAAM,OAAO,KAAK,IAAI,QAAQ,UAAU,CAAC;EACzC,MAAM,KAAK,KAAK,IAAI,MAAM,IAAI,UAAU,MAAM,UAAU;EACxD,YAAY,MAAM;EAClB,IAAI,KAAK,MACR,WAAW,QAAQ,MAAM,SAAS,MAAM,EAAE,CAAC;EAE5C,IAAI,WAAW,KACd,WAAW,UAAU;CAEvB,EACD,CAAC,CACF;AACD;;;;;;AAOA,SAAS,cACR,MAC6B;CAC7B,IAAI;CACJ,OAAO,IAAI,eACV;EACC,MAAM,KAAK,YAAY;GACtB,WAAW,KAAK,CAAC,CAAC,MAAM,WAAW,OAAO,UAAU,CAAC;GACrD,MAAM,EAAE,MAAM,UAAU,OAAO,MAAM,OAAM,CAAE,KAAK;GAClD,IAAI,MACH,WAAW,MAAM;QAEjB,WAAW,QAAQ,KAAK;EAE1B;EACA,MAAM,OAAO,QAAQ;GACpB,OAAO,MAAM,OAAM,EAAG,OAAO,MAAM;EACpC;CACD,GACA,EAAE,eAAe,EAAE,CACpB;AACD;;;;;;;;;;;AAYA,SAAgB,gBAAgB,OAAiC;CAChE,IAAI;CAGJ,MAAM,YAAY,YAAY;EAC7B,UAAU,MAAM,KAAK,gBAAgB,CAAC,CAAC,KAAK,OAAO,SAAS;GAC3D,IAAI,MAAM,SAAS,QAClB,uBAAO,IAAI,IAAoB;GAEhC,MAAM,OAAO,MAAM,IAAI,SAAS,MAAM,MAAM,KAAK,gBAAgB,CAAC,CAAC,CAAC,KAAK;GAEzE,OAAO,IAAI,IAAI,OAAO,QAAQ,KAAK,MAAM,IAAI,CAA2B,CAAC;EAC1E,CAAC;EACD,OAAO;CACR;CACA,MAAM,QAAQ,UAAoC;EACjD,MAAM,EAAE,aAAa;EACrB,OAAO;GACN,MAAM,OAAO,SAAS;IACrB,MAAM,OAAO,MAAM,MAAM,KAAK,IAAI;IAClC,MAAM,OAAO,QAAQ,QAAQ,MAAM,UAAU,EAAC,CAAE,IAAI,IAAI,IAAI;IAC5D,IAAI,QAAQ,MACX,OAAO;IAER,MAAM,OAAO,MAAM,MAAM,KAAK,WAAW,IAAI,CAAC;IAC9C,OAAO,MAAM,SAAS,SAAS;KAAE,MAAM;KAAQ;KAAM,SAAS,KAAK;IAAQ,IAAI;GAChF;GACA,MAAM,OAAO,MAAM,UAAU;IAC5B,IAAI,EAAE,MAAM,UAAU,EAAC,CAAE,IAAI,IAAI,GAChC,OAAO,MAAM,KAAK,MAAM,KAAK;IAE9B,OAAO,cAAc,YAAY;KAEhC,MAAM,aAAa,OAAO,MAAM,uBAAuB,CAAC;KAIxD,MAAM,WAAW,MAAM,MAAM,KAAK,WAAW,IAAI,CAAC,EAAC,CAAE,YAAY,UAAU;KAC3E,OAAO,SAAS,OAAO,UAAU,OAAO,SAAS,KAAK;IACvD,CAAC;GACF;GACA,GAAI,YAAY,OAAO,CAAC,IAAI,EAAE,UAAU,YAAY,KAAK,MAAM,SAAS,CAAC,EAAE;EAC5E;CACD;CACA,OAAO,KAAK,KAAK;AAClB"}
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as parsePort } from "./tcp-
|
|
2
|
+
import { t as parsePort } from "./tcp-Bws6oEau.js";
|
|
3
3
|
import process from "node:process";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
|
|
@@ -42,12 +42,12 @@ try {
|
|
|
42
42
|
if (args?.help === true) process.stdout.write(`${USAGE}\n`);
|
|
43
43
|
else if (args?.command === "start") {
|
|
44
44
|
process.env.NODE_ENV ??= "development";
|
|
45
|
-
const { start } = await import("./start-
|
|
45
|
+
const { start } = await import("./start-DiQf9ttp.js");
|
|
46
46
|
await start();
|
|
47
47
|
} else if (args?.command === "serve") {
|
|
48
48
|
process.env.NODE_ENV ??= "production";
|
|
49
49
|
if (typeof process.setSourceMapsEnabled === "function") process.setSourceMapsEnabled(true);
|
|
50
|
-
const { serve } = await import("./serve-
|
|
50
|
+
const { serve } = await import("./serve-BUJ7ZonG.js");
|
|
51
51
|
await serve({ buildPath: args.buildPath });
|
|
52
52
|
} else if (args != null) fail(args.command == null ? "Give a command." : `Unknown command: ${args.command}.`);
|
|
53
53
|
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { brotliCompressSync, constants, crc32, createBrotliCompress, createDeflateRaw, createGzip } from "node:zlib";
|
|
2
|
+
|
|
3
|
+
// src/node/compress.ts
|
|
4
|
+
/**
|
|
5
|
+
* A stream from {@link gzipStream} or {@link brotliStream}. Why a class: the
|
|
6
|
+
* node response writer reads `busy` to hold the head for zlib's first chunk,
|
|
7
|
+
* and a field costs less than a `WeakMap` entry for each response.
|
|
8
|
+
*/
|
|
9
|
+
var ZlibBody = class extends ReadableStream {
|
|
10
|
+
/** Whether the next chunk waits only on zlib, because the source gave the bytes already. */
|
|
11
|
+
busy = false;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Whether the next chunk of `stream` waits only on zlib, because its source
|
|
15
|
+
* gave the bytes already. It gives `undefined` for a stream that is not from
|
|
16
|
+
* {@link gzipStream} or {@link brotliStream}.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* waitsForZlib(gzipStream(source)); // => false, until the first read gives zlib a chunk
|
|
20
|
+
*/
|
|
21
|
+
function waitsForZlib(stream) {
|
|
22
|
+
return stream instanceof ZlibBody ? stream.busy : void 0;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The gzip header (RFC 1952): the magic number, deflate, no flags, no time,
|
|
26
|
+
* and the Unix system code.
|
|
27
|
+
*/
|
|
28
|
+
const HEADER = Buffer.from([
|
|
29
|
+
31,
|
|
30
|
+
139,
|
|
31
|
+
8,
|
|
32
|
+
0,
|
|
33
|
+
0,
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
0,
|
|
38
|
+
3
|
|
39
|
+
]);
|
|
40
|
+
/** The gzip member of an empty body: the header, a final empty block, a CRC-32 of 0, and a size of 0. */
|
|
41
|
+
const EMPTY_MEMBER = Buffer.concat([HEADER, Buffer.from([
|
|
42
|
+
3,
|
|
43
|
+
0,
|
|
44
|
+
0,
|
|
45
|
+
0,
|
|
46
|
+
0,
|
|
47
|
+
0,
|
|
48
|
+
0,
|
|
49
|
+
0,
|
|
50
|
+
0,
|
|
51
|
+
0
|
|
52
|
+
])]);
|
|
53
|
+
/**
|
|
54
|
+
* The largest body that a pooled gzip or brotli stream compresses. Why a
|
|
55
|
+
* limit: above about 64 KiB, the CRC-32 of a gzip body on the main thread
|
|
56
|
+
* costs more than a new zlib stream does.
|
|
57
|
+
*/
|
|
58
|
+
const POOLED_MAX_BYTES = 65536;
|
|
59
|
+
/** The most zlib streams that wait for a body in each pool. Why a limit: an idle deflate stream keeps about 276 KiB of zlib state. */
|
|
60
|
+
const MAX_IDLE = 16;
|
|
61
|
+
function ignore() {}
|
|
62
|
+
function newPooled(pool) {
|
|
63
|
+
const pooled = {
|
|
64
|
+
stream: pool.create(),
|
|
65
|
+
output: ignore,
|
|
66
|
+
fail: ignore,
|
|
67
|
+
size: 0
|
|
68
|
+
};
|
|
69
|
+
pooled.stream.on("data", (chunk) => {
|
|
70
|
+
pooled.output(chunk);
|
|
71
|
+
});
|
|
72
|
+
pooled.stream.on("error", (error) => {
|
|
73
|
+
pooled.fail(error);
|
|
74
|
+
});
|
|
75
|
+
pooled.stream.on("close", () => {
|
|
76
|
+
if (pooled.fail !== ignore) pooled.fail(/* @__PURE__ */ new Error("The zlib stream closed."));
|
|
77
|
+
});
|
|
78
|
+
return pooled;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The deflate pool. Why `Z_FINISH`: when the output fills the buffer of the
|
|
82
|
+
* stream exactly, the stream calls zlib again, and a sync flush then adds an
|
|
83
|
+
* empty block. So the output would depend on earlier bodies.
|
|
84
|
+
*/
|
|
85
|
+
const DEFLATE_POOL = {
|
|
86
|
+
create: () => createDeflateRaw({ flush: constants.Z_FINISH }),
|
|
87
|
+
idle: [],
|
|
88
|
+
sized: true,
|
|
89
|
+
reuses: true
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* The brotli quality of a response. Why 4: for HTML, its output is smaller
|
|
93
|
+
* than gzip level 6, for the same CPU time. The `compression` middleware of
|
|
94
|
+
* Express also uses 4.
|
|
95
|
+
*/
|
|
96
|
+
const BROTLI_QUALITY = 4;
|
|
97
|
+
const BROTLI_PARAMS = {
|
|
98
|
+
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,
|
|
99
|
+
[constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The brotli pool. Why not sized: a reset makes a new brotli encoder with the
|
|
103
|
+
* same parameters, so the output of a body never depends on an earlier one.
|
|
104
|
+
*/
|
|
105
|
+
const BROTLI_POOL = {
|
|
106
|
+
create: () => createBrotliCompress({
|
|
107
|
+
flush: constants.BROTLI_OPERATION_FINISH,
|
|
108
|
+
params: BROTLI_PARAMS
|
|
109
|
+
}),
|
|
110
|
+
idle: [],
|
|
111
|
+
sized: false,
|
|
112
|
+
reuses: !("Bun" in globalThis) && !("Deno" in globalThis)
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* An idle stream of `pool` for a body of `size` bytes, else a new one. It
|
|
116
|
+
* takes a stream whose bodies were each no longer than this one: the longest
|
|
117
|
+
* such, and of those the last one given back, whose memory is still in the
|
|
118
|
+
* cache.
|
|
119
|
+
*
|
|
120
|
+
* Why for deflate: a reset keeps the window of zlib, and zlib reads the bytes
|
|
121
|
+
* after the end of a body to hash and match its last bytes. After a longer
|
|
122
|
+
* body, these are bytes of that body, so they could change the output of this
|
|
123
|
+
* one. A stream whose bodies were no longer has only the zeros that zlib
|
|
124
|
+
* writes after each body there, as a new stream does.
|
|
125
|
+
*/
|
|
126
|
+
function take(pool, size) {
|
|
127
|
+
const { idle } = pool;
|
|
128
|
+
let best = -1;
|
|
129
|
+
let bestSize = -1;
|
|
130
|
+
for (let index = idle.length - 1; index >= 0; index -= 1) {
|
|
131
|
+
const each = idle[index]?.size ?? Infinity;
|
|
132
|
+
if (each <= size && each > bestSize) {
|
|
133
|
+
best = index;
|
|
134
|
+
bestSize = each;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (best === -1) return newPooled(pool);
|
|
138
|
+
const taken = idle[best];
|
|
139
|
+
const last = idle.pop();
|
|
140
|
+
if (taken == null || last == null) return newPooled(pool);
|
|
141
|
+
if (last !== taken) idle[best] = last;
|
|
142
|
+
return taken;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Gives back a stream of `pool` after its body. Why reset: the next body must
|
|
146
|
+
* not use the bytes of this one as a dictionary. When the pool is full, it
|
|
147
|
+
* keeps the streams of the shortest bodies, which more bodies can take.
|
|
148
|
+
*/
|
|
149
|
+
function release(pool, pooled) {
|
|
150
|
+
pooled.output = ignore;
|
|
151
|
+
pooled.fail = ignore;
|
|
152
|
+
const { stream } = pooled;
|
|
153
|
+
if (!pool.reuses || stream.destroyed || stream.readableLength !== 0 || stream.writableLength !== 0) {
|
|
154
|
+
stream.destroy();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
stream.reset();
|
|
158
|
+
if (stream.destroyed) return;
|
|
159
|
+
const { idle } = pool;
|
|
160
|
+
if (idle.length < MAX_IDLE) {
|
|
161
|
+
idle.push(pooled);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
let longest = 0;
|
|
165
|
+
for (let index = 1; index < idle.length; index += 1) if ((idle[index]?.size ?? 0) > (idle[longest]?.size ?? 0)) longest = index;
|
|
166
|
+
const evicted = idle[longest];
|
|
167
|
+
if (evicted != null && evicted.size > pooled.size) {
|
|
168
|
+
idle[longest] = pooled;
|
|
169
|
+
evicted.stream.destroy();
|
|
170
|
+
} else stream.destroy();
|
|
171
|
+
}
|
|
172
|
+
/** Writes `chunk` to `stream`. zlib gives all the output of the chunk before this resolves. */
|
|
173
|
+
async function write(stream, chunk) {
|
|
174
|
+
return new Promise((resolve, reject) => {
|
|
175
|
+
stream.write(chunk, (error) => {
|
|
176
|
+
if (error == null) resolve();
|
|
177
|
+
else reject(error);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The output of a stream of `pool` for a body of one chunk, in one zlib call.
|
|
183
|
+
* `use` gets the stream while zlib compresses, so a cancel can destroy it,
|
|
184
|
+
* and gets `undefined` before the stream goes back to the pool. Why only a
|
|
185
|
+
* body of one chunk: a deflate reset keeps the old window, and a second write
|
|
186
|
+
* reads its bytes as history. The output of one write is the same as from a
|
|
187
|
+
* new stream, so it does not depend on an earlier body.
|
|
188
|
+
*/
|
|
189
|
+
async function compressOnce(pool, value, use) {
|
|
190
|
+
const pooled = take(pool, value.byteLength);
|
|
191
|
+
use(pooled);
|
|
192
|
+
const parts = [];
|
|
193
|
+
pooled.output = (chunk) => {
|
|
194
|
+
parts.push(chunk);
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
await new Promise((resolve, reject) => {
|
|
198
|
+
pooled.fail = reject;
|
|
199
|
+
pooled.stream.write(value, (error) => {
|
|
200
|
+
if (error != null) reject(error);
|
|
201
|
+
else if (pooled.stream.destroyed) reject(/* @__PURE__ */ new Error("The zlib stream closed."));
|
|
202
|
+
else resolve();
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
pooled.output = ignore;
|
|
207
|
+
pooled.fail = ignore;
|
|
208
|
+
pooled.stream.destroy();
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
if (pool.sized) pooled.size = Math.max(pooled.size, value.byteLength);
|
|
212
|
+
use(void 0);
|
|
213
|
+
release(pool, pooled);
|
|
214
|
+
return parts;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The gzip member of a body of one chunk: the raw deflate output of a pooled
|
|
218
|
+
* stream, with the header and the trailer written here.
|
|
219
|
+
*/
|
|
220
|
+
async function gzipOnce(value, use) {
|
|
221
|
+
const parts = await compressOnce(DEFLATE_POOL, value, use);
|
|
222
|
+
const trailer = Buffer.allocUnsafe(8);
|
|
223
|
+
trailer.writeUInt32LE(crc32(value), 0);
|
|
224
|
+
trailer.writeUInt32LE(value.byteLength, 4);
|
|
225
|
+
return Buffer.concat([
|
|
226
|
+
HEADER,
|
|
227
|
+
...parts,
|
|
228
|
+
trailer
|
|
229
|
+
]);
|
|
230
|
+
}
|
|
231
|
+
/** The brotli stream of a body of one chunk, from a pooled encoder. */
|
|
232
|
+
async function brotliOnce(value, use) {
|
|
233
|
+
return Buffer.concat(await compressOnce(BROTLI_POOL, value, use));
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Compresses a byte stream with `codec`. Each written chunk is readable at
|
|
237
|
+
* once. It reads the next source chunk only when the consumer pulls, so a
|
|
238
|
+
* slow client holds back the source. When the source ends by the time zlib
|
|
239
|
+
* finishes a chunk, that chunk and the end of the stream go out as one. Why:
|
|
240
|
+
* the host writes each chunk to the socket on its own, and a write costs more
|
|
241
|
+
* than its few bytes.
|
|
242
|
+
*/
|
|
243
|
+
function zlibStream(source, codec) {
|
|
244
|
+
const reader = source.getReader();
|
|
245
|
+
let ended = false;
|
|
246
|
+
const markEnded = () => {
|
|
247
|
+
ended = true;
|
|
248
|
+
};
|
|
249
|
+
reader.closed.then(markEnded, () => {});
|
|
250
|
+
/** The zlib stream of a body of more than one chunk. */
|
|
251
|
+
let zlib;
|
|
252
|
+
/** The pooled zlib stream while it compresses the one chunk of a body. */
|
|
253
|
+
let pooled;
|
|
254
|
+
let started = false;
|
|
255
|
+
/** The zlib output that waits for the next enqueue. */
|
|
256
|
+
let held = [];
|
|
257
|
+
let heldBytes = 0;
|
|
258
|
+
const enqueueHeld = (controller) => {
|
|
259
|
+
const [first] = held;
|
|
260
|
+
if (first == null) return;
|
|
261
|
+
controller.enqueue(held.length === 1 ? first : Buffer.concat(held));
|
|
262
|
+
held = [];
|
|
263
|
+
heldBytes = 0;
|
|
264
|
+
};
|
|
265
|
+
const startZlib = (body, controller) => {
|
|
266
|
+
const created = codec.create();
|
|
267
|
+
created.on("data", (chunk) => {
|
|
268
|
+
held.push(chunk);
|
|
269
|
+
heldBytes += chunk.byteLength;
|
|
270
|
+
if (heldBytes >= constants.Z_DEFAULT_CHUNK) enqueueHeld(controller);
|
|
271
|
+
});
|
|
272
|
+
created.on("end", () => {
|
|
273
|
+
body.busy = false;
|
|
274
|
+
enqueueHeld(controller);
|
|
275
|
+
controller.close();
|
|
276
|
+
});
|
|
277
|
+
created.on("error", (error) => {
|
|
278
|
+
body.busy = false;
|
|
279
|
+
controller.error(error);
|
|
280
|
+
});
|
|
281
|
+
return created;
|
|
282
|
+
};
|
|
283
|
+
const stream = new ZlibBody({
|
|
284
|
+
async pull(controller) {
|
|
285
|
+
try {
|
|
286
|
+
let chunk = await reader.read();
|
|
287
|
+
while (!chunk.done && chunk.value.byteLength === 0) chunk = await reader.read();
|
|
288
|
+
if (!started) {
|
|
289
|
+
started = true;
|
|
290
|
+
if (chunk.done) {
|
|
291
|
+
controller.enqueue(codec.empty);
|
|
292
|
+
controller.close();
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
stream.busy = true;
|
|
296
|
+
await Promise.resolve();
|
|
297
|
+
if (ended && chunk.value.byteLength <= POOLED_MAX_BYTES) {
|
|
298
|
+
const output = await codec.once(chunk.value, (each) => {
|
|
299
|
+
pooled = each;
|
|
300
|
+
});
|
|
301
|
+
stream.busy = false;
|
|
302
|
+
controller.enqueue(output);
|
|
303
|
+
controller.close();
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
zlib ??= startZlib(stream, controller);
|
|
308
|
+
stream.busy = true;
|
|
309
|
+
if (chunk.done) {
|
|
310
|
+
zlib.end();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
await write(zlib, chunk.value);
|
|
314
|
+
if (ended) {
|
|
315
|
+
zlib.end();
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
stream.busy = false;
|
|
319
|
+
enqueueHeld(controller);
|
|
320
|
+
} catch (error) {
|
|
321
|
+
stream.busy = false;
|
|
322
|
+
zlib?.destroy();
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
async cancel(reason) {
|
|
327
|
+
zlib?.destroy();
|
|
328
|
+
pooled?.stream.destroy();
|
|
329
|
+
return reader.cancel(reason);
|
|
330
|
+
}
|
|
331
|
+
}, { highWaterMark: 0 });
|
|
332
|
+
return stream;
|
|
333
|
+
}
|
|
334
|
+
const GZIP = {
|
|
335
|
+
create: () => createGzip({ flush: constants.Z_SYNC_FLUSH }),
|
|
336
|
+
empty: EMPTY_MEMBER,
|
|
337
|
+
once: gzipOnce
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* Gzips a byte stream. `Z_SYNC_FLUSH` makes each written chunk readable at
|
|
341
|
+
* once. It reads the next source chunk only when the consumer pulls, so a slow
|
|
342
|
+
* client holds back the source. When the source ends by the time zlib
|
|
343
|
+
* finishes a chunk, that chunk and the gzip trailer go out as one.
|
|
344
|
+
*
|
|
345
|
+
* A body that ends with its first chunk, up to 64 KiB, takes a pooled raw
|
|
346
|
+
* deflate stream. Why: a new zlib stream costs more main-thread time than
|
|
347
|
+
* zlib takes for a small page.
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* new Response(gzipStream(response.body), response);
|
|
351
|
+
*/
|
|
352
|
+
function gzipStream(source) {
|
|
353
|
+
return zlibStream(source, GZIP);
|
|
354
|
+
}
|
|
355
|
+
const BROTLI = {
|
|
356
|
+
create: () => createBrotliCompress({
|
|
357
|
+
flush: constants.BROTLI_OPERATION_FLUSH,
|
|
358
|
+
params: BROTLI_PARAMS
|
|
359
|
+
}),
|
|
360
|
+
empty: brotliCompressSync(/* @__PURE__ */ new Uint8Array(), { params: BROTLI_PARAMS }),
|
|
361
|
+
once: brotliOnce
|
|
362
|
+
};
|
|
363
|
+
/**
|
|
364
|
+
* Compresses a byte stream with brotli, quality 4. `BROTLI_OPERATION_FLUSH`
|
|
365
|
+
* makes each written chunk readable at once. It reads and ends as
|
|
366
|
+
* {@link gzipStream} does, and a body that ends with its first chunk, up to
|
|
367
|
+
* 64 KiB, also takes a pooled encoder.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* new Response(brotliStream(response.body), response);
|
|
371
|
+
*/
|
|
372
|
+
function brotliStream(source) {
|
|
373
|
+
return zlibStream(source, BROTLI);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export { gzipStream as n, waitsForZlib as r, brotliStream as t };
|
|
377
|
+
//# sourceMappingURL=compress-JuJf2HaO.js.map
|