Repository navigation
Expand file tree
/
Copy pathResolver.h
More file actions
93 lines (81 loc) · 3.96 KB
/
Copy pathResolver.h
File metadata and controls
93 lines (81 loc) · 3.96 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
#ifndef NATIVESCRIPT_COMMON_LOADER_RESOLVER_H_
#define NATIVESCRIPT_COMMON_LOADER_RESOLVER_H_
#include <string>
#include <string_view>
#include "ns/LoaderConfig.h"
#include "ns/loader/ImportMap.h"
// Module specifier resolution: one specifier, from one importer, to something
// the loader can act on. The static resolver, import() and the graph walk all
// resolve through here, so a module gets one registry key whichever of them
// reaches it first.
//
// V8-free. It consults the import map and probes files through
// platform::Stat; it never compiles, registers, fetches or throws.
namespace nativescript::common::loader {
struct ModuleResolution {
enum class Kind {
kUnresolved, // nothing locatable; the caller decides how to report it
kBuiltin, // ns: or node:, served by the builtin registry
kHttp, // an absolute http(s) URL
kFile, // a regular file, by its canonical path
kBlob, // a blob: URL; only import() serves one, from the blob store
};
Kind kind = Kind::kUnresolved;
std::string url; // kHttp; kBlob without its fragment
std::string path; // kFile: what is read, probed and code-cached
// kFile: the registry key, `path` plus `query`: one file imported with two
// queries is two modules.
std::string key;
std::string query; // kFile: ModuleQuery of the specifier
std::string specifier; // after the import map rewrote it
std::string attempted; // kUnresolved: the last candidate tried
// kUnresolved: when a rule refuses the specifier outright, the whole error
// message. Nothing was probed for it.
std::string blocked;
};
// "Cannot find module '<specifier>' (tried <attempted>)", or `blocked`.
std::string UnresolvedModuleMessage(const ModuleResolution& resolution);
enum class ImportMapLookup {
kApply,
// The specifier is an existing module's origin, which the map never
// rewrites.
kSkip,
};
// Any ns: or node: specifier, registered or not: the whole scheme is claimed,
// so a prefixed name can never reach the file system.
bool IsBuiltinModuleSpecifier(std::string_view specifier);
struct ResolveReferrer {
// The importer's registry key; empty when it is unknown (an import() with
// no compiled referrer).
std::string_view key;
// The file the importer was read from (the registry's record, or the key's
// KeyPath); empty when it is not a file module.
std::string_view path;
};
// Resolves `specifier`:
// - ns: and node: are builtins, before anything else.
// - The import map rewrites it next, unless `lookup` is kSkip. A target may
// name a builtin, and is never looked up again (A9).
// - A specifier that then holds a NUL resolves to nothing.
// - A blob: URL resolves as kBlob (A9).
// - An http(s) importer resolves as a browser does: relative forms against
// its URL, bare names only through the import map. It reaches files only
// through the map or a file: or app: URL.
// - An http(s) URL resolves as kHttp.
// - A file: or app: URL names the path FileUrlToPath gives (A3, A10, Q3, Q10);
// one it refuses resolves to nothing.
// - Otherwise a path: relative to the importer's directory, joined lexically
// (A1), and unresolved from an importer with no absolute path; "~/" under
// appRoot (then appRoot/app); a root-absolute path under appRoot unless it is
// already there (Q7), then without a leading /app; a bare name under appRoot.
// In those three forms a ".." never climbs above appRoot, as it never climbs
// above a URL's root. The query and fragment are cut first, and a specifier
// left empty resolves
// to nothing (Q8). Each candidate is probed as written, with .mjs and .js,
// then as a directory with index.mjs and index.js.
ModuleResolution ResolveSpecifier(
std::string_view specifier, const ResolveReferrer& referrer,
const LoaderVocabulary& vocabulary, const LoaderConfig& config,
ImportMapLookup lookup = ImportMapLookup::kApply);
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_RESOLVER_H_