Commit 7e050637 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

Add fileHashes to NFT file

parent 30b04a27
...@@ -5,14 +5,15 @@ use bincode::{Decode, Encode}; ...@@ -5,14 +5,15 @@ use bincode::{Decode, Encode};
use either::Either; use either::Either;
use next_core::{get_next_package, next_server::get_tracing_compile_time_info}; use next_core::{get_next_package, next_server::get_tracing_compile_time_info};
use serde_json::{Value, json}; use serde_json::{Value, json};
use turbo_rcstr::RcStr; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
NonLocalValue, ResolvedVc, TaskInput, TryFlatJoinIterExt, TryJoinIterExt, Vc, NonLocalValue, ReadRef, ResolvedVc, TaskInput, TryFlatJoinIterExt, TryJoinIterExt, Vc,
trace::TraceRawVcs, trace::TraceRawVcs,
}; };
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DirectoryContent, DirectoryEntry, File, FileContent, FileSystemPath, glob::Glob, DirectoryContent, DirectoryEntry, File, FileContent, FileSystemPath, glob::Glob,
}; };
use turbo_tasks_hash::HashAlgorithm;
use turbopack::externals_tracing_module_context; use turbopack::externals_tracing_module_context;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -111,25 +112,33 @@ impl Asset for ServerNftJsonAsset { ...@@ -111,25 +112,33 @@ impl Asset for ServerNftJsonAsset {
.await? .await?
.iter() .iter()
.map(async |m| { .map(async |m| {
Ok((
base_dir base_dir
.get_relative_path_to(&*m.path().await?) .get_relative_path_to(&*m.path().await?)
.context("failed to compute relative path for server NFT JSON") .context("failed to compute relative path for server NFT JSON")?,
m.content().hash(HashAlgorithm::Xxh3Hash128Hex).await?,
))
}) })
.try_join() .try_join()
.await?; .await?;
// A few hardcoded files (not recursive) // A few hardcoded files (not recursive)
server_output_assets.push("./package.json".into()); // TODO hash
server_output_assets.push(("./package.json".into(), ReadRef::new_owned(rcstr!("0"))));
let next_dir = get_next_package(this.project.project_path().owned().await?).await?; let next_dir = get_next_package(this.project.project_path().owned().await?).await?;
for ty in ["app-page", "pages"] { for ty in ["app-page", "pages"] {
let dir = next_dir.join(&format!("dist/server/route-modules/{ty}"))?; let dir = next_dir.join(&format!("dist/server/route-modules/{ty}"))?;
let module_path = dir.join("module.compiled.js")?; let module_path = dir.join("module.compiled.js")?;
server_output_assets.push( server_output_assets.push((
base_dir base_dir
.get_relative_path_to(&module_path) .get_relative_path_to(&module_path)
.context("failed to compute relative path for server NFT JSON")?, .context("failed to compute relative path for server NFT JSON")?,
); module_path
.read()
.hash(HashAlgorithm::Xxh3Hash128Hex)
.await?,
));
let contexts_dir = dir.join("vendored/contexts")?; let contexts_dir = dir.join("vendored/contexts")?;
let DirectoryContent::Entries(contexts_files) = &*contexts_dir.read_dir().await? else { let DirectoryContent::Entries(contexts_files) = &*contexts_dir.read_dir().await? else {
...@@ -143,24 +152,27 @@ impl Asset for ServerNftJsonAsset { ...@@ -143,24 +152,27 @@ impl Asset for ServerNftJsonAsset {
continue; continue;
}; };
if file.extension() == Some("js") { if file.extension() == Some("js") {
server_output_assets.push( server_output_assets.push((
base_dir base_dir
.get_relative_path_to(file) .get_relative_path_to(file)
.context("failed to compute relative path for server NFT JSON")?, .context("failed to compute relative path for server NFT JSON")?,
) file.read().hash(HashAlgorithm::Xxh3Hash128Hex).await?,
))
} }
} }
} }
server_output_assets.sort(); server_output_assets.sort_unstable();
// Dedupe as some entries may be duplicates: a file might be referenced multiple times, // Dedupe as some entries may be duplicates: a file might be referenced multiple times,
// e.g. as a RawModule (from an FS operation) and as an EcmascriptModuleAsset because it // e.g. as a RawModule (from an FS operation) and as an EcmascriptModuleAsset because it
// was required. // was required.
server_output_assets.dedup(); server_output_assets.dedup();
let (files, file_hashes): (Vec<_>, Vec<_>) = server_output_assets.into_iter().unzip();
let json = json!({ let json = json!({
"version": 1, "version": 1,
"files": server_output_assets "files": files,
"fileHashes": file_hashes
}); });
Ok(AssetContent::file( Ok(AssetContent::file(
......
use std::collections::{BTreeSet, HashSet, VecDeque}; use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque};
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use async_trait::async_trait; use async_trait::async_trait;
...@@ -14,6 +14,7 @@ use turbo_tasks_fs::{ ...@@ -14,6 +14,7 @@ use turbo_tasks_fs::{
DirectoryEntry, File, FileContent, FileSystem, FileSystemPath, DirectoryEntry, File, FileContent, FileSystem, FileSystemPath,
glob::{Glob, GlobOptions}, glob::{Glob, GlobOptions},
}; };
use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
issue::{Issue, IssueExt, IssueSeverity, IssueStage, StyledString}, issue::{Issue, IssueExt, IssueSeverity, IssueStage, StyledString},
...@@ -106,14 +107,14 @@ async fn apply_includes( ...@@ -106,14 +107,14 @@ async fn apply_includes(
project_root_path: FileSystemPath, project_root_path: FileSystemPath,
glob: Vc<Glob>, glob: Vc<Glob>,
ident_folder: &FileSystemPath, ident_folder: &FileSystemPath,
) -> Result<BTreeSet<RcStr>> { ) -> Result<BTreeMap<RcStr, ReadRef<RcStr>>> {
debug_assert_eq!(project_root_path.fs, ident_folder.fs); debug_assert_eq!(project_root_path.fs, ident_folder.fs);
// Read files matching the glob pattern from the project root // Read files matching the glob pattern from the project root
// This result itself has random order, but the BTreeSet will ensure a deterministic ordering. // This result itself has random order, but the BTreeSet will ensure a deterministic ordering.
let glob_result = project_root_path.read_glob(glob).await?; let glob_result = project_root_path.read_glob(glob).await?;
// Walk the full glob_result using an explicit stack to avoid async recursion overheads. // Walk the full glob_result using an explicit stack to avoid async recursion overheads.
let mut result = BTreeSet::new(); let mut result = BTreeMap::new();
let mut stack = VecDeque::new(); let mut stack = VecDeque::new();
stack.push_back(glob_result); stack.push_back(glob_result);
while let Some(glob_result) = stack.pop_back() { while let Some(glob_result) = stack.pop_back() {
...@@ -128,7 +129,10 @@ async fn apply_includes( ...@@ -128,7 +129,10 @@ async fn apply_includes(
// unwrap is safe because project_root_path and ident_folder have the same filesystem // unwrap is safe because project_root_path and ident_folder have the same filesystem
// and paths produced by read_glob stay in the filesystem // and paths produced by read_glob stay in the filesystem
let relative_path = ident_folder.get_relative_path_to(file_path).unwrap(); let relative_path = ident_folder.get_relative_path_to(file_path).unwrap();
result.insert(relative_path); result.insert(
relative_path,
file_path.read().hash(HashAlgorithm::Xxh3Hash128Hex).await?,
);
} }
for nested_result in glob_result.inner.values() { for nested_result in glob_result.inner.values() {
...@@ -149,7 +153,7 @@ impl Asset for NftJsonAsset { ...@@ -149,7 +153,7 @@ impl Asset for NftJsonAsset {
path = display(self.path().to_string().await?) path = display(self.path().to_string().await?)
); );
async move { async move {
let mut result: BTreeSet<RcStr> = BTreeSet::new(); let mut result: BTreeMap<RcStr, ReadRef<RcStr>> = BTreeMap::new();
let project_path = this.project.project_path().owned().await?; let project_path = this.project.project_path().owned().await?;
let output_root_ref = this.project.output_fs().root().await?; let output_root_ref = this.project.output_fs().root().await?;
...@@ -320,7 +324,13 @@ impl Asset for NftJsonAsset { ...@@ -320,7 +324,13 @@ impl Asset for NftJsonAsset {
} }
}; };
result.insert(specifier); result.insert(
specifier,
referenced_chunk
.content()
.hash(HashAlgorithm::Xxh3Hash128Hex)
.await?,
);
} }
// Apply outputFileTracingIncludes and outputFileTracingExcludes // Apply outputFileTracingIncludes and outputFileTracingExcludes
...@@ -371,9 +381,11 @@ impl Asset for NftJsonAsset { ...@@ -371,9 +381,11 @@ impl Asset for NftJsonAsset {
result.extend(includes.into_iter().flatten()); result.extend(includes.into_iter().flatten());
} }
let (files, file_hashes): (Vec<_>, Vec<_>) = result.into_iter().unzip();
let json = json!({ let json = json!({
"version": 1, "version": 1,
"files": result "files": files,
"fileHashes": file_hashes
}); });
Ok(AssetContent::file( Ok(AssetContent::file(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment