| 698 | |
| 699 | // One promise per in-memory entry costs less than a stream |
| 700 | const deflateBytes = async (bytes: Uint8Array): Promise<Uint8Array> => { |
| 701 | const compressor = new CompressionStream("deflate-raw") |
| 702 | const writer = compressor.writable.getWriter() |
| 703 | // A failed write errors the readable, so the reads report it |
| 704 | if (bytes.length > 0) writer.write(compressible(bytes)).catch(() => {}) |
| 705 | writer.close().catch(() => {}) |
| 706 | const reader = compressor.readable.getReader() |
| 707 | const chunks: Array<Uint8Array> = [] |
| 708 | let length = 0 |
| 709 | while (true) { |
| 710 | const { done, value } = await reader.read() |
| 711 | if (done) break |
| 712 | chunks.push(value) |
| 713 | length += value.length |
| 714 | } |
| 715 | return concat(chunks, length) |
| 716 | } |
| 717 | |
| 718 | const concat = (chunks: ReadonlyArray<Uint8Array>, length: number): Uint8Array => { |
| 719 | if (chunks.length === 1) return chunks[0] |