function

bun.JSONL.parseChunk

function parseChunk(
input: string | ArrayBufferLike | TypedArray<ArrayBufferLike> | DataView<ArrayBufferLike>,
start?: number,
end?: number

Parse a JSONL chunk, designed for streaming use.

Never throws on parse errors. Instead, returns whatever values were successfully parsed along with an error property containing the SyntaxError (or null on success). Use read to determine how much input was consumed and done to check if all input was parsed.

When a TypedArray is passed, the bytes are parsed directly without copying if the content is ASCII. Optional start and end parameters select a window of the input without copying. For typed arrays these are byte offsets and read is a byte offset into the original typed array. For strings these are character offsets and read is a character offset into the original string.

@param input

The JSONL string or typed array to parse

@param start

Offset to start parsing from (bytes for typed arrays, characters for strings, default: 0)

@param end

Offset to stop parsing at (bytes for typed arrays, characters for strings, default: input length)

@returns

An object with values, read, done, and error properties

let buffer = new Uint8Array(0);
for await (const chunk of stream) {
  buffer = Buffer.concat([buffer, chunk]);
  const { values, read, error } = Bun.JSONL.parseChunk(buffer);
  if (error) throw error;
  for (const value of values) handle(value);
  buffer = buffer.subarray(read);
}