@acpr/rate-limit-postgresql
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -20,2 +20,2 @@ MIT License | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
{ | ||
"name": "@acpr/rate-limit-postgresql", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A PostgreSQL store for the `express-rate-limit` middleware", | ||
@@ -11,25 +11,24 @@ "homepage": "https://github.com/adrianprelipcean/express-rate-limit-postgresql", | ||
"license": "MIT", | ||
"types": "./dist/cjs/types/index.d.ts", | ||
"main": "./dist/cjs/index.js", | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.cts", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/esm/types/index.d.ts", | ||
"default": "./dist/esm/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/types/index.d.ts", | ||
"default": "./dist/cjs/index.js" | ||
} | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "mocha -r ts-node/register 'test/**/*.ts'", | ||
"test": "cross-env TS_NODE_PROJECT='./configs/tsconfig.esm.json' mocha", | ||
"clean": "rm -rf ./dist", | ||
"build": "npm run clean && npm run build:esm && npm run build:cjs && cp source/migrations/ dist/ -r", | ||
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv dist/esm/index.js dist/esm/index.mjs", | ||
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json", | ||
"build": "npm run clean && pkgroll --src source/ && cp source/migrations/ dist/ -r && npm run mjs-fix-prepend", | ||
"lint": "prettier --check .", | ||
"lint-autofix": "prettier --write .", | ||
"prepack": "npm run build", | ||
@@ -39,3 +38,4 @@ "prepare": "husky install", | ||
"generate-production-licenses": "./node_modules/license-checker-rseidelsohn/bin/license-checker-rseidelsohn --limitAttributes licenses,repository,publisher,email --json --production > third_party_licenses/production_detailed.json && ./node_modules/license-checker-rseidelsohn/bin/license-checker-rseidelsohn --summary --production > third_party_licenses/production_summary.txt", | ||
"generate-licenses": "npm run generate-dev-licenses && npm run generate-production-licenses" | ||
"generate-licenses": "npm run generate-dev-licenses && npm run generate-production-licenses", | ||
"mjs-fix-prepend": "printf '%s\\n%s\\n' \"import { dirname } from 'path'\nimport { fileURLToPath } from 'url';\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\" \"$(cat dist/index.mjs)\" > dist/index.mjs" | ||
}, | ||
@@ -65,2 +65,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@types/pg-pool": "2.0.3", | ||
"express-rate-limit": "6.11.0", | ||
@@ -77,5 +78,6 @@ "pg": "8.11.3", | ||
"@types/mocha": "10.0.1", | ||
"@types/pg-pool": "2.0.3", | ||
"@types/sinon": "10.0.16", | ||
"chai": "4.3.8", | ||
"cross-env": "7.0.3", | ||
"global-jsdom": "9.1.0", | ||
"husky": "8.0.3", | ||
@@ -85,2 +87,3 @@ "license-checker-rseidelsohn": "3.3.0", | ||
"mocha": "10.2.0", | ||
"pkgroll": "1.11.0", | ||
"prettier": "3.0.3", | ||
@@ -93,2 +96,3 @@ "sinon": "15.2.0", | ||
"lint-staged": { | ||
"*.md": "prettier --write", | ||
"source/**/*.ts": "prettier --write", | ||
@@ -95,0 +99,0 @@ "test/**/*.ts": "prettier --write", |
@@ -5,3 +5,5 @@ # rate-limit-postgresql | ||
A [`PostgreSQL`](https://www.postgresql.org/) store for the [`express-rate-limit`](https://github.com/nfriedly/express-rate-limit) middleware. | ||
A [`PostgreSQL`](https://www.postgresql.org/) store for the | ||
[`express-rate-limit`](https://github.com/nfriedly/express-rate-limit) | ||
middleware. | ||
@@ -22,36 +24,37 @@ ## Installation | ||
```js | ||
let rateLimit = require('express-rate-limit'); | ||
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, | ||
}); | ||
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); | ||
app.use(limiter) | ||
``` | ||
### Importing | ||
This library is provided in ESM as well as CJS forms, and works with both Javascript and Typescript projects. | ||
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: | ||
Import it in a CommonJS project (`type: commonjs` or no `type` field in | ||
`package.json`) as follows: | ||
@@ -68,34 +71,42 @@ ```ts | ||
## 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 | ||
1. `PostgresStoreAggregatedIP` (with the default `PostgresStore` constructor)- | ||
which aggregates the IP count in the table, as shown in the following table | ||
| key | session_id | count | | ||
|-------------|------------|-------| | ||
| ----------- | ---------- | ----- | | ||
| 192.168.1.1 | 1 | 3 | | ||
| 192.168.2.1 | 1 | 1 | | ||
2. `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 | ||
2. `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 | ||
| id | key | session_id | event_time | | ||
| --- | ----------- | ---------- | ------------------------- | | ||
| 1 | 192.168.1.1 | 1 | 2023-09-13T07:40:09+00:00 | | ||
| 2 | 192.168.1.1 | 1 | 2023-09-13T07:40:10+00:00 | | ||
| 3 | 192.168.1.1 | 1 | 2023-09-13T07:40:11+00:00 | | ||
| 4 | 192.168.2.1 | 1 | 2023-09-13T07:40:11+00:00 | | ||
| id | key | session_id | event_time | | ||
|----|-------------|------------|---------------------------| | ||
| 1 | 192.168.1.1 | 1 | 2023-09-13T07:40:09+00:00 | | ||
| 2 | 192.168.1.1 | 1 | 2023-09-13T07:40:10+00:00 | | ||
| 3 | 192.168.1.1 | 1 | 2023-09-13T07:40:11+00:00 | | ||
| 4 | 192.168.2.1 | 1 | 2023-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. | ||
> 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](https://node-postgres.com/apis/client) configuration. | ||
- `name` - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores. | ||
Both types of store take the same input in their constructor | ||
- `config` - The database configuration as specified in the | ||
[node-postgres](https://node-postgres.com/apis/client) configuration. | ||
- `name` - The unique name of the session. This is useful when applying multiple | ||
rate limiters with multiple stores. | ||
## Installation | ||
Project license is specified in the [license file](license.md). Third party licenses are located in the [third_party_licenses folder](third_party_licenses) | ||
Project license is specified in the [license file](license.md). Third party | ||
licenses are located in the [third_party_licenses folder](third_party_licenses) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
110
40662
5
18
8
790
1
+ Added@types/pg-pool@2.0.3
+ Added@types/node@22.13.0(transitive)
+ Added@types/pg@8.11.11(transitive)
+ Added@types/pg-pool@2.0.3(transitive)
+ Addedobuf@1.1.2(transitive)
+ Addedpg-numeric@1.0.2(transitive)
+ Addedpg-types@4.0.2(transitive)
+ Addedpostgres-array@3.0.2(transitive)
+ Addedpostgres-bytea@3.0.0(transitive)
+ Addedpostgres-date@2.1.0(transitive)
+ Addedpostgres-interval@3.0.0(transitive)
+ Addedpostgres-range@1.1.4(transitive)
+ Addedundici-types@6.20.0(transitive)