From a24884c9a581bc065ed34290b643c94b6653c32b Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 14 Apr 2023 07:34:20 -0400 Subject: [PATCH] url: handle URL.canParse without base parameter PR-URL: https://github.com/nodejs/node/pull/47547 Reviewed-By: Debadree Chatterjee Reviewed-By: Matthew Aitken Reviewed-By: Robert Nagy Reviewed-By: Luigi Pinca --- src/node_url.cc | 2 +- test/parallel/test-whatwg-url-canparse.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-whatwg-url-canparse.js diff --git a/src/node_url.cc b/src/node_url.cc index 551e726145f06b..ca0d064281c23c 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -115,7 +115,7 @@ void BindingData::DomainToUnicode(const FunctionCallbackInfo& args) { // TODO(@anonrig): Add V8 Fast API for CanParse method void BindingData::CanParse(const FunctionCallbackInfo& args) { - CHECK_GE(args.Length(), 2); + CHECK_GE(args.Length(), 1); CHECK(args[0]->IsString()); // input // args[1] // base url diff --git a/test/parallel/test-whatwg-url-canparse.js b/test/parallel/test-whatwg-url-canparse.js new file mode 100644 index 00000000000000..5edda72979164e --- /dev/null +++ b/test/parallel/test-whatwg-url-canparse.js @@ -0,0 +1,21 @@ +// Flags: --expose-internals +'use strict'; + +require('../common'); + +const { URL } = require('url'); +const assert = require('assert'); + +let internalBinding; +try { + internalBinding = require('internal/test/binding').internalBinding; +} catch (e) { + console.log('using `test/parallel/test-whatwg-url-canparse` requires `--expose-internals`'); + throw e; +} + +const { canParse } = internalBinding('url'); + +// It should not throw when called without a base string +assert.strictEqual(URL.canParse('https://example.org'), true); +assert.strictEqual(canParse('https://example.org'), true);