Skip to content

Commit

Permalink
url: improve isURL detection
Browse files Browse the repository at this point in the history
PR-URL: #47886
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
anonrig authored and targos committed May 12, 2023
1 parent a11507e commit 1b06c1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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
* 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);

0 comments on commit 1b06c1e

Please sign in to comment.