Skip to content

Commit c5725a1

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

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/internal/url.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,14 @@ ObjectDefineProperties(URLSearchParams.prototype, {
692692
*
693693
* We use `href` and `protocol` as they are the only properties that are
694694
* easy to retrieve and calculate due to the lazy nature of the getters.
695+
*
696+
* We check for auth attribute to distinguish legacy url instance with
697+
* WHATWG URL instance.
695698
* @param {*} self
696699
* @returns {self is URL}
697700
*/
698701
function isURL(self) {
699-
return Boolean(self?.href && self.protocol);
702+
return Boolean(self?.href && self.protocol && self.auth === undefined);
700703
}
701704

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