1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Merge pull request #19084 from Snuffleupagus/getUuid-shorten

Simplify the `getUuid` helper function
This commit is contained in:
Jonas Jenwald 2024-11-21 15:17:14 +01:00 committed by GitHub
commit 445b7ece61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 11 deletions

View file

@ -1075,21 +1075,12 @@ function normalizeUnicode(str) {
function getUuid() {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
(typeof crypto !== "undefined" && typeof crypto?.randomUUID === "function")
typeof crypto.randomUUID === "function"
) {
return crypto.randomUUID();
}
const buf = new Uint8Array(32);
if (
typeof crypto !== "undefined" &&
typeof crypto?.getRandomValues === "function"
) {
crypto.getRandomValues(buf);
} else {
for (let i = 0; i < 32; i++) {
buf[i] = Math.floor(Math.random() * 255);
}
}
crypto.getRandomValues(buf);
return bytesToString(buf);
}

View file

@ -18,6 +18,7 @@ import {
bytesToString,
createValidAbsoluteUrl,
getModificationDate,
getUuid,
string32,
stringToBytes,
stringToPDFString,
@ -248,4 +249,12 @@ describe("util", function () {
expect(getModificationDate(date)).toEqual("31410609020653");
});
});
describe("getUuid", function () {
it("should get uuid string", function () {
const uuid = getUuid();
expect(typeof uuid).toEqual("string");
expect(uuid.length).toBeGreaterThanOrEqual(32);
});
});
});