Overview
Concrete tool for NDJSON line framing over byte streams, provided by the TypeScript SDK.
Description
The framing module implements the wire-level NDJSON protocol shared between the SDK client and the jcode server. The NdjsonDecoder class keeps a remainder buffer and only emits complete lines, because socket chunks can split frames anywhere. Blank lines are skipped, matching the Rust reader on the server side. The encodeFrame() function serializes any value as a JSON string followed by a newline, producing the framing for outbound messages.
Usage
- Streaming protocol: Use
NdjsonDecoder to parse incoming socket data incrementally.
- Outbound messages: Use
encodeFrame() to serialize request objects before writing to the socket.
- Any harness API transport: This is the framing layer underneath the client's request/response cycle.
Code Reference
Source Location
Signature
export class NdjsonDecoder {
private buffer: string;
push(chunk: Buffer | string): unknown[];
}
export function encodeFrame(frame: unknown): string;
Import
import { NdjsonDecoder, encodeFrame } from "@1jehuang/jcode/framing";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| chunk |
string |
Yes |
Raw byte chunk received from a socket
|
| frame |
unknown |
Yes (encodeFrame) |
Any JSON-serializable value to send
|
Outputs
| Name |
Type |
Description
|
| push() returns |
unknown[] /// Array of fully parsed JSON objects from complete lines
|
| encodeFrame() returns |
string /// JSON string terminated by a newline character
|
Usage Examples
Decode Incoming Socket Data
import { NdjsonDecoder } from "@1jehuang/jcode/framing";
import { createConnection } from "node:net";
const decoder = new NdjsonDecoder();
const socket = createConnection(socketPath);
socket.on("data", (chunk) => {
const messages = decoder.push(chunk);
for (const msg of messages) {
console.log("Received:", msg);
}
});
Encode and Send a Frame
import { encodeFrame } from "@1jehuang/jcode/framing";
const request = { type: "run", prompt: "hello world" };
socket.write(encodeFrame(request));
// Wire format: {"type":"run","prompt":"hello world"}\n
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.