File size: 13,044 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
use std::{collections::VecDeque, iter::once};
use anyhow::Result;
use rustc_hash::FxHashSet;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
Completion, FxIndexMap, FxIndexSet, ResolvedVc, State, TryJoinIterExt, Vc, fxindexset,
};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
asset::Asset,
introspect::{Introspectable, IntrospectableChildren, output_asset::IntrospectableOutputAsset},
output::{OutputAsset, OutputAssetsSet},
};
use super::{
ContentSource, ContentSourceContent, ContentSourceData, ContentSourceSideEffect,
GetContentSourceContent,
route_tree::{BaseSegment, RouteTree, RouteTrees, RouteType},
};
#[turbo_tasks::value(transparent)]
struct OutputAssetsMap(FxIndexMap<RcStr, ResolvedVc<Box<dyn OutputAsset>>>);
type ExpandedState = State<FxHashSet<RcStr>>;
#[turbo_tasks::value(serialization = "none", eq = "manual", cell = "new")]
pub struct AssetGraphContentSource {
root_path: FileSystemPath,
root_assets: ResolvedVc<OutputAssetsSet>,
expanded: Option<ExpandedState>,
}
#[turbo_tasks::value_impl]
impl AssetGraphContentSource {
/// Serves all assets references by root_asset.
#[turbo_tasks::function]
pub fn new_eager(
root_path: FileSystemPath,
root_asset: ResolvedVc<Box<dyn OutputAsset>>,
) -> Vc<Self> {
Self::cell(AssetGraphContentSource {
root_path,
root_assets: ResolvedVc::cell(fxindexset! { root_asset }),
expanded: None,
})
}
/// Serves all assets references by root_asset. Only serve references of an
/// asset when it has served its content before.
#[turbo_tasks::function]
pub fn new_lazy(
root_path: FileSystemPath,
root_asset: ResolvedVc<Box<dyn OutputAsset>>,
) -> Vc<Self> {
Self::cell(AssetGraphContentSource {
root_path,
root_assets: ResolvedVc::cell(fxindexset! { root_asset }),
expanded: Some(State::new(FxHashSet::default())),
})
}
/// Serves all assets references by all root_assets.
#[turbo_tasks::function]
pub fn new_eager_multiple(
root_path: FileSystemPath,
root_assets: ResolvedVc<OutputAssetsSet>,
) -> Vc<Self> {
Self::cell(AssetGraphContentSource {
root_path,
root_assets,
expanded: None,
})
}
/// Serves all assets references by all root_assets. Only serve references
/// of an asset when it has served its content before.
#[turbo_tasks::function]
pub fn new_lazy_multiple(
root_path: FileSystemPath,
root_assets: ResolvedVc<OutputAssetsSet>,
) -> Vc<Self> {
Self::cell(AssetGraphContentSource {
root_path,
root_assets,
expanded: Some(State::new(FxHashSet::default())),
})
}
#[turbo_tasks::function]
async fn all_assets_map(&self) -> Result<Vc<OutputAssetsMap>> {
Ok(Vc::cell(
expand(
&*self.root_assets.await?,
&self.root_path,
self.expanded.as_ref(),
)
.await?,
))
}
}
async fn expand(
root_assets: &FxIndexSet<ResolvedVc<Box<dyn OutputAsset>>>,
root_path: &FileSystemPath,
expanded: Option<&ExpandedState>,
) -> Result<FxIndexMap<RcStr, ResolvedVc<Box<dyn OutputAsset>>>> {
let mut map = FxIndexMap::default();
let mut assets = Vec::new();
let mut queue = VecDeque::with_capacity(32);
let mut assets_set = FxHashSet::default();
let root_assets_with_path = root_assets
.iter()
.map(|&asset| async move {
let path = asset.path().await?;
Ok((path, asset))
})
.try_join()
.await?;
if let Some(expanded) = &expanded {
let expanded = expanded.get();
for (path, root_asset) in root_assets_with_path.into_iter() {
if let Some(sub_path) = root_path.get_path_to(&path) {
let (sub_paths_buffer, sub_paths) = get_sub_paths(sub_path);
let expanded = sub_paths_buffer
.iter()
.take(sub_paths)
.any(|sub_path| expanded.contains(sub_path));
for sub_path in sub_paths_buffer.into_iter().take(sub_paths) {
assets.push((sub_path, root_asset));
}
assets_set.insert(root_asset);
if expanded {
queue.push_back(root_asset.references());
}
}
}
} else {
for (path, root_asset) in root_assets_with_path.into_iter() {
if let Some(sub_path) = root_path.get_path_to(&path) {
let (sub_paths_buffer, sub_paths) = get_sub_paths(sub_path);
for sub_path in sub_paths_buffer.into_iter().take(sub_paths) {
assets.push((sub_path, root_asset));
}
queue.push_back(root_asset.references());
assets_set.insert(root_asset);
}
}
}
while let Some(references) = queue.pop_front() {
for asset in references.await?.iter() {
if assets_set.insert(*asset) {
let path = asset.path().await?;
if let Some(sub_path) = root_path.get_path_to(&path) {
let (sub_paths_buffer, sub_paths) = get_sub_paths(sub_path);
let expanded = if let Some(expanded) = &expanded {
let expanded = expanded.get();
sub_paths_buffer
.iter()
.take(sub_paths)
.any(|sub_path| expanded.contains(sub_path))
} else {
true
};
if expanded {
queue.push_back(asset.references());
}
for sub_path in sub_paths_buffer.into_iter().take(sub_paths) {
assets.push((sub_path, *asset));
}
}
}
}
}
for (sub_path, asset) in assets {
if &*sub_path == "index.html" {
map.insert(rcstr!(""), asset);
} else if let Some(p) = sub_path.strip_suffix("/index.html") {
map.insert(p.into(), asset);
map.insert(format!("{p}/").into(), asset);
} else if let Some(p) = sub_path.strip_suffix(".html") {
map.insert(p.into(), asset);
}
map.insert(sub_path, asset);
}
Ok(map)
}
fn get_sub_paths(sub_path: &str) -> ([RcStr; 3], usize) {
let sub_paths_buffer: [RcStr; 3];
let n = if sub_path == "index.html" {
sub_paths_buffer = [rcstr!(""), sub_path.into(), Default::default()];
2
} else if let Some(p) = sub_path.strip_suffix("/index.html") {
sub_paths_buffer = [p.into(), format!("{p}/").into(), sub_path.into()];
3
} else if let Some(p) = sub_path.strip_suffix(".html") {
sub_paths_buffer = [p.into(), sub_path.into(), Default::default()];
2
} else {
sub_paths_buffer = [sub_path.into(), Default::default(), Default::default()];
1
};
(sub_paths_buffer, n)
}
#[turbo_tasks::function(operation)]
fn all_assets_map_operation(source: ResolvedVc<AssetGraphContentSource>) -> Vc<OutputAssetsMap> {
source.all_assets_map()
}
#[turbo_tasks::value_impl]
impl ContentSource for AssetGraphContentSource {
#[turbo_tasks::function]
async fn get_routes(self: ResolvedVc<Self>) -> Result<Vc<RouteTree>> {
let assets = all_assets_map_operation(self)
.read_strongly_consistent()
.await?;
let mut paths = Vec::new();
let routes = assets
.iter()
.map(|(path, asset)| {
paths.push(path.as_str());
RouteTree::new_route(
BaseSegment::from_static_pathname(path).collect(),
RouteType::Exact,
Vc::upcast(AssetGraphGetContentSourceContent::new(
*self,
path.clone(),
**asset,
)),
)
})
.map(|v| async move { v.to_resolved().await })
.try_join()
.await?;
Ok(Vc::<RouteTrees>::cell(routes).merge())
}
}
#[turbo_tasks::value]
struct AssetGraphGetContentSourceContent {
source: ResolvedVc<AssetGraphContentSource>,
path: RcStr,
asset: ResolvedVc<Box<dyn OutputAsset>>,
}
#[turbo_tasks::value_impl]
impl AssetGraphGetContentSourceContent {
#[turbo_tasks::function]
pub fn new(
source: ResolvedVc<AssetGraphContentSource>,
path: RcStr,
asset: ResolvedVc<Box<dyn OutputAsset>>,
) -> Vc<Self> {
Self::cell(AssetGraphGetContentSourceContent {
source,
path,
asset,
})
}
}
#[turbo_tasks::value_impl]
impl GetContentSourceContent for AssetGraphGetContentSourceContent {
#[turbo_tasks::function]
async fn get(
self: ResolvedVc<Self>,
_path: RcStr,
_data: ContentSourceData,
) -> Result<Vc<ContentSourceContent>> {
let this = self.await?;
turbo_tasks::emit(ResolvedVc::upcast::<Box<dyn ContentSourceSideEffect>>(self));
Ok(ContentSourceContent::static_content(
this.asset.versioned_content(),
))
}
}
#[turbo_tasks::value_impl]
impl ContentSourceSideEffect for AssetGraphGetContentSourceContent {
#[turbo_tasks::function]
async fn apply(&self) -> Result<Vc<Completion>> {
let source = self.source.await?;
if let Some(expanded) = &source.expanded {
expanded.update_conditionally(|expanded| expanded.insert(self.path.clone()));
}
Ok(Completion::new())
}
}
#[turbo_tasks::value_impl]
impl Introspectable for AssetGraphContentSource {
#[turbo_tasks::function]
fn ty(&self) -> Vc<RcStr> {
Vc::cell(rcstr!("asset graph content source"))
}
#[turbo_tasks::function]
fn title(&self) -> Vc<RcStr> {
self.root_path.value_to_string()
}
#[turbo_tasks::function]
fn details(&self) -> Vc<RcStr> {
Vc::cell(if let Some(expanded) = &self.expanded {
format!("{} assets expanded", expanded.get().len()).into()
} else {
rcstr!("eager")
})
}
#[turbo_tasks::function]
async fn children(self: Vc<Self>) -> Result<Vc<IntrospectableChildren>> {
let this = self.await?;
let root_assets = this.root_assets.await?;
let root_asset_children = root_assets
.iter()
.map(|&asset| async move {
Ok((
rcstr!("root"),
IntrospectableOutputAsset::new(*ResolvedVc::upcast(asset))
.to_resolved()
.await?,
))
})
.try_join()
.await?;
let expanded_assets = self.all_assets_map().await?;
let expanded_asset_children = expanded_assets
.values()
.filter(|&a| !root_assets.contains(a))
.map(|&asset| async move {
Ok((
rcstr!("inner"),
IntrospectableOutputAsset::new(*ResolvedVc::upcast(asset))
.to_resolved()
.await?,
))
})
.try_join()
.await?;
Ok(Vc::cell(
root_asset_children
.into_iter()
.chain(expanded_asset_children)
.chain(once((
rcstr!("expanded"),
ResolvedVc::upcast(FullyExpanded(self.to_resolved().await?).resolved_cell()),
)))
.collect(),
))
}
}
#[turbo_tasks::value]
struct FullyExpanded(ResolvedVc<AssetGraphContentSource>);
#[turbo_tasks::value_impl]
impl Introspectable for FullyExpanded {
#[turbo_tasks::function]
fn ty(&self) -> Vc<RcStr> {
Vc::cell(rcstr!("fully expanded asset graph content source"))
}
#[turbo_tasks::function]
async fn title(&self) -> Result<Vc<RcStr>> {
Ok(self.0.await?.root_path.value_to_string())
}
#[turbo_tasks::function]
async fn children(&self) -> Result<Vc<IntrospectableChildren>> {
let source = self.0.await?;
let expanded_assets = expand(&*source.root_assets.await?, &source.root_path, None).await?;
let children = expanded_assets
.iter()
.map(|(_k, &v)| async move {
Ok((
rcstr!("asset"),
IntrospectableOutputAsset::new(*v).to_resolved().await?,
))
})
.try_join()
.await?
.into_iter()
.collect();
Ok(Vc::cell(children))
}
}
|