Skip to content
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

url: improve WHATWG URL inspection #12253

Closed
wants to merge 2 commits into from

Conversation

TimothyGu
Copy link
Member

  • Show all available properties (including searchParams!)
  • Colors!
  • Respect depth
  • Throw an error if this is not an actual URL
  • Inspect [context] if showHidden is true

Before:

screenshot from 2017-04-05 21 53 17

After:

screenshot from 2017-04-04 20 21 43

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

url

@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Apr 6, 2017
port: this.port,
pathname: this.pathname,
search: this.search,
searchParams: this.searchParams,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url.searchParams is a property one can access: https://nodejs.org/api/url.html#url_url_searchparams

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, I missed that.

return ret;

return util.inspect(obj, opts).replace(/^URLTemp/,
() => this.constructor.name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be simplified to just be 'URL'?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to allow subclassing. For example, with this PR and

class MyURL extends URL {}

inspecting new MyURL('...') will show MyURL instead URL.

protocol: this.protocol,
username: this.username,
password: (opts.showHidden || ctx.password == null) ?
this.password : '--------',
Copy link
Contributor

@mscdex mscdex Apr 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @jasnell

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was me just being paranoid ;-) ... I'm fine with showing the password here.

ret += '}';
return ret;

return util.inspect(obj, opts).replace(/^URLTemp/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

if (typeof depth === 'number' && depth < 0)
return opts.stylize('[Object]', 'special');

const obj = Object.assign(new URLTemp(), {
Copy link
Contributor

@mscdex mscdex Apr 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@mscdex
Copy link
Contributor

mscdex commented Apr 7, 2017

Did you benchmark the hasOwnProperty() loop vs. the replace()? I'm wondering if the replace() would be faster?

port: '8080',
pathname: '/path/name/',
search: '?que=ry',
searchParams: URLSearchParams { 'que' => 'ry' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't really say that I'm a fan of the use of => here instead of a :.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, multiple values could be shown together as an array?

{ query: [ 'value1', 'value2' ] }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mscdex that'd remove the order of queries.

@addaleax
Copy link
Member

Landed in 2841f47

@addaleax addaleax closed this Apr 14, 2017
addaleax pushed a commit that referenced this pull request Apr 14, 2017
PR-URL: #12253
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
@TimothyGu TimothyGu deleted the url-inspect branch April 14, 2017 22:31
@jasnell jasnell mentioned this pull request May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants