Remove the isArrayBuffer helper function

This old helper function can now be replaced with `ArrayBuffer.isView()` and/or `instanceof ArrayBuffer` checks, as needed depending on the situation.
This commit is contained in:
Jonas Jenwald 2024-01-19 14:10:52 +01:00
parent a0e2b62245
commit b37536c38c
4 changed files with 10 additions and 35 deletions

View File

@ -24,7 +24,6 @@ import {
getVerbosityLevel, getVerbosityLevel,
info, info,
InvalidPDFException, InvalidPDFException,
isArrayBuffer,
isNodeJS, isNodeJS,
MAX_IMAGE_SIZE_TO_CACHE, MAX_IMAGE_SIZE_TO_CACHE,
MissingPDFException, MissingPDFException,
@ -103,10 +102,6 @@ const DefaultStandardFontDataFactory =
* } TypedArray * } TypedArray
*/ */
/**
* @typedef { TypedArray | ArrayBuffer | Array<number> | string } BinaryData
*/
/** /**
* @typedef {Object} RefProxy * @typedef {Object} RefProxy
* @property {number} num * @property {number} num
@ -118,7 +113,8 @@ const DefaultStandardFontDataFactory =
* *
* @typedef {Object} DocumentInitParameters * @typedef {Object} DocumentInitParameters
* @property {string | URL} [url] - The URL of the PDF. * @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 * Use TypedArrays (Uint8Array) to improve the memory usage. If PDF data is
* BASE64-encoded, use `atob()` to convert it to a binary string first. * BASE64-encoded, use `atob()` to convert it to a binary string first.
* *
@ -235,7 +231,7 @@ function getDocument(src) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (typeof src === "string" || src instanceof URL) { if (typeof src === "string" || src instanceof URL) {
src = { url: src }; src = { url: src };
} else if (isArrayBuffer(src)) { } else if (src instanceof ArrayBuffer || ArrayBuffer.isView(src)) {
src = { data: src }; src = { data: src };
} }
} }
@ -552,7 +548,11 @@ function getDataProp(val) {
if (typeof val === "string") { if (typeof val === "string") {
return stringToBytes(val); 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); return new Uint8Array(val);
} }
throw new Error( throw new Error(

View File

@ -17,8 +17,6 @@
* Hashes roughly 100 KB per millisecond on i7 3.4 GHz. * Hashes roughly 100 KB per millisecond on i7 3.4 GHz.
*/ */
import { isArrayBuffer } from "./util.js";
const SEED = 0xc3d2e1f0; const SEED = 0xc3d2e1f0;
// Workaround for missing math precision in JS. // Workaround for missing math precision in JS.
const MASK_HIGH = 0xffff0000; const MASK_HIGH = 0xffff0000;
@ -44,14 +42,11 @@ class MurmurHash3_64 {
data[length++] = code & 0xff; data[length++] = code & 0xff;
} }
} }
} else if (isArrayBuffer(input)) { } else if (ArrayBuffer.isView(input)) {
data = input.slice(); data = input.slice();
length = data.byteLength; length = data.byteLength;
} else { } else {
throw new Error( throw new Error("Invalid data format, must be a string or TypedArray.");
"Wrong data format in MurmurHash3_64_update. " +
"Input must be a string or array."
);
} }
const blockCounts = length >> 2; const blockCounts = length >> 2;

View File

@ -963,10 +963,6 @@ function utf8StringToString(str) {
return unescape(encodeURIComponent(str)); return unescape(encodeURIComponent(str));
} }
function isArrayBuffer(v) {
return typeof v === "object" && v?.byteLength !== undefined;
}
function isArrayEqual(arr1, arr2) { function isArrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) { if (arr1.length !== arr2.length) {
return false; return false;
@ -1101,7 +1097,6 @@ export {
ImageKind, ImageKind,
info, info,
InvalidPDFException, InvalidPDFException,
isArrayBuffer,
isArrayEqual, isArrayEqual,
isNodeJS, isNodeJS,
LINE_DESCENT_FACTOR, LINE_DESCENT_FACTOR,

View File

@ -17,7 +17,6 @@ import {
bytesToString, bytesToString,
createValidAbsoluteUrl, createValidAbsoluteUrl,
getModificationDate, getModificationDate,
isArrayBuffer,
PromiseCapability, PromiseCapability,
string32, string32,
stringToBytes, stringToBytes,
@ -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 () { describe("string32", function () {
it("converts unsigned 32-bit integers to strings", function () { it("converts unsigned 32-bit integers to strings", function () {
expect(string32(0x74727565)).toEqual("true"); expect(string32(0x74727565)).toEqual("true");