import { E as EnforceOptions } from './enforce-DOM8Sh43.cjs'; import './types-DKirIBQt.cjs'; import './core-CS9fzc6a.cjs'; /** * gRPC server adapter (grpc-js). Wraps a unary method handler with a rate-limit gate built on the * transport-agnostic {@link createEnforcer}: on allow it forwards to the handler; on deny it fails * the RPC with gRPC status `RESOURCE_EXHAUSTED` (8) and a `retry after` detail; a fail-closed store * outage fails with `UNAVAILABLE` (14). The call/callback shapes are modeled structurally, so a real * `@grpc/grpc-js` `ServerUnaryCall`/`sendUnaryData` satisfies them with no dependency on grpc-js. */ /** gRPC status code for a throttled call (maps to HTTP 429 at a gateway). */ declare const GRPC_RESOURCE_EXHAUSTED = 8; /** gRPC status code used when the limiter store is unreachable under a fail-closed policy. */ declare const GRPC_UNAVAILABLE = 14; /** The slice of a grpc-js `ServerUnaryCall` the adapter reads: the peer and (optionally) metadata. */ interface GrpcServerCallLike { /** The peer address string, e.g. `"ipv4:203.0.113.7:54321"`. The default limit key. */ getPeer(): string; } /** A grpc-js error completion: a status `code` and human-readable `details`. */ interface GrpcServiceError { code: number; details: string; } /** grpc-js `sendUnaryData`: complete the call with an error, or with a value. */ type GrpcSendUnaryData = (error: GrpcServiceError | null, value?: Res | null) => void; /** A grpc-js unary handler `(call, callback) => void`. */ type GrpcUnaryHandler = (call: Call, callback: GrpcSendUnaryData) => void; /** Options for {@link grpcRateLimit}. */ type GrpcRateLimitOptions = EnforceOptions & { /** Cost of a call in limiter units. A function computes it per call. Default 1. */ cost?: number | ((call: Call) => number); /** Derive the limit key from a call. Default: the peer address (`call.getPeer()`). */ key?: (call: Call) => string; }; /** A gRPC rate-limit gate: wrap method handlers with {@link GrpcRateLimiter.unary}. */ interface GrpcRateLimiter { /** Wrap a unary handler so it is only invoked when the call is under the limit. */ unary(handler: GrpcUnaryHandler): GrpcUnaryHandler; } /** * Build a gRPC rate-limit gate. Wrap each unary method handler with `.unary(...)`; the wrapper keys * the limit on the peer (override with `key`, e.g. read an API token from `call.metadata`), enforces * it, and either forwards to the handler or completes the call with `RESOURCE_EXHAUSTED`. * * @example * ```ts * import { grpcRateLimit } from "throttlekit/grpc"; * import { gcra } from "throttlekit"; * * const gate = grpcRateLimit({ strategy: gcra({ limit: 100, periodMs: 60_000 }) }); * server.addService(GreeterService, { sayHello: gate.unary(sayHelloImpl) }); * ``` */ declare function grpcRateLimit(options: GrpcRateLimitOptions): GrpcRateLimiter; export { GRPC_RESOURCE_EXHAUSTED, GRPC_UNAVAILABLE, type GrpcRateLimitOptions, type GrpcRateLimiter, type GrpcSendUnaryData, type GrpcServerCallLike, type GrpcServiceError, type GrpcUnaryHandler, grpcRateLimit };