Skip to content

Commit e926354

Browse files
anonrigdanielleadams
authored andcommittedApr 12, 2023
url: replace url-parser with ada
PR-URL: #46410 Backport-PR-URL: #47435 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 0bc4c17 commit e926354

17 files changed

+376
-2957
lines changed
 

‎lib/internal/url.js

+139-356
Large diffs are not rendered by default.

‎node.gyp

-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,6 @@
545545
'src/node_trace_events.cc',
546546
'src/node_types.cc',
547547
'src/node_url.cc',
548-
'src/node_url_tables.cc',
549548
'src/node_util.cc',
550549
'src/node_v8.cc',
551550
'src/node_wasi.cc',
@@ -1261,7 +1260,6 @@
12611260
'test/cctest/test_sockaddr.cc',
12621261
'test/cctest/test_traced_value.cc',
12631262
'test/cctest/test_util.cc',
1264-
'test/cctest/test_url.cc',
12651263
],
12661264

12671265
'conditions': [

‎src/crypto/crypto_common.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
#include "crypto/crypto_common.h"
12
#include "base_object-inl.h"
23
#include "env-inl.h"
4+
#include "memory_tracker-inl.h"
5+
#include "node.h"
36
#include "node_buffer.h"
47
#include "node_crypto.h"
5-
#include "crypto/crypto_common.h"
6-
#include "node.h"
78
#include "node_internals.h"
8-
#include "node_url.h"
99
#include "string_bytes.h"
10-
#include "memory_tracker-inl.h"
1110
#include "v8.h"
1211

1312
#include <openssl/ec.h>

‎src/inspector_agent.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,9 @@ class NodeInspectorClient : public V8InspectorClient {
647647
protocol::StringUtil::StringViewToUtf8(resource_name_view);
648648
if (!IsFilePath(resource_name))
649649
return nullptr;
650-
node::url::URL url = node::url::URL::FromFilePath(resource_name);
651-
return Utf8ToStringView(url.href());
650+
651+
std::string url = node::url::FromFilePath(resource_name);
652+
return Utf8ToStringView(url);
652653
}
653654

654655
node::Environment* env_;

‎src/module_wrap.cc

-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "node_errors.h"
77
#include "node_internals.h"
88
#include "node_process-inl.h"
9-
#include "node_url.h"
109
#include "node_watchdog.h"
1110
#include "util-inl.h"
1211

@@ -20,8 +19,6 @@ namespace loader {
2019
using errors::TryCatchScope;
2120

2221
using node::contextify::ContextifyContext;
23-
using node::url::URL;
24-
using node::url::URL_FLAGS_FAILED;
2522
using v8::Array;
2623
using v8::ArrayBufferView;
2724
using v8::Context;

‎src/node_api.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
657657
// a file system path.
658658
// TODO(gabrielschulhof): Pass the `filename` through unchanged if/when we
659659
// receive it as a URL already.
660-
module_filename = node::url::URL::FromFilePath(filename.ToString()).href();
660+
module_filename = node::url::FromFilePath(filename.ToString());
661661
}
662662

663663
// Create a new napi_env for this specific module.

‎src/node_metadata.cc

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "node_metadata.h"
22
#include "acorn_version.h"
3+
#include "ada.h"
34
#include "ares.h"
45
#include "brotli/encode.h"
56
#include "llhttp.h"
@@ -115,6 +116,7 @@ Metadata::Versions::Versions() {
115116
#endif
116117

117118
simdutf = SIMDUTF_VERSION;
119+
ada = ADA_VERSION;
118120
}
119121

120122
Metadata::Release::Release() : name(NODE_RELEASE) {

‎src/node_metadata.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace node {
4747
V(uvwasi) \
4848
V(acorn) \
4949
V(simdutf) \
50+
V(ada) \
5051
NODE_VERSIONS_KEY_UNDICI(V)
5152

5253
#if HAVE_OPENSSL

‎src/node_url.cc

+187-1,750
Large diffs are not rendered by default.

‎src/node_url.h

+3-181
Original file line numberDiff line numberDiff line change
@@ -3,196 +3,18 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include "ada.h"
67
#include "node.h"
8+
#include "util.h"
79

810
#include <string>
911

1012
namespace node {
1113
namespace url {
1214

13-
#define PARSESTATES(XX) \
14-
XX(kSchemeStart) \
15-
XX(kScheme) \
16-
XX(kNoScheme) \
17-
XX(kSpecialRelativeOrAuthority) \
18-
XX(kPathOrAuthority) \
19-
XX(kRelative) \
20-
XX(kRelativeSlash) \
21-
XX(kSpecialAuthoritySlashes) \
22-
XX(kSpecialAuthorityIgnoreSlashes) \
23-
XX(kAuthority) \
24-
XX(kHost) \
25-
XX(kHostname) \
26-
XX(kPort) \
27-
XX(kFile) \
28-
XX(kFileSlash) \
29-
XX(kFileHost) \
30-
XX(kPathStart) \
31-
XX(kPath) \
32-
XX(kCannotBeBase) \
33-
XX(kQuery) \
34-
XX(kFragment)
35-
36-
#define FLAGS(XX) \
37-
XX(URL_FLAGS_NONE, 0) \
38-
XX(URL_FLAGS_FAILED, 0x01) \
39-
XX(URL_FLAGS_CANNOT_BE_BASE, 0x02) \
40-
XX(URL_FLAGS_INVALID_PARSE_STATE, 0x04) \
41-
XX(URL_FLAGS_TERMINATED, 0x08) \
42-
XX(URL_FLAGS_SPECIAL, 0x10) \
43-
XX(URL_FLAGS_HAS_USERNAME, 0x20) \
44-
XX(URL_FLAGS_HAS_PASSWORD, 0x40) \
45-
XX(URL_FLAGS_HAS_HOST, 0x80) \
46-
XX(URL_FLAGS_HAS_PATH, 0x100) \
47-
XX(URL_FLAGS_HAS_QUERY, 0x200) \
48-
XX(URL_FLAGS_HAS_FRAGMENT, 0x400) \
49-
XX(URL_FLAGS_IS_DEFAULT_SCHEME_PORT, 0x800) \
50-
51-
enum url_parse_state {
52-
kUnknownState = -1,
53-
#define XX(name) name,
54-
PARSESTATES(XX)
55-
#undef XX
56-
};
57-
58-
enum url_flags {
59-
#define XX(name, val) name = val,
60-
FLAGS(XX)
61-
#undef XX
62-
};
63-
64-
struct url_data {
65-
int32_t flags = URL_FLAGS_NONE;
66-
int port = -1;
67-
std::string scheme;
68-
std::string username;
69-
std::string password;
70-
std::string host;
71-
std::string query;
72-
std::string fragment;
73-
std::vector<std::string> path;
74-
std::string href;
75-
};
76-
77-
namespace table_data {
78-
extern const char hex[1024];
79-
extern const uint8_t C0_CONTROL_ENCODE_SET[32];
80-
extern const uint8_t FRAGMENT_ENCODE_SET[32];
81-
extern const uint8_t PATH_ENCODE_SET[32];
82-
extern const uint8_t USERINFO_ENCODE_SET[32];
83-
extern const uint8_t QUERY_ENCODE_SET_NONSPECIAL[32];
84-
extern const uint8_t QUERY_ENCODE_SET_SPECIAL[32];
85-
}
86-
87-
class URL {
88-
public:
89-
static void Parse(const char* input,
90-
size_t len,
91-
enum url_parse_state state_override,
92-
struct url_data* url,
93-
bool has_url,
94-
const struct url_data* base,
95-
bool has_base);
96-
97-
static std::string SerializeURL(const url_data& url, bool exclude);
98-
99-
URL(const char* input, const size_t len) {
100-
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
101-
}
102-
103-
URL(const char* input, const size_t len, const URL* base) {
104-
if (base != nullptr)
105-
Parse(input, len, kUnknownState,
106-
&context_, false,
107-
&(base->context_), true);
108-
else
109-
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
110-
}
111-
112-
URL(const char* input, const size_t len,
113-
const char* base, const size_t baselen) {
114-
if (base != nullptr && baselen > 0) {
115-
URL _base(base, baselen);
116-
Parse(input, len, kUnknownState,
117-
&context_, false,
118-
&(_base.context_), true);
119-
} else {
120-
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
121-
}
122-
}
123-
124-
explicit URL(const std::string& input) :
125-
URL(input.c_str(), input.length()) {}
126-
127-
URL(const std::string& input, const URL* base) :
128-
URL(input.c_str(), input.length(), base) {}
129-
130-
URL(const std::string& input, const URL& base) :
131-
URL(input.c_str(), input.length(), &base) {}
132-
133-
URL(const std::string& input, const std::string& base) :
134-
URL(input.c_str(), input.length(), base.c_str(), base.length()) {}
135-
136-
int32_t flags() const {
137-
return context_.flags;
138-
}
139-
140-
int port() const {
141-
return context_.port;
142-
}
143-
144-
const std::string& protocol() const {
145-
return context_.scheme;
146-
}
147-
148-
const std::string& username() const {
149-
return context_.username;
150-
}
151-
152-
const std::string& password() const {
153-
return context_.password;
154-
}
155-
156-
const std::string& host() const {
157-
return context_.host;
158-
}
159-
160-
const std::string& query() const {
161-
return context_.query;
162-
}
163-
164-
const std::string& fragment() const {
165-
return context_.fragment;
166-
}
167-
168-
std::string path() const {
169-
std::string ret;
170-
for (const std::string& element : context_.path) {
171-
ret += '/' + element;
172-
}
173-
return ret;
174-
}
175-
176-
std::string href() const {
177-
return SerializeURL(context_, false);
178-
}
179-
180-
// Get the file URL from native file system path.
181-
static URL FromFilePath(const std::string& file_path);
182-
183-
URL(const URL&) = default;
184-
URL& operator=(const URL&) = default;
185-
URL(URL&&) = default;
186-
URL& operator=(URL&&) = default;
187-
188-
URL() : URL("") {}
189-
190-
private:
191-
url_data context_;
192-
};
15+
std::string FromFilePath(const std::string_view file_path);
19316

19417
} // namespace url
195-
19618
} // namespace node
19719

19820
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

‎src/node_url_tables.cc

-448
This file was deleted.

‎test/benchmark/test-benchmark-url.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
4+
5+
// TODO(@anonrig): Remove this check when Ada removes ICU requirement.
6+
if (!common.hasIntl) {
7+
// A handful of the benchmarks fail when ICU is not included.
8+
// ICU is responsible for ignoring certain inputs from the hostname
9+
// and without it, it is not possible to validate the correctness of the input.
10+
// DomainToASCII method in Unicode specification states which characters are
11+
// ignored and/or remapped. Doing this outside of the scope of DomainToASCII,
12+
// would be a violation of the WHATWG URL specification.
13+
// Please look into: https://unicode.org/reports/tr46/#ProcessingStepMap
14+
common.skip('missing Intl');
15+
}
416

517
const runBenchmark = require('../common/benchmark');
618

‎test/cctest/test_url.cc

-188
This file was deleted.

‎test/fuzzers/fuzz_url.cc

-11
This file was deleted.

‎test/parallel/test-process-versions.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const expected_keys = [
1818
'uvwasi',
1919
'acorn',
2020
'simdutf',
21+
'ada',
2122
];
2223

2324
const hasUndici = process.config.variables.node_builtin_shareable_builtins.includes('deps/undici/undici.js');

‎test/parallel/test-whatwg-url-custom-inspect.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ assert.strictEqual(
4545
search: '?que=ry',
4646
searchParams: URLSearchParams { 'que' => 'ry' },
4747
hash: '#hash',
48-
cannotBeBase: false,
49-
special: true,
5048
[Symbol(context)]: URLContext {
51-
flags: 2032,
52-
scheme: 'https:',
49+
href: 'https://username:password@host.name:8080/path/name/?que=ry#hash',
50+
origin: 'https://host.name:8080',
51+
protocol: 'https:',
52+
host: 'host.name:8080',
53+
hostname: 'host.name',
54+
pathname: '/path/name/',
55+
search: '?que=ry',
5356
username: 'username',
5457
password: 'password',
55-
host: 'host.name',
56-
port: 8080,
57-
path: [ 'path', 'name', '', [length]: 3 ],
58-
query: 'que=ry',
59-
fragment: 'hash'
58+
port: '8080',
59+
hash: '#hash',
60+
hasHost: true,
61+
hasOpaquePath: false
6062
}
6163
}`);
6264

‎test/parallel/test-whatwg-url-properties.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4-
const { URL, URLSearchParams } = require('url');
4+
const { URL, URLSearchParams, format } = require('url');
55

66
[
77
{ name: 'toString' },
@@ -11,6 +11,17 @@ const { URL, URLSearchParams } = require('url');
1111
testMethod(URL.prototype, name);
1212
});
1313

14+
[
15+
'http://www.google.com',
16+
'https://www.domain.com:443',
17+
'file:///Users/yagiz/Developer/node',
18+
].forEach((url) => {
19+
const u = new URL(url);
20+
assert.strictEqual(JSON.stringify(u), `"${u.href}"`);
21+
assert.strictEqual(u.toString(), u.href);
22+
assert.strictEqual(format(u), u.href);
23+
});
24+
1425
[
1526
{ name: 'href' },
1627
{ name: 'protocol' },

0 commit comments

Comments
 (0)
Please sign in to comment.