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

@finley_ge/ezmock

v1.0.0

Published

Easy API mocking tool built with Bun

Readme

EzMock

一个基于 Bun + TypeScript 的简单易用的 API Mock 工具。通过 CLI 的方式快速启动一个 Mock 服务器,支持 JSON 配置和交互式配置。

功能特性

  • 🚀 基于 Bun 构建,启动速度快
  • 📋 支持 JSON 配置文件加载
  • 🖥️ 交互式 CLI 配置
  • 🎯 支持多种响应类型(JSON、文本、文件)
  • 🔀 支持参数化路由(如 /api/users/:id
  • ⏱️ 支持响应延迟配置
  • 🌐 内置 CORS 支持
  • 📝 请求日志记录

安装和使用

前置要求

确保已安装 Bun

curl -fsSL https://bun.sh/install | bash

安装依赖

bun install

编译二进制文件

编译为本机可执行文件:

# 编译为当前平台的二进制文件
bun run build

# 编译为特定平台
bun run build:macos    # macOS (Apple Silicon)
bun run build:linux    # Linux (x64)
bun run build:windows  # Windows (x64)

# 编译所有平台
bun run build:all

编译后可以直接运行二进制文件,无需安装 Bun:

./ezmock -c example.json

使用方法

1. 使用配置文件启动

bun start -c example.json

2. 交互式配置

bun start

3. 命令行参数

使用源码:

# 查看帮助
bun start --help

# 指定端口和主机
bun start -p 8080 -h 0.0.0.0

# 加载配置文件并覆盖端口
bun start -c config.json -p 8080

# 启用请求日志(包含 URL、headers 和 payload)
bun start -c config.json --logging

使用编译后的二进制:

# 查看帮助
./ezmock --help

# 指定端口和主机
./ezmock -p 8080 -h 0.0.0.0

# 加载配置文件并覆盖端口
./ezmock -c config.json -p 8080

# 启用请求日志(包含 URL、headers 和 payload)
./ezmock -c config.json --logging

请求日志默认关闭,只能通过启动参数 -l--logging 开启。日志可能包含认证 header 和请求 payload 中的敏感信息,请仅在可信的调试环境中使用。

配置文件格式

{
  "port": 3000,
  "host": "localhost",
  "cors": true,
  "routes": [
    {
      "method": "GET",
      "path": "/api/users",
      "type": "json",
      "statusCode": 200,
      "response": [
        { "id": 1, "name": "John Doe", "email": "[email protected]" }
      ]
    },
    {
      "method": "GET",
      "path": "/api/users/:id",
      "type": "json",
      "statusCode": 200,
      "response": { "id": 1, "name": "John Doe", "email": "[email protected]" }
    },
    {
      "method": "POST",
      "path": "/api/users",
      "type": "json",
      "statusCode": 201,
      "response": { "id": 2, "name": "New User" },
      "delay": 500
    },
    {
      "method": "GET",
      "path": "/hello",
      "type": "text",
      "response": "Hello World!"
    }
  ]
}

路由配置参数

  • method: HTTP 方法(GET, POST, PUT, DELETE, PATCH)
  • path: 路由路径,支持参数化(如 /api/users/:id
  • type: 响应类型(jsontextfilestream
  • response: 响应内容
  • statusCode: HTTP 状态码(可选,默认 200)
  • delay: 响应延迟(毫秒,可选)
  • headers: 自定义响应头(可选)
  • stream: 流式响应配置(仅当 typestream 时使用)
    • mode: 流式模式(line 为 SSE 格式,chunk 为原始块)
    • chunks: 数据块数组(字符串或对象)
    • chunkDelay: 块之间的延迟(毫秒,可选,默认 100)

示例

启动示例服务器

# 启动标准示例
bun start -c example.json

# 启动流式响应示例
bun start -c stream-example.json

测试 API

# 获取用户列表
curl http://localhost:3000/api/users

# 获取特定用户
curl http://localhost:3000/api/users/123

# 创建用户
curl -X POST http://localhost:3000/api/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Test User"}'

# 健康检查
curl http://localhost:3000/health

# 文本响应
curl http://localhost:3000/hello

测试流式响应

# 测试 Server-Sent Events (SSE)
curl -N http://localhost:3000/stream/sse

# 测试 JSON 流
curl -N http://localhost:3000/stream/json

# 测试原始数据块流
curl -N http://localhost:3000/stream/chunks

# 测试模拟 LLM 响应
curl -N http://localhost:3000/stream/llm-response

# 测试聊天流式响应
curl -N -X POST http://localhost:3000/stream/chat

流式响应配置示例

{
  "method": "GET",
  "path": "/stream/sse",
  "type": "stream",
  "statusCode": 200,
  "stream": {
    "mode": "line",
    "chunkDelay": 500,
    "chunks": [
      "First message",
      "Second message",
      "Third message"
    ]
  }
}

SSE 模式会自动格式化为 data: <content>\n\n 格式,适用于 Server-Sent Events 客户端。

开发

开发模式运行

bun run dev

运行测试

bun test

许可证

MIT