FparseChunk
Bun

function

JSONL.parseChunk

function parseChunk(
input: string

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 allow slicing without copying, and read will be a byte offset into the original typed array.

@param input

The JSONL string or typed array to parse

@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);
}
function parseChunk(
input: ArrayBufferLike | TypedArray<ArrayBufferLike> | DataView<ArrayBuffer>,
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 allow slicing without copying, and read will be a byte offset into the original typed array.

@param input

The JSONL string or typed array to parse

@param start

Byte offset to start parsing from (typed array only, default: 0)

@param end

Byte offset to stop parsing at (typed array only, default: input.byteLength)

@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);
}

Referenced types

interface ParseChunkResult

The result of Bun.JSONL.parseChunk.

  • done: boolean

    true if all input was consumed successfully. false if the input ends with an incomplete value or a parse error occurred.

  • error: null | SyntaxError

    A SyntaxError if a parse error occurred, otherwise null. Values parsed before the error are still available in values.

  • read: number

    How far into the input was consumed. When the input is a string, this is a character offset. When the input is a TypedArray, this is a byte offset. Use input.slice(read) or input.subarray(read) to get the unconsumed remainder.

  • values: unknown[]

    The successfully parsed JSON values.

class ArrayBuffer

Represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.

  • readonly [Symbol.toStringTag]: string
  • readonly byteLength: number

    Read-only. The length of the ArrayBuffer (in bytes).

  • newByteLength?: number
    ): void;

    Resizes the ArrayBuffer to the specified size (in bytes).

    MDN

    byteLength: number

    Resize an ArrayBuffer in-place.

  • begin: number,
    end?: number

    Returns a section of an ArrayBuffer.

  • newByteLength?: number

    Creates a new ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

    MDN

  • newByteLength?: number

    Creates a new non-resizable ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

    MDN