1616import { assert , stringToBytes , warn } from "../shared/util.js" ;
1717import {
1818 BasePDFStream ,
19+ BasePDFStreamRangeReader ,
1920 BasePDFStreamReader ,
2021} from "../shared/base_pdf_stream.js" ;
2122import {
2223 createHeaders ,
2324 createResponseError ,
25+ ensureResponseOrigin ,
2426 extractFilenameFromHeader ,
2527 getResponseOrigin ,
2628 validateRangeRequestCapabilities ,
@@ -307,57 +309,59 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
307309 }
308310}
309311
310- /** @implements {IPDFStreamRangeReader} */
311- class PDFNetworkStreamRangeReader {
312+ class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
312313 onClosed = null ;
313314
315+ _done = false ;
316+
317+ _queuedChunk = null ;
318+
319+ _requests = [ ] ;
320+
321+ _storedError = null ;
322+
314323 constructor ( stream , begin , end ) {
315- this . _stream = stream ;
324+ super ( stream , begin , end ) ;
316325
317326 this . _requestXhr = stream . _request ( {
318327 begin,
319328 end,
320- onHeadersReceived : this . _onHeadersReceived . bind ( this ) ,
321- onDone : this . _onDone . bind ( this ) ,
322- onError : this . _onError . bind ( this ) ,
329+ onHeadersReceived : this . #onHeadersReceived . bind ( this ) ,
330+ onDone : this . #onDone . bind ( this ) ,
331+ onError : this . #onError . bind ( this ) ,
323332 onProgress : null ,
324333 } ) ;
325- this . _requests = [ ] ;
326- this . _queuedChunk = null ;
327- this . _done = false ;
328- this . _storedError = undefined ;
329334 }
330335
331- _onHeadersReceived ( ) {
336+ #onHeadersReceived ( ) {
332337 const responseOrigin = getResponseOrigin ( this . _requestXhr ?. responseURL ) ;
333-
334- if ( responseOrigin !== this . _stream . _responseOrigin ) {
335- this . _storedError = new Error (
336- `Expected range response-origin "${ responseOrigin } " to match "${ this . _stream . _responseOrigin } ".`
337- ) ;
338- this . _onError ( 0 ) ;
338+ try {
339+ ensureResponseOrigin ( responseOrigin , this . _stream . _responseOrigin ) ;
340+ } catch ( ex ) {
341+ this . _storedError = ex ;
342+ this . #onError( 0 ) ;
339343 }
340344 }
341345
342- _onDone ( chunk ) {
346+ #onDone ( chunk ) {
343347 if ( this . _requests . length > 0 ) {
344- const requestCapability = this . _requests . shift ( ) ;
345- requestCapability . resolve ( { value : chunk , done : false } ) ;
348+ const capability = this . _requests . shift ( ) ;
349+ capability . resolve ( { value : chunk , done : false } ) ;
346350 } else {
347351 this . _queuedChunk = chunk ;
348352 }
349353 this . _done = true ;
350- for ( const requestCapability of this . _requests ) {
351- requestCapability . resolve ( { value : undefined , done : true } ) ;
354+ for ( const capability of this . _requests ) {
355+ capability . resolve ( { value : undefined , done : true } ) ;
352356 }
353357 this . _requests . length = 0 ;
354358 this . onClosed ?. ( ) ;
355359 }
356360
357- _onError ( status ) {
361+ #onError ( status ) {
358362 this . _storedError ??= createResponseError ( status , this . _stream . url ) ;
359- for ( const requestCapability of this . _requests ) {
360- requestCapability . reject ( this . _storedError ) ;
363+ for ( const capability of this . _requests ) {
364+ capability . reject ( this . _storedError ) ;
361365 }
362366 this . _requests . length = 0 ;
363367 this . _queuedChunk = null ;
@@ -375,15 +379,15 @@ class PDFNetworkStreamRangeReader {
375379 if ( this . _done ) {
376380 return { value : undefined , done : true } ;
377381 }
378- const requestCapability = Promise . withResolvers ( ) ;
379- this . _requests . push ( requestCapability ) ;
380- return requestCapability . promise ;
382+ const capability = Promise . withResolvers ( ) ;
383+ this . _requests . push ( capability ) ;
384+ return capability . promise ;
381385 }
382386
383387 cancel ( reason ) {
384388 this . _done = true ;
385- for ( const requestCapability of this . _requests ) {
386- requestCapability . resolve ( { value : undefined , done : true } ) ;
389+ for ( const capability of this . _requests ) {
390+ capability . resolve ( { value : undefined , done : true } ) ;
387391 }
388392 this . _requests . length = 0 ;
389393
0 commit comments