npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

llm-stream-guard

v1.0.0

Published

Zero-dependency TypeScript security filter for LLM streams — redact secrets, enforce tool policy, byte and event modes.

Readme

llm-stream-guard

core node runtime deps tests ci status

Security filter for LLM streams — redact secrets and PII, enforce tool-call policy, sanitize errors. Works on raw bytes (TransformStream) and parsed event streams. Declarative JSON/YAML policies and a CLI for offline scans.

A standalone, zero-dependency TypeScript security filter for LLM proxy and agent pipelines. Byte mode: chunk-safe secret redaction on raw SSE. Event mode: tool allow/deny, arg blocking, PII & error sanitization on parsed streams. Policy files + llm-stream-guard scan for CI prep.

Status: Stable 1.0.0 — frozen SARIF rule IDs, onFinish stream summaries, doctor CLI, Phase 10 contract tests. Start at Getting started. See API stability and Migration 0.x → 1.0 before upgrades.

New to LLM streaming? Read Getting started (~15 min) → Concepts & glossaryDocumentation map for your role.


Contents


New to LLM streams?

If you have never worked with streaming LLM APIs (SSE, deltas, tool calls), start here — no prior guard knowledge required:

| Step | Doc | Diagram | | ---- | --------------------------------------------------------------------------------------------- | ------------------------------------------------- | | 1 | Getting started — install, first byte guard, first tool gate | Journey | | 2 | Concepts & glossary — SSE, GuardEvent, modes | Stream anatomy | | 3 | Documentation map — pick a path by role (proxy dev, agent dev, CI) | — |

Full reference: Policy · CLI · Cookbook


Why stream guard?

When proxying or running agents, unsafe content leaks downstream in predictable ways:

  1. Secrets in text deltas — API keys, bearer tokens, JWTs echoed in model output.
  2. Dangerous tool args — shell injection, exfil URLs, oversized JSON before execution.
  3. Unauthorized tool names — models invoke tools outside your allowlist.
  4. Raw provider errors — internal URLs and stack traces forwarded to browsers.

Many filters scan raw bytes only and miss precise policy on assembled tool_call.done JSON. This library targets both byte and event modes with zero runtime dependencies.

Chunk redaction: secrets split across TCP reads

  • Mid-chunk splits — secrets split across TCP reads use a rolling buffer + prefix holdback (LSG-C).
  • Tool policy timing — evaluate names early; validate args on done when JSON is complete (LSG-T).
  • Violation modesblock, warn, or audit with onViolation for SIEM-friendly logs.

Two modes

| Mode | API | When | | --------- | ------------------- | -------------------------------------------------- | | Byte | createByteGuard() | Proxy forwards provider-shaped SSE without parsing | | Event | guardEvents() | Parsed stream — assemble, AI SDK, or custom mapper |

Byte mode vs event mode


Architecture

Raw upstream content enters through byte guard or event guard; composable rules redact or block before your proxy, UI, or tool executor sees output.

End-to-end pipeline

Optional pairing with llm-stream-assemble (parse → guard) — cookbook only, no npm coupling:

Ecosystem: optional assemble + guard

Lifecycle and concurrency

Create one GuardContext per stream — never share across concurrent requests. Stateless helpers (pipeGuard, internal transform pipeline) compose into stateful entry points.

GuardContext lifecycle

Diagram sources: docs/img/ (Mermaid .mmd + committed SVG). Regenerate with pnpm diagrams:build.


GuardEvent model

Independent event union — not StreamEvent, not provider types:

GuardEvent mindmap

| Type | Shape | | ----------- | ----------------------------------------------- | | text | { type, phase: delta \| done, text } | | tool_call | { type, phase, id?, name?, args?, argsText? } | | reasoning | { type, phase, text } | | error | { type, message, code? } | | finish | { type, reason? } |

Full spec: docs/proposal.MD.


Violation modes

block / warn / audit

| Mode | Byte mode (secrets) | Event mode (secrets + PII) | Tool policy | | ------- | --------------------------------------- | ------------------------------- | ------------------------------------------- | | block | Redact secrets | Redact secrets/PII | Safe substitute + policy_violation finish | | warn | Redact secrets | Redact secrets/PII | Block tool + onViolation | | audit | Redact secrets + onViolation on match | Redact + onViolation on match | Pass tool through + onViolation |


Install

pnpm add llm-stream-guard
# or npm install llm-stream-guard

Requirements: Node.js 18+ · Bun / Deno / Workers (Web Streams)

Maintainers: run pnpm release:prep before tagging and npm publish. GitHub Release notes from CHANGELOG.md.


First success in 30 seconds

git clone [email protected]:01laky/llm-stream-guard.git
cd llm-stream-guard
pnpm install
./scripts/setup-githooks.sh
pnpm verify

Then pipe bytes through the byte guard:

import { createByteGuard } from "llm-stream-guard";

const guarded = sourceStream.pipeThrough(createByteGuard({ redactSecrets: true, mode: "warn" }));

Quickstart

Proxy (byte mode)

import { createByteGuard } from "llm-stream-guard";

return new Response(
	upstream.body!.pipeThrough(
		createByteGuard({ redactSecrets: true, sanitizeErrors: true, mode: "warn" }),
	),
	{ headers: { "Content-Type": "text/event-stream" } },
);

redactSecrets and sanitizeErrors are active on createByteGuard() options.

Agent (event mode)

import {
	allowTools,
	blockToolArgs,
	guardEvents,
	redactSecrets,
	sanitizeErrors,
} from "llm-stream-guard";

