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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use cow_utils::CowUtils;
use rustc_hash::{FxHashMap, FxHashSet};
use std::sync::Arc;
use tokio::sync::{Mutex, mpsc};
use tracing::error;
use crate::runtime::JsExecutionRuntime;
use super::constants::PROMISE_RESOLUTION_SCRIPT;
use super::types::{BoundaryError, BoundaryUpdate, PendingSuspensePromise, RscWireFormatTag};
pub(super) fn process_client_components(
content: &mut serde_json::Value,
row_counter: &mut u32,
) -> Vec<String> {
let mut import_rows = Vec::new();
let mut component_map: FxHashMap<String, String> = FxHashMap::default();
collect_client_components(content, &mut component_map, row_counter, &mut import_rows);
replace_client_component_paths(content, &component_map);
import_rows
}
fn collect_client_components(
value: &serde_json::Value,
component_map: &mut FxHashMap<String, String>,
row_counter: &mut u32,
import_rows: &mut Vec<String>,
) {
match value {
serde_json::Value::Array(arr) => {
if arr.len() >= 4
&& arr[0].as_str() == Some("$")
&& let Some(type_str) = arr[1].as_str()
&& (type_str.contains('/') || type_str.contains('\\') || type_str.contains('#'))
&& !type_str.starts_with("$L")
{
let normalized_type_str = type_str.cow_replace('\\', "/");
let type_str_normalized = normalized_type_str.as_ref();
if !component_map.contains_key(type_str)
&& !component_map.contains_key(type_str_normalized)
{
*row_counter += 1;
let module_ref = format!("$L{:x}", row_counter);
let (file_path, export_name) = if let Some(idx) = type_str_normalized.find('#')
{
(&type_str_normalized[..idx], &type_str_normalized[idx + 1..])
} else {
(type_str_normalized, "default")
};
#[allow(clippy::disallowed_methods)]
let import_data = serde_json::json!({
"id": file_path,
"chunks": [],
"name": export_name
});
let import_row = RscWireFormatTag::ModuleImport
.format_row(*row_counter, &import_data.to_string());
import_rows.push(import_row.trim_end().to_string());
component_map.insert(type_str.to_string(), module_ref.clone());
component_map.insert(type_str_normalized.to_string(), module_ref);
}
}
for item in arr {
collect_client_components(item, component_map, row_counter, import_rows);
}
}
serde_json::Value::Object(obj) => {
for value in obj.values() {
collect_client_components(value, component_map, row_counter, import_rows);
}
}
_ => {}
}
}
fn replace_client_component_paths(
value: &mut serde_json::Value,
component_map: &FxHashMap<String, String>,
) {
match value {
serde_json::Value::Array(arr) => {
if arr.len() >= 4
&& arr[0].as_str() == Some("$")
&& let Some(type_str) = arr[1].as_str()
{
let module_ref = component_map.get(type_str).or_else(|| {
let normalized = type_str.cow_replace('\\', "/");
component_map.get(normalized.as_ref())
});
if let Some(module_ref) = module_ref {
arr[1] = serde_json::Value::String(module_ref.clone());
}
}
for item in arr {
replace_client_component_paths(item, component_map);
}
}
serde_json::Value::Object(obj) => {
for value in obj.values_mut() {
replace_client_component_paths(value, component_map);
}
}
_ => {}
}
}
pub struct BackgroundPromiseResolver {
runtime: Arc<JsExecutionRuntime>,
active_promises: Arc<Mutex<FxHashMap<String, PendingSuspensePromise>>>,
update_sender: mpsc::UnboundedSender<BoundaryUpdate>,
error_sender: mpsc::UnboundedSender<BoundaryError>,
shared_row_counter: Arc<Mutex<u32>>,
promise_to_row: Arc<Mutex<FxHashMap<String, u32>>>,
}
impl BackgroundPromiseResolver {
pub fn new(
runtime: Arc<JsExecutionRuntime>,
update_sender: mpsc::UnboundedSender<BoundaryUpdate>,
error_sender: mpsc::UnboundedSender<BoundaryError>,
shared_row_counter: Arc<Mutex<u32>>,
promise_to_row: Arc<Mutex<FxHashMap<String, u32>>>,
) -> Self {
Self {
runtime,
active_promises: Arc::new(Mutex::new(FxHashMap::default())),
update_sender,
error_sender,
shared_row_counter,
promise_to_row,
}
}
pub fn resolve_async(&self, promise: PendingSuspensePromise) {
self.resolve_all(vec![promise]);
}
pub fn resolve_all(&self, promises: Vec<PendingSuspensePromise>) {
if promises.is_empty() {
return;
}
let runtime = Arc::clone(&self.runtime);
let update_sender = self.update_sender.clone();
let error_sender = self.error_sender.clone();
let shared_row_counter = Arc::clone(&self.shared_row_counter);
let active_promises = Arc::clone(&self.active_promises);
let promise_to_row_map = Arc::clone(&self.promise_to_row);
tokio::spawn(async move {
{
let mut active = active_promises.lock().await;
for p in &promises {
active.insert(p.id.clone(), p.clone());
}
}
let scripts: Vec<(String, String)> = promises
.iter()
.map(|p| {
let script = PROMISE_RESOLUTION_SCRIPT
.cow_replace("{promise_id}", &p.id)
.cow_replace("{boundary_id}", &p.boundary_id)
.cow_replace("{component_path}", &p.component_path)
.cow_replace("{render_generation}", &p.render_generation.to_string())
.into_owned();
(format!("<promise_resolution_{}>", p.id), script)
})
.collect();
let mut result_rx = runtime.execute_script_batch(scripts).await;
let n = promises.len();
let mut received = 0;
let mut received_indices = FxHashSet::default();
while received < n {
match result_rx.recv().await {
Some((idx, result)) => {
if idx >= promises.len() || !received_indices.insert(idx) {
error!("Ignoring invalid or duplicate batch result index {}", idx);
continue;
}
received += 1;
let promise = &promises[idx];
let promise_id = &promise.id;
let boundary_id = &promise.boundary_id;
match result {
Ok(result_val) => {
let result_string = result_val.to_string();
match serde_json::from_str::<serde_json::Value>(&result_string) {
Ok(result_data) => {
if result_data
.get("stale")
.and_then(|v| v.as_bool())
.unwrap_or(false)
{
continue;
}
if result_data["success"].as_bool().unwrap_or(false) {
let mut content = result_data["content"].clone();
let row_id = {
let maybe_row = {
let map = promise_to_row_map.lock().await;
map.get(promise_id).copied()
};
if let Some(id) = maybe_row {
id
} else {
let mut counter =
shared_row_counter.lock().await;
*counter += 1;
*counter
}
};
let import_rows = {
let mut counter = shared_row_counter.lock().await;
process_client_components(
&mut content,
&mut counter,
)
};
let update = BoundaryUpdate {
boundary_id: boundary_id.clone(),
content,
row_id,
dom_path: Vec::new(),
import_rows,
};
if let Err(e) = update_sender.send(update) {
error!(
"Failed to send boundary update for {}: {}",
boundary_id, e
);
}
} else {
let error_message = result_data["error"]
.as_str()
.unwrap_or("Unknown error");
let error_name = result_data["errorName"]
.as_str()
.unwrap_or("UnknownError");
let error_stack = result_data["errorStack"]
.as_str()
.unwrap_or("No stack trace");
let error_context = &result_data["errorContext"];
error!(
"Promise resolution failed for boundary {}: {} (Name: {}, Phase: {}, Component: {}, Promise: {}, Stack: {})",
boundary_id,
error_message,
error_name,
error_context["phase"]
.as_str()
.unwrap_or("unknown"),
error_context["componentPath"]
.as_str()
.unwrap_or("unknown"),
error_context["promiseId"]
.as_str()
.unwrap_or("unknown"),
error_stack
);
let row_id = {
let mut counter = shared_row_counter.lock().await;
*counter += 1;
*counter
};
if let Err(e) = error_sender.send(BoundaryError {
boundary_id: boundary_id.clone(),
error_message: error_message.to_string(),
row_id,
}) {
error!(
"Failed to send boundary error for {}: {}",
boundary_id, e
);
}
}
}
Err(e) => {
error!(
"Failed to parse promise resolution result for {}: {} - Raw: {}",
boundary_id, e, result_string
);
let row_id = {
let mut counter = shared_row_counter.lock().await;
*counter += 1;
*counter
};
if let Err(e) = error_sender.send(BoundaryError {
boundary_id: boundary_id.clone(),
error_message: format!(
"Failed to parse promise result: {}",
e
),
row_id,
}) {
error!(
"Failed to send boundary error for {}: {}",
boundary_id, e
);
}
}
}
}
Err(e) => {
error!(
"Failed to execute promise resolution script for boundary {}: {}",
boundary_id, e
);
let row_id = {
let mut counter = shared_row_counter.lock().await;
*counter += 1;
*counter
};
if let Err(e) = error_sender.send(BoundaryError {
boundary_id: boundary_id.clone(),
error_message: format!("Failed to execute promise: {}", e),
row_id,
}) {
error!(
"Failed to send boundary error for {}: {}",
boundary_id, e
);
}
}
}
}
None => break,
}
}
if received < n {
let mut counter = shared_row_counter.lock().await;
for (idx, promise) in promises.iter().enumerate() {
if !received_indices.contains(&idx) {
*counter += 1;
if let Err(e) = error_sender.send(BoundaryError {
boundary_id: promise.boundary_id.clone(),
error_message: "Promise channel closed before result arrived"
.to_string(),
row_id: *counter,
}) {
error!(
"Failed to send boundary error for {}: {}",
promise.boundary_id, e
);
}
}
}
}
{
let mut active = active_promises.lock().await;
for p in &promises {
active.remove(&p.id);
}
}
});
}
pub async fn active_count(&self) -> usize {
self.active_promises.lock().await.len()
}
}