File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import consola from "consola"
2+
3+ import type { State } from "./state"
4+
5+ import { HTTPError } from "./http-error"
6+
7+ export function checkRateLimit ( state : State ) {
8+ if ( state . rateLimitSeconds === undefined ) return
9+
10+ const now = Date . now ( )
11+
12+ if ( ! state . lastRequestTimestamp ) {
13+ state . lastRequestTimestamp = now
14+ return
15+ }
16+
17+ const elapsedSeconds = ( now - state . lastRequestTimestamp ) / 1000
18+
19+ if ( elapsedSeconds > state . rateLimitSeconds ) {
20+ state . lastRequestTimestamp = now
21+ return
22+ }
23+
24+ const waitTimeSeconds = Math . round ( state . rateLimitSeconds - elapsedSeconds )
25+ consola . warn (
26+ `Rate limit exceeded. Need to wait ${ waitTimeSeconds } more seconds.` ,
27+ )
28+
29+ throw new HTTPError ( "Rate limit exceeded" , Response . json ( { status : 429 } ) )
30+ }
You can’t perform that action at this time.
0 commit comments