Skip to content

Commit

Permalink
Merge pull request #18665 from Snuffleupagus/PDFNodeStream-createRequest
Browse files Browse the repository at this point in the history
Add a helper function for http/https requests in `src/display/node_stream.js`
  • Loading branch information
timvandermeij authored Sep 1, 2024
2 parents 2a68aa6 + a94e8ba commit 4a13222
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ function parseUrlOrPath(sourceUrl) {
return new URL(url.pathToFileURL(sourceUrl));
}

function createRequest(url, headers, callback) {
if (url.protocol === "http:") {
const http = NodePackages.get("http");
return http.request(url, { headers }, callback);
}
const https = NodePackages.get("https");
return https.request(url, { headers }, callback);
}

class PDFNodeStream {
constructor(source) {
this.source = source;
Expand Down Expand Up @@ -312,22 +321,11 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._filename = extractFilenameFromHeader(getResponseHeader);
};

this._request = null;
if (this._url.protocol === "http:") {
const http = NodePackages.get("http");
this._request = http.request(
this._url,
{ headers: stream.httpHeaders },
handleResponse
);
} else {
const https = NodePackages.get("https");
this._request = https.request(
this._url,
{ headers: stream.httpHeaders },
handleResponse
);
}
this._request = createRequest(
this._url,
stream.httpHeaders,
handleResponse
);

this._request.on("error", reason => {
this._storedError = reason;
Expand Down Expand Up @@ -363,22 +361,7 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
this._setReadableStream(response);
};

this._request = null;
if (this._url.protocol === "http:") {
const http = NodePackages.get("http");
this._request = http.request(
this._url,
{ headers: this._httpHeaders },
handleResponse
);
} else {
const https = NodePackages.get("https");
this._request = https.request(
this._url,
{ headers: this._httpHeaders },
handleResponse
);
}
this._request = createRequest(this._url, this._httpHeaders, handleResponse);

this._request.on("error", reason => {
this._storedError = reason;
Expand Down

0 comments on commit 4a13222

Please sign in to comment.