Skip to content

Commit

Permalink
Merge pull request #17537 from Snuffleupagus/rm-isArrayBuffer
Browse files Browse the repository at this point in the history
Remove the `isArrayBuffer` helper function
  • Loading branch information
Snuffleupagus authored Jan 19, 2024
2 parents f6c4b29 + b37536c commit f8e3c79
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 35 deletions.
16 changes: 8 additions & 8 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
getVerbosityLevel,
info,
InvalidPDFException,
isArrayBuffer,
isNodeJS,
MAX_IMAGE_SIZE_TO_CACHE,
MissingPDFException,
Expand Down Expand Up @@ -103,10 +102,6 @@ const DefaultStandardFontDataFactory =
* } TypedArray
*/

/**
* @typedef { TypedArray | ArrayBuffer | Array<number> | string } BinaryData
*/

/**
* @typedef {Object} RefProxy
* @property {number} num
Expand All @@ -118,7 +113,8 @@ const DefaultStandardFontDataFactory =
*
* @typedef {Object} DocumentInitParameters
* @property {string | URL} [url] - The URL of the PDF.
* @property {BinaryData} [data] - Binary PDF data.
* @property {TypedArray | ArrayBuffer | Array<number> | string} [data] -
* Binary PDF data.
* Use TypedArrays (Uint8Array) to improve the memory usage. If PDF data is
* BASE64-encoded, use `atob()` to convert it to a binary string first.
*
Expand Down Expand Up @@ -235,7 +231,7 @@ function getDocument(src) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (typeof src === "string" || src instanceof URL) {
src = { url: src };
} else if (isArrayBuffer(src)) {
} else if (src instanceof ArrayBuffer || ArrayBuffer.isView(src)) {
src = { data: src };
}
}
Expand Down Expand Up @@ -552,7 +548,11 @@ function getDataProp(val) {
if (typeof val === "string") {
return stringToBytes(val);
}
if ((typeof val === "object" && !isNaN(val?.length)) || isArrayBuffer(val)) {
if (
val instanceof ArrayBuffer ||
ArrayBuffer.isView(val) ||
(typeof val === "object" && !isNaN(val?.length))
) {
return new Uint8Array(val);
}
throw new Error(
Expand Down
9 changes: 2 additions & 7 deletions src/shared/murmurhash3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* Hashes roughly 100 KB per millisecond on i7 3.4 GHz.
*/

import { isArrayBuffer } from "./util.js";

const SEED = 0xc3d2e1f0;
// Workaround for missing math precision in JS.
const MASK_HIGH = 0xffff0000;
Expand All @@ -44,14 +42,11 @@ class MurmurHash3_64 {
data[length++] = code & 0xff;
}
}
} else if (isArrayBuffer(input)) {
} else if (ArrayBuffer.isView(input)) {
data = input.slice();
length = data.byteLength;
} else {
throw new Error(
"Wrong data format in MurmurHash3_64_update. " +
"Input must be a string or array."
);
throw new Error("Invalid data format, must be a string or TypedArray.");
}

const blockCounts = length >> 2;
Expand Down
5 changes: 0 additions & 5 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,6 @@ function utf8StringToString(str) {
return unescape(encodeURIComponent(str));
}

function isArrayBuffer(v) {
return typeof v === "object" && v?.byteLength !== undefined;
}

function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
Expand Down Expand Up @@ -1101,7 +1097,6 @@ export {
ImageKind,
info,
InvalidPDFException,
isArrayBuffer,
isArrayEqual,
isNodeJS,
LINE_DESCENT_FACTOR,
Expand Down
15 changes: 0 additions & 15 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
bytesToString,
createValidAbsoluteUrl,
getModificationDate,
isArrayBuffer,
PromiseCapability,
string32,
stringToBytes,
Expand Down Expand Up @@ -53,20 +52,6 @@ describe("util", function () {
});
});

describe("isArrayBuffer", function () {
it("handles array buffer values", function () {
expect(isArrayBuffer(new ArrayBuffer(0))).toEqual(true);
expect(isArrayBuffer(new Uint8Array(0))).toEqual(true);
});

it("handles non-array buffer values", function () {
expect(isArrayBuffer("true")).toEqual(false);
expect(isArrayBuffer(1)).toEqual(false);
expect(isArrayBuffer(null)).toEqual(false);
expect(isArrayBuffer(undefined)).toEqual(false);
});
});

describe("string32", function () {
it("converts unsigned 32-bit integers to strings", function () {
expect(string32(0x74727565)).toEqual("true");
Expand Down

0 comments on commit f8e3c79

Please sign in to comment.