@acpr/rate-limit-postgresql

中文文档
1.4.1发布于 2024/03/18MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.3万

A PostgreSQL store for the express-rate-limit middleware

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Importing
  4. Configuration
  5. Types of Postgres Stores
  6. Constructor
  7. Installation

rate-limit-postgresql

Contributor Covenant

A PostgreSQL store for the express-rate-limit middleware.

Installation

From the npm registry:

# Using npm
> npm install --save @acpr/rate-limit-postgresql
# Using yarn or pnpm
> yarn/pnpm add @acpr/rate-limit-postgresql

Usage

Functional examples for using rate-limit-postgresql are found in the following repository

let rateLimit = require('express-rate-limit')
let postgresStores = require('@acpr/rate-limit-postgresql')

let limiter = new RateLimit({
    store: new postgresStores.PostgresStore(
        {
            user: 'postgres',
            password: 'postgres',
            host: 'localhost',
            database: 'rate-limit',
            port: 5432,
        },
        'aggregated_store',
    ),
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 3, // Limit each IP to 3 requests per `window` (here, per 15 minutes)
    message:
        'Too many accounts created from this IP, please try again after 15 minutes',
    standardHeaders: 'draft-7', // Set `RateLimit` and `RateLimit-Policy`` headers
    legacyHeaders: false,
})

//  apply to all requests
app.use(limiter)

Importing

This library is provided in ESM as well as CJS forms, and works with both Javascript and Typescript projects.

This package requires you to use Node 16 or above.

Import it in a CommonJS project (type: commonjs or no type field in package.json) as follows:

let postgresStores = require('@acpr/rate-limit-postgresql')

Import it in a ESM project (type: module in package.json) as follows:

import postgresStores from '@acpr/rate-limit-postgresql'

Configuration

Types of Postgres Stores

There are two different types of Postgres Stores:

  1. PostgresStoreAggregatedIP (with the default PostgresStore constructor)- which aggregates the IP count in the table, as shown in the following table
keysession_idcount
192.168.1.113
192.168.2.111
  1. PostgresStoreIndividualIP - which stores the IP of each request in a separate row (as shown in the following table) and performs the aggregation at a separate step
idkeysession_idevent_time
1192.168.1.112023-09-13T07:40:09+00:00
2192.168.1.112023-09-13T07:40:10+00:00
3192.168.1.112023-09-13T07:40:11+00:00
4192.168.2.112023-09-13T07:40:11+00:00

Note: The database uses UUID as a data type for IDs, the tables contain integers as IDs to keep illustration simple.

Constructor

Both types of store take the same input in their constructor

  • config - The database configuration as specified in the node-postgres configuration.
  • prefix - The unique name of the session (persisted in the database). Used by the double-count check to avoid false-positives when a key is counted twice, but with different prefixes.

Installation

Project license is specified in the license file. Third party licenses are located in the third_party_licenses folder

版本与发布记录

@acpr/rate-limit-postgresql 共发布 10 个版本,首次发布于 2023/09/13;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.1latest2024/03/182 年前
1.4.02024/02/272 年前
1.3.22023/10/103 年前
1.3.12023/10/023 年前
1.3.02023/09/293 年前
1.2.02023/09/213 年前

关于 @acpr/rate-limit-postgresql 的常见问题

@acpr/rate-limit-postgresql 是什么?

A PostgreSQL store for the express-rate-limit middleware

如何安装 @acpr/rate-limit-postgresql?

在项目目录运行 npm install @acpr/rate-limit-postgresql,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:express-rate-limit(>=6.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@acpr/rate-limit-postgresql 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@acpr/rate-limit-postgresql 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@acpr/rate-limit-postgresql 可以商用吗?

@acpr/rate-limit-postgresql 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@acpr/rate-limit-postgresql 的下载量有多少?

根据 npm 官方下载统计,@acpr/rate-limit-postgresql 在 2026/10/05 – 2026/11/03 这 30 天内被下载 13,422 次,日均约 447 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@acpr/rate-limit-postgresql 还在维护吗?

最新版本 1.4.1 发布于 2024/03/18(2 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。