diff --git a/src/display/fetch_stream.js b/src/display/fetch_stream.js index 7dcd3e537..f70ed1d54 100644 --- a/src/display/fetch_stream.js +++ b/src/display/fetch_stream.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint no-var: error */ import { AbortException, assert, createPromiseCapability @@ -32,6 +33,7 @@ function createFetchOptions(headers, withCredentials, abortController) { }; } +/** @implements {IPDFStream} */ class PDFFetchStream { constructor(source) { this.source = source; @@ -56,7 +58,7 @@ class PDFFetchStream { if (end <= this._progressiveDataLength) { return null; } - let reader = new PDFFetchStreamRangeReader(this, begin, end); + const reader = new PDFFetchStreamRangeReader(this, begin, end); this._rangeRequestReaders.push(reader); return reader; } @@ -65,21 +67,22 @@ class PDFFetchStream { if (this._fullRequestReader) { this._fullRequestReader.cancel(reason); } - let readers = this._rangeRequestReaders.slice(0); + const readers = this._rangeRequestReaders.slice(0); readers.forEach(function(reader) { reader.cancel(reason); }); } } +/** @implements {IPDFStreamReader} */ class PDFFetchStreamReader { constructor(stream) { this._stream = stream; this._reader = null; this._loaded = 0; this._filename = null; - let source = stream.source; - this._withCredentials = source.withCredentials; + const source = stream.source; + this._withCredentials = source.withCredentials || false; this._contentLength = source.length; this._headersCapability = createPromiseCapability(); this._disableRange = source.disableRange || false; @@ -95,15 +98,15 @@ class PDFFetchStreamReader { this._isRangeSupported = !source.disableRange; this._headers = new Headers(); - for (let property in this._stream.httpHeaders) { - let value = this._stream.httpHeaders[property]; + for (const property in this._stream.httpHeaders) { + const value = this._stream.httpHeaders[property]; if (typeof value === 'undefined') { continue; } this._headers.append(property, value); } - let url = source.url; + const url = source.url; fetch(url, createFetchOptions(this._headers, this._withCredentials, this._abortController)).then((response) => { if (!validateResponseStatus(response.status)) { @@ -115,7 +118,7 @@ class PDFFetchStreamReader { const getResponseHeader = (name) => { return response.headers.get(name); }; - let { allowRangeRequests, suggestedLength, } = + const { allowRangeRequests, suggestedLength, } = validateRangeRequestCapabilities({ getResponseHeader, isHttp: this._stream.isHttp, @@ -132,7 +135,7 @@ class PDFFetchStreamReader { // 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')); + this.cancel(new AbortException('Streaming is disabled.')); } }).catch(this._headersCapability.reject); @@ -172,7 +175,7 @@ class PDFFetchStreamReader { total: this._contentLength, }); } - let buffer = new Uint8Array(value).buffer; + const buffer = new Uint8Array(value).buffer; return { value: buffer, done: false, }; } @@ -186,13 +189,14 @@ class PDFFetchStreamReader { } } +/** @implements {IPDFStreamRangeReader} */ class PDFFetchStreamRangeReader { constructor(stream, begin, end) { this._stream = stream; this._reader = null; this._loaded = 0; - let source = stream.source; - this._withCredentials = source.withCredentials; + const source = stream.source; + this._withCredentials = source.withCredentials || false; this._readCapability = createPromiseCapability(); this._isStreamingSupported = !source.disableStream; @@ -201,17 +205,16 @@ class PDFFetchStreamRangeReader { } this._headers = new Headers(); - for (let property in this._stream.httpHeaders) { - let value = this._stream.httpHeaders[property]; + for (const property in this._stream.httpHeaders) { + const value = this._stream.httpHeaders[property]; if (typeof value === 'undefined') { continue; } this._headers.append(property, value); } + this._headers.append('Range', `bytes=${begin}-${end - 1}`); - let rangeStr = begin + '-' + (end - 1); - this._headers.append('Range', 'bytes=' + rangeStr); - let url = source.url; + const url = source.url; fetch(url, createFetchOptions(this._headers, this._withCredentials, this._abortController)).then((response) => { if (!validateResponseStatus(response.status)) { @@ -238,7 +241,7 @@ class PDFFetchStreamRangeReader { if (this.onProgress) { this.onProgress({ loaded: this._loaded, }); } - let buffer = new Uint8Array(value).buffer; + const buffer = new Uint8Array(value).buffer; return { value: buffer, done: false, }; }