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 isURL detection #47886

Merged
merged 1 commit into from
May 7, 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
5 changes: 4 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,14 @@ ObjectDefineProperties(URLSearchParams.prototype, {
*
* We use `href` and `protocol` as they are the only properties that are
* easy to retrieve and calculate due to the lazy nature of the getters.
*
* We check for auth attribute to distinguish legacy url instance with
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* We check for auth attribute to distinguish legacy url instance with
* We check for auth attribute to distinguish legacy URL instance with

For consistency.

Copy link
Member

Choose a reason for hiding this comment

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

URL is the name of the WHATWG URL object. Perhaps to avoid confusion, it should be urlObject here to be consistent with https://nodejs.org/dist/latest-v20.x/docs/api/url.html#legacy-urlobject?

Suggested change
* We check for auth attribute to distinguish legacy url instance with
* We check for auth attribute to distinguish legacy urlObject instance with

* WHATWG URL instance.
* @param {*} self
* @returns {self is URL}
*/
function isURL(self) {
return Boolean(self?.href && self.protocol);
return Boolean(self?.href && self.protocol && self.auth === undefined);
}

class URL {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-url-is-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Flags: --expose-internals
'use strict';

require('../common');

const { URL, parse } = require('url');
const assert = require('assert');
const { isURL } = require('internal/url');

assert.strictEqual(isURL(new URL('https://www.nodejs.org')), true);
assert.strictEqual(isURL(parse('https://www.nodejs.org')), false);