@@ -5188,6 +5188,11 @@ have written that much by now. So, here’s to squashing bugs.`);
51885188 } ) ;
51895189
51905190 describe ( "PDFDataRangeTransport" , function ( ) {
5191+ async function streamDelay ( ) {
5192+ return new Promise ( resolve => {
5193+ setTimeout ( resolve , 250 ) ;
5194+ } ) ;
5195+ }
51915196 let dataPromise ;
51925197
51935198 beforeAll ( function ( ) {
@@ -5200,28 +5205,37 @@ have written that much by now. So, here’s to squashing bugs.`);
52005205 dataPromise = null ;
52015206 } ) ;
52025207
5203- it ( "should fetch document info and page using ranges" , async function ( ) {
5204- const initialDataLength = 4000 ;
5208+ it ( "should fetch document info and page using only ranges" , async function ( ) {
5209+ const initialDataLength = 80000 ; // Larger than `rangeChunkSize`, since otherwise it's pretty pointless.
52055210 const subArrays = [ ] ;
52065211 let initialProgress = null ;
52075212 let fetches = 0 ;
52085213
52095214 const data = await dataPromise ;
5215+ const dataLength = data . length ;
52105216 const initialData = new Uint8Array ( data . subarray ( 0 , initialDataLength ) ) ;
52115217 subArrays . push ( initialData ) ;
52125218
5213- const transport = new PDFDataRangeTransport ( data . length , initialData ) ;
5214- transport . requestDataRange = function ( begin , end ) {
5219+ const transport = new PDFDataRangeTransport (
5220+ dataLength ,
5221+ initialData ,
5222+ /* progressiveDone = */ undefined ,
5223+ /* contentDispositionFilename = */ "aaa.pdf"
5224+ ) ;
5225+ transport . requestDataRange = ( begin , end ) => {
52155226 fetches ++ ;
5216- waitSome ( function ( ) {
5227+ waitSome ( ( ) => {
52175228 const chunk = new Uint8Array ( data . subarray ( begin , end ) ) ;
52185229 subArrays . push ( chunk ) ;
52195230
52205231 transport . onDataRange ( begin , chunk ) ;
52215232 } ) ;
52225233 } ;
52235234
5224- const loadingTask = getDocument ( { range : transport } ) ;
5235+ const loadingTask = getDocument ( {
5236+ range : transport ,
5237+ rangeChunkSize : 65536 ,
5238+ } ) ;
52255239 loadingTask . onProgress = evt => {
52265240 initialProgress = evt ;
52275241 loadingTask . onProgress = null ;
@@ -5232,12 +5246,18 @@ have written that much by now. So, here’s to squashing bugs.`);
52325246
52335247 const pdfPage = await pdfDocument . getPage ( 10 ) ;
52345248 expect ( pdfPage . rotate ) . toEqual ( 0 ) ;
5235- expect ( fetches ) . toBeGreaterThan ( 2 ) ;
5249+
5250+ const { contentDispositionFilename, contentLength } =
5251+ await pdfDocument . getMetadata ( ) ;
5252+ expect ( contentDispositionFilename ) . toEqual ( "aaa.pdf" ) ;
5253+ expect ( contentLength ) . toEqual ( dataLength ) ;
5254+
5255+ expect ( fetches ) . toBeGreaterThan ( 4 ) ;
52365256
52375257 expect ( initialProgress ) . toEqual ( {
52385258 loaded : initialDataLength ,
5239- total : data . length ,
5240- percent : 0 ,
5259+ total : dataLength ,
5260+ percent : 8 ,
52415261 } ) ;
52425262
52435263 // Check that the TypedArrays were transferred.
@@ -5248,35 +5268,133 @@ have written that much by now. So, here’s to squashing bugs.`);
52485268 await loadingTask . destroy ( ) ;
52495269 } ) ;
52505270
5251- it ( "should fetch document info and page using range and streaming" , async function ( ) {
5252- const initialDataLength = 4000 ;
5271+ it ( "should fetch document info and page using only streaming" , async function ( ) {
5272+ const initialDataLength = 80000 ; // Larger than `rangeChunkSize`, since otherwise it's pretty pointless.
52535273 const subArrays = [ ] ;
52545274 let initialProgress = null ;
52555275 let fetches = 0 ;
52565276
52575277 const data = await dataPromise ;
5278+ const dataLength = data . length ;
52585279 const initialData = new Uint8Array ( data . subarray ( 0 , initialDataLength ) ) ;
52595280 subArrays . push ( initialData ) ;
52605281
5261- const transport = new PDFDataRangeTransport ( data . length , initialData ) ;
5262- transport . requestDataRange = function ( begin , end ) {
5263- fetches ++ ;
5264- if ( fetches === 1 ) {
5265- const chunk = new Uint8Array ( data . subarray ( initialDataLength ) ) ;
5282+ const transport = new PDFDataRangeTransport (
5283+ dataLength ,
5284+ initialData ,
5285+ /* progressiveDone = */ undefined ,
5286+ /* contentDispositionFilename = */ "BBB.PDF"
5287+ ) ;
5288+ transport . requestDataRange = ( begin , end ) => {
5289+ fetches ++ ; // There should be no range requests, since `disableRange` is used.
5290+ } ;
5291+ async function streamAllData ( ) {
5292+ const streamChunkSize = 131072 ;
5293+ let pos = initialDataLength ;
5294+
5295+ while ( pos < dataLength ) {
5296+ const begin = pos ,
5297+ end = Math . min ( pos + streamChunkSize , dataLength ) ;
5298+ pos = end ;
5299+
5300+ const chunk = new Uint8Array ( data . subarray ( begin , end ) ) ;
52665301 subArrays . push ( chunk ) ;
52675302
5268- // Send rest of the data on first range request.
52695303 transport . onDataProgressiveRead ( chunk ) ;
5304+ await streamDelay ( ) ;
52705305 }
5271- waitSome ( function ( ) {
5306+ transport . onDataProgressiveDone ( ) ;
5307+ }
5308+ streamAllData ( ) ;
5309+
5310+ const loadingTask = getDocument ( {
5311+ range : transport ,
5312+ rangeChunkSize : 65536 ,
5313+ disableRange : true ,
5314+ } ) ;
5315+ loadingTask . onProgress = evt => {
5316+ initialProgress = evt ;
5317+ loadingTask . onProgress = null ;
5318+ } ;
5319+
5320+ const pdfDocument = await loadingTask . promise ;
5321+ expect ( pdfDocument . numPages ) . toEqual ( 14 ) ;
5322+
5323+ const pdfPage = await pdfDocument . getPage ( 10 ) ;
5324+ expect ( pdfPage . rotate ) . toEqual ( 0 ) ;
5325+
5326+ const { contentDispositionFilename, contentLength } =
5327+ await pdfDocument . getMetadata ( ) ;
5328+ expect ( contentDispositionFilename ) . toEqual ( "BBB.PDF" ) ;
5329+ expect ( contentLength ) . toEqual ( dataLength ) ;
5330+
5331+ expect ( fetches ) . toEqual ( 0 ) ;
5332+
5333+ expect ( initialProgress . loaded ) . toBeGreaterThan ( initialDataLength ) ;
5334+ expect ( initialProgress . total ) . toEqual ( dataLength ) ;
5335+ expect ( initialProgress . percent ) . toBeGreaterThan ( 8 ) ;
5336+
5337+ // Check that the TypedArrays were transferred.
5338+ for ( const array of subArrays ) {
5339+ expect ( array . length ) . toEqual ( 0 ) ;
5340+ }
5341+
5342+ await loadingTask . destroy ( ) ;
5343+ } ) ;
5344+
5345+ it ( "should fetch document info and page using ranges and streaming" , async function ( ) {
5346+ const initialDataLength = 80000 ; // Larger than `rangeChunkSize`, since otherwise it's pretty pointless.
5347+ const subArrays = [ ] ;
5348+ let initialProgress = null ;
5349+ let fetches = 0 ;
5350+
5351+ const data = await dataPromise ;
5352+ const dataLength = data . length ;
5353+ const initialData = new Uint8Array ( data . subarray ( 0 , initialDataLength ) ) ;
5354+ subArrays . push ( initialData ) ;
5355+
5356+ const transport = new PDFDataRangeTransport (
5357+ dataLength ,
5358+ initialData ,
5359+ /* progressiveDone = */ undefined ,
5360+ /* contentDispositionFilename = */ ""
5361+ ) ;
5362+ transport . requestDataRange = ( begin , end ) => {
5363+ fetches ++ ;
5364+ waitSome ( ( ) => {
52725365 const chunk = new Uint8Array ( data . subarray ( begin , end ) ) ;
52735366 subArrays . push ( chunk ) ;
52745367
52755368 transport . onDataRange ( begin , chunk ) ;
52765369 } ) ;
52775370 } ;
5371+ async function streamPartialData ( ) {
5372+ const MAX_CHUNKS = 2 ;
5373+ let numChunks = 0 ;
5374+ const streamChunkSize = 131072 ;
5375+ let pos = initialDataLength ;
5376+
5377+ while ( pos < dataLength ) {
5378+ const begin = pos ,
5379+ end = Math . min ( pos + streamChunkSize , dataLength ) ;
5380+ pos = end ;
5381+
5382+ const chunk = new Uint8Array ( data . subarray ( begin , end ) ) ;
5383+ subArrays . push ( chunk ) ;
52785384
5279- const loadingTask = getDocument ( { range : transport } ) ;
5385+ transport . onDataProgressiveRead ( chunk ) ;
5386+ if ( ++ numChunks >= MAX_CHUNKS ) {
5387+ break ;
5388+ }
5389+ await streamDelay ( ) ;
5390+ }
5391+ }
5392+ streamPartialData ( ) ;
5393+
5394+ const loadingTask = getDocument ( {
5395+ range : transport ,
5396+ rangeChunkSize : 65536 ,
5397+ } ) ;
52805398 loadingTask . onProgress = evt => {
52815399 initialProgress = evt ;
52825400 loadingTask . onProgress = null ;
@@ -5287,17 +5405,17 @@ have written that much by now. So, here’s to squashing bugs.`);
52875405
52885406 const pdfPage = await pdfDocument . getPage ( 10 ) ;
52895407 expect ( pdfPage . rotate ) . toEqual ( 0 ) ;
5290- expect ( fetches ) . toEqual ( 1 ) ;
52915408
5292- expect ( initialProgress ) . toEqual ( {
5293- loaded : initialDataLength ,
5294- total : data . length ,
5295- percent : 0 ,
5296- } ) ;
5409+ const { contentDispositionFilename, contentLength } =
5410+ await pdfDocument . getMetadata ( ) ;
5411+ expect ( contentDispositionFilename ) . toEqual ( null ) ;
5412+ expect ( contentLength ) . toEqual ( dataLength ) ;
52975413
5298- await new Promise ( resolve => {
5299- waitSome ( resolve ) ;
5300- } ) ;
5414+ expect ( fetches ) . toBeGreaterThan ( 2 ) ;
5415+
5416+ expect ( initialProgress . loaded ) . toBeGreaterThan ( initialDataLength ) ;
5417+ expect ( initialProgress . total ) . toEqual ( dataLength ) ;
5418+ expect ( initialProgress . percent ) . toBeGreaterThan ( 8 ) ;
53015419
53025420 // Check that the TypedArrays were transferred.
53035421 for ( const array of subArrays ) {
@@ -5307,57 +5425,61 @@ have written that much by now. So, here’s to squashing bugs.`);
53075425 await loadingTask . destroy ( ) ;
53085426 } ) ;
53095427
5310- it (
5311- "should fetch document info and page, without range, " +
5312- "using complete initialData" ,
5313- async function ( ) {
5314- const subArrays = [ ] ;
5315- let initialProgress = null ;
5316- let fetches = 0 ;
5317-
5318- const data = await dataPromise ;
5319- const initialData = new Uint8Array ( data ) ;
5320- subArrays . push ( initialData ) ;
5321-
5322- const transport = new PDFDataRangeTransport (
5323- data . length ,
5324- initialData ,
5325- /* progressiveDone = */ true
5326- ) ;
5327- transport . requestDataRange = function ( begin , end ) {
5328- fetches ++ ;
5329- } ;
5428+ it ( "should fetch document info and page, without ranges, using complete initialData" , async function ( ) {
5429+ const subArrays = [ ] ;
5430+ let initialProgress = null ;
5431+ let fetches = 0 ;
53305432
5331- const loadingTask = getDocument ( {
5332- disableRange : true ,
5333- range : transport ,
5334- } ) ;
5335- loadingTask . onProgress = evt => {
5336- initialProgress = evt ;
5337- loadingTask . onProgress = null ;
5338- } ;
5433+ const data = await dataPromise ;
5434+ const dataLength = data . length ;
5435+ const initialData = new Uint8Array ( data ) ;
5436+ subArrays . push ( initialData ) ;
5437+
5438+ const transport = new PDFDataRangeTransport (
5439+ dataLength ,
5440+ initialData ,
5441+ /* progressiveDone = */ true ,
5442+ /* contentDispositionFilename = */ "pdf.txt"
5443+ ) ;
5444+ transport . requestDataRange = ( begin , end ) => {
5445+ fetches ++ ; // There should be no range requests, since `initialData` is complete.
5446+ } ;
53395447
5340- const pdfDocument = await loadingTask . promise ;
5341- expect ( pdfDocument . numPages ) . toEqual ( 14 ) ;
5448+ const loadingTask = getDocument ( {
5449+ range : transport ,
5450+ rangeChunkSize : 65536 ,
5451+ } ) ;
5452+ loadingTask . onProgress = evt => {
5453+ initialProgress = evt ;
5454+ loadingTask . onProgress = null ;
5455+ } ;
53425456
5343- const pdfPage = await pdfDocument . getPage ( 10 ) ;
5344- expect ( pdfPage . rotate ) . toEqual ( 0 ) ;
5345- expect ( fetches ) . toEqual ( 0 ) ;
5457+ const pdfDocument = await loadingTask . promise ;
5458+ expect ( pdfDocument . numPages ) . toEqual ( 14 ) ;
53465459
5347- expect ( initialProgress ) . toEqual ( {
5348- loaded : data . length ,
5349- total : data . length ,
5350- percent : 100 ,
5351- } ) ;
5460+ const pdfPage = await pdfDocument . getPage ( 10 ) ;
5461+ expect ( pdfPage . rotate ) . toEqual ( 0 ) ;
53525462
5353- // Check that the TypedArrays were transferred.
5354- for ( const array of subArrays ) {
5355- expect ( array . length ) . toEqual ( 0 ) ;
5356- }
5463+ const { contentDispositionFilename , contentLength } =
5464+ await pdfDocument . getMetadata ( ) ;
5465+ expect ( contentDispositionFilename ) . toEqual ( null ) ;
5466+ expect ( contentLength ) . toEqual ( dataLength ) ;
53575467
5358- await loadingTask . destroy ( ) ;
5468+ expect ( fetches ) . toEqual ( 0 ) ;
5469+
5470+ expect ( initialProgress ) . toEqual ( {
5471+ loaded : dataLength ,
5472+ total : dataLength ,
5473+ percent : 100 ,
5474+ } ) ;
5475+
5476+ // Check that the TypedArrays were transferred.
5477+ for ( const array of subArrays ) {
5478+ expect ( array . length ) . toEqual ( 0 ) ;
53595479 }
5360- ) ;
5480+
5481+ await loadingTask . destroy ( ) ;
5482+ } ) ;
53615483 } ) ;
53625484
53635485 describe ( "Annotations" , function ( ) {
0 commit comments