Repository navigation
Expand file tree
/
Copy pathModuleIdentity.h
More file actions
122 lines (104 loc) · 6.16 KB
/
Copy pathModuleIdentity.h
File metadata and controls
122 lines (104 loc) · 6.16 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
#ifndef NATIVESCRIPT_COMMON_LOADER_MODULE_IDENTITY_H_
#define NATIVESCRIPT_COMMON_LOADER_MODULE_IDENTITY_H_
#include <optional>
#include <string>
#include <string_view>
#include "ns/LoaderConfig.h"
#include "ns/http/ModuleFetch.h"
// How a module is named: its registry key, the script origin V8 reports for
// it, its import.meta strings, and the path that an origin or a file: URL
// names. Every function reads only its arguments (and platform::ParseUrl), so
// it runs on any thread with whichever LoaderConfig the caller's isolate uses.
//
// A file module's registry key is its absolute path, lexically normalized and
// spelled under appRoot's spelling, followed by the query it was imported
// with (ModuleQuery). An http(s) module's key is its canonical URL; blob: and
// builtin modules are keyed by their specifier.
namespace nativescript::common::loader {
// `path` with empty and "." segments dropped, each ".." removing the segment
// before it, and no trailing slash. Lexical: it never consults the file
// system, so a symlink is not followed. A ".." never climbs above the root of
// an absolute path; a relative path keeps its leading "..".
std::string NormalizePath(std::string_view path);
// NormalizePath(path), moved from an appRootAliases spelling onto the one
// appRoot uses.
std::string CanonicalFilePath(std::string_view path,
const LoaderConfig& config);
// Whether `key` is spelled the way the loader keys an HTTP module: a lowercase
// `http://` or `https://` prefix. Deliberately not a URL parse: it gates the
// rule that an HTTP importer never reaches the file system implicitly, so a
// key the URL parser would reject must still count as HTTP.
bool IsHttpRegistryKey(std::string_view key);
// Whether `s` starts with a URL scheme and ':' (`node:fs`, `blob:…`,
// `http://…`) rather than being a file system path.
bool HasUrlScheme(std::string_view s);
// The query of `specifier` as the URL parser serializes it, '?' included and
// the fragment dropped: what a file module's key carries after its path. "?"
// for an empty query, which the URL standard keeps distinct from none; empty
// when there is no query. When the parser rejects the specifier, the text
// from its first '?' up to any '#', as written, so that two queries still
// name two modules.
std::string ModuleQuery(std::string_view specifier);
// The path part of a file module's key: everything before the first '?'.
// Wrong for a file whose name contains '?', which only the registry's
// key-to-path record gets right.
std::string_view KeyPath(std::string_view fileKey);
// The registry key that `key` names: an http(s) URL canonicalized through
// `vocabulary` (see http::CanonicalizeNormalizedHttpUrlKey); a file: or app:
// URL (either scheme in any case, with or without "//") as the path
// FileUrlToPath gives plus its ModuleQuery; a blob: URL or any other scheme
// verbatim; a path normalized and aliased, followed by the ModuleQuery of
// the text from its first '?'. An absolute spelling of a file gets the key
// ResolveSpecifier gives it. A file: or app: URL that names no path is kept
// verbatim.
std::string CanonicalizeModuleKey(
std::string_view key, const LoaderConfig& config,
const http::CanonicalizationConfig* vocabulary);
// The script origin of the file at `path` (absolute, canonical) imported with
// `query`: appRootOrigin plus the path relative to appRoot for a file under
// it, file:// plus the absolute path for any other. Paths are written
// verbatim under legacyOrigins and percent-encoded otherwise.
std::string ModuleOrigin(std::string_view path, std::string_view query,
const LoaderConfig& config);
// The URL a module or script is reported under, from its registry key: an
// http(s) URL as platform::NormalizeHttpModuleUrl spells it; any other
// string with a URL scheme unchanged; a file key as ModuleOrigin of its
// KeyPath and the rest of the key; an empty key as the app root's origin.
std::string ReportedModuleUrl(std::string_view key, const LoaderConfig& config);
struct ImportMetaStrings {
std::string url;
// Only for a module loaded from the file system.
std::optional<std::string> filename;
std::optional<std::string> dirname;
};
// import.meta of the module registered under `key`, whose file is at
// `filePath` (the registry's record; empty to read the key's KeyPath).
// - A key with a URL scheme (http(s), blob:, builtins) gets only `url`.
// - Under legacyOrigins, `url` is the origin and `dirname` the directory of
// the path the origin spells; there is no `filename`.
// - Otherwise `url` is the file's file: URL with the key's query, and
// `filename` and `dirname` are its real path and directory.
// An empty key stands for a module the registry does not know, and gets the
// app root's strings.
ImportMetaStrings ImportMetaFor(std::string_view key, std::string_view filePath,
const LoaderConfig& config);
// The canonical path that an origin ModuleOrigin wrote names, the query and
// fragment dropped; nullopt for a string it could not have written. Under
// legacyOrigins the path is read verbatim, so a '#' or '%' in a file name
// survives; otherwise it is percent-decoded. An app:/// or app://localhost/
// URL always names a path under appRoot; an app: URL whose path does not
// start with '/' names none.
std::optional<std::string> OriginToPath(std::string_view origin,
const LoaderConfig& config);
// The canonical path that a file: or app: URL from outside the runtime (an
// import specifier, an ns:module argument) names, parsed and percent-decoded
// by the URL parser: app:/// (or app://localhost/) URLs, appRootOrigin and
// virtualFileRoots name paths under appRoot, and any other file: URL names
// its absolute path.
// nullopt for another scheme, a URL the parser rejects, a host other than
// localhost, an app: URL whose path does not start with '/' (`app:x.mjs`),
// and an encoded '/', '\\' or NUL in the path.
std::optional<std::string> FileUrlToPath(std::string_view url,
const LoaderConfig& config);
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_MODULE_IDENTITY_H_