@withremyinc/stream
v1.0.3
Published
Composable Web Streams utilities with streaming JSON and XML parsing.
Maintainers
Readme
@withremyinc/stream
Composable helpers for the Web Streams API, plus incremental JSON and XML parsers for text and AI pipelines.
- ESM-only
- Node 18+
- Web Streams-native APIs
- Small functional helpers for
ReadableStream/TransformStream - Tolerant streaming parsers for JSONC-style JSON and XML fragments
Installation
npm install @withremyinc/streampnpm add @withremyinc/streamyarn add @withremyinc/streamQuick start
import { arrayStream, collect, filter, map } from "@withremyinc/stream";
const output = await collect(
arrayStream([1, 2, 3, 4])
.pipeThrough(map((value) => value * 2))
.pipeThrough(filter((value) => value > 4)),
);
console.log(output); // [6, 8]What is included?
Stream helpers
Sources / composition
arrayStream()merge()mergeKeyed()concat()pipeThrough()tee()
pipeThrough() composes multiple TransformStreams into a single transform when you want to pass around a whole pipeline.
Collectors
collect()collectFirst()collectLast()collectToString()
Transforms
map()filter()filterMap()flatMap()scan()reduce()take()takeLast()drop()toArray()toString()forEach()some()every()find()extractDelimiter()
Streaming JSON
parseJSON() tokenizes and parses JSON incrementally and emits visit events.
jsonToJSObject() folds those events back into a plain JavaScript value.
It is intentionally tolerant of JSONC-style input such as comments and trailing commas.
When parsing LLM/tool-call streams, pass { emitPartialStrings: true } to emit onPartialLiteralValue events for open string literals at chunk boundaries.
import {
arrayStream,
collect,
jsonToJSObject,
parseJSON,
takeLast,
} from "@withremyinc/stream";
const [value] = await collect(
arrayStream(['{ "name": "Remy", /* comment */ }'])
.pipeThrough(parseJSON())
.pipeThrough(jsonToJSObject())
.pipeThrough(takeLast(1)),
);
console.log(value); // { name: "Remy" }import { arrayStream, collect, parseJSON } from "@withremyinc/stream";
const events = await collect(
arrayStream(['{"name":"str', 'eam"}']).pipeThrough(
parseJSON({ emitPartialStrings: true }),
),
);
console.log(events);
// includes { type: "onPartialLiteralValue", value: "str", path: ["name"] }Streaming XML
parseXML() incrementally parses XML documents or fragments and emits events such as:
onDocumentBeginonElementBeginonTextonElementEndonError
It is non-validating and recovery-oriented, which makes it useful for LLM output and other imperfect text streams.
import { arrayStream, collect, parseXML } from "@withremyinc/stream";
const events = await collect(
arrayStream(["<root><item>Hello</item></root>"]).pipeThrough(parseXML()),
);
console.log(events);If you only want specific top-level XML islands from mixed text, use extractXML():
import { arrayStream, collect, extractXML } from "@withremyinc/stream";
const events = await collect(
arrayStream([
'before <answer confidence="0.9">hello</answer> after',
]).pipeThrough(extractXML({ allowTags: ["answer"] })),
);Delimited block extraction
extractDelimiter() streams the body of the first matching fenced block and drops the fence lines.
This is handy for extracting Markdown code fences before passing the contents into parseJSON() or parseXML().
import {
arrayStream,
collectToString,
extractDelimiter,
} from "@withremyinc/stream";
const body = await collectToString(
arrayStream([
"ignore this\n```json\n",
'{"ok": true}\n',
"```\ntrailing text",
]).pipeThrough(extractDelimiter({ allowLanguages: ["json"] })),
);
console.log(body); // {"ok": true}\nDevelopment
Published output targets Node 18+, but local builds currently require Node 20.19+ because tsdown does. .nvmrc and .node-version are included for that toolchain.
pnpm install
pnpm test
pnpm typecheck
pnpm buildExtra scripts:
pnpm bench:geojsonpnpm bench:xml
Changelog
See CHANGELOG.md.
Releasing
For the full checklist, see RELEASING.md.
Quick version:
- Bump the package version with
pnpm version patch(orminor/major). - Push the commit and matching tag with
git push --follow-tags. - GitHub Actions publishes that tag to npm.
The publish workflow expects an NPM_TOKEN repository secret and uses npm provenance.
Release notes / packaging
This package publishes the built dist/ output plus:
README.mdCHANGELOG.mdLICENSE.mdNOTICES.md
License
MIT.
Copyright © 2026 Remy, Inc.
See LICENSE.md for the project license and NOTICES.md for bundled third-party notices.
