Repository navigation
Expand file tree
/
Copy pathModuleFetch.h
More file actions
161 lines (130 loc) · 7.88 KB
/
Copy pathModuleFetch.h
File metadata and controls
161 lines (130 loc) · 7.88 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
#ifndef NATIVESCRIPT_COMMON_HTTP_MODULE_FETCH_H_
#define NATIVESCRIPT_COMMON_HTTP_MODULE_FETCH_H_
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "ns/Deadline.h"
#include "ns/http/ModuleTransport.h"
// The HTTP module loader's policy: which URLs may be fetched, what a response
// means, and the module registry key of an HTTP URL. The transport is the
// platform's (ns/http/ModuleTransport.h).
namespace nativescript::common::http {
// ─── Responses ──────────────────────────────────────────────────────────────
enum class ModuleResponseKind {
kJavaScript,
kJson,
};
// One module fetch's verdict, the same for every fetch path and transport.
struct ModuleFetchResult {
bool ok = false;
int status = 0;
ModuleResponseKind kind = ModuleResponseKind::kJavaScript;
// An empty 2xx JavaScript body becomes the canonical empty module.
// Meaningful only when `ok`.
std::string body;
std::string contentType; // as received, parameters included
// Reader-facing explanation, non-empty exactly when `!ok`. It reaches the
// importer's rejection verbatim, so it names the URL and the cause.
std::string failureReason;
};
// ─── Remote-module gate ─────────────────────────────────────────────────────
// The allowlist the gate applies once remote modules are enabled.
struct RemoteModuleAllowlist {
// False only when `security.remoteModuleAllowlist` is absent, not an array,
// or an empty array: then every URL is allowed.
bool inForce = false;
// The usable items, each spelled the way the loader spells the URLs it
// fetches (NormalizeHttpModuleUrl) so path-scoped entries compare against
// resolved paths, or kept verbatim when it is not an http(s) URL. An item
// that is not a string, is empty or has no exact UTF-8 form is not here: it
// matches nothing but still keeps the list in force.
std::vector<std::string> entries;
};
// The allowlist for the raw `security.remoteModuleAllowlist` value: nullopt
// when it is absent or not an array, otherwise its items in order, each
// nullopt when it is not a string or has no exact UTF-8 form.
RemoteModuleAllowlist RemoteModuleAllowlistFromConfig(
const std::optional<std::vector<std::optional<std::string>>>& items);
// Whether `url` (as fetched) passes `allowlist`: always when it is not in
// force, otherwise when one entry is a prefix of it that ends at a
// URL-component boundary.
bool RemoteUrlAllowedByAllowlist(const std::string& url,
const RemoteModuleAllowlist& allowlist);
// ─── Registry keys ──────────────────────────────────────────────────────────
// Which query params are cache busters, which path prefixes are dev endpoints
// whose queries may be normalized, and which paths keep their query verbatim
// because the query is the identity. Supplied per isolate by the dev client
// (ns:module `configureLoader({ canonicalization })`).
struct CanonicalizationConfig {
std::vector<std::string> stripParams; // query param names to drop
std::vector<std::string> devPathPrefixes; // StartsWith → normalize query
std::vector<std::string> preserveQueryPrefixes; // contains → keep query
};
// The module registry key of a URL already spelled by NormalizeHttpModuleUrl.
// The fragment is always stripped. The query survives unless `vocabulary`
// says otherwise: a path containing a `preserveQueryPrefixes` entry keeps it
// verbatim; a path under a `devPathPrefixes` entry drops every `stripParams`
// name and sorts what remains. A null `vocabulary` keeps every query.
std::string CanonicalizeNormalizedHttpUrlKey(
const std::string& normalizedUrl, const CanonicalizationConfig* vocabulary);
// ─── Fetch concurrency ──────────────────────────────────────────────────────
// Which concurrency cap a module URL's fetches share when the transport
// blocks a thread per request.
// kLoopback: 127.0.0.1, [::1], localhost, or the development machine as seen
// from the Android emulator (10.0.2.2) or Genymotion (10.0.3.2). On a
// device that is almost always `adb reverse`, whose listener overflows
// under a burst of connects, each overflow costing a ~1 s SYN retry.
// kRemote: any other host, where a fetch is bound by its round trip and
// more connections in flight hide more of it.
enum class ModuleFetchHostClass { kLoopback, kRemote };
ModuleFetchHostClass ModuleFetchHostClassOf(const std::string& url);
const char* ModuleFetchHostClassName(ModuleFetchHostClass hostClass);
// The fetch thread cap for `hostClass`: the platform's configured value, or
// the default (loopback 6, remote 32). Unused while the transport schedules
// itself.
size_t ModuleFetchConcurrency(ModuleFetchHostClass hostClass);
// ─── Gate ───────────────────────────────────────────────────────────────────
// Debug builds always allow remote modules; release builds only when the app
// config enables them.
bool IsRemoteModulesAllowed();
// Whether `url` (NormalizeHttpModuleUrl spelling) may be fetched: in debug
// builds always, otherwise when remote modules are enabled and the allowlist
// passes it.
bool IsRemoteUrlAllowed(const std::string& url);
// ─── Fetching ───────────────────────────────────────────────────────────────
//
// `url` is gated, fetched and reported in NormalizeHttpModuleUrl spelling;
// nothing here canonicalizes. Both primitives judge responses alike and share
// the cache. Either retries once, only for a transport error and never for an
// HTTP status.
// Blocks the calling thread for one module. The wait is bounded by the
// attempt timeout plus a margin when the transport can cancel an attempt.
// Returns `result.ok`.
bool HttpFetchModule(const std::string& url, ModuleFetchResult& result);
// The graph load's fetch budget; nullopt is unbounded. Each attempt's timeout
// is clamped to what remains when it starts, so time spent queued counts, and
// no attempt starts past it. A response that lands late is still reported.
using ModuleFetchDeadline = Deadline;
// Never blocks. `completion` runs exactly once, on any thread, possibly
// before this returns (a blocked URL, a deadline already passed). It must not
// touch the engine.
void FetchModuleBodyAsync(const std::string& url, ModuleFetchDeadline deadline,
std::function<void(ModuleFetchResult)> completion);
// ─── HTTP cache ─────────────────────────────────────────────────────────────
//
// Process-wide, from any thread. Each fetch reads the switch once, when it is
// issued, so a change never affects a fetch already in flight.
// The setting, and no holder disabling it.
bool ModuleHttpCacheEnabled();
void SetModuleHttpCacheEnabled(bool enabled);
// Holders (an inspector connection) disable the cache for as long as any
// holds it. Idempotent per id; a holder going away passes false.
void SetModuleHttpCacheDisabledBy(uint64_t holderId, bool disabled);
// Everything the module cache stores, durably before returning.
CacheOpResult ClearModuleHttpCache();
} // namespace nativescript::common::http
#endif // NATIVESCRIPT_COMMON_HTTP_MODULE_FETCH_H_