@cacheplane/json-stream

中文文档
0.1.1发布于 2026/10/01MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1.2万

Streaming incremental JSON parser with push/pull APIs, node guards, and JSON Pointer resolution.

原始使用文档
文档目录8 节
  1. Install
  2. 30-Second Example
  3. Mental Model
  4. Structured Errors
  5. Type Guards
  6. JSON Pointer Lookup
  7. Related
  8. License

@cacheplane/json-stream

Incremental JSON parser for input that arrives a chunk at a time.

Use it when an AI model, network stream, worker, or tool call emits JSON progressively. JSON.parse() needs a complete document; this parser gives you a typed tree while the document is still arriving, marking each node partial or complete so you can render what has landed and leave the rest.

Install

npm install @cacheplane/json-stream

Runtime and packaging:

  • Node >=20
  • TypeScript declarations included
  • ESM and CJS bundled
  • Zero runtime dependencies
  • Marked side-effect free

30-Second Example

import { create, push, finish, isObjectNode } from "@cacheplane/json-stream";

let state = create();

for (const chunk of ['{"name": "Ada', '", "age": 3', "6}"]) {
  state = push(state, chunk);
}

state = finish(state);

if (isObjectNode(state.root)) {
  // Every node carries a status, so a half-arrived string is still readable.
  console.log(state.root.status); // "complete"
}

Mental Model

Three functions drive the whole parser, and each returns a new StreamState rather than mutating the one you passed:

  • create() — a fresh, empty state.
  • push(state, chunk) — feed the next chunk; safe to call at any byte boundary, including mid-token and mid-escape-sequence.
  • finish(state) — signal end of input. Anything still open is resolved or reported as a StreamError.

Every node in the tree carries a status of partial or complete. That is the distinction the whole library exists for: a consumer can render a string that is still arriving, and know not to treat it as final.

Structured Errors

Resolved object values preserve JSON keys as own enumerable, writable, configurable data properties on ordinary objects, including the empty key and names such as __proto__, constructor, and toString. Duplicate keys keep the last value, matching JSON.parse. Defining a JSON key does not change the result object's prototype.

When parsing fails, state.error contains a StreamError with source location, a human-readable message, and a stable code from the public StreamErrorCode union:

type StreamErrorCode =
  | "INVALID_SYNTAX"
  | "UNEXPECTED_END"
  | "TRAILING_CONTENT";
  • INVALID_SYNTAX means a token or JSON grammar rule is malformed, including a completed primitive that cannot parse.
  • UNEXPECTED_END means finish() found input that ended while a value or container was still an incrementally valid prefix. This includes number prefixes such as -, 1., and 1e+, which could become valid with more input.
  • TRAILING_CONTENT means more non-whitespace input arrived after the root JSON value was complete.

Use code for control flow. Messages remain useful for people and diagnostics, but their exact wording is not part of the control-flow contract. Error states are terminal: later push() and finish() calls preserve the same structured error state.

Type Guards

Narrowing helpers for each node kind, plus a status check:

import {
  isArrayNode,
  isBoolNode,
  isComplete,
  isNullNode,
  isNumberNode,
  isObjectNode,
  isStringNode,
} from "@cacheplane/json-stream";

isComplete(node) is the one to reach for when deciding whether a value is safe to commit downstream.

JSON Pointer Lookup

resolve reads a node out of the tree by RFC 6901 pointer, which is convenient when you care about one field of a large streaming document:

import { resolve } from "@cacheplane/json-stream";

const node = resolve(state.root, "/choices/0/message/content");

It returns undefined when the path does not exist yet — a normal condition mid-stream, not an error.

License

MIT

版本与发布记录

@cacheplane/json-stream 共发布 6 个版本,首次发布于 2026/05/03;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.1latest2026/10/011 个月前
0.1.02026/09/022 个月前
0.0.42026/08/073 个月前
0.0.32026/08/073 个月前
0.0.22026/08/063 个月前
0.0.12026/05/036 个月前

关于 @cacheplane/json-stream 的常见问题

@cacheplane/json-stream 是什么?

Streaming incremental JSON parser with push/pull APIs, node guards, and JSON Pointer resolution.

如何安装 @cacheplane/json-stream?

在项目目录运行 npm install @cacheplane/json-stream,或使用 pnpm add / yarn add / bun add。

@cacheplane/json-stream 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cacheplane/json-stream 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cacheplane/json-stream 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cacheplane/json-stream 可以商用吗?

@cacheplane/json-stream 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cacheplane/json-stream 的下载量有多少?

根据 npm 官方下载统计,@cacheplane/json-stream 在 2026/10/09 – 2026/11/07 这 30 天内被下载 11,567 次,日均约 386 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cacheplane/json-stream 还在维护吗?

最新版本 0.1.1 发布于 2026/10/01(1 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/05/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。