@ngrok/app-server 0.3.2 → 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 (82) hide show
  1. package/README.md +63 -23
  2. package/dist/brotli-only-DrA29Ecw.js +97 -0
  3. package/dist/brotli-only-DrA29Ecw.js.map +1 -0
  4. package/dist/{build-id-C6sYO9Yp.js → build-id-B0448vZH.js} +27 -2
  5. package/dist/build-id-B0448vZH.js.map +1 -0
  6. package/dist/{build-manifest-UDNk81Ce.js → build-manifest-DBMWe3Lx.js} +12 -2
  7. package/dist/build-manifest-DBMWe3Lx.js.map +1 -0
  8. package/dist/cli.js +3 -3
  9. package/dist/client-browser.js +1 -1
  10. package/dist/client.js +1 -1
  11. package/dist/compress-JuJf2HaO.js +377 -0
  12. package/dist/compress-JuJf2HaO.js.map +1 -0
  13. package/dist/{compression-7_gToa8R.js → compression-CT2mWlKM.js} +9 -2
  14. package/dist/compression-CT2mWlKM.js.map +1 -0
  15. package/dist/{create-server-D0De6R7Q.js → create-server-BX2rH6-E.js} +616 -547
  16. package/dist/create-server-BX2rH6-E.js.map +1 -0
  17. package/dist/{create-server-kzZWd_Lo.d.ts → create-server-kxyhCdhv.d.ts} +50 -13
  18. package/dist/{entry-DuGgSKeB.d.ts → entry-CNL4OsEn.d.ts} +39 -3
  19. package/dist/entry-ChHgs269.js +352 -0
  20. package/dist/entry-ChHgs269.js.map +1 -0
  21. package/dist/{index-Bo5G9LuQ.d.ts → index-B8LsM6Mh.d.ts} +26 -1
  22. package/dist/index.d.ts +14 -23
  23. package/dist/index.js +3 -3
  24. package/dist/{load-entry-Bodyd2Ok.js → load-entry-BOgJPt1I.js} +4 -4
  25. package/dist/{load-entry-Bodyd2Ok.js.map → load-entry-BOgJPt1I.js.map} +1 -1
  26. package/dist/{node-C1qudLs_.js → node-B0AYf_IA.js} +5 -4
  27. package/dist/{node-C1qudLs_.js.map → node-B0AYf_IA.js.map} +1 -1
  28. package/dist/{node-tBVQPjQv.js → node-C0Ad7CVL.js} +2 -2
  29. package/dist/{node-tBVQPjQv.js.map → node-C0Ad7CVL.js.map} +1 -1
  30. package/dist/node-vite.d.ts +1 -1
  31. package/dist/node.d.ts +1 -1
  32. package/dist/node.js +2 -2
  33. package/dist/react-router-vite.d.ts +1 -1
  34. package/dist/react-router.d.ts +3 -2
  35. package/dist/react-router.js +31 -2
  36. package/dist/react-router.js.map +1 -1
  37. package/dist/{response-writer-B7FoJjEE.js → response-writer-DzvUAz9x.js} +3 -3
  38. package/dist/response-writer-DzvUAz9x.js.map +1 -0
  39. package/dist/{script-hashes-C6hQ5XnP.js → script-hashes-CU7P9oZU.js} +2 -2
  40. package/dist/{script-hashes-C6hQ5XnP.js.map → script-hashes-CU7P9oZU.js.map} +1 -1
  41. package/dist/{serve-caOkS3o5.js → serve-BUJ7ZonG.js} +2 -2
  42. package/dist/{serve-caOkS3o5.js.map → serve-BUJ7ZonG.js.map} +1 -1
  43. package/dist/{stale-build-navigation-PnMLtTF8.js → stale-build-navigation-rg5xxmTr.js} +2 -2
  44. package/dist/{stale-build-navigation-PnMLtTF8.js.map → stale-build-navigation-rg5xxmTr.js.map} +1 -1
  45. package/dist/{start-DREqYrZh.js → start-DiQf9ttp.js} +36 -8
  46. package/dist/start-DiQf9ttp.js.map +1 -0
  47. package/dist/{static-files-Beju9riB.js → static-files-C2ea8KQ6.js} +11 -3
  48. package/dist/static-files-C2ea8KQ6.js.map +1 -0
  49. package/dist/tanstack-start-vite.d.ts +1 -1
  50. package/dist/tanstack-start.d.ts +2 -2
  51. package/dist/{tcp-Dd1Mt6sN.js → tcp-Bws6oEau.js} +2 -2
  52. package/dist/{tcp-Dd1Mt6sN.js.map → tcp-Bws6oEau.js.map} +1 -1
  53. package/dist/testing.d.ts +2 -2
  54. package/dist/testing.js +4 -4
  55. package/dist/{trailing-slash-BrymAEGv.d.ts → trailing-slash-DeOUxMYo.d.ts} +2 -2
  56. package/dist/{types-CEvzAmcn.d.ts → types-BKJxtIuj.d.ts} +24 -3
  57. package/dist/vercel-pages.d.ts +32 -0
  58. package/dist/vercel-pages.js +54 -0
  59. package/dist/vercel-pages.js.map +1 -0
  60. package/dist/vercel-vite.d.ts +30 -9
  61. package/dist/vercel-vite.js +183 -20
  62. package/dist/vercel-vite.js.map +1 -1
  63. package/dist/vercel.d.ts +4 -3
  64. package/dist/vercel.js +7 -5
  65. package/dist/vercel.js.map +1 -1
  66. package/dist/{vite-tTxVimP6.js → vite-DPiSKHDn.js} +182 -51
  67. package/dist/vite-DPiSKHDn.js.map +1 -0
  68. package/dist/vite.d.ts +1 -1
  69. package/dist/vite.js +1 -1
  70. package/package.json +2 -1
  71. package/dist/build-id-C6sYO9Yp.js.map +0 -1
  72. package/dist/build-manifest-UDNk81Ce.js.map +0 -1
  73. package/dist/compression-7_gToa8R.js.map +0 -1
  74. package/dist/create-server-D0De6R7Q.js.map +0 -1
  75. package/dist/entry-UDsxuqBC.js +0 -116
  76. package/dist/entry-UDsxuqBC.js.map +0 -1
  77. package/dist/gzip-Doi4wqjX.js +0 -294
  78. package/dist/gzip-Doi4wqjX.js.map +0 -1
  79. package/dist/response-writer-B7FoJjEE.js.map +0 -1
  80. package/dist/start-DREqYrZh.js.map +0 -1
  81. package/dist/static-files-Beju9riB.js.map +0 -1
  82. package/dist/vite-tTxVimP6.js.map +0 -1
