Repository navigation
Expand file tree
/
Copy pathImportMap.cpp
More file actions
180 lines (163 loc) · 6.37 KB
/
Copy pathImportMap.cpp
File metadata and controls
180 lines (163 loc) · 6.37 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "ns/loader/ImportMap.h"
#include <algorithm>
#include "ns/Platform.h"
#include "ns/Trace.h"
#include "ns/loader/ModuleIdentity.h"
namespace nativescript::common::loader {
namespace {
using platform::TraceCategory;
std::optional<std::string> LookupInSection(const ImportMapSection& section,
std::string_view specifier) {
auto exact = section.entries.find(specifier);
if (exact != section.entries.end()) {
NS_TRACE(TraceCategory::kEsm, "[import-map] exact: %.*s -> %s",
static_cast<int>(specifier.size()), specifier.data(),
exact->second.c_str());
return exact->second;
}
for (const auto& [key, target] : section.prefixes) {
if (specifier.size() > key.size() &&
specifier.compare(0, key.size(), key) == 0) {
std::string resolved = target;
resolved.append(specifier.substr(key.size()));
NS_TRACE(TraceCategory::kEsm, "[import-map] prefix: %.*s -> %s (via %s)",
static_cast<int>(specifier.size()), specifier.data(),
resolved.c_str(), key.c_str());
return resolved;
}
}
return std::nullopt;
}
void IndexPrefixes(ImportMapSection& section) {
section.prefixes.clear();
for (const auto& [key, target] : section.entries) {
if (key.back() == '/') {
section.prefixes.emplace_back(key, target);
}
}
std::sort(section.prefixes.begin(), section.prefixes.end(),
[](const auto& a, const auto& b) {
if (a.first.size() != b.first.size()) {
return a.first.size() > b.first.size();
}
return a.first < b.first;
});
}
// An http(s) scope prefix that ImportMapScopePrefix left with no path (Q9).
// Its http(s) prefixes are spelled as registry keys are.
bool IsPathlessHttpPrefix(std::string_view prefix) {
return IsHttpRegistryKey(prefix) &&
prefix.find('/', prefix.find("://") + 3) == std::string_view::npos;
}
// The import-map spec's scope match: the referrer itself, or below a
// '/'-terminated prefix. A path-less http(s) prefix (Q9) matches as a prefix
// too.
bool ScopeMatches(std::string_view prefix, std::string_view referrer) {
if (referrer == prefix) {
return true;
}
return !prefix.empty() && referrer.size() > prefix.size() &&
referrer.compare(0, prefix.size(), prefix) == 0 &&
(prefix.back() == '/' || IsPathlessHttpPrefix(prefix));
}
} // namespace
bool AddImportMapEntry(ImportMapSection& section, std::string_view label,
std::string_view key, std::string_view target,
std::string* error) {
const std::string specifier(key);
if (specifier.empty()) {
*error = std::string(label) + ": a specifier key must not be empty";
return false;
}
if (target.empty()) {
*error = std::string(label) + ": the target for '" + specifier +
"' must not be empty";
return false;
}
// Targets reach C-string path and URL APIs, which would end them there.
if (target.find('\0') != std::string_view::npos) {
*error = std::string(label) + ": the target for '" + specifier +
"' must not contain U+0000";
return false;
}
// A '/'-terminated key maps a subtree, and the rest of the specifier is
// appended to its target, which must therefore name a directory too.
if (specifier.back() == '/' && target.back() != '/') {
*error = std::string(label) + ": the target for '" + specifier +
"' must end with '/' because the specifier key does";
return false;
}
section.entries.insert_or_assign(specifier, std::string(target));
return true;
}
std::string ImportMapScopePrefix(std::string_view scopeKey) {
std::string prefix = platform::NormalizeHttpModuleUrl(std::string(scopeKey));
if (prefix.empty()) {
return std::string(scopeKey);
}
const size_t colon = scopeKey.find(':');
const size_t authority = colon == std::string_view::npos
? std::string_view::npos
: scopeKey.find_first_not_of("/\\", colon + 1);
const bool writtenWithoutPath =
authority != std::string_view::npos &&
scopeKey.find_first_of("/\\?#", authority) == std::string_view::npos;
if (writtenWithoutPath && prefix.back() == '/') {
prefix.pop_back();
}
return prefix;
}
void FinishImportMap(ParsedImportMap& map) {
// Scope keys spelled differently can name one prefix: the last one
// written replaces the others, as a later duplicate key does in JSON.
std::vector<std::pair<std::string, ImportMapSection>> scopes;
scopes.reserve(map.scopes.size());
for (auto it = map.scopes.rbegin(); it != map.scopes.rend(); ++it) {
const bool replaced =
std::any_of(scopes.begin(), scopes.end(),
[&](const auto& kept) { return kept.first == it->first; });
if (!replaced) {
scopes.push_back(std::move(*it));
}
}
map.scopes = std::move(scopes);
IndexPrefixes(map.imports);
for (auto& [prefix, section] : map.scopes) {
IndexPrefixes(section);
}
// A longer prefix is the more specific scope; the key keeps equal lengths
// in a fixed order.
std::stable_sort(map.scopes.begin(), map.scopes.end(),
[](const auto& a, const auto& b) {
if (a.first.size() != b.first.size()) {
return a.first.size() > b.first.size();
}
return a.first > b.first;
});
}
std::optional<std::string> LookupImportMap(const ParsedImportMap& map,
std::string_view specifier,
std::string_view referrer) {
for (const auto& [prefix, section] : map.scopes) {
if (!ScopeMatches(prefix, referrer)) {
continue;
}
std::optional<std::string> mapped = LookupInSection(section, specifier);
if (mapped) {
NS_TRACE(TraceCategory::kEsm,
"[import-map] scope '%s' matched referrer %.*s", prefix.c_str(),
static_cast<int>(referrer.size()), referrer.data());
return mapped;
}
}
return LookupInSection(map.imports, specifier);
}
bool IsVolatileUrl(const LoaderVocabulary& vocabulary, std::string_view url) {
for (const std::string& pattern : vocabulary.volatilePatterns) {
if (url.find(pattern) != std::string_view::npos) {
return true;
}
}
return false;
}
} // namespace nativescript::common::loader