Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: implement URL.canParse #47179

Merged
merged 6 commits into from Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions benchmark/url/whatwgurl-canParse.js
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
type: Object.keys(common.urls),
n: [25e6],
});

function main({ type, n }) {
bench.start();
for (let i = 0; i < n; i += 1)
URL.canParse(common.urls[type]);
bench.end(n);
}
21 changes: 21 additions & 0 deletions doc/api/url.md
Expand Up @@ -662,6 +662,27 @@ added: v16.7.0
Removes the stored {Blob} identified by the given ID. Attempting to revoke a
ID that isn't registered will silently fail.

#### `URL.canParse(input[, base])`

<!-- YAML
added: REPLACEME
-->

* `input` {string} The absolute or relative input URL to parse. If `input`
is relative, then `base` is required. If `input` is absolute, the `base`
is ignored. If `input` is not a string, it is [converted to a string][] first.
* `base` {string} The base URL to resolve against if the `input` is not
absolute. If `base` is not a string, it is [converted to a string][] first.
* Returns: {boolean}

Checks if an `input` relative to the `base` can be parsed to a `URL`.

```js
const isValid = URL.canParse('/foo', 'https://example.org/'); // true

const isNotValid = URL.canParse('/foo'); // false
```

### Class: `URLSearchParams`

<!-- YAML
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/url.js
Expand Up @@ -88,6 +88,7 @@ const {
domainToASCII: _domainToASCII,
domainToUnicode: _domainToUnicode,
parse,
canParse: _canParse,
updateUrl,
} = internalBinding('url');

Expand Down Expand Up @@ -721,7 +722,7 @@ class URL {
base = `${base}`;
}

return parse(url, base, () => {});
return _canParse(url, base);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/node_url.cc
Expand Up @@ -93,6 +93,30 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(true);
}

void CanParse(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 2);
CHECK(args[0]->IsString()); // input
// args[1] // base url

Environment* env = Environment::GetCurrent(args);
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

Utf8Value input(env->isolate(), args[0]);
ada::result base;
ada::url* base_pointer = nullptr;
if (args[1]->IsString()) {
base = ada::parse(Utf8Value(env->isolate(), args[1]).ToString());
anonrig marked this conversation as resolved.
Show resolved Hide resolved
if (!base) {
return args.GetReturnValue().Set(false);
}
base_pointer = &base.value();
}
ada::result out = ada::parse(input.ToStringView(), base_pointer);

args.GetReturnValue().Set(out.has_value());
}

void DomainToASCII(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 1);
Expand Down Expand Up @@ -284,6 +308,7 @@ void Initialize(Local<Object> target,
Local<Context> context,
void* priv) {
SetMethod(context, target, "parse", Parse);
SetMethod(context, target, "canParse", CanParse);
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
SetMethod(context, target, "updateUrl", UpdateUrl);
SetMethodNoSideEffect(context, target, "formatUrl", FormatUrl);

Expand All @@ -294,6 +319,7 @@ void Initialize(Local<Object> target,

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Parse);
registry->Register(CanParse);
registry->Register(UpdateUrl);
registry->Register(FormatUrl);

Expand Down