Skip to content

Commit

Permalink
Fix JSDOM.fromURL() bugs in the browser build
Browse files Browse the repository at this point in the history
Extracted from #3393.

Co-authored-by: LungZeno <lungzenoopen@gmail.com>
  • Loading branch information
domenic and LungZeno committed Jan 7, 2023
1 parent 77f530d commit 0c5aea7
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 165 deletions.
2 changes: 1 addition & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class JSDOM {
options = Object.assign(options, {
url: req.href + originalHash,
contentType: res.headers["content-type"],
referrer: req.getHeader("referer")
referrer: req.getHeader("referer") ?? undefined
});

return new JSDOM(body, options);
Expand Down
21 changes: 17 additions & 4 deletions lib/jsdom/living/helpers/http-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,21 @@ module.exports = class Request extends Writable {
_performRequest() {
const urlOptions = new URL(this.currentURL);
const scheme = urlOptions.protocol;
this._requestOptions.agent = this.agents[scheme.substring(0, scheme.length - 1)];

// browserify's (http|https).request() does not work correctly with the (url, options, callback) signature. Instead
// we need to have options for each of the URL components. Note that we can't use the spread operator because URL
// instances don't have own properties for the URL components.
const requestOptions = {
...this._requestOptions,
agent: this.agents[scheme.substring(0, scheme.length - 1)],
protocol: urlOptions.protocol,
hostname: urlOptions.hostname,
port: urlOptions.port,
path: urlOptions.pathname + urlOptions.search
};

const { request } = scheme === "https:" ? https : http;
this._currentRequest = request(this.currentURL, this._requestOptions, response => {
this._currentRequest = request(requestOptions, response => {
this._processResponse(response);
});

Expand Down Expand Up @@ -211,9 +223,10 @@ module.exports = class Request extends Writable {
statusCode !== 204 &&
statusCode !== 304
) {
// Browserify's zlib does not support zlib.constants.
const zlibOptions = {
flush: zlib.constants.Z_SYNC_FLUSH,
finishFlush: zlib.constants.Z_SYNC_FLUSH
flush: (zlib.constants ?? zlib).Z_SYNC_FLUSH,
finishFlush: (zlib.constants ?? zlib).Z_SYNC_FLUSH
};
const contentEncoding = (response.headers["content-encoding"] || "identity").trim().toLowerCase();
if (contentEncoding === "gzip") {
Expand Down
Loading

0 comments on commit 0c5aea7

Please sign in to comment.