Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1jehuang Jcode NdjsonDecoder EncodeFrame

From Leeroopedia
Knowledge Sources
Domains SDK, Networking
Last Updated 2026-10-18 00:00 GMT

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.
Principle
Implementation
Heuristic
Environment