27 lines
704 B
JavaScript
27 lines
704 B
JavaScript
const store = new Map();
|
|
export function makeIdempotencyKey(scope, key) {
|
|
return `${scope}:${key}`;
|
|
}
|
|
export function readIdempotency(scope, key) {
|
|
const entry = store.get(makeIdempotencyKey(scope, key));
|
|
if (!entry) {
|
|
return null;
|
|
}
|
|
if (entry.expiresAt < Date.now()) {
|
|
store.delete(makeIdempotencyKey(scope, key));
|
|
return null;
|
|
}
|
|
return entry.value;
|
|
}
|
|
export function writeIdempotency(scope, key, value, ttlMs) {
|
|
store.set(makeIdempotencyKey(scope, key), {
|
|
key,
|
|
scope,
|
|
value,
|
|
expiresAt: Date.now() + ttlMs
|
|
});
|
|
}
|
|
export function clearIdempotency(scope, key) {
|
|
store.delete(makeIdempotencyKey(scope, key));
|
|
}
|