Skip to content

Commit 4a0b183

Browse files
committed
url: improve isURL detection
1 parent 03db049 commit 4a0b183

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/internal/url.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
IteratorPrototype,
1414
Number,
1515
ObjectDefineProperties,
16+
ObjectPrototypeHasOwnProperty,
1617
ObjectSetPrototypeOf,
1718
ReflectGetOwnPropertyDescriptor,
1819
ReflectOwnKeys,
@@ -692,11 +693,14 @@ ObjectDefineProperties(URLSearchParams.prototype, {
692693
*
693694
* We use `href` and `protocol` as they are the only properties that are
694695
* easy to retrieve and calculate due to the lazy nature of the getters.
696+
*
697+
* We check for auth attribute to distinguish legacy url instance with
698+
* WHATWG URL instance.
695699
* @param {*} self
696700
* @returns {self is URL}
697701
*/
698702
function isURL(self) {
699-
return Boolean(self?.href && self.protocol);
703+
return Boolean(self?.href && self.protocol && !ObjectPrototypeHasOwnProperty(self.auth));
700704
}
701705

702706
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)