Merge pull request #19084 from Snuffleupagus/getUuid-shorten
Simplify the `getUuid` helper function
This commit is contained in:
commit
445b7ece61
@ -1075,21 +1075,12 @@ function normalizeUnicode(str) {
|
|||||||
function getUuid() {
|
function getUuid() {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||||
(typeof crypto !== "undefined" && typeof crypto?.randomUUID === "function")
|
typeof crypto.randomUUID === "function"
|
||||||
) {
|
) {
|
||||||
return crypto.randomUUID();
|
return crypto.randomUUID();
|
||||||
}
|
}
|
||||||
const buf = new Uint8Array(32);
|
const buf = new Uint8Array(32);
|
||||||
if (
|
|
||||||
typeof crypto !== "undefined" &&
|
|
||||||
typeof crypto?.getRandomValues === "function"
|
|
||||||
) {
|
|
||||||
crypto.getRandomValues(buf);
|
crypto.getRandomValues(buf);
|
||||||
} else {
|
|
||||||
for (let i = 0; i < 32; i++) {
|
|
||||||
buf[i] = Math.floor(Math.random() * 255);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bytesToString(buf);
|
return bytesToString(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import {
|
|||||||
bytesToString,
|
bytesToString,
|
||||||
createValidAbsoluteUrl,
|
createValidAbsoluteUrl,
|
||||||
getModificationDate,
|
getModificationDate,
|
||||||
|
getUuid,
|
||||||
string32,
|
string32,
|
||||||
stringToBytes,
|
stringToBytes,
|
||||||
stringToPDFString,
|
stringToPDFString,
|
||||||
@ -248,4 +249,12 @@ describe("util", function () {
|
|||||||
expect(getModificationDate(date)).toEqual("31410609020653");
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user