turbopack_core/introspect/
utils.rs1use anyhow::Result;
2use turbo_rcstr::{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
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 = if let Some(chunkable) =
72 ResolvedVc::try_downcast::<Box<dyn ChunkableModuleReference>>(reference)
73 {
74 match &*chunkable.chunking_type().await? {
75 None => key.clone(),
76 Some(ChunkingType::Parallel { inherit_async, .. }) => {
77 if *inherit_async {
78 parallel_inherit_async_reference_ty()
79 } else {
80 parallel_reference_ty()
81 }
82 }
83 Some(ChunkingType::Async) => async_reference_ty(),
84 Some(ChunkingType::Isolated { .. }) => isolated_reference_ty(),
85 Some(ChunkingType::Shared { .. }) => shared_reference_ty(),
86 Some(ChunkingType::Traced) => traced_reference_ty(),
87 }
88 } else {
89 key.clone()
90 };
91
92 for &module in reference
93 .resolve_reference()
94 .resolve()
95 .await?
96 .primary_modules()
97 .await?
98 .iter()
99 {
100 children.insert((
101 key.clone(),
102 IntrospectableModule::new(*module).to_resolved().await?,
103 ));
104 }
105 for &output_asset in reference
106 .resolve_reference()
107 .primary_output_assets()
108 .await?
109 .iter()
110 {
111 children.insert((
112 key.clone(),
113 IntrospectableOutputAsset::new(*output_asset)
114 .to_resolved()
115 .await?,
116 ));
117 }
118 }
119 Ok(Vc::cell(children))
120}
121
122#[turbo_tasks::function]
123pub async fn children_from_output_assets(
124 references: Vc<OutputAssets>,
125) -> Result<Vc<IntrospectableChildren>> {
126 let key = reference_ty();
127 let mut children = FxIndexSet::default();
128 let references = references.await?;
129 for &reference in &*references {
130 children.insert((
131 key.clone(),
132 IntrospectableOutputAsset::new(*ResolvedVc::upcast(reference))
133 .to_resolved()
134 .await?,
135 ));
136 }
137 Ok(Vc::cell(children))
138}