-
Notifications
You must be signed in to change notification settings - Fork 31.1k
/
Copy pathnode_url_pattern.h
118 lines (99 loc) Β· 4.35 KB
/
node_url_pattern.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef SRC_NODE_URL_PATTERN_H_
#define SRC_NODE_URL_PATTERN_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "ada.h"
#include "base_object.h"
#include "util.h"
#include <v8.h>
#include <optional>
#include <string_view>
namespace node::url_pattern {
#define URL_PATTERN_COMPONENTS(V) \
V(Protocol, protocol) \
V(Username, username) \
V(Password, password) \
V(Hostname, hostname) \
V(Port, port) \
V(Pathname, pathname) \
V(Search, search) \
V(Hash, hash)
// By default, ada::url_pattern doesn't ship with any regex library.
// Ada has a std::regex implementation, but it is considered unsafe and does
// not have a fully compliant ecmascript syntax support. Therefore, Ada
// supports passing custom regex provider that conforms to the following
// class and function structure. For more information, please look into
// url_pattern_regex.h inside github.com/ada-url/ada.
class URLPatternRegexProvider {
public:
using regex_type = v8::Global<v8::RegExp>;
static std::optional<regex_type> create_instance(std::string_view pattern,
bool ignore_case);
static std::optional<std::vector<std::optional<std::string>>> regex_search(
std::string_view input, const regex_type& pattern);
static bool regex_match(std::string_view input, const regex_type& pattern);
};
class URLPattern : public BaseObject {
public:
URLPattern(Environment* env,
v8::Local<v8::Object> object,
ada::url_pattern<URLPatternRegexProvider>&& url_pattern);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
// V8 APIs
// - Functions
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& info);
static void Test(const v8::FunctionCallbackInfo<v8::Value>& info);
// - Component Getters
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
static void name(const v8::FunctionCallbackInfo<v8::Value>& info);
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
// - Has Regexp Groups
static void HasRegexpGroups(const v8::FunctionCallbackInfo<v8::Value>& info);
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(URLPattern)
SET_SELF_SIZE(URLPattern)
class URLPatternInit {
public:
static std::optional<ada::url_pattern_init> FromJsObject(
Environment* env, v8::Local<v8::Object> obj);
static v8::MaybeLocal<v8::Value> ToJsObject(
Environment* env, const ada::url_pattern_init& init);
};
class URLPatternOptions {
public:
static std::optional<ada::url_pattern_options> FromJsObject(
Environment* env, v8::Local<v8::Object> obj);
};
class URLPatternResult {
public:
static v8::MaybeLocal<v8::Value> ToJSValue(
Environment* env, const ada::url_pattern_result& result);
};
class URLPatternComponentResult {
public:
static v8::MaybeLocal<v8::Object> ToJSObject(
Environment* env, const ada::url_pattern_component_result& result);
};
private:
ada::url_pattern<URLPatternRegexProvider> url_pattern_;
// Getter methods
#define URL_PATTERN_COMPONENT_GETTERS(name, _) v8::MaybeLocal<v8::Value> name();
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
#undef URL_PATTERN_COMPONENT_GETTERS
bool HasRegExpGroups() const;
// Public API
v8::MaybeLocal<v8::Value> Exec(
Environment* env,
const ada::url_pattern_input& input,
std::optional<std::string_view>& baseURL); // NOLINT (runtime/references)
bool Test(
Environment* env,
const ada::url_pattern_input& input,
std::optional<std::string_view>& baseURL); // NOLINT (runtime/references)
#define URL_PATTERN_CACHED_VALUES(_, name) v8::Global<v8::Value> name;
URL_PATTERN_COMPONENTS(URL_PATTERN_CACHED_VALUES)
#undef URL_PATTERN_CACHED_VALUES
};
} // namespace node::url_pattern
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_URL_PATTERN_H_