From 0daab88a486bd62d1d9cf5dacd48e00c87cf4a9e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 10 Feb 2022 11:42:42 +0100 Subject: [PATCH 1/2] Update two `display_utils` unit-tests to use native functionality rather than the `createObjectURL` helper function Given that most of the code-base is already using native functionality, we can update these unit-tests similarily as well. - For the `blob:`-URL test, we simply use `URL.createObjectURL(...)` and `Blob` directly instead. - For the `data:`-URL test, we simply use `btoa` to do the Base64 encoding and then build the final URL-string. --- test/unit/display_utils_spec.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/unit/display_utils_spec.js b/test/unit/display_utils_spec.js index bcb196e59..55ebe6e7d 100644 --- a/test/unit/display_utils_spec.js +++ b/test/unit/display_utils_spec.js @@ -21,7 +21,7 @@ import { isValidFetchUrl, PDFDateString, } from "../../src/display/display_utils.js"; -import { createObjectURL } from "../../src/shared/util.js"; +import { bytesToString } from "../../src/shared/util.js"; import { isNodeJS } from "../../src/shared/is_node.js"; describe("display_utils", function () { @@ -320,7 +320,9 @@ describe("display_utils", function () { pending("Blob in not supported in Node.js."); } const typedArray = new Uint8Array([1, 2, 3, 4, 5]); - const blobUrl = createObjectURL(typedArray, "application/pdf"); + const blobUrl = URL.createObjectURL( + new Blob([typedArray], { type: "application/pdf" }) + ); // Sanity check to ensure that a "blob:" URL was returned. expect(blobUrl.startsWith("blob:")).toEqual(true); @@ -328,12 +330,9 @@ describe("display_utils", function () { }); it('gets fallback filename from query string appended to "data:" URL', function () { - const typedArray = new Uint8Array([1, 2, 3, 4, 5]); - const dataUrl = createObjectURL( - typedArray, - "application/pdf", - /* forceDataSchema = */ true - ); + const typedArray = new Uint8Array([1, 2, 3, 4, 5]), + str = bytesToString(typedArray); + const dataUrl = `data:application/pdf;base64,${btoa(str)}`; // Sanity check to ensure that a "data:" URL was returned. expect(dataUrl.startsWith("data:")).toEqual(true); From b87a24322268d5e3d57c89b2640fe0062d587d05 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 10 Feb 2022 11:42:50 +0100 Subject: [PATCH 2/2] [api-minor] Stop exposing the `createObjectURL` helper function in the API With recent changes, specifically PR 14515 *and* the previous patch, the `createObjectURL` helper function is now only used with the SVG back-end. All other call-sites, throughout the code-base, are now using `URL.createObjectURL(...)` directly and it no longer seems necessary to keep exposing the helper function in the API. Finally, the `createObjectURL` helper function is moved into the `src/display/svg.js` file to avoid unnecessarily duplicating this code on both the main- and worker-threads. --- src/display/svg.js | 31 ++++++++++++++++++++++++++++++- src/pdf.js | 2 -- src/shared/util.js | 23 ----------------------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/display/svg.js b/src/display/svg.js index c2b6f7c0d..52fe55189 100644 --- a/src/display/svg.js +++ b/src/display/svg.js @@ -15,7 +15,6 @@ /* globals __non_webpack_require__ */ import { - createObjectURL, FONT_IDENTITY_MATRIX, IDENTITY_MATRIX, ImageKind, @@ -50,6 +49,36 @@ if ( const LINE_CAP_STYLES = ["butt", "round", "square"]; const LINE_JOIN_STYLES = ["miter", "round", "bevel"]; + const createObjectURL = function ( + data, + contentType = "", + forceDataSchema = false + ) { + if ( + URL.createObjectURL && + typeof Blob !== "undefined" && + !forceDataSchema + ) { + return URL.createObjectURL(new Blob([data], { type: contentType })); + } + // Blob/createObjectURL is not available, falling back to data schema. + const digits = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + + let buffer = `data:${contentType};base64,`; + for (let i = 0, ii = data.length; i < ii; i += 3) { + const b1 = data[i] & 0xff; + const b2 = data[i + 1] & 0xff; + const b3 = data[i + 2] & 0xff; + const d1 = b1 >> 2, + d2 = ((b1 & 3) << 4) | (b2 >> 4); + const d3 = i + 1 < ii ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64; + const d4 = i + 2 < ii ? b3 & 0x3f : 64; + buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4]; + } + return buffer; + }; + const convertImgDataToPng = (function () { const PNG_HEADER = new Uint8Array([ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, diff --git a/src/pdf.js b/src/pdf.js index 8b5c35043..7c38367d1 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -23,7 +23,6 @@ import { AnnotationMode, CMapCompressionType, - createObjectURL, createPromiseCapability, createValidAbsoluteUrl, InvalidPDFException, @@ -109,7 +108,6 @@ export { AnnotationMode, build, CMapCompressionType, - createObjectURL, createPromiseCapability, createValidAbsoluteUrl, getDocument, diff --git a/src/shared/util.js b/src/shared/util.js index 92be937b3..802860133 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -1109,28 +1109,6 @@ function createPromiseCapability() { return capability; } -function createObjectURL(data, contentType = "", forceDataSchema = false) { - if (URL.createObjectURL && typeof Blob !== "undefined" && !forceDataSchema) { - return URL.createObjectURL(new Blob([data], { type: contentType })); - } - // Blob/createObjectURL is not available, falling back to data schema. - const digits = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - - let buffer = `data:${contentType};base64,`; - for (let i = 0, ii = data.length; i < ii; i += 3) { - const b1 = data[i] & 0xff; - const b2 = data[i + 1] & 0xff; - const b3 = data[i + 2] & 0xff; - const d1 = b1 >> 2, - d2 = ((b1 & 3) << 4) | (b2 >> 4); - const d3 = i + 1 < ii ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64; - const d4 = i + 2 < ii ? b3 & 0x3f : 64; - buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4]; - } - return buffer; -} - export { AbortException, AnnotationActionEventType, @@ -1149,7 +1127,6 @@ export { BaseException, bytesToString, CMapCompressionType, - createObjectURL, createPromiseCapability, createValidAbsoluteUrl, DocumentActionEventType,