Files
faunosteo/apps/app/tests/unit/pdf.attachment.inflate.test.ts
T

29 lines
1.0 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { inflateFlate } from '@/pdf/attachment'
async function deflate(data: Uint8Array): Promise<Uint8Array> {
const stream = new Blob([data as BlobPart])
.stream()
.pipeThrough(new CompressionStream('deflate'))
return new Uint8Array(await new Response(stream).arrayBuffer())
}
describe('inflateFlate decompression cap', () => {
it('inflates a normal-size payload', async () => {
const payload = new TextEncoder().encode(
JSON.stringify({ version: 2, hello: 'world' }),
)
const out = await inflateFlate(await deflate(payload))
expect(out).not.toBeNull()
expect(new TextDecoder().decode(out!)).toContain('hello')
})
it('returns null for a decompression bomb above the cap', async () => {
// 24 MB of zeros deflates to a few KB but inflates past the 16 MB cap
const bomb = await deflate(new Uint8Array(24 * 1024 * 1024))
expect(bomb.byteLength).toBeLessThan(1024 * 1024)
const out = await inflateFlate(bomb)
expect(out).toBeNull()
})
})