-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
151 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,13 @@ var assert = require("assert"); | |
var debug = require("./debug"); | ||
|
||
// Whether to use the native URL object or the legacy url module | ||
var hasNativeURL = typeof URL !== "undefined"; | ||
var useNativeURL = false; | ||
try { | ||
assert(new URL()); | ||
} | ||
catch (error) { | ||
useNativeURL = error.code === "ERR_INVALID_URL"; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
RubenVerborgh
Author
Collaborator
|
||
} | ||
|
||
// URL fields to preserve in copy operations | ||
var preservedUrlFields = [ | ||
|
@@ -493,27 +499,16 @@ function wrap(protocols) { | |
|
||
// Executes a request, following redirects | ||
function request(input, options, callback) { | ||
// Parse parameters | ||
if (isString(input)) { | ||
var parsed; | ||
try { | ||
parsed = spreadUrlObject(new URL(input)); | ||
} | ||
catch (err) { | ||
/* istanbul ignore next */ | ||
parsed = url.parse(input); | ||
} | ||
if (!isString(parsed.protocol)) { | ||
throw new InvalidUrlError({ input }); | ||
} | ||
input = parsed; | ||
} | ||
else if (hasNativeURL && (input instanceof URL)) { | ||
// Parse parameters, ensuring that input is an object | ||
if (isURL(input)) { | ||
input = spreadUrlObject(input); | ||
} | ||
else if (isString(input)) { | ||
input = spreadUrlObject(parseUrl(input)); | ||
} | ||
else { | ||
callback = options; | ||
options = input; | ||
options = validateUrl(input); | ||
input = { protocol: protocol }; | ||
} | ||
if (isFunction(options)) { | ||
|
@@ -554,14 +549,35 @@ function wrap(protocols) { | |
|
||
function noop() { /* empty */ } | ||
|
||
function parseUrl(string) { | ||
/* istanbul ignore next */ | ||
return hasNativeURL ? new URL(string) : url.parse(string); | ||
function parseUrl(input) { | ||
var parsed; | ||
/* istanbul ignore else */ | ||
if (useNativeURL) { | ||
parsed = new URL(input); | ||
} | ||
else { | ||
// Ensure the URL is valid and absolute | ||
parsed = validateUrl(url.parse(input)); | ||
if (!isString(parsed.protocol)) { | ||
throw new InvalidUrlError({ input }); | ||
} | ||
} | ||
return parsed; | ||
} | ||
|
||
function resolveUrl(relative, base) { | ||
/* istanbul ignore next */ | ||
return hasNativeURL ? new URL(relative, base) : parseUrl(url.resolve(base, relative)); | ||
return useNativeURL ? new URL(relative, base) : parseUrl(url.resolve(base, relative)); | ||
} | ||
|
||
function validateUrl(input) { | ||
if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) { | ||
throw new InvalidUrlError({ input: input.href || input }); | ||
} | ||
if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) { | ||
throw new InvalidUrlError({ input: input.href || input }); | ||
} | ||
return input; | ||
} | ||
|
||
function spreadUrlObject(urlObject, target) { | ||
|
@@ -646,6 +662,10 @@ function isBuffer(value) { | |
return typeof value === "object" && ("length" in value); | ||
} | ||
|
||
function isURL(value) { | ||
return URL && value instanceof URL; | ||
} | ||
|
||
// Exports | ||
module.exports = wrap({ http: http, https: https }); | ||
module.exports.wrap = wrap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RubenVerborgh Hi there, just resurfacing this diff.
useNativeURL
appears to always be false.Since
ERR_MISSING_ARGS
!=ERR_INVALID_URL
, that meansuseNativeURL
is always false in Node.js. We discovered this bug while testing Node.js compatiblity with Bun.Did you mean to use
new URL("")
instead? That appears to show the correct error code.