From df3b3592806734accfcc37e33a99624e40078388 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 Feb 2023 10:45:28 +0100 Subject: [PATCH 1/2] Remove "else after return" from the `getUrlProp`/`getDataProp` helper functions This helps readability of this code a little bit, in my opinion, and it's actually ever so slightly less code in the *built* `pdf.js` file. --- src/display/api.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index c7cb81007..fcccfc2cb 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -507,7 +507,8 @@ async function _fetchDocument(worker, source) { function getUrlProp(val) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { return null; // The 'url' is unused with `PDFDataRangeTransport`. - } else if (val instanceof URL) { + } + if (val instanceof URL) { return val.href; } try { @@ -539,20 +540,17 @@ function getDataProp(val) { val instanceof Buffer // eslint-disable-line no-undef ) { return new Uint8Array(val); - } else if ( - val instanceof Uint8Array && - val.byteLength === val.buffer.byteLength - ) { + } + if (val instanceof Uint8Array && val.byteLength === val.buffer.byteLength) { // Use the data as-is when it's already a Uint8Array that completely // "utilizes" its underlying ArrayBuffer, to prevent any possible // issues when transferring it to the worker-thread. return val; - } else if (typeof val === "string") { + } + if (typeof val === "string") { return stringToBytes(val); - } else if ( - (typeof val === "object" && !isNaN(val?.length)) || - isArrayBuffer(val) - ) { + } + if ((typeof val === "object" && !isNaN(val?.length)) || isArrayBuffer(val)) { return new Uint8Array(val); } throw new Error( From b6ba8cc84a0cef762aa5349dcb18cc0799f5d946 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 Feb 2023 11:30:40 +0100 Subject: [PATCH 2/2] [api-minor] Deprecate providing binary data as `Buffer` in Node.js environments The `Buffer`-object is Node.js specific functionality[1], thus (obviously) not found in browsers. Please note that the PDF.js library has never officially supported/documented that binary data can be passed as a `Buffer`, and that *internally* in the `src/core`-code we only work with standard `Uint8Array`s. This means that if, in Node.js environments, a `Buffer` is passed to the API we need to wrap it into a `Uint8Array`, which essentially means creating a copy of the data and thus increasing memory usage. --- [1] Refer to https://nodejs.org/api/buffer.html#buffer --- src/display/api.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/display/api.js b/src/display/api.js index fcccfc2cb..c4954f237 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -539,6 +539,9 @@ function getDataProp(val) { typeof Buffer !== "undefined" && // eslint-disable-line no-undef val instanceof Buffer // eslint-disable-line no-undef ) { + deprecated( + "Please provide binary data as `Uint8Array`, rather than `Buffer`." + ); return new Uint8Array(val); } if (val instanceof Uint8Array && val.byteLength === val.buffer.byteLength) {