@@ -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
@@ -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,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;IAC7B,IAAI,OAAO;KACV,KAAK,IAAI;KACT;IACD;IACA,OAAO,OAAO;IAGd,YAAY,UAAU;GACvB,SAAS,OAAO;IACf,OAAO,OAAO;IACd,MAAM,QAAQ;IACd,MAAM;GACP;EACD;EAEA,MAAM,OAAO,QAAQ;GACpB,MAAM,QAAQ;GAEd,QAAQ,OAAO,QAAQ;GACvB,OAAO,OAAO,OAAO,MAAM;EAC5B;CACD,GAEA,EAAE,eAAe,EAAE,CACpB;CACA,OAAO;AACR;AAEA,MAAM,OAAc;CACnB,cAAc,WAAW,EAAE,OAAO,UAAU,aAAa,CAAC;CAC1D,OAAO;CACP,MAAM;AACP;;;;;;;;;;;;;;AAeA,SAAgB,WAAW,QAAgE;CAC1F,OAAO,WAAW,QAAQ,IAAI;AAC/B;AAEA,MAAM,SAAgB;CACrB,cACC,qBAAqB;EAAE,OAAO,UAAU;EAAwB,QAAQ;CAAc,CAAC;CACxF,OAAO,mCAAmB,IAAI,WAAW,GAAG,EAAE,QAAQ,cAAc,CAAC;CACrE,MAAM;AACP;;;;;;;;;;AAWA,SAAgB,aAAa,QAAgE;CAC5F,OAAO,WAAW,QAAQ,MAAM;AACjC"}
@@ -1,7 +1,14 @@
1
1
  import { parseAccept } 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,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