Merge pull request #14551 from Snuffleupagus/mv-createObjectURL

[api-minor] Stop exposing the `createObjectURL` helper function in the API
This commit is contained in:
Tim van der Meij 2022-02-11 19:40:25 +01:00 committed by GitHub
commit e9fd67a3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 34 deletions

View File

@ -15,7 +15,6 @@
/* globals __non_webpack_require__ */ /* globals __non_webpack_require__ */
import { import {
createObjectURL,
FONT_IDENTITY_MATRIX, FONT_IDENTITY_MATRIX,
IDENTITY_MATRIX, IDENTITY_MATRIX,
ImageKind, ImageKind,
@ -50,6 +49,36 @@ if (
const LINE_CAP_STYLES = ["butt", "round", "square"]; const LINE_CAP_STYLES = ["butt", "round", "square"];
const LINE_JOIN_STYLES = ["miter", "round", "bevel"]; 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 convertImgDataToPng = (function () {
const PNG_HEADER = new Uint8Array([ const PNG_HEADER = new Uint8Array([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,

View File

@ -23,7 +23,6 @@
import { import {
AnnotationMode, AnnotationMode,
CMapCompressionType, CMapCompressionType,
createObjectURL,
createPromiseCapability, createPromiseCapability,
createValidAbsoluteUrl, createValidAbsoluteUrl,
InvalidPDFException, InvalidPDFException,
@ -109,7 +108,6 @@ export {
AnnotationMode, AnnotationMode,
build, build,
CMapCompressionType, CMapCompressionType,
createObjectURL,
createPromiseCapability, createPromiseCapability,
createValidAbsoluteUrl, createValidAbsoluteUrl,
getDocument, getDocument,

View File

@ -1109,28 +1109,6 @@ function createPromiseCapability() {
return capability; 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 { export {
AbortException, AbortException,
AnnotationActionEventType, AnnotationActionEventType,
@ -1149,7 +1127,6 @@ export {
BaseException, BaseException,
bytesToString, bytesToString,
CMapCompressionType, CMapCompressionType,
createObjectURL,
createPromiseCapability, createPromiseCapability,
createValidAbsoluteUrl, createValidAbsoluteUrl,
DocumentActionEventType, DocumentActionEventType,

View File

@ -21,7 +21,7 @@ import {
isValidFetchUrl, isValidFetchUrl,
PDFDateString, PDFDateString,
} from "../../src/display/display_utils.js"; } 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"; import { isNodeJS } from "../../src/shared/is_node.js";
describe("display_utils", function () { describe("display_utils", function () {
@ -320,7 +320,9 @@ describe("display_utils", function () {
pending("Blob in not supported in Node.js."); pending("Blob in not supported in Node.js.");
} }
const typedArray = new Uint8Array([1, 2, 3, 4, 5]); 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. // Sanity check to ensure that a "blob:" URL was returned.
expect(blobUrl.startsWith("blob:")).toEqual(true); 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 () { it('gets fallback filename from query string appended to "data:" URL', function () {
const typedArray = new Uint8Array([1, 2, 3, 4, 5]); const typedArray = new Uint8Array([1, 2, 3, 4, 5]),
const dataUrl = createObjectURL( str = bytesToString(typedArray);
typedArray, const dataUrl = `data:application/pdf;base64,${btoa(str)}`;
"application/pdf",
/* forceDataSchema = */ true
);
// Sanity check to ensure that a "data:" URL was returned. // Sanity check to ensure that a "data:" URL was returned.
expect(dataUrl.startsWith("data:")).toEqual(true); expect(dataUrl.startsWith("data:")).toEqual(true);