File size: 13,250 Bytes
1e92f2d |
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 377 378 379 380 381 382 383 384 385 386 387 388 389 |
use std::{ops::Deref, sync::Arc};
use anyhow::Result;
use futures_util::TryFutureExt;
use napi::{JsFunction, bindgen_prelude::External};
use next_api::{
module_graph_snapshot::{ModuleGraphSnapshot, get_module_graph_snapshot},
operation::OptionEndpoint,
paths::ServerPath,
route::{
Endpoint, EndpointOutputPaths, endpoint_client_changed_operation,
endpoint_server_changed_operation, endpoint_write_to_disk_operation,
},
};
use tracing::Instrument;
use turbo_tasks::{
Completion, Effects, OperationVc, ReadRef, TryFlatJoinIterExt, TryJoinIterExt, Vc,
};
use turbopack_core::{diagnostics::PlainDiagnostic, error::PrettyPrintError, issue::PlainIssue};
use super::utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult,
strongly_consistent_catch_collectables, subscribe,
};
use crate::next_api::module_graph::NapiModuleGraphSnapshot;
#[napi(object)]
#[derive(Default)]
pub struct NapiEndpointConfig {}
#[napi(object)]
#[derive(Default)]
pub struct NapiServerPath {
pub path: String,
pub content_hash: String,
}
impl From<ServerPath> for NapiServerPath {
fn from(server_path: ServerPath) -> Self {
Self {
path: server_path.path,
content_hash: format!("{:x}", server_path.content_hash),
}
}
}
#[napi(object)]
#[derive(Default)]
pub struct NapiWrittenEndpoint {
pub r#type: String,
pub entry_path: Option<String>,
pub client_paths: Vec<String>,
pub server_paths: Vec<NapiServerPath>,
pub config: NapiEndpointConfig,
}
impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
fn from(written_endpoint: Option<EndpointOutputPaths>) -> Self {
match written_endpoint {
Some(EndpointOutputPaths::NodeJs {
server_entry_path,
server_paths,
client_paths,
}) => Self {
r#type: "nodejs".to_string(),
entry_path: Some(server_entry_path),
client_paths: client_paths.into_iter().map(From::from).collect(),
server_paths: server_paths.into_iter().map(From::from).collect(),
..Default::default()
},
Some(EndpointOutputPaths::Edge {
server_paths,
client_paths,
}) => Self {
r#type: "edge".to_string(),
client_paths: client_paths.into_iter().map(From::from).collect(),
server_paths: server_paths.into_iter().map(From::from).collect(),
..Default::default()
},
Some(EndpointOutputPaths::NotFound) | None => Self {
r#type: "none".to_string(),
..Default::default()
},
}
}
}
#[napi(object)]
pub struct NapiModuleGraphSnapshots {
pub module_graphs: Vec<NapiModuleGraphSnapshot>,
}
// NOTE(alexkirsz) We go through an extra layer of indirection here because of
// two factors:
// 1. rustc currently has a bug where using a dyn trait as a type argument to
// some async functions (in this case `endpoint_write_to_disk`) can cause
// higher-ranked lifetime errors. See https://github.com/rust-lang/rust/issues/102211
// 2. the type_complexity clippy lint.
pub struct ExternalEndpoint(pub DetachedVc<OptionEndpoint>);
impl Deref for ExternalEndpoint {
type Target = DetachedVc<OptionEndpoint>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[turbo_tasks::value(serialization = "none")]
struct WrittenEndpointWithIssues {
written: Option<ReadRef<EndpointOutputPaths>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn get_written_endpoint_with_issues_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<WrittenEndpointWithIssues>> {
let write_to_disk_op = endpoint_write_to_disk_operation(endpoint_op);
let (written, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(write_to_disk_op).await?;
Ok(WrittenEndpointWithIssues {
written,
issues,
diagnostics,
effects,
}
.cell())
}
#[napi]
#[tracing::instrument(skip_all)]
pub async fn endpoint_write_to_disk(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> {
let ctx = endpoint.turbopack_ctx();
let endpoint_op = ***endpoint;
let (written, issues, diags) = endpoint
.turbopack_ctx()
.turbo_tasks()
.run_once(async move {
let written_entrypoint_with_issues_op =
get_written_endpoint_with_issues_operation(endpoint_op);
let WrittenEndpointWithIssues {
written,
issues,
diagnostics,
effects,
} = &*written_entrypoint_with_issues_op
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((written.clone(), issues.clone(), diagnostics.clone()))
})
.or_else(|e| ctx.throw_turbopack_internal_result(&e))
.await?;
Ok(TurbopackResult {
result: NapiWrittenEndpoint::from(written.map(ReadRef::into_owned)),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
})
}
#[turbo_tasks::value(serialization = "none")]
struct ModuleGraphsWithIssues {
module_graphs: Option<ReadRef<ModuleGraphSnapshots>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn get_module_graphs_with_issues_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<ModuleGraphsWithIssues>> {
let module_graphs_op = get_module_graphs_operation(endpoint_op);
let (module_graphs, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(module_graphs_op).await?;
Ok(ModuleGraphsWithIssues {
module_graphs,
issues,
diagnostics,
effects,
}
.cell())
}
#[turbo_tasks::value(transparent)]
struct ModuleGraphSnapshots(Vec<ReadRef<ModuleGraphSnapshot>>);
#[turbo_tasks::function(operation)]
async fn get_module_graphs_operation(
endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<ModuleGraphSnapshots>> {
let Some(endpoint) = *endpoint_op.connect().await? else {
return Ok(Vc::cell(vec![]));
};
let graphs = endpoint.module_graphs().await?;
let entries = endpoint.entries().await?;
let entry_modules = entries.iter().flat_map(|e| e.entries()).collect::<Vec<_>>();
let snapshots = graphs
.iter()
.map(async |&graph| {
let module_graph = graph.await?;
let entry_modules = entry_modules
.iter()
.map(async |&m| Ok(module_graph.has_entry(m).await?.then_some(m)))
.try_flat_join()
.await?;
Ok((*graph, entry_modules))
})
.try_join()
.await?
.into_iter()
.map(|(graph, entry_modules)| (graph, Vc::cell(entry_modules)))
.collect::<Vec<_>>()
.into_iter()
.map(async |(graph, entry_modules)| {
get_module_graph_snapshot(graph, Some(entry_modules)).await
})
.try_join()
.await?;
Ok(Vc::cell(snapshots))
}
#[napi]
pub async fn endpoint_module_graphs(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<TurbopackResult<NapiModuleGraphSnapshots>> {
let endpoint_op: OperationVc<OptionEndpoint> = ***endpoint;
let (module_graphs, issues, diagnostics) = endpoint
.turbopack_ctx()
.turbo_tasks()
.run_once(async move {
let module_graphs_op = get_module_graphs_with_issues_operation(endpoint_op);
let ModuleGraphsWithIssues {
module_graphs,
issues,
diagnostics,
effects: _,
} = &*module_graphs_op.connect().await?;
Ok((module_graphs.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(TurbopackResult {
result: NapiModuleGraphSnapshots {
module_graphs: module_graphs
.into_iter()
.flat_map(|m| m.into_iter())
.map(|m| NapiModuleGraphSnapshot::from(&**m))
.collect(),
},
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
})
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_server_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
issues: bool,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let turbopack_ctx = endpoint.turbopack_ctx().clone();
let endpoint = ***endpoint;
subscribe(
turbopack_ctx,
func,
move || {
async move {
let issues_and_diags_op = subscribe_issues_and_diags_operation(endpoint, issues);
let result = issues_and_diags_op.read_strongly_consistent().await?;
result.effects.apply().await?;
Ok(result)
}
.instrument(tracing::info_span!("server changes subscription"))
},
|ctx| {
let EndpointIssuesAndDiags {
changed: _,
issues,
diagnostics,
effects: _,
} = &*ctx.value;
Ok(vec![TurbopackResult {
result: (),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
}])
},
)
}
#[turbo_tasks::value(shared, serialization = "none", eq = "manual")]
struct EndpointIssuesAndDiags {
changed: Option<ReadRef<Completion>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
impl PartialEq for EndpointIssuesAndDiags {
fn eq(&self, other: &Self) -> bool {
(match (&self.changed, &other.changed) {
(Some(a), Some(b)) => ReadRef::ptr_eq(a, b),
(None, None) => true,
(None, Some(_)) | (Some(_), None) => false,
}) && self.issues == other.issues
&& self.diagnostics == other.diagnostics
}
}
impl Eq for EndpointIssuesAndDiags {}
#[turbo_tasks::function(operation)]
async fn subscribe_issues_and_diags_operation(
endpoint_op: OperationVc<OptionEndpoint>,
should_include_issues: bool,
) -> Result<Vc<EndpointIssuesAndDiags>> {
let changed_op = endpoint_server_changed_operation(endpoint_op);
if should_include_issues {
let (changed_value, issues, diagnostics, effects) =
strongly_consistent_catch_collectables(changed_op).await?;
Ok(EndpointIssuesAndDiags {
changed: changed_value,
issues,
diagnostics,
effects,
}
.cell())
} else {
let changed_value = changed_op.read_strongly_consistent().await?;
Ok(EndpointIssuesAndDiags {
changed: Some(changed_value),
issues: Arc::new(vec![]),
diagnostics: Arc::new(vec![]),
effects: Arc::new(Effects::default()),
}
.cell())
}
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_client_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let turbopack_ctx = endpoint.turbopack_ctx().clone();
let endpoint_op = ***endpoint;
subscribe(
turbopack_ctx,
func,
move || {
async move {
let changed_op = endpoint_client_changed_operation(endpoint_op);
// We don't capture issues and diagnostics here since we don't want to be
// notified when they change
//
// This must be a *read*, not just a resolve, because we need the root task created
// by `subscribe` to re-run when the `Completion`'s value changes (via equality),
// even if the cell id doesn't change.
let _ = changed_op.read_strongly_consistent().await?;
Ok(())
}
.instrument(tracing::info_span!("client changes subscription"))
},
|_| {
Ok(vec![TurbopackResult {
result: (),
issues: vec![],
diagnostics: vec![],
}])
},
)
}
|