Skip to content

Commit f1eaa85

Browse files
committed
url: improve isURL detection
PR-URL: nodejs#47886 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 0ec5262 commit f1eaa85

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/internal/url.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,13 @@ ObjectDefineProperties(URLSearchParams.prototype, {
571571
* We use `href` and `protocol` as they are the only properties that are
572572
* easy to retrieve and calculate due to the lazy nature of the getters.
573573
*
574+
* We check for auth attribute to distinguish legacy url instance with
575+
* WHATWG URL instance.
574576
* @param {*} self
575577
* @returns {self is URL}
576578
*/
577579
function isURL(self) {
578-
return Boolean(self?.href && self.protocol);
580+
return Boolean(self?.href && self.protocol && self.auth === undefined);
579581
}
580582

581583
class URL {

test/parallel/test-url-is-url.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
6+
const { URL, parse } = require('url');
7+
const assert = require('assert');
8+
const { isURL } = require('internal/url');
9+
10+
assert.strictEqual(isURL(new URL('https://www.nodejs.org')), true);
11+
assert.strictEqual(isURL(parse('https://www.nodejs.org')), false);

0 commit comments

Comments
 (0)