@@ -29,11 +29,15 @@ pub use spin_capabilities::InheritConfiguration;
2929/// dependent component. Finally, the composer will export all exports from the
3030/// dependent component to its dependents. The composer will then encode the
3131/// composition graph into a byte array and return it.
32- pub async fn compose < L : ComponentSourceLoader > (
32+ pub async fn compose <
33+ L : ComponentSourceLoader ,
34+ Fut : std:: future:: Future < Output = Result < Vec < u8 > , ComposeError > > ,
35+ > (
3336 loader : & L ,
3437 component : & L :: Component ,
38+ complicator : impl Fn ( Vec < u8 > ) -> Fut ,
3539) -> Result < Vec < u8 > , ComposeError > {
36- Composer :: new ( loader) . compose ( component) . await
40+ Composer :: new ( loader) . compose ( component, complicator ) . await
3741}
3842
3943/// A Spin component dependency. This abstracts over the metadata associated with the
@@ -90,8 +94,10 @@ impl DependencyLike for spin_app::locked::LockedComponentDependency {
9094pub trait ComponentSourceLoader {
9195 type Component : ComponentLike < Dependency = Self :: Dependency > ;
9296 type Dependency : DependencyLike ;
97+ type Source ;
9398 async fn load_component_source ( & self , source : & Self :: Component ) -> anyhow:: Result < Vec < u8 > > ;
9499 async fn load_dependency_source ( & self , source : & Self :: Dependency ) -> anyhow:: Result < Vec < u8 > > ;
100+ async fn load_source ( & self , source : & Self :: Source ) -> anyhow:: Result < Vec < u8 > > ;
95101}
96102
97103/// A ComponentSourceLoader that loads component sources from the filesystem.
@@ -101,6 +107,7 @@ pub struct ComponentSourceLoaderFs;
101107impl ComponentSourceLoader for ComponentSourceLoaderFs {
102108 type Component = spin_app:: locked:: LockedComponent ;
103109 type Dependency = spin_app:: locked:: LockedComponentDependency ;
110+ type Source = spin_app:: locked:: LockedComponentSource ;
104111
105112 async fn load_component_source ( & self , source : & Self :: Component ) -> anyhow:: Result < Vec < u8 > > {
106113 Self :: load_from_locked_source ( & source. source ) . await
@@ -109,6 +116,10 @@ impl ComponentSourceLoader for ComponentSourceLoaderFs {
109116 async fn load_dependency_source ( & self , source : & Self :: Dependency ) -> anyhow:: Result < Vec < u8 > > {
110117 Self :: load_from_locked_source ( & source. source ) . await
111118 }
119+
120+ async fn load_source ( & self , source : & Self :: Source ) -> anyhow:: Result < Vec < u8 > > {
121+ Self :: load_from_locked_source ( source) . await
122+ }
112123}
113124
114125impl ComponentSourceLoaderFs {
@@ -195,39 +206,47 @@ struct Composer<'a, L> {
195206}
196207
197208impl < ' a , L : ComponentSourceLoader > Composer < ' a , L > {
198- async fn compose ( mut self , component : & L :: Component ) -> Result < Vec < u8 > , ComposeError > {
209+ async fn compose < Fut : std:: future:: Future < Output = Result < Vec < u8 > , ComposeError > > > (
210+ mut self ,
211+ component : & L :: Component ,
212+ complicator : impl Fn ( Vec < u8 > ) -> Fut ,
213+ ) -> Result < Vec < u8 > , ComposeError > {
199214 let source = self
200215 . loader
201216 . load_component_source ( component)
202217 . await
203218 . map_err ( ComposeError :: PrepareError ) ?;
204219
205- if component. dependencies ( ) . len ( ) == 0 {
206- return Ok ( source) ;
207- }
220+ let fulfilled_source = if component. dependencies ( ) . len ( ) == 0 {
221+ source
222+ } else {
223+ let ( world_id, instantiation_id) = self
224+ . register_package ( component. id ( ) , None , source)
225+ . map_err ( ComposeError :: PrepareError ) ?;
208226
209- let ( world_id, instantiation_id) = self
210- . register_package ( component. id ( ) , None , source)
211- . map_err ( ComposeError :: PrepareError ) ?;
227+ let prepared = self . prepare_dependencies ( world_id, component) . await ?;
212228
213- let prepared = self . prepare_dependencies ( world_id, component) . await ?;
229+ let arguments = self
230+ . build_instantiation_arguments ( world_id, prepared)
231+ . await ?;
214232
215- let arguments = self
216- . build_instantiation_arguments ( world_id, prepared)
217- . await ?;
233+ for ( argument_name, argument) in arguments {
234+ self . graph
235+ . set_instantiation_argument ( instantiation_id, & argument_name, argument)
236+ . map_err ( |e| ComposeError :: PrepareError ( e. into ( ) ) ) ?;
237+ }
238+
239+ self . export_dependents_exports ( world_id, instantiation_id)
240+ . map_err ( ComposeError :: PrepareError ) ?;
218241
219- for ( argument_name, argument) in arguments {
220242 self . graph
221- . set_instantiation_argument ( instantiation_id , & argument_name , argument )
222- . map_err ( |e| ComposeError :: PrepareError ( e. into ( ) ) ) ?;
223- }
243+ . encode ( Default :: default ( ) )
244+ . map_err ( |e| ComposeError :: EncodeError ( e. into ( ) ) ) ?
245+ } ;
224246
225- self . export_dependents_exports ( world_id, instantiation_id)
226- . map_err ( ComposeError :: PrepareError ) ?;
247+ let with_extras = complicator ( fulfilled_source) . await ?;
227248
228- self . graph
229- . encode ( Default :: default ( ) )
230- . map_err ( |e| ComposeError :: EncodeError ( e. into ( ) ) )
249+ Ok ( with_extras)
231250 }
232251
233252 fn new ( loader : & ' a L ) -> Self {
0 commit comments