@ngrok/app-server 0.3.4 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +3 -3
  3. package/dist/{compress-JuJf2HaO.js → compress-Cp5NfXCC.js} +141 -23
  4. package/dist/compress-Cp5NfXCC.js.map +1 -0
  5. package/dist/{create-server-BX2rH6-E.js → create-server-C_90R50y.js} +11 -9
  6. package/dist/{create-server-BX2rH6-E.js.map → create-server-C_90R50y.js.map} +1 -1
  7. package/dist/{entry-ChHgs269.js → entry-65IESBSA.js} +3 -3
  8. package/dist/{entry-ChHgs269.js.map → entry-65IESBSA.js.map} +1 -1
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +4 -4
  11. package/dist/{load-entry-BOgJPt1I.js → load-entry-BftQ3qlz.js} +4 -4
  12. package/dist/{load-entry-BOgJPt1I.js.map → load-entry-BftQ3qlz.js.map} +1 -1
  13. package/dist/{log-BP6xXXZl.js → log-C8LGQ23s.js} +29 -2
  14. package/dist/log-C8LGQ23s.js.map +1 -0
  15. package/dist/{node-B0AYf_IA.js → node-CA7t-s6D.js} +4 -4
  16. package/dist/{node-B0AYf_IA.js.map → node-CA7t-s6D.js.map} +1 -1
  17. package/dist/{node-C0Ad7CVL.js → node-Fo6EodEV.js} +2 -2
  18. package/dist/{node-C0Ad7CVL.js.map → node-Fo6EodEV.js.map} +1 -1
  19. package/dist/node.js +2 -2
  20. package/dist/react-router.js +1 -1
  21. package/dist/{response-edge-Bc1-C_Cx.js → response-edge-D3CAjukJ.js} +2 -2
  22. package/dist/{response-edge-Bc1-C_Cx.js.map → response-edge-D3CAjukJ.js.map} +1 -1
  23. package/dist/{response-writer-DzvUAz9x.js → response-writer-BUyOW4n2.js} +3 -3
  24. package/dist/{response-writer-DzvUAz9x.js.map → response-writer-BUyOW4n2.js.map} +1 -1
  25. package/dist/{serve-BUJ7ZonG.js → serve-CEecObwD.js} +2 -2
  26. package/dist/{serve-BUJ7ZonG.js.map → serve-CEecObwD.js.map} +1 -1
  27. package/dist/{start-DiQf9ttp.js → start-CDnHKNca.js} +63 -8
  28. package/dist/start-CDnHKNca.js.map +1 -0
  29. package/dist/{tcp-Bws6oEau.js → tcp-BPeUZaNg.js} +2 -2
  30. package/dist/{tcp-Bws6oEau.js.map → tcp-BPeUZaNg.js.map} +1 -1
  31. package/dist/testing.js +3 -3
  32. package/dist/vercel.js +4 -4
  33. package/package.json +1 -1
  34. package/dist/compress-JuJf2HaO.js.map +0 -1
  35. package/dist/log-BP6xXXZl.js.map +0 -1
  36. package/dist/start-DiQf9ttp.js.map +0 -1
