-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
url: expose urlToOptions utility #35960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1029,6 +1029,50 @@ new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c | |
pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) | ||
``` | ||
|
||
### `url.urlToHttpOptions(url)` | ||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
* `url` {URL} The [WHATWG URL][] object to convert to an options object. | ||
* Returns: {Object} Options object | ||
* `protocol` {string} Protocol to use. | ||
* `hostname` {string} A domain name or IP address of the server to issue the | ||
request to. | ||
* `hash` {string} The fragment portion of the URL. | ||
* `search` {string} The serialized query portion of the URL. | ||
* `pathname` {string} The path portion of the URL. | ||
* `path` {string} Request path. Should include query string if any. | ||
E.G. `'/index.html?page=12'`. An exception is thrown when the request path | ||
contains illegal characters. Currently, only spaces are rejected but that | ||
may change in the future. | ||
* `href` {string} The serialized URL. | ||
* `port` {number} Port of remote server. | ||
* `auth` {string} Basic authentication i.e. `'user:password'` to compute an | ||
Authorization header. | ||
|
||
This utility function converts a URL object into an ordinary options object as | ||
expected by the [`http.request()`][] and [`https.request()`][] APIs. | ||
|
||
```js | ||
const { urlToHttpOptions } = require('url'); | ||
const myURL = new URL('https://a:b@測試?abc#foo'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for intruding here - but just out of curiosity: what's > new URL('https://a:b@測試?abc#foo')
URL {
href: 'https://a:b@xn--g6w251d/?abc#foo',
origin: 'https://xn--g6w251d',
protocol: 'https:',
username: 'a',
password: 'b',
host: 'xn--g6w251d',
hostname: 'xn--g6w251d',
port: '',
pathname: '/',
search: '?abc',
searchParams: URLSearchParams { 'abc' => '' },
hash: '#foo'
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification 👍🏼. Exactly - that's why I was saying "almost 1:1". I mean, function urlToHttpOptions(url) {
return {
...url,
auth: `${url.username}:${url.password}`.
port: url.port === '' ? undefined : Number(url.port),
path: `${url.pathname ?? ''}${url.search ?? ''}`
}
} Not 100%, but that's pretty much it, right? EDIT |
||
|
||
console.log(urlToHttpOptions(myUrl)); | ||
/** | ||
{ | ||
protocol: 'https:', | ||
hostname: 'xn--g6w251d', | ||
hash: '#foo', | ||
search: '?abc', | ||
pathname: '/', | ||
path: '/?abc', | ||
href: 'https://a:b@xn--g6w251d/?abc#foo', | ||
auth: 'a:b' | ||
} | ||
*/ | ||
``` | ||
|
||
## Legacy URL API | ||
<!-- YAML | ||
deprecated: v11.0.0 | ||
|
@@ -1388,6 +1432,8 @@ console.log(myURL.origin); | |
[`TypeError`]: errors.md#errors_class_typeerror | ||
[`URLSearchParams`]: #url_class_urlsearchparams | ||
[`array.toString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString | ||
[`http.request()`]: http.md#http_http_request_options_callback | ||
[`https.request()`]: https.md#https_https_request_options_callback | ||
[`new URL()`]: #url_new_url_input_base | ||
[`querystring`]: querystring.md | ||
[`require('url').format()`]: #url_url_format_url_options | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { urlToHttpOptions } = require('url'); | ||
|
||
// Test urlToHttpOptions | ||
const urlObj = new URL('http://user:[email protected]:21/aaa/zzz?l=24#test'); | ||
const opts = urlToHttpOptions(urlObj); | ||
assert.strictEqual(opts instanceof URL, false); | ||
assert.strictEqual(opts.protocol, 'http:'); | ||
assert.strictEqual(opts.auth, 'user:pass'); | ||
assert.strictEqual(opts.hostname, 'foo.bar.com'); | ||
assert.strictEqual(opts.port, 21); | ||
assert.strictEqual(opts.path, '/aaa/zzz?l=24'); | ||
assert.strictEqual(opts.pathname, '/aaa/zzz'); | ||
assert.strictEqual(opts.search, '?l=24'); | ||
assert.strictEqual(opts.hash, '#test'); | ||
|
||
const { hostname } = urlToHttpOptions(new URL('http://[::1]:21')); | ||
assert.strictEqual(hostname, '::1'); | ||
|
||
// If a WHATWG URL object is copied, it is possible that the resulting copy | ||
// contains the Symbols that Node uses for brand checking, but not the data | ||
// properties, which are getters. Verify that urlToHttpOptions() can handle | ||
// such a case. | ||
const copiedUrlObj = { ...urlObj }; | ||
const copiedOpts = urlToHttpOptions(copiedUrlObj); | ||
assert.strictEqual(copiedOpts instanceof URL, false); | ||
assert.strictEqual(copiedOpts.protocol, undefined); | ||
assert.strictEqual(copiedOpts.auth, undefined); | ||
assert.strictEqual(copiedOpts.hostname, undefined); | ||
assert.strictEqual(copiedOpts.port, NaN); | ||
assert.strictEqual(copiedOpts.path, ''); | ||
assert.strictEqual(copiedOpts.pathname, undefined); | ||
assert.strictEqual(copiedOpts.search, undefined); | ||
assert.strictEqual(copiedOpts.hash, undefined); | ||
assert.strictEqual(copiedOpts.href, undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
require('../common'); | ||
const URL = require('url').URL; | ||
const assert = require('assert'); | ||
const urlToOptions = require('internal/url').urlToOptions; | ||
|
||
const url = new URL('http://user:[email protected]:21/aaa/zzz?l=24#test'); | ||
const oldParams = url.searchParams; // For test of [SameObject] | ||
|
@@ -131,41 +130,6 @@ assert.strictEqual(url.toString(), | |
assert.strictEqual((delete url.searchParams), true); | ||
assert.strictEqual(url.searchParams, oldParams); | ||
|
||
// Test urlToOptions | ||
{ | ||
const urlObj = new URL('http://user:[email protected]:21/aaa/zzz?l=24#test'); | ||
const opts = urlToOptions(urlObj); | ||
assert.strictEqual(opts instanceof URL, false); | ||
assert.strictEqual(opts.protocol, 'http:'); | ||
assert.strictEqual(opts.auth, 'user:pass'); | ||
assert.strictEqual(opts.hostname, 'foo.bar.com'); | ||
assert.strictEqual(opts.port, 21); | ||
assert.strictEqual(opts.path, '/aaa/zzz?l=24'); | ||
assert.strictEqual(opts.pathname, '/aaa/zzz'); | ||
assert.strictEqual(opts.search, '?l=24'); | ||
assert.strictEqual(opts.hash, '#test'); | ||
|
||
const { hostname } = urlToOptions(new URL('http://[::1]:21')); | ||
assert.strictEqual(hostname, '::1'); | ||
|
||
// If a WHATWG URL object is copied, it is possible that the resulting copy | ||
// contains the Symbols that Node uses for brand checking, but not the data | ||
// properties, which are getters. Verify that urlToOptions() can handle such | ||
// a case. | ||
const copiedUrlObj = { ...urlObj }; | ||
const copiedOpts = urlToOptions(copiedUrlObj); | ||
assert.strictEqual(copiedOpts instanceof URL, false); | ||
assert.strictEqual(copiedOpts.protocol, undefined); | ||
assert.strictEqual(copiedOpts.auth, undefined); | ||
assert.strictEqual(copiedOpts.hostname, undefined); | ||
assert.strictEqual(copiedOpts.port, NaN); | ||
assert.strictEqual(copiedOpts.path, ''); | ||
assert.strictEqual(copiedOpts.pathname, undefined); | ||
assert.strictEqual(copiedOpts.search, undefined); | ||
assert.strictEqual(copiedOpts.hash, undefined); | ||
assert.strictEqual(copiedOpts.href, undefined); | ||
} | ||
|
||
// Test special origins | ||
[ | ||
{ expected: 'https://whatwg.org', | ||
|
Uh oh!
There was an error while loading. Please reload this page.