mirror of
https://github.com/stablyai/orca.git
synced 2026-09-23 00:02:29 +00:00
* perf: avoid repeated pending SOCKS payload copies * test(browser): cover pipelined SOCKS request-tail ordering in pending buffer Also document takeBuffer's view-not-copy contract and restore node: import order. --------- Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local> Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { GrowingByteBuffer } from './growing-byte-buffer'
|
|
|
|
describe('GrowingByteBuffer', () => {
|
|
it('transfers binary bytes without changing them on clear or reuse', () => {
|
|
const buffer = new GrowingByteBuffer()
|
|
const bytes = Buffer.from([0, 255, 128, 10, 0])
|
|
buffer.append(bytes.subarray(0, 2))
|
|
buffer.append(bytes.subarray(2))
|
|
const taken = buffer.takeBuffer()
|
|
expect(taken).toEqual(bytes)
|
|
expect(buffer.byteLength).toBe(0)
|
|
expect(buffer.takeBuffer()).toEqual(Buffer.alloc(0))
|
|
buffer.append(Buffer.alloc(1024, 7))
|
|
buffer.clear()
|
|
expect(taken).toEqual(bytes)
|
|
})
|
|
|
|
it('retains 100,000 one-byte fragments in one growable allocation', () => {
|
|
const buffer = new GrowingByteBuffer()
|
|
const expected = Buffer.alloc(100_000)
|
|
|
|
for (let index = 0; index < expected.byteLength; index += 1) {
|
|
const value = index % 251
|
|
expected[index] = value
|
|
buffer.append(Uint8Array.of(value))
|
|
}
|
|
|
|
expect(buffer.byteLength).toBe(expected.byteLength)
|
|
expect(buffer.takeString('latin1')).toBe(expected.toString('latin1'))
|
|
expect(buffer.byteLength).toBe(0)
|
|
})
|
|
|
|
it('consumes delimited prefixes and retains a bounded suffix', () => {
|
|
const buffer = new GrowingByteBuffer()
|
|
for (const byte of Buffer.from('first\nsecond-tail')) {
|
|
buffer.append(Uint8Array.of(byte))
|
|
}
|
|
|
|
const newline = buffer.indexOfByte(0x0a)
|
|
expect(buffer.takePrefixString(newline)).toBe('first')
|
|
buffer.discardPrefix(1)
|
|
buffer.retainSuffix(4)
|
|
|
|
expect(buffer.toString()).toBe('tail')
|
|
})
|
|
|
|
it('appends only a bounded copy from an oversized source chunk', () => {
|
|
const buffer = new GrowingByteBuffer()
|
|
buffer.append(Buffer.from('old'))
|
|
const source = Buffer.from('discard-prefix-tail')
|
|
|
|
buffer.appendRetainedSuffix(source, 4)
|
|
source.fill(0)
|
|
|
|
expect(buffer.byteLength).toBe(4)
|
|
expect(buffer.toString()).toBe('tail')
|
|
})
|
|
|
|
it('keeps the newest bytes across bounded suffix appends', () => {
|
|
const buffer = new GrowingByteBuffer()
|
|
|
|
buffer.appendRetainedSuffix(Buffer.from('1234'), 6)
|
|
buffer.appendRetainedSuffix(Buffer.from('5678'), 6)
|
|
|
|
expect(buffer.toString()).toBe('345678')
|
|
})
|
|
})
|