/** * The capped inflater: round trips, the exact limit, every failure mode as * null, the bomb cancelled early, and the compressed bytes fed to the * platform inflater in slices (the Chromium read-ahead bound; Node cannot * show the memory effect, only the feeding shape). */ import { deflateSync } from 'node:zlib' import { afterEach, describe, expect, it, vi } from 'vitest' import { inflateFlate, MAX_ATTACHMENT_BYTES } from '../src/attachment/inflate' const KiB = 1024 const MiB = 1024 * KiB /** Incompressible bytes: the compressed stream is about as long as the input. */ function noise(length: number): Uint8Array { let seed = 0x9e3779b9 return Uint8Array.from({ length }, () => { seed = (seed * 1664525 + 1013904223) >>> 0 return seed >>> 24 }) } const deflate = (data: Uint8Array) => new Uint8Array(deflateSync(data)) afterEach(() => { vi.restoreAllMocks() }) describe('inflateFlate', () => { it('round-trips a payload spanning several slices', async () => { const payload = noise(200 * KiB) const compressed = deflate(payload) expect(compressed.byteLength).toBeGreaterThan(10 * 16 * KiB) expect(await inflateFlate(compressed)).toEqual(payload) }) it('feeds the inflater in 16 KiB slices, each awaited', async () => { const compressed = deflate(noise(100 * KiB)) const write = vi.spyOn(WritableStreamDefaultWriter.prototype, 'write') await inflateFlate(compressed) const slices = Math.ceil(compressed.byteLength / (16 * KiB)) expect(write).toHaveBeenCalledTimes(slices) const sizes = write.mock.calls.map((c) => (c[0] as Uint8Array).byteLength) expect(Math.max(...sizes)).toBe(16 * KiB) }) it('an output exactly at the limit passes, one byte more is null', async () => { const payload = noise(64 * KiB) const compressed = deflate(payload) expect(await inflateFlate(compressed, payload.byteLength)).toEqual(payload) expect(await inflateFlate(compressed, payload.byteLength - 1)).toBeNull() }) it.each([ ['empty input', new Uint8Array(0)], ['truncated stream', deflate(noise(4 * KiB)).subarray(0, 2 * KiB)], ['trailing junk', Uint8Array.from([...deflate(noise(KiB)), 1, 2, 3])], [ 'corrupt body', Uint8Array.from(deflate(noise(KiB)), (b, i) => (i > 8 ? b ^ 0xff : b)), ], ['not deflate at all', new TextEncoder().encode('{"version":3}')], ])('%s: null', async (_name, bytes) => { expect(await inflateFlate(bytes)).toBeNull() }) it('a 64 MiB bomb past the default cap is cancelled in well under a second', async () => { const bomb = deflate(new Uint8Array(64 * MiB)) expect(bomb.byteLength).toBeLessThan(MiB) const started = performance.now() expect(await inflateFlate(bomb)).toBeNull() const elapsed = performance.now() - started console.log(`[inflate] 64 MiB bomb: ${elapsed.toFixed(0)} ms`) expect(elapsed).toBeLessThan(1000) expect(MAX_ATTACHMENT_BYTES).toBe(16 * MiB) }) })