Skip to content

Commit b7d06c7

Browse files
committed
Ensure that getDocument handles Node.js Buffers more gracefully (issue 13075)
While the JSDocs have never advertised `getDocument` as supporting Node.js `Buffer`s, that apparently doesn't stop users from passing such data structures to `getDocument`. In theory the existing `instanceof Uint8Array` check ought to have caught Node.js `Buffer`s, however for reasons that I don't even pretend to understand that check actually passes. Hence this patch which, *only* in Node.js environments, will special-case `Buffer`s to hopefully provide a slightly better out-of-the-box behaviour in Node.js environments[1]. --- [1] Although I'm not sure that we necessarily want to advertise this in the JSDocs, given the specialized use-case.
1 parent 0987d5b commit b7d06c7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/display/api.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,22 @@ function getDocument(src) {
243243
} else if (key === "worker") {
244244
worker = source[key];
245245
continue;
246-
} else if (key === "data" && !(source[key] instanceof Uint8Array)) {
246+
} else if (key === "data") {
247247
// Converting string or array-like data to Uint8Array.
248248
const pdfBytes = source[key];
249+
if (
250+
typeof PDFJSDev !== "undefined" &&
251+
PDFJSDev.test("GENERIC") &&
252+
isNodeJS &&
253+
pdfBytes instanceof Buffer // eslint-disable-line no-undef
254+
) {
255+
params[key] = new Uint8Array(pdfBytes);
256+
continue;
257+
}
258+
if (pdfBytes instanceof Uint8Array) {
259+
params[key] = pdfBytes;
260+
continue;
261+
}
249262
if (typeof pdfBytes === "string") {
250263
params[key] = stringToBytes(pdfBytes);
251264
} else if (

0 commit comments

Comments
 (0)