Repository navigation
Expand file tree
/
Copy pathImportMap.h
More file actions
98 lines (82 loc) · 4.01 KB
/
Copy pathImportMap.h
File metadata and controls
98 lines (82 loc) · 4.01 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
#ifndef NATIVESCRIPT_COMMON_LOADER_IMPORT_MAP_H_
#define NATIVESCRIPT_COMMON_LOADER_IMPORT_MAP_H_
#include <cstddef>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ns/http/ModuleFetch.h"
// The loader vocabulary: everything a dev client teaches one isolate's
// loader. Which specifiers an import map rewrites, how HTTP module URLs are
// keyed, and which URLs are never reused from the registry. Each isolate
// owns its copy; a worker starts from a copy of its parent's.
//
// V8-free: ParseImportMap (ns/loader/LoaderState.h) reads the JSON and builds
// a ParsedImportMap through AddImportMapEntry and FinishImportMap.
namespace nativescript::common::loader {
struct StringViewHash {
using is_transparent = void;
size_t operator()(std::string_view s) const {
return std::hash<std::string_view>()(s);
}
};
// One imports-shaped section: specifier key -> target.
struct ImportMapSection {
std::unordered_map<std::string, std::string, StringViewHash, std::equal_to<>>
entries;
// The entries whose key ends in '/', longest key first: the first one that
// prefixes a specifier is its longest match. Built by FinishImportMap.
std::vector<std::pair<std::string, std::string>> prefixes;
bool empty() const { return entries.empty(); }
};
struct ParsedImportMap {
ImportMapSection imports;
// By the referrer prefix each scope matches, most specific first.
std::vector<std::pair<std::string, ImportMapSection>> scopes;
bool empty() const { return imports.empty() && scopes.empty(); }
};
struct LoaderVocabulary {
ParsedImportMap importMap;
http::CanonicalizationConfig canonicalization;
// Whether a client supplied `canonicalization`. Supplied and empty is
// explicit policy, distinct from none (the mechanical canonicalization).
bool canonicalizationConfigured = false;
// URL substrings whose modules are requested again on every import instead
// of being reused from the registry.
std::vector<std::string> volatilePatterns;
};
// Validates one entry of a section and adds it; a later duplicate key
// replaces an earlier one. On a rejection, `error` names the section
// (`label`) and the key, and the section is unchanged.
bool AddImportMapEntry(ImportMapSection& section, std::string_view label,
std::string_view key, std::string_view target,
std::string* error);
// The referrer prefix a scope key matches. A referrer key of an http(s)
// module is spelled by platform::NormalizeHttpModuleUrl, so an http(s) scope
// key is spelled the same way, except that a key written with no path stays
// path-less: the parser's "/" would narrow it. Any other scope key is used as
// written.
std::string ImportMapScopePrefix(std::string_view scopeKey);
// Keeps one scope per prefix, the last one added, orders the scopes most
// specific first and indexes each section's prefix entries. Call once every
// entry is in.
void FinishImportMap(ParsedImportMap& map);
// `specifier` as the import map rewrites it for a module whose scope-matching
// spelling is `referrer` (its file path, or its key when it has none);
// nullopt when no entry applies. A scope matches a referrer equal to its
// prefix, or one below a prefix that ends in '/'; a path-less http(s) prefix
// (Q9) matches every referrer it prefixes. The most specific matching scope
// answers
// first, then less specific ones, then "imports"; within a section, an exact
// key, then the longest '/'-terminated key that prefixes the specifier, with
// the rest appended to its target.
std::optional<std::string> LookupImportMap(const ParsedImportMap& map,
std::string_view specifier,
std::string_view referrer);
// Whether `url` contains one of the volatile patterns.
bool IsVolatileUrl(const LoaderVocabulary& vocabulary, std::string_view url);
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_IMPORT_MAP_H_