FrameBuffer
Defined in: packages/protocol/src/frame-buffer.ts:234
High-Performance FrameBuffer with Streaming Support.
TWO MODES OF OPERATION:
-
BATCH MODE (default):
- Accumulates chunks in memory (zero-copy list)
- Returns Frame[] when complete frame(s) available
- Good for small/medium payloads
- Simple API: frames = buffer.push(chunk)
-
STREAMING MODE:
- Delivers payload chunks via callbacks AS THEY ARRIVE
- Minimal memory footprint (only one chunk at a time)
- Good for large payloads (100MB+), file transfers
- Callback API: buffer.setStreamHandler(handler)
PERFORMANCE:
- Accumulation is O(1) per chunk (no Buffer.concat!)
- Memory usage in batch mode: O(payload size)
- Memory usage in streaming mode: O(chunk size) = ~64KB
Examples
Section titled “Examples”const buffer = new FrameBuffer();
socket.on('data', (chunk) => { const frames = buffer.push(chunk); for (const frame of frames) { // Process complete frame const data = codec.deserialize(frame.payload); }});const buffer = new FrameBuffer();
buffer.setStreamHandler({ onFrameStart(header) { this.fd = fs.openSync('output.bin', 'w'); }, onPayloadChunk(chunk, offset, isLast) { fs.writeSync(this.fd, chunk); // Write immediately! }, onFrameEnd(header) { fs.closeSync(this.fd); },});
socket.on('data', (chunk) => buffer.push(chunk));Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new FrameBuffer(
options):FrameBuffer
Defined in: packages/protocol/src/frame-buffer.ts:273
Create a new FrameBuffer.
Parameters
Section titled “Parameters”options
Section titled “options”FrameBufferOptions = {}
Configuration options
Returns
Section titled “Returns”FrameBuffer
Example
Section titled “Example”// Default 1GB limitconst buffer = new FrameBuffer();
// Custom limit for large transfersconst buffer = new FrameBuffer({ maxPayloadSize: 2 * 1024 * 1024 * 1024 - 1 });
// Strict limit for control channelconst buffer = new FrameBuffer({ maxPayloadSize: 1024 * 1024 }); // 1MBAccessors
Section titled “Accessors”bufferedBytes
Section titled “bufferedBytes”Get Signature
Section titled “Get Signature”get bufferedBytes():
number
Defined in: packages/protocol/src/frame-buffer.ts:589
Number of bytes currently buffered.
Returns
Section titled “Returns”number
hasPartialFrame
Section titled “hasPartialFrame”Get Signature
Section titled “Get Signature”get hasPartialFrame():
boolean
Defined in: packages/protocol/src/frame-buffer.ts:596
Check if there’s a partial frame in the buffer.
Returns
Section titled “Returns”boolean
isStreaming
Section titled “isStreaming”Get Signature
Section titled “Get Signature”get isStreaming():
boolean
Defined in: packages/protocol/src/frame-buffer.ts:309
Check if streaming mode is enabled.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”clear()
Section titled “clear()”clear():
void
Defined in: packages/protocol/src/frame-buffer.ts:603
Clear the buffer. Use when connection resets.
Returns
Section titled “Returns”void
push()
Section titled “push()”push(
chunk):Frame[]
Defined in: packages/protocol/src/frame-buffer.ts:322
Push a chunk of bytes and extract any complete frames.
BATCH MODE: Returns Frame[] when complete frame(s) available. STREAMING MODE: Calls handler callbacks, always returns [].
Parameters
Section titled “Parameters”Buffer
Bytes received from socket
Returns
Section titled “Returns”Frame[]
Array of complete frames (empty in streaming mode)
setStreamHandler()
Section titled “setStreamHandler()”setStreamHandler(
handler):void
Defined in: packages/protocol/src/frame-buffer.ts:296
Enable streaming mode.
In streaming mode:
- Payload chunks are delivered via callbacks AS THEY ARRIVE
- Memory footprint is minimal (one chunk at a time)
- push() always returns [] (frames delivered via callbacks)
Call with null to disable streaming mode and return to batch mode.
Parameters
Section titled “Parameters”handler
Section titled “handler”Streaming callbacks, or null to disable
FrameStreamHandler | null
Returns
Section titled “Returns”void