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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-ai-stream-markdown

v0.2.0

Published

Stream AI/UGC Markdown into React safely with coalescing, callbacks, and auto-scroll. Renders via safe-markdown-react.

Readme

react-ai-stream-markdown

Stream AI/UGC Markdown into React safely with coalescing, callbacks, and auto-scroll. Renders via safe-markdown-react.

Perfect for LLM chat UIs: pass a ReadableStream or AsyncIterable and render as the text arrives.


Features

  • ✅ Accepts ReadableStream<string|Uint8Array> or AsyncIterable<string>
  • ✅ Coalesces chunks (reduce re-renders) with renderIntervalMs
  • onChunk / onComplete callbacks
  • ✅ Optional auto-scroll to bottom
  • ✅ Renders safely via safe-markdown-react (sanitized, link policy, image allowlist)
  • ✅ Typed, React 18+, ESM

Install

npm i react-ai-stream-markdown safe-markdown-react
# peer deps: react, react-dom >= 18

Usage

1) With a Web ReadableStream (e.g., fetch from your API)

import { StreamMarkdown } from 'react-ai-stream-markdown';

function ChatMessage({ stream }: { stream: ReadableStream<Uint8Array> }) {
  return (
    <StreamMarkdown
      source={stream}
      renderIntervalMs={50}
      autoScroll
      allowedImageHosts={['images.example.com']}
    />
  );
}

2) With an AsyncIterable (e.g., SDK that yields tokens)

import { StreamMarkdown } from 'react-ai-stream-markdown';

async function* toky() {
  yield 'Hello'; yield ' '; yield '**world**';
}

<StreamMarkdown source={toky()} />;

Props

type StreamSource =
  | ReadableStream<string | Uint8Array>
  | AsyncIterable<string | Uint8Array>;

interface StreamMarkdownProps {
  source: StreamSource;
  initialText?: string;
  renderIntervalMs?: number;   // default 32ms
  autoScroll?: boolean;        // default false
  onChunk?: (chunk: string, full: string) => void;
  onComplete?: (full: string) => void;
  // Pass-through to safe-markdown-react
  allowedImageHosts?: string[];
  allowedSchemes?: string[];
  maxHeadingLevel?: 1|2|3|4|5|6;
  components?: Record<string, React.ComponentType<any>>;
}

OpenAI streaming example

Server (Next.js App Router)

Create app/api/chat/route.ts:

export const runtime = 'edge'; // optional
export async function POST(req: Request) {
  const body = await req.json();
  const r = await fetch('https://api.openai.com/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.OPENAI_API_KEY!}`,
    },
    body: JSON.stringify({
      model: 'gpt-4o-mini', // or latest
      stream: true,
      messages: body.messages,
    }),
  });

  if (!r.ok || !r.body) {
    return new Response('Upstream error', { status: 500 });
  }

  // Pipe the upstream ReadableStream to the client unmodified
  // (Optionally, transform SSE → plain text here.)
  return new Response(r.body, {
    headers: {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache, no-transform',
      'Connection': 'keep-alive',
    },
  });
}

Client

import { StreamMarkdown } from 'react-ai-stream-markdown';

async function startChat(messages) {
  const res = await fetch('/api/chat', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ messages }),
  });
  if (!res.body) throw new Error('No stream');
  return res.body; // ReadableStream<Uint8Array>
}

// in your component
const [stream, setStream] = useState<ReadableStream<Uint8Array> | null>(null);

<button onClick={async () => setStream(await startChat([{ role: 'user', content: 'Hello' }]))}>
  Ask
</button>

{stream && <StreamMarkdown source={stream} autoScroll renderIntervalMs={40} />}

If you want Markdown specifically, make sure your system prompt tells the model to reply in Markdown.


Notes

  • We don’t parse tokens; we just append to a string and re-render safe-markdown-react. It’s fast enough for chat UIs.
  • If your stream is Uint8Array chunks, we TextDecoder them as UTF-8.
  • For extremely high-frequency streams, increase renderIntervalMs (e.g., 50–80ms).

License

MIT