Re-use the getArrayBuffer helper from src/display/fetch_stream.js with PDFNodeStreamReader and PDFNodeStreamRangeReader

Given that the Node.js code uses standard `ReadableStream`s now, see PR 20594, it can use the same `getArrayBuffer` as the Fetch API implementation.

Also, change the `getArrayBuffer` fallback case to an Error (rather than a warning) since that should never actually happen.
This commit is contained in:
Jonas Jenwald 2026-02-08 11:39:18 +01:00
parent 916b58a027
commit 0306e6c7ed
2 changed files with 5 additions and 16 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { AbortException, assert, warn } from "../shared/util.js";
import { AbortException, assert } from "../shared/util.js";
import {
BasePDFStream,
BasePDFStreamRangeReader,
@ -58,8 +58,7 @@ function getArrayBuffer(val) {
if (val instanceof ArrayBuffer) {
return val;
}
warn(`getArrayBuffer - unexpected data format: ${val}`);
return new Uint8Array(val).buffer;
throw new Error(`getArrayBuffer - unexpected data: ${val}`);
}
class PDFFetchStream extends BasePDFStream {
@ -194,4 +193,4 @@ class PDFFetchStreamRangeReader extends BasePDFStreamRangeReader {
}
}
export { PDFFetchStream };
export { getArrayBuffer, PDFFetchStream };

View File

@ -14,13 +14,14 @@
*/
/* globals process */
import { AbortException, assert, warn } from "../shared/util.js";
import { AbortException, assert } from "../shared/util.js";
import {
BasePDFStream,
BasePDFStreamRangeReader,
BasePDFStreamReader,
} from "../shared/base_pdf_stream.js";
import { createResponseError } from "./network_utils.js";
import { getArrayBuffer } from "./fetch_stream.js";
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
@ -44,17 +45,6 @@ function getReadableStream(readStream) {
return polyfill.makeDefaultReadableStreamFromNodeReadable(readStream);
}
function getArrayBuffer(val) {
if (val instanceof Uint8Array) {
return val.buffer;
}
if (val instanceof ArrayBuffer) {
return val;
}
warn(`getArrayBuffer - unexpected data format: ${val}`);
return new Uint8Array(val).buffer;
}
class PDFNodeStream extends BasePDFStream {
constructor(source) {
super(source, PDFNodeStreamReader, PDFNodeStreamRangeReader);