-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathHMRSupport.mm
More file actions
325 lines (289 loc) · 11.8 KB
/
HMRSupport.mm
File metadata and controls
325 lines (289 loc) · 11.8 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "HMRSupport.h"
#import <Foundation/Foundation.h>
#include <algorithm>
#include <cctype>
#include <cstring>
#include "DevFlags.h"
#include <unordered_map>
#include <vector>
#include <string>
#include "Helpers.h"
// Use centralized dev flags helper for logging
namespace tns {
static inline bool StartsWith(const std::string& s, const char* prefix) {
size_t n = strlen(prefix);
return s.size() >= n && s.compare(0, n, prefix) == 0;
}
// Per-module hot data and callbacks. Keyed by canonical module path.
static std::unordered_map<std::string, v8::Global<v8::Object>> g_hotData;
static std::unordered_map<std::string, std::vector<v8::Global<v8::Function>>> g_hotAccept;
static std::unordered_map<std::string, std::vector<v8::Global<v8::Function>>> g_hotDispose;
v8::Local<v8::Object> GetOrCreateHotData(v8::Isolate* isolate, const std::string& key) {
auto it = g_hotData.find(key);
if (it != g_hotData.end()) {
if (!it->second.IsEmpty()) {
return it->second.Get(isolate);
}
}
v8::Local<v8::Object> obj = v8::Object::New(isolate);
g_hotData[key].Reset(isolate, obj);
return obj;
}
void RegisterHotAccept(v8::Isolate* isolate, const std::string& key, v8::Local<v8::Function> cb) {
if (cb.IsEmpty()) return;
g_hotAccept[key].emplace_back(v8::Global<v8::Function>(isolate, cb));
}
void RegisterHotDispose(v8::Isolate* isolate, const std::string& key, v8::Local<v8::Function> cb) {
if (cb.IsEmpty()) return;
g_hotDispose[key].emplace_back(v8::Global<v8::Function>(isolate, cb));
}
std::vector<v8::Local<v8::Function>> GetHotAcceptCallbacks(v8::Isolate* isolate, const std::string& key) {
std::vector<v8::Local<v8::Function>> out;
auto it = g_hotAccept.find(key);
if (it != g_hotAccept.end()) {
for (auto& gfn : it->second) {
if (!gfn.IsEmpty()) out.push_back(gfn.Get(isolate));
}
}
return out;
}
std::vector<v8::Local<v8::Function>> GetHotDisposeCallbacks(v8::Isolate* isolate, const std::string& key) {
std::vector<v8::Local<v8::Function>> out;
auto it = g_hotDispose.find(key);
if (it != g_hotDispose.end()) {
for (auto& gfn : it->second) {
if (!gfn.IsEmpty()) out.push_back(gfn.Get(isolate));
}
}
return out;
}
void InitializeImportMetaHot(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> importMeta,
const std::string& modulePath) {
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
// Ensure context scope for property creation
v8::HandleScope scope(isolate);
// Helper to capture key in function data
auto makeKeyData = [&](const std::string& key) -> Local<Value> {
return tns::ToV8String(isolate, key.c_str());
};
// accept([deps], cb?) — we register cb if provided; deps ignored for now
auto acceptCb = [](const FunctionCallbackInfo<Value>& info) {
v8::Isolate* iso = info.GetIsolate();
Local<Value> data = info.Data();
std::string key;
if (!data.IsEmpty()) {
v8::String::Utf8Value s(iso, data);
key = *s ? *s : "";
}
v8::Local<v8::Function> cb;
if (info.Length() >= 1 && info[0]->IsFunction()) {
cb = info[0].As<v8::Function>();
} else if (info.Length() >= 2 && info[1]->IsFunction()) {
cb = info[1].As<v8::Function>();
}
if (!cb.IsEmpty()) {
RegisterHotAccept(iso, key, cb);
}
// Return undefined
info.GetReturnValue().Set(v8::Undefined(iso));
};
// dispose(cb) — register disposer
auto disposeCb = [](const FunctionCallbackInfo<Value>& info) {
v8::Isolate* iso = info.GetIsolate();
Local<Value> data = info.Data();
std::string key;
if (!data.IsEmpty()) { v8::String::Utf8Value s(iso, data); key = *s ? *s : ""; }
if (info.Length() >= 1 && info[0]->IsFunction()) {
RegisterHotDispose(iso, key, info[0].As<v8::Function>());
}
info.GetReturnValue().Set(v8::Undefined(iso));
};
// decline() — mark declined (no-op for now)
auto declineCb = [](const FunctionCallbackInfo<Value>& info) {
info.GetReturnValue().Set(v8::Undefined(info.GetIsolate()));
};
// invalidate() — no-op for now
auto invalidateCb = [](const FunctionCallbackInfo<Value>& info) {
info.GetReturnValue().Set(v8::Undefined(info.GetIsolate()));
};
Local<Object> hot = Object::New(isolate);
// Stable flags
hot->CreateDataProperty(context, tns::ToV8String(isolate, "data"),
GetOrCreateHotData(isolate, modulePath)).Check();
hot->CreateDataProperty(context, tns::ToV8String(isolate, "prune"),
v8::Boolean::New(isolate, false)).Check();
// Methods
hot->CreateDataProperty(
context, tns::ToV8String(isolate, "accept"),
v8::Function::New(context, acceptCb, makeKeyData(modulePath)).ToLocalChecked()).Check();
hot->CreateDataProperty(
context, tns::ToV8String(isolate, "dispose"),
v8::Function::New(context, disposeCb, makeKeyData(modulePath)).ToLocalChecked()).Check();
hot->CreateDataProperty(
context, tns::ToV8String(isolate, "decline"),
v8::Function::New(context, declineCb, makeKeyData(modulePath)).ToLocalChecked()).Check();
hot->CreateDataProperty(
context, tns::ToV8String(isolate, "invalidate"),
v8::Function::New(context, invalidateCb, makeKeyData(modulePath)).ToLocalChecked()).Check();
// Attach to import.meta
importMeta->CreateDataProperty(
context, tns::ToV8String(isolate, "hot"),
hot).Check();
}
// ─────────────────────────────────────────────────────────────
// Dev HTTP loader helpers
std::string CanonicalizeHttpUrlKey(const std::string& url) {
if (!(StartsWith(url, "http://") || StartsWith(url, "https://"))) {
return url;
}
// Drop fragment entirely
size_t hashPos = url.find('#');
std::string noHash = (hashPos == std::string::npos) ? url : url.substr(0, hashPos);
// Locate path start and query start
size_t schemePos = noHash.find("://");
if (schemePos == std::string::npos) {
// Unexpected shape; fall back to removing whole query
size_t q = noHash.find('?');
return (q == std::string::npos) ? noHash : noHash.substr(0, q);
}
size_t pathStart = noHash.find('/', schemePos + 3);
if (pathStart == std::string::npos) {
// No path; nothing to normalize
return noHash;
}
size_t qPos = noHash.find('?', pathStart);
std::string originAndPath = (qPos == std::string::npos) ? noHash : noHash.substr(0, qPos);
std::string query = (qPos == std::string::npos) ? std::string() : noHash.substr(qPos + 1);
// Normalize bridge endpoints to keep a single realm across HMR updates:
// - /ns/rt/<ver> -> /ns/rt
// - /ns/core/<ver> -> /ns/core
// Preserve query params (e.g. /ns/core?p=...) as part of module identity.
{
std::string pathOnly = originAndPath.substr(pathStart);
auto normalizeBridge = [&](const char* needle) {
size_t nlen = strlen(needle);
if (pathOnly.compare(0, nlen, needle) != 0) return;
if (pathOnly.size() == nlen) return; // already canonical
if (pathOnly.size() <= nlen + 1 || pathOnly[nlen] != '/') return;
// Only normalize exact version segment: /ns/*/<digits> (no further segments)
size_t i = nlen + 1;
size_t j = i;
while (j < pathOnly.size() && std::isdigit(static_cast<unsigned char>(pathOnly[j]))) {
j++;
}
if (j == i) return; // no digits
if (j != pathOnly.size()) return; // has extra path
originAndPath = originAndPath.substr(0, pathStart) + std::string(needle);
pathOnly = originAndPath.substr(pathStart);
};
normalizeBridge("/ns/rt");
normalizeBridge("/ns/core");
}
if (query.empty()) return originAndPath;
// Keep all params except Vite's import marker; sort for stability.
std::vector<std::string> kept;
size_t start = 0;
while (start <= query.size()) {
size_t amp = query.find('&', start);
std::string pair = (amp == std::string::npos) ? query.substr(start) : query.substr(start, amp - start);
if (!pair.empty()) {
size_t eq = pair.find('=');
std::string name = (eq == std::string::npos) ? pair : pair.substr(0, eq);
if (!(name == "import")) kept.push_back(pair);
}
if (amp == std::string::npos) break;
start = amp + 1;
}
if (kept.empty()) return originAndPath;
std::sort(kept.begin(), kept.end());
std::string rebuilt = originAndPath + "?";
for (size_t i = 0; i < kept.size(); i++) {
if (i > 0) rebuilt += "&";
rebuilt += kept[i];
}
return rebuilt;
}
bool HttpFetchText(const std::string& url, std::string& out, std::string& contentType, int& status) {
// Security gate: check if remote module loading is allowed before any HTTP fetch.
// This is the single point of enforcement for all HTTP module loading.
if (!IsRemoteUrlAllowed(url)) {
status = 403; // Forbidden
if (IsScriptLoadingLogEnabled()) {
Log(@"[http-esm][security][blocked] %s", url.c_str());
}
return false;
}
@autoreleasepool {
NSURL* u = [NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]];
if (!u) { status = 0; return false; }
__block NSError* err = nil;
__block NSInteger httpStatusLocal = 0;
__block std::string contentTypeLocal;
__block std::string bodyLocal;
auto fetchOnce = ^BOOL(NSURL* reqUrl) {
bodyLocal.clear();
err = nil;
httpStatusLocal = 0;
contentTypeLocal.clear();
NSURLSessionConfiguration* cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
cfg.HTTPAdditionalHeaders = @{ @"Accept": @"application/javascript, text/javascript, */*;q=0.1",
@"Accept-Encoding": @"identity" };
// Note: this could be made configurable if needed
cfg.timeoutIntervalForRequest = 5.0;
cfg.timeoutIntervalForResource = 5.0;
NSURLSession* session = [NSURLSession sessionWithConfiguration:cfg];
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSURLSessionDataTask* task = [session dataTaskWithURL:reqUrl
completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
@autoreleasepool {
err = error;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
httpStatusLocal = ((NSHTTPURLResponse*)response).statusCode;
NSString* ct = ((NSHTTPURLResponse*)response).allHeaderFields[@"Content-Type"];
if (ct) { contentTypeLocal = std::string([ct UTF8String] ?: ""); }
}
if (data) {
const void* bytes = [data bytes];
NSUInteger len = [data length];
if (bytes && len > 0) {
bodyLocal.assign(static_cast<const char*>(bytes), static_cast<size_t>(len));
}
}
}
dispatch_semaphore_signal(sema);
}];
[task resume];
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC));
dispatch_semaphore_wait(sema, timeout);
[session finishTasksAndInvalidate];
return err == nil && !bodyLocal.empty();
};
BOOL ok = fetchOnce(u);
if (!ok) {
if (tns::IsScriptLoadingLogEnabled()) { Log(@"[http-loader] retrying %s after initial fetch error", url.c_str()); }
usleep(120 * 1000);
ok = fetchOnce(u);
}
status = (int)httpStatusLocal;
contentType = contentTypeLocal;
if (!ok || status < 200 || status >= 300) {
return false;
}
out.swap(bodyLocal);
if (out.empty()) return false;
if (tns::IsScriptLoadingLogEnabled()) {
unsigned long long blen = (unsigned long long)out.size();
const char* ctstr = contentType.empty() ? "<none>" : contentType.c_str();
Log(@"[http-loader] fetched status=%ld content-type=%s bytes=%llu", (long)status, ctstr, blen);
}
return true;
}
}
} // namespace tns