提交
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
export function createIdempotencyKey(prefix = 'client') {
|
||||
const nativeUuid = globalThis.crypto?.randomUUID
|
||||
if (typeof nativeUuid === 'function') return nativeUuid.call(globalThis.crypto)
|
||||
|
||||
const bytes = new Uint8Array(16)
|
||||
if (typeof globalThis.crypto?.getRandomValues === 'function') {
|
||||
globalThis.crypto.getRandomValues(bytes)
|
||||
} else {
|
||||
for (let index = 0; index < bytes.length; index += 1) {
|
||||
bytes[index] = Math.floor(Math.random() * 256)
|
||||
}
|
||||
}
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80
|
||||
const hex = Array.from(bytes, value => value.toString(16).padStart(2, '0')).join('')
|
||||
const uuid = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
|
||||
return prefix ? `${prefix}-${uuid}` : uuid
|
||||
}
|
||||
Reference in New Issue
Block a user