|
|
use std::sync::Arc; |
|
|
|
|
|
use anyhow::{Result, anyhow}; |
|
|
use turbo_rcstr::RcStr; |
|
|
use turbo_tasks::{ |
|
|
IntoTraitRef, NonLocalValue, OperationValue, ReadRef, ResolvedVc, State, TraitRef, Vc, |
|
|
debug::ValueDebugFormat, trace::TraceRawVcs, |
|
|
}; |
|
|
use turbo_tasks_fs::{FileContent, LinkType}; |
|
|
use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64}; |
|
|
|
|
|
use crate::asset::AssetContent; |
|
|
|
|
|
#[turbo_tasks::value(transparent)] |
|
|
pub struct OptionVersionedContent(Option<ResolvedVc<Box<dyn VersionedContent>>>); |
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait VersionedContent { |
|
|
|
|
|
#[turbo_tasks::function] |
|
|
fn content(self: Vc<Self>) -> Vc<AssetContent>; |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
fn version(self: Vc<Self>) -> Vc<Box<dyn Version>>; |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
async fn update(self: Vc<Self>, from: Vc<Box<dyn Version>>) -> Result<Vc<Update>> { |
|
|
|
|
|
|
|
|
|
|
|
let to = self.version(); |
|
|
let from_ref = from.into_trait_ref().await?; |
|
|
let to_ref = to.into_trait_ref().await?; |
|
|
|
|
|
|
|
|
if TraitRef::ptr_eq(&from_ref, &to_ref) { |
|
|
return Ok(Update::None.into()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let from_id = from.id(); |
|
|
let to_id = to.id(); |
|
|
let from_id = from_id.await?; |
|
|
let to_id = to_id.await?; |
|
|
Ok(if *from_id == *to_id { |
|
|
Update::None.into() |
|
|
} else { |
|
|
Update::Total(TotalUpdate { to: to_ref }).into() |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value] |
|
|
pub struct VersionedAssetContent { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asset_content: ReadRef<AssetContent>, |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value] |
|
|
#[derive(Clone)] |
|
|
enum AssetContentSnapshot { |
|
|
File(ReadRef<FileContent>), |
|
|
Redirect { target: String, link_type: LinkType }, |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl VersionedContent for VersionedAssetContent { |
|
|
#[turbo_tasks::function] |
|
|
fn content(&self) -> Vc<AssetContent> { |
|
|
(*self.asset_content).clone().cell() |
|
|
} |
|
|
|
|
|
#[turbo_tasks::function] |
|
|
async fn version(&self) -> Result<Vc<Box<dyn Version>>> { |
|
|
Ok(Vc::upcast( |
|
|
FileHashVersion::compute(&self.asset_content).await?, |
|
|
)) |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl VersionedAssetContent { |
|
|
#[turbo_tasks::function] |
|
|
|
|
|
pub async fn new(asset_content: Vc<AssetContent>) -> Result<Vc<Self>> { |
|
|
let asset_content = asset_content.await?; |
|
|
Ok(Self::cell(VersionedAssetContent { asset_content })) |
|
|
} |
|
|
} |
|
|
|
|
|
impl From<AssetContent> for Vc<VersionedAssetContent> { |
|
|
fn from(asset_content: AssetContent) -> Self { |
|
|
VersionedAssetContent::new(asset_content.cell()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl From<AssetContent> for Vc<Box<dyn VersionedContent>> { |
|
|
fn from(asset_content: AssetContent) -> Self { |
|
|
Vc::upcast(VersionedAssetContent::new(asset_content.cell())) |
|
|
} |
|
|
} |
|
|
|
|
|
pub trait VersionedContentExt: Send { |
|
|
fn versioned(self: Vc<Self>) -> Vc<Box<dyn VersionedContent>>; |
|
|
} |
|
|
|
|
|
impl VersionedContentExt for AssetContent { |
|
|
fn versioned(self: Vc<Self>) -> Vc<Box<dyn VersionedContent>> { |
|
|
Vc::upcast(VersionedAssetContent::new(self)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait Version { |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
fn id(self: Vc<Self>) -> Vc<RcStr>; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait MergeableVersionedContent: VersionedContent { |
|
|
#[turbo_tasks::function] |
|
|
fn get_merger(self: Vc<Self>) -> Vc<Box<dyn VersionedContentMerger>>; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value_trait] |
|
|
pub trait VersionedContentMerger { |
|
|
#[turbo_tasks::function] |
|
|
fn merge(self: Vc<Self>, contents: Vc<VersionedContents>) -> Vc<Box<dyn VersionedContent>>; |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value(transparent)] |
|
|
pub struct VersionedContents(Vec<ResolvedVc<Box<dyn VersionedContent>>>); |
|
|
|
|
|
#[turbo_tasks::value(operation)] |
|
|
pub struct NotFoundVersion; |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl NotFoundVersion { |
|
|
#[turbo_tasks::function] |
|
|
pub fn new() -> Vc<Self> { |
|
|
NotFoundVersion.cell() |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl Version for NotFoundVersion { |
|
|
#[turbo_tasks::function] |
|
|
fn id(&self) -> Vc<RcStr> { |
|
|
Vc::cell(Default::default()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value(serialization = "none", shared)] |
|
|
#[derive(Debug)] |
|
|
pub enum Update { |
|
|
|
|
|
|
|
|
Total(TotalUpdate), |
|
|
|
|
|
|
|
|
|
|
|
Partial(PartialUpdate), |
|
|
|
|
|
|
|
|
Missing, |
|
|
|
|
|
|
|
|
None, |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, TraceRawVcs, ValueDebugFormat, NonLocalValue)] |
|
|
pub struct TotalUpdate { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks(trace_ignore)] |
|
|
pub to: TraitRef<Box<dyn Version>>, |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, TraceRawVcs, ValueDebugFormat, NonLocalValue)] |
|
|
pub struct PartialUpdate { |
|
|
|
|
|
|
|
|
#[turbo_tasks(trace_ignore)] |
|
|
pub to: TraitRef<Box<dyn Version>>, |
|
|
|
|
|
|
|
|
#[turbo_tasks(trace_ignore)] |
|
|
pub instruction: Arc<serde_json::Value>, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::value(operation)] |
|
|
#[derive(Clone)] |
|
|
pub struct FileHashVersion { |
|
|
hash: RcStr, |
|
|
} |
|
|
|
|
|
impl FileHashVersion { |
|
|
|
|
|
pub async fn compute(asset_content: &AssetContent) -> Result<Vc<Self>> { |
|
|
match asset_content { |
|
|
AssetContent::File(file_vc) => match &*file_vc.await? { |
|
|
FileContent::Content(file) => { |
|
|
let hash = hash_xxh3_hash64(file.content()); |
|
|
let hex_hash = encode_hex(hash); |
|
|
Ok(Self::cell(FileHashVersion { |
|
|
hash: hex_hash.into(), |
|
|
})) |
|
|
} |
|
|
FileContent::NotFound => Err(anyhow!("file not found")), |
|
|
}, |
|
|
AssetContent::Redirect { .. } => Err(anyhow!("not a file")), |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl Version for FileHashVersion { |
|
|
#[turbo_tasks::function] |
|
|
fn id(&self) -> Vc<RcStr> { |
|
|
Vc::cell(self.hash.clone()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, TraceRawVcs, NonLocalValue, OperationValue)] |
|
|
struct VersionRef( |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks(trace_ignore)] TraitRef<Box<dyn Version>>, |
|
|
); |
|
|
|
|
|
#[turbo_tasks::value(serialization = "none")] |
|
|
pub struct VersionState { |
|
|
version: State<VersionRef>, |
|
|
} |
|
|
|
|
|
#[turbo_tasks::value_impl] |
|
|
impl VersionState { |
|
|
#[turbo_tasks::function] |
|
|
pub fn get(&self) -> Vc<Box<dyn Version>> { |
|
|
TraitRef::cell(self.version.get().0.clone()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl VersionState { |
|
|
pub async fn new(version: TraitRef<Box<dyn Version>>) -> Result<Vc<Self>> { |
|
|
Ok(Self::cell(VersionState { |
|
|
version: State::new(VersionRef(version)), |
|
|
})) |
|
|
} |
|
|
|
|
|
pub async fn set(self: Vc<Self>, new_version: TraitRef<Box<dyn Version>>) -> Result<()> { |
|
|
let this = self.await?; |
|
|
this.version.set(VersionRef(new_version)); |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|