File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments