url: improve WHATWG URL inspection#12253
Conversation
lib/internal/url.js
Outdated
| port: this.port, | ||
| pathname: this.pathname, | ||
| search: this.search, | ||
| searchParams: this.searchParams, |
There was a problem hiding this comment.
Shouldn't this be this[searchParams] since searchParams is a Symbol and not a regular property name?
Additionally, I think it's very misleading to be representing the searchParams as a regular property when it cannot be accessed as such.
There was a problem hiding this comment.
url.searchParams is a property one can access: https://nodejs.org/api/url.html#url_url_searchparams
lib/internal/url.js
Outdated
| return ret; | ||
|
|
||
| return util.inspect(obj, opts).replace(/^URLTemp/, | ||
| () => this.constructor.name); |
There was a problem hiding this comment.
Couldn't this be simplified to just be 'URL'?
There was a problem hiding this comment.
I wanted to allow subclassing. For example, with this PR and
class MyURL extends URL {}inspecting new MyURL('...') will show MyURL instead URL.
lib/internal/url.js
Outdated
| protocol: this.protocol, | ||
| username: this.username, | ||
| password: (opts.showHidden || ctx.password == null) ? | ||
| this.password : '--------', |
There was a problem hiding this comment.
This might be confusing/unexpected behavior for some... Why hide it at all? There does not seem to be a precedent for this AFAICT. For example the browser seems to show the password value every time when inspecting it.
There was a problem hiding this comment.
This was me just being paranoid ;-) ... I'm fine with showing the password here.
lib/internal/url.js
Outdated
| ret += '}'; | ||
| return ret; | ||
|
|
||
| return util.inspect(obj, opts).replace(/^URLTemp/, |
There was a problem hiding this comment.
This replace() can be avoided by simply doing var URL = function() {}; at the top of the function here and then simply using new URL() instead of new URLTemp().
lib/internal/url.js
Outdated
| if (typeof depth === 'number' && depth < 0) | ||
| return opts.stylize('[Object]', 'special'); | ||
|
|
||
| const obj = Object.assign(new URLTemp(), { |
There was a problem hiding this comment.
I think we should avoid Object.assign(), especially when we're already using a custom/"fake" constructor which could easily assign its own appropriate values in its constructor (e.g. just pass this to the constructor).
|
Did you benchmark the |
| port: '8080', | ||
| pathname: '/path/name/', | ||
| search: '?que=ry', | ||
| searchParams: URLSearchParams { 'que' => 'ry' }, |
There was a problem hiding this comment.
Can't really say that I'm a fan of the use of => here instead of a :.
There was a problem hiding this comment.
That is to be consistent with the inspection output of Map, plus it's weird to write
{ query: 'value1',
query: 'value2' }with object literal syntax when it doesn't really work.
There was a problem hiding this comment.
Well, multiple values could be shown together as an array?
{ query: [ 'value1', 'value2' ] }|
Landed in 2841f47 |
PR-URL: #12253 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
searchParams!)depththisis not an actual URL[context]ifshowHiddenis trueBefore:
After:
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
url