Skip to content

Commit 949c3e9

Browse files
committed
Add abort functionality in fetch stream
1 parent c5c06bf commit 949c3e9

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/display/fetch_stream.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import {
2121
validateRangeRequestCapabilities, validateResponseStatus
2222
} from './network_utils';
2323

24-
function createFetchOptions(headers, withCredentials) {
24+
function createFetchOptions(headers, withCredentials, abortController) {
2525
return {
2626
method: 'GET',
2727
headers,
28+
signal: abortController && abortController.signal,
2829
mode: 'cors',
2930
credentials: withCredentials ? 'include' : 'same-origin',
3031
redirect: 'follow',
@@ -80,6 +81,9 @@ class PDFFetchStreamReader {
8081
this._disableRange = true;
8182
}
8283

84+
if (typeof AbortController !== 'undefined') {
85+
this._abortController = new AbortController();
86+
}
8387
this._isStreamingSupported = !source.disableStream;
8488
this._isRangeSupported = !source.disableRange;
8589

@@ -93,8 +97,8 @@ class PDFFetchStreamReader {
9397
}
9498

9599
let url = source.url;
96-
fetch(url, createFetchOptions(this._headers, this._withCredentials)).
97-
then((response) => {
100+
fetch(url, createFetchOptions(this._headers, this._withCredentials,
101+
this._abortController)).then((response) => {
98102
if (!validateResponseStatus(response.status)) {
99103
throw createResponseStatusError(response.status, url);
100104
}
@@ -171,6 +175,9 @@ class PDFFetchStreamReader {
171175
if (this._reader) {
172176
this._reader.cancel(reason);
173177
}
178+
if (this._abortController) {
179+
this._abortController.abort();
180+
}
174181
}
175182
}
176183

@@ -184,6 +191,10 @@ class PDFFetchStreamRangeReader {
184191
this._readCapability = createPromiseCapability();
185192
this._isStreamingSupported = !source.disableStream;
186193

194+
if (typeof AbortController !== 'undefined') {
195+
this._abortController = new AbortController();
196+
}
197+
187198
this._headers = new Headers();
188199
for (let property in this._stream.httpHeaders) {
189200
let value = this._stream.httpHeaders[property];
@@ -196,8 +207,8 @@ class PDFFetchStreamRangeReader {
196207
let rangeStr = begin + '-' + (end - 1);
197208
this._headers.append('Range', 'bytes=' + rangeStr);
198209
let url = source.url;
199-
fetch(url, createFetchOptions(this._headers, this._withCredentials)).
200-
then((response) => {
210+
fetch(url, createFetchOptions(this._headers, this._withCredentials,
211+
this._abortController)).then((response) => {
201212
if (!validateResponseStatus(response.status)) {
202213
throw createResponseStatusError(response.status, url);
203214
}
@@ -232,6 +243,9 @@ class PDFFetchStreamRangeReader {
232243
if (this._reader) {
233244
this._reader.cancel(reason);
234245
}
246+
if (this._abortController) {
247+
this._abortController.abort();
248+
}
235249
}
236250
}
237251

0 commit comments

Comments
 (0)