Skip to content
File

Blob: src/workerd/api/urlpattern.h

cpp124 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5#pragma once
6 
7#include <workerd/jsg/jsg.h>
8#include <workerd/jsg/url.h>
9 
10namespace workerd::api {
11 
12#define URL_PATTERN_COMPONENTS(V) \
13 V(Protocol, protocol) \
14 V(Username, username) \
15 V(Password, password) \
16 V(Hostname, hostname) \
17 V(Port, port) \
18 V(Pathname, pathname) \
19 V(Search, search) \
20 V(Hash, hash)
21 
22// URLPattern is a Web Platform standard API for matching URLs against a
23// pattern syntax (think of it as a regular expression for URLs). It is
24// defined in https://wicg.github.io/urlpattern.
25// More information about the URL Pattern syntax can be found at
26// https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
27class URLPattern final: public jsg::Object {
28 public:
29 // A structure providing matching patterns for individual components
30 // of a URL. When a URLPattern is created, or when a URLPattern is
31 // used to match or test against a URL, the input can be given as
32 // either a string or a URLPatternInit struct. If a string is given,
33 // it will be parsed to create a URLPatternInit. The URLPatternInit
34 // API is defined as part of the URLPattern specification.
35 struct URLPatternInit final {
36#define V(_, name) jsg::Optional<kj::String> name;
37 URL_PATTERN_COMPONENTS(V)
38#undef V
39 jsg::Optional<kj::String> baseURL;
40 
41 JSG_STRUCT(protocol, username, password, hostname, port, pathname, search, hash, baseURL);
42 
43 operator jsg::UrlPattern::Init();
44 };
45 
46 using URLPatternInput = kj::OneOf<kj::String, URLPatternInit>;
47 
48 // A struct providing the URLPattern matching results for a single
49 // URL component. The URLPatternComponentResult is only ever used
50 // as a member attribute of a URLPatternResult struct. The
51 // URLPatternComponentResult API is defined as part of the URLPattern
52 // specification.
53 struct URLPatternComponentResult final {
54 kj::String input;
55 jsg::Dict<kj::String, kj::String> groups;
56 
57 JSG_STRUCT(input, groups);
58 };
59 
60 // A struct providing the URLPattern matching results for all
61 // components of a URL. The URLPatternResult API is defined as
62 // part of the URLPattern specification.
63 struct URLPatternResult final {
64 kj::Array<URLPatternInput> inputs;
65#define V(_, name) URLPatternComponentResult name;
66 URL_PATTERN_COMPONENTS(V)
67#undef V
68 
69 JSG_STRUCT(inputs, protocol, username, password, hostname, port, pathname, search, hash);
70 };
71 
72 struct URLPatternOptions final {
73 jsg::Optional<bool> ignoreCase;
74 
75 JSG_STRUCT(ignoreCase);
76 };
77 
78 explicit URLPattern(jsg::UrlPattern inner
79#define V(_, name) , jsg::JsRef<jsg::JsRegExp> name##Regex
80 URL_PATTERN_COMPONENTS(V)
81#undef V
82 );
83 
84 static jsg::Ref<URLPattern> constructor(jsg::Lock& js,
85 jsg::Optional<URLPatternInput> input,
86 jsg::Optional<kj::OneOf<kj::String, URLPatternOptions>> baseURL,
87 jsg::Optional<URLPatternOptions> patternOptions);
88 
89 kj::Maybe<URLPatternResult> exec(
90 jsg::Lock& js, jsg::Optional<URLPatternInput> input, jsg::Optional<kj::String> baseURL);
91 
92 bool test(jsg::Lock& js, jsg::Optional<URLPatternInput> input, jsg::Optional<kj::String> baseURL);
93 
94#define V(name, _) kj::StringPtr get##name();
95 URL_PATTERN_COMPONENTS(V)
96#undef V
97 
98 JSG_RESOURCE_TYPE(URLPattern) {
99#define V(Name, name) JSG_READONLY_PROTOTYPE_PROPERTY(name, get##Name);
100 URL_PATTERN_COMPONENTS(V)
101#undef V
102 JSG_METHOD(test);
103 JSG_METHOD(exec);
104 }
105 
106 void visitForMemoryInfo(jsg::MemoryTracker& tracker) const {
107 tracker.trackField("inner", inner);
108 }
109 
110 private:
111 jsg::UrlPattern inner;
112#define V(_, name) jsg::JsRef<jsg::JsRegExp> name##Regex;
113 URL_PATTERN_COMPONENTS(V)
114#undef V
115 
116 void visitForGc(jsg::GcVisitor& visitor);
117};
118 
119#define EW_URLPATTERN_ISOLATE_TYPES \
120 api::URLPattern, api::URLPattern::URLPatternInit, api::URLPattern::URLPatternComponentResult, \
121 api::URLPattern::URLPatternResult, api::URLPattern::URLPatternOptions
122 
123} // namespace workerd::api