From f1eaa851dfd94d3d27e42f5cea4f1140a373ea9f Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 7 May 2023 15:41:21 -0400 Subject: [PATCH] url: improve `isURL` detection PR-URL: https://github.com/nodejs/node/pull/47886 Reviewed-By: Rich Trott Reviewed-By: Matthew Aitken Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Luigi Pinca --- lib/internal/url.js | 4 +++- test/parallel/test-url-is-url.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-url-is-url.js diff --git a/lib/internal/url.js b/lib/internal/url.js index 07d2e2241ddfe4..ed25b2d2188fa1 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -571,11 +571,13 @@ 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 { diff --git a/test/parallel/test-url-is-url.js b/test/parallel/test-url-is-url.js new file mode 100644 index 00000000000000..0b42bb3b2f2d4a --- /dev/null +++ b/test/parallel/test-url-is-url.js @@ -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);