In Node.js, don't abort the full request for local PDF files smaller than two range requests

This follows the behaviour used with both the Fetch API and `XMLHttpRequest`, compare with the `validateRangeRequestCapabilities` helper function.
This commit is contained in:
Jonas Jenwald 2026-01-25 12:34:35 +01:00
parent 95f62f3b33
commit 45294d31cb
2 changed files with 44 additions and 1 deletions

View File

@ -104,8 +104,14 @@ class PDFNodeStreamFsFullReader {
const fs = process.getBuiltinModule("fs");
fs.promises.lstat(this._url).then(
stat => {
const { size } = stat;
if (size <= 2 * this._rangeChunkSize) {
// The file size is smaller than the size of two chunks, so it doesn't
// make any sense to abort the request and retry with a range request.
this._isRangeSupported = false;
}
// Setting right content length.
this._contentLength = stat.size;
this._contentLength = size;
this._setReadableStream(fs.createReadStream(this._url));
this._headersCapability.resolve();

View File

@ -117,4 +117,41 @@ describe("node_stream", function () {
expect(isRangeSupported).toEqual(true);
expect(fullReaderCancelled).toEqual(true);
});
it("read filesystem pdf files (smaller than two range requests)", async function () {
const smallPdf = new URL("./test/pdfs/empty.pdf", cwdURL).href;
const smallLength = 4920;
const stream = new PDFNodeStream({
url: smallPdf,
rangeChunkSize: 65536,
disableStream: true,
disableRange: false,
});
const fullReader = stream.getFullReader();
let isStreamingSupported, isRangeSupported;
const promise = fullReader.headersReady.then(() => {
isStreamingSupported = fullReader.isStreamingSupported;
isRangeSupported = fullReader.isRangeSupported;
});
let len = 0;
const read = function () {
return fullReader.read().then(function (result) {
if (result.done) {
return undefined;
}
len += result.value.byteLength;
return read();
});
};
await Promise.all([read(), promise]);
expect(isStreamingSupported).toEqual(false);
expect(isRangeSupported).toEqual(false);
expect(len).toEqual(smallLength);
});
});