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

json-response-stream

v1.0.2

Published

Utility function for streaming JSON from LLM libraries such as Genkit

Readme

json-response-stream

npm version license

A tiny, powerful utility for streaming JSON from API responses, LLMs (like Genkit), and anywhere else you're getting a stream of JSON objects.

✨ Features

  • 🧩 Parse multiple JSON objects from a stream
  • 🔄 Handle partial JSON data across multiple chunks
  • 🔍 Skip duplicate objects automatically
  • 🛡️ Safely parse without throwing errors
  • 🧵 Works great with fetch, LLM APIs, or any ReadableStream

🚀 Quick Start

npm install json-response-stream
# or
yarn add json-response-stream
# or
pnpm add json-response-stream

🌟 Usage

Basic Example

import { jsonParser } from 'json-response-stream'

// Let's fetch some streaming data!
const response = await fetch('https://api.example.com/stream')

// The magic happens here ✨
for await (const data of jsonParser(response.body)) {
  console.log('Got new data:', data)
  // Do something cool with each JSON object as it arrives
}

Handling Stream with TypeScript

import { jsonParser } from 'json-response-stream'

interface User {
  id: number
  name: string
  role: string
}

const response = await fetch('https://api.example.com/users/stream')

// Type safety! 🛡️
for await (const user of jsonParser<User>(response.body)) {
  console.log(`Welcome, ${user.name} the ${user.role}!`)
}

LLM Example - Genkit on a Nuxt (h3) server

// server side:
const { stream } = await ai.generateStream('Suggest a complete menu for a pirate themed restaurant.')

const transformedStream = ReadableStream.from(stream).pipeThrough(
  new TransformStream({
    transform(chunk, controller) {
      controller.enqueue(JSON.stringify(chunk))
    }
  })
)

// Send the stream to the client
sendStream(event, transformedStream)

// Client side:

const response = await fetch('/api/genkit/stream')

// Process each chunk as it comes in!
for await (const idea of jsonParser(response.body)) {
  console.log(chunk.text)
}

🧰 API Reference

Main Function

jsonParser<T>(stream: ReadableStream): AsyncIterableIterator<T>

The star of the show! Creates an async iterator that processes a stream and yields JSON objects as they become available.

// Advanced example with fetch and AbortController
const controller = new AbortController()
const response = await fetch('https://api.example.com/stream', {
  signal: controller.signal
})

setTimeout(() => controller.abort(), 5000) // Cancel after 5 seconds

try {
  for await (const data of jsonParser(response.body)) {
    console.log(data)
  }
} catch (error) {
  if (error.name === 'AbortError') {
    console.log('Stream reading was cancelled! 🛑')
  } else {
    console.error('Error processing stream:', error)
  }
}

Other Exports

jsonTransformStream<T>()

Creates a TransformStream for processing JSON chunks. Useful if you're working directly with the Streams API.

const transformedStream = someReadableStream
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(jsonTransformStream())

safeParse<T>(str: string): T | null

A JSON.parse wrapper that never throws - returns null on invalid JSON.

hashString(str: string): string

A simple string hashing function used internally to detect duplicates.

Why use json-response-stream?

Streaming APIs are awesome, but dealing with chunked JSON can be a pain. This library makes it seamless to:

  • Process large datasets without waiting for the entire response
  • Handle real-time updates from LLMs and other streaming APIs
  • Avoid the headaches of parsing partial JSON chunks

🙌 Contributing

Contributions welcome! Open an issue or PR on GitHub.

📄 License

MIT - do awesome things with it!


Publishing

  1. Update the version in package.json
  2. Run npm run build && npm publish

Made with ❤️ by Jamie Curnow