30 lines
736 B
JavaScript
30 lines
736 B
JavaScript
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,
|
|
};
|
|
|