GeezerVison is showing github.com — url: expose urlToHttpOptions utility · nodejs/node@64fed31
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/url.md +46Lines changed: 46 additions & 0 deletions - Display the source diff
- Display the rich diff
| 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 | 1029 | 1029 | pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) | 1030 | 1030 | ``` | 1031 | 1031 |
| | 1032 | +### `url.urlToHttpOptions(url)` | | 1033 | +<!-- YAML | | 1034 | +added: REPLACEME | | 1035 | +--> | | 1036 | + | | 1037 | +* `url` {URL} The [WHATWG URL][] object to convert to an options object. | | 1038 | +* Returns: {Object} Options object | | 1039 | + * `protocol` {string} Protocol to use. | | 1040 | + * `hostname` {string} A domain name or IP address of the server to issue the | | 1041 | + request to. | | 1042 | + * `hash` {string} The fragment portion of the URL. | | 1043 | + * `search` {string} The serialized query portion of the URL. | | 1044 | + * `pathname` {string} The path portion of the URL. | | 1045 | + * `path` {string} Request path. Should include query string if any. | | 1046 | + E.G. `'/index.html?page=12'`. An exception is thrown when the request path | | 1047 | + contains illegal characters. Currently, only spaces are rejected but that | | 1048 | + may change in the future. | | 1049 | + * `href` {string} The serialized URL. | | 1050 | + * `port` {number} Port of remote server. | | 1051 | + * `auth` {string} Basic authentication i.e. `'user:password'` to compute an | | 1052 | + Authorization header. | | 1053 | + | | 1054 | +This utility function converts a URL object into an ordinary options object as | | 1055 | +expected by the [`http.request()`][] and [`https.request()`][] APIs. | | 1056 | + | | 1057 | +```js | | 1058 | +const { urlToHttpOptions } = require('url'); | | 1059 | +const myURL = new URL('https://a:b@測試?abc#foo'); | | 1060 | + | | 1061 | +console.log(urlToHttpOptions(myUrl)); | | 1062 | +/** | | 1063 | +{ | | 1064 | + protocol: 'https:', | | 1065 | + hostname: 'xn--g6w251d', | | 1066 | + hash: '#foo', | | 1067 | + search: '?abc', | | 1068 | + pathname: '/', | | 1069 | + path: '/?abc', | | 1070 | + href: 'https://a:b@xn--g6w251d/?abc#foo', | | 1071 | + auth: 'a:b' | | 1072 | +} | | 1073 | +*/ | | 1074 | +``` | | 1075 | + | 1032 | 1076 | ## Legacy URL API | 1033 | 1077 | <!-- YAML | 1034 | 1078 | deprecated: v11.0.0 | @@ -1388,6 +1432,8 @@ console.log(myURL.origin); | 1388 | 1432 | [`TypeError`]: errors.md#errors_class_typeerror | 1389 | 1433 | [`URLSearchParams`]: #url_class_urlsearchparams | 1390 | 1434 | [`array.toString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString | | 1435 | +[`http.request()`]: http.md#http_http_request_options_callback | | 1436 | +[`https.request()`]: https.md#https_https_request_options_callback | 1391 | 1437 | [`new URL()`]: #url_new_url_input_base | 1392 | 1438 | [`querystring`]: querystring.md | 1393 | 1439 | [`require('url').format()`]: #url_url_format_url_options | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,7 +58,7 @@ const { OutgoingMessage } = require('_http_outgoing'); | 58 | 58 | const Agent = require('_http_agent'); | 59 | 59 | const { Buffer } = require('buffer'); | 60 | 60 | const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); | 61 | | -const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); | | 61 | +const { URL, urlToHttpOptions, searchParamsSymbol } = require('internal/url'); | 62 | 62 | const { kOutHeaders, kNeedDrain } = require('internal/http'); | 63 | 63 | const { connResetException, codes } = require('internal/errors'); | 64 | 64 | const { | @@ -105,7 +105,7 @@ function ClientRequest(input, options, cb) { | 105 | 105 | if (typeof input === 'string') { | 106 | 106 | const urlStr = input; | 107 | 107 | try { | 108 | | - input = urlToOptions(new URL(urlStr)); | | 108 | + input = urlToHttpOptions(new URL(urlStr)); | 109 | 109 | } catch (err) { | 110 | 110 | input = url.parse(urlStr); | 111 | 111 | if (!input.hostname) { | @@ -122,7 +122,7 @@ function ClientRequest(input, options, cb) { | 122 | 122 | } else if (input && input[searchParamsSymbol] && | 123 | 123 | input[searchParamsSymbol][searchParamsSymbol]) { | 124 | 124 | // url.URL instance | 125 | | - input = urlToOptions(input); | | 125 | + input = urlToHttpOptions(input); | 126 | 126 | } else { | 127 | 127 | cb = options; | 128 | 128 | options = input; | |
Collapse file Copy file name to clipboardExpand all lines: lib/https.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,7 @@ const { ClientRequest } = require('_http_client'); | 48 | 48 | let debug = require('internal/util/debuglog').debuglog('https', (fn) => { | 49 | 49 | debug = fn; | 50 | 50 | }); | 51 | | -const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); | | 51 | +const { URL, urlToHttpOptions, searchParamsSymbol } = require('internal/url'); | 52 | 52 | const { IncomingMessage, ServerResponse } = require('http'); | 53 | 53 | const { kIncomingMessage } = require('_http_common'); | 54 | 54 |
| @@ -303,7 +303,7 @@ function request(...args) { | 303 | 303 | if (typeof args[0] === 'string') { | 304 | 304 | const urlStr = ArrayPrototypeShift(args); | 305 | 305 | try { | 306 | | - options = urlToOptions(new URL(urlStr)); | | 306 | + options = urlToHttpOptions(new URL(urlStr)); | 307 | 307 | } catch (err) { | 308 | 308 | options = url.parse(urlStr); | 309 | 309 | if (!options.hostname) { | @@ -320,7 +320,7 @@ function request(...args) { | 320 | 320 | } else if (args[0] && args[0][searchParamsSymbol] && | 321 | 321 | args[0][searchParamsSymbol][searchParamsSymbol]) { | 322 | 322 | // url.URL instance | 323 | | - options = urlToOptions(ArrayPrototypeShift(args)); | | 323 | + options = urlToHttpOptions(ArrayPrototypeShift(args)); | 324 | 324 | } | 325 | 325 |
| 326 | 326 | if (args[0] && typeof args[0] !== 'function') { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1295,7 +1295,7 @@ function domainToUnicode(domain) { | 1295 | 1295 | // Utility function that converts a URL object into an ordinary | 1296 | 1296 | // options object as expected by the http.request and https.request | 1297 | 1297 | // APIs. | 1298 | | -function urlToOptions(url) { | | 1298 | +function urlToHttpOptions(url) { | 1299 | 1299 | const options = { | 1300 | 1300 | protocol: url.protocol, | 1301 | 1301 | hostname: typeof url.hostname === 'string' && | @@ -1494,7 +1494,7 @@ module.exports = { | 1494 | 1494 | URLSearchParams, | 1495 | 1495 | domainToASCII, | 1496 | 1496 | domainToUnicode, | 1497 | | - urlToOptions, | | 1497 | + urlToHttpOptions, | 1498 | 1498 | formatSymbol: kFormat, | 1499 | 1499 | searchParamsSymbol: searchParams, | 1500 | 1500 | encodeStr | |
Collapse file Copy file name to clipboardExpand all lines: lib/url.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,9 +47,10 @@ const { | 47 | 47 | URLSearchParams, | 48 | 48 | domainToASCII, | 49 | 49 | domainToUnicode, | | 50 | + fileURLToPath, | 50 | 51 | formatSymbol, | 51 | 52 | pathToFileURL, | 52 | | - fileURLToPath | | 53 | + urlToHttpOptions, | 53 | 54 | } = require('internal/url'); | 54 | 55 |
| 55 | 56 | // Original url.parse() API | @@ -987,5 +988,6 @@ module.exports = { | 987 | 988 |
| 988 | 989 | // Utilities | 989 | 990 | pathToFileURL, | 990 | | - fileURLToPath | | 991 | + fileURLToPath, | | 992 | + urlToHttpOptions, | 991 | 993 | }; |
Collapse file Copy file name to clipboard +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +require('../common'); | | 3 | +const assert = require('assert'); | | 4 | +const { urlToHttpOptions } = require('url'); | | 5 | + | | 6 | +// Test urlToHttpOptions | | 7 | + | | 8 | +const opts = urlToHttpOptions(urlObj); | | 9 | +assert.strictEqual(opts instanceof URL, false); | | 10 | +assert.strictEqual(opts.protocol, 'http:'); | | 11 | +assert.strictEqual(opts.auth, 'user:pass'); | | 12 | +assert.strictEqual(opts.hostname, 'foo.bar.com'); | | 13 | +assert.strictEqual(opts.port, 21); | | 14 | +assert.strictEqual(opts.path, '/aaa/zzz?l=24'); | | 15 | +assert.strictEqual(opts.pathname, '/aaa/zzz'); | | 16 | +assert.strictEqual(opts.search, '?l=24'); | | 17 | +assert.strictEqual(opts.hash, '#test'); | | 18 | + | | 19 | +const { hostname } = urlToHttpOptions(new URL('http://[::1]:21')); | | 20 | +assert.strictEqual(hostname, '::1'); | | 21 | + | | 22 | +// If a WHATWG URL object is copied, it is possible that the resulting copy | | 23 | +// contains the Symbols that Node uses for brand checking, but not the data | | 24 | +// properties, which are getters. Verify that urlToHttpOptions() can handle | | 25 | +// such a case. | | 26 | +const copiedUrlObj = { ...urlObj }; | | 27 | +const copiedOpts = urlToHttpOptions(copiedUrlObj); | | 28 | +assert.strictEqual(copiedOpts instanceof URL, false); | | 29 | +assert.strictEqual(copiedOpts.protocol, undefined); | | 30 | +assert.strictEqual(copiedOpts.auth, undefined); | | 31 | +assert.strictEqual(copiedOpts.hostname, undefined); | | 32 | +assert.strictEqual(copiedOpts.port, NaN); | | 33 | +assert.strictEqual(copiedOpts.path, ''); | | 34 | +assert.strictEqual(copiedOpts.pathname, undefined); | | 35 | +assert.strictEqual(copiedOpts.search, undefined); | | 36 | +assert.strictEqual(copiedOpts.hash, undefined); | | 37 | +assert.strictEqual(copiedOpts.href, undefined); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-url-custom-properties.js -36Lines changed: 0 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | require('../common'); | 7 | 7 | const URL = require('url').URL; | 8 | 8 | const assert = require('assert'); | 9 | | -const urlToOptions = require('internal/url').urlToOptions; | 10 | 9 |
| 11 | 10 | | 12 | 11 | const oldParams = url.searchParams; // For test of [SameObject] | @@ -131,41 +130,6 @@ assert.strictEqual(url.toString(), | 131 | 130 | assert.strictEqual((delete url.searchParams), true); | 132 | 131 | assert.strictEqual(url.searchParams, oldParams); | 133 | 132 |
| 134 | | -// Test urlToOptions | 135 | | -{ | 136 | | - | 137 | | - const opts = urlToOptions(urlObj); | 138 | | - assert.strictEqual(opts instanceof URL, false); | 139 | | - assert.strictEqual(opts.protocol, 'http:'); | 140 | | - assert.strictEqual(opts.auth, 'user:pass'); | 141 | | - assert.strictEqual(opts.hostname, 'foo.bar.com'); | 142 | | - assert.strictEqual(opts.port, 21); | 143 | | - assert.strictEqual(opts.path, '/aaa/zzz?l=24'); | 144 | | - assert.strictEqual(opts.pathname, '/aaa/zzz'); | 145 | | - assert.strictEqual(opts.search, '?l=24'); | 146 | | - assert.strictEqual(opts.hash, '#test'); | 147 | | - | 148 | | - const { hostname } = urlToOptions(new URL('http://[::1]:21')); | 149 | | - assert.strictEqual(hostname, '::1'); | 150 | | - | 151 | | - // If a WHATWG URL object is copied, it is possible that the resulting copy | 152 | | - // contains the Symbols that Node uses for brand checking, but not the data | 153 | | - // properties, which are getters. Verify that urlToOptions() can handle such | 154 | | - // a case. | 155 | | - const copiedUrlObj = { ...urlObj }; | 156 | | - const copiedOpts = urlToOptions(copiedUrlObj); | 157 | | - assert.strictEqual(copiedOpts instanceof URL, false); | 158 | | - assert.strictEqual(copiedOpts.protocol, undefined); | 159 | | - assert.strictEqual(copiedOpts.auth, undefined); | 160 | | - assert.strictEqual(copiedOpts.hostname, undefined); | 161 | | - assert.strictEqual(copiedOpts.port, NaN); | 162 | | - assert.strictEqual(copiedOpts.path, ''); | 163 | | - assert.strictEqual(copiedOpts.pathname, undefined); | 164 | | - assert.strictEqual(copiedOpts.search, undefined); | 165 | | - assert.strictEqual(copiedOpts.hash, undefined); | 166 | | - assert.strictEqual(copiedOpts.href, undefined); | 167 | | -} | 168 | | - | 169 | 133 | // Test special origins | 170 | 134 | [ | 171 | 135 | { expected: 'https://whatwg.org', | |
|