2017-08-10 16:30:54 +09:00
|
|
|
/* Copyright 2012 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-31 00:53:38 +09:00
|
|
|
import {
|
|
|
|
AbortException, assert, createPromiseCapability
|
|
|
|
} from '../shared/util';
|
2017-08-10 16:30:54 +09:00
|
|
|
import {
|
2018-01-13 17:01:50 +09:00
|
|
|
createResponseStatusError, extractFilenameFromHeader,
|
|
|
|
validateRangeRequestCapabilities, validateResponseStatus
|
2017-08-10 16:30:54 +09:00
|
|
|
} from './network_utils';
|
|
|
|
|
2018-05-04 05:28:34 +09:00
|
|
|
function createFetchOptions(headers, withCredentials, abortController) {
|
2017-08-10 16:30:54 +09:00
|
|
|
return {
|
|
|
|
method: 'GET',
|
|
|
|
headers,
|
2018-05-04 05:28:34 +09:00
|
|
|
signal: abortController && abortController.signal,
|
2017-08-10 16:30:54 +09:00
|
|
|
mode: 'cors',
|
2017-09-15 03:23:06 +09:00
|
|
|
credentials: withCredentials ? 'include' : 'same-origin',
|
2017-08-10 16:30:54 +09:00
|
|
|
redirect: 'follow',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class PDFFetchStream {
|
2017-10-16 22:52:20 +09:00
|
|
|
constructor(source) {
|
|
|
|
this.source = source;
|
|
|
|
this.isHttp = /^https?:/i.test(source.url);
|
|
|
|
this.httpHeaders = (this.isHttp && source.httpHeaders) || {};
|
2017-08-10 16:30:54 +09:00
|
|
|
|
|
|
|
this._fullRequestReader = null;
|
|
|
|
this._rangeRequestReaders = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getFullReader() {
|
|
|
|
assert(!this._fullRequestReader);
|
|
|
|
this._fullRequestReader = new PDFFetchStreamReader(this);
|
|
|
|
return this._fullRequestReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRangeReader(begin, end) {
|
|
|
|
let reader = new PDFFetchStreamRangeReader(this, begin, end);
|
|
|
|
this._rangeRequestReaders.push(reader);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelAllRequests(reason) {
|
|
|
|
if (this._fullRequestReader) {
|
|
|
|
this._fullRequestReader.cancel(reason);
|
|
|
|
}
|
|
|
|
let readers = this._rangeRequestReaders.slice(0);
|
|
|
|
readers.forEach(function(reader) {
|
|
|
|
reader.cancel(reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PDFFetchStreamReader {
|
|
|
|
constructor(stream) {
|
|
|
|
this._stream = stream;
|
|
|
|
this._reader = null;
|
|
|
|
this._loaded = 0;
|
2018-01-17 00:24:36 +09:00
|
|
|
this._filename = null;
|
2017-10-16 22:52:20 +09:00
|
|
|
let source = stream.source;
|
|
|
|
this._withCredentials = source.withCredentials;
|
|
|
|
this._contentLength = source.length;
|
2017-08-10 16:30:54 +09:00
|
|
|
this._headersCapability = createPromiseCapability();
|
2018-02-09 21:39:38 +09:00
|
|
|
this._disableRange = source.disableRange || false;
|
2017-10-16 22:52:20 +09:00
|
|
|
this._rangeChunkSize = source.rangeChunkSize;
|
2017-08-10 16:30:54 +09:00
|
|
|
if (!this._rangeChunkSize && !this._disableRange) {
|
|
|
|
this._disableRange = true;
|
|
|
|
}
|
|
|
|
|
2018-05-04 05:28:34 +09:00
|
|
|
if (typeof AbortController !== 'undefined') {
|
|
|
|
this._abortController = new AbortController();
|
|
|
|
}
|
2017-10-16 22:52:20 +09:00
|
|
|
this._isStreamingSupported = !source.disableStream;
|
2018-02-09 21:39:38 +09:00
|
|
|
this._isRangeSupported = !source.disableRange;
|
2017-08-10 16:30:54 +09:00
|
|
|
|
|
|
|
this._headers = new Headers();
|
|
|
|
for (let property in this._stream.httpHeaders) {
|
|
|
|
let value = this._stream.httpHeaders[property];
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this._headers.append(property, value);
|
|
|
|
}
|
|
|
|
|
2017-10-16 22:52:20 +09:00
|
|
|
let url = source.url;
|
2018-05-04 05:28:34 +09:00
|
|
|
fetch(url, createFetchOptions(this._headers, this._withCredentials,
|
|
|
|
this._abortController)).then((response) => {
|
2017-09-02 18:57:15 +09:00
|
|
|
if (!validateResponseStatus(response.status)) {
|
2017-08-10 16:30:54 +09:00
|
|
|
throw createResponseStatusError(response.status, url);
|
|
|
|
}
|
|
|
|
this._reader = response.body.getReader();
|
2017-08-31 00:53:38 +09:00
|
|
|
this._headersCapability.resolve();
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2018-01-13 17:01:50 +09:00
|
|
|
const getResponseHeader = (name) => {
|
|
|
|
return response.headers.get(name);
|
|
|
|
};
|
2017-08-10 16:30:54 +09:00
|
|
|
let { allowRangeRequests, suggestedLength, } =
|
|
|
|
validateRangeRequestCapabilities({
|
2018-01-13 17:01:50 +09:00
|
|
|
getResponseHeader,
|
2017-08-10 16:30:54 +09:00
|
|
|
isHttp: this._stream.isHttp,
|
|
|
|
rangeChunkSize: this._rangeChunkSize,
|
|
|
|
disableRange: this._disableRange,
|
|
|
|
});
|
|
|
|
|
|
|
|
this._isRangeSupported = allowRangeRequests;
|
2018-02-09 21:39:38 +09:00
|
|
|
// Setting right content length.
|
|
|
|
this._contentLength = suggestedLength || this._contentLength;
|
2018-01-17 00:24:36 +09:00
|
|
|
|
|
|
|
this._filename = extractFilenameFromHeader(getResponseHeader);
|
2017-08-31 00:53:38 +09:00
|
|
|
|
|
|
|
// We need to stop reading when range is supported and streaming is
|
|
|
|
// disabled.
|
|
|
|
if (!this._isStreamingSupported && this._isRangeSupported) {
|
|
|
|
this.cancel(new AbortException('streaming is disabled'));
|
|
|
|
}
|
2017-08-10 16:30:54 +09:00
|
|
|
}).catch(this._headersCapability.reject);
|
|
|
|
|
|
|
|
this.onProgress = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get headersReady() {
|
|
|
|
return this._headersCapability.promise;
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:24:36 +09:00
|
|
|
get filename() {
|
|
|
|
return this._filename;
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
|
2018-01-17 00:24:36 +09:00
|
|
|
get contentLength() {
|
|
|
|
return this._contentLength;
|
2018-01-13 17:01:50 +09:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:54 +09:00
|
|
|
get isRangeSupported() {
|
|
|
|
return this._isRangeSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isStreamingSupported() {
|
|
|
|
return this._isStreamingSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
read() {
|
|
|
|
return this._headersCapability.promise.then(() => {
|
|
|
|
return this._reader.read().then(({ value, done, }) => {
|
|
|
|
if (done) {
|
|
|
|
return Promise.resolve({ value, done, });
|
|
|
|
}
|
|
|
|
this._loaded += value.byteLength;
|
|
|
|
if (this.onProgress) {
|
|
|
|
this.onProgress({
|
|
|
|
loaded: this._loaded,
|
|
|
|
total: this._contentLength,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let buffer = new Uint8Array(value).buffer;
|
|
|
|
return Promise.resolve({ value: buffer, done: false, });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(reason) {
|
|
|
|
if (this._reader) {
|
|
|
|
this._reader.cancel(reason);
|
|
|
|
}
|
2018-05-04 05:28:34 +09:00
|
|
|
if (this._abortController) {
|
|
|
|
this._abortController.abort();
|
|
|
|
}
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PDFFetchStreamRangeReader {
|
|
|
|
constructor(stream, begin, end) {
|
|
|
|
this._stream = stream;
|
|
|
|
this._reader = null;
|
|
|
|
this._loaded = 0;
|
2017-10-16 22:52:20 +09:00
|
|
|
let source = stream.source;
|
|
|
|
this._withCredentials = source.withCredentials;
|
2017-08-10 16:30:54 +09:00
|
|
|
this._readCapability = createPromiseCapability();
|
2017-10-16 22:52:20 +09:00
|
|
|
this._isStreamingSupported = !source.disableStream;
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2018-05-04 05:28:34 +09:00
|
|
|
if (typeof AbortController !== 'undefined') {
|
|
|
|
this._abortController = new AbortController();
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:54 +09:00
|
|
|
this._headers = new Headers();
|
|
|
|
for (let property in this._stream.httpHeaders) {
|
|
|
|
let value = this._stream.httpHeaders[property];
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this._headers.append(property, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let rangeStr = begin + '-' + (end - 1);
|
|
|
|
this._headers.append('Range', 'bytes=' + rangeStr);
|
2017-10-16 22:52:20 +09:00
|
|
|
let url = source.url;
|
2018-05-04 05:28:34 +09:00
|
|
|
fetch(url, createFetchOptions(this._headers, this._withCredentials,
|
|
|
|
this._abortController)).then((response) => {
|
2017-09-02 18:57:15 +09:00
|
|
|
if (!validateResponseStatus(response.status)) {
|
2017-08-10 16:30:54 +09:00
|
|
|
throw createResponseStatusError(response.status, url);
|
|
|
|
}
|
|
|
|
this._readCapability.resolve();
|
|
|
|
this._reader = response.body.getReader();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.onProgress = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isStreamingSupported() {
|
|
|
|
return this._isStreamingSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
read() {
|
|
|
|
return this._readCapability.promise.then(() => {
|
|
|
|
return this._reader.read().then(({ value, done, }) => {
|
|
|
|
if (done) {
|
|
|
|
return Promise.resolve({ value, done, });
|
|
|
|
}
|
|
|
|
this._loaded += value.byteLength;
|
|
|
|
if (this.onProgress) {
|
|
|
|
this.onProgress({ loaded: this._loaded, });
|
|
|
|
}
|
|
|
|
let buffer = new Uint8Array(value).buffer;
|
|
|
|
return Promise.resolve({ value: buffer, done: false, });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(reason) {
|
|
|
|
if (this._reader) {
|
|
|
|
this._reader.cancel(reason);
|
|
|
|
}
|
2018-05-04 05:28:34 +09:00
|
|
|
if (this._abortController) {
|
|
|
|
this._abortController.abort();
|
|
|
|
}
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
PDFFetchStream,
|
|
|
|
};
|