Download next.js/crates/next-api/src/operation.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 9.01 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/crates/next-api/src/operation.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/crates/next-api/src/operation.rs
-
curl -L -o operation.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/crates/next-api/src/operation.rs
9.01 kB
| use anyhow::Result; | |
| use serde::{Deserialize, Serialize}; | |
| use turbo_rcstr::RcStr; | |
| use turbo_tasks::{ | |
| CollectiblesSource, FxIndexMap, NonLocalValue, OperationValue, OperationVc, ResolvedVc, | |
| TaskInput, Vc, debug::ValueDebugFormat, get_effects, trace::TraceRawVcs, | |
| }; | |
| use turbopack_core::{diagnostics::Diagnostic, issue::IssueDescriptionExt}; | |
| use crate::{ | |
| entrypoints::Entrypoints, | |
| route::{Endpoint, Route}, | |
| }; | |
| /// Based on [`Entrypoints`], but with [`OperationVc<Endpoint>`][OperationVc] for every endpoint. | |
| /// | |
| /// This is used when constructing `ExternalEndpoint`s in the `napi` crate. | |
| /// | |
| /// This is important as `OperationVc`s can be stored in the VersionedContentMap and can be exposed | |
| /// to JS via napi. | |
| /// | |
| /// This is needed to call `write_to_disk` which expects an `OperationVc<Endpoint>`. | |
| pub struct EntrypointsOperation { | |
| pub routes: FxIndexMap<RcStr, RouteOperation>, | |
| pub middleware: Option<MiddlewareOperation>, | |
| pub instrumentation: Option<InstrumentationOperation>, | |
| pub pages_document_endpoint: OperationVc<OptionEndpoint>, | |
| pub pages_app_endpoint: OperationVc<OptionEndpoint>, | |
| pub pages_error_endpoint: OperationVc<OptionEndpoint>, | |
| } | |
| /// Removes diagnostics, issues, and effects from the top-level `entrypoints` operation so that | |
| /// they're not duplicated across many different individual entrypoints or routes. | |
| async fn entrypoints_without_collectibles_operation( | |
| entrypoints: OperationVc<Entrypoints>, | |
| ) -> Result<Vc<Entrypoints>> { | |
| let _ = entrypoints.resolve_strongly_consistent().await?; | |
| let _ = entrypoints.take_collectibles::<Box<dyn Diagnostic>>(); | |
| let _ = entrypoints.take_issues_with_path().await?; | |
| let _ = get_effects(entrypoints).await?; | |
| Ok(entrypoints.connect()) | |
| } | |
| impl EntrypointsOperation { | |
| pub async fn new(entrypoints: OperationVc<Entrypoints>) -> Result<Vc<Self>> { | |
| let e = entrypoints.connect().await?; | |
| let entrypoints = entrypoints_without_collectibles_operation(entrypoints); | |
| Ok(Self { | |
| routes: e | |
| .routes | |
| .iter() | |
| .map(|(k, v)| (k.clone(), pick_route(entrypoints, k.clone(), v))) | |
| .collect(), | |
| middleware: e.middleware.as_ref().map(|_| MiddlewareOperation { | |
| endpoint: pick_endpoint(entrypoints, EndpointSelector::Middleware), | |
| }), | |
| instrumentation: e | |
| .instrumentation | |
| .as_ref() | |
| .map(|_| InstrumentationOperation { | |
| node_js: pick_endpoint(entrypoints, EndpointSelector::InstrumentationNodeJs), | |
| edge: pick_endpoint(entrypoints, EndpointSelector::InstrumentationEdge), | |
| }), | |
| pages_document_endpoint: pick_endpoint(entrypoints, EndpointSelector::PagesDocument), | |
| pages_app_endpoint: pick_endpoint(entrypoints, EndpointSelector::PagesApp), | |
| pages_error_endpoint: pick_endpoint(entrypoints, EndpointSelector::PagesError), | |
| } | |
| .cell()) | |
| } | |
| } | |
| fn pick_route(entrypoints: OperationVc<Entrypoints>, key: RcStr, route: &Route) -> RouteOperation { | |
| match route { | |
| Route::Page { .. } => RouteOperation::Page { | |
| html_endpoint: pick_endpoint(entrypoints, EndpointSelector::RoutePageHtml(key.clone())), | |
| data_endpoint: pick_endpoint(entrypoints, EndpointSelector::RoutePageData(key)), | |
| }, | |
| Route::PageApi { .. } => RouteOperation::PageApi { | |
| endpoint: pick_endpoint(entrypoints, EndpointSelector::RoutePageApi(key)), | |
| }, | |
| Route::AppPage(pages) => RouteOperation::AppPage( | |
| pages | |
| .iter() | |
| .enumerate() | |
| .map(|(i, p)| AppPageRouteOperation { | |
| original_name: p.original_name.clone(), | |
| html_endpoint: pick_endpoint( | |
| entrypoints, | |
| EndpointSelector::RouteAppPageHtml(key.clone(), i), | |
| ), | |
| rsc_endpoint: pick_endpoint( | |
| entrypoints, | |
| EndpointSelector::RouteAppPageRsc(key.clone(), i), | |
| ), | |
| }) | |
| .collect(), | |
| ), | |
| Route::AppRoute { original_name, .. } => RouteOperation::AppRoute { | |
| original_name: original_name.clone(), | |
| endpoint: pick_endpoint(entrypoints, EndpointSelector::RouteAppRoute(key)), | |
| }, | |
| Route::Conflict => RouteOperation::Conflict, | |
| } | |
| } | |
| enum EndpointSelector { | |
| RoutePageHtml(RcStr), | |
| RoutePageData(RcStr), | |
| RoutePageApi(RcStr), | |
| RouteAppPageHtml(RcStr, usize), | |
| RouteAppPageRsc(RcStr, usize), | |
| RouteAppRoute(RcStr), | |
| InstrumentationNodeJs, | |
| InstrumentationEdge, | |
| Middleware, | |
| PagesDocument, | |
| PagesApp, | |
| PagesError, | |
| } | |
| pub struct OptionEndpoint(Option<ResolvedVc<Box<dyn Endpoint>>>); | |
| /// Given a selector and the `Entrypoints` operation that it comes from, connect the operation and | |
| /// return an `OperationVc` containing the selected value. The returned operation will keep the | |
| /// entire `Entrypoints` operation alive. | |
| async fn pick_endpoint( | |
| op: OperationVc<Entrypoints>, | |
| selector: EndpointSelector, | |
| ) -> Result<Vc<OptionEndpoint>> { | |
| let endpoints = op.connect().strongly_consistent().await?; | |
| let endpoint = match selector { | |
| EndpointSelector::InstrumentationNodeJs => { | |
| endpoints.instrumentation.as_ref().map(|i| i.node_js) | |
| } | |
| EndpointSelector::InstrumentationEdge => endpoints.instrumentation.as_ref().map(|i| i.edge), | |
| EndpointSelector::Middleware => endpoints.middleware.as_ref().map(|m| m.endpoint), | |
| EndpointSelector::PagesDocument => Some(endpoints.pages_document_endpoint), | |
| EndpointSelector::PagesApp => Some(endpoints.pages_app_endpoint), | |
| EndpointSelector::PagesError => Some(endpoints.pages_error_endpoint), | |
| EndpointSelector::RoutePageHtml(name) => { | |
| if let Some(Route::Page { html_endpoint, .. }) = endpoints.routes.get(&name) { | |
| Some(*html_endpoint) | |
| } else { | |
| None | |
| } | |
| } | |
| EndpointSelector::RoutePageData(name) => { | |
| if let Some(Route::Page { data_endpoint, .. }) = endpoints.routes.get(&name) { | |
| Some(*data_endpoint) | |
| } else { | |
| None | |
| } | |
| } | |
| EndpointSelector::RoutePageApi(name) => { | |
| if let Some(Route::PageApi { endpoint }) = endpoints.routes.get(&name) { | |
| Some(*endpoint) | |
| } else { | |
| None | |
| } | |
| } | |
| EndpointSelector::RouteAppPageHtml(name, i) => { | |
| if let Some(Route::AppPage(pages)) = endpoints.routes.get(&name) { | |
| pages.get(i).as_ref().map(|p| p.html_endpoint) | |
| } else { | |
| None | |
| } | |
| } | |
| EndpointSelector::RouteAppPageRsc(name, i) => { | |
| if let Some(Route::AppPage(pages)) = endpoints.routes.get(&name) { | |
| pages.get(i).as_ref().map(|p| p.rsc_endpoint) | |
| } else { | |
| None | |
| } | |
| } | |
| EndpointSelector::RouteAppRoute(name) => { | |
| if let Some(Route::AppRoute { endpoint, .. }) = endpoints.routes.get(&name) { | |
| Some(*endpoint) | |
| } else { | |
| None | |
| } | |
| } | |
| }; | |
| Ok(Vc::cell(endpoint)) | |
| } | |
| pub struct InstrumentationOperation { | |
| pub node_js: OperationVc<OptionEndpoint>, | |
| pub edge: OperationVc<OptionEndpoint>, | |
| } | |
| pub struct MiddlewareOperation { | |
| pub endpoint: OperationVc<OptionEndpoint>, | |
| } | |
| pub enum RouteOperation { | |
| Page { | |
| html_endpoint: OperationVc<OptionEndpoint>, | |
| data_endpoint: OperationVc<OptionEndpoint>, | |
| }, | |
| PageApi { | |
| endpoint: OperationVc<OptionEndpoint>, | |
| }, | |
| AppPage(Vec<AppPageRouteOperation>), | |
| AppRoute { | |
| original_name: RcStr, | |
| endpoint: OperationVc<OptionEndpoint>, | |
| }, | |
| Conflict, | |
| } | |
| pub struct AppPageRouteOperation { | |
| pub original_name: RcStr, | |
| pub html_endpoint: OperationVc<OptionEndpoint>, | |
| pub rsc_endpoint: OperationVc<OptionEndpoint>, | |
| } | |