Files
pda-uniapp/utils/idempotency.js

20 lines
742 B
JavaScript

function hash32(value, seed) {
let hash = seed >>> 0
for (let index = 0; index < value.length; index += 1) {
hash ^= value.charCodeAt(index)
hash = Math.imul(hash, 0x01000193) >>> 0
}
return hash.toString(16).padStart(8, '0')
}
export function shortStableHash(value) {
const input = String(value ?? '')
return `${hash32(input, 0x811c9dc5)}${hash32(input, 0x9e3779b9)}`
}
export function stableOperationId(scope, identity, payload) {
const safeScope = String(scope || 'operation').replace(/[^A-Za-z0-9_-]/g, '-').slice(0, 24)
const safeIdentity = String(identity || 'unknown').replace(/[^A-Za-z0-9_-]/g, '-').slice(0, 48)
return `${safeScope}-${safeIdentity}-${shortStableHash(JSON.stringify(payload ?? null))}`.slice(0, 120)
}