edge-json-stream
v1.0.0
Published
Hyper-efficient, zero-dependency JSON array streaming parser for browser and modern Edge runtimes like Cloudflare Workers and Vercel Edge.
Maintainers
Readme
edge-json-stream
Memory-safe JSON array streaming over the native Web Streams API for Edge runtimes.
The Memory Problem at the Edge
Standard JSON parsing approaches—such as calling await response.json() or using JSON.parse()—require buffering the entire payload into RAM. In memory-constrained serverless environments (like Cloudflare Workers with a 128MB ceiling or Vercel Edge Functions), parsing arrays larger than 10MB-100MB+ can trigger catastrophic Out-Of-Memory (OOM) failures.
Furthermore, traditional JSON streaming libraries rely heavily on legacy Node.js stream APIs. These modules are unsupported by strict modern Edge environments that exclusively implement browser-native Web APIs.
edge-json-stream solves this by consuming a raw ReadableStream<Uint8Array> and parsing it chunk-by-chunk using a lightweight, character-by-character state machine. It streams objects to your logic as they arrive, ensuring memory overhead remains completely flat, regardless of stream length.
Core Features
- Zero Runtime Dependencies — Restricts all packaging, building, and validation tools strictly to development environments.
- Edge-Native Architecture — Relies exclusively on standard Web APIs (
TextDecoderandReadableStream), making it fully compatible with Cloudflare Workers, Vercel Edge, Bun, Deno, AWS Lambda, Node.js, and modern browsers. - Truncation & Buffer Resiliency — State machine seamlessly tracks and buffers partial strings, numbers, escaped quotes, and object braces across raw network packet boundaries.
- Inferable TypeScript Generics — Fully-typed async generator yield types for end-to-end type safety and autocomplete.
Installation
# npm
npm install edge-json-stream
# pnpm
pnpm add edge-json-stream
# bun
bun add edge-json-streamQuick Start
import { parseJsonArrayStream } from "edge-json-stream";
interface TransactionRecord {
id: string;
amount: number;
status: "pending" | "completed" | "failed";
timestamp: string;
}
async function processTransactions() {
const response = await fetch("https://api.example.com/massive-transactions.json");
if (!response.body) {
throw new Error("API response is missing readable body stream");
}
// The stream is processed on-the-fly. Memory remains flat and minimal (< 100 KB).
const stream = parseJsonArrayStream<TransactionRecord>(response.body);
// Yields fully typed TransactionRecord objects one by one before the download completes.
for await (const record of stream) {
console.log(`Transaction ${record.id}: $${record.amount} is ${record.status}`);
}
}
processTransactions().catch(console.error);API Signature Matrix
| Function | Parameter | Returns | Execution Properties |
| :--- | :--- | :--- | :--- |
| parseJsonArrayStream<T = any> | stream: ReadableStream<Uint8Array> | AsyncGenerator<T, void, unknown> | Non-blocking, zero-dependency, character-by-character stream consumption. |
Advanced Resiliency Modes
The parser runs a single-pass state machine that maintains safety under standard streaming anomalies:
- Chunk Cut-off Reassembly: If a network packet boundary cuts directly through a key name, a numeric value, or a string literal (e.g.
{"amount": 12...5.50}), the unparsed fragment is held in a local buffer and resolved correctly when the next chunk is appended. - String Escape Safety: The state machine tracks backslash escapes (e.g.
\"or\\) within string literals, preventing literal quotes or nested brackets from corrupting the internal bracket counter. - Brace & Bracket Balancing: Tracks curly braces
{and}balance outside of string literals to isolate root-level objects, while safely bypassing array wrappers ([and]), spacing, and trailing commas.
Performance Benchmark
| Metric | JSON.parse / response.json() | edge-json-stream |
| :--- | :--- | :--- |
| Bundle Footprint | 0 KB (Native) | < 1.5 KB (Gzipped, tree-shakable) |
| Memory scaling | Linear (scales with payload size) ❌ | Flatline (constant <100 KB) ✅ |
| Edge Runtime Compatible | Yes ✅ | Yes (100% standards-based) ✅ |
| Streaming Output | No (blocking) ❌ | Yes (AsyncGenerator) ✅ |
License & Contributing
Licensed under the MIT License. Contributions are welcome—feel free to open an issue or submit a pull request on GitHub.
