Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Fixing the support of WHATWG URLs
Browse files Browse the repository at this point in the history
This is an attempt to support WHATWG URLs.
Feedback is appreciated!

Fixes #26
  • Loading branch information
sadasant committed Jul 6, 2019
1 parent 560f111 commit ed48b77
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ function isAgent(v) {
return v && typeof v.addRequest === 'function';
}

function urlInstanceToObject(url) {
let urlObject = {};
// If the opts are genearted with the WHATWG API
if (url && Object.getOwnPropertySymbols(url)[0]) {
let urlContext = url[Object.getOwnPropertySymbols(url)[0]];
for (let key of Object.keys(urlContext)) {
urlObject[key] = urlContext[key];
}
} else {
urlObject = Object.assign({}, url);
}
return urlObject;
}

/**
* Base `http.Agent` implementation.
* No pooling/keep-alive is implemented by default.
Expand All @@ -18,6 +32,8 @@ function isAgent(v) {
* @api public
*/
function Agent(callback, _opts) {
_opts = urlInstanceToObject(_opts);

if (!(this instanceof Agent)) {
return new Agent(callback, _opts);
}
Expand Down Expand Up @@ -58,7 +74,7 @@ Agent.prototype.callback = function callback(req, opts) {
* @api public
*/
Agent.prototype.addRequest = function addRequest(req, _opts) {
const ownOpts = Object.assign({}, _opts);
let ownOpts = urlInstanceToObject(_opts);

// Set default `host` for HTTP to localhost
if (null == ownOpts.host) {
Expand Down
63 changes: 53 additions & 10 deletions patch-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,35 @@ const https = require('https');
const patchMarker = "__agent_base_https_request_patched__";
if (!https.request[patchMarker]) {
https.request = (function(request) {
return function(_options, cb) {
return function(_url, _options, cb) {
// Support for both
// https.request(options[, callback])
// https.request(url[, options][, callback])
if (typeof _url !== 'string' && !cb) {
cb = _options;
_options = _url;
_url = undefined;
}
if (!_url && _options) {
_url = url.format(_options);
}
let options;
if (typeof _options === 'string') {
options = url.parse(_options);
} else {
options = Object.assign({}, _options);
options = url.parse(_url);
if (_options) {
if (_options && Object.getOwnPropertySymbols(_options)[0]) {
let urlContext = _options[Object.getOwnPropertySymbols(_options)[0]];
for (let key of Object.keys(urlContext)) {
options[key] = urlContext[key];
}
} else {
for (let key of Object.keys(_options)) {
options[key] = _options[key];
}
}
}
}
if (null == options.port) {
options.port = 443;
Expand All @@ -35,15 +58,35 @@ if (!https.request[patchMarker]) {
* Ref: https://github.com/nodejs/node/commit/5118f31
*/
https.get = function (_url, _options, cb) {
let options;
if (typeof _url === 'string' && _options && typeof _options !== 'function') {
options = Object.assign({}, url.parse(_url), _options);
} else if (!_options && !cb) {
options = _url;
} else if (!cb) {
options = _url;
cb = _options;
// Support for both
// https.get(options[, callback])
// https.get(url[, options][, callback])
if (typeof _url !== 'string' && !cb) {
cb = _options;
_options = _url;
_url = undefined;
}
if (!_url && _options) {
_url = url.format(_options);
}
let options;
if (typeof _options === 'string') {
options = url.parse(_options);
} else {
options = url.parse(_url);
if (_options) {
if (_options && Object.getOwnPropertySymbols(_options)[0]) {
let urlContext = _options[Object.getOwnPropertySymbols(_options)[0]];
for (let key of Object.keys(urlContext)) {
options[key] = urlContext[key];
}
} else {
for (let key of Object.keys(_options)) {
options[key] = _options[key];
}
}
}
}

const req = https.request(options, cb);
req.end();
Expand Down

0 comments on commit ed48b77

Please sign in to comment.