From d40d33682bc1f903f029a4b9be58ecd85b1054d1 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Sun, 22 Dec 2019 22:04:25 +0100 Subject: [PATCH] Extract & use createHeaders helper in src/display/fetch_stream.js --- src/display/fetch_stream.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/display/fetch_stream.js b/src/display/fetch_stream.js index f70ed1d54..4d9cc52a1 100644 --- a/src/display/fetch_stream.js +++ b/src/display/fetch_stream.js @@ -33,6 +33,18 @@ function createFetchOptions(headers, withCredentials, abortController) { }; } +function createHeaders(httpHeaders) { + const headers = new Headers(); + for (const property in httpHeaders) { + const value = httpHeaders[property]; + if (typeof value === 'undefined') { + continue; + } + headers.append(property, value); + } + return headers; +} + /** @implements {IPDFStream} */ class PDFFetchStream { constructor(source) { @@ -97,14 +109,7 @@ class PDFFetchStreamReader { this._isStreamingSupported = !source.disableStream; this._isRangeSupported = !source.disableRange; - this._headers = new Headers(); - 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 = createHeaders(this._stream.httpHeaders); const url = source.url; fetch(url, createFetchOptions(this._headers, this._withCredentials, @@ -204,14 +209,7 @@ class PDFFetchStreamRangeReader { this._abortController = new AbortController(); } - this._headers = new Headers(); - 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 = createHeaders(this._stream.httpHeaders); this._headers.append('Range', `bytes=${begin}-${end - 1}`); const url = source.url;