32 lines
1012 B
TypeScript
32 lines
1012 B
TypeScript
/**
|
|
* 일반 HTTP에서는 `crypto.randomUUID`가 제공되지 않을 수 있어 Web Crypto 난수로 UUID v4를 생성한다.
|
|
* 백엔드 `clientRequestId`가 UUID 타입이므로 fallback도 같은 36자 형식을 유지한다.
|
|
*/
|
|
export const createDredgingClientUuid = () => {
|
|
if (
|
|
typeof crypto !== "undefined" &&
|
|
typeof crypto.randomUUID === "function"
|
|
) {
|
|
return crypto.randomUUID();
|
|
}
|
|
|
|
const bytes = new Uint8Array(16);
|
|
if (
|
|
typeof crypto !== "undefined" &&
|
|
typeof crypto.getRandomValues === "function"
|
|
) {
|
|
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("");
|
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
};
|