package/README.md CHANGED
@@ -167,7 +167,7 @@ A React Router app with no Vite plugin also works: `app-server serve build/serve
167
167
 
168
168
  ## Commands
169
169
 
170
- - `app-server start`: development. Vite in middleware mode inside the pipeline, with HMR on the same port. It needs the `appServer` plugin. Each request loads the latest server entry, so an edit to a route or to `server.ts` applies at once. A Vite plugin gets the CLI's HTTP server as `server.httpServer` in its `configureServer` hook, so it can wait for `listening` and `close`. A Vite restart, such as after an edit to `vite.config.ts`, keeps that server open. So a new plugin instance gets no `listening`, and the old instance gets `close` only at exit. To clean up for each instance, use Vite's `closeServer` hook. The port is `--port`, else `PORT`, else Vite's `server.port`. When that port is in use, `start` tries each next port, unless Vite's `server.strictPort` is true. The host is `HOST`, else Vite's `server.host`. On `SIGTERM` or `SIGINT`, it stops Vite and the server, then exits.
170
+ - `app-server start`: development. Vite in middleware mode inside the pipeline, with HMR on the same port. It needs the `appServer` plugin. Each request loads the latest server entry, so an edit to a route or to `server.ts` applies at once. A Vite plugin gets the CLI's HTTP server as `server.httpServer` in its `configureServer` hook, so it can wait for `listening` and `close`. A Vite restart, such as after an edit to `vite.config.ts`, keeps that server open. So a new plugin instance gets no `listening`, and the old instance gets `close` only at exit. To clean up for each instance, use Vite's `closeServer` hook. The port is `--port`, else `PORT`, else Vite's `server.port`. When that port is in use, `start` tries each next port, unless Vite's `server.strictPort` is true. The host is `HOST`, else Vite's `server.host`. When it listens, `start` prints its URLs as `vite dev` does, through Vite's logger. Its other log lines, such as a port in use, use the text format of Go's `log/slog`, not JSON. On `SIGTERM` or `SIGINT`, it stops Vite and the server, then exits.
171
171
  - `node build/server/app-server.js`: production, with no install. Use `dist/server/app-server.js` for TanStack Start. It reads `PORT` and `HOST`, and it drains on `SIGTERM` or `SIGINT`, as `app-server serve` does. It has no `--port`. It sets `NODE_ENV` to `production` when `NODE_ENV` has no value.
172
172
  - `app-server serve [path]`: production. With no path, it runs the first of `build/server/app-server.js` (React Router), `dist/server/app-server.js` (TanStack Start), and `build/server/index.js` (a plain React Router build). The port is `--port`, else `PORT`, else 3000. The host is `HOST`, else every interface. A port in use stops startup at once. On `SIGTERM` or `SIGINT`, it drains, then exits, even when the app left a timer. A server process serves the client build that was there at start. A deploy starts a new process.
173
173
 
@@ -274,7 +274,7 @@ The function differs from a long-lived process:
274
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).
275
275
  2. `requestId()`: reuses a valid incoming `X-Request-Id`, else makes a UUID, and echoes it.
276
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.
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.
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. On the node host, brotli holds the start of a body to pick its window. A body that ends within 256 KiB gets a window of 256 KiB, in one zlib call. A longer body gets the default window of 4 MiB, so it can use a repeat that is more than 256 KiB back. Why not 4 MiB for each body: Node.js tells V8 the full window of each open encoder, and under load V8 then runs a full garbage collection for every few responses. When the body waits for its next chunk first, for one turn of the event loop, the stream sends what it holds with the 256 KiB window. So a page shell that waits for slow data, such as a React `<Suspense>` boundary, still goes out at once. Chunks that the body has queued together go out as one, up to 64 KiB, so a rendered page costs fewer flushes. For example, a rendered page of 19 KB that the body queues at once costs one zlib call, not three. 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 for gzip and 256 KiB for brotli, 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.
278
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).
279
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.
280
280
  7. The plugins of the named options. They see every response, prerendered pages included:
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { t as parsePort } from "./tcp-Bws6oEau.js";
2
+ import { t as parsePort } from "./tcp-BPeUZaNg.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-DiQf9ttp.js");
45
+ const { start } = await import("./start-CDnHKNca.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-BUJ7ZonG.js");
50
+ const { serve } = await import("./serve-CEecObwD.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
 
@@ -1,3 +1,4 @@
1
+ import { setImmediate } from "node:timers";
1
2
  import { brotliCompressSync, constants, crc32, createBrotliCompress, createDeflateRaw, createGzip } from "node:zlib";
2
3
 
3
4
  // src/node/compress.ts
@@ -7,7 +8,11 @@ import { brotliCompressSync, constants, crc32, createBrotliCompress, createDefla
7
8
  * and a field costs less than a `WeakMap` entry for each response.
8
9
  */
9
10
  var ZlibBody = class extends ReadableStream {
10
- /** Whether the next chunk waits only on zlib, because the source gave the bytes already. */
11
+ /**
12
+ * Whether the next chunk waits only on zlib, because the source gave the
13
+ * bytes already. It stays set while a brotli stream holds the start of a
14
+ * body, which ends when the source waits.
15
+ */
11
16
  busy = false;
12
17
  };
13
18
  /**
@@ -51,11 +56,11 @@ const EMPTY_MEMBER = Buffer.concat([HEADER, Buffer.from([
51
56
  0
52
57
  ])]);
53
58
  /**
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.
59
+ * The largest body that a pooled gzip stream compresses. Why a limit: above
60
+ * about 64 KiB, the CRC-32 of a gzip body on the main thread costs more than
61
+ * a new zlib stream does.
57
62
  */
58
- const POOLED_MAX_BYTES = 65536;
63
+ const GZIP_POOLED_MAX_BYTES = 65536;
59
64
  /** 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
65
  const MAX_IDLE = 16;
61
66
  function ignore() {}
@@ -94,10 +99,30 @@ const DEFLATE_POOL = {
94
99
  * Express also uses 4.
95
100
  */
96
101
  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
- };
102
+ /**
103
+ * The base-2 logarithm of the brotli window of a body up to
104
+ * {@link BROTLI_HOLD_BYTES}, 256 KiB. Why not the default of 22 (4 MiB):
105
+ * Node.js tells V8 the full size of each encoder's ring buffer as external
106
+ * memory, and at 32 open encoders V8 then runs a full garbage collection for
107
+ * every few responses.
108
+ */
109
+ const BROTLI_WINDOW_BITS = 18;
110
+ /**
111
+ * The most bytes of a body that a brotli stream holds to pick the window: 256
112
+ * KiB for a body that ends within them, else the default of 4 MiB. Why: a
113
+ * stream has no length to pick from, and a repeat more than 256 KiB back costs
114
+ * a page of 2.5 MB 30% more bytes.
115
+ */
116
+ const BROTLI_HOLD_BYTES = 2 ** BROTLI_WINDOW_BITS;
117
+ function brotliParams(windowBits) {
118
+ return {
119
+ [constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,
120
+ [constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY,
121
+ [constants.BROTLI_PARAM_LGWIN]: windowBits
122
+ };
123
+ }
124
+ const BROTLI_PARAMS = brotliParams(BROTLI_WINDOW_BITS);
125
+ const BROTLI_LARGE_PARAMS = brotliParams(constants.BROTLI_DEFAULT_WINDOW);
101
126
  /**
102
127
  * The brotli pool. Why not sized: a reset makes a new brotli encoder with the
103
128
  * same parameters, so the output of a body never depends on an earlier one.
@@ -233,12 +258,26 @@ async function brotliOnce(value, use) {
233
258
  return Buffer.concat(await compressOnce(BROTLI_POOL, value, use));
234
259
  }
235
260
  /**
261
+ * The most chunks that {@link zlibStream} holds. Why: a source that gives each
262
+ * chunk at once keeps the hold in one turn of the event loop, and 262,144
263
+ * chunks of 1 byte blocked it for 215 ms on an Apple M3 Max.
264
+ */
265
+ const HOLD_MAX_CHUNKS = 1024;
266
+ function join(chunks, size) {
267
+ const [first] = chunks;
268
+ return chunks.length === 1 && first != null ? first : Buffer.concat(chunks, size);
269
+ }
270
+ /**
236
271
  * Compresses a byte stream with `codec`. Each written chunk is readable at
237
272
  * once. It reads the next source chunk only when the consumer pulls, so a
238
273
  * slow client holds back the source. When the source ends by the time zlib
239
274
  * finishes a chunk, that chunk and the end of the stream go out as one. Why:
240
275
  * the host writes each chunk to the socket on its own, and a write costs more
241
276
  * than its few bytes.
277
+ *
278
+ * With `codec.createLarge`, it first holds the body up to
279
+ * `codec.onceMaxBytes`, the end, or a wait of the source, and then writes
280
+ * what it holds.
242
281
  */
243
282
  function zlibStream(source, codec) {
244
283
  const reader = source.getReader();
@@ -252,6 +291,62 @@ function zlibStream(source, codec) {
252
291
  /** The pooled zlib stream while it compresses the one chunk of a body. */
253
292
  let pooled;
254
293
  let started = false;
294
+ let canceled = false;
295
+ /** The source read that is still open after a hold. */
296
+ let pending;
297
+ /** The next source chunk with bytes, or the end. */
298
+ const read = async () => {
299
+ let chunk = await (pending ?? reader.read());
300
+ pending = void 0;
301
+ while (!chunk.done && chunk.value.byteLength === 0) chunk = await reader.read();
302
+ return chunk;
303
+ };
304
+ /**
305
+ * `first` and the chunks after it, up to `max` bytes, until the source ends,
306
+ * gives an empty chunk, or leaves a read open for a turn of the event loop.
307
+ * Why stop on a wait: the source waits for data, such as a React
308
+ * `<Suspense>` boundary, and what it gave must reach the client now.
309
+ */
310
+ const hold = async (first, max) => {
311
+ const parts = [first];
312
+ let size = first.byteLength;
313
+ /** Resolves at the end of this turn of the event loop. Why one for each turn: an immediate costs more than a read. */
314
+ let turn;
315
+ const endOfTurn = async () => new Promise((resolve) => {
316
+ setImmediate(() => {
317
+ turn = void 0;
318
+ resolve(void 0);
319
+ });
320
+ });
321
+ while (size <= max && parts.length < HOLD_MAX_CHUNKS) {
322
+ pending ??= reader.read();
323
+ turn ??= endOfTurn();
324
+ const result = await Promise.race([pending, turn]);
325
+ if (result == null) break;
326
+ pending = void 0;
327
+ if (result.done) return {
328
+ value: join(parts, size),
329
+ done: true,
330
+ large: false
331
+ };
332
+ if (result.value.byteLength === 0) break;
333
+ if (size + result.value.byteLength > max) {
334
+ pending = Promise.resolve(result);
335
+ return {
336
+ value: join(parts, size),
337
+ done: false,
338
+ large: true
339
+ };
340
+ }
341
+ parts.push(result.value);
342
+ size += result.value.byteLength;
343
+ }
344
+ return {
345
+ value: join(parts, size),
346
+ done: false,
347
+ large: size > max
348
+ };
349
+ };
255
350
  /** The zlib output that waits for the next enqueue. */
256
351
  let held = [];
257
352
  let heldBytes = 0;
@@ -262,8 +357,8 @@ function zlibStream(source, codec) {
262
357
  held = [];
263
358
  heldBytes = 0;
264
359
  };
265
- const startZlib = (body, controller) => {
266
- const created = codec.create();
360
+ const startZlib = (body, controller, create) => {
361
+ const created = create();
267
362
  created.on("data", (chunk) => {
268
363
  held.push(chunk);
269
364
  heldBytes += chunk.byteLength;
@@ -283,8 +378,9 @@ function zlibStream(source, codec) {
283
378
  const stream = new ZlibBody({
284
379
  async pull(controller) {
285
380
  try {
286
- let chunk = await reader.read();
287
- while (!chunk.done && chunk.value.byteLength === 0) chunk = await reader.read();
381
+ const chunk = await read();
382
+ let create = codec.create;
383
+ let start;
288
384
  if (!started) {
289
385
  started = true;
290
386
  if (chunk.done) {
@@ -294,8 +390,18 @@ function zlibStream(source, codec) {
294
390
  }
295
391
  stream.busy = true;
296
392
  await Promise.resolve();
297
- if (ended && chunk.value.byteLength <= POOLED_MAX_BYTES) {
298
- const output = await codec.once(chunk.value, (each) => {
393
+ const { createLarge, onceMaxBytes } = codec;
394
+ start = ended || createLarge == null ? {
395
+ value: chunk.value,
396
+ done: ended,
397
+ large: chunk.value.byteLength > onceMaxBytes
398
+ } : await hold(chunk.value, onceMaxBytes);
399
+ if (canceled) {
400
+ stream.busy = false;
401
+ return;
402
+ }
403
+ if (start.done && !start.large) {
404
+ const output = await codec.once(start.value, (each) => {
299
405
  pooled = each;
300
406
  });
301
407
  stream.busy = false;
@@ -303,15 +409,17 @@ function zlibStream(source, codec) {
303
409
  controller.close();
304
410
  return;
305
411
  }
412
+ if (createLarge != null && start.large) create = createLarge;
306
413
  }
307
- zlib ??= startZlib(stream, controller);
414
+ zlib ??= startZlib(stream, controller, create);
308
415
  stream.busy = true;
309
416
  if (chunk.done) {
310
417
  zlib.end();
311
418
  return;
312
419
  }
313
- await write(zlib, chunk.value);
314
- if (ended) {
420
+ await write(zlib, start?.value ?? chunk.value);
421
+ ended ||= start?.done === true;
422
+ if (ended && pending == null) {
315
423
  zlib.end();
316
424
  return;
317
425
  }
@@ -324,6 +432,7 @@ function zlibStream(source, codec) {
324
432
  }
325
433
  },
326
434
  async cancel(reason) {
435
+ canceled = true;
327
436
  zlib?.destroy();
328
437
  pooled?.stream.destroy();
329
438
  return reader.cancel(reason);
@@ -334,7 +443,8 @@ function zlibStream(source, codec) {
334
443
  const GZIP = {
335
444
  create: () => createGzip({ flush: constants.Z_SYNC_FLUSH }),
336
445
  empty: EMPTY_MEMBER,
337
- once: gzipOnce
446
+ once: gzipOnce,
447
+ onceMaxBytes: GZIP_POOLED_MAX_BYTES
338
448
  };
339
449
  /**
340
450
  * Gzips a byte stream. `Z_SYNC_FLUSH` makes each written chunk readable at
@@ -358,13 +468,21 @@ const BROTLI = {
358
468
  params: BROTLI_PARAMS
359
469
  }),
360
470
  empty: brotliCompressSync(/* @__PURE__ */ new Uint8Array(), { params: BROTLI_PARAMS }),
361
- once: brotliOnce
471
+ once: brotliOnce,
472
+ onceMaxBytes: BROTLI_HOLD_BYTES,
473
+ createLarge: () => createBrotliCompress({
474
+ flush: constants.BROTLI_OPERATION_FLUSH,
475
+ params: BROTLI_LARGE_PARAMS
476
+ })
362
477
  };
363
478
  /**
364
479
  * Compresses a byte stream with brotli, quality 4. `BROTLI_OPERATION_FLUSH`
365
480
  * 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.
481
+ * {@link gzipStream} does.
482
+ *
483
+ * It holds the start of a body to pick the window, as
484
+ * {@link BROTLI_HOLD_BYTES} says. When the source waits for its next chunk
485
+ * first, the stream sends what it holds with the 256 KiB window.
368
486
  *
369
487
  * @example
370
488
  * new Response(brotliStream(response.body), response);
@@ -374,4 +492,4 @@ function brotliStream(source) {
374
492
  }
375
493
 
376
494
  export { gzipStream as n, waitsForZlib as r, brotliStream as t };
377
- //# sourceMappingURL=compress-JuJf2HaO.js.map
495
+ //# sourceMappingURL=compress-Cp5NfXCC.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compress-Cp5NfXCC.js","names":[],"sources":["../src/node/compress.ts"],"sourcesContent":["import type { Transform } from \"node:stream\";\nimport { setImmediate } from \"node:timers\";\nimport type { ReadResult } from \"../core/compression.js\";\nimport {\n\tbrotliCompressSync,\n\tconstants,\n\tcrc32,\n\tcreateBrotliCompress,\n\tcreateDeflateRaw,\n\tcreateGzip,\n} from \"node:zlib\";\n\n/**\n * A stream from {@link gzipStream} or {@link brotliStream}. Why a class: the\n * node response writer reads `busy` to hold the head for zlib's first chunk,\n * and a field costs less than a `WeakMap` entry for each response.\n */\nclass ZlibBody extends ReadableStream<Uint8Array> {\n\t/**\n\t * Whether the next chunk waits only on zlib, because the source gave the\n\t * bytes already. It stays set while a brotli stream holds the start of a\n\t * body, which ends when the source waits.\n\t */\n\tpublic busy = false;\n}\n\n/**\n * Whether the next chunk of `stream` waits only on zlib, because its source\n * gave the bytes already. It gives `undefined` for a stream that is not from\n * {@link gzipStream} or {@link brotliStream}.\n *\n * @example\n * waitsForZlib(gzipStream(source)); // => false, until the first read gives zlib a chunk\n */\nexport function waitsForZlib(stream: ReadableStream<Uint8Array>): boolean | undefined {\n\treturn stream instanceof ZlibBody ? stream.busy : undefined;\n}\n\n/**\n * The gzip header (RFC 1952): the magic number, deflate, no flags, no time,\n * and the Unix system code.\n */\nconst HEADER = Buffer.from([31, 139, 8, 0, 0, 0, 0, 0, 0, 3]);\n\n/** The gzip member of an empty body: the header, a final empty block, a CRC-32 of 0, and a size of 0. */\nconst EMPTY_MEMBER = Buffer.concat([HEADER, Buffer.from([3, 0, 0, 0, 0, 0, 0, 0, 0, 0])]);\n\n/**\n * The largest body that a pooled gzip stream compresses. Why a limit: above\n * about 64 KiB, the CRC-32 of a gzip body on the main thread costs more than\n * a new zlib stream does.\n */\nconst GZIP_POOLED_MAX_BYTES = 64 * 1024;\n\n/** 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. */\nconst MAX_IDLE = 16;\n\nfunction ignore(): void {}\n\n/**\n * A zlib stream that compresses one body at a time, the target of its output\n * and of its failure, and the size of the longest body that it compressed.\n */\ntype Pooled = {\n\tstream: Transform;\n\toutput: (chunk: Buffer) => void;\n\tfail: (error: Error) => void;\n\tsize: number;\n};\n\n/** The idle zlib streams of one encoding, the last one given back last. */\ntype Pool = {\n\t/** A new zlib stream that finishes its output with each write. */\n\tcreate: () => Transform;\n\tidle: Pooled[];\n\t/**\n\t * Whether a reset keeps state of the earlier bodies, so a stream records\n\t * the size of its longest body (see {@link take}).\n\t */\n\tsized: boolean;\n\t/** Whether a reset lets the stream compress the next body. Else each body gets a new stream. */\n\treuses: boolean;\n};\n\nfunction newPooled(pool: Pool): Pooled {\n\tconst pooled: Pooled = { stream: pool.create(), output: ignore, fail: ignore, size: 0 };\n\tpooled.stream.on(\"data\", (chunk: Buffer) => {\n\t\tpooled.output(chunk);\n\t});\n\t// Why both: zlib destroys the stream at a failure, and then it never calls the write callback.\n\tpooled.stream.on(\"error\", (error) => {\n\t\tpooled.fail(error);\n\t});\n\tpooled.stream.on(\"close\", () => {\n\t\t// Why the check: a stream that the pool destroys has no body, and an error costs its stack.\n\t\tif (pooled.fail !== ignore) {\n\t\t\tpooled.fail(new Error(\"The zlib stream closed.\"));\n\t\t}\n\t});\n\treturn pooled;\n}\n\n/**\n * The deflate pool. Why `Z_FINISH`: when the output fills the buffer of the\n * stream exactly, the stream calls zlib again, and a sync flush then adds an\n * empty block. So the output would depend on earlier bodies.\n */\nconst DEFLATE_POOL: Pool = {\n\tcreate: () => createDeflateRaw({ flush: constants.Z_FINISH }),\n\tidle: [],\n\tsized: true,\n\treuses: true,\n};\n\n/**\n * The brotli quality of a response. Why 4: for HTML, its output is smaller\n * than gzip level 6, for the same CPU time. The `compression` middleware of\n * Express also uses 4.\n */\nconst BROTLI_QUALITY = 4;\n\n/**\n * The base-2 logarithm of the brotli window of a body up to\n * {@link BROTLI_HOLD_BYTES}, 256 KiB. Why not the default of 22 (4 MiB):\n * Node.js tells V8 the full size of each encoder's ring buffer as external\n * memory, and at 32 open encoders V8 then runs a full garbage collection for\n * every few responses.\n */\nconst BROTLI_WINDOW_BITS = 18;\n\n/**\n * The most bytes of a body that a brotli stream holds to pick the window: 256\n * KiB for a body that ends within them, else the default of 4 MiB. Why: a\n * stream has no length to pick from, and a repeat more than 256 KiB back costs\n * a page of 2.5 MB 30% more bytes.\n */\nconst BROTLI_HOLD_BYTES = 2 ** BROTLI_WINDOW_BITS;\n\nfunction brotliParams(windowBits: number): Record<number, number> {\n\treturn {\n\t\t[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,\n\t\t[constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY,\n\t\t[constants.BROTLI_PARAM_LGWIN]: windowBits,\n\t};\n}\n\nconst BROTLI_PARAMS = brotliParams(BROTLI_WINDOW_BITS);\nconst BROTLI_LARGE_PARAMS = brotliParams(constants.BROTLI_DEFAULT_WINDOW);\n\n/**\n * The brotli pool. Why not sized: a reset makes a new brotli encoder with the\n * same parameters, so the output of a body never depends on an earlier one.\n */\nconst BROTLI_POOL: Pool = {\n\tcreate: () =>\n\t\tcreateBrotliCompress({ flush: constants.BROTLI_OPERATION_FINISH, params: BROTLI_PARAMS }),\n\tidle: [],\n\tsized: false,\n\t// Why not on Bun or Deno: after a reset, a Bun encoder forgets its quality, and a Deno encoder gives no bytes.\n\treuses: !(\"Bun\" in globalThis) && !(\"Deno\" in globalThis),\n};\n\n/**\n * An idle stream of `pool` for a body of `size` bytes, else a new one. It\n * takes a stream whose bodies were each no longer than this one: the longest\n * such, and of those the last one given back, whose memory is still in the\n * cache.\n *\n * Why for deflate: a reset keeps the window of zlib, and zlib reads the bytes\n * after the end of a body to hash and match its last bytes. After a longer\n * body, these are bytes of that body, so they could change the output of this\n * one. A stream whose bodies were no longer has only the zeros that zlib\n * writes after each body there, as a new stream does.\n */\nfunction take(pool: Pool, size: number): Pooled {\n\tconst { idle } = pool;\n\tlet best = -1;\n\tlet bestSize = -1;\n\tfor (let index = idle.length - 1; index >= 0; index -= 1) {\n\t\tconst each = idle[index]?.size ?? Infinity;\n\t\tif (each <= size && each > bestSize) {\n\t\t\tbest = index;\n\t\t\tbestSize = each;\n\t\t}\n\t}\n\tif (best === -1) {\n\t\treturn newPooled(pool);\n\t}\n\tconst taken = idle[best];\n\tconst last = idle.pop();\n\tif (taken == null || last == null) {\n\t\treturn newPooled(pool);\n\t}\n\t// Why move the last one into the gap: the order of the others does not matter, and it costs no copy.\n\tif (last !== taken) {\n\t\tidle[best] = last;\n\t}\n\treturn taken;\n}\n\n/**\n * Gives back a stream of `pool` after its body. Why reset: the next body must\n * not use the bytes of this one as a dictionary. When the pool is full, it\n * keeps the streams of the shortest bodies, which more bodies can take.\n */\nfunction release(pool: Pool, pooled: Pooled): void {\n\tpooled.output = ignore;\n\tpooled.fail = ignore;\n\tconst { stream } = pooled;\n\t// Why check the queues: a stream with output or input still queued would give it to the next body.\n\tif (\n\t\t!pool.reuses ||\n\t\tstream.destroyed ||\n\t\tstream.readableLength !== 0 ||\n\t\tstream.writableLength !== 0\n\t) {\n\t\tstream.destroy();\n\t\treturn;\n\t}\n\t// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- each pool creates a zlib stream, which has `reset`.\n\t(stream as Transform & { reset: () => void }).reset();\n\t// Why the check: a reset that fails destroys the stream.\n\tif (stream.destroyed) {\n\t\treturn;\n\t}\n\tconst { idle } = pool;\n\tif (idle.length < MAX_IDLE) {\n\t\tidle.push(pooled);\n\t\treturn;\n\t}\n\tlet longest = 0;\n\tfor (let index = 1; index < idle.length; index += 1) {\n\t\tif ((idle[index]?.size ?? 0) > (idle[longest]?.size ?? 0)) {\n\t\t\tlongest = index;\n\t\t}\n\t}\n\tconst evicted = idle[longest];\n\tif (evicted != null && evicted.size > pooled.size) {\n\t\tidle[longest] = pooled;\n\t\tevicted.stream.destroy();\n\t} else {\n\t\tstream.destroy();\n\t}\n}\n\n/** Writes `chunk` to `stream`. zlib gives all the output of the chunk before this resolves. */\nasync function write(stream: Transform, chunk: Uint8Array): Promise<void> {\n\treturn new Promise((resolve, reject) => {\n\t\tstream.write(chunk, (error) => {\n\t\t\tif (error == null) {\n\t\t\t\tresolve();\n\t\t\t} else {\n\t\t\t\treject(error);\n\t\t\t}\n\t\t});\n\t});\n}\n\n/**\n * The output of a stream of `pool` for a body of one chunk, in one zlib call.\n * `use` gets the stream while zlib compresses, so a cancel can destroy it,\n * and gets `undefined` before the stream goes back to the pool. Why only a\n * body of one chunk: a deflate reset keeps the old window, and a second write\n * reads its bytes as history. The output of one write is the same as from a\n * new stream, so it does not depend on an earlier body.\n */\nasync function compressOnce(\n\tpool: Pool,\n\tvalue: Uint8Array,\n\tuse: (pooled: Pooled | undefined) => void,\n): Promise<Buffer[]> {\n\tconst pooled = take(pool, value.byteLength);\n\tuse(pooled);\n\tconst parts: Buffer[] = [];\n\tpooled.output = (chunk) => {\n\t\tparts.push(chunk);\n\t};\n\ttry {\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\tpooled.fail = reject;\n\t\t\tpooled.stream.write(value, (error) => {\n\t\t\t\tif (error != null) {\n\t\t\t\t\treject(error);\n\t\t\t\t} else if (pooled.stream.destroyed) {\n\t\t\t\t\treject(new Error(\"The zlib stream closed.\"));\n\t\t\t\t} else {\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\t} catch (error) {\n\t\tpooled.output = ignore;\n\t\tpooled.fail = ignore;\n\t\tpooled.stream.destroy();\n\t\tthrow error;\n\t}\n\tif (pool.sized) {\n\t\tpooled.size = Math.max(pooled.size, value.byteLength);\n\t}\n\t// Why first: a cancel after this must not destroy a stream that the pool gives to another body.\n\tuse(undefined);\n\trelease(pool, pooled);\n\treturn parts;\n}\n\n/**\n * The gzip member of a body of one chunk: the raw deflate output of a pooled\n * stream, with the header and the trailer written here.\n */\nasync function gzipOnce(\n\tvalue: Uint8Array,\n\tuse: (pooled: Pooled | undefined) => void,\n): Promise<Buffer> {\n\tconst parts = await compressOnce(DEFLATE_POOL, value, use);\n\tconst trailer = Buffer.allocUnsafe(8);\n\ttrailer.writeUInt32LE(crc32(value), 0);\n\ttrailer.writeUInt32LE(value.byteLength, 4);\n\treturn Buffer.concat([HEADER, ...parts, trailer]);\n}\n\n/** The brotli stream of a body of one chunk, from a pooled encoder. */\nasync function brotliOnce(\n\tvalue: Uint8Array,\n\tuse: (pooled: Pooled | undefined) => void,\n): Promise<Buffer> {\n\treturn Buffer.concat(await compressOnce(BROTLI_POOL, value, use));\n}\n\n/** How {@link zlibStream} compresses a body. */\ntype Codec = {\n\t/** A new zlib stream that flushes after each write. */\n\tcreate: () => Transform;\n\t/** The output for a body with no bytes. */\n\tempty: Buffer;\n\t/**\n\t * The output for a body of one chunk, up to `onceMaxBytes`, with no new\n\t * zlib stream. `use` gets the zlib stream while it compresses.\n\t */\n\tonce: (value: Uint8Array, use: (pooled: Pooled | undefined) => void) => Promise<Buffer>;\n\tonceMaxBytes: number;\n\t/**\n\t * A new zlib stream for a body longer than `onceMaxBytes`, which\n\t * {@link zlibStream} holds until it knows that. Without it, the stream\n\t * holds nothing, and each body gets `create`.\n\t */\n\tcreateLarge?: () => Transform;\n};\n\n/**\n * The start of a body that {@link zlibStream} held, whether the source ended\n * with it, and whether the body is longer than the hold.\n */\ntype Start = { value: Uint8Array; done: boolean; large: boolean };\n\n/**\n * The most chunks that {@link zlibStream} holds. Why: a source that gives each\n * chunk at once keeps the hold in one turn of the event loop, and 262,144\n * chunks of 1 byte blocked it for 215 ms on an Apple M3 Max.\n */\nconst HOLD_MAX_CHUNKS = 1024;\n\nfunction join(chunks: Uint8Array[], size: number): Uint8Array {\n\tconst [first] = chunks;\n\treturn chunks.length === 1 && first != null ? first : Buffer.concat(chunks, size);\n}\n\n/**\n * Compresses a byte stream with `codec`. Each written chunk is readable at\n * once. It reads the next source chunk only when the consumer pulls, so a\n * slow client holds back the source. When the source ends by the time zlib\n * finishes a chunk, that chunk and the end of the stream go out as one. Why:\n * the host writes each chunk to the socket on its own, and a write costs more\n * than its few bytes.\n *\n * With `codec.createLarge`, it first holds the body up to\n * `codec.onceMaxBytes`, the end, or a wait of the source, and then writes\n * what it holds.\n */\nfunction zlibStream(source: ReadableStream<Uint8Array>, codec: Codec): ReadableStream<Uint8Array> {\n\tconst reader = source.getReader();\n\tlet ended = false;\n\tconst markEnded = () => {\n\t\tended = true;\n\t};\n\t// Why a flag: after each zlib write, it tells with no read whether the source ended meanwhile.\n\tvoid reader.closed.then(markEnded, () => {});\n\t/** The zlib stream of a body of more than one chunk. */\n\tlet zlib: Transform | undefined;\n\t/** The pooled zlib stream while it compresses the one chunk of a body. */\n\tlet pooled: Pooled | undefined;\n\tlet started = false;\n\tlet canceled = false;\n\t/** The source read that is still open after a hold. */\n\tlet pending: Promise<ReadResult> | undefined;\n\t/** The next source chunk with bytes, or the end. */\n\tconst read = async () => {\n\t\tlet chunk = await (pending ?? reader.read());\n\t\tpending = undefined;\n\t\t// Why skip empty chunks: zlib writes nothing for one, and a pull that enqueues nothing is not\n\t\t// called again.\n\t\twhile (!chunk.done && chunk.value.byteLength === 0) {\n\t\t\tchunk = await reader.read();\n\t\t}\n\t\treturn chunk;\n\t};\n\t/**\n\t * `first` and the chunks after it, up to `max` bytes, until the source ends,\n\t * gives an empty chunk, or leaves a read open for a turn of the event loop.\n\t * Why stop on a wait: the source waits for data, such as a React\n\t * `<Suspense>` boundary, and what it gave must reach the client now.\n\t */\n\tconst hold = async (first: Uint8Array, max: number): Promise<Start> => {\n\t\tconst parts = [first];\n\t\tlet size = first.byteLength;\n\t\t/** Resolves at the end of this turn of the event loop. Why one for each turn: an immediate costs more than a read. */\n\t\tlet turn: Promise<undefined> | undefined;\n\t\tconst endOfTurn = async () =>\n\t\t\tnew Promise<undefined>((resolve) => {\n\t\t\t\tsetImmediate(() => {\n\t\t\t\t\tturn = undefined;\n\t\t\t\t\tresolve(undefined);\n\t\t\t\t});\n\t\t\t});\n\t\twhile (size <= max && parts.length < HOLD_MAX_CHUNKS) {\n\t\t\tpending ??= reader.read();\n\t\t\tturn ??= endOfTurn();\n\t\t\t// oxlint-disable-next-line no-await-in-loop -- each read waits for the source.\n\t\t\tconst result = await Promise.race([pending, turn]);\n\t\t\tif (result == null) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tpending = undefined;\n\t\t\tif (result.done) {\n\t\t\t\treturn { value: join(parts, size), done: true, large: false };\n\t\t\t}\n\t\t\t// Why stop on an empty chunk: a read that resolves at once never lets the turn end.\n\t\t\tif (result.value.byteLength === 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// Why give it to the next read: a join would copy the large chunk.\n\t\t\tif (size + result.value.byteLength > max) {\n\t\t\t\tpending = Promise.resolve(result);\n\t\t\t\treturn { value: join(parts, size), done: false, large: true };\n\t\t\t}\n\t\t\tparts.push(result.value);\n\t\t\tsize += result.value.byteLength;\n\t\t}\n\t\treturn { value: join(parts, size), done: false, large: size > max };\n\t};\n\t/** The zlib output that waits for the next enqueue. */\n\tlet held: Buffer[] = [];\n\tlet heldBytes = 0;\n\tconst enqueueHeld = (controller: ReadableStreamDefaultController<Uint8Array>) => {\n\t\tconst [first] = held;\n\t\tif (first == null) {\n\t\t\treturn;\n\t\t}\n\t\t// Why no copy for one chunk: zlib gives each chunk its own bytes, and it never writes them again.\n\t\tcontroller.enqueue(held.length === 1 ? first : Buffer.concat(held));\n\t\theld = [];\n\t\theldBytes = 0;\n\t};\n\tconst startZlib = (\n\t\tbody: ZlibBody,\n\t\tcontroller: ReadableStreamDefaultController<Uint8Array>,\n\t\tcreate: () => Transform,\n\t): Transform => {\n\t\tconst created = create();\n\t\tcreated.on(\"data\", (chunk: Buffer) => {\n\t\t\theld.push(chunk);\n\t\t\theldBytes += chunk.byteLength;\n\t\t\t// Why: the output of a large source chunk goes out while zlib still compresses the rest.\n\t\t\tif (heldBytes >= constants.Z_DEFAULT_CHUNK) {\n\t\t\t\tenqueueHeld(controller);\n\t\t\t}\n\t\t});\n\t\tcreated.on(\"end\", () => {\n\t\t\tbody.busy = false;\n\t\t\tenqueueHeld(controller);\n\t\t\tcontroller.close();\n\t\t});\n\t\tcreated.on(\"error\", (error) => {\n\t\t\tbody.busy = false;\n\t\t\tcontroller.error(error);\n\t\t});\n\t\treturn created;\n\t};\n\tconst stream: ZlibBody = new ZlibBody(\n\t\t{\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tconst chunk = await read();\n\t\t\t\t\tlet create = codec.create;\n\t\t\t\t\tlet start: Start | undefined;\n\t\t\t\t\tif (!started) {\n\t\t\t\t\t\tstarted = true;\n\t\t\t\t\t\tif (chunk.done) {\n\t\t\t\t\t\t\tcontroller.enqueue(codec.empty);\n\t\t\t\t\t\t\tcontroller.close();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tstream.busy = true;\n\t\t\t\t\t\t// Why wait: a source that closes in the pull that gave this chunk ends one microtask later.\n\t\t\t\t\t\tawait Promise.resolve();\n\t\t\t\t\t\tconst { createLarge, onceMaxBytes } = codec;\n\t\t\t\t\t\tstart =\n\t\t\t\t\t\t\tended || createLarge == null\n\t\t\t\t\t\t\t\t? { value: chunk.value, done: ended, large: chunk.value.byteLength > onceMaxBytes }\n\t\t\t\t\t\t\t\t: await hold(chunk.value, onceMaxBytes);\n\t\t\t\t\t\t// Why: a cancel during the hold ran before this stream had an encoder to destroy.\n\t\t\t\t\t\tif (canceled) {\n\t\t\t\t\t\t\tstream.busy = false;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (start.done && !start.large) {\n\t\t\t\t\t\t\tconst output = await codec.once(start.value, (each) => {\n\t\t\t\t\t\t\t\tpooled = each;\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tstream.busy = false;\n\t\t\t\t\t\t\tcontroller.enqueue(output);\n\t\t\t\t\t\t\tcontroller.close();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (createLarge != null && start.large) {\n\t\t\t\t\t\t\tcreate = createLarge;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tzlib ??= startZlib(stream, controller, create);\n\t\t\t\t\tstream.busy = true;\n\t\t\t\t\tif (chunk.done) {\n\t\t\t\t\t\tzlib.end();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tawait write(zlib, start?.value ?? chunk.value);\n\t\t\t\t\tended ||= start?.done === true;\n\t\t\t\t\t// Why check `pending`: an open read can hold the last chunk when the source closes.\n\t\t\t\t\tif (ended && pending == null) {\n\t\t\t\t\t\tzlib.end();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tstream.busy = false;\n\t\t\t\t\t// Why the output is here: zlib emits the output of a chunk before it calls the write\n\t\t\t\t\t// callback, so this pull enqueues a chunk and the consumer's read does not wait.\n\t\t\t\t\tenqueueHeld(controller);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tstream.busy = false;\n\t\t\t\t\tzlib?.destroy();\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t\t// Why cancel the source: a client disconnect must reach the handler, so it stops its work.\n\t\t\tasync cancel(reason) {\n\t\t\t\tcanceled = true;\n\t\t\t\tzlib?.destroy();\n\t\t\t\t// Why destroy: a stream that a write still uses must never go back to the pool.\n\t\t\t\tpooled?.stream.destroy();\n\t\t\t\treturn reader.cancel(reason);\n\t\t\t},\n\t\t},\n\t\t// Why 0: the stream reads its source only when the consumer reads.\n\t\t{ highWaterMark: 0 },\n\t);\n\treturn stream;\n}\n\nconst GZIP: Codec = {\n\tcreate: () => createGzip({ flush: constants.Z_SYNC_FLUSH }),\n\tempty: EMPTY_MEMBER,\n\tonce: gzipOnce,\n\tonceMaxBytes: GZIP_POOLED_MAX_BYTES,\n};\n\n/**\n * Gzips a byte stream. `Z_SYNC_FLUSH` makes each written chunk readable at\n * once. It reads the next source chunk only when the consumer pulls, so a slow\n * client holds back the source. When the source ends by the time zlib\n * finishes a chunk, that chunk and the gzip trailer go out as one.\n *\n * A body that ends with its first chunk, up to 64 KiB, takes a pooled raw\n * deflate stream. Why: a new zlib stream costs more main-thread time than\n * zlib takes for a small page.\n *\n * @example\n * new Response(gzipStream(response.body), response);\n */\nexport function gzipStream(source: ReadableStream<Uint8Array>): ReadableStream<Uint8Array> {\n\treturn zlibStream(source, GZIP);\n}\n\nconst BROTLI: Codec = {\n\tcreate: () =>\n\t\tcreateBrotliCompress({ flush: constants.BROTLI_OPERATION_FLUSH, params: BROTLI_PARAMS }),\n\tempty: brotliCompressSync(new Uint8Array(), { params: BROTLI_PARAMS }),\n\tonce: brotliOnce,\n\tonceMaxBytes: BROTLI_HOLD_BYTES,\n\tcreateLarge: () =>\n\t\tcreateBrotliCompress({\n\t\t\tflush: constants.BROTLI_OPERATION_FLUSH,\n\t\t\tparams: BROTLI_LARGE_PARAMS,\n\t\t}),\n};\n\n/**\n * Compresses a byte stream with brotli, quality 4. `BROTLI_OPERATION_FLUSH`\n * makes each written chunk readable at once. It reads and ends as\n * {@link gzipStream} does.\n *\n * It holds the start of a body to pick the window, as\n * {@link BROTLI_HOLD_BYTES} says. When the source waits for its next chunk\n * first, the stream sends what it holds with the 256 KiB window.\n *\n * @example\n * new Response(brotliStream(response.body), response);\n */\nexport function brotliStream(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AAwB,QAAQ;CAAc,CAAC;CACxF,OAAO,mCAAmB,IAAI,WAAW,GAAG,EAAE,QAAQ,cAAc,CAAC;CACrE,MAAM;CACN,cAAc;CACd,mBACC,qBAAqB;EACpB,OAAO,UAAU;EACjB,QAAQ;CACT,CAAC;AACH;;;;;;;;;;;;;AAcA,SAAgB,aAAa,QAAgE;CAC5F,OAAO,WAAW,QAAQ,MAAM;AACjC"}
@@ -1,8 +1,8 @@
1
1
  import { r as withRequestContext } from "./request-context-BtQ87VYb.js";
2
2
  import { a as replaceResponse, o as withHeaderHook, r as hookedHeaders, s as writableHeaders } from "./header-hook-Vzw9J9BY.js";
3
3
  import { c as presenceHeaders, l as presenceHeadersOf, o as checkOptionPlugins, r as buildId, s as optionPlugin } from "./build-id-B0448vZH.js";
4
- import { i as requestLogger, r as jsonSink, t as errorAttributes } from "./log-BP6xXXZl.js";
5
- import { n as finishResponse, r as onResponseSent } from "./response-edge-Bc1-C_Cx.js";
4
+ import { i as requestLogger, r as jsonSink, t as errorAttributes } from "./log-C8LGQ23s.js";
5
+ import { n as finishResponse, r as onResponseSent } from "./response-edge-D3CAjukJ.js";
6
6
  import { r as manifestKey } from "./build-manifest-DBMWe3Lx.js";
7
7
  import { a as isCompressible, i as forbidsTransform, n as MIN_COMPRESS_BYTES, o as preferredEncoding, s as readIfQueued } from "./compression-CT2mWlKM.js";
8
8
  import { i as isFilePath$1, n as findStaticFile, o as staticResponse, s as withVary, t as contentType } from "./static-files-C2ea8KQ6.js";
@@ -1048,11 +1048,13 @@ function requestId({ headerName = "X-Request-Id", datadogTraceIds = false } = {}
1048
1048
 
1049
1049
  // src/core/plugins/streaming-compress.ts
1050
1050
  /**
1051
- * The most bytes that one pull of {@link replay} joins. Why 16 KiB: a source
1051
+ * The most bytes that one pull of {@link replay} joins. Why 64 KiB: each
1052
+ * zlib write costs a trip to the thread pool, and the node host compresses a
1053
+ * body of one chunk up to 64 KiB with a pooled stream in one trip. A source
1052
1054
  * that makes a chunk on each read looks queued, so the join can read ahead of
1053
- * the client by this size, the default read-ahead of a Node.js stream.
1055
+ * the client by this size.
1054
1056
  */
1055
- const MAX_JOINED_BYTES = 16384;
1057
+ const MAX_JOINED_BYTES = 65536;
1056
1058
  /**
1057
1059
  * Reads at most two chunks that `reader` has queued, and never waits for
1058
1060
  * more. Why two: a body from a string or a buffer queues its bytes and its
@@ -1109,7 +1111,7 @@ function replay(reader, queued) {
1109
1111
  async pull(controller) {
1110
1112
  const parts = [];
1111
1113
  let size = 0;
1112
- const fits = (chunk) => parts.length === 0 || size + chunk.byteLength <= 16384;
1114
+ const fits = (chunk) => parts.length === 0 || size + chunk.byteLength <= 65536;
1113
1115
  for (let [chunk] = chunks; chunk != null && fits(chunk); [chunk] = chunks) {
1114
1116
  chunks.shift();
1115
1117
  parts.push(chunk);
@@ -1125,7 +1127,7 @@ function replay(reader, queued) {
1125
1127
  parts.push(result.value);
1126
1128
  size = result.value.byteLength;
1127
1129
  }
1128
- while (chunks.length === 0 && pending == null && !done && size < 16384) {
1130
+ while (chunks.length === 0 && pending == null && !done && size < 65536) {
1129
1131
  const next = await readIfQueued(reader);
1130
1132
  if ("pending" in next) ({pending} = next);
1131
1133
  else if (next.result.done) done = true;
@@ -1158,7 +1160,7 @@ function compressed(response, body, { encoding, hasLength }) {
1158
1160
  * chunk, so a streamed page shell or a server-sent event reaches the client
1159
1161
  * at once. A client gets brotli when its `Accept-Encoding` ranks `br` at
1160
1162
  * least as high as `gzip`. Chunks that the body has queued together are one
1161
- * chunk, up to 16 KiB. `hono/compress` holds a stream until it ends. Skips a
1163
+ * chunk, up to 64 KiB. `hono/compress` holds a stream until it ends. Skips a
1162
1164
  * response that is already encoded, a partial response, one marked
1163
1165
  * `no-transform`, and one whose `Content-Length` is under 1 KiB. It adds
1164
1166
  * `Accept-Encoding` to the `Vary` of each response that it can compress. The
@@ -1485,4 +1487,4 @@ function serverInternals(app) {
1485
1487
  }
1486
1488
 
1487
1489
  export { assets as _, streamingCompress as a, prefersMarkdown as b, securityHeaders as c, noindex as d, health as f, accessLog as g, bodyLimit as h, withForwardedProto as i, redirects as l, shutdownSignal as m, defaultPlugins as n, requestId as o, abortShutdownSignal as p, serverInternals as r, contentSecurityPolicy as s, createServer as t, privateDocuments as u, prerendered as v, trailingSlash as y };
1488
- //# sourceMappingURL=create-server-BX2rH6-E.js.map
1490
+ //# sourceMappingURL=create-server-C_90R50y.js.map