turbopack_core/introspect/
utils.rs

1use anyhow::Result;
2use turbo_rcstr::RcStr;
3use turbo_tasks::{FxIndexSet, ResolvedVc, Vc};
4use turbo_tasks_fs::FileContent;
5
6use super::{
7    IntrospectableChildren, module::IntrospectableModule, output_asset::IntrospectableOutputAsset,
8};
9use crate::{
10    asset::AssetContent,
11    chunk::{ChunkableModuleReference, ChunkingType},
12    output::OutputAssets,
13    reference::{ModuleReference, ModuleReferences},
14};
15
16#[turbo_tasks::function]
17fn reference_ty() -> Vc<RcStr> {
18    Vc::cell("reference".into())
19}
20
21#[turbo_tasks::function]
22fn parallel_reference_ty() -> Vc<RcStr> {
23    Vc::cell("parallel reference".into())
24}
25
26#[turbo_tasks::function]
27fn parallel_inherit_async_reference_ty() -> Vc<RcStr> {
28    Vc::cell("parallel reference (inherit async module)".into())
29}
30
31#[turbo_tasks::function]
32fn async_reference_ty() -> Vc<RcStr> {
33    Vc::cell("async reference".into())
34}
35
36#[turbo_tasks::function]
37fn isolated_reference_ty() -> Vc<RcStr> {
38    Vc::cell("isolated reference".into())
39}
40
41#[turbo_tasks::function]
42fn shared_reference_ty() -> Vc<RcStr> {
43    Vc::cell("shared reference".into())
44}
45
46#[turbo_tasks::function]
47fn traced_reference_ty() -> Vc<RcStr> {
48    Vc::cell("traced reference".into())
49}
50
51#[turbo_tasks::function]
52pub async fn content_to_details(content: Vc<AssetContent>) -> Result<Vc<RcStr>> {
53    Ok(match &*content.await? {
54        AssetContent::File(file_content) => match &*file_content.await? {
55            FileContent::Content(file) => {
56                let content = file.content();
57                match content.to_str() {
58                    Ok(str) => Vc::cell(str.into()),
59                    Err(_) => Vc::cell(format!("{} binary bytes", content.len()).into()),
60                }
61            }
62            FileContent::NotFound => Vc::cell("not found".into()),
63        },
64        AssetContent::Redirect { target, link_type } => {
65            Vc::cell(format!("redirect to {target} with type {link_type:?}").into())
66        }
67    })
68}
69
70#[turbo_tasks::function]
71pub async fn children_from_module_references(
72    references: Vc<ModuleReferences>,
73) -> Result<Vc<IntrospectableChildren>> {
74    let key = reference_ty();
75    let mut children = FxIndexSet::default();
76    let references = references.await?;
77    for &reference in &*references {
78        let mut key = key;
79        if let Some(chunkable) =
80            ResolvedVc::try_downcast::<Box<dyn ChunkableModuleReference>>(reference)
81        {
82            match &*chunkable.chunking_type().await? {
83                None => {}
84                Some(ChunkingType::Parallel { .. }) => key = parallel_reference_ty(),
85                Some(ChunkingType::Async) => key = async_reference_ty(),
86                Some(ChunkingType::Isolated { .. }) => key = isolated_reference_ty(),
87                Some(ChunkingType::Shared { .. }) => key = shared_reference_ty(),
88                Some(ChunkingType::Traced) => key = traced_reference_ty(),
89            }
90        }
91
92        let key = key.to_resolved().await?;
93        for &module in reference
94            .resolve_reference()
95            .resolve()
96            .await?
97            .primary_modules()
98            .await?
99            .iter()
100        {
101            children.insert((key, IntrospectableModule::new(*module).to_resolved().await?));
102        }
103        for &output_asset in reference
104            .resolve_reference()
105            .primary_output_assets()
106            .await?
107            .iter()
108        {
109            children.insert((
110                key,
111                IntrospectableOutputAsset::new(*output_asset)
112                    .to_resolved()
113                    .await?,
114            ));
115        }
116    }
117    Ok(Vc::cell(children))
118}
119
120#[turbo_tasks::function]
121pub async fn children_from_output_assets(
122    references: Vc<OutputAssets>,
123) -> Result<Vc<IntrospectableChildren>> {
124    let key = reference_ty().to_resolved().await?;
125    let mut children = FxIndexSet::default();
126    let references = references.await?;
127    for &reference in &*references {
128        children.insert((
129            key,
130            IntrospectableOutputAsset::new(*ResolvedVc::upcast(reference))
131                .to_resolved()
132                .await?,
133        ));
134    }
135    Ok(Vc::cell(children))
136}