@@ -18,6 +18,12 @@ import { assert } from "../shared/util.js";
1818import { Stream } from "./stream.js" ;
1919
2020class ChunkedStream extends Stream {
21+ progressiveDataLength = 0 ;
22+
23+ _lastSuccessfulEnsureByteChunk = - 1 ; // Single-entry cache
24+
25+ _loadedChunks = new Set ( ) ;
26+
2127 constructor ( length , chunkSize , manager ) {
2228 super (
2329 /* arrayBuffer = */ new Uint8Array ( length ) ,
@@ -27,11 +33,8 @@ class ChunkedStream extends Stream {
2733 ) ;
2834
2935 this . chunkSize = chunkSize ;
30- this . _loadedChunks = new Set ( ) ;
3136 this . numChunks = Math . ceil ( length / chunkSize ) ;
3237 this . manager = manager ;
33- this . progressiveDataLength = 0 ;
34- this . lastSuccessfulEnsureByteChunk = - 1 ; // Single-entry cache
3538 }
3639
3740 // If a particular stream does not implement one or more of these methods,
@@ -106,14 +109,14 @@ class ChunkedStream extends Stream {
106109 if ( chunk > this . numChunks ) {
107110 return ;
108111 }
109- if ( chunk === this . lastSuccessfulEnsureByteChunk ) {
112+ if ( chunk === this . _lastSuccessfulEnsureByteChunk ) {
110113 return ;
111114 }
112115
113116 if ( ! this . _loadedChunks . has ( chunk ) ) {
114117 throw new MissingDataException ( pos , pos + 1 ) ;
115118 }
116- this . lastSuccessfulEnsureByteChunk = chunk ;
119+ this . _lastSuccessfulEnsureByteChunk = chunk ;
117120 }
118121
119122 ensureRange ( begin , end ) {
@@ -257,23 +260,25 @@ class ChunkedStream extends Stream {
257260}
258261
259262class ChunkedStreamManager {
263+ aborted = false ;
264+
265+ currRequestId = 0 ;
266+
267+ _chunksNeededByRequest = new Map ( ) ;
268+
269+ _loadedStreamCapability = Promise . withResolvers ( ) ;
270+
271+ _promisesByRequest = new Map ( ) ;
272+
273+ _requestsByChunk = new Map ( ) ;
274+
260275 constructor ( pdfNetworkStream , args ) {
261276 this . length = args . length ;
262277 this . chunkSize = args . rangeChunkSize ;
263278 this . stream = new ChunkedStream ( this . length , this . chunkSize , this ) ;
264279 this . pdfNetworkStream = pdfNetworkStream ;
265280 this . disableAutoFetch = args . disableAutoFetch ;
266281 this . msgHandler = args . msgHandler ;
267-
268- this . currRequestId = 0 ;
269-
270- this . _chunksNeededByRequest = new Map ( ) ;
271- this . _requestsByChunk = new Map ( ) ;
272- this . _promisesByRequest = new Map ( ) ;
273- this . progressiveDataLength = 0 ;
274- this . aborted = false ;
275-
276- this . _loadedStreamCapability = Promise . withResolvers ( ) ;
277282 }
278283
279284 sendRequest ( begin , end ) {
@@ -454,26 +459,25 @@ class ChunkedStreamManager {
454459 }
455460
456461 onReceiveData ( args ) {
462+ const { chunkSize, length, stream } = this ;
463+
457464 const chunk = args . chunk ;
458465 const isProgressive = args . begin === undefined ;
459- const begin = isProgressive ? this . progressiveDataLength : args . begin ;
466+ const begin = isProgressive ? stream . progressiveDataLength : args . begin ;
460467 const end = begin + chunk . byteLength ;
461468
462- const beginChunk = Math . floor ( begin / this . chunkSize ) ;
469+ const beginChunk = Math . floor ( begin / chunkSize ) ;
463470 const endChunk =
464- end < this . length
465- ? Math . floor ( end / this . chunkSize )
466- : Math . ceil ( end / this . chunkSize ) ;
471+ end < length ? Math . floor ( end / chunkSize ) : Math . ceil ( end / chunkSize ) ;
467472
468473 if ( isProgressive ) {
469- this . stream . onReceiveProgressiveData ( chunk ) ;
470- this . progressiveDataLength = end ;
474+ stream . onReceiveProgressiveData ( chunk ) ;
471475 } else {
472- this . stream . onReceiveData ( begin , chunk ) ;
476+ stream . onReceiveData ( begin , chunk ) ;
473477 }
474478
475- if ( this . stream . isDataLoaded ) {
476- this . _loadedStreamCapability . resolve ( this . stream ) ;
479+ if ( stream . isDataLoaded ) {
480+ this . _loadedStreamCapability . resolve ( stream ) ;
477481 }
478482
479483 const loadedRequests = [ ] ;
@@ -502,16 +506,16 @@ class ChunkedStreamManager {
502506 // unfetched chunk of the PDF file.
503507 if ( ! this . disableAutoFetch && this . _requestsByChunk . size === 0 ) {
504508 let nextEmptyChunk ;
505- if ( this . stream . numChunksLoaded === 1 ) {
509+ if ( stream . numChunksLoaded === 1 ) {
506510 // This is a special optimization so that after fetching the first
507511 // chunk, rather than fetching the second chunk, we fetch the last
508512 // chunk.
509- const lastChunk = this . stream . numChunks - 1 ;
510- if ( ! this . stream . hasChunk ( lastChunk ) ) {
513+ const lastChunk = stream . numChunks - 1 ;
514+ if ( ! stream . hasChunk ( lastChunk ) ) {
511515 nextEmptyChunk = lastChunk ;
512516 }
513517 } else {
514- nextEmptyChunk = this . stream . nextEmptyChunk ( endChunk ) ;
518+ nextEmptyChunk = stream . nextEmptyChunk ( endChunk ) ;
515519 }
516520 if ( Number . isInteger ( nextEmptyChunk ) ) {
517521 this . _requestChunks ( [ nextEmptyChunk ] ) ;
@@ -525,8 +529,8 @@ class ChunkedStreamManager {
525529 }
526530
527531 this . msgHandler . send ( "DocProgress" , {
528- loaded : this . stream . numChunksLoaded * this . chunkSize ,
529- total : this . length ,
532+ loaded : stream . numChunksLoaded * chunkSize ,
533+ total : length ,
530534 } ) ;
531535 }
532536
0 commit comments