@eristack/idempotency
Version:
Idempotency-Key guard with Drizzle store, scoped keys, lease, Express/Nest/client adapters
1 lines • 3.23 kB
Source Map (JSON)
{"version":3,"sources":["../../src/testing/index.ts","../../src/core/memory-store.ts"],"sourcesContent":["export { createMemoryIdempotencyStore } from \"../core/memory-store.js\";\n","import type { IdempotencyRecord, IdempotencyStore } from \"./types.js\";\n\nfunction isPendingActive(record: IdempotencyRecord, now: number) {\n return (\n record.state === \"pending\" &&\n (record.leaseExpiresAt == null || record.leaseExpiresAt > now)\n );\n}\n\nexport function createMemoryIdempotencyStore(): IdempotencyStore {\n const records = new Map<string, IdempotencyRecord>();\n\n return {\n async get(key) {\n return records.get(key) ?? null;\n },\n async claim(input) {\n const now = Date.now();\n const existing = records.get(input.key);\n if (existing?.state === \"completed\") return false;\n if (existing && isPendingActive(existing, now)) return false;\n if (existing?.state === \"failed\" || (existing?.state === \"pending\" && !isPendingActive(existing, now))) {\n records.set(input.key, {\n state: \"pending\",\n requestHash: input.requestHash,\n leaseExpiresAt: now + (input.leaseMs ?? 60_000),\n createdAt: existing.createdAt,\n });\n return true;\n }\n records.set(input.key, {\n state: \"pending\",\n requestHash: input.requestHash,\n leaseExpiresAt: now + (input.leaseMs ?? 60_000),\n createdAt: now,\n });\n return true;\n },\n async complete(key, result) {\n const prev = records.get(key);\n records.set(key, {\n state: \"completed\",\n result,\n requestHash: prev?.requestHash,\n createdAt: prev?.createdAt ?? Date.now(),\n });\n },\n async fail(key, errorMessage) {\n const prev = records.get(key);\n records.set(key, {\n state: \"failed\",\n errorMessage,\n requestHash: prev?.requestHash,\n createdAt: prev?.createdAt ?? Date.now(),\n });\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,SAAS,gBAAgB,QAA2B,KAAa;AAC/D,SACE,OAAO,UAAU,cAChB,OAAO,kBAAkB,QAAQ,OAAO,iBAAiB;AAE9D;AAEO,SAAS,+BAAiD;AAC/D,QAAM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