Skip to content

Commit

Permalink
url: resolve feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
thisalihassan committed Apr 11, 2024
1 parent 48e3b9a commit c304252
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
13 changes: 10 additions & 3 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,18 @@ function isURL(self) {
return Boolean(self?.href && self.protocol && self.auth === undefined && self.path === undefined);
}

/**
* A unique symbol used as a private identifier to safely invoke the URL constructor
* with a special parsing behavior. When passed as the third argument to the URL
* constructor, it signals that the constructor should not throw an exception
* for invalid URL inputs.
*/
const kParseURLSymbol = Symbol('kParseURL');

class URL {
#context = new URLContext();
#searchParams;
#searchParamsModified;
static #kParseURLSymbol = Symbol('kParseURL');

static {
setURLSearchParamsModified = (obj) => {
Expand Down Expand Up @@ -802,7 +809,7 @@ class URL {
base = `${base}`;
}

const raiseException = parseSymbol !== URL.#kParseURLSymbol;
const raiseException = parseSymbol !== kParseURLSymbol;
const href = bindingUrl.parse(input, base, raiseException);
if (href) {
this.#updateContext(href);
Expand All @@ -813,7 +820,7 @@ class URL {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('url');
}
const parsedURLObject = new URL(input, base, URL.#kParseURLSymbol);
const parsedURLObject = new URL(input, base, kParseURLSymbol);
return parsedURLObject.href ? parsedURLObject : null;
}

Expand Down
14 changes: 6 additions & 8 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
// args[1] // base url
// args[2] // raise Exception

const bool raise_exception = args[2]->BooleanValue(args.GetIsolate());
const bool raise_exception = args.Length() > 2 && args[2]->IsTrue();

Realm* realm = Realm::GetCurrent(args);
BindingData* binding_data = realm->GetBindingData<BindingData>();
Expand All @@ -251,19 +251,17 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
if (!base && raise_exception) {
return ThrowInvalidURL(realm->env(), input.ToStringView(), base_);
} else if (!base) {
return args.GetReturnValue().SetNull();
return;
}
base_pointer = &base.value();
}
auto out =
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);

if (!out) {
if (raise_exception) {
return ThrowInvalidURL(realm->env(), input.ToStringView(), base_);
} else {
return args.GetReturnValue().SetNull();
}
if (!out && raise_exception) {
return ThrowInvalidURL(realm->env(), input.ToStringView(), base_);
} else if (!out) {
return;
}

binding_data->UpdateComponents(out->get_components(), out->type);
Expand Down

0 comments on commit c304252

Please sign in to comment.