Skip to content

Commit ce3bde2

Browse files
feat: Use prefix instead of name.
Fixes #3
1 parent 716d35e commit ce3bde2

File tree

9 files changed

+34
-27
lines changed

9 files changed

+34
-27
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@acpr/rate-limit-postgresql",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A PostgreSQL store for the `express-rate-limit` middleware",
55
"homepage": "https://github.com/adrianprelipcean/express-rate-limit-postgresql",
66
"repository": "https://github.com/adrianprelipcean/express-rate-limit-postgresql",
@@ -61,7 +61,7 @@
6161
],
6262
"dependencies": {
6363
"@types/pg-pool": "2.0.3",
64-
"express-rate-limit": "6.11.0",
64+
"express-rate-limit": "6.11.1",
6565
"pg": "8.11.3",
6666
"pg-pool": "3.6.1",
6767
"postgres-migrations": "5.3.0"

readme.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ Both types of store take the same input in their constructor
103103

104104
- `config` - The database configuration as specified in the
105105
[node-postgres](https://node-postgres.com/apis/client) configuration.
106-
- `name` - The unique name of the session. This is useful when applying multiple
107-
rate limiters with multiple stores.
106+
- `prefix` - The unique name of the session (persisted in the database). Used by
107+
the double-count check to avoid false-positives when a key is counted twice,
108+
but with different prefixes.
108109

109110
## Installation
110111

source/stores/aggregated_ip/store_aggregated_ip.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class PostgresStoreAggregatedIP implements Store {
1818
config: any
1919

2020
/**
21-
* The name of the session
21+
* Optional value that the store prepends to keys
22+
*
23+
* Used by the double-count check to avoid false-positives when a key is counted twice, but with different prefixes
2224
*/
23-
name: string
25+
prefix: string
2426

2527
/**
2628
* The type of session (as an enum)
@@ -56,11 +58,11 @@ class PostgresStoreAggregatedIP implements Store {
5658
* @constructor for `PostgresStoreAggregatedIP`.
5759
*
5860
* @param config {any} - The database configuration as specified in https://node-postgres.com/apis/client.
59-
* @param name {string} - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores.
61+
* @param prefix {string} - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores.
6062
*/
61-
constructor(config: any, name: string) {
63+
constructor(config: any, prefix: string) {
6264
this.config = config
63-
this.name = name
65+
this.prefix = prefix
6466
applyMigrations(config)
6567
}
6668

@@ -96,7 +98,7 @@ class PostgresStoreAggregatedIP implements Store {
9698
`
9799
if (!isSessionValid(this.session)) {
98100
this.session = await getSession(
99-
this.name,
101+
this.prefix,
100102
this.SESSION_TYPE,
101103
this.windowMs,
102104
this.pool,

source/stores/individual_ip/store_individual_ip.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ class PostgresStoreIndividualIP implements Store {
2323
pool: any
2424

2525
/**
26-
* The name of the session
26+
* Optional value that the store prepends to keys
27+
*
28+
* Used by the double-count check to avoid false-positives when a key is counted twice, but with different prefixes
2729
*/
28-
name: string
30+
prefix: string
2931

3032
/**
3133
* The type of session (as an enum)
@@ -56,11 +58,11 @@ class PostgresStoreIndividualIP implements Store {
5658
* @constructor for `PostgresStoreIndividualIP`.
5759
*
5860
* @param config {any} - The database configuration as specified in https://node-postgres.com/apis/client.
59-
* @param name {string} - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores.
61+
* @param prefix {string} - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores.
6062
*/
61-
constructor(config: any, name: string) {
63+
constructor(config: any, prefix: string) {
6264
this.config = config
63-
this.name = name
65+
this.prefix = prefix
6466
applyMigrations(config)
6567
}
6668

@@ -94,7 +96,7 @@ class PostgresStoreIndividualIP implements Store {
9496
'SELECT count(id) AS count FROM rate_limit.individual_records WHERE key = $1 AND session_id = $2'
9597
if (!isSessionValid(this.session)) {
9698
this.session = await getSession(
97-
this.name,
99+
this.prefix,
98100
this.SESSION_TYPE,
99101
this.windowMs,
100102
this.pool,

test/stores/store_aggregated_ip.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ describe('Postgres Store Aggregated IP', () => {
5050
isSessionValidSpy.restore()
5151
})
5252

53-
it('constructor should call correct functions', async () => {
54-
new PostgresStore({}, 'test')
53+
it('constructor should call correct functions and populate correct fields', async () => {
54+
let testStore = new PostgresStore({}, 'test')
55+
assert.equal(testStore.prefix, 'test')
5556
sinon.assert.callCount(applyMigrationsStub, 1)
5657
})
5758

test/stores/store_individual_ip.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ describe('Postgres Store Individual IP', () => {
5050
isSessionValidSpy.restore()
5151
})
5252

53-
it('constructor should call correct functions', async () => {
54-
new PostgresStoreIndividualIP({}, 'test')
53+
it('constructor should call correct functions and populate correct fields', async () => {
54+
let testStore = new PostgresStoreIndividualIP({}, 'test')
5555
sinon.assert.callCount(applyMigrationsStub, 1)
56+
assert.equal(testStore.prefix, 'test')
5657
})
5758

5859
it('increment function should follow expected business logic', async () => {

third_party_licenses/dev_detailed.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"@acpr/[email protected].0": {
2+
"@acpr/[email protected].1": {
33
"licenses": "MIT",
44
"repository": "https://github.com/adrianprelipcean/express-rate-limit-postgresql",
55
"publisher": "Adrian C. Prelipcean"

third_party_licenses/production_detailed.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"@acpr/[email protected].0": {
2+
"@acpr/[email protected].1": {
33
"licenses": "MIT",
44
"repository": "https://github.com/adrianprelipcean/express-rate-limit-postgresql",
55
"publisher": "Adrian C. Prelipcean"
@@ -107,7 +107,7 @@
107107
"licenses": "MIT",
108108
"repository": "https://github.com/jshttp/etag"
109109
},
110-
110+
111111
"licenses": "MIT",
112112
"repository": "https://github.com/express-rate-limit/express-rate-limit",
113113
"publisher": "Nathan Friedly"

0 commit comments

Comments
 (0)