Skip to content

Commit 1719629

Browse files
Snuffleupaguspull[bot]
authored andcommitted
Move the arrayBuffersToBytes helper function into the worker-thread
Given that this helper function is only used on the worker-thread, there's no reason to duplicate it in both of the *built* `pdf.js` and `pdf.worker.js` files.
1 parent de10f48 commit 1719629

File tree

5 files changed

+74
-48
lines changed

5 files changed

+74
-48
lines changed

src/core/chunked_stream.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
* limitations under the License.
1414
*/
1515

16-
import {
17-
arrayBuffersToBytes,
18-
assert,
19-
createPromiseCapability,
20-
} from "../shared/util.js";
21-
import { MissingDataException } from "./core_utils.js";
16+
import { arrayBuffersToBytes, MissingDataException } from "./core_utils.js";
17+
import { assert, createPromiseCapability } from "../shared/util.js";
2218
import { Stream } from "./stream.js";
2319

2420
class ChunkedStream extends Stream {

src/core/core_utils.js

+39
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,44 @@ class XRefParseException extends BaseException {
8080
}
8181
}
8282

83+
/**
84+
* Combines multiple ArrayBuffers into a single Uint8Array.
85+
* @param {Array<ArrayBuffer>} arr - An array of ArrayBuffers.
86+
* @returns {Uint8Array}
87+
*/
88+
function arrayBuffersToBytes(arr) {
89+
if (
90+
typeof PDFJSDev === "undefined" ||
91+
PDFJSDev.test("!PRODUCTION || TESTING")
92+
) {
93+
for (const item of arr) {
94+
assert(
95+
item instanceof ArrayBuffer,
96+
"arrayBuffersToBytes - expected an ArrayBuffer."
97+
);
98+
}
99+
}
100+
const length = arr.length;
101+
if (length === 0) {
102+
return new Uint8Array(0);
103+
}
104+
if (length === 1) {
105+
return new Uint8Array(arr[0]);
106+
}
107+
let dataLength = 0;
108+
for (let i = 0; i < length; i++) {
109+
dataLength += arr[i].byteLength;
110+
}
111+
const data = new Uint8Array(dataLength);
112+
let pos = 0;
113+
for (let i = 0; i < length; i++) {
114+
const item = new Uint8Array(arr[i]);
115+
data.set(item, pos);
116+
pos += item.byteLength;
117+
}
118+
return data;
119+
}
120+
83121
/**
84122
* Get the value of an inheritable property.
85123
*
@@ -579,6 +617,7 @@ function getRotationMatrix(rotation, width, height) {
579617
}
580618

581619
export {
620+
arrayBuffersToBytes,
582621
collectActions,
583622
encodeToXmlString,
584623
escapePDFName,

src/core/worker.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import {
1717
AbortException,
18-
arrayBuffersToBytes,
1918
assert,
2019
createPromiseCapability,
2120
getVerbosityLevel,
@@ -30,8 +29,12 @@ import {
3029
VerbosityLevel,
3130
warn,
3231
} from "../shared/util.js";
32+
import {
33+
arrayBuffersToBytes,
34+
getNewAnnotationsMap,
35+
XRefParseException,
36+
} from "./core_utils.js";
3337
import { Dict, Ref } from "./primitives.js";
34-
import { getNewAnnotationsMap, XRefParseException } from "./core_utils.js";
3538
import { LocalPdfManager, NetworkPdfManager } from "./pdf_manager.js";
3639
import { clearGlobalCaches } from "./cleanup_helper.js";
3740
import { incrementalUpdate } from "./writer.js";

src/shared/util.js

-39
Original file line numberDiff line numberDiff line change
@@ -597,44 +597,6 @@ function stringToBytes(str) {
597597
return bytes;
598598
}
599599

600-
/**
601-
* Combines multiple ArrayBuffers into a single Uint8Array.
602-
* @param {Array<ArrayBuffer>} arr - An array of ArrayBuffers.
603-
* @returns {Uint8Array}
604-
*/
605-
function arrayBuffersToBytes(arr) {
606-
if (
607-
typeof PDFJSDev === "undefined" ||
608-
PDFJSDev.test("!PRODUCTION || TESTING")
609-
) {
610-
for (const item of arr) {
611-
assert(
612-
item instanceof ArrayBuffer,
613-
"arrayBuffersToBytes - expected an ArrayBuffer."
614-
);
615-
}
616-
}
617-
const length = arr.length;
618-
if (length === 0) {
619-
return new Uint8Array(0);
620-
}
621-
if (length === 1) {
622-
return new Uint8Array(arr[0]);
623-
}
624-
let dataLength = 0;
625-
for (let i = 0; i < length; i++) {
626-
dataLength += arr[i].byteLength;
627-
}
628-
const data = new Uint8Array(dataLength);
629-
let pos = 0;
630-
for (let i = 0; i < length; i++) {
631-
const item = new Uint8Array(arr[i]);
632-
data.set(item, pos);
633-
pos += item.byteLength;
634-
}
635-
return data;
636-
}
637-
638600
function string32(value) {
639601
if (
640602
typeof PDFJSDev === "undefined" ||
@@ -1103,7 +1065,6 @@ export {
11031065
AnnotationReviewState,
11041066
AnnotationStateModelType,
11051067
AnnotationType,
1106-
arrayBuffersToBytes,
11071068
assert,
11081069
BaseException,
11091070
BASELINE_FACTOR,

test/unit/core_utils_spec.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { Dict, Ref } from "../../src/core/primitives.js";
1716
import {
17+
arrayBuffersToBytes,
1818
encodeToXmlString,
1919
escapePDFName,
2020
escapeString,
@@ -30,9 +30,36 @@ import {
3030
toRomanNumerals,
3131
validateCSSFont,
3232
} from "../../src/core/core_utils.js";
33+
import { Dict, Ref } from "../../src/core/primitives.js";
3334
import { XRefMock } from "./test_utils.js";
3435

3536
describe("core_utils", function () {
37+
describe("arrayBuffersToBytes", function () {
38+
it("handles zero ArrayBuffers", function () {
39+
const bytes = arrayBuffersToBytes([]);
40+
41+
expect(bytes).toEqual(new Uint8Array(0));
42+
});
43+
44+
it("handles one ArrayBuffer", function () {
45+
const buffer = new Uint8Array([1, 2, 3]).buffer;
46+
const bytes = arrayBuffersToBytes([buffer]);
47+
48+
expect(bytes).toEqual(new Uint8Array([1, 2, 3]));
49+
// Ensure that the fast-path works correctly.
50+
expect(bytes.buffer).toBe(buffer);
51+
});
52+
53+
it("handles multiple ArrayBuffers", function () {
54+
const buffer1 = new Uint8Array([1, 2, 3]).buffer,
55+
buffer2 = new Uint8Array(0).buffer,
56+
buffer3 = new Uint8Array([4, 5]).buffer;
57+
const bytes = arrayBuffersToBytes([buffer1, buffer2, buffer3]);
58+
59+
expect(bytes).toEqual(new Uint8Array([1, 2, 3, 4, 5]));
60+
});
61+
});
62+
3663
describe("getInheritableProperty", function () {
3764
it("handles non-dictionary arguments", function () {
3865
expect(getInheritableProperty({ dict: null, key: "foo" })).toEqual(

0 commit comments

Comments
 (0)