@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.
Files changed (63) hide show
  1. package/README.md +13 -9
  2. package/dist/brotli-only-DrA29Ecw.js +97 -0
  3. package/dist/brotli-only-DrA29Ecw.js.map +1 -0
  4. package/dist/cli.js +3 -3
  5. package/dist/compress-JuJf2HaO.js +377 -0
  6. package/dist/compress-JuJf2HaO.js.map +1 -0
  7. package/dist/{compression-7_gToa8R.js → compression-CT2mWlKM.js} +9 -2
  8. package/dist/compression-CT2mWlKM.js.map +1 -0
  9. package/dist/{create-server-jMl2ltHD.js → create-server-BX2rH6-E.js} +45 -26
  10. package/dist/{create-server-jMl2ltHD.js.map → create-server-BX2rH6-E.js.map} +1 -1
  11. package/dist/{create-server-D4-nVOVJ.d.ts → create-server-kxyhCdhv.d.ts} +3 -3
  12. package/dist/{entry-BPrgE_6K.d.ts → entry-CNL4OsEn.d.ts} +3 -3
  13. package/dist/{entry-D9H4LA9c.js → entry-ChHgs269.js} +4 -3
  14. package/dist/{entry-D9H4LA9c.js.map → entry-ChHgs269.js.map} +1 -1
  15. package/dist/{index-Db7dspq6.d.ts → index-B8LsM6Mh.d.ts} +21 -1
  16. package/dist/index.d.ts +13 -12
  17. package/dist/index.js +2 -2
  18. package/dist/{load-entry-D7r0mjEp.js → load-entry-BOgJPt1I.js} +4 -4
  19. package/dist/{load-entry-D7r0mjEp.js.map → load-entry-BOgJPt1I.js.map} +1 -1
  20. package/dist/{node-C1qudLs_.js → node-B0AYf_IA.js} +5 -4
  21. package/dist/{node-C1qudLs_.js.map → node-B0AYf_IA.js.map} +1 -1
  22. package/dist/{node-tBVQPjQv.js → node-C0Ad7CVL.js} +2 -2
  23. package/dist/{node-tBVQPjQv.js.map → node-C0Ad7CVL.js.map} +1 -1
  24. package/dist/node-vite.d.ts +1 -1
  25. package/dist/node.d.ts +1 -1
  26. package/dist/node.js +2 -2
  27. package/dist/react-router-vite.d.ts +1 -1
  28. package/dist/react-router.d.ts +1 -1
  29. package/dist/{response-writer-B7FoJjEE.js → response-writer-DzvUAz9x.js} +3 -3
  30. package/dist/response-writer-DzvUAz9x.js.map +1 -0
  31. package/dist/{script-hashes-C6hQ5XnP.js → script-hashes-CU7P9oZU.js} +2 -2
  32. package/dist/{script-hashes-C6hQ5XnP.js.map → script-hashes-CU7P9oZU.js.map} +1 -1
  33. package/dist/{serve-E0hSGe6_.js → serve-BUJ7ZonG.js} +2 -2
  34. package/dist/{serve-E0hSGe6_.js.map → serve-BUJ7ZonG.js.map} +1 -1
  35. package/dist/{start-CifYPRq1.js → start-DiQf9ttp.js} +6 -6
  36. package/dist/{start-CifYPRq1.js.map → start-DiQf9ttp.js.map} +1 -1
  37. package/dist/{static-files-Beju9riB.js → static-files-C2ea8KQ6.js} +11 -3
  38. package/dist/static-files-C2ea8KQ6.js.map +1 -0
  39. package/dist/tanstack-start-vite.d.ts +1 -1
  40. package/dist/tanstack-start.d.ts +2 -2
  41. package/dist/{tcp-Dd1Mt6sN.js → tcp-Bws6oEau.js} +2 -2
  42. package/dist/{tcp-Dd1Mt6sN.js.map → tcp-Bws6oEau.js.map} +1 -1
  43. package/dist/testing.d.ts +2 -2
  44. package/dist/testing.js +3 -3
  45. package/dist/{trailing-slash-CFSv_Gzx.d.ts → trailing-slash-DeOUxMYo.d.ts} +2 -2
  46. package/dist/{types-j29vspx_.d.ts → types-BKJxtIuj.d.ts} +10 -3
  47. package/dist/vercel-vite.d.ts +1 -1
  48. package/dist/vercel-vite.js +24 -4
  49. package/dist/vercel-vite.js.map +1 -1
  50. package/dist/vercel.d.ts +4 -3
  51. package/dist/vercel.js +7 -5
  52. package/dist/vercel.js.map +1 -1
  53. package/dist/{vite-BDREIgiH.js → vite-DPiSKHDn.js} +139 -33
  54. package/dist/vite-DPiSKHDn.js.map +1 -0
  55. package/dist/vite.d.ts +1 -1
  56. package/dist/vite.js +1 -1
  57. package/package.json +2 -1
  58. package/dist/compression-7_gToa8R.js.map +0 -1
  59. package/dist/gzip-Doi4wqjX.js +0 -294
  60. package/dist/gzip-Doi4wqjX.js.map +0 -1
  61. package/dist/response-writer-B7FoJjEE.js.map +0 -1
  62. package/dist/static-files-Beju9riB.js.map +0 -1
  63. package/dist/vite-BDREIgiH.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compress-JuJf2HaO.js","names":[],"sources":["../src/node/compress.ts"],"sourcesContent":["import type { Transform } from \"node:stream\";\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/** Whether the next chunk waits only on zlib, because the source gave the bytes already. */\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 or brotli stream compresses. Why a\n * limit: above about 64 KiB, the CRC-32 of a gzip body on the main thread\n * costs more than a new zlib stream does.\n */\nconst 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\nconst BROTLI_PARAMS = {\n\t[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,\n\t[constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY,\n};\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 64 KiB, with no new zlib\n\t * stream. `use` gets the zlib stream while it compresses.\n\t */\n\tonce: (value: Uint8Array, use: (pooled: Pooled | undefined) => void) => Promise<Buffer>;\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 */\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\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): Transform => {\n\t\tconst created = codec.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\tlet chunk = await reader.read();\n\t\t\t\t\t// Why skip empty chunks: zlib writes nothing for one, and a pull that enqueues nothing\n\t\t\t\t\t// is not called again.\n\t\t\t\t\twhile (!chunk.done && chunk.value.byteLength === 0) {\n\t\t\t\t\t\tchunk = await reader.read();\n\t\t\t\t\t}\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\tif (ended && chunk.value.byteLength <= POOLED_MAX_BYTES) {\n\t\t\t\t\t\t\tconst output = await codec.once(chunk.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}\n\t\t\t\t\tzlib ??= startZlib(stream, controller);\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, chunk.value);\n\t\t\t\t\tif (ended) {\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\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};\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};\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, and a body that ends with its first chunk, up to\n * 64 KiB, also takes a pooled encoder.\n *\n * @example\n * new Response(brotliStream(response.body), response);\n */\nexport function brotliStream(source: ReadableStream<Uint8Array>): ReadableStream<Uint8Array> {\n\treturn zlibStream(source, BROTLI);\n}\n"],"mappings":";;;;;;;;AAeA,IAAM,WAAN,cAAuB,eAA2B;;CAEjD,AAAO,OAAO;AACf;;;;;;;;;AAUA,SAAgB,aAAa,QAAyD;CACrF,OAAO,kBAAkB,WAAW,OAAO,OAAO;AACnD;;;;;AAMA,MAAM,SAAS,OAAO,KAAK;CAAC;CAAI;CAAK;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;AAAC,CAAC;;AAG5D,MAAM,eAAe,OAAO,OAAO,CAAC,QAAQ,OAAO,KAAK;CAAC;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;CAAG;AAAC,CAAC,CAAC,CAAC;;;;;;AAOxF,MAAM,mBAAmB;;AAGzB,MAAM,WAAW;AAEjB,SAAS,SAAe,CAAC;AA2BzB,SAAS,UAAU,MAAoB;CACtC,MAAM,SAAiB;EAAE,QAAQ,KAAK,OAAO;EAAG,QAAQ;EAAQ,MAAM;EAAQ,MAAM;CAAE;CACtF,OAAO,OAAO,GAAG,SAAS,UAAkB;EAC3C,OAAO,OAAO,KAAK;CACpB,CAAC;CAED,OAAO,OAAO,GAAG,UAAU,UAAU;EACpC,OAAO,KAAK,KAAK;CAClB,CAAC;CACD,OAAO,OAAO,GAAG,eAAe;EAE/B,IAAI,OAAO,SAAS,QACnB,OAAO,qBAAK,IAAI,MAAM,yBAAyB,CAAC;CAElD,CAAC;CACD,OAAO;AACR;;;;;;AAOA,MAAM,eAAqB;CAC1B,cAAc,iBAAiB,EAAE,OAAO,U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Accept } from "hono/utils/accept";
2
2
 
3
3
  // src/core/compression.ts
4
- const COMPRESSIBLE = /^(?:text\/|application\/(?:json|javascript|xml|schema\+json|manifest\+json|x-ndjson|connect\+json)|image\/svg\+xml)/u;
4
+ /**
5
+ * The content types that the server and the build compress: each text type,
6
+ * each type with a `+json` or `+xml` suffix, and the other types that
7
+ * `mime-db` marks compressible and that a site sends, such as `image/x-icon`
8
+ * and `font/ttf`. Why not `font/woff` and `font/woff2`: they are compressed
9
+ * already.
10
+ */
11
+ const COMPRESSIBLE = /^(?:text\/|[\w-]+\/[\w.-]+\+(?:json|xml)|application\/(?:json|xml|javascript|x-ndjson|wasm|yaml|x-yaml|x-sh\b|vnd\.ms-fontobject)|image\/(?:x-icon|vnd\.microsoft\.icon|bmp)|font\/(?:ttf|otf))/u;
5
12
  /**
6
13
  * The smallest body that the server or the build compresses. Why: gzip
7
14
  * makes a smaller body larger, and each gzip stream allocates about 300
@@ -94,4 +101,4 @@ async function readIfQueued(reader) {
94
101
  }
95
102
 
96
103
  export { isCompressible as a, forbidsTransform as i, MIN_COMPRESS_BYTES as n, preferredEncoding as o, copyName as r, readIfQueued as s, COPY_ENCODINGS as t };
97
- //# sourceMappingURL=compression-7_gToa8R.js.map
104
+ //# sourceMappingURL=compression-CT2mWlKM.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compression-CT2mWlKM.js","names":[],"sources":["../src/core/compression.ts"],"sourcesContent":["import { parseAccept } from \"hono/utils/accept\";\n\n/**\n * The content types that the server and the build compress: each text type,\n * each type with a `+json` or `+xml` suffix, and the other types that\n * `mime-db` marks compressible and that a site sends, such as `image/x-icon`\n * and `font/ttf`. Why not `font/woff` and `font/woff2`: they are compressed\n * already.\n */\nconst COMPRESSIBLE =\n\t/^(?:text\\/|[\\w-]+\\/[\\w.-]+\\+(?:json|xml)|application\\/(?:json|xml|javascript|x-ndjson|wasm|yaml|x-yaml|x-sh\\b|vnd\\.ms-fontobject)|image\\/(?:x-icon|vnd\\.microsoft\\.icon|bmp)|font\\/(?:ttf|otf))/u;\n\n/**\n * The smallest body that the server or the build compresses. Why: gzip\n * makes a smaller body larger, and each gzip stream allocates about 300\n * kibibytes (KiB) of zlib state. `hono/compress` uses the same value.\n */\nexport const MIN_COMPRESS_BYTES = 1024;\n\n/** A content coding of a compressed copy that the build writes. */\nexport type Encoding = \"br\" | \"gzip\";\n\n/**\n * The encodings of the copies, in the order that the server prefers them.\n * Why brotli first: its copy is smaller, such as 45 KB in place of 56 KB\n * for a docs page of 417 KB.\n */\nexport const COPY_ENCODINGS: readonly Encoding[] = [\"br\", \"gzip\"];\n\nconst COPY_EXTENSIONS: Readonly<Record<Encoding, string>> = { br: \"br\", gzip: \"gz\" };\n\n/**\n * The name of the `encoding` copy of the file `name`, which the build writes\n * next to it.\n *\n * Why a dotfile: the static stages never serve a dotfile, so the copy has no\n * URL of its own. A request gets the copy only through the URL of its file,\n * which `skip` and an auth check can block. The build also never replaces or\n * removes a `.gz` or `.br` file of the app.\n *\n * @example\n * copyName(\"app-abc.js\", \"br\"); // => \".app-abc.js.br\"\n */\nexport function copyName(name: string, encoding: Encoding): string {\n\treturn `.${name}.${COPY_EXTENSIONS[encoding]}`;\n}\n\n/**\n * Whether the server compresses a body of the content type `type`.\n *\n * @example\n * isCompressible(\"text/html; charset=utf-8\"); // => true\n * isCompressible(\"image/png\"); // => false\n */\nexport function isCompressible(type: string): boolean {\n\treturn COMPRESSIBLE.test(type);\n}\n\n/**\n * The encoding in `offered` that an `Accept-Encoding` header prefers, or\n * `undefined` for the body as it is. Each encoding gets the `q` of its own\n * entry, else of `*`. The highest `q` above 0 wins, and a tie goes to the\n * first encoding in `offered`. An `identity` entry with a higher `q` keeps\n * the body as it is.\n *\n * @example\n * preferredEncoding(\"gzip, deflate, br\", [\"br\", \"gzip\"]); // => \"br\"\n * preferredEncoding(\"br;q=0, gzip\", [\"br\", \"gzip\"]); // => \"gzip\"\n */\nexport function preferredEncoding(\n\theader: string | undefined,\n\toffered: readonly Encoding[],\n): Encoding | undefined {\n\tconst entries = parseAccept(header ?? \"\");\n\tconst quality = (coding: string) =>\n\t\t(\n\t\t\tentries.find((entry) => entry.type.toLowerCase() === coding) ??\n\t\t\tentries.find((entry) => entry.type === \"*\")\n\t\t)?.q ?? 0;\n\tlet best: Encoding | undefined;\n\tlet bestQuality = 0;\n\tfor (const encoding of offered) {\n\t\tconst value = quality(encoding);\n\t\tif (value > bestQuality) {\n\t\t\tbest = encoding;\n\t\t\tbestQuality = value;\n\t\t}\n\t}\n\t// Why only an `identity` entry: with no entry, identity is acceptable, but the client prefers a coding that it names.\n\tconst identity = entries.find((entry) => entry.type.toLowerCase() === \"identity\")?.q;\n\treturn identity != null && identity > bestQuality ? undefined : best;\n}\n\n/**\n * Whether a `Cache-Control` value forbids a change to the body, such as gzip.\n *\n * @example\n * forbidsTransform(\"public, no-transform\"); // => true\n * forbidsTransform(\"public, max-age=3600\"); // => false\n */\nexport function forbidsTransform(cacheControl: string | null): boolean {\n\treturn /\\bno-transform\\b/u.test(cacheControl ?? \"\");\n}\n\n/** The result of one read of a byte stream. */\nexport type ReadResult = ReadableStreamReadResult<Uint8Array>;\n\n/**\n * The next result if `reader` has it queued, or the read that is still open.\n * It never waits for the source.\n *\n * @example\n * const next = await readIfQueued(reader); // => { result: { done: false, value } } or { pending }\n */\nexport async function readIfQueued(\n\treader: ReadableStreamDefaultReader<Uint8Array>,\n): Promise<{ result: ReadResult } | { pending: Promise<ReadResult> }> {\n\tconst pending = reader.read();\n\t// Why: a client that leaves before the body is read must not cause an unhandled rejection.\n\tpending.catch(() => {});\n\tconst result = await Promise.race([pending, Promise.resolve().then(async () => undefined)]);\n\treturn result == null ? { pending } : { result };\n}\n"],"mappings":";;;;;;;;;;AASA,MAAM,eACL;;;;;;AAOD,MAAa,qBAAqB;;;;;;AAUlC,MAAa,iBAAsC,CAAC,MAAM,MAAM;AAEhE,MAAM,kBAAsD;CAAE,IAAI;CAAM,MAAM;AAAK;;;;;;;;;;;;;AAcnF,SAAgB,SAAS,MAAc,UAA4B;CAClE,OAAO,IAAI,KAAK,GAAG,gBAAgB;AACpC;;;;;;;;AASA,SAAgB,eAAe,MAAuB;CACrD,OAAO,aAAa,KAAK,IAAI;AAC9B;;;;;;;;;;;;AAaA,SAAgB,kBACf,QACA,SACuB;CACvB,MAAM,UAAU,YAAY,UAAU,EAAE;CACxC,MAAM,WAAW,YAEf,QAAQ,MAAM,UAAU,MAAM,KAAK,YAAY,MAAM,MAAM,KAC3D,QAAQ,MAAM,UAAU,MAAM,SAAS,GAAG,EAAC,EACzC,KAAK;CACT,IAAI;CACJ,IAAI,cAAc;CAClB,KAAK,MAAM,YAAY,SAAS;EAC/B,MAAM,QAAQ,QAAQ,QAAQ;EAC9B,IAAI,QAAQ,aAAa;GACxB,OAAO;GACP,cAAc;EACf;CACD;CAEA,MAAM,WAAW,QAAQ,MAAM,UAAU,MAAM,KAAK,YAAY,MAAM,UAAU,CAAC,EAAE;CACnF,OAAO,YAAY,QAAQ,WAAW,cAAc,SAAY;AACjE;;;;;;;;AASA,SAAgB,iBAAiB,cAAsC;CACtE,OAAO,oBAAoB,KAAK,gBAAgB,EAAE;AACnD;;;;;;;;AAYA,eAAsB,aACrB,QACqE;CACrE,MAAM,UAAU,OAAO,KAAK;CAE5B,QAAQ,YAAY,CAAC,CAAC;CACtB,MAAM,SAAS,MAAM,QAAQ,KAAK,CAAC,SAAS,QAAQ,QAAQ,CAAC,CAAC,KAAK,YAAY,MAAS,CAAC,CAAC;CAC1F,OAAO,UAAU,OAAO,EAAE,QAAQ,IAAI,EAAE,OAAO;AAChD"}
@@ -4,8 +4,8 @@ import { c as presenceHeaders, l as presenceHeadersOf, o as checkOptionPlugins,
4
4
  import { i as requestLogger, r as jsonSink, t as errorAttributes } from "./log-BP6xXXZl.js";
5
5
  import { n as finishResponse, r as onResponseSent } from "./response-edge-Bc1-C_Cx.js";
6
6
  import { r as manifestKey } from "./build-manifest-DBMWe3Lx.js";
7
- import { a as isCompressible, i as forbidsTransform, n as MIN_COMPRESS_BYTES, o as preferredEncoding, s as readIfQueued } from "./compression-7_gToa8R.js";
8
- import { i as isFilePath$1, n as findStaticFile, o as staticResponse, s as withVary, t as contentType } from "./static-files-Beju9riB.js";
7
+ import { a as isCompressible, i as forbidsTransform, n as MIN_COMPRESS_BYTES, o as preferredEncoding, s as readIfQueued } from "./compression-CT2mWlKM.js";
8
+ import { i as isFilePath$1, n as findStaticFile, o as staticResponse, s as withVary, t as contentType } from "./static-files-C2ea8KQ6.js";
9
9
  import { Hono } from "hono";
10
10
  import { getPath } from "hono/utils/url";
11
11
  import { matchedRoutes } from "hono/route";
@@ -1095,7 +1095,7 @@ function join(chunks, size) {
1095
1095
  /**
1096
1096
  * The body again: the queued chunks, then the rest of `reader`. Each pull
1097
1097
  * waits for one chunk at most, and joins it with the chunks that are queued
1098
- * after it, up to {@link MAX_JOINED_BYTES}. Why join: gzip flushes after each
1098
+ * after it, up to {@link MAX_JOINED_BYTES}. Why join: zlib flushes after each
1099
1099
  * chunk, and each flush costs more than the bytes. A renderer such as React
1100
1100
  * queues many small chunks at once. A chunk that comes later still goes out on
1101
1101
  * its own. A chunk that does not fit waits for the next pull, and an empty
@@ -1142,52 +1142,68 @@ function replay(reader, queued) {
1142
1142
  }, { highWaterMark: 0 });
1143
1143
  }
1144
1144
  /**
1145
- * `response` with the gzip `body` and its headers. Why not in a header hook:
1146
- * the body and its headers go together, also to the code that an outer stage
1147
- * runs after its `next()`.
1145
+ * `response` with the compressed `body` and its headers. Why not in a header
1146
+ * hook: the body and its headers go together, also to the code that an outer
1147
+ * stage runs after its `next()`.
1148
1148
  */
1149
- function gzipped(response, body, { hasLength }) {
1149
+ function compressed(response, body, { encoding, hasLength }) {
1150
1150
  const replacement = new Response(body, response);
1151
- replacement.headers.set("Content-Encoding", "gzip");
1151
+ replacement.headers.set("Content-Encoding", encoding);
1152
1152
  if (hasLength) replacement.headers.delete("Content-Length");
1153
1153
  replacement.headers.delete("Accept-Ranges");
1154
1154
  return replacement;
1155
1155
  }
1156
1156
  /**
1157
- * Gzips text responses and flushes after each chunk, so a streamed page shell
1158
- * or a server-sent event reaches the client at once. Chunks that the body has
1159
- * queued together are one chunk, up to 16 KiB. `hono/compress` holds a
1160
- * stream until it ends. Skips a response that is already encoded, a partial
1161
- * response, one marked `no-transform`, and one whose `Content-Length` is
1162
- * under 1 KiB. It adds `Accept-Encoding` to the `Vary` of each response that
1163
- * it can gzip. The host gives the gzip stream. On a host with none, the
1164
- * plugin does nothing.
1157
+ * Compresses text responses with brotli or gzip, and flushes after each
1158
+ * chunk, so a streamed page shell or a server-sent event reaches the client
1159
+ * at once. A client gets brotli when its `Accept-Encoding` ranks `br` at
1160
+ * 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
1162
+ * response that is already encoded, a partial response, one marked
1163
+ * `no-transform`, and one whose `Content-Length` is under 1 KiB. It adds
1164
+ * `Accept-Encoding` to the `Vary` of each response that it can compress. The
1165
+ * host gives the streams. On a host with none, the plugin does nothing.
1165
1166
  *
1166
1167
  * @example
1167
1168
  * createServer({ plugins: [requestId(), accessLog(), streamingCompress()] });
1168
1169
  */
1169
1170
  function streamingCompress() {
1170
- let gzip;
1171
+ /** The host's streams, in the order that the plugin prefers them. */
1172
+ let streams = /* @__PURE__ */ new Map();
1173
+ let offered = [];
1174
+ /** The encoding that an `Accept-Encoding` header prefers, with its stream. */
1175
+ const choose = (header) => {
1176
+ const encoding = preferredEncoding(header, offered);
1177
+ const stream = encoding == null ? void 0 : streams.get(encoding);
1178
+ return encoding == null || stream == null ? void 0 : {
1179
+ encoding,
1180
+ stream
1181
+ };
1182
+ };
1171
1183
  return {
1172
1184
  name: "streaming-compress",
1173
- setup: (info) => {
1174
- ({gzip} = info);
1185
+ setup: ({ brotli, gzip }) => {
1186
+ streams = /* @__PURE__ */ new Map();
1187
+ if (brotli != null) streams.set("br", brotli);
1188
+ if (gzip != null) streams.set("gzip", gzip);
1189
+ offered = [...streams.keys()];
1175
1190
  },
1176
1191
  middleware: async (context, next) => {
1177
- if (gzip == null) return next();
1178
- let gzipping = false;
1192
+ if (offered.length === 0) return next();
1193
+ let compressing = false;
1179
1194
  await withHeaderHook(context, next, (headers) => {
1180
1195
  const type = headers.get("Content-Type");
1181
1196
  const vary = type == null || isCompressible(type) ? withVary(headers.get("Vary"), "Accept-Encoding") : void 0;
1182
1197
  if (vary != null) headers.set("Vary", vary);
1183
- if (gzipping) headers.delete("Content-Length");
1198
+ if (compressing) headers.delete("Content-Length");
1184
1199
  });
1185
1200
  const response = context.res;
1186
1201
  const type = response.headers.get("Content-Type");
1187
1202
  if (type != null && !isCompressible(type)) return;
1188
1203
  const length = response.headers.get("Content-Length");
1189
1204
  const { body } = response;
1190
- if (type == null || body == null || context.req.method === "HEAD" || length != null && Number(length) < 1024 || preferredEncoding(context.req.header("Accept-Encoding"), ["gzip"]) == null || response.headers.has("Content-Encoding") || response.headers.has("Content-Range") || forbidsTransform(response.headers.get("Cache-Control"))) return;
1205
+ const chosen = choose(context.req.header("Accept-Encoding"));
1206
+ if (type == null || body == null || context.req.method === "HEAD" || length != null && Number(length) < 1024 || chosen == null || response.headers.has("Content-Encoding") || response.headers.has("Content-Range") || forbidsTransform(response.headers.get("Cache-Control"))) return;
1191
1207
  let source = body;
1192
1208
  let compress = true;
1193
1209
  if (length == null) {
@@ -1202,8 +1218,11 @@ function streamingCompress() {
1202
1218
  compress = !hooked.has("Content-Encoding") && !hooked.has("Content-Range") && !forbidsTransform(hooked.get("Cache-Control"));
1203
1219
  }
1204
1220
  if (compress) {
1205
- gzipping = true;
1206
- replaceResponse(context, gzipped(response, gzip(source), { hasLength: length != null }));
1221
+ compressing = true;
1222
+ replaceResponse(context, compressed(response, chosen.stream(source), {
1223
+ encoding: chosen.encoding,
1224
+ hasLength: length != null
1225
+ }));
1207
1226
  } else if (source !== body) replaceResponse(context, new Response(source, response));
1208
1227
  }
1209
1228
  };
@@ -1466,4 +1485,4 @@ function serverInternals(app) {
1466
1485
  }
1467
1486
 
1468
1487
  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 };
1469
- //# sourceMappingURL=create-server-jMl2ltHD.js.map
1488
+ //# sourceMappingURL=create-server-BX2rH6-E.js.map