Unverified Commit 9404b901 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: add Rope.content_hash and SHA hashing (#90235)

We want `FileContent.content_hash()` to only hash the content bytes. This is also needed to produce the correct hashes [for SRI](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity)

However, the default `DeterministicHash` and `Hash` impls are ill-suited for that because they're a generic hashing infrastructure.
Hashing only the content bytes isn't what you want actually, otherwise `hash(("a", "b", "c"))`  would be the same as `hash("abc")`
https://doc.rust-lang.org/std/hash/trait.Hasher.html#method.write_str describes this problem as well

This is exactly what we want for Rope though, so this adds a `Rope.content_hash() -> impl DeterministicHash`.

Additionally, this changes `content_hash` to accept a HashAlgorithm to prepare for sha SRI hashes. This required changing the return type to `RcStr`, because of the variable hash lengths. But the return value was always `encode_hex`d everywhere anyway.
parent 074ba4a9
......@@ -9646,6 +9646,9 @@ dependencies = [
name = "turbo-tasks-hash"
version = "0.1.0"
dependencies = [
"bincode 2.0.1",
"data-encoding",
"sha2",
"turbo-tasks-macros",
"twox-hash 2.1.0",
]
......
......@@ -448,6 +448,7 @@ serde_json = "1.0.138"
serde_path_to_error = "0.1.16"
serde_qs = "0.13.0"
serde_with = "3.12.0"
sha2 = "0.10.2"
smallvec = { version = "1.15.1", features = [
"serde",
"const_generics",
......
......@@ -2007,24 +2007,14 @@ impl Endpoint for AppEndpoint {
async move {
let output = self.output();
let output_assets = output.output_assets();
let output = output.await?;
let node_root = &*this.app_project.project().node_root().await?;
let project = this.app_project.project();
let node_root = project.node_root().owned().await?;
let client_relative_root = project.client_relative_path().owned().await?;
let (server_paths, client_paths) = if this
.app_project
.project()
.next_mode()
.await?
.is_development()
{
let node_root = this.app_project.project().node_root().owned().await?;
let server_paths = all_asset_paths(output_assets, node_root).owned().await?;
let output_assets = output.output_assets();
let client_relative_root = this
.app_project
.project()
.client_relative_path()
let (server_paths, client_paths) = if project.next_mode().await?.is_development() {
let server_paths = all_asset_paths(output_assets, node_root.clone(), None)
.owned()
.await?;
let client_paths = all_paths_in_root(output_assets, client_relative_root)
......@@ -2035,7 +2025,7 @@ impl Endpoint for AppEndpoint {
(vec![], vec![])
};
let written_endpoint = match *output {
let written_endpoint = match *output.await? {
AppEndpointOutput::NodeJs { rsc_chunk, .. } => EndpointOutputPaths::NodeJs {
server_entry_path: node_root
.get_path_to(&*rsc_chunk.path().await?)
......@@ -2054,7 +2044,7 @@ impl Endpoint for AppEndpoint {
EndpointOutput {
output_assets: output_assets.to_resolved().await?,
output_paths: written_endpoint.resolved_cell(),
project: this.app_project.project().to_resolved().await?,
project: project.to_resolved().await?,
}
.cell(),
)
......
......@@ -210,7 +210,9 @@ impl Endpoint for InstrumentationEndpoint {
let server_paths = if this.project.next_mode().await?.is_development() {
let node_root = this.project.node_root().owned().await?;
all_asset_paths(output_assets, node_root).owned().await?
all_asset_paths(output_assets, node_root, None)
.owned()
.await?
} else {
vec![]
};
......
......@@ -337,7 +337,9 @@ impl Endpoint for MiddlewareEndpoint {
let (server_paths, client_paths) = if this.project.next_mode().await?.is_development() {
let node_root = this.project.node_root().owned().await?;
let server_paths = all_asset_paths(output_assets, node_root).owned().await?;
let server_paths = all_asset_paths(output_assets, node_root, None)
.owned()
.await?;
// Middleware could in theory have a client path (e.g. `new URL`).
let client_relative_root = this.project.client_relative_path().owned().await?;
......
......@@ -1601,26 +1601,15 @@ impl Endpoint for PageEndpoint {
}
};
async move {
let output = self.output().await?;
let output_assets = self.output().output_assets();
let output = self.output();
let project = this.pages_project.project();
let node_root = project.node_root().owned().await?;
let client_relative_root = project.client_relative_path().owned().await?;
let node_root = this.pages_project.project().node_root().owned().await?;
let output_assets = output.output_assets();
let (server_paths, client_paths) = if this
.pages_project
.project()
.next_mode()
.await?
.is_development()
{
let server_paths = all_asset_paths(output_assets, node_root.clone())
.owned()
.await?;
let client_relative_root = this
.pages_project
.project()
.client_relative_path()
let (server_paths, client_paths) = if project.next_mode().await?.is_development() {
let server_paths = all_asset_paths(output_assets, node_root.clone(), None)
.owned()
.await?;
let client_paths = all_paths_in_root(output_assets, client_relative_root)
......@@ -1631,8 +1620,7 @@ impl Endpoint for PageEndpoint {
(vec![], vec![])
};
let node_root = node_root.clone();
let written_endpoint = match *output {
let written_endpoint = match *output.await? {
PageEndpointOutput::NodeJs { entry_chunk, .. } => {
// Only set server_entry_path if pages should be created
let pages_structure = this.pages_structure.await?;
......@@ -1661,7 +1649,7 @@ impl Endpoint for PageEndpoint {
EndpointOutput {
output_assets: output_assets.to_resolved().await?,
output_paths: written_endpoint.resolved_cell(),
project: this.pages_project.project().to_resolved().await?,
project: project.to_resolved().await?,
}
.cell(),
)
......
use anyhow::Result;
use anyhow::{Context, Result};
use next_core::next_manifests::AssetBinding;
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{HashAlgorithm, encode_hex};
use turbopack_core::{
asset::Asset,
output::{OutputAsset, OutputAssets},
......@@ -17,7 +18,7 @@ use turbopack_wasm::wasm_edge_var_name;
pub struct AssetPath {
/// Relative to the root_path
pub path: RcStr,
pub content_hash: u64,
pub content_hash: RcStr,
}
/// A list of asset paths
......@@ -31,13 +32,23 @@ pub struct OptionAssetPath(Option<AssetPath>);
async fn asset_path(
asset: Vc<Box<dyn OutputAsset>>,
node_root: FileSystemPath,
should_content_hash: Option<HashAlgorithm>,
) -> Result<Vc<OptionAssetPath>> {
Ok(Vc::cell(
if let Some(path) = node_root.get_path_to(&*asset.path().await?) {
let content_hash = *asset.content().hash().await?;
let hash = if let Some(algorithm) = should_content_hash {
asset
.content()
.content_hash(algorithm)
.owned()
.await?
.context("asset content not found")?
} else {
encode_hex(*asset.content().hash().await?).into()
};
Some(AssetPath {
path: RcStr::from(path),
content_hash,
content_hash: hash,
})
} else {
None
......@@ -51,6 +62,7 @@ async fn asset_path(
pub async fn all_asset_paths(
assets: Vc<OutputAssets>,
node_root: FileSystemPath,
should_content_hash: Option<HashAlgorithm>,
) -> Result<Vc<AssetPaths>> {
let span = tracing::info_span!(
"collect all asset paths",
......@@ -63,7 +75,7 @@ pub async fn all_asset_paths(
span.record("assets_count", all_assets.len());
let asset_paths = all_assets
.iter()
.map(|&asset| asset_path(*asset, node_root.clone()).owned())
.map(|&asset| asset_path(*asset, node_root.clone(), should_content_hash).owned())
.try_flat_join()
.await?;
span.record("asset_paths_count", asset_paths.len());
......
......@@ -7,6 +7,7 @@ use indoc::formatdoc;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{
asset::AssetContent,
context::AssetContext,
......@@ -33,13 +34,8 @@ async fn dynamic_image_metadata_with_generator_source(
let stem = stem.unwrap_or_default();
let ext = path.extension();
let hash_query = format!(
"?{:x}",
path.read()
.content_hash()
.await?
.context("metadata file not found")?
);
let hash = path.read().content_hash(HashAlgorithm::default()).await?;
let hash = hash.as_ref().context("metadata file not found")?;
let use_numeric_sizes = ty == "twitter" || ty == "openGraph";
let sizes = if use_numeric_sizes {
......@@ -70,7 +66,7 @@ async fn dynamic_image_metadata_with_generator_source(
const data = {{
alt: imageMetadata.alt,
type: imageMetadata.contentType || 'image/png',
url: imageUrl + (idParam ? ('/' + idParam) : '') + {hash_query},
url: imageUrl + (idParam ? ('/' + idParam) : '') + '?' + {hash},
}}
const {{ size }} = imageMetadata
if (size) {{
......@@ -91,7 +87,7 @@ async fn dynamic_image_metadata_with_generator_source(
pathname_prefix = StringifyJs(&page.to_string()),
page_segment = StringifyJs(stem),
sizes = sizes,
hash_query = StringifyJs(&hash_query),
hash = StringifyJs(&hash),
};
let file = File::from(code);
......@@ -113,13 +109,8 @@ async fn dynamic_image_metadata_without_generator_source(
let stem = stem.unwrap_or_default();
let ext = path.extension();
let hash_query = format!(
"?{:x}",
path.read()
.content_hash()
.await?
.context("metadata file not found")?
);
let hash = path.read().content_hash(HashAlgorithm::default()).await?;
let hash = hash.as_ref().context("metadata file not found")?;
let use_numeric_sizes = ty == "twitter" || ty == "openGraph";
let sizes = if use_numeric_sizes {
......@@ -148,7 +139,7 @@ async fn dynamic_image_metadata_without_generator_source(
const data = {{
alt: imageMetadata.alt,
type: imageMetadata.contentType || 'image/png',
url: imageUrl + (idParam ? ('/' + idParam) : '') + {hash_query},
url: imageUrl + (idParam ? ('/' + idParam) : '') + '?' + {hash},
}}
const {{ size }} = imageMetadata
if (size) {{
......@@ -165,7 +156,7 @@ async fn dynamic_image_metadata_without_generator_source(
pathname_prefix = StringifyJs(&page.to_string()),
page_segment = StringifyJs(stem),
sizes = sizes,
hash_query = StringifyJs(&hash_query),
hash = StringifyJs(&hash),
};
let file = File::from(code);
......
......@@ -39,7 +39,7 @@ impl From<AssetPath> for NapiAssetPath {
fn from(asset_path: AssetPath) -> Self {
Self {
path: asset_path.path.into_owned(),
content_hash: format!("{:x}", asset_path.content_hash),
content_hash: asset_path.content_hash.into_owned(),
}
}
}
......
......@@ -80,7 +80,7 @@ describe('next-image-proxy', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(local).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=90"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=90"`
)
} else {
expect(local).toMatchInlineSnapshot(
......
......@@ -50,7 +50,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(layout.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=85"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=85"`
)
} else {
expect(layout.attr('src')).toMatchInlineSnapshot(
......@@ -60,7 +60,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(layout.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=85 2x"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=85 2x"`
)
} else {
expect(layout.attr('srcset')).toMatchInlineSnapshot(
......@@ -72,7 +72,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(page.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=90"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=90"`
)
} else {
expect(page.attr('src')).toMatchInlineSnapshot(
......@@ -82,7 +82,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(page.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=90 2x"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=90 2x"`
)
} else {
expect(page.attr('srcset')).toMatchInlineSnapshot(
......@@ -94,7 +94,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(comp.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=80"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=80"`
)
} else {
expect(comp.attr('src')).toMatchInlineSnapshot(
......@@ -104,7 +104,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(comp.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=80 2x"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=80 2x"`
)
} else {
expect(comp.attr('srcset')).toMatchInlineSnapshot(
......@@ -194,7 +194,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(await layout.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=85"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=85"`
)
} else {
expect(await layout.getAttribute('src')).toMatchInlineSnapshot(
......@@ -204,7 +204,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(await layout.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=85 2x"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=85 2x"`
)
} else {
expect(await layout.getAttribute('srcset')).toMatchInlineSnapshot(
......@@ -216,7 +216,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(await page.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=90"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=90"`
)
} else {
expect(await page.getAttribute('src')).toMatchInlineSnapshot(
......@@ -226,7 +226,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(await page.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=90 2x"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=90 2x"`
)
} else {
expect(await page.getAttribute('srcset')).toMatchInlineSnapshot(
......@@ -238,7 +238,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(await comp.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=80"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=80"`
)
} else {
expect(await comp.getAttribute('src')).toMatchInlineSnapshot(
......@@ -248,7 +248,7 @@ describe('app dir - next-image', () => {
if (process.env.IS_TURBOPACK_TEST) {
expect(await comp.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.4813cd24.png&w=828&q=80 2x"`
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.55df2443.png&w=828&q=80 2x"`
)
} else {
expect(await comp.getAttribute('srcset')).toMatchInlineSnapshot(
......
......@@ -564,8 +564,8 @@ module.exports = {
expect(cssContent).toMatchInlineSnapshot(`
[
"/_next/static/chunks/HASH.css:
.red-text{color:red;background-image:url(../media/dark.8425d343.svg),url(../media/dark2.8425d343.svg)}
.blue-text{color:orange;background-image:url(../media/light.fc9b5caa.svg);font-weight:bolder}
.red-text{color:red;background-image:url(../media/dark.656fe8ee.svg),url(../media/dark2.656fe8ee.svg)}
.blue-text{color:orange;background-image:url(../media/light.750c956a.svg);font-weight:bolder}
.blue-text{color:#00f}",
]
`)
......@@ -573,8 +573,8 @@ module.exports = {
expect(cssContent).toMatchInlineSnapshot(`
[
"/_next/static/chunks/HASH.css:
.red-text{color:red;background-image:url(../media/dark.8425d343.svg),url(../media/dark2.8425d343.svg)}
.blue-text{color:orange;background-image:url(../media/light.fc9b5caa.svg);font-weight:bolder}
.red-text{color:red;background-image:url(../media/dark.656fe8ee.svg),url(../media/dark2.656fe8ee.svg)}
.blue-text{color:orange;background-image:url(../media/light.750c956a.svg);font-weight:bolder}
.blue-text{color:#00f}",
]
`)
......@@ -640,8 +640,8 @@ describe('CSS URL via `file-loader` and asset prefix (1)', () => {
expect(cssContent).toMatchInlineSnapshot(`
[
"/_next/static/chunks/HASH.css:
.red-text{color:red;background-image:url(../media/dark.8425d343.svg) url(../media/dark2.8425d343.svg)}
.blue-text{color:orange;background-image:url(../media/light.fc9b5caa.svg);font-weight:bolder}
.red-text{color:red;background-image:url(../media/dark.656fe8ee.svg) url(../media/dark2.656fe8ee.svg)}
.blue-text{color:orange;background-image:url(../media/light.750c956a.svg);font-weight:bolder}
.blue-text{color:#00f}",
]
`)
......@@ -692,8 +692,8 @@ describe('CSS URL via `file-loader` and asset prefix (2)', () => {
expect(cssContent).toMatchInlineSnapshot(`
[
"/_next/static/chunks/HASH.css:
.red-text{color:red;background-image:url(../media/dark.8425d343.svg) url(../media/dark2.8425d343.svg)}
.blue-text{color:orange;background-image:url(../media/light.fc9b5caa.svg);font-weight:bolder}
.red-text{color:red;background-image:url(../media/dark.656fe8ee.svg) url(../media/dark2.656fe8ee.svg)}
.blue-text{color:orange;background-image:url(../media/light.750c956a.svg);font-weight:bolder}
.blue-text{color:#00f}",
]
`)
......
......@@ -58,7 +58,7 @@ urlencoding = { workspace = true }
criterion = { workspace = true, features = ["async_tokio"] }
rand = { workspace = true }
rstest = { workspace = true }
sha2 = "0.10.2"
sha2 = { workspace = true }
tempfile = { workspace = true }
turbo-tasks-testing = { workspace = true }
turbo-tasks-backend = { workspace = true }
......
......@@ -67,7 +67,9 @@ use turbo_tasks::{
ResolvedVc, TaskInput, TurboTasksApi, ValueToString, Vc, debug::ValueDebugFormat, effect,
mark_session_dependent, parallel, trace::TraceRawVcs, turbo_tasks_weak,
};
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher, hash_xxh3_hash64};
use turbo_tasks_hash::{
DeterministicHash, DeterministicHasher, HashAlgorithm, deterministic_hash, hash_xxh3_hash64,
};
use turbo_unix_path::{
get_parent_path, get_relative_path_to, join_path, normalize_path, sys_to_unix, unix_to_sys,
};
......@@ -2371,9 +2373,11 @@ impl FileContent {
/// Compared to [FileContent::hash], this hashes only the bytes of the file content and nothing
/// else. If there is no file content, it returns `None`.
#[turbo_tasks::function]
pub async fn content_hash(&self) -> Result<Vc<Option<u64>>> {
pub async fn content_hash(&self, algorithm: HashAlgorithm) -> Result<Vc<Option<RcStr>>> {
match self {
FileContent::Content(file) => Ok(Vc::cell(Some(hash_xxh3_hash64(&file.content)))),
FileContent::Content(file) => Ok(Vc::cell(Some(
deterministic_hash(file.content().content_hash(), algorithm).into(),
))),
FileContent::NotFound => Ok(Vc::cell(None)),
}
}
......
......@@ -391,6 +391,25 @@ impl DeterministicHash for Rope {
}
}
impl Rope {
/// Returns a DeterministicHash impl that only hashes the bytes of the rope (still regardless of
/// their structure).
///
/// The default (Deterministic)Hash implementation also includes the length of the rope. Be
/// careful when using this, as it would case `(Rope("abc"), Rope("def"))` and `(Rope("abcd"),
/// Rope("ef"))` to have the same hash. The best usecase is when the rope is the _whole_
/// datastructure being hashed and it isn't part of some other structure.
pub fn content_hash(&self) -> impl DeterministicHash + '_ {
RopeBytesOnlyHash(self)
}
}
pub struct RopeBytesOnlyHash<'a>(&'a Rope);
impl DeterministicHash for RopeBytesOnlyHash<'_> {
fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
self.0.data.deterministic_hash(state);
}
}
/// Encode as a len + raw bytes format using the encoder's [`bincode::enc::write::Writer`]. Encoding
/// [`Rope::to_bytes`] instead would be easier, but would require copying to an intermediate buffer.
///
......@@ -893,7 +912,7 @@ mod test {
};
use anyhow::Result;
use turbo_tasks_hash::hash_xxh3_hash64;
use turbo_tasks_hash::{DeterministicHasher, Xxh3Hash64Hasher, hash_xxh3_hash64};
use super::{InnerRope, Rope, RopeBuilder, RopeElem};
......@@ -1094,6 +1113,17 @@ mod test {
assert_eq!(hash_xxh3_hash64(a), hash_xxh3_hash64(b));
}
#[test]
fn content_hash() {
let rope = Rope::new(vec!["abc".into(), "def".into()]);
let string = "abcdef";
let mut hasher = Xxh3Hash64Hasher::default();
hasher.write_bytes(string.as_bytes());
assert_eq!(hash_xxh3_hash64(rope.content_hash()), hasher.finish());
}
#[test]
fn iteration() {
let shared = Rope::from("def");
......
......@@ -13,5 +13,8 @@ bench = false
workspace = true
[dependencies]
bincode = { workspace = true }
data-encoding = { workspace = true }
sha2 = { workspace = true }
turbo-tasks-macros = { workspace = true }
twox-hash = { workspace = true }
......@@ -6,10 +6,52 @@
mod deterministic_hash;
mod hex;
mod sha;
mod xxh3_hash64;
use bincode::{Decode, Encode};
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Decode, Encode)]
pub enum HashAlgorithm {
/// The default hash algorithm, use this when the exact hashing algorithm doesn't matter.
#[default]
Xxh3Hash64Hex,
/// Used for https://nextjs.org/docs/app/guides/content-security-policy#enabling-sri
Sha256Base64,
/// Used for https://nextjs.org/docs/app/guides/content-security-policy#enabling-sri
Sha384Base64,
/// Used for https://nextjs.org/docs/app/guides/content-security-policy#enabling-sri
Sha512Base64,
}
pub fn deterministic_hash<T: DeterministicHash>(input: T, algorithm: HashAlgorithm) -> String {
match algorithm {
HashAlgorithm::Xxh3Hash64Hex => {
let mut hasher = Xxh3Hash64Hasher::new();
input.deterministic_hash(&mut hasher);
encode_hex(hasher.finish())
}
HashAlgorithm::Sha256Base64 => {
let mut hasher = ShaHasher::new_sha256();
input.deterministic_hash(&mut hasher);
hasher.finish_base64()
}
HashAlgorithm::Sha384Base64 => {
let mut hasher = ShaHasher::new_sha384();
input.deterministic_hash(&mut hasher);
hasher.finish_base64()
}
HashAlgorithm::Sha512Base64 => {
let mut hasher = ShaHasher::new_sha512();
input.deterministic_hash(&mut hasher);
hasher.finish_base64()
}
}
}
pub use crate::{
deterministic_hash::{DeterministicHash, DeterministicHasher},
hex::encode_hex,
sha::ShaHasher,
xxh3_hash64::{Xxh3Hash64Hasher, hash_xxh3_hash64},
};
use sha2::{Digest, Sha256, Sha384, Sha512, digest::typenum::Unsigned};
use crate::{DeterministicHash, DeterministicHasher};
pub struct ShaHasher<D: Digest>(D);
impl<D: Digest> ShaHasher<D>
where
sha2::digest::Output<D>: core::fmt::LowerHex,
{
/// Uses the DeterministicHash trait to hash the input in a
/// cross-platform way.
pub fn write_value<T: DeterministicHash>(&mut self, input: T) {
input.deterministic_hash(self);
}
/// Uses the DeterministicHash trait to hash the input in a
/// cross-platform way.
pub fn write_ref<T: DeterministicHash>(&mut self, input: &T) {
input.deterministic_hash(self);
}
/// Finish the hash computation and return the digest as hex.
pub fn finish(self) -> String {
let result = self.0.finalize();
format!("{:01$x}", result, D::OutputSize::to_usize() * 2)
}
/// Finish the hash computation and return the digest as base64.
pub fn finish_base64(self) -> String {
let result = self.0.finalize();
data_encoding::BASE64.encode(result.as_slice())
}
}
impl<D: Digest> DeterministicHasher for ShaHasher<D> {
fn finish(&self) -> u64 {
panic!("use the ShaHasher non-trait function instead");
}
fn write_bytes(&mut self, bytes: &[u8]) {
self.0.update(bytes);
}
}
impl ShaHasher<Sha256> {
pub fn new_sha256() -> Self {
ShaHasher(Sha256::new())
}
}
impl ShaHasher<Sha384> {
pub fn new_sha384() -> Self {
ShaHasher(Sha384::new())
}
}
impl ShaHasher<Sha512> {
pub fn new_sha512() -> Self {
ShaHasher(Sha512::new())
}
}
......@@ -18,6 +18,7 @@ use bincode::{
use either::Either;
use turbo_frozenmap::{FrozenMap, FrozenSet};
use turbo_rcstr::RcStr;
use turbo_tasks_hash::HashAlgorithm;
// This import is necessary for derive macros to work, as their expansion refers to the crate
// name directly.
......@@ -72,7 +73,8 @@ impl_task_input! {
TaskId,
ValueTypeId,
Duration,
String
String,
HashAlgorithm
}
impl<T> TaskInput for Vec<T>
......
......@@ -14,6 +14,7 @@ use indexmap::{IndexMap, IndexSet};
use smallvec::SmallVec;
use turbo_frozenmap::{FrozenMap, FrozenSet};
use turbo_rcstr::RcStr;
use turbo_tasks_hash::HashAlgorithm;
use crate::RawVc;
......@@ -81,6 +82,7 @@ ignore!(
ignore!((), str, String, Duration, anyhow::Error, RcStr);
ignore!(Path, PathBuf);
ignore!(serde_json::Value, serde_json::Map<String, serde_json::Value>);
ignore!(HashAlgorithm);
impl<T: ?Sized> TraceRawVcs for PhantomData<T> {
fn trace_raw_vcs(&self, _trace_context: &mut TraceRawVcsContext) {}
......
......@@ -7,7 +7,7 @@ use turbo_tasks::{
trace::TraceRawVcs,
};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{DeterministicHash, encode_hex};
use turbo_tasks_hash::{DeterministicHash, HashAlgorithm};
use turbopack_core::{
asset::Asset,
chunk::{
......@@ -577,15 +577,20 @@ impl ChunkingContext for BrowserChunkingContext {
let Some(asset) = asset else {
bail!("chunk_path requires an asset when content hashing is enabled");
};
let hash = asset.content().content_hash().await?.context(
let hash = asset
.content()
.content_hash(HashAlgorithm::default())
.await?;
let hash = hash.as_ref().context(
"chunk_path requires an asset with file content when content hashing is \
enabled",
)?;
let length = length as usize;
let hash = &hash[0..length];
if let Some(prefix) = prefix {
format!("{prefix}-{hash:0length$x}{extension}").into()
format!("{prefix}-{hash}{extension}").into()
} else {
format!("{hash:0length$x}{extension}").into()
format!("{hash}{extension}").into()
}
}
};
......@@ -641,13 +646,13 @@ impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
async fn asset_path(
&self,
content_hash: Vc<u64>,
content_hash: Vc<RcStr>,
original_asset_ident: Vc<AssetIdent>,
tag: Option<RcStr>,
) -> Result<Vc<FileSystemPath>> {
let source_path = original_asset_ident.path().await?;
let basename = source_path.file_name();
let content_hash = encode_hex(*content_hash.await?);
let content_hash = content_hash.await?;
let asset_path = match source_path.extension_ref() {
Some(ext) => format!(
"{basename}.{content_hash}.{ext}",
......
......@@ -4,7 +4,7 @@ use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType,
};
use turbo_tasks_hash::Xxh3Hash64Hasher;
use turbo_tasks_hash::{HashAlgorithm, Xxh3Hash64Hasher};
use crate::version::{VersionedAssetContent, VersionedContent};
......@@ -121,9 +121,9 @@ impl AssetContent {
/// Compared to [AssetContent::hash], this hashes only the bytes of the file content and nothing
/// else. If there is no file content, it returns `None`.
#[turbo_tasks::function]
pub async fn content_hash(&self) -> Result<Vc<Option<u64>>> {
pub async fn content_hash(&self, algorithm: HashAlgorithm) -> Result<Vc<Option<RcStr>>> {
match self {
AssetContent::File(content) => Ok(content.content_hash()),
AssetContent::File(content) => Ok(content.content_hash(algorithm)),
AssetContent::Redirect { .. } => Ok(Vc::cell(None)),
}
}
......
......@@ -350,7 +350,7 @@ pub trait ChunkingContext {
#[turbo_tasks::function]
fn asset_path(
self: Vc<Self>,
content_hash: Vc<u64>,
content_hash: Vc<RcStr>,
original_asset_ident: Vc<AssetIdent>,
tag: Option<RcStr>,
) -> Vc<FileSystemPath>;
......
......@@ -7,7 +7,7 @@ use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs,
};
use turbo_tasks_fs::{FileContent, LinkType};
use turbo_tasks_hash::encode_hex;
use turbo_tasks_hash::HashAlgorithm;
use crate::asset::AssetContent;
......@@ -233,10 +233,12 @@ impl FileHashVersion {
pub async fn compute(asset_content: &AssetContent) -> Result<Vc<Self>> {
match asset_content {
AssetContent::File(file_vc) => {
let hash = file_vc.content_hash().await?.context("file not found")?;
Ok(Self::cell(FileHashVersion {
hash: encode_hex(hash).into(),
}))
let hash = file_vc
.content_hash(HashAlgorithm::default())
.owned()
.await?
.context("file not found")?;
Ok(Self::cell(FileHashVersion { hash }))
}
AssetContent::Redirect { .. } => Err(anyhow!("not a file")),
}
......
......@@ -3,7 +3,6 @@ use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, ResolvedVc, TryJoinIterExt, Upcast, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::encode_hex;
use turbopack_core::{
asset::Asset,
chunk::{
......@@ -455,13 +454,13 @@ impl ChunkingContext for NodeJsChunkingContext {
#[turbo_tasks::function]
async fn asset_path(
&self,
content_hash: Vc<u64>,
content_hash: Vc<RcStr>,
original_asset_ident: Vc<AssetIdent>,
tag: Option<RcStr>,
) -> Result<Vc<FileSystemPath>> {
let source_path = original_asset_ident.path().await?;
let basename = source_path.file_name();
let content_hash = encode_hex(*content_hash.await?);
let content_hash = content_hash.await?;
let asset_path = match source_path.extension_ref() {
Some(ext) => format!(
"{basename}.{content_hash}.{ext}",
......
......@@ -2,6 +2,7 @@ use anyhow::{Context, Result};
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::ChunkingContext,
......@@ -39,9 +40,13 @@ impl OutputAsset for StaticOutputAsset {
#[turbo_tasks::function]
async fn path(&self) -> Result<Vc<FileSystemPath>> {
let content = self.source.content();
let content_hash = content.content_hash().await?.context(
"Missing content when trying to generate the content hash for StaticOutputAsset",
)?;
let content_hash = content
.content_hash(HashAlgorithm::default())
.owned()
.await?
.context(
"Missing content when trying to generate the content hash for StaticOutputAsset",
)?;
Ok(self.chunking_context.asset_path(
Vc::cell(content_hash),
self.source.ident(),
......
/* [project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.module.css [test] (css) */
.style-module__3oVw9q__module-style {
cursor: url("../static/image.0ccdd6e3.png");
background-image: url("../static/image.0ccdd6e3.png");
list-style-image: url("../static/image.0ccdd6e3.png");
cursor: url("../static/image.7bbaf41b.png");
background-image: url("../static/image.7bbaf41b.png");
list-style-image: url("../static/image.7bbaf41b.png");
}
/* [project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css [test] (css) */
.style {
cursor: url("../static/image.0ccdd6e3.png");
background-image: url("../static/image.0ccdd6e3.png");
list-style-image: url("../static/image.0ccdd6e3.png");
cursor: url("../static/image.7bbaf41b.png");
background-image: url("../static/image.7bbaf41b.png");
list-style-image: url("../static/image.7bbaf41b.png");
}
.style-unresolveable {
......
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/turbopack_crates_turbopack-tests_tests_snapshot_import-meta_url_input_3fece31c._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/import-meta/url/input/asset.txt (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/asset.a5b1faf2.txt");}),
__turbopack_context__.q("/static/asset.6fdf3006.txt");}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/import-meta/url/input/mod.mjs [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
......
......@@ -5,7 +5,7 @@ module.exports = 'turbopack';
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/vercel.242d4ff2.cjs");}),
__turbopack_context__.q("/static/vercel.fad5a703.cjs");}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/vercel.cjs [test] (ecmascript, worker loader)", ((__turbopack_context__) => {
__turbopack_context__.v(function(Ctor, opts) {
......@@ -14,7 +14,7 @@ __turbopack_context__.v(function(Ctor, opts) {
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/ignore-worker.cjs (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/ignore-worker.481250f3.cjs");}),
__turbopack_context__.q("/static/ignore-worker.4e0cf842.cjs");}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/ignore-comments/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
......
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/turbopack_crates_turbopack-tests_tests_snapshot_imports_static_input_fb142aa8._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/static/input/vercel.svg (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/vercel.ede1923f.svg");}),
__turbopack_context__.q("/static/vercel.77adf0a9.svg");}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/static/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
......
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/turbopack_crates_turbopack-tests_tests_snapshot_workers_basic_input_73fc86c5._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/workers/basic/input/worker.js (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/worker.60655f93.js");}),
__turbopack_context__.q("/static/worker.adfa2c73.js");}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/workers/basic/input/worker.js [test] (ecmascript, worker loader)", ((__turbopack_context__) => {
__turbopack_context__.v(function(Ctor, opts) {
......
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/turbopack_crates_turbopack-tests_tests_snapshot_workers_shared_input_3845375a._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/workers/shared/input/worker.js (static in ecmascript)", ((__turbopack_context__) => {
__turbopack_context__.q("/static/worker.35b5336b.js");}),
__turbopack_context__.q("/static/worker.b01ba0d1.js");}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/workers/shared/input/worker.js [test] (ecmascript, worker loader)", ((__turbopack_context__) => {
__turbopack_context__.v(function(Ctor, opts) {
......
......@@ -12,6 +12,7 @@
use anyhow::{Context, Result};
use turbo_rcstr::RcStr;
use turbo_tasks::Vc;
use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::asset::Asset;
pub(crate) mod analysis;
......@@ -25,8 +26,10 @@ pub mod source;
pub async fn wasm_edge_var_name(asset: Vc<Box<dyn Asset>>) -> Result<Vc<RcStr>> {
let hash = asset
.content()
.content_hash()
.await?
.content_hash(HashAlgorithm::default())
.await?;
let hash = hash
.as_ref()
.context("Missing content when trying to generate the content hash for a WASM asset")?;
Ok(Vc::cell(format!("wasm_{:08x}", hash).into()))
Ok(Vc::cell(format!("wasm_{}", hash).into()))
}
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