forked from adonisjs/limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.ts
More file actions
161 lines (153 loc) · 5.61 KB
/
database.ts
File metadata and controls
161 lines (153 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* @adonisjs/limiter
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import string from '@adonisjs/core/helpers/string'
import { RuntimeException } from '@adonisjs/core/exceptions'
import type { QueryClientContract } from '@adonisjs/lucid/types/database'
import { RateLimiterMySQL, RateLimiterPostgres, RateLimiterSQLite } from 'rate-limiter-flexible'
import debug from '../debug.js'
import RateLimiterBridge from './bridge.js'
import type { LimiterDatabaseStoreConfig } from '../types.js'
/**
* Limiter database store wraps the "RateLimiterMySQL" or "RateLimiterPostgres"
* implementations from the "rate-limiter-flixible" package.
*/
export default class LimiterDatabaseStore extends RateLimiterBridge {
#config: LimiterDatabaseStoreConfig
#client: QueryClientContract
get name() {
return 'database'
}
constructor(client: QueryClientContract, config: LimiterDatabaseStoreConfig) {
const dialectName = client.dialect.name
if (
dialectName !== 'mysql' &&
dialectName !== 'postgres' &&
dialectName !== 'better-sqlite3' &&
dialectName !== 'sqlite3'
) {
throw new RuntimeException(
`Unsupported database "${dialectName}". The limiter can only work with PostgreSQL, MySQL, and SQLite databases`
)
}
debug('creating %s limiter store %O', dialectName, config)
switch (dialectName) {
case 'mysql':
super(
new RateLimiterMySQL({
storeType: 'knex',
storeClient: client.getWriteClient(),
tableCreated: true,
dbName: config.dbName,
tableName: config.tableName,
keyPrefix: config.keyPrefix,
execEvenly: config.execEvenly,
points: config.requests,
clearExpiredByTimeout: config.clearExpiredByTimeout,
duration: string.seconds.parse(config.duration),
inMemoryBlockOnConsumed: config.inMemoryBlockOnConsumed,
blockDuration: config.blockDuration
? string.seconds.parse(config.blockDuration)
: undefined,
inMemoryBlockDuration: config.inMemoryBlockDuration
? string.seconds.parse(config.inMemoryBlockDuration)
: undefined,
})
)
this.#client = client
this.#config = config
break
case 'postgres':
super(
new RateLimiterPostgres({
storeType: 'knex',
schemaName: config.schemaName,
storeClient: client.getWriteClient(),
tableCreated: true,
dbName: config.dbName,
tableName: config.tableName,
keyPrefix: config.keyPrefix,
execEvenly: config.execEvenly,
points: config.requests,
clearExpiredByTimeout: config.clearExpiredByTimeout,
duration: string.seconds.parse(config.duration),
inMemoryBlockOnConsumed: config.inMemoryBlockOnConsumed,
blockDuration: config.blockDuration
? string.seconds.parse(config.blockDuration)
: undefined,
inMemoryBlockDuration: config.inMemoryBlockDuration
? string.seconds.parse(config.inMemoryBlockDuration)
: undefined,
})
)
this.#client = client
this.#config = config
break
case 'better-sqlite3':
super(
new RateLimiterSQLite({
storeType: 'knex',
storeClient: client.getWriteClient(),
tableCreated: true,
dbName: config.dbName,
tableName: config.tableName,
keyPrefix: config.keyPrefix,
execEvenly: config.execEvenly,
points: config.requests,
clearExpiredByTimeout: config.clearExpiredByTimeout,
duration: string.seconds.parse(config.duration),
inMemoryBlockOnConsumed: config.inMemoryBlockOnConsumed,
blockDuration: config.blockDuration
? string.seconds.parse(config.blockDuration)
: undefined,
inMemoryBlockDuration: config.inMemoryBlockDuration
? string.seconds.parse(config.inMemoryBlockDuration)
: undefined,
})
)
this.#client = client
this.#config = config
break
case 'sqlite3':
super(
new RateLimiterSQLite({
storeType: 'knex',
storeClient: client.getWriteClient(),
tableCreated: true,
dbName: config.dbName,
tableName: config.tableName,
keyPrefix: config.keyPrefix,
execEvenly: config.execEvenly,
points: config.requests,
clearExpiredByTimeout: config.clearExpiredByTimeout,
duration: string.seconds.parse(config.duration),
inMemoryBlockOnConsumed: config.inMemoryBlockOnConsumed,
blockDuration: config.blockDuration
? string.seconds.parse(config.blockDuration)
: undefined,
inMemoryBlockDuration: config.inMemoryBlockDuration
? string.seconds.parse(config.inMemoryBlockDuration)
: undefined,
})
)
this.#client = client
this.#config = config
break
}
}
/**
* Deletes all rows from the database table. Make sure to
* use separate database tables for every rate limiter
* your configure.
*/
async clear() {
debug('truncating database table %s', this.#config.tableName)
this.deleteInMemoryBlockedKeys()
await this.#client.dialect.truncate(this.#config.tableName, true)
}
}