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

meirin

v0.3.1

Published

A powerful frequency limitation library

Downloads

4

Readme

Hon Meirin (红美铃) 频率限制库

红美铃(Hon Meirin)是系列作品《东方Project》中的角色,于《东方红魔乡》首次登场。 红美铃是住在红魔馆的妖怪之一。由于是担任门卫,在红魔馆的妖怪里面是和人类接触率较高的。

开发中,可能会缺少文档或者文档错误,请见谅。

Quick Start

const { Limiter } = require('meirin')
const limiter = new Limiter({
    prefix: 'darts', // 缓存的前缀
    rules: [{
        expression: 'app_id',
        limitation: '10/1s:500/1min:10000/1h'
    }]
})

limiter.request({app_id: 'BMW'}).then(status => {
    console.log(status.allowed) // 是否允许这次访问
    console.log(status.limit) // 总共限制访问的次数
    console.log(status.expires) // 过期时间,单位 ms
    console.log(status.times) // 已经访问了多少次了
})

Rule 规则

// Rule Schema
interface Rule {
    id?: string, // 每一个规则都一个 ID,用于区分不同的规则。会自动生成
    expression: string, // 表达式规则
    limitation: string, // 限制频率和间隔
    weight?: number, // 权重
}

Expression 表达式

<维度> [操作符 值]

  • 维度 是要进行限制的维度
  • 操作符 是进行逻辑运算的操作符
    • = 相等比较
    • != >= <= < >
    • 可以自定义操作符
  • 需要进行比较的值
    • 字符串' 或者 " 包裹
    • 正则表达式/ 包裹,支持标志位
    • 数字 符合 JS 数字字面量
    • true/false 布尔值

Limitation 频率限制

<次数1>/<时间1>[:<次数2>/<时间2>[...]]

其中 次数 必须是整数,时间 符合 ms 包的格式

存储层

默认是内存存储,也可以通过继承 Store 进行扩展

const {Store, Limiter} = require('meirin')

class OtherStore extends Store {
    // Overwrite methods
}

const limiter = new Limiter({
    store: new OtherStore(),
})

Test

npm run test

Benchmark

npm run bench