Skip to content

Commit

Permalink
Merge d6fe2ba into 1689dc3
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony authored Feb 10, 2023
2 parents 1689dc3 + d6fe2ba commit 1b74f41
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 41 deletions.
1 change: 1 addition & 0 deletions crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ async fn create_app_source_for_directory(
let params_matcher = NextParamsMatcherVc::new(pathname);

sources.push(create_node_rendered_source(
project_path,
specificity,
server_root,
params_matcher.into(),
Expand Down
26 changes: 15 additions & 11 deletions crates/next-core/src/page_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub async fn create_page_source(
/// Handles a single page file in the pages directory
#[turbo_tasks::function]
async fn create_page_source_for_file(
context_path: FileSystemPathVc,
project_path: FileSystemPathVc,
server_context: AssetContextVc,
server_data_context: AssetContextVc,
client_context: AssetContextVc,
Expand All @@ -320,7 +320,7 @@ async fn create_page_source_for_file(
);

let server_chunking_context = DevChunkingContextVc::builder(
context_path,
project_path,
intermediate_output_path,
intermediate_output_path.join("chunks"),
get_client_assets_path(
Expand All @@ -334,7 +334,7 @@ async fn create_page_source_for_file(
let data_intermediate_output_path = intermediate_output_path.join("data");

let server_data_chunking_context = DevChunkingContextVc::builder(
context_path,
project_path,
data_intermediate_output_path,
data_intermediate_output_path.join("chunks"),
get_client_assets_path(
Expand All @@ -346,7 +346,7 @@ async fn create_page_source_for_file(
.build();

let client_chunking_context = get_client_chunking_context(
context_path,
project_path,
server_root,
client_context.environment(),
Value::new(ClientContextType::Pages { pages_dir }),
Expand All @@ -364,6 +364,7 @@ async fn create_page_source_for_file(
SsrType::Api
};
create_node_api_source(
project_path,
specificity,
server_root,
pathname,
Expand Down Expand Up @@ -408,6 +409,7 @@ async fn create_page_source_for_file(

CombinedContentSourceVc::new(vec![
create_node_rendered_source(
project_path,
specificity,
server_root,
route_matcher.into(),
Expand All @@ -417,6 +419,7 @@ async fn create_page_source_for_file(
fallback_page,
),
create_node_rendered_source(
project_path,
specificity,
server_root,
data_route_matcher.into(),
Expand Down Expand Up @@ -454,7 +457,7 @@ async fn get_not_found_page(
/// Handles a single page file in the pages directory
#[turbo_tasks::function]
async fn create_not_found_page_source(
context_path: FileSystemPathVc,
project_path: FileSystemPathVc,
server_context: AssetContextVc,
client_context: AssetContextVc,
pages_dir: FileSystemPathVc,
Expand All @@ -467,7 +470,7 @@ async fn create_not_found_page_source(
route_matcher: RouteMatcherVc,
) -> Result<ContentSourceVc> {
let server_chunking_context = DevChunkingContextVc::builder(
context_path,
project_path,
intermediate_output_path,
intermediate_output_path.join("chunks"),
get_client_assets_path(
Expand All @@ -479,7 +482,7 @@ async fn create_not_found_page_source(
.build();

let client_chunking_context = get_client_chunking_context(
context_path,
project_path,
server_root,
client_context.environment(),
Value::new(ClientContextType::Pages { pages_dir }),
Expand All @@ -494,7 +497,7 @@ async fn create_not_found_page_source(
// The error page asset must be within the context path so it can depend on the
// Next.js module.
next_asset(
attached_next_js_package_path(context_path).join("entry/error.tsx"),
attached_next_js_package_path(project_path).join("entry/error.tsx"),
"entry/error.tsx",
),
// If no 404 page is defined, the pathname should be _error.
Expand Down Expand Up @@ -528,6 +531,7 @@ async fn create_not_found_page_source(

Ok(CombinedContentSourceVc::new(vec![
create_node_rendered_source(
project_path,
specificity,
server_root,
route_matcher,
Expand All @@ -546,7 +550,7 @@ async fn create_not_found_page_source(
/// [create_page_source_for_file] method for files.
#[turbo_tasks::function]
async fn create_page_source_for_directory(
context_path: FileSystemPathVc,
project_path: FileSystemPathVc,
server_context: AssetContextVc,
server_data_context: AssetContextVc,
client_context: AssetContextVc,
Expand Down Expand Up @@ -595,7 +599,7 @@ async fn create_page_source_for_directory(
sources.push((
name,
create_page_source_for_file(
context_path,
project_path,
server_context,
server_data_context,
client_context,
Expand All @@ -618,7 +622,7 @@ async fn create_page_source_for_directory(
sources.push((
name,
create_page_source_for_directory(
context_path,
project_path,
server_context,
server_data_context,
client_context,
Expand Down
18 changes: 13 additions & 5 deletions crates/turbo-tasks-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use self::{json::UnparseableJson, mutex_map::MutexMap};
#[cfg(target_family = "windows")]
use crate::util::is_windows_raw_path;
use crate::{
attach::AttachedFileSystemVc,
retry::{retry_blocking, retry_future},
rope::{Rope, RopeReadRef, RopeReader},
};
Expand Down Expand Up @@ -1668,12 +1669,19 @@ impl ValueToString for NullFileSystem {
}
}

pub async fn to_sys_path(path: FileSystemPathVc) -> Result<Option<PathBuf>> {
if let Some(fs) = DiskFileSystemVc::resolve_from(path.fs()).await? {
let sys_path = fs.await?.to_sys_path(path).await?;
return Ok(Some(sys_path));
pub async fn to_sys_path(mut path: FileSystemPathVc) -> Result<Option<PathBuf>> {
loop {
if let Some(fs) = AttachedFileSystemVc::resolve_from(path.fs()).await? {
path = fs.get_inner_fs_path(path);
}

if let Some(fs) = DiskFileSystemVc::resolve_from(path.fs()).await? {
let sys_path = fs.await?.to_sys_path(path).await?;
return Ok(Some(sys_path));
}

return Ok(None);
}
Ok(None)
}

pub fn register() {
Expand Down
21 changes: 11 additions & 10 deletions crates/turbopack-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use std::{
path::PathBuf,
};

use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use futures::{stream::FuturesUnordered, TryStreamExt};
use indexmap::IndexSet;
pub use node_entry::{
NodeEntry, NodeEntryVc, NodeRenderingEntriesVc, NodeRenderingEntry, NodeRenderingEntryVc,
};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use turbo_tasks::{CompletionVc, CompletionsVc, TryJoinIterExt};
use turbo_tasks::{CompletionVc, CompletionsVc, TryJoinIterExt, ValueToString};
use turbo_tasks_fs::{to_sys_path, File, FileContent, FileSystemPathVc};
use turbopack_core::{
asset::{Asset, AssetVc, AssetsSetVc},
Expand Down Expand Up @@ -181,6 +181,7 @@ async fn separate_assets(
/// Creates a node.js renderer pool for an entrypoint.
#[turbo_tasks::function]
pub async fn get_renderer_pool(
cwd: FileSystemPathVc,
intermediate_asset: AssetVc,
intermediate_output_path: FileSystemPathVc,
output_root: FileSystemPathVc,
Expand All @@ -204,16 +205,16 @@ pub async fn get_renderer_pool(

emit(intermediate_asset, output_root).await?;

let cwd = output_root;
let entrypoint = intermediate_output_path.join("index.js");

if let (Some(cwd), Some(entrypoint)) = (to_sys_path(cwd).await?, to_sys_path(entrypoint).await?)
{
let pool = NodeJsPool::new(cwd, entrypoint, HashMap::new(), 4, debug);
Ok(pool.cell())
} else {
Err(anyhow!("can only render from a disk filesystem"))
}
let Some(cwd) = to_sys_path(cwd).await? else {
bail!("can only render from a disk filesystem, but `cwd = {}`", cwd.fs().to_string().await?);
};
let Some(entrypoint) = to_sys_path(entrypoint).await? else {
bail!("can only render from a disk filesystem, but `entrypoint = {}`", entrypoint.fs().to_string().await?);
};

Ok(NodeJsPool::new(cwd, entrypoint, HashMap::new(), 4, debug).cell())
}

/// Converts a module graph into node.js executable assets
Expand Down
14 changes: 9 additions & 5 deletions crates/turbopack-node/src/render/node_api_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
/// Creates a [NodeApiContentSource].
#[turbo_tasks::function]
pub fn create_node_api_source(
cwd: FileSystemPathVc,
specificity: SpecificityVc,
server_root: FileSystemPathVc,
pathname: StringVc,
Expand All @@ -31,6 +32,7 @@ pub fn create_node_api_source(
runtime_entries: EcmascriptChunkPlaceablesVc,
) -> ContentSourceVc {
NodeApiContentSource {
cwd,
specificity,
server_root,
pathname,
Expand All @@ -50,6 +52,7 @@ pub fn create_node_api_source(
/// to this directory.
#[turbo_tasks::value]
pub struct NodeApiContentSource {
cwd: FileSystemPathVc,
specificity: SpecificityVc,
server_root: FileSystemPathVc,
pathname: StringVc,
Expand Down Expand Up @@ -114,8 +117,8 @@ impl GetContentSourceContent for NodeApiGetContentResult {
}
#[turbo_tasks::function]
async fn get(&self, data: Value<ContentSourceData>) -> Result<ContentSourceContentVc> {
let this = self.source.await?;
let Some(params) = &*this.route_match.params(&self.path).await? else {
let source = self.source.await?;
let Some(params) = &*source.route_match.params(&self.path).await? else {
return Err(anyhow!("Non matching path provided"));
};
let ContentSourceData {
Expand All @@ -128,11 +131,12 @@ impl GetContentSourceContent for NodeApiGetContentResult {
} = &*data else {
return Err(anyhow!("Missing request data"));
};
let entry = this.entry.entry(data.clone()).await?;
let entry = source.entry.entry(data.clone()).await?;
Ok(ContentSourceContent::HttpProxy(render_proxy(
this.server_root.join(&self.path),
source.cwd,
source.server_root.join(&self.path),
entry.module,
this.runtime_entries,
source.runtime_entries,
entry.chunking_context,
entry.intermediate_output_path,
entry.output_root,
Expand Down
10 changes: 7 additions & 3 deletions crates/turbopack-node/src/render/render_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{get_intermediate_asset, get_renderer_pool, pool::NodeJsOperation, tr
/// Renders a module as static HTML in a node.js process.
#[turbo_tasks::function]
pub async fn render_proxy(
cwd: FileSystemPathVc,
path: FileSystemPathVc,
module: EcmascriptModuleAssetVc,
runtime_entries: EcmascriptChunkPlaceablesVc,
Expand All @@ -27,13 +28,16 @@ pub async fn render_proxy(
module.as_evaluated_chunk(chunking_context, Some(runtime_entries)),
intermediate_output_path,
);
let renderer_pool = get_renderer_pool(

let pool = get_renderer_pool(
cwd,
intermediate_asset,
intermediate_output_path,
output_root,
/* debug */ false,
);
let pool = renderer_pool.await?;
)
.await?;

let mut operation = match pool.operation().await {
Ok(operation) => operation,
Err(err) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/turbopack-node/src/render/render_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl StaticResultVc {
/// Renders a module as static HTML in a node.js process.
#[turbo_tasks::function]
pub async fn render_static(
cwd: FileSystemPathVc,
path: FileSystemPathVc,
module: EcmascriptModuleAssetVc,
runtime_entries: EcmascriptChunkPlaceablesVc,
Expand All @@ -61,6 +62,7 @@ pub async fn render_static(
intermediate_output_path,
);
let renderer_pool = get_renderer_pool(
cwd,
intermediate_asset,
intermediate_output_path,
output_root,
Expand Down
18 changes: 11 additions & 7 deletions crates/turbopack-node/src/render/rendered_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::{
/// to this directory.
#[turbo_tasks::function]
pub fn create_node_rendered_source(
cwd: FileSystemPathVc,
specificity: SpecificityVc,
server_root: FileSystemPathVc,
route_match: RouteMatcherVc,
Expand All @@ -53,6 +54,7 @@ pub fn create_node_rendered_source(
fallback_page: DevHtmlAssetVc,
) -> ContentSourceVc {
let source = NodeRenderContentSource {
cwd,
specificity,
server_root,
route_match,
Expand All @@ -76,6 +78,7 @@ pub fn create_node_rendered_source(
/// see [create_node_rendered_source]
#[turbo_tasks::value]
pub struct NodeRenderContentSource {
cwd: FileSystemPathVc,
specificity: SpecificityVc,
server_root: FileSystemPathVc,
route_match: RouteMatcherVc,
Expand Down Expand Up @@ -185,8 +188,8 @@ impl GetContentSourceContent for NodeRenderGetContentResult {

#[turbo_tasks::function]
async fn get(&self, data: Value<ContentSourceData>) -> Result<ContentSourceContentVc> {
let this = self.source.await?;
let Some(params) = &*this.route_match.params(&self.path).await? else {
let source = self.source.await?;
let Some(params) = &*source.route_match.params(&self.path).await? else {
return Err(anyhow!("Non matching path provided"));
};
let ContentSourceData {
Expand All @@ -198,12 +201,13 @@ impl GetContentSourceContent for NodeRenderGetContentResult {
} = &*data else {
return Err(anyhow!("Missing request data"));
};
let entry = this.entry.entry(data.clone()).await?;
let entry = source.entry.entry(data.clone()).await?;
let result = render_static(
this.server_root.join(&self.path),
source.cwd,
source.server_root.join(&self.path),
entry.module,
this.runtime_entries,
this.fallback_page,
source.runtime_entries,
source.fallback_page,
entry.chunking_context,
entry.intermediate_output_path,
entry.output_root,
Expand All @@ -213,7 +217,7 @@ impl GetContentSourceContent for NodeRenderGetContentResult {
url: url.clone(),
raw_query: raw_query.clone(),
raw_headers: raw_headers.clone(),
path: format!("/{}", this.pathname.await?),
path: format!("/{}", source.pathname.await?),
}
.cell(),
);
Expand Down

0 comments on commit 1b74f41

Please sign in to comment.