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

koa-process-engine

v1.0.14

Published

Koa process engine resolver

Readme

Koa Process Engine

它是一套自动处理engine开关的中间件。主要用来处理比如类似mysql redis mssql之类的数据库自动关闭问题。同时进行一些最佳实践的部署。

它暴露出的一些api,能够让你在编写nodejs程序的时候尽可能快速简单。目前对51信用卡管家公司内部具有高度自由的支持。

Dictionary

  • [x] URI 路径编辑模块
  • [x] MYSQL mysql模块类
  • [x] FTP ftp模块类
  • [x] LDAP ldap模块类
  • [x] PROCESS 核心process模块
  • [x] REDIS redis模块类
  • [x] SENDCAST I/O输出方法插件
  • [x] SECRET 加密模块类
  • [x] TASKER 任务调度模块类
  • [x] CACHE 缓存模块类

PROCESS

核心模块提供一个中间件,让之后的路由具有自动管理引擎开关的功能。

我们时常会为一个mysql数据库什么时候开启与关闭的问题而烦恼,目前此类能够自动帮你管理这些引擎的开关,无需让用户考虑关闭时机的问题。大大提高了代码的效率。

import { PROCESS } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({ ... configs }));

只要经过/api的路由都具有自动管理的功能。

configs 参数配置如下:

| 属性名 | 描述 | 类型 | 默认值 | | :---- | :---- | :---- | :---- | | name | 命名空间。比如name=process 那么方法被放置在 ctx.process | String | process | | error | 错误接收方法,用于自定义输出错误 | Function | undefined | | plugins | 插件列表集合 | Array | undefined |

Plugins

插件制作非常方便:

  • 必须抛出的是一个class
  • constructor 具有一个context对象,就是类似上面的 ctx.process
  • 存在一个异步的install方法用于安装这个插件,具有一个options参数,该参数是在plugins中用户定义的
  • 存在一个异步的destroy方法,用来销毁。
// a.js
class NewComponent {
    constructor(options) {
        ...
    }
    post() {
        
    }
    ...
}

export default class NewComponentInstall {
    constructor(context) {
        this.context = context;
    }
    
    async install(options) {
        this.context.component = new NewComponent(options);
    }
    
    async destroy() {
        ...
    }
}

我们来调用这个插件

import { PROCESS } from 'koa-process-engine';
import Router from 'koa-router';
import A from './a';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({
    plugins: [
        {
           loader: A,
           options: { a:1, b: 2 }
        }
    ]
}));

// 或者如果没有参数,这样调用
route.use('/api', PROCESS.connect({
    plugins: [ A ]
}));

Define Engine

我们需要使用插件模式来定义一些引擎

// engine.js
import { MYSQL } from 'koa-process-engine';
export default class NewComponentInstall {
    constructor(context) {
        this.context = context;
    }
    
    async install() {
        this.mysql();
    }
    
    mysql() {
       const object = this.context.set('mysql', MYSQL);
       object.set('fanding', {
           host: '',
           database: '',
           port: '',
           user: ''
       });
    }
    
    ...
}

然后我们注册这个插件

import { PROCESS } from 'koa-process-engine';
import Router from 'koa-router';
import ENGINE from './engine';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({
    plugins: [ ENGINE ]
}));

Create an Engine

创建一个mysql的数据库链接对象。

route.get('/a', async ctx => {
    const mysql = await ctx.process.create('mysql:fanding');
    ...
})

URI

路由设置模块,用来统一设置路由,简化路由操作。

domain

设置一个域:

import { URI } from 'koa-process-engine';
URI.domain('a', 'http://api.u51.com');

观察一个域

import { URI } from 'koa-process-engine';
URI.domain('a', 'http://api.u51.com').watch(pather => {
    // 将这种 a/b/c 转化成 a:b:c
    return pather.replace(/\//g, ':');
});

获取一个域

import { URI } from 'koa-process-engine';
console.log(URI.domain('a')); // http://api.u51.com

path

设置一个层

import { URI } from 'koa-process-engine';
URI.path('b', '/a/b/c/:id(\\d+)');

获取一个完整链接(不带query)

import { URI } from 'koa-process-engine';
URI.path('a:b', { id: 3 }).toString(); // http://api.u51.com/a/b/c/3

带上query参数

import { URI } from 'koa-process-engine';
URI.path('a:b', { id: 3 }).parse({a:1}); // http://api.u51.com/a/b/c/3?a=1

完整demo

import { URI } from 'koa-process-engine';
URI.domain('Redis', '').watch(pather => {
   // 将这种 a/b/c 转化成 a:b:c
   return pather.replace(/\//g, ':');
});
URI.path('Get', '/user/:id(\\d+)');

const result = URI.path('Redis:Get', { id:100 }).parse({ name: 'evio' });
// user:100?name=evio

SENDCAST

import { PROCESS, SENDCAST } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({
    plugins: [ SENDCAST ],
    // 或者
    plugins: [
        {
            loader: SENDCAST,
            options: {
                name: 'cast', // 命名为cast 默认 send
                cache: true // 是否缓存输出
            }
        }
    ]
}));

route.get('/', async ctx => {
    ctx.process.send({ a:1 });
})

SECRET

import { PROCESS, SECRET } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({
    plugins: [ SECRET ],
    // 或者
    plugins: [
        {
            loader: SECRET,
            options: {
                key: 'test'
            }
        }
    ]
}));

route.get('/', async ctx => {
    const {send, secret} = ctx.process;
    send(secret('ddddsaffasfdsfas'));
})

TASKER

当我们需要使用任务调度,必须用到此模块

import { PROCESS, TASKER } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({
    plugins: [ TASKER ]
}));

route.get('/', async ctx => {
    const { task, send } = ctx.process;
    task.on('done', () =>  send('ok'));
    task.on('reject', () => send('error done', 500));
    task.on('error', err => console.log(err));
    task.pipe(fn1).pipe(fn2);
})

CACHE

内置的一个redis缓存类设计

User Cache Plugin

import { PROCESS, CACHE } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;

route.use('/api', PROCESS.connect({
    plugins: [ {
        loader: CACHE,
        options: 'redis:local' // 之前设置过的引擎名 必须是redis引擎名
    } ]
}));

route.get('/', async ctx => {
    ctx.process.cache.load(name, args); // 加载一个缓存数据
    ctx.process.cache.build(name, args); // 重建一个缓存数据
    ctx.process.cache.delete(name, args); // 删除一个缓存
})

Use to define cache

// x.js
import { uri, cache, URI } from 'koa-process-engine';
URI.domain('redis', '/').watch(pather => {
   // 将这种 a/b/c 转化成 a:b:c
   return pather.replace(/\//g, ':');
});;

export default class User {
    @uri static getuser = '/a/b/c';
    @cache('redis:getuser') cacheUserInfo(process, args) {
        const mysql = await process.create('mysql:local');
        return process.methods.getuser(mysql, args); // 瞎写的方法,别在意
    }
}

使用

import User from './x'; //必须引用才能注册进去
router.get('/x', async ctx => {
    ctx.process.cache.load('redis:getuser'); // 加载一个缓存数据
    ctx.process.cache.build('redis:getuser'); // 重建一个缓存数据
    ctx.process.cache.delete('redis:getuser'); // 删除一个缓存
})