web: add uuid() function with fallback if randomUUID is missing

This commit is contained in:
jj
2025-06-11 14:18:04 +00:00
parent eb90843fc9
commit a06baa41c1
4 changed files with 27 additions and 7 deletions

View File

@@ -26,3 +26,20 @@ export const ffmpegMetadataArgs = (metadata: CobaltFileMetadata) =>
}
return [];
});
const digit = () => '0123456789abcdef'[Math.random() * 16 | 0];
export const uuid = () => {
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
return crypto.randomUUID();
}
const digits = Array.from({length: 32}, digit);
digits[12] = '4';
digits[16] = '89ab'[Math.random() * 4 | 0];
return digits
.join('')
.match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/)!
.slice(1)
.join('-');
}