@@ -2,13 +2,17 @@ import { Database, eq, and, sql, inArray } from "@opencode-ai/console-core/drizz
22import { IpRateLimitTable } from "@opencode-ai/console-core/schema/ip.sql.js"
33import { RateLimitError } from "./error"
44import { logger } from "./logger"
5+ import { ZenData } from "@opencode-ai/console-core/model.js"
56
6- export function createRateLimiter ( limit : number | undefined , rawIp : string ) {
7+ export function createRateLimiter ( limit : ZenData . RateLimit | undefined , rawIp : string ) {
78 if ( ! limit ) return
89
910 const ip = ! rawIp . length ? "unknown" : rawIp
1011 const now = Date . now ( )
11- const intervals = [ buildYYYYMMDDHH ( now ) , buildYYYYMMDDHH ( now - 3_600_000 ) , buildYYYYMMDDHH ( now - 7_200_000 ) ]
12+ const intervals =
13+ limit . period === "day"
14+ ? [ buildYYYYMMDD ( now ) ]
15+ : [ buildYYYYMMDDHH ( now ) , buildYYYYMMDDHH ( now - 3_600_000 ) , buildYYYYMMDDHH ( now - 7_200_000 ) ]
1216
1317 return {
1418 track : async ( ) => {
@@ -28,11 +32,18 @@ export function createRateLimiter(limit: number | undefined, rawIp: string) {
2832 )
2933 const total = rows . reduce ( ( sum , r ) => sum + r . count , 0 )
3034 logger . debug ( `rate limit total: ${ total } ` )
31- if ( total >= limit ) throw new RateLimitError ( `Rate limit exceeded. Please try again later.` )
35+ if ( total >= limit . value ) throw new RateLimitError ( `Rate limit exceeded. Please try again later.` )
3236 } ,
3337 }
3438}
3539
40+ function buildYYYYMMDD ( timestamp : number ) {
41+ return new Date ( timestamp )
42+ . toISOString ( )
43+ . replace ( / [ ^ 0 - 9 ] / g, "" )
44+ . substring ( 0 , 8 )
45+ }
46+
3647function buildYYYYMMDDHH ( timestamp : number ) {
3748 return new Date ( timestamp )
3849 . toISOString ( )
0 commit comments