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 trait_ref = reference.into_trait_ref().await?;
72        let key = match &trait_ref.chunking_type() {
73            None => key.clone(),
74            Some(ChunkingType::Parallel { inherit_async, .. }) => {
75                if *inherit_async {
76                    parallel_inherit_async_reference_ty()
77                } else {
78                    parallel_reference_ty()
79                }
80            }
81            Some(ChunkingType::Async) => async_reference_ty(),
82            Some(ChunkingType::Isolated { .. }) => isolated_reference_ty(),
83            Some(ChunkingType::Shared { .. }) => shared_reference_ty(),
84            Some(ChunkingType::Traced { .. }) => traced_reference_ty(),
85        };
86
87        for &module in reference
88            .resolve_reference()
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    }
100    Ok(Vc::cell(children))
101}
102
103#[turbo_tasks::function]
104pub async fn children_from_output_assets(
105    references: Vc<OutputAssetsWithReferenced>,
106) -> Result<Vc<IntrospectableChildren>> {
107    let key = reference_ty();
108    let mut children = FxIndexSet::default();
109    let references = references.expand_all_assets().await?;
110    for &reference in &*references {
111        children.insert((
112            key.clone(),
113            IntrospectableOutputAsset::new(*reference)
114                .to_resolved()
115                .await?,
116        ));
117    }
118    Ok(Vc::cell(children))
119}