Skip to content

Commit 5b368dd

Browse files
committed
Remove the Uint8Array.prototype.toHex(), Uint8Array.prototype.toBase64(), and Uint8Array.fromBase64() polyfills
(During rebasing of the previous patches I happened to look at the polyfills and noticed that this one could be removed now.) See: - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toHex#browser_compatibility - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64#browser_compatibility - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64#browser_compatibility Note that technically this functionality can still be disabled via a preference in Firefox, however that's slated for removal in [bug 1985120](https://bugzilla.mozilla.org/show_bug.cgi?id=1985120). Looking at the Firefox source-code, see https://searchfox.org/firefox-main/search?q=array.tobase64%28%29&path=&case=false&regexp=false, you can see that it's already being used *unconditionally* elsewhere in the browser hence removing the polyfills ought to be fine (since toggling the preference would break other parts of the browser).
1 parent 247ee02 commit 5b368dd

11 files changed

Lines changed: 23 additions & 67 deletions

File tree

src/core/document.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
stringToBytes,
2828
stringToPDFString,
2929
stringToUTF8String,
30-
toHexUtil,
3130
unreachable,
3231
Util,
3332
warn,
@@ -1605,8 +1604,8 @@ class PDFDocument {
16051604
}
16061605

16071606
return shadow(this, "fingerprints", [
1608-
toHexUtil(hashOriginal),
1609-
hashModified ? toHexUtil(hashModified) : null,
1607+
hashOriginal.toHex(),
1608+
hashModified?.toHex() ?? null,
16101609
]);
16111610
}
16121611

src/core/xfa/template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ import {
9090
XFAObject,
9191
XFAObjectArray,
9292
} from "./xfa_object.js";
93-
import { fromBase64Util, Util, warn } from "../../shared/util.js";
9493
import {
9594
getBBox,
9695
getColor,
@@ -103,6 +102,7 @@ import {
103102
getStringOption,
104103
HTMLResult,
105104
} from "./utils.js";
105+
import { Util, warn } from "../../shared/util.js";
106106
import { getMetrics } from "./fonts.js";
107107
import { recoverJsURL } from "../core_utils.js";
108108
import { searchNode } from "./som.js";
@@ -3420,7 +3420,7 @@ class Image extends StringObject {
34203420
}
34213421

34223422
if (!buffer && this.transferEncoding === "base64") {
3423-
buffer = fromBase64Util(this[$content]);
3423+
buffer = Uint8Array.fromBase64(this[$content]);
34243424
}
34253425

34263426
if (!buffer) {

src/display/editor/drawers/signaturedraw.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { fromBase64Util, toBase64Util, warn } from "../../../shared/util.js";
1716
import { ContourDrawOutline } from "./contour.js";
1817
import { InkDrawOutline } from "./inkdraw.js";
1918
import { Outline } from "./outline.js";
19+
import { warn } from "../../../shared/util.js";
2020

2121
const BASE_HEADER_LENGTH = 8;
2222
const POINTS_PROPERTIES_NUMBER = 3;
@@ -749,12 +749,12 @@ class SignatureExtractor {
749749
const buf = await new Response(cs.readable).arrayBuffer();
750750
const bytes = new Uint8Array(buf);
751751

752-
return toBase64Util(bytes);
752+
return bytes.toBase64();
753753
}
754754

755755
static async decompressSignature(signatureData) {
756756
try {
757-
const bytes = fromBase64Util(signatureData);
757+
const bytes = Uint8Array.fromBase64(signatureData);
758758
const { readable, writable } = new DecompressionStream("deflate-raw");
759759
const writer = writable.getWriter();
760760
await writer.ready;

src/display/font_loader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
isNodeJS,
2020
shadow,
2121
string32,
22-
toBase64Util,
2322
unreachable,
2423
warn,
2524
} from "../shared/util.js";
@@ -408,7 +407,7 @@ class FontFaceObject {
408407
return null;
409408
}
410409
// Add the @font-face rule to the document.
411-
const url = `url(data:${this.mimetype};base64,${toBase64Util(this.data)});`;
410+
const url = `url(data:${this.mimetype};base64,${this.data.toBase64()});`;
412411
let rule;
413412
if (!this.cssFontInfo) {
414413
rule = `@font-face {font-family:"${this.loadedName}";src:${url}}`;

src/shared/util.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,31 +1235,6 @@ function MathClamp(v, min, max) {
12351235
return Math.min(Math.max(v, min), max);
12361236
}
12371237

1238-
// TODO: Remove this once `Uint8Array.prototype.toHex` is generally available.
1239-
function toHexUtil(arr) {
1240-
if (Uint8Array.prototype.toHex) {
1241-
return arr.toHex();
1242-
}
1243-
return Array.from(arr, num => hexNumbers[num]).join("");
1244-
}
1245-
1246-
// TODO: Remove this once `Uint8Array.prototype.toBase64` is generally
1247-
// available.
1248-
function toBase64Util(arr) {
1249-
if (Uint8Array.prototype.toBase64) {
1250-
return arr.toBase64();
1251-
}
1252-
return btoa(bytesToString(arr));
1253-
}
1254-
1255-
// TODO: Remove this once `Uint8Array.fromBase64` is generally available.
1256-
function fromBase64Util(str) {
1257-
if (Uint8Array.fromBase64) {
1258-
return Uint8Array.fromBase64(str);
1259-
}
1260-
return stringToBytes(atob(str));
1261-
}
1262-
12631238
// TODO: Remove this once the `javascript.options.experimental.math_sumprecise`
12641239
// preference is removed from Firefox.
12651240
if (typeof Math.sumPrecise !== "function") {
@@ -1325,7 +1300,6 @@ export {
13251300
FeatureTest,
13261301
FONT_IDENTITY_MATRIX,
13271302
FormatError,
1328-
fromBase64Util,
13291303
getModificationDate,
13301304
getUuid,
13311305
getVerbosityLevel,
@@ -1355,8 +1329,6 @@ export {
13551329
stringToPDFString,
13561330
stringToUTF8String,
13571331
TextRenderingMode,
1358-
toBase64Util,
1359-
toHexUtil,
13601332
UnknownErrorException,
13611333
unreachable,
13621334
updateUrlHash,

test/font/font_core_spec.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_fpgm_spec.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_os2_spec.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_post_spec.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/fontutils.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { bytesToString, stringToBytes } from "../../src/shared/util.js";
18-
19-
function decodeFontData(base64) {
20-
const str = atob(base64);
21-
return stringToBytes(str);
22-
}
23-
24-
function encodeFontData(data) {
25-
const str = bytesToString(data);
26-
return btoa(str);
27-
}
28-
2917
async function ttx(data) {
3018
const response = await fetch("/ttx", {
3119
method: "POST",
32-
body: encodeFontData(data),
20+
body: data.toBase64(),
3321
});
3422

3523
if (!response.ok) {
@@ -45,4 +33,4 @@ function verifyTtxOutput(output) {
4533
}
4634
}
4735

48-
export { decodeFontData, encodeFontData, ttx, verifyTtxOutput };
36+
export { ttx, verifyTtxOutput };

0 commit comments

Comments
 (0)