for await (const event of guardEvents(
	parsedEvents,
	{ mode: "block", onViolation: (v) => console.warn(v.rule, v.message) },
	redactSecrets(),
	allowTools(["search", "read_file"]),
	blockToolArgs(/rm\s+-rf/),
	sanitizeErrors(),
)) {
	if (event.type === "tool_call" && event.phase === "done") {
		await executeTool(event);
	}
}

Transform ordering

Recommended pipeline:

redactSecrets() → redactPII()? → allowTools/denyTools → blockToolArgs → maxToolArgsBytes → sanitizeErrors()

Reversing order is explicit — see docs/integration-cookbook.md.


Policy files & CLI

Policy compile pipeline

Declarative policies map to the same rule factories as manual stacks. Built-in profiles: proxy-strict, agent-gate, audit-only.

Policy file (policies/agent-gate.json)

{
	"version": "1",
	"policyVersion": "team-alpha-v3",
	"mode": "block",
	"rules": [
		{ "allowTools": { "names": ["search", "read_file", "grep"] } },
		{ "maxToolArgsBytes": { "max": 65536 } },
		{ "sanitizeErrors": {} }
	]
}

Programmatic (loadPolicy / createGuardFromPolicy)

import { createGuardFromPolicy, loadPolicy } from "llm-stream-guard";

const guard = createGuardFromPolicy(loadPolicy("./policies/agent-gate.json"));
for await (const event of guard.guard(parsedEvents)) {
	await handle(event);
}
const byteGuard = guard.createByteGuard();

CLI

npx llm-stream-guard validate policies/agent-gate.json
npx llm-stream-guard resolve policies/examples/extends-agent.json
npx llm-stream-guard scan --policy policies/agent-gate.json test/fixtures/events/
cat capture.log | npx llm-stream-guard scan --policy policies/proxy-strict.json -
npx llm-stream-guard diff policies/v1.json policies/v2.json --check
npx llm-stream-guard profiles list
npx llm-stream-guard doctor
npx llm-stream-guard audit static --policy policies/agent-gate.json --manifest tools/manifest.json

| Env variable | Effect | | ------------------- | --------------------------------------------------- | | GUARD_MODE | Override policy mode (block / warn / audit) | | GUARD_POLICY_PATH | Default --policy path for CLI scan |

Schema reference: schemas/policy-v1.json. Example policies: policies/.

Policy pitfalls: overlapping allow/deny lists (POLICY_E009); empty allowlist with mode: block (POLICY_E010 / POLICY_E008).


Mode decision guide

Pick byte vs event mode in ~30 seconds:

Use the modes diagram above, or:

  • Raw SSE to browser, no parsercreateByteGuard()
  • Tool gate before executeguardEvents() + rule factories
  • Parse with assemble / AI SDK first → map to GuardEvent, then guardEvents()

Documentation

Start here (1.0.0)

| Guide | Audience | | ---------------------------------------------------------- | ---------------------------------------------------------- | | Getting started | First-time users — install, byte vs event, common mistakes | | Concepts & glossary | LLM streaming vocabulary + guard terms | | Documentation map | Learning paths by persona | | Policy reference | All rule types, error codes, profiles | | CLI reference | Every command, flags, exit codes | | Troubleshooting | Symptom → cause → fix | | Upgrade guide | 0.x → 1.0 semver jumps | | API stability | 1.x semver guarantees | | Migration 0.x → 1.0 | SARIF, onFinish, Action pins |

CI & static audit

Policy drift detection, static tool manifest scanning, and a composite GitHub Action for PR gates:

Integration cookbook (1.0.0)

End-to-end recipes for byte proxies (Hono, Express, Workers), agent tool gates, policy-driven setup, assemble/AI SDK mappers, dual-stream audit, MCP mapping, LiteLLM hooks, CI scans, and migration from regex middleware:

Reference

Related: llm-stream-assemble — stream parsing and assembly (separate package).


How this compares

| | llm-stream-guard | Enterprise middleware | llm-stream-assemble | | ------------ | -------------------------- | --------------------- | -------------------- | | Scope | Stream security filter | Broad platform | Stream parsing | | Byte + event | Both first-class | Often bytes-only | Events (after parse) | | Tool policy | First-class | Varies | Assembly only | | Dependencies | Zero runtime | Varies | Zero runtime |

Full matrix: docs/comparison.md.


Non-goals

  • No HTTP client, auth, or agent loop
  • No tool execution or UI components
  • No LLM-as-judge classifier
  • No hard dependency on assemble, AI SDK, or LangChain
  • No provider adapters (use assemble or your parser)

See docs/proposal.MD.


Development

pnpm install
./scripts/setup-githooks.sh
pnpm verify

| Command | Description | | ------------------------------------- | ---------------------------------------------------- | | pnpm verify | format + typecheck + build + test + fixtures + smoke | | pnpm verify:deps | fail if runtime dependencies are added | | pnpm release:prep | pre-tag checks (version, CHANGELOG, dist, npm pack) | | pnpm diagrams:build | regenerate README SVGs from Mermaid sources | | pnpm fixtures:check-policies | validate example + profile policies | | pnpm fixtures:audit-policy-registry | policy fixture REGISTRY parity | | pnpm test | Vitest (LSG-S/B/E/C/R/T/P/POL/CBK, LSG-REL) | | pnpm examples:typecheck | Typecheck cookbook examples against dist/ | | pnpm examples:smoke | minimal-node install smoke after build | | pnpm cookbook:check-examples | examples README registry parity | | pnpm bench:smoke | local byte/event timing (informational) | | pnpm build | tsup → ESM + CJS + declarations |


Author

Ladislav Kostolny[email protected] · GitHub @01laky

License

MIT — see LICENSE. Copyright (c) 2026 Ladislav Kostolny.