Skip to main content

turbopack_core/introspect/
utils.rs

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