123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /**
- * @licstart The following is the entire license notice for the
- * Javascript code in this page
- *
- * Copyright 2017 Mozilla Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @licend The above is the entire license notice for the
- * Javascript code in this page
- */
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.PDFFetchStream = undefined;
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- var _util = require('../shared/util');
- var _network_utils = require('./network_utils');
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function createFetchOptions(headers, withCredentials) {
- return {
- method: 'GET',
- headers: headers,
- mode: 'cors',
- credentials: withCredentials ? 'include' : 'same-origin',
- redirect: 'follow'
- };
- }
- var PDFFetchStream = function () {
- function PDFFetchStream(source) {
- _classCallCheck(this, PDFFetchStream);
- this.source = source;
- this.isHttp = /^https?:/i.test(source.url);
- this.httpHeaders = this.isHttp && source.httpHeaders || {};
- this._fullRequestReader = null;
- this._rangeRequestReaders = [];
- }
- _createClass(PDFFetchStream, [{
- key: 'getFullReader',
- value: function getFullReader() {
- (0, _util.assert)(!this._fullRequestReader);
- this._fullRequestReader = new PDFFetchStreamReader(this);
- return this._fullRequestReader;
- }
- }, {
- key: 'getRangeReader',
- value: function getRangeReader(begin, end) {
- var reader = new PDFFetchStreamRangeReader(this, begin, end);
- this._rangeRequestReaders.push(reader);
- return reader;
- }
- }, {
- key: 'cancelAllRequests',
- value: function cancelAllRequests(reason) {
- if (this._fullRequestReader) {
- this._fullRequestReader.cancel(reason);
- }
- var readers = this._rangeRequestReaders.slice(0);
- readers.forEach(function (reader) {
- reader.cancel(reason);
- });
- }
- }]);
- return PDFFetchStream;
- }();
- var PDFFetchStreamReader = function () {
- function PDFFetchStreamReader(stream) {
- var _this = this;
- _classCallCheck(this, PDFFetchStreamReader);
- this._stream = stream;
- this._reader = null;
- this._loaded = 0;
- var source = stream.source;
- this._withCredentials = source.withCredentials;
- this._contentLength = source.length;
- this._headersCapability = (0, _util.createPromiseCapability)();
- this._disableRange = source.disableRange;
- this._rangeChunkSize = source.rangeChunkSize;
- if (!this._rangeChunkSize && !this._disableRange) {
- this._disableRange = true;
- }
- this._isRangeSupported = !source.disableRange;
- this._isStreamingSupported = !source.disableStream;
- this._headers = new Headers();
- for (var property in this._stream.httpHeaders) {
- var value = this._stream.httpHeaders[property];
- if (typeof value === 'undefined') {
- continue;
- }
- this._headers.append(property, value);
- }
- var url = source.url;
- fetch(url, createFetchOptions(this._headers, this._withCredentials)).then(function (response) {
- if (!(0, _network_utils.validateResponseStatus)(response.status)) {
- throw (0, _network_utils.createResponseStatusError)(response.status, url);
- }
- _this._reader = response.body.getReader();
- _this._headersCapability.resolve();
- var _validateRangeRequest = (0, _network_utils.validateRangeRequestCapabilities)({
- getResponseHeader: function getResponseHeader(name) {
- return response.headers.get(name);
- },
- isHttp: _this._stream.isHttp,
- rangeChunkSize: _this._rangeChunkSize,
- disableRange: _this._disableRange
- }),
- allowRangeRequests = _validateRangeRequest.allowRangeRequests,
- suggestedLength = _validateRangeRequest.suggestedLength;
- _this._contentLength = suggestedLength;
- _this._isRangeSupported = allowRangeRequests;
- if (!_this._isStreamingSupported && _this._isRangeSupported) {
- _this.cancel(new _util.AbortException('streaming is disabled'));
- }
- }).catch(this._headersCapability.reject);
- this.onProgress = null;
- }
- _createClass(PDFFetchStreamReader, [{
- key: 'read',
- value: function read() {
- var _this2 = this;
- return this._headersCapability.promise.then(function () {
- return _this2._reader.read().then(function (_ref) {
- var value = _ref.value,
- done = _ref.done;
- if (done) {
- return Promise.resolve({
- value: value,
- done: done
- });
- }
- _this2._loaded += value.byteLength;
- if (_this2.onProgress) {
- _this2.onProgress({
- loaded: _this2._loaded,
- total: _this2._contentLength
- });
- }
- var buffer = new Uint8Array(value).buffer;
- return Promise.resolve({
- value: buffer,
- done: false
- });
- });
- });
- }
- }, {
- key: 'cancel',
- value: function cancel(reason) {
- if (this._reader) {
- this._reader.cancel(reason);
- }
- }
- }, {
- key: 'headersReady',
- get: function get() {
- return this._headersCapability.promise;
- }
- }, {
- key: 'contentLength',
- get: function get() {
- return this._contentLength;
- }
- }, {
- key: 'isRangeSupported',
- get: function get() {
- return this._isRangeSupported;
- }
- }, {
- key: 'isStreamingSupported',
- get: function get() {
- return this._isStreamingSupported;
- }
- }]);
- return PDFFetchStreamReader;
- }();
- var PDFFetchStreamRangeReader = function () {
- function PDFFetchStreamRangeReader(stream, begin, end) {
- var _this3 = this;
- _classCallCheck(this, PDFFetchStreamRangeReader);
- this._stream = stream;
- this._reader = null;
- this._loaded = 0;
- var source = stream.source;
- this._withCredentials = source.withCredentials;
- this._readCapability = (0, _util.createPromiseCapability)();
- this._isStreamingSupported = !source.disableStream;
- this._headers = new Headers();
- for (var property in this._stream.httpHeaders) {
- var value = this._stream.httpHeaders[property];
- if (typeof value === 'undefined') {
- continue;
- }
- this._headers.append(property, value);
- }
- var rangeStr = begin + '-' + (end - 1);
- this._headers.append('Range', 'bytes=' + rangeStr);
- var url = source.url;
- fetch(url, createFetchOptions(this._headers, this._withCredentials)).then(function (response) {
- if (!(0, _network_utils.validateResponseStatus)(response.status)) {
- throw (0, _network_utils.createResponseStatusError)(response.status, url);
- }
- _this3._readCapability.resolve();
- _this3._reader = response.body.getReader();
- });
- this.onProgress = null;
- }
- _createClass(PDFFetchStreamRangeReader, [{
- key: 'read',
- value: function read() {
- var _this4 = this;
- return this._readCapability.promise.then(function () {
- return _this4._reader.read().then(function (_ref2) {
- var value = _ref2.value,
- done = _ref2.done;
- if (done) {
- return Promise.resolve({
- value: value,
- done: done
- });
- }
- _this4._loaded += value.byteLength;
- if (_this4.onProgress) {
- _this4.onProgress({ loaded: _this4._loaded });
- }
- var buffer = new Uint8Array(value).buffer;
- return Promise.resolve({
- value: buffer,
- done: false
- });
- });
- });
- }
- }, {
- key: 'cancel',
- value: function cancel(reason) {
- if (this._reader) {
- this._reader.cancel(reason);
- }
- }
- }, {
- key: 'isStreamingSupported',
- get: function get() {
- return this._isStreamingSupported;
- }
- }]);
- return PDFFetchStreamRangeReader;
- }();
- exports.PDFFetchStream = PDFFetchStream;
|