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

mock-stream-sequence

v1.0.1

Published

模拟流式数据输出的轻量工具,适用于 SSE、WebSocket 和 AI 大模型等场景。只是流式返回数据的模拟工具,不包含任何网络请求功能。

Readme

mock-stream-sequence

一个轻量、框架无关的流式数据模拟工具,用于在开发或测试中模拟 逐字输出 的流式响应(如 SSE、WebSocket、AI 大模型流式输出等)。

支持自定义输出格式、间隔时间、结束标记,适用于前端 mock、后端测试、AI 对话模拟等场景。


✨ 特性

  • 🧪 模拟真实流式文本输出(逐字符发送)
  • ⚙️ 支持自定义 JSON 结构(通过 transform 函数)
  • 🏁 可选自动追加 <end> 结束标记
  • 🔄 支持多段文本连续输出
  • 📦 零依赖,仅 1KB(gzip 后)
  • 🌐 同时支持 CommonJS 和 ES Modules
  • 📝 完整 TypeScript 类型支持

📦 安装

npm install mock-stream-sequence

🚀 快速开始

CommonJS(Node.js 默认)

const { createStream } = require('mock-stream-sequence');

const { start, stop } = createStream({
  interval: 80,
  format: [
    {
      text: '你好,世界!',
      needEnd: true,
      transform: (char) => ({ content: char })
    }
  ]
}, (data) => {
  console.log(data);
});

start();
setTimeout(stop, 5000);

ES Modules(需 package.json 中设置 "type": "module")

import { createStream } from 'mock-stream-sequence';

const { start, stop } = createStream({
  interval: 100,
  format: [
    {
      text: 'Hello AI!',
      needEnd: true,
      transform: (char, index, fullText) => ({
        id: 'chat-123',
        delta: { content: char },
        finish_reason: char === '<end>' ? 'stop' : null
      })
    }
  ]
}, console.log);

start();

📚 API 说明

createStream(options, callback)

参数

  • options(对象)
    • format(Array,必填):输出配置数组
      • text(string):要模拟输出的文本
      • needEnd(boolean,默认 true):是否在文本结束后发送 标记
      • transform(function,必填):将字符转换为任意 JSON 结构的函数签名:(char: string, index: number | null, fullText: string | null) => any
    • interval(number,可选,默认 50):字符发送间隔(毫秒)
  • callback(function):每次输出时的回调函数,接收转换后的数据

返回值

  • start():开始流式输出
  • stop():立即停止输出(清除定时器)

💡 使用场景示例

模拟 AI 聊天流式响应(OpenAI 格式)

createStream({
  interval: 60,
  format: [{
    text: '这是一个由 AI 生成的模拟回答。',
    needEnd: true,
    transform: (char) => ({
      choices: [{
        delta: { content: char },
        finish_reason: char === '<end>' ? 'stop' : null
      }]
    })
  }]
}, (data) => {
  res.write(`data: ${JSON.stringify(data)}\n\n`);
});

多轮对话模拟

createStream({
  format: [
    {
      text: '第一段回复',
      needEnd: true,
      transform: (char) => ({ role: 'assistant', content: char })
    },
    {
      text: '第二段回复',
      needEnd: false,
      transform: (char) => ({ role: 'assistant', content: char })
    }
  ]
}, console.log);

📄 许可证

MIT © [wangwu]