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

asai-nodejs-wsmock

v0.2.6

Published

asai for you

Readme

asai-nodejs-wsmock — WebSocket Mock 数据模拟插件

功能概述

asai-nodejs-wsmock 是一个Mock 数据模拟插件,用于在开发环境中模拟数据接口。它通过读取 dirmock.json 配置文件和对应的 JSON 数据文件,动态加载模拟数据(dirdb)和模拟函数(dirfn),为前后端分离开发提供数据模拟支持。


文件结构

asai-nodejs-wsmock/
├── README.md              ← 本文档
└── AsaiWsMock.ts          ← 入口文件:Mock 数据初始化

入口文件 — AsaiWsMock.ts

import * as fs from 'fs';

function startMock($asai: any = {}, dirmockfn: any = {}) {
  if ($asai.asaimock && $asai.hostconfig.path?.mock) {
    $asai.asaimock.dirfn = dirmockfn($asai);
    initMock(process.cwd());
  }
  // ...
}
export default startMock;

核心逻辑分析

startMock($asai, dirmockfn)

初始化 Mock 数据系统:

  1. 前置条件检查$asai.asaimock 对象必须存在,且 $asai.hostconfig.path.mock 已配置
  2. 挂载模拟函数:将 dirmockfn($asai) 的处理函数列表注入到 $asai.asaimock.dirfn
  3. 加载 Mock 配置:调用 initMock() 读取 dirmock.json 和数据文件

initMock(mockpath) — Mock 数据加载

| 步骤 | 说明 | |------|------| | 1 | 读取 {mockpath}/dirmock.json,合并到 $asai.asaimock | | 2 | 遍历 $asai.asaimock.dirdb,逐个读取 JSON 数据文件存入 $asai.asaimock.db | | 3 | 遍历 $asai.asaimock.dirfn,将模拟函数合并到 $asai.asaimock.fn |

数据加载路径{mockpath}{$asai.hostconfig.path.mock}{filename}


配置说明

// $asai.hostconfig
{
    path: {
        mock: '/mock/'           // Mock 数据目录(相对于项目根目录)
    },
    asaimock: { ... }            // 由外部系统初始化的 Mock 容器对象
}

dirmock.json 配置格式

{
    "dirdb": {
        "users": "users.json",       // 键名 → 数据文件映射
        "config": "config.json"
    },
    "dirfn": {
        "handler1": "..."            // 模拟函数名列表
    }
}

使用示例

import startMock from './AsaiWsMock';

// 在系统初始化时调用
startMock($asai, ($asai) => {
    return {
        // 模拟处理函数
        userHandler: (req, res) => {
            const users = $asai.asaimock.db.users;
            res.json(users);
        }
    };
});

注意事项

  1. Mock 系统仅在开发环境启用(通过 $asai.asaimock 是否存在控制)
  2. dirmock.json 和数据文件使用同步 fs 读取,仅初始化时加载一次
  3. 模拟函数(dirfn)会在 startMock() 时被合并到 $asai.asaimock.fn 供路由调用
  4. 加载失败时会通过 $asai.$log('MOCK', ...) 输出错误日志,不会中断进程