Initial commit
This commit is contained in:
29
shared/idempotency.js
Normal file
29
shared/idempotency.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const path = require('path');
|
||||
const { createHash } = require('crypto');
|
||||
const { idempotencyDir } = require('./paths');
|
||||
const { readJsonIfExists, writeJson } = require('./fs');
|
||||
|
||||
function recordPath(scope, key) {
|
||||
const hash = createHash('sha256').update(`${scope}:${key}`).digest('hex');
|
||||
return path.join(idempotencyDir, scope, `${hash}.json`);
|
||||
}
|
||||
|
||||
async function getIdempotencyRecord(scope, key) {
|
||||
return readJsonIfExists(recordPath(scope, key));
|
||||
}
|
||||
|
||||
async function saveIdempotencyRecord(scope, key, value) {
|
||||
await writeJson(recordPath(scope, key), {
|
||||
schema_version: 'v1',
|
||||
scope,
|
||||
key,
|
||||
value,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getIdempotencyRecord,
|
||||
saveIdempotencyRecord,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user