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 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa2-match

v1.0.7

Published

match request or response and change koa2 context.

Downloads

24

Readme

koa2 match rule and change response

匹配规则并修改响应数据

install

npm install koa@next
npm install koa2-match

usage

var koa = require('koa');
var app = new koa();
var match = require('koa2-match');

match.match('test_str', function (ctx) {
  ctx.response.body = 'test_str';
});

app.use(match.callback());

console.log('rules:', match.rules);

app.listen(4000, function(err){
    err && console.error(err) || console.log('start Server At http://localhost:4000');
});

api

  • callback() 生成koa@next的中间件
  • match(condition, callback) 匹配后执行程序修改请求或者响应
  • add(condition, callback) 同match
  • matchs(rules) 批量设置匹配规则,rules中rule的属性会被保留
  • adds(rules) 同matchs
  • clean() 清空所有匹配规则
  • getRules() 取得所有的规则列表,对其修改则会直接影响callback处理程序
  • setRules(rules) 清空规则后设置规则

rule 匹配规则,包含属性id, condition, handle

  • id: 添加rule时自动分配的唯一id
  • condition: 匹配规则
  • handle: 匹配condition后的处理

condition 匹配条件

  • {string} url中包含condition
  • {reg} condition.test(url)为true
  • {function} condition(ctx)为true
  • {object} 循环遍历condition的属性,并取出ctx或者ctx.request对应属性进行判断,例如condition为{host: 'test'},则取出ctx.request.host后使用条件'test'进行判断。 condition的其属性值为string、reg或者function。
  • {array} 多个匹配条件,只有数组中所有的条件判断都通过时,其判断才通过
  • phase condition.phase表示匹配阶段,为空或者'request'匹配请求阶段,当为'response'时匹配响应阶段
  • attr&value 假如condition存在属性attr和value并且都不为空,则更改condition同{attr:value}处理方式,例如{'attr': 'phase','value': 'request'},处理方式同{'phase': 'request'}

handle 处理程序

  • {array} 多个处理组成的数组
  • {function} 处理程序,参数ctx
  • {plainObject} 使用对象属性覆盖ctx的属性,例如属性'request.header.host'则将修改ctx.request的header
    • 特殊情况: 假如handle存在属性attr和value并且都不为空,则更改handle同{attr:value}处理方式,例如{'attr': 'request.header.host','value': 'www.test.com'},处理方式同{'request.header.host': 'www.test.com'}

其他

  • ctx._matchs{array} 匹配完毕后所有匹配的rule的id组成的数组

build

npm install -g babel-cli babel-loader mocha
npm run build

history

  • v1.0.6
    • 增加函数match.setRules
    • condition和handle的plainObject格式时,增加同时存在attr和value属性的特殊处理
  • v1.0.4
    • 匹配规则为函数时,其参数改为ctx
  • v1.0.3
    • 回调处理支持配置为数组
    • 代码格式话
  • v1.0.2
    • 自动为匹配规则生成随机id
    • 匹配规则执行完一个后再匹配下一个
    • 执行完成后ctx._matchs存放当前链接匹配的所有规则
  • v1.0.1
    • 匹配的第二个参数是一个对象时,自动修改ctx对应字段
  • v1.0.0
    • 匹配后执行函数替换内容

todo:

  • [x] 为规则自动分配一个id
  • [x] 当前地址匹配匹配了哪些规则
  • [ ] 删除规则
  • [x] 按次序匹配,执行完一个匹配之后再判断下一个匹配项
  • [x] 匹配后直接根据指定对象修改ctx