UNPKG

trigger.dev

Version:

A Command-Line Interface for Trigger.dev projects

70 lines • 2.68 kB
import { fromFriendlyId } from "@trigger.dev/core/v3"; import { USAGE_JWT_REFRESH_BEFORE_EXPIRY_SECONDS, isUsageReportingEnabled, } from "@trigger.dev/core/v3/workers"; export class UsageJwtRefresher { opts; expiresAt; /** Set when the initial credential is not renewable here; the run keeps the legacy billing path. */ fallbackReason; constructor(opts) { this.opts = opts; if (!isUsageReportingEnabled(opts.usageOptions)) return; const claims = claimsOf(opts.usageOptions.jwt); if (!claims || claims.usage_auth !== "control-plane-v1" || claims.run_id !== fromFriendlyId(opts.runId) || claims.usage_attempt !== String(opts.attemptNumber)) { this.fallbackReason = "initial billing credential is not renewable by the control plane for this run and attempt; using legacy billing renewal"; return; } this.expiresAt = claims.exp; } get isDue() { return (this.expiresAt !== undefined && this.expiresAt - (this.opts.now?.() ?? Date.now()) / 1_000 <= USAGE_JWT_REFRESH_BEFORE_EXPIRY_SECONDS); } report() { return this.isDue ? { expiresAt: this.expiresAt, attemptNumber: this.opts.attemptNumber } : undefined; } async installReplacement(token) { if (this.expiresAt === undefined) return false; const claims = claimsOf(token); if (!claims || claims.run_id !== fromFriendlyId(this.opts.runId) || claims.usage_attempt !== String(this.opts.attemptNumber) || claims.usage_auth !== "control-plane-v1" || claims.exp - (this.opts.now?.() ?? Date.now()) / 1_000 <= USAGE_JWT_REFRESH_BEFORE_EXPIRY_SECONDS) { return false; } try { const installed = await this.opts.install({ runId: this.opts.runId, attemptNumber: this.opts.attemptNumber, token, }); if (installed) this.expiresAt = claims.exp; return installed; } catch { return false; } } } // Timing/identity checks only. The replacement is received over the control-plane connection. function claimsOf(token) { try { const claims = JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString("utf8")); return typeof claims?.exp === "number" && Number.isFinite(claims.exp) ? claims : undefined; } catch { return undefined; } } //# sourceMappingURL=usageJwtRefresh.js.map