document-bytes.client.spec.ts 1.8 KB

12345678910111213141516171819202122232425262728293031
  1. import { spawnSync } from 'node:child_process'
  2. import { expect, it } from 'vitest'
  3. import { documentFileBytes } from '../src/client/rpc.ts'
  4. const source = { absolutePath: '/report.pdf', version: 'v1', offset: 0, eof: true, missingFonts: ['Missing Serif'] }
  5. it('preserves every byte value, source metadata, font notices, padding, and empty input', () => {
  6. for (const size of [0, 1, 2, 256, 257]) {
  7. const input = Uint8Array.from({ length: size }, (_, index) => index % 256)
  8. expect(documentFileBytes({ ...source, data: Buffer.from(input).toString('base64') }))
  9. .toEqual({ ...source, data: input })
  10. }
  11. expect(() => documentFileBytes({ ...source, data: '!invalid!' })).toThrow()
  12. })
  13. it('decodes a large response within a bounded JavaScript heap', () => {
  14. // Typed buffers fit this heap; expanding the binary string into JS elements does not.
  15. const decoder = new URL('../src/client/rpc.ts', import.meta.url).href
  16. const child = spawnSync(process.execPath, ['--max-old-space-size=96', '--import', 'tsx/esm', '--input-type=module', '-e', `
  17. import { documentFileBytes } from ${JSON.stringify(decoder)}
  18. const input = Buffer.alloc(12 * 1024 * 1024)
  19. for (let index = 0; index < input.length; index++) input[index] = index % 256
  20. const output = documentFileBytes({ ...${JSON.stringify(source)}, data: input.toString('base64') }).data
  21. if (!Buffer.from(output).equals(input)) throw new Error('Decoded PDF bytes differ')
  22. process.stdout.write(String(output.length))
  23. `], { encoding: 'utf8', timeout: 30_000, maxBuffer: 1024 * 1024, env: { ...process.env, NODE_OPTIONS: '' } })
  24. expect(child.error).toBeUndefined()
  25. expect(child.signal, child.stderr).toBeNull()
  26. expect(child.status, child.stderr).toBe(0)
  27. expect(child.stdout).toBe(String(12 * 1024 * 1024))
  28. })