Skip to content

Commit 45294d3

Browse files
committed
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.
1 parent 95f62f3 commit 45294d3

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/display/node_stream.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ class PDFNodeStreamFsFullReader {
104104
const fs = process.getBuiltinModule("fs");
105105
fs.promises.lstat(this._url).then(
106106
stat => {
107+
const { size } = stat;
108+
if (size <= 2 * this._rangeChunkSize) {
109+
// The file size is smaller than the size of two chunks, so it doesn't
110+
// make any sense to abort the request and retry with a range request.
111+
this._isRangeSupported = false;
112+
}
107113
// Setting right content length.
108-
this._contentLength = stat.size;
114+
this._contentLength = size;
109115

110116
this._setReadableStream(fs.createReadStream(this._url));
111117
this._headersCapability.resolve();

test/unit/node_stream_spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,41 @@ describe("node_stream", function () {
117117
expect(isRangeSupported).toEqual(true);
118118
expect(fullReaderCancelled).toEqual(true);
119119
});
120+
121+
it("read filesystem pdf files (smaller than two range requests)", async function () {
122+
const smallPdf = new URL("./test/pdfs/empty.pdf", cwdURL).href;
123+
const smallLength = 4920;
124+
125+
const stream = new PDFNodeStream({
126+
url: smallPdf,
127+
rangeChunkSize: 65536,
128+
disableStream: true,
129+
disableRange: false,
130+
});
131+
132+
const fullReader = stream.getFullReader();
133+
134+
let isStreamingSupported, isRangeSupported;
135+
const promise = fullReader.headersReady.then(() => {
136+
isStreamingSupported = fullReader.isStreamingSupported;
137+
isRangeSupported = fullReader.isRangeSupported;
138+
});
139+
140+
let len = 0;
141+
const read = function () {
142+
return fullReader.read().then(function (result) {
143+
if (result.done) {
144+
return undefined;
145+
}
146+
len += result.value.byteLength;
147+
return read();
148+
});
149+
};
150+
151+
await Promise.all([read(), promise]);
152+
153+
expect(isStreamingSupported).toEqual(false);
154+
expect(isRangeSupported).toEqual(false);
155+
expect(len).toEqual(smallLength);
156+
});
120157
});

0 commit comments

Comments
 (0)