Implementation:Openai Openai node LineDecoder
| Knowledge Sources | |
|---|---|
| Domains | SDK, Streaming |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
LineDecoder is an internal utility class that incrementally decodes byte streams into individual text lines, handling various newline conventions across platforms.
Description
The LineDecoder class is a re-implementation of httpx's LineDecoder from Python, adapted for TypeScript. It operates on an internal byte buffer, accepting chunks of data as strings, ArrayBuffer, or Uint8Array and splitting them into discrete lines based on newline characters (\n, \r, or \r\n).
The class handles edge cases such as carriage return characters that may or may not be followed by a newline character, buffering partial lines across multiple decode() calls until a complete line boundary is found. This is essential for server-sent events (SSE) parsing and streaming API responses where data arrives in arbitrary-sized chunks that do not necessarily align with line boundaries.
The module also exports a findDoubleNewlineIndex helper function that locates double newline patterns (\r\r, \n\n, or \r\n\r\n) in a byte buffer, which is used to detect the boundary between HTTP headers and body content in streaming responses.
Usage
Use LineDecoder when you need to process streaming byte data and extract complete lines as they arrive. This is primarily used internally by the SDK's streaming infrastructure to parse SSE responses from the OpenAI API. You would typically not interact with this class directly unless building custom streaming parsers.
Code Reference
Source Location
- Repository: openai-node
- File: src/internal/decoders/line.ts
Signature
export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
export class LineDecoder {
static NEWLINE_CHARS: Set<string>;
static NEWLINE_REGEXP: RegExp;
constructor();
decode(chunk: Bytes): string[];
flush(): string[];
}
export function findDoubleNewlineIndex(buffer: Uint8Array): number;
Import
import { LineDecoder, findDoubleNewlineIndex } from 'openai/internal/decoders/line';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| chunk | ArrayBuffer | Uint8Array | null | undefined) | Yes | A chunk of byte data to decode into lines. Null or undefined inputs return an empty array. |
Outputs
| Name | Type | Description |
|---|---|---|
| lines | string[] |
Array of complete lines extracted from the buffered input. Partial lines are buffered until the next decode call provides a newline boundary. |
Usage Examples
const decoder = new LineDecoder();
// Feed chunks of data incrementally
const lines1 = decoder.decode('Hello\nWorld');
// lines1 = ['Hello']
const lines2 = decoder.decode('\nFoo');
// lines2 = ['World']
// Flush remaining buffered content
const remaining = decoder.flush();
// remaining = ['Foo']
// Using findDoubleNewlineIndex to locate header-body boundary
import { findDoubleNewlineIndex } from 'openai/internal/decoders/line';
const encoder = new TextEncoder();
const buffer = encoder.encode('HTTP/1.1 200 OK\r\n\r\nBody content');
const bodyStart = findDoubleNewlineIndex(buffer);
// bodyStart will point to the index right after \r\n\r\n