@vaadin-component-factory/vcf-pdf-viewer 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- import { c as assert, l as createPromiseCapability, n as stringToBytes } from './util.js';
2
- import { v as validateRangeRequestCapabilities, e as extractFilenameFromHeader, c as createResponseStatusError } from './network_utils.js';
3
- import './display_utils.js';
4
-
1
+ import { c as assert, l as createPromiseCapability, n as stringToBytes } from './util.js';
2
+ import { v as validateRangeRequestCapabilities, e as extractFilenameFromHeader, c as createResponseStatusError } from './network_utils.js';
3
+ import './display_utils.js';
4
+
5
5
  /* Copyright 2012 Mozilla Foundation
6
6
  *
7
7
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,551 +15,551 @@ import './display_utils.js';
15
15
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
16
  * See the License for the specific language governing permissions and
17
17
  * limitations under the License.
18
- */
19
-
20
- if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
21
- throw new Error('Module "./network.js" shall not be used with MOZCENTRAL builds.');
22
- }
23
-
24
- const OK_RESPONSE = 200;
25
- const PARTIAL_CONTENT_RESPONSE = 206;
26
-
27
- function getArrayBuffer(xhr) {
28
- const data = xhr.response;
29
-
30
- if (typeof data !== "string") {
31
- return data;
32
- }
33
-
34
- const array = stringToBytes(data);
35
- return array.buffer;
36
- }
37
-
38
- class NetworkManager {
39
- constructor(url, args) {
40
- this.url = url;
41
- args = args || {};
42
- this.isHttp = /^https?:/i.test(url);
43
- this.httpHeaders = this.isHttp && args.httpHeaders || {};
44
- this.withCredentials = args.withCredentials || false;
45
-
46
- this.getXhr = args.getXhr || function NetworkManager_getXhr() {
47
- return new XMLHttpRequest();
48
- };
49
-
50
- this.currXhrId = 0;
51
- this.pendingRequests = Object.create(null);
52
- }
53
-
54
- requestRange(begin, end, listeners) {
55
- const args = {
56
- begin,
57
- end
58
- };
59
-
60
- for (const prop in listeners) {
61
- args[prop] = listeners[prop];
62
- }
63
-
64
- return this.request(args);
65
- }
66
-
67
- requestFull(listeners) {
68
- return this.request(listeners);
69
- }
70
-
71
- request(args) {
72
- const xhr = this.getXhr();
73
- const xhrId = this.currXhrId++;
74
- const pendingRequest = this.pendingRequests[xhrId] = {
75
- xhr
76
- };
77
- xhr.open("GET", this.url);
78
- xhr.withCredentials = this.withCredentials;
79
-
80
- for (const property in this.httpHeaders) {
81
- const value = this.httpHeaders[property];
82
-
83
- if (typeof value === "undefined") {
84
- continue;
85
- }
86
-
87
- xhr.setRequestHeader(property, value);
88
- }
89
-
90
- if (this.isHttp && "begin" in args && "end" in args) {
91
- xhr.setRequestHeader("Range", `bytes=${args.begin}-${args.end - 1}`);
92
- pendingRequest.expectedStatus = PARTIAL_CONTENT_RESPONSE;
93
- } else {
94
- pendingRequest.expectedStatus = OK_RESPONSE;
95
- }
96
-
97
- xhr.responseType = "arraybuffer";
98
-
99
- if (args.onError) {
100
- xhr.onerror = function (evt) {
101
- args.onError(xhr.status);
102
- };
103
- }
104
-
105
- xhr.onreadystatechange = this.onStateChange.bind(this, xhrId);
106
- xhr.onprogress = this.onProgress.bind(this, xhrId);
107
- pendingRequest.onHeadersReceived = args.onHeadersReceived;
108
- pendingRequest.onDone = args.onDone;
109
- pendingRequest.onError = args.onError;
110
- pendingRequest.onProgress = args.onProgress;
111
- xhr.send(null);
112
- return xhrId;
113
- }
114
-
115
- onProgress(xhrId, evt) {
116
- const pendingRequest = this.pendingRequests[xhrId];
117
-
118
- if (!pendingRequest) {
119
- // Maybe abortRequest was called...
120
- return;
121
- }
122
-
123
- if (pendingRequest.onProgress) {
124
- pendingRequest.onProgress(evt);
125
- }
126
- }
127
-
128
- onStateChange(xhrId, evt) {
129
- const pendingRequest = this.pendingRequests[xhrId];
130
-
131
- if (!pendingRequest) {
132
- // Maybe abortRequest was called...
133
- return;
134
- }
135
-
136
- const xhr = pendingRequest.xhr;
137
-
138
- if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) {
139
- pendingRequest.onHeadersReceived();
140
- delete pendingRequest.onHeadersReceived;
141
- }
142
-
143
- if (xhr.readyState !== 4) {
144
- return;
145
- }
146
-
147
- if (!(xhrId in this.pendingRequests)) {
148
- // The XHR request might have been aborted in onHeadersReceived()
149
- // callback, in which case we should abort request.
150
- return;
151
- }
152
-
153
- delete this.pendingRequests[xhrId]; // Success status == 0 can be on ftp, file and other protocols.
154
-
155
- if (xhr.status === 0 && this.isHttp) {
156
- if (pendingRequest.onError) {
157
- pendingRequest.onError(xhr.status);
158
- }
159
-
160
- return;
161
- }
162
-
163
- const xhrStatus = xhr.status || OK_RESPONSE; // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2:
164
- // "A server MAY ignore the Range header". This means it's possible to
165
- // get a 200 rather than a 206 response from a range request.
166
-
167
- const ok_response_on_range_request = xhrStatus === OK_RESPONSE && pendingRequest.expectedStatus === PARTIAL_CONTENT_RESPONSE;
168
-
169
- if (!ok_response_on_range_request && xhrStatus !== pendingRequest.expectedStatus) {
170
- if (pendingRequest.onError) {
171
- pendingRequest.onError(xhr.status);
172
- }
173
-
174
- return;
175
- }
176
-
177
- const chunk = getArrayBuffer(xhr);
178
-
179
- if (xhrStatus === PARTIAL_CONTENT_RESPONSE) {
180
- const rangeHeader = xhr.getResponseHeader("Content-Range");
181
- const matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader);
182
- pendingRequest.onDone({
183
- begin: parseInt(matches[1], 10),
184
- chunk
185
- });
186
- } else if (chunk) {
187
- pendingRequest.onDone({
188
- begin: 0,
189
- chunk
190
- });
191
- } else if (pendingRequest.onError) {
192
- pendingRequest.onError(xhr.status);
193
- }
194
- }
195
-
196
- getRequestXhr(xhrId) {
197
- return this.pendingRequests[xhrId].xhr;
198
- }
199
-
200
- isPendingRequest(xhrId) {
201
- return xhrId in this.pendingRequests;
202
- }
203
-
204
- abortRequest(xhrId) {
205
- const xhr = this.pendingRequests[xhrId].xhr;
206
- delete this.pendingRequests[xhrId];
207
- xhr.abort();
208
- }
209
-
210
- }
211
- /** @implements {IPDFStream} */
212
-
213
-
214
- class PDFNetworkStream {
215
- constructor(source) {
216
- this._source = source;
217
- this._manager = new NetworkManager(source.url, {
218
- httpHeaders: source.httpHeaders,
219
- withCredentials: source.withCredentials
220
- });
221
- this._rangeChunkSize = source.rangeChunkSize;
222
- this._fullRequestReader = null;
223
- this._rangeRequestReaders = [];
224
- }
225
-
226
- _onRangeRequestReaderClosed(reader) {
227
- const i = this._rangeRequestReaders.indexOf(reader);
228
-
229
- if (i >= 0) {
230
- this._rangeRequestReaders.splice(i, 1);
231
- }
232
- }
233
-
234
- getFullReader() {
235
- assert(!this._fullRequestReader, "PDFNetworkStream.getFullReader can only be called once.");
236
- this._fullRequestReader = new PDFNetworkStreamFullRequestReader(this._manager, this._source);
237
- return this._fullRequestReader;
238
- }
239
-
240
- getRangeReader(begin, end) {
241
- const reader = new PDFNetworkStreamRangeRequestReader(this._manager, begin, end);
242
- reader.onClosed = this._onRangeRequestReaderClosed.bind(this);
243
-
244
- this._rangeRequestReaders.push(reader);
245
-
246
- return reader;
247
- }
248
-
249
- cancelAllRequests(reason) {
250
- if (this._fullRequestReader) {
251
- this._fullRequestReader.cancel(reason);
252
- }
253
-
254
- for (const reader of this._rangeRequestReaders.slice(0)) {
255
- reader.cancel(reason);
256
- }
257
- }
258
-
259
- }
260
- /** @implements {IPDFStreamReader} */
261
-
262
-
263
- class PDFNetworkStreamFullRequestReader {
264
- constructor(manager, source) {
265
- this._manager = manager;
266
- const args = {
267
- onHeadersReceived: this._onHeadersReceived.bind(this),
268
- onDone: this._onDone.bind(this),
269
- onError: this._onError.bind(this),
270
- onProgress: this._onProgress.bind(this)
271
- };
272
- this._url = source.url;
273
- this._fullRequestId = manager.requestFull(args);
274
- this._headersReceivedCapability = createPromiseCapability();
275
- this._disableRange = source.disableRange || false;
276
- this._contentLength = source.length; // Optional
277
-
278
- this._rangeChunkSize = source.rangeChunkSize;
279
-
280
- if (!this._rangeChunkSize && !this._disableRange) {
281
- this._disableRange = true;
282
- }
283
-
284
- this._isStreamingSupported = false;
285
- this._isRangeSupported = false;
286
- this._cachedChunks = [];
287
- this._requests = [];
288
- this._done = false;
289
- this._storedError = undefined;
290
- this._filename = null;
291
- this.onProgress = null;
292
- }
293
-
294
- _onHeadersReceived() {
295
- const fullRequestXhrId = this._fullRequestId;
296
-
297
- const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
298
-
299
- const getResponseHeader = name => {
300
- return fullRequestXhr.getResponseHeader(name);
301
- };
302
-
303
- const {
304
- allowRangeRequests,
305
- suggestedLength
306
- } = validateRangeRequestCapabilities({
307
- getResponseHeader,
308
- isHttp: this._manager.isHttp,
309
- rangeChunkSize: this._rangeChunkSize,
310
- disableRange: this._disableRange
311
- });
312
-
313
- if (allowRangeRequests) {
314
- this._isRangeSupported = true;
315
- } // Setting right content length.
316
-
317
-
318
- this._contentLength = suggestedLength || this._contentLength;
319
- this._filename = extractFilenameFromHeader(getResponseHeader);
320
-
321
- if (this._isRangeSupported) {
322
- // NOTE: by cancelling the full request, and then issuing range
323
- // requests, there will be an issue for sites where you can only
324
- // request the pdf once. However, if this is the case, then the
325
- // server should not be returning that it can support range requests.
326
- this._manager.abortRequest(fullRequestXhrId);
327
- }
328
-
329
- this._headersReceivedCapability.resolve();
330
- }
331
-
332
- _onDone(args) {
333
- if (args) {
334
- if (this._requests.length > 0) {
335
- const requestCapability = this._requests.shift();
336
-
337
- requestCapability.resolve({
338
- value: args.chunk,
339
- done: false
340
- });
341
- } else {
342
- this._cachedChunks.push(args.chunk);
343
- }
344
- }
345
-
346
- this._done = true;
347
-
348
- if (this._cachedChunks.length > 0) {
349
- return;
350
- }
351
-
352
- for (const requestCapability of this._requests) {
353
- requestCapability.resolve({
354
- value: undefined,
355
- done: true
356
- });
357
- }
358
-
359
- this._requests.length = 0;
360
- }
361
-
362
- _onError(status) {
363
- const url = this._url;
364
- const exception = createResponseStatusError(status, url);
365
- this._storedError = exception;
366
-
367
- this._headersReceivedCapability.reject(exception);
368
-
369
- for (const requestCapability of this._requests) {
370
- requestCapability.reject(exception);
371
- }
372
-
373
- this._requests.length = 0;
374
- this._cachedChunks.length = 0;
375
- }
376
-
377
- _onProgress(data) {
378
- if (this.onProgress) {
379
- this.onProgress({
380
- loaded: data.loaded,
381
- total: data.lengthComputable ? data.total : this._contentLength
382
- });
383
- }
384
- }
385
-
386
- get filename() {
387
- return this._filename;
388
- }
389
-
390
- get isRangeSupported() {
391
- return this._isRangeSupported;
392
- }
393
-
394
- get isStreamingSupported() {
395
- return this._isStreamingSupported;
396
- }
397
-
398
- get contentLength() {
399
- return this._contentLength;
400
- }
401
-
402
- get headersReady() {
403
- return this._headersReceivedCapability.promise;
404
- }
405
-
406
- async read() {
407
- if (this._storedError) {
408
- throw this._storedError;
409
- }
410
-
411
- if (this._cachedChunks.length > 0) {
412
- const chunk = this._cachedChunks.shift();
413
-
414
- return {
415
- value: chunk,
416
- done: false
417
- };
418
- }
419
-
420
- if (this._done) {
421
- return {
422
- value: undefined,
423
- done: true
424
- };
425
- }
426
-
427
- const requestCapability = createPromiseCapability();
428
-
429
- this._requests.push(requestCapability);
430
-
431
- return requestCapability.promise;
432
- }
433
-
434
- cancel(reason) {
435
- this._done = true;
436
-
437
- this._headersReceivedCapability.reject(reason);
438
-
439
- for (const requestCapability of this._requests) {
440
- requestCapability.resolve({
441
- value: undefined,
442
- done: true
443
- });
444
- }
445
-
446
- this._requests.length = 0;
447
-
448
- if (this._manager.isPendingRequest(this._fullRequestId)) {
449
- this._manager.abortRequest(this._fullRequestId);
450
- }
451
-
452
- this._fullRequestReader = null;
453
- }
454
-
455
- }
456
- /** @implements {IPDFStreamRangeReader} */
457
-
458
-
459
- class PDFNetworkStreamRangeRequestReader {
460
- constructor(manager, begin, end) {
461
- this._manager = manager;
462
- const args = {
463
- onDone: this._onDone.bind(this),
464
- onProgress: this._onProgress.bind(this)
465
- };
466
- this._requestId = manager.requestRange(begin, end, args);
467
- this._requests = [];
468
- this._queuedChunk = null;
469
- this._done = false;
470
- this.onProgress = null;
471
- this.onClosed = null;
472
- }
473
-
474
- _close() {
475
- if (this.onClosed) {
476
- this.onClosed(this);
477
- }
478
- }
479
-
480
- _onDone(data) {
481
- const chunk = data.chunk;
482
-
483
- if (this._requests.length > 0) {
484
- const requestCapability = this._requests.shift();
485
-
486
- requestCapability.resolve({
487
- value: chunk,
488
- done: false
489
- });
490
- } else {
491
- this._queuedChunk = chunk;
492
- }
493
-
494
- this._done = true;
495
-
496
- for (const requestCapability of this._requests) {
497
- requestCapability.resolve({
498
- value: undefined,
499
- done: true
500
- });
501
- }
502
-
503
- this._requests.length = 0;
504
-
505
- this._close();
506
- }
507
-
508
- _onProgress(evt) {
509
- if (!this.isStreamingSupported && this.onProgress) {
510
- this.onProgress({
511
- loaded: evt.loaded
512
- });
513
- }
514
- }
515
-
516
- get isStreamingSupported() {
517
- return false;
518
- }
519
-
520
- async read() {
521
- if (this._queuedChunk !== null) {
522
- const chunk = this._queuedChunk;
523
- this._queuedChunk = null;
524
- return {
525
- value: chunk,
526
- done: false
527
- };
528
- }
529
-
530
- if (this._done) {
531
- return {
532
- value: undefined,
533
- done: true
534
- };
535
- }
536
-
537
- const requestCapability = createPromiseCapability();
538
-
539
- this._requests.push(requestCapability);
540
-
541
- return requestCapability.promise;
542
- }
543
-
544
- cancel(reason) {
545
- this._done = true;
546
-
547
- for (const requestCapability of this._requests) {
548
- requestCapability.resolve({
549
- value: undefined,
550
- done: true
551
- });
552
- }
553
-
554
- this._requests.length = 0;
555
-
556
- if (this._manager.isPendingRequest(this._requestId)) {
557
- this._manager.abortRequest(this._requestId);
558
- }
559
-
560
- this._close();
561
- }
562
-
563
- }
564
-
565
- export { PDFNetworkStream };
18
+ */
19
+
20
+ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
21
+ throw new Error('Module "./network.js" shall not be used with MOZCENTRAL builds.');
22
+ }
23
+
24
+ const OK_RESPONSE = 200;
25
+ const PARTIAL_CONTENT_RESPONSE = 206;
26
+
27
+ function getArrayBuffer(xhr) {
28
+ const data = xhr.response;
29
+
30
+ if (typeof data !== "string") {
31
+ return data;
32
+ }
33
+
34
+ const array = stringToBytes(data);
35
+ return array.buffer;
36
+ }
37
+
38
+ class NetworkManager {
39
+ constructor(url, args) {
40
+ this.url = url;
41
+ args = args || {};
42
+ this.isHttp = /^https?:/i.test(url);
43
+ this.httpHeaders = this.isHttp && args.httpHeaders || {};
44
+ this.withCredentials = args.withCredentials || false;
45
+
46
+ this.getXhr = args.getXhr || function NetworkManager_getXhr() {
47
+ return new XMLHttpRequest();
48
+ };
49
+
50
+ this.currXhrId = 0;
51
+ this.pendingRequests = Object.create(null);
52
+ }
53
+
54
+ requestRange(begin, end, listeners) {
55
+ const args = {
56
+ begin,
57
+ end
58
+ };
59
+
60
+ for (const prop in listeners) {
61
+ args[prop] = listeners[prop];
62
+ }
63
+
64
+ return this.request(args);
65
+ }
66
+
67
+ requestFull(listeners) {
68
+ return this.request(listeners);
69
+ }
70
+
71
+ request(args) {
72
+ const xhr = this.getXhr();
73
+ const xhrId = this.currXhrId++;
74
+ const pendingRequest = this.pendingRequests[xhrId] = {
75
+ xhr
76
+ };
77
+ xhr.open("GET", this.url);
78
+ xhr.withCredentials = this.withCredentials;
79
+
80
+ for (const property in this.httpHeaders) {
81
+ const value = this.httpHeaders[property];
82
+
83
+ if (typeof value === "undefined") {
84
+ continue;
85
+ }
86
+
87
+ xhr.setRequestHeader(property, value);
88
+ }
89
+
90
+ if (this.isHttp && "begin" in args && "end" in args) {
91
+ xhr.setRequestHeader("Range", `bytes=${args.begin}-${args.end - 1}`);
92
+ pendingRequest.expectedStatus = PARTIAL_CONTENT_RESPONSE;
93
+ } else {
94
+ pendingRequest.expectedStatus = OK_RESPONSE;
95
+ }
96
+
97
+ xhr.responseType = "arraybuffer";
98
+
99
+ if (args.onError) {
100
+ xhr.onerror = function (evt) {
101
+ args.onError(xhr.status);
102
+ };
103
+ }
104
+
105
+ xhr.onreadystatechange = this.onStateChange.bind(this, xhrId);
106
+ xhr.onprogress = this.onProgress.bind(this, xhrId);
107
+ pendingRequest.onHeadersReceived = args.onHeadersReceived;
108
+ pendingRequest.onDone = args.onDone;
109
+ pendingRequest.onError = args.onError;
110
+ pendingRequest.onProgress = args.onProgress;
111
+ xhr.send(null);
112
+ return xhrId;
113
+ }
114
+
115
+ onProgress(xhrId, evt) {
116
+ const pendingRequest = this.pendingRequests[xhrId];
117
+
118
+ if (!pendingRequest) {
119
+ // Maybe abortRequest was called...
120
+ return;
121
+ }
122
+
123
+ if (pendingRequest.onProgress) {
124
+ pendingRequest.onProgress(evt);
125
+ }
126
+ }
127
+
128
+ onStateChange(xhrId, evt) {
129
+ const pendingRequest = this.pendingRequests[xhrId];
130
+
131
+ if (!pendingRequest) {
132
+ // Maybe abortRequest was called...
133
+ return;
134
+ }
135
+
136
+ const xhr = pendingRequest.xhr;
137
+
138
+ if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) {
139
+ pendingRequest.onHeadersReceived();
140
+ delete pendingRequest.onHeadersReceived;
141
+ }
142
+
143
+ if (xhr.readyState !== 4) {
144
+ return;
145
+ }
146
+
147
+ if (!(xhrId in this.pendingRequests)) {
148
+ // The XHR request might have been aborted in onHeadersReceived()
149
+ // callback, in which case we should abort request.
150
+ return;
151
+ }
152
+
153
+ delete this.pendingRequests[xhrId]; // Success status == 0 can be on ftp, file and other protocols.
154
+
155
+ if (xhr.status === 0 && this.isHttp) {
156
+ if (pendingRequest.onError) {
157
+ pendingRequest.onError(xhr.status);
158
+ }
159
+
160
+ return;
161
+ }
162
+
163
+ const xhrStatus = xhr.status || OK_RESPONSE; // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2:
164
+ // "A server MAY ignore the Range header". This means it's possible to
165
+ // get a 200 rather than a 206 response from a range request.
166
+
167
+ const ok_response_on_range_request = xhrStatus === OK_RESPONSE && pendingRequest.expectedStatus === PARTIAL_CONTENT_RESPONSE;
168
+
169
+ if (!ok_response_on_range_request && xhrStatus !== pendingRequest.expectedStatus) {
170
+ if (pendingRequest.onError) {
171
+ pendingRequest.onError(xhr.status);
172
+ }
173
+
174
+ return;
175
+ }
176
+
177
+ const chunk = getArrayBuffer(xhr);
178
+
179
+ if (xhrStatus === PARTIAL_CONTENT_RESPONSE) {
180
+ const rangeHeader = xhr.getResponseHeader("Content-Range");
181
+ const matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader);
182
+ pendingRequest.onDone({
183
+ begin: parseInt(matches[1], 10),
184
+ chunk
185
+ });
186
+ } else if (chunk) {
187
+ pendingRequest.onDone({
188
+ begin: 0,
189
+ chunk
190
+ });
191
+ } else if (pendingRequest.onError) {
192
+ pendingRequest.onError(xhr.status);
193
+ }
194
+ }
195
+
196
+ getRequestXhr(xhrId) {
197
+ return this.pendingRequests[xhrId].xhr;
198
+ }
199
+
200
+ isPendingRequest(xhrId) {
201
+ return xhrId in this.pendingRequests;
202
+ }
203
+
204
+ abortRequest(xhrId) {
205
+ const xhr = this.pendingRequests[xhrId].xhr;
206
+ delete this.pendingRequests[xhrId];
207
+ xhr.abort();
208
+ }
209
+
210
+ }
211
+ /** @implements {IPDFStream} */
212
+
213
+
214
+ class PDFNetworkStream {
215
+ constructor(source) {
216
+ this._source = source;
217
+ this._manager = new NetworkManager(source.url, {
218
+ httpHeaders: source.httpHeaders,
219
+ withCredentials: source.withCredentials
220
+ });
221
+ this._rangeChunkSize = source.rangeChunkSize;
222
+ this._fullRequestReader = null;
223
+ this._rangeRequestReaders = [];
224
+ }
225
+
226
+ _onRangeRequestReaderClosed(reader) {
227
+ const i = this._rangeRequestReaders.indexOf(reader);
228
+
229
+ if (i >= 0) {
230
+ this._rangeRequestReaders.splice(i, 1);
231
+ }
232
+ }
233
+
234
+ getFullReader() {
235
+ assert(!this._fullRequestReader, "PDFNetworkStream.getFullReader can only be called once.");
236
+ this._fullRequestReader = new PDFNetworkStreamFullRequestReader(this._manager, this._source);
237
+ return this._fullRequestReader;
238
+ }
239
+
240
+ getRangeReader(begin, end) {
241
+ const reader = new PDFNetworkStreamRangeRequestReader(this._manager, begin, end);
242
+ reader.onClosed = this._onRangeRequestReaderClosed.bind(this);
243
+
244
+ this._rangeRequestReaders.push(reader);
245
+
246
+ return reader;
247
+ }
248
+
249
+ cancelAllRequests(reason) {
250
+ if (this._fullRequestReader) {
251
+ this._fullRequestReader.cancel(reason);
252
+ }
253
+
254
+ for (const reader of this._rangeRequestReaders.slice(0)) {
255
+ reader.cancel(reason);
256
+ }
257
+ }
258
+
259
+ }
260
+ /** @implements {IPDFStreamReader} */
261
+
262
+
263
+ class PDFNetworkStreamFullRequestReader {
264
+ constructor(manager, source) {
265
+ this._manager = manager;
266
+ const args = {
267
+ onHeadersReceived: this._onHeadersReceived.bind(this),
268
+ onDone: this._onDone.bind(this),
269
+ onError: this._onError.bind(this),
270
+ onProgress: this._onProgress.bind(this)
271
+ };
272
+ this._url = source.url;
273
+ this._fullRequestId = manager.requestFull(args);
274
+ this._headersReceivedCapability = createPromiseCapability();
275
+ this._disableRange = source.disableRange || false;
276
+ this._contentLength = source.length; // Optional
277
+
278
+ this._rangeChunkSize = source.rangeChunkSize;
279
+
280
+ if (!this._rangeChunkSize && !this._disableRange) {
281
+ this._disableRange = true;
282
+ }
283
+
284
+ this._isStreamingSupported = false;
285
+ this._isRangeSupported = false;
286
+ this._cachedChunks = [];
287
+ this._requests = [];
288
+ this._done = false;
289
+ this._storedError = undefined;
290
+ this._filename = null;
291
+ this.onProgress = null;
292
+ }
293
+
294
+ _onHeadersReceived() {
295
+ const fullRequestXhrId = this._fullRequestId;
296
+
297
+ const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
298
+
299
+ const getResponseHeader = name => {
300
+ return fullRequestXhr.getResponseHeader(name);
301
+ };
302
+
303
+ const {
304
+ allowRangeRequests,
305
+ suggestedLength
306
+ } = validateRangeRequestCapabilities({
307
+ getResponseHeader,
308
+ isHttp: this._manager.isHttp,
309
+ rangeChunkSize: this._rangeChunkSize,
310
+ disableRange: this._disableRange
311
+ });
312
+
313
+ if (allowRangeRequests) {
314
+ this._isRangeSupported = true;
315
+ } // Setting right content length.
316
+
317
+
318
+ this._contentLength = suggestedLength || this._contentLength;
319
+ this._filename = extractFilenameFromHeader(getResponseHeader);
320
+
321
+ if (this._isRangeSupported) {
322
+ // NOTE: by cancelling the full request, and then issuing range
323
+ // requests, there will be an issue for sites where you can only
324
+ // request the pdf once. However, if this is the case, then the
325
+ // server should not be returning that it can support range requests.
326
+ this._manager.abortRequest(fullRequestXhrId);
327
+ }
328
+
329
+ this._headersReceivedCapability.resolve();
330
+ }
331
+
332
+ _onDone(args) {
333
+ if (args) {
334
+ if (this._requests.length > 0) {
335
+ const requestCapability = this._requests.shift();
336
+
337
+ requestCapability.resolve({
338
+ value: args.chunk,
339
+ done: false
340
+ });
341
+ } else {
342
+ this._cachedChunks.push(args.chunk);
343
+ }
344
+ }
345
+
346
+ this._done = true;
347
+
348
+ if (this._cachedChunks.length > 0) {
349
+ return;
350
+ }
351
+
352
+ for (const requestCapability of this._requests) {
353
+ requestCapability.resolve({
354
+ value: undefined,
355
+ done: true
356
+ });
357
+ }
358
+
359
+ this._requests.length = 0;
360
+ }
361
+
362
+ _onError(status) {
363
+ const url = this._url;
364
+ const exception = createResponseStatusError(status, url);
365
+ this._storedError = exception;
366
+
367
+ this._headersReceivedCapability.reject(exception);
368
+
369
+ for (const requestCapability of this._requests) {
370
+ requestCapability.reject(exception);
371
+ }
372
+
373
+ this._requests.length = 0;
374
+ this._cachedChunks.length = 0;
375
+ }
376
+
377
+ _onProgress(data) {
378
+ if (this.onProgress) {
379
+ this.onProgress({
380
+ loaded: data.loaded,
381
+ total: data.lengthComputable ? data.total : this._contentLength
382
+ });
383
+ }
384
+ }
385
+
386
+ get filename() {
387
+ return this._filename;
388
+ }
389
+
390
+ get isRangeSupported() {
391
+ return this._isRangeSupported;
392
+ }
393
+
394
+ get isStreamingSupported() {
395
+ return this._isStreamingSupported;
396
+ }
397
+
398
+ get contentLength() {
399
+ return this._contentLength;
400
+ }
401
+
402
+ get headersReady() {
403
+ return this._headersReceivedCapability.promise;
404
+ }
405
+
406
+ async read() {
407
+ if (this._storedError) {
408
+ throw this._storedError;
409
+ }
410
+
411
+ if (this._cachedChunks.length > 0) {
412
+ const chunk = this._cachedChunks.shift();
413
+
414
+ return {
415
+ value: chunk,
416
+ done: false
417
+ };
418
+ }
419
+
420
+ if (this._done) {
421
+ return {
422
+ value: undefined,
423
+ done: true
424
+ };
425
+ }
426
+
427
+ const requestCapability = createPromiseCapability();
428
+
429
+ this._requests.push(requestCapability);
430
+
431
+ return requestCapability.promise;
432
+ }
433
+
434
+ cancel(reason) {
435
+ this._done = true;
436
+
437
+ this._headersReceivedCapability.reject(reason);
438
+
439
+ for (const requestCapability of this._requests) {
440
+ requestCapability.resolve({
441
+ value: undefined,
442
+ done: true
443
+ });
444
+ }
445
+
446
+ this._requests.length = 0;
447
+
448
+ if (this._manager.isPendingRequest(this._fullRequestId)) {
449
+ this._manager.abortRequest(this._fullRequestId);
450
+ }
451
+
452
+ this._fullRequestReader = null;
453
+ }
454
+
455
+ }
456
+ /** @implements {IPDFStreamRangeReader} */
457
+
458
+
459
+ class PDFNetworkStreamRangeRequestReader {
460
+ constructor(manager, begin, end) {
461
+ this._manager = manager;
462
+ const args = {
463
+ onDone: this._onDone.bind(this),
464
+ onProgress: this._onProgress.bind(this)
465
+ };
466
+ this._requestId = manager.requestRange(begin, end, args);
467
+ this._requests = [];
468
+ this._queuedChunk = null;
469
+ this._done = false;
470
+ this.onProgress = null;
471
+ this.onClosed = null;
472
+ }
473
+
474
+ _close() {
475
+ if (this.onClosed) {
476
+ this.onClosed(this);
477
+ }
478
+ }
479
+
480
+ _onDone(data) {
481
+ const chunk = data.chunk;
482
+
483
+ if (this._requests.length > 0) {
484
+ const requestCapability = this._requests.shift();
485
+
486
+ requestCapability.resolve({
487
+ value: chunk,
488
+ done: false
489
+ });
490
+ } else {
491
+ this._queuedChunk = chunk;
492
+ }
493
+
494
+ this._done = true;
495
+
496
+ for (const requestCapability of this._requests) {
497
+ requestCapability.resolve({
498
+ value: undefined,
499
+ done: true
500
+ });
501
+ }
502
+
503
+ this._requests.length = 0;
504
+
505
+ this._close();
506
+ }
507
+
508
+ _onProgress(evt) {
509
+ if (!this.isStreamingSupported && this.onProgress) {
510
+ this.onProgress({
511
+ loaded: evt.loaded
512
+ });
513
+ }
514
+ }
515
+
516
+ get isStreamingSupported() {
517
+ return false;
518
+ }
519
+
520
+ async read() {
521
+ if (this._queuedChunk !== null) {
522
+ const chunk = this._queuedChunk;
523
+ this._queuedChunk = null;
524
+ return {
525
+ value: chunk,
526
+ done: false
527
+ };
528
+ }
529
+
530
+ if (this._done) {
531
+ return {
532
+ value: undefined,
533
+ done: true
534
+ };
535
+ }
536
+
537
+ const requestCapability = createPromiseCapability();
538
+
539
+ this._requests.push(requestCapability);
540
+
541
+ return requestCapability.promise;
542
+ }
543
+
544
+ cancel(reason) {
545
+ this._done = true;
546
+
547
+ for (const requestCapability of this._requests) {
548
+ requestCapability.resolve({
549
+ value: undefined,
550
+ done: true
551
+ });
552
+ }
553
+
554
+ this._requests.length = 0;
555
+
556
+ if (this._manager.isPendingRequest(this._requestId)) {
557
+ this._manager.abortRequest(this._requestId);
558
+ }
559
+
560
+ this._close();
561
+ }
562
+
563
+ }
564
+
565
+ export { PDFNetworkStream };