36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
/**
|
|
* Browser compatibility utilities
|
|
*
|
|
* Polyfills and wrappers for APIs that aren't available in all browsers.
|
|
* Import individual helpers as needed.
|
|
*/
|
|
|
|
/**
|
|
* crypto.randomUUID() polyfill for Safari <15.4, Firefox <95
|
|
*/
|
|
export function randomUUID(): string {
|
|
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
return crypto.randomUUID();
|
|
}
|
|
// RFC 4122 v4 UUID fallback using crypto.getRandomValues
|
|
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
bytes[6] = (bytes[6] & 0x0f) | 0x40; // version 4
|
|
bytes[8] = (bytes[8] & 0x3f) | 0x80; // variant 10
|
|
const hex = Array.from(bytes, (b) => b.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)}`;
|
|
}
|
|
|
|
/**
|
|
* AbortSignal.timeout() polyfill for Safari <17, Firefox <122
|
|
*
|
|
* Returns an AbortSignal that aborts after the given milliseconds.
|
|
*/
|
|
export function timeoutSignal(ms: number): AbortSignal {
|
|
if (typeof AbortSignal.timeout === "function") {
|
|
return AbortSignal.timeout(ms);
|
|
}
|
|
const controller = new AbortController();
|
|
setTimeout(() => controller.abort(new DOMException("The operation was aborted due to timeout", "TimeoutError")), ms);
|
|
return controller.signal;
|
|
}
|