UNPKG

@eristack/idempotency

Version:

Idempotency-Key guard with Drizzle store, scoped keys, lease, Express/Nest/client adapters

118 lines (115 loc) • 3.71 kB
import { IdempotencyConflictError, IdempotencyRequestMismatchError, hashIdempotencyRequest } from "./chunk-UR7ALJYJ.js"; import { createMemoryIdempotencyStore } from "./chunk-KGXI6BT2.js"; import "./chunk-CGZBV6BD.js"; // src/core/scoped-key.ts function formatScopedIdempotencyKey(scope, key) { const tenant = scope.tenantId?.trim() || "_"; return `${tenant}:${scope.scope}:${key.trim()}`; } // src/core/guard-scoped.ts function isPendingActive(record, now) { return record.state === "pending" && (record.leaseExpiresAt == null || record.leaseExpiresAt > now); } async function waitForCompletion(store, storageKey, waitPollMs, maxWaitMs) { const deadline = Date.now() + maxWaitMs; while (Date.now() < deadline) { const row = await store.get(storageKey); if (!row) return null; if (row.state === "completed") return row; if (row.state === "failed") return row; if (row.state === "pending" && !isPendingActive(row, Date.now())) { return null; } await new Promise((r) => setTimeout(r, waitPollMs)); } throw new IdempotencyConflictError(storageKey); } async function runScopedIdempotency(options) { const storageKey = formatScopedIdempotencyKey(options.scope, options.key); const leaseMs = options.leaseMs ?? options.defaultLeaseMs ?? 6e4; const waitPollMs = options.waitPollMs ?? 50; const waitOnPending = options.waitOnPending ?? true; const existing = await options.store.get(storageKey); if (existing?.state === "completed") { if (existing.requestHash && existing.requestHash !== options.requestHash) { throw new IdempotencyRequestMismatchError(storageKey); } return existing.result; } if (existing && isPendingActive(existing, Date.now())) { if (!waitOnPending) { throw new IdempotencyConflictError(storageKey); } const waited = await waitForCompletion( options.store, storageKey, waitPollMs, leaseMs ); if (waited?.state === "completed") { if (waited.requestHash && waited.requestHash !== options.requestHash) { throw new IdempotencyRequestMismatchError(storageKey); } return waited.result; } throw new IdempotencyConflictError(storageKey); } const claimed = await options.store.claim({ key: storageKey, requestHash: options.requestHash, leaseMs }); if (!claimed) { const again = await options.store.get(storageKey); if (again?.state === "completed") { if (again.requestHash && again.requestHash !== options.requestHash) { throw new IdempotencyRequestMismatchError(storageKey); } return again.result; } throw new IdempotencyConflictError(storageKey); } try { const result = await options.fn(); await options.store.complete(storageKey, result); return result; } catch (err) { const message = err instanceof Error ? err.message : String(err); await options.store.fail(storageKey, message); throw err; } } // src/core/guard.ts function createIdempotencyGuard(storeOrOptions) { const options = "get" in storeOrOptions ? { store: storeOrOptions } : storeOrOptions; return { async run(key, fn) { const requestHash = await hashIdempotencyRequest(null); return runScopedIdempotency({ ...options, scope: { scope: "default" }, key, requestHash, fn }); }, runScoped(input) { return runScopedIdempotency({ ...options, ...input }); } }; } export { IdempotencyConflictError, IdempotencyRequestMismatchError, createIdempotencyGuard, createMemoryIdempotencyStore, formatScopedIdempotencyKey, hashIdempotencyRequest }; //# sourceMappingURL=index.js.map