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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 54 additions & 24 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ function onParseHashComplete(flags, protocol, username, password,
}
}

function getEligibleConstructor(obj) {
while (obj !== null) {
if (Object.prototype.hasOwnProperty.call(obj, 'constructor') &&
typeof obj.constructor === 'function') {
return obj.constructor;
}
obj = Object.getPrototypeOf(obj);
}
return null;
}

class URL {
constructor(input, base) {
// toUSVString is not needed.
Expand All @@ -255,33 +266,43 @@ class URL {
}

[util.inspect.custom](depth, opts) {
if (this == null ||
Object.getPrototypeOf(this[context]) !== URLContext.prototype) {
throw new TypeError('Value of `this` is not a URL');
}

const ctx = this[context];
var ret = 'URL {\n';
ret += ` href: ${this.href}\n`;
if (ctx.scheme !== undefined)
ret += ` protocol: ${this.protocol}\n`;
if (ctx.username !== undefined)
ret += ` username: ${this.username}\n`;
if (ctx.password !== undefined) {
const pwd = opts.showHidden ? ctx.password : '--------';
ret += ` password: ${pwd}\n`;
}
if (ctx.host !== undefined)
ret += ` hostname: ${this.hostname}\n`;
if (ctx.port !== undefined)
ret += ` port: ${this.port}\n`;
if (ctx.path !== undefined)
ret += ` pathname: ${this.pathname}\n`;
if (ctx.query !== undefined)
ret += ` search: ${this.search}\n`;
if (ctx.fragment !== undefined)
ret += ` hash: ${this.hash}\n`;

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

const ctor = getEligibleConstructor(this);

const obj = Object.create({
constructor: ctor === null ? URL : ctor
});

obj.href = this.href;
obj.origin = this.origin;
obj.protocol = this.protocol;
obj.username = this.username;
obj.password = (opts.showHidden || ctx.password == null) ?
this.password : '--------';
obj.host = this.host;
obj.hostname = this.hostname;
obj.port = this.port;
obj.pathname = this.pathname;
obj.search = this.search;
obj.searchParams = this.searchParams;
obj.hash = this.hash;

if (opts.showHidden) {
ret += ` cannot-be-base: ${this[cannotBeBase]}\n`;
ret += ` special: ${this[special]}\n`;
obj.cannotBeBase = this[cannotBeBase];
obj.special = this[special];
obj[context] = this[context];
}
ret += '}';
return ret;

return util.inspect(obj, opts);
}
}

Expand Down Expand Up @@ -891,6 +912,9 @@ class URLSearchParams {
throw new TypeError('Value of `this` is not a URLSearchParams');
}

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

const separator = ', ';
const innerOpts = Object.assign({}, ctx);
if (recurseTimes !== null) {
Expand Down Expand Up @@ -1244,6 +1268,12 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', {
};
},
[util.inspect.custom](recurseTimes, ctx) {
if (this == null || this[context] == null || this[context].target == null)
throw new TypeError('Value of `this` is not a URLSearchParamsIterator');

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

const innerOpts = Object.assign({}, ctx);
if (recurseTimes !== null) {
innerOpts.depth = recurseTimes - 1;
Expand Down
114 changes: 49 additions & 65 deletions test/parallel/test-whatwg-url-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const common = require('../common');
const util = require('util');
const URL = require('url').URL;
const path = require('path');
const assert = require('assert');

if (!common.hasIntl) {
Expand All @@ -13,71 +12,56 @@ if (!common.hasIntl) {
}

// Tests below are not from WPT.
const tests = require(path.join(common.fixturesDir, 'url-tests'));
const additional_tests = require(
path.join(common.fixturesDir, 'url-tests-additional'));
const url = new URL('https://username:password@host.name:8080/path/name/?que=ry#hash');

const allTests = additional_tests.slice();
for (const test of tests) {
if (test.failure || typeof test === 'string') continue;
allTests.push(test);
}

for (const test of allTests) {
const url = test.url ? new URL(test.url) : new URL(test.input, test.base);

for (const showHidden of [true, false]) {
const res = util.inspect(url, {
showHidden
});

const lines = res.split('\n');
assert.strictEqual(
util.inspect(url),
`URL {
href: 'https://username:password@host.name:8080/path/name/?que=ry#hash',
origin: 'https://host.name:8080',
protocol: 'https:',
username: 'username',
password: '--------',
host: 'host.name:8080',
hostname: 'host.name',
port: '8080',
pathname: '/path/name/',
search: '?que=ry',
searchParams: URLSearchParams { 'que' => 'ry' },
hash: '#hash' }`);

const firstLine = lines[0];
assert.strictEqual(firstLine, 'URL {');
assert.strictEqual(
util.inspect(url, { showHidden: true }),
`URL {
href: 'https://username:password@host.name:8080/path/name/?que=ry#hash',
origin: 'https://host.name:8080',
protocol: 'https:',
username: 'username',
password: 'password',
host: 'host.name:8080',
hostname: 'host.name',
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.

hash: '#hash',
cannotBeBase: false,
special: true,
[Symbol(context)]:\x20
URLContext {
flags: 2032,
scheme: 'https:',
username: 'username',
password: 'password',
host: 'host.name',
port: 8080,
path: [ 'path', 'name', '', [length]: 3 ],
query: 'que=ry',
fragment: 'hash' } }`);

const lastLine = lines[lines.length - 1];
assert.strictEqual(lastLine, '}');
assert.strictEqual(
util.inspect({ a: url }, { depth: 0 }),
'{ a: [Object] }');

const innerLines = lines.slice(1, lines.length - 1);
const keys = new Set();
for (const line of innerLines) {
const i = line.indexOf(': ');
const k = line.slice(0, i).trim();
const v = line.slice(i + 2);
assert.strictEqual(keys.has(k), false, 'duplicate key found: ' + k);
keys.add(k);

const hidden = new Set([
'password',
'cannot-be-base',
'special'
]);
if (showHidden) {
if (!hidden.has(k)) {
assert.strictEqual(v, url[k], k);
continue;
}

if (k === 'password') {
assert.strictEqual(v, url[k], k);
}
if (k === 'cannot-be-base') {
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
}
if (k === 'special') {
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
}
continue;
}

// showHidden is false
if (k === 'password') {
assert.strictEqual(v, '--------', k);
continue;
}
assert.strictEqual(hidden.has(k), false, 'no hidden keys: ' + k);
assert.strictEqual(v, url[k], k);
}
}
}
class MyURL extends URL {}
assert(util.inspect(new MyURL(url.href)).startsWith('MyURL {'));