Merge pull request #9701 from mukulmishra18/fetch-abort

Add abort functionality in fetch stream
This commit is contained in:
Rob Wu 2018-05-22 15:11:49 +02:00 committed by GitHub
commit 28360a0142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,10 +21,11 @@ import {
validateRangeRequestCapabilities, validateResponseStatus validateRangeRequestCapabilities, validateResponseStatus
} from './network_utils'; } from './network_utils';
function createFetchOptions(headers, withCredentials) { function createFetchOptions(headers, withCredentials, abortController) {
return { return {
method: 'GET', method: 'GET',
headers, headers,
signal: abortController && abortController.signal,
mode: 'cors', mode: 'cors',
credentials: withCredentials ? 'include' : 'same-origin', credentials: withCredentials ? 'include' : 'same-origin',
redirect: 'follow', redirect: 'follow',
@ -80,6 +81,9 @@ class PDFFetchStreamReader {
this._disableRange = true; this._disableRange = true;
} }
if (typeof AbortController !== 'undefined') {
this._abortController = new AbortController();
}
this._isStreamingSupported = !source.disableStream; this._isStreamingSupported = !source.disableStream;
this._isRangeSupported = !source.disableRange; this._isRangeSupported = !source.disableRange;
@ -93,8 +97,8 @@ class PDFFetchStreamReader {
} }
let url = source.url; let url = source.url;
fetch(url, createFetchOptions(this._headers, this._withCredentials)). fetch(url, createFetchOptions(this._headers, this._withCredentials,
then((response) => { this._abortController)).then((response) => {
if (!validateResponseStatus(response.status)) { if (!validateResponseStatus(response.status)) {
throw createResponseStatusError(response.status, url); throw createResponseStatusError(response.status, url);
} }
@ -171,6 +175,9 @@ class PDFFetchStreamReader {
if (this._reader) { if (this._reader) {
this._reader.cancel(reason); this._reader.cancel(reason);
} }
if (this._abortController) {
this._abortController.abort();
}
} }
} }
@ -184,6 +191,10 @@ class PDFFetchStreamRangeReader {
this._readCapability = createPromiseCapability(); this._readCapability = createPromiseCapability();
this._isStreamingSupported = !source.disableStream; this._isStreamingSupported = !source.disableStream;
if (typeof AbortController !== 'undefined') {
this._abortController = new AbortController();
}
this._headers = new Headers(); this._headers = new Headers();
for (let property in this._stream.httpHeaders) { for (let property in this._stream.httpHeaders) {
let value = this._stream.httpHeaders[property]; let value = this._stream.httpHeaders[property];
@ -196,8 +207,8 @@ class PDFFetchStreamRangeReader {
let rangeStr = begin + '-' + (end - 1); let rangeStr = begin + '-' + (end - 1);
this._headers.append('Range', 'bytes=' + rangeStr); this._headers.append('Range', 'bytes=' + rangeStr);
let url = source.url; let url = source.url;
fetch(url, createFetchOptions(this._headers, this._withCredentials)). fetch(url, createFetchOptions(this._headers, this._withCredentials,
then((response) => { this._abortController)).then((response) => {
if (!validateResponseStatus(response.status)) { if (!validateResponseStatus(response.status)) {
throw createResponseStatusError(response.status, url); throw createResponseStatusError(response.status, url);
} }
@ -232,6 +243,9 @@ class PDFFetchStreamRangeReader {
if (this._reader) { if (this._reader) {
this._reader.cancel(reason); this._reader.cancel(reason);
} }
if (this._abortController) {
this._abortController.abort();
}
} }
} }