Infohub JS API
Loading...
Searching...
No Matches
JsonStream Class Reference

Description

Streaming JSON for payloads that exceed V8's max string length (0x1fffffe8 ≈ 512 MiB).

A single JS string can never hold such a payload, so JSON.parse(wholeString) / JSON.stringify(wholeObject) throw "Invalid string length". These helpers parse from / serialize to chunks and never materialize the payload as one giant string — the resulting object lives in memory (objects are not size-capped, only strings).

Environment-agnostic: works in the browser (Blob/File, ReadableStream, fetch Response) and in Node (fs read streams are async-iterable). No fs import, so it bundles safely for the browser.

Static Public Member Functions

static async parse (source)
static * stringifyChunks (value, chunkSize=1<< 20)

Function Documentation

◆ parse()

async parse ( source)
static

Parse a JSON payload of any size into a JS value without ever holding it as a single string.

Accepts:

  • a Blob / File (browser file upload) → uses .stream()
  • a WHATWG ReadableStream (file.stream(), Response.body)
  • a Response (fetch result) → uses .body
  • a Node Readable stream / any async-iterable of chunks
  • a Uint8Array / string (already-in-memory buffer)

Chunks may be Uint8Array (incl. Node Buffer) or string.

Parameters
{Blob|File|ReadableStream|Response|AsyncIterable|Iterable|Uint8Array|string}source
Returns
{Promise<any>} the parsed value

◆ stringifyChunks()

* stringifyChunks ( value,
chunkSize = 1 << 20 )
static

Serialize a JS value to a sequence of string chunks (each well under the string-length cap) without ever building one giant string.

Walks the value and only ever calls JSON.stringify() on individual leaves / keys.

Consume it however the environment needs:

  • Node : for (const c of JsonStream.stringifyChunks(v)) ws.write(c)
  • Browser download : new Blob([...JsonStream.stringifyChunks(v)])

Matches JSON.stringify semantics: undefined/function values are dropped from objects and rendered as null in arrays.

Parameters
{any}value
{number}[chunkSize=1048576] flush threshold in chars (~1 MiB)
Returns
{Generator<string>}