19
19
* For more project information, see <https://github.com/tmpim/Krist.js>.
20
20
*/
21
21
22
- import rateLimiter from "limiter" ;
22
+ import RateLimiterMemory from "rate- limiter-flexible/lib/RateLimiterMemory.js " ;
23
23
import os from "os" ;
24
24
25
25
import { KristApiResponse } from "../types/index.js" ;
26
26
27
- import packageJson from "../../package.json" with { "type" : "json" } ;
28
27
import { coerceKristError , KristErrorRateLimit } from "../errors/index.js" ;
29
28
import { argObject , argStringNonEmpty } from "../util/argValidation.js" ;
30
29
import { isNil } from "../util/internalUtil.js" ;
@@ -74,10 +73,10 @@ export const buildBody = (value: any): RequestInit => ({
74
73
} ) ;
75
74
76
75
// Global rate limiter so that multiple KristApi instances will share this
77
- const limiter = new rateLimiter . RateLimiter ( {
76
+ const limiter = new RateLimiterMemory ( {
78
77
// Slightly lower than server to account for timing differences
79
- tokensPerInterval : 300 ,
80
- interval : "minute"
78
+ points : 300 , // 300 requests per minute
79
+ duration : 60
81
80
} ) ;
82
81
83
82
/** Client class for the Krist API. {@link KristApiOptions | Options} may be
@@ -97,7 +96,7 @@ export class KristApi {
97
96
*/
98
97
constructor ( {
99
98
syncNode = "https://krist.dev/" ,
100
- userAgent = "krist.js/" + packageJson . version ,
99
+ userAgent = "krist.js" ,
101
100
} : KristApiOptions = { } ) {
102
101
argStringNonEmpty ( syncNode , "syncNode" ) ;
103
102
argStringNonEmpty ( userAgent , "userAgent" ) ;
@@ -139,10 +138,10 @@ export class KristApi {
139
138
const url = new URL ( endpoint . replace ( / ^ \/ / , "" ) + qss , this . syncNode ) ;
140
139
141
140
// Apply rate limiting
142
- await limiter . removeTokens ( 1 ) ;
141
+ await limiter . consume ( "krist-api" , 1 ) ;
143
142
144
143
const browserHeaders : any = typeof window !== "undefined" ? {
145
- "Library-Agent" : "krist.js/" + packageJson . version ,
144
+ "Library-Agent" : "krist.js" ,
146
145
} : { } ;
147
146
const nodeHeaders : any = typeof window === "undefined" ? {
148
147
"Library-Agent" : `${ this . userAgent } (${ os . platform ( ) } ${ os . release ( ) } ; Node ${ process . version } )` ,
@@ -151,7 +150,7 @@ export class KristApi {
151
150
const res = await fetch ( url . toString ( ) , {
152
151
method,
153
152
headers : {
154
- "Library-Agent" : "krist.js/" + packageJson . version ,
153
+ "Library-Agent" : "krist.js" ,
155
154
"Accept" : "application/json" ,
156
155
...browserHeaders ,
157
156
...nodeHeaders ,
0 commit comments