Skip to content

Commit

Permalink
Merge pull request #19084 from Snuffleupagus/getUuid-shorten
Browse files Browse the repository at this point in the history
Simplify the `getUuid` helper function
  • Loading branch information
Snuffleupagus authored Nov 21, 2024
2 parents 07765e9 + c290a12 commit 445b7ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
bytesToString,
createValidAbsoluteUrl,
getModificationDate,
getUuid,
string32,
stringToBytes,
stringToPDFString,
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 445b7ec

Please sign in to comment.