wasmer_wasix/runtime/resolver/
multi_source.rs

1use std::sync::Arc;
2
3use wasmer_config::package::PackageSource;
4
5use crate::runtime::resolver::{PackageSummary, QueryError, Source};
6
7/// A [`Source`] that works by querying multiple [`Source`]s in succession.
8///
9/// # Error Handling
10///
11/// A [`Source`] implementation can return certain non-fatal errors and,
12/// depending on the [`MultiSourceStrategy`], the [`MultiSource`] can choose to
13/// deal with it in different ways. Sometimes
14///
15///
16/// The first [`Source`] to return one or more [`Summaries`][PackageSummary]
17/// will be treated as the canonical source for that [`Dependency`][dep] and no
18/// further [`Source`]s will be queried.
19///
20/// [dep]: crate::runtime::resolver::Dependency
21#[derive(Debug, Clone)]
22pub struct MultiSource {
23    sources: Vec<Arc<dyn Source + Send + Sync>>,
24    strategy: MultiSourceStrategy,
25}
26
27impl Default for MultiSource {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl MultiSource {
34    pub fn new() -> Self {
35        MultiSource {
36            sources: Vec::new(),
37            strategy: MultiSourceStrategy::default(),
38        }
39    }
40
41    pub fn add_source(&mut self, source: impl Source + Send + 'static) -> &mut Self {
42        self.add_shared_source(Arc::new(source))
43    }
44
45    pub fn add_shared_source(&mut self, source: Arc<dyn Source + Send + Sync>) -> &mut Self {
46        self.sources.push(source);
47        self
48    }
49
50    /// Override the strategy used when a [`Source`] returns a non-fatal error.
51    pub fn with_strategy(self, strategy: MultiSourceStrategy) -> Self {
52        MultiSource { strategy, ..self }
53    }
54}
55
56#[async_trait::async_trait]
57impl Source for MultiSource {
58    #[tracing::instrument(level = "debug", skip_all, fields(%package))]
59    async fn query(&self, package: &PackageSource) -> Result<Vec<PackageSummary>, QueryError> {
60        let mut output = Vec::<PackageSummary>::new();
61
62        for source in &self.sources {
63            match source.query(package).await {
64                Ok(mut summaries) => {
65                    if self.strategy.merge_results {
66                        // Extend matches, but skip already found versions.
67                        summaries.retain(|new| {
68                            !output.iter().any(|existing| new.pkg.id == existing.pkg.id)
69                        });
70                        output.extend(summaries);
71                    } else {
72                        return Ok(summaries);
73                    }
74                }
75                Err(QueryError::Unsupported { .. })
76                    if self.strategy.continue_if_unsupported || self.strategy.merge_results =>
77                {
78                    continue;
79                }
80                Err(QueryError::NotFound { .. })
81                    if self.strategy.continue_if_not_found || self.strategy.merge_results =>
82                {
83                    continue;
84                }
85                Err(QueryError::NoMatches { .. })
86                    if self.strategy.continue_if_no_matches || self.strategy.merge_results =>
87                {
88                    continue;
89                }
90                // Generic errors do not respect the `merge_results` strategy
91                // flag, because unexpected errors should be bubbled to the
92                // caller.
93                Err(e) => return Err(e),
94            }
95        }
96
97        if !output.is_empty() {
98            output.sort_by(|a, b| a.pkg.id.cmp(&b.pkg.id));
99
100            Ok(output)
101        } else {
102            Err(QueryError::NotFound {
103                query: package.clone(),
104            })
105        }
106    }
107}
108
109#[derive(Debug, Clone, PartialEq, Eq)]
110#[non_exhaustive]
111pub struct MultiSourceStrategy {
112    /// If encountered, treat [`QueryError::Unsupported`] as a non-fatal error
113    /// and query the next [`Source`] in turn.
114    ///
115    /// This flag is **enabled** by default.
116    pub continue_if_unsupported: bool,
117    /// If encountered, treat [`QueryError::NotFound`] as a non-fatal error and
118    /// query the next [`Source`] in turn.
119    ///
120    /// This flag is **enabled** by default and can be used to let earlier
121    /// [`Source`]s "override" later ones.
122    pub continue_if_not_found: bool,
123    /// If encountered, treat [`QueryError::NoMatches`] as a non-fatal error and
124    /// query the next [`Source`] in turn.
125    ///
126    /// This flag is **disabled** by default.
127    pub continue_if_no_matches: bool,
128
129    /// Merge results from all sources into a single result.
130    ///
131    /// True by default.
132    pub merge_results: bool,
133}
134
135impl Default for MultiSourceStrategy {
136    fn default() -> Self {
137        MultiSourceStrategy {
138            continue_if_unsupported: true,
139            continue_if_not_found: true,
140            continue_if_no_matches: true,
141            merge_results: true,
142        }
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use wasmer_config::package::PackageId;
149
150    use super::super::{DistributionInfo, InMemorySource, PackageInfo, WebcHash};
151    use super::*;
152
153    /// Test that the `MultiSource` can merge results from multiple sources.
154    #[tokio::test]
155    async fn test_multi_source_merge() {
156        let id1 = PackageId::new_named("ns/pkg", "0.0.1".parse().unwrap());
157        let pkg1 = PackageSummary {
158            pkg: PackageInfo {
159                id: id1.clone(),
160                commands: Vec::new(),
161                entrypoint: None,
162                dependencies: Vec::new(),
163                filesystem: Vec::new(),
164            },
165            dist: DistributionInfo {
166                webc: "https://example.com/ns/pkg/0.0.1".parse().unwrap(),
167                webc_sha256: WebcHash([0u8; 32]),
168            },
169        };
170
171        let id2 = PackageId::new_named("ns/pkg", "0.0.2".parse().unwrap());
172        let mut pkg2 = pkg1.clone();
173        pkg2.pkg.id = id2.clone();
174
175        let mut mem1 = InMemorySource::new();
176        mem1.add(pkg1);
177
178        let mut mem2 = InMemorySource::new();
179        mem2.add(pkg2);
180
181        let mut multi = MultiSource::new();
182        multi.add_source(mem1);
183        multi.add_source(mem2);
184
185        let summaries = multi.query(&"ns/pkg".parse().unwrap()).await.unwrap();
186        assert_eq!(summaries.len(), 2);
187        assert_eq!(summaries[0].pkg.id, id1);
188        assert_eq!(summaries[1].pkg.id, id2);
189    }
190}