1
0

residual-detach.spec.ts 1.4 KB

1234567891011121314151617181920212223242526272829
  1. import { describe, expect, it } from 'vitest'
  2. import { detachResidual } from '../src/index.ts'
  3. describe('detachResidual — fd-3 residual detachment', () => {
  4. it('returns a copy that does NOT share the source frame allocation', () => {
  5. // Simulate the data handler's state: one large joined frame from
  6. // Buffer.concat, sliced past its newline to leave a small residual VIEW.
  7. const joined = Buffer.alloc(1024 * 1024, 0x61) // 1 MiB backing allocation
  8. joined[512] = 0x0a // a newline partway through
  9. const residual = joined.subarray(513) // a view onto `joined`'s backing store
  10. // Before the fix the handler carried this view forward verbatim, pinning the
  11. // whole 1 MiB `joined` allocation behind a residual that reports far fewer
  12. // bytes. A right-sized copy must not point back into `joined`.
  13. const [carried] = detachResidual(residual)
  14. expect(carried).toBeDefined()
  15. expect(carried!.length).toBe(residual.length)
  16. expect(carried!.equals(residual)).toBe(true)
  17. // The copy's backing store is its own, sized to its content — not the 1 MiB
  18. // frame. A subarray view would report the source's full byteLength here.
  19. expect(carried!.buffer.byteLength).toBe(carried!.length)
  20. expect(carried!.buffer).not.toBe(joined.buffer)
  21. })
  22. it('carries nothing forward for an empty residual', () => {
  23. expect(detachResidual(Buffer.alloc(0))).toEqual([])
  24. })
  25. })