Download next.js/turbopack/crates/turbopack-node/src/render/node_api_source.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 6.24 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-node/src/render/node_api_source.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-node/src/render/node_api_source.rs
-
curl -L -o node_api_source.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-node/src/render/node_api_source.rs
6.24 kB
| use anyhow::Result; | |
| use serde_json::Value as JsonValue; | |
| use turbo_rcstr::{RcStr, rcstr}; | |
| use turbo_tasks::{FxIndexSet, ResolvedVc, Vc}; | |
| use turbo_tasks_env::ProcessEnv; | |
| use turbo_tasks_fs::FileSystemPath; | |
| use turbopack_core::introspect::{ | |
| Introspectable, IntrospectableChildren, module::IntrospectableModule, | |
| output_asset::IntrospectableOutputAsset, | |
| }; | |
| use turbopack_dev_server::source::{ | |
| ContentSource, ContentSourceContent, ContentSourceData, ContentSourceDataVary, | |
| GetContentSourceContent, | |
| route_tree::{BaseSegment, RouteTree, RouteType}, | |
| }; | |
| use super::{RenderData, render_proxy::render_proxy_operation}; | |
| use crate::{get_intermediate_asset, node_entry::NodeEntry, route_matcher::RouteMatcher}; | |
| /// Creates a [NodeApiContentSource]. | |
| pub fn create_node_api_source( | |
| cwd: FileSystemPath, | |
| env: ResolvedVc<Box<dyn ProcessEnv>>, | |
| base_segments: Vec<BaseSegment>, | |
| route_type: RouteType, | |
| server_root: FileSystemPath, | |
| route_match: ResolvedVc<Box<dyn RouteMatcher>>, | |
| pathname: ResolvedVc<RcStr>, | |
| entry: ResolvedVc<Box<dyn NodeEntry>>, | |
| render_data: ResolvedVc<JsonValue>, | |
| debug: bool, | |
| ) -> Vc<Box<dyn ContentSource>> { | |
| Vc::upcast( | |
| NodeApiContentSource { | |
| cwd, | |
| env, | |
| base_segments, | |
| route_type, | |
| server_root, | |
| pathname, | |
| route_match, | |
| entry, | |
| render_data, | |
| debug, | |
| } | |
| .cell(), | |
| ) | |
| } | |
| /// A content source that proxies API requests to one-off Node.js | |
| /// servers running the passed `entry` when it matches a `path_regex`. | |
| /// | |
| /// It needs a temporary directory (`intermediate_output_path`) to place file | |
| /// for Node.js execution during rendering. The `chunking_context` should emit | |
| /// to this directory. | |
| pub struct NodeApiContentSource { | |
| cwd: FileSystemPath, | |
| env: ResolvedVc<Box<dyn ProcessEnv>>, | |
| base_segments: Vec<BaseSegment>, | |
| route_type: RouteType, | |
| server_root: FileSystemPath, | |
| pathname: ResolvedVc<RcStr>, | |
| route_match: ResolvedVc<Box<dyn RouteMatcher>>, | |
| entry: ResolvedVc<Box<dyn NodeEntry>>, | |
| render_data: ResolvedVc<JsonValue>, | |
| debug: bool, | |
| } | |
| impl NodeApiContentSource { | |
| pub fn get_pathname(&self) -> Vc<RcStr> { | |
| *self.pathname | |
| } | |
| } | |
| impl ContentSource for NodeApiContentSource { | |
| async fn get_routes(self: Vc<Self>) -> Result<Vc<RouteTree>> { | |
| let this = self.await?; | |
| Ok(RouteTree::new_route( | |
| this.base_segments.clone(), | |
| this.route_type.clone(), | |
| Vc::upcast(self), | |
| )) | |
| } | |
| } | |
| impl GetContentSourceContent for NodeApiContentSource { | |
| fn vary(&self) -> Vc<ContentSourceDataVary> { | |
| ContentSourceDataVary { | |
| method: true, | |
| url: true, | |
| original_url: true, | |
| raw_headers: true, | |
| raw_query: true, | |
| body: true, | |
| cache_buster: true, | |
| ..Default::default() | |
| } | |
| .cell() | |
| } | |
| async fn get(&self, path: RcStr, data: ContentSourceData) -> Result<Vc<ContentSourceContent>> { | |
| let Some(params) = &*self.route_match.params(path.clone()).await? else { | |
| anyhow::bail!("Non matching path provided") | |
| }; | |
| let ContentSourceData { | |
| method: Some(method), | |
| url: Some(url), | |
| original_url: Some(original_url), | |
| raw_headers: Some(raw_headers), | |
| raw_query: Some(raw_query), | |
| body: Some(body), | |
| .. | |
| } = &data | |
| else { | |
| anyhow::bail!("Missing request data") | |
| }; | |
| let entry = (*self.entry).entry(data.clone()).await?; | |
| Ok(ContentSourceContent::HttpProxy(render_proxy_operation( | |
| self.cwd.clone(), | |
| self.env, | |
| self.server_root.join(&path.clone())?, | |
| ResolvedVc::upcast(entry.module), | |
| entry.runtime_entries, | |
| entry.chunking_context, | |
| entry.intermediate_output_path.clone(), | |
| entry.output_root.clone(), | |
| entry.project_dir.clone(), | |
| RenderData { | |
| params: params.clone(), | |
| method: method.clone(), | |
| url: url.clone(), | |
| original_url: original_url.clone(), | |
| raw_query: raw_query.clone(), | |
| raw_headers: raw_headers.clone(), | |
| path: format!("/{path}").into(), | |
| data: Some(self.render_data.await?), | |
| } | |
| .resolved_cell(), | |
| *body, | |
| self.debug, | |
| )) | |
| .cell()) | |
| } | |
| } | |
| impl Introspectable for NodeApiContentSource { | |
| fn ty(&self) -> Vc<RcStr> { | |
| Vc::cell(rcstr!("node api content source")) | |
| } | |
| fn title(&self) -> Vc<RcStr> { | |
| *self.pathname | |
| } | |
| fn details(&self) -> Vc<RcStr> { | |
| Vc::cell( | |
| format!( | |
| "base: {:?}\ntype: {:?}", | |
| self.base_segments, self.route_type | |
| ) | |
| .into(), | |
| ) | |
| } | |
| async fn children(&self) -> Result<Vc<IntrospectableChildren>> { | |
| let mut set = FxIndexSet::default(); | |
| for &entry in self.entry.entries().await?.iter() { | |
| let entry = entry.await?; | |
| set.insert(( | |
| rcstr!("module"), | |
| IntrospectableModule::new(Vc::upcast(*entry.module)) | |
| .to_resolved() | |
| .await?, | |
| )); | |
| set.insert(( | |
| rcstr!("intermediate asset"), | |
| IntrospectableOutputAsset::new(get_intermediate_asset( | |
| *entry.chunking_context, | |
| Vc::upcast(*entry.module), | |
| *entry.runtime_entries, | |
| )) | |
| .to_resolved() | |
| .await?, | |
| )); | |
| } | |
| Ok(Vc::cell(set)) | |
| } | |
| } | |