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

fix(ext/node): fix whatwg url formatting #19146

Merged
merged 3 commits into from
May 17, 2023
Merged
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
1 change: 1 addition & 0 deletions cli/tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
"test-url-domain-ascii-unicode.js",
"test-url-fileurltopath.js",
"test-url-format-invalid-input.js",
"test-url-format-whatwg.js",
"test-url-format.js",
"test-url-parse-invalid-input.js",
"test-url-parse-query.js",
Expand Down
149 changes: 149 additions & 0 deletions cli/tests/node_compat/test/parallel/test-url-format-whatwg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

'use strict';

const common = require('../common');
if (!common.hasIntl)
common.skip('missing Intl');

const assert = require('assert');
const url = require('url');

const myURL = new URL('http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c');

assert.strictEqual(
url.format(myURL),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {}),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

{
[true, 1, 'test', Infinity].forEach((value) => {
assert.throws(
() => url.format(myURL, value),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options" argument must be of type object.' +
common.invalidArgTypeHelper(value)
}
);
});
}

// Any falsy value other than undefined will be treated as false.
// Any truthy value will be treated as true.

assert.strictEqual(
url.format(myURL, { auth: false }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { auth: '' }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { auth: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { auth: 1 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { auth: {} }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { fragment: false }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
);

assert.strictEqual(
url.format(myURL, { fragment: '' }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
);

assert.strictEqual(
url.format(myURL, { fragment: 0 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
);

assert.strictEqual(
url.format(myURL, { fragment: 1 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { fragment: {} }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { search: false }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
);

assert.strictEqual(
url.format(myURL, { search: '' }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
);

assert.strictEqual(
url.format(myURL, { search: 0 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
);

assert.strictEqual(
url.format(myURL, { search: 1 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { search: {} }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { unicode: true }),
'http://user:pass@理容ナカムラ.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { unicode: 1 }),
'http://user:pass@理容ナカムラ.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { unicode: {} }),
'http://user:pass@理容ナカムラ.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { unicode: false }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, { unicode: 0 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(new URL('http://user:pass@xn--0zwm56d.com:8080/path'), { unicode: true }),
'http://user:pass@测试.com:8080/path'
);
16 changes: 6 additions & 10 deletions ext/node/polyfills/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,14 +939,11 @@ export function format(
["Object", "string"],
urlObject,
);
} else if (!(urlObject instanceof Url)) {
if (urlObject instanceof URL) {
return formatWhatwg(urlObject, options);
}
return Url.prototype.format.call(urlObject);
} else if (urlObject instanceof URL) {
return formatWhatwg(urlObject, options);
}

return (urlObject as Url).format();
return Url.prototype.format.call(urlObject);
}

/**
Expand Down Expand Up @@ -1002,10 +999,9 @@ function formatWhatwg(
}
ret += "@";
}
// TODO(wafuwfu13): Support unicode option
// ret += options.unicode ?
// domainToUnicode(urlObject.host) : urlObject.host;
ret += urlObject.host;
ret += options.unicode
? domainToUnicode(urlObject.hostname)
: urlObject.hostname;
if (urlObject.port) {
ret += `:${urlObject.port}`;
}
Expand Down
3 changes: 1 addition & 2 deletions tools/node_compat/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

NOTE: This file should not be manually edited. Please edit 'cli/tests/node_compat/config.json' and run 'tools/node_compat/setup.ts' instead.

Total: 2935
Total: 2934

- [abort/test-abort-backtrace.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-backtrace.js)
- [abort/test-abort-fatal-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-fatal-error.js)
Expand Down Expand Up @@ -2372,7 +2372,6 @@ Total: 2935
- [parallel/test-ttywrap-stack.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-ttywrap-stack.js)
- [parallel/test-unhandled-exception-rethrow-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-unhandled-exception-rethrow-error.js)
- [parallel/test-unicode-node-options.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-unicode-node-options.js)
- [parallel/test-url-format-whatwg.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-url-format-whatwg.js)
- [parallel/test-url-null-char.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-url-null-char.js)
- [parallel/test-url-parse-format.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-url-parse-format.js)
- [parallel/test-utf8-scripts.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-utf8-scripts.js)
Expand Down