11import { Ratelimit } from "@upstash/ratelimit" ;
22import type { RuntimeEnvironmentType } from "@trigger.dev/database" ;
3- import { createHash } from "node:crypto" ;
43import { env } from "~/env.server" ;
54import { getCurrentPlan } from "~/services/platform.v3.server" ;
65import {
@@ -90,13 +89,11 @@ export class LimitsPresenter extends BasePresenter {
9089 projectId,
9190 environmentId,
9291 environmentType,
93- environmentApiKey,
9492 } : {
9593 organizationId : string ;
9694 projectId : string ;
9795 environmentId : string ;
9896 environmentType : RuntimeEnvironmentType ;
99- environmentApiKey : string ;
10097 } ) : Promise < LimitsResult > {
10198 // Get organization with all limit-related fields
10299 const organization = await this . _replica . organization . findFirstOrThrow ( {
@@ -168,10 +165,10 @@ export class LimitsPresenter extends BasePresenter {
168165 where : { organizationId } ,
169166 } ) ;
170167
171- // Get current rate limit tokens for this environment's API key
168+ // Get current rate limit tokens for this environment's API bucket
172169 const apiRateLimitTokens = await getRateLimitRemainingTokens (
173170 "api" ,
174- environmentApiKey ,
171+ environmentId ,
175172 apiRateLimitConfig
176173 ) ;
177174 // Batch rate limiter uses environment ID directly (not hashed) with a different key prefix
@@ -454,20 +451,14 @@ function resolveBatchConcurrencyConfig(batchConcurrencyConfig?: unknown): {
454451
455452/**
456453 * Query the current remaining tokens for a rate limiter using the Upstash getRemaining method.
457- * This uses the same configuration and hashing logic as the rate limit middleware .
454+ * The API limiter uses the environment ID as the bucket identifier for private API keys .
458455 */
459456async function getRateLimitRemainingTokens (
460457 keyPrefix : string ,
461- apiKey : string ,
458+ identifier : string ,
462459 config : RateLimiterConfig
463460) : Promise < number | null > {
464461 try {
465- // Hash the authorization header the same way the rate limiter does
466- const authorizationValue = `Bearer ${ apiKey } ` ;
467- const hash = createHash ( "sha256" ) ;
468- hash . update ( authorizationValue ) ;
469- const hashedKey = hash . digest ( "hex" ) ;
470-
471462 // Create a Ratelimit instance with the same configuration
472463 const limiter = createLimiterFromConfig ( config ) ;
473464 const ratelimit = new Ratelimit ( {
@@ -478,9 +469,9 @@ async function getRateLimitRemainingTokens(
478469 prefix : `ratelimit:${ keyPrefix } ` ,
479470 } ) ;
480471
481- // Use the getRemaining method to get the current remaining tokens
472+ // Use the same identifier as the API rate-limit middleware.
482473 // getRemaining returns a Promise<number>
483- const remaining = await ratelimit . getRemaining ( hashedKey ) ;
474+ const remaining = await ratelimit . getRemaining ( identifier ) ;
484475 return remaining ;
485476 } catch ( error ) {
486477 logger . warn ( "Failed to get rate limit remaining tokens" , {
0 commit comments