wasm_pkg_common/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::{
    collections::{hash_map::Entry, HashMap},
    io::ErrorKind,
    path::{Path, PathBuf},
};

use etcetera::BaseStrategy;
use serde::{Deserialize, Serialize};

use crate::{
    label::Label, metadata::RegistryMetadata, package::PackageRef, registry::Registry, Error,
};

mod toml;

const DEFAULT_FALLBACK_NAMESPACE_REGISTRIES: &[(&str, &str)] =
    &[("wasi", "wasi.dev"), ("ba", "bytecodealliance.org")];

/// Wasm Package registry configuration.
///
/// Most consumers are expected to start with [`Config::global_defaults`] to
/// provide a consistent baseline user experience. Where needed, these defaults
/// can be overridden with application-specific config via [`Config::merge`] or
/// other mutation methods.
#[derive(Debug, Clone, Serialize)]
#[serde(into = "toml::TomlConfig")]
pub struct Config {
    default_registry: Option<Registry>,
    namespace_registries: HashMap<Label, RegistryMapping>,
    package_registry_overrides: HashMap<PackageRef, RegistryMapping>,
    // Note: these are only used for hard-coded defaults currently
    fallback_namespace_registries: HashMap<Label, Registry>,
    registry_configs: HashMap<Registry, RegistryConfig>,
}

/// Possible options for namespace configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RegistryMapping {
    /// Use the given registry address (this will fetch the well-known registry metadata from the given hostname).
    Registry(Registry),
    /// Use custom configuration for reaching a registry
    Custom(CustomConfig),
}

/// Custom registry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomConfig {
    /// A valid name for the registry. This still must be a valid [`Registry`] in that it should
    /// look like a valid hostname. When doing custom configuration however, this is just used as a
    /// key to identify the configuration for this namespace
    pub registry: Registry,
    /// The metadata for the registry. This is used to determine the protocol to use for the
    /// registry as well as mapping information for the registry.
    pub metadata: RegistryMetadata,
}

impl Default for Config {
    fn default() -> Self {
        let fallback_namespace_registries = DEFAULT_FALLBACK_NAMESPACE_REGISTRIES
            .iter()
            .map(|(k, v)| (k.parse().unwrap(), v.parse().unwrap()))
            .collect();
        Self {
            default_registry: Default::default(),
            namespace_registries: Default::default(),
            package_registry_overrides: Default::default(),
            fallback_namespace_registries,
            registry_configs: Default::default(),
        }
    }
}

impl Config {
    /// Returns an empty config.
    ///
    /// Note that this may differ from the `Default` implementation, which
    /// includes hard-coded global defaults.
    pub fn empty() -> Self {
        Self {
            default_registry: Default::default(),
            namespace_registries: Default::default(),
            package_registry_overrides: Default::default(),
            fallback_namespace_registries: Default::default(),
            registry_configs: Default::default(),
        }
    }

    /// Loads config from several default sources.
    ///
    /// The following sources are loaded in this order, with later sources
    /// merged into (overriding) earlier sources.
    /// - Hard-coded defaults
    /// - User-global config file (e.g. `~/.config/wasm-pkg/config.toml`)
    ///
    /// Note: This list is expected to expand in the future to include
    /// "workspace" config files like `./.wasm-pkg/config.toml`.
    pub async fn global_defaults() -> Result<Self, Error> {
        let mut config = Self::default();
        if let Some(global_config) = Self::read_global_config().await? {
            config.merge(global_config);
        }
        Ok(config)
    }

    /// Returns the default global config file location
    pub fn global_config_path() -> Option<PathBuf> {
        etcetera::choose_base_strategy()
            .ok()
            .map(|strat| strat.config_dir().join("wasm-pkg").join("config.toml"))
    }

    /// Reads config from the default global config file location
    pub async fn read_global_config() -> Result<Option<Self>, Error> {
        let path = match Config::global_config_path() {
            Some(path) => path,
            None => return Ok(None),
        };
        let contents = match tokio::fs::read_to_string(&path).await {
            Ok(contents) => contents,
            Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None),
            Err(err) => return Err(Error::ConfigFileIoError(err)),
        };
        Ok(Some(Self::from_toml(&contents)?))
    }

    /// Reads config from a TOML file at the given path.
    pub async fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
        let contents = tokio::fs::read_to_string(path)
            .await
            .map_err(Error::ConfigFileIoError)?;
        Self::from_toml(&contents)
    }

    /// Parses config from the given TOML contents.
    pub fn from_toml(contents: &str) -> Result<Self, Error> {
        let toml_cfg: toml::TomlConfig =
            ::toml::from_str(contents).map_err(Error::invalid_config)?;
        Ok(toml_cfg.into())
    }

    /// Writes the config to a TOML file at the given path.
    pub async fn to_file(&self, path: impl AsRef<Path>) -> Result<(), Error> {
        let toml_str = ::toml::to_string(&self).map_err(Error::invalid_config)?;
        tokio::fs::write(path, toml_str)
            .await
            .map_err(Error::ConfigFileIoError)
    }

    /// Merges the given other config into this one.
    pub fn merge(&mut self, other: Self) {
        let Self {
            default_registry,
            namespace_registries,
            package_registry_overrides,
            fallback_namespace_registries,
            registry_configs,
        } = other;
        if default_registry.is_some() {
            self.default_registry = default_registry;
        }
        self.namespace_registries.extend(namespace_registries);
        self.package_registry_overrides
            .extend(package_registry_overrides);
        self.fallback_namespace_registries
            .extend(fallback_namespace_registries);
        for (registry, config) in registry_configs {
            match self.registry_configs.entry(registry) {
                Entry::Occupied(mut occupied) => occupied.get_mut().merge(config),
                Entry::Vacant(vacant) => {
                    vacant.insert(config);
                }
            }
        }
    }

    /// Resolves a [`Registry`] for the given [`PackageRef`].
    ///
    /// Resolution returns the first of these that matches:
    /// - A package registry exactly matching the package
    /// - A namespace registry matching the package's namespace
    /// - The default registry
    /// - Hard-coded fallbacks for certain well-known namespaces
    pub fn resolve_registry(&self, package: &PackageRef) -> Option<&Registry> {
        if let Some(RegistryMapping::Registry(reg)) = self.package_registry_overrides.get(package) {
            Some(reg)
        } else if let Some(RegistryMapping::Custom(custom)) =
            self.package_registry_overrides.get(package)
        {
            Some(&custom.registry)
        } else if let Some(RegistryMapping::Registry(reg)) =
            self.namespace_registries.get(package.namespace())
        {
            Some(reg)
        } else if let Some(RegistryMapping::Custom(custom)) =
            self.namespace_registries.get(package.namespace())
        {
            Some(&custom.registry)
        } else if let Some(reg) = self.default_registry.as_ref() {
            Some(reg)
        } else if let Some(reg) = self.fallback_namespace_registries.get(package.namespace()) {
            Some(reg)
        } else {
            None
        }
    }

    /// Returns the default registry.
    pub fn default_registry(&self) -> Option<&Registry> {
        self.default_registry.as_ref()
    }

    /// Sets the default registry.
    ///
    /// To unset the default registry, pass `None`.
    pub fn set_default_registry(&mut self, registry: Option<Registry>) {
        self.default_registry = registry;
    }

    /// Returns a registry for the given namespace.
    ///
    /// Does not fall back to the default registry; see
    /// [`Self::resolve_registry`].
    pub fn namespace_registry(&self, namespace: &Label) -> Option<&RegistryMapping> {
        self.namespace_registries.get(namespace)
    }

    /// Sets a registry for the given namespace.
    pub fn set_namespace_registry(&mut self, namespace: Label, registry: RegistryMapping) {
        self.namespace_registries.insert(namespace, registry);
    }

    /// Returns a registry override configured for the given package.
    ///
    /// Does not fall back to namespace or default registries; see
    /// [`Self::resolve_registry`].
    pub fn package_registry_override(&self, package: &PackageRef) -> Option<&RegistryMapping> {
        self.package_registry_overrides.get(package)
    }

    /// Sets a registry override for the given package.
    pub fn set_package_registry_override(
        &mut self,
        package: PackageRef,
        registry: RegistryMapping,
    ) {
        self.package_registry_overrides.insert(package, registry);
    }

    /// Returns [`RegistryConfig`] for the given registry.
    pub fn registry_config(&self, registry: &Registry) -> Option<&RegistryConfig> {
        self.registry_configs.get(registry)
    }

    /// Returns a mutable [`RegistryConfig`] for the given registry, inserting
    /// an empty one if needed.
    pub fn get_or_insert_registry_config_mut(
        &mut self,
        registry: &Registry,
    ) -> &mut RegistryConfig {
        if !self.registry_configs.contains_key(registry) {
            self.registry_configs
                .insert(registry.clone(), Default::default());
        }
        self.registry_configs.get_mut(registry).unwrap()
    }
}

#[derive(Clone, Default)]
pub struct RegistryConfig {
    default_backend: Option<String>,
    backend_configs: HashMap<String, ::toml::Table>,
}

impl RegistryConfig {
    /// Merges the given other config into this one.
    pub fn merge(&mut self, other: Self) {
        let Self {
            default_backend: backend_type,
            backend_configs,
        } = other;
        if backend_type.is_some() {
            self.default_backend = backend_type;
        }
        for (ty, config) in backend_configs {
            match self.backend_configs.entry(ty) {
                Entry::Occupied(mut occupied) => occupied.get_mut().extend(config),
                Entry::Vacant(vacant) => {
                    vacant.insert(config);
                }
            }
        }
    }

    /// Returns default backend type, if one is configured. If none are configured and there is only
    /// one type of configured backend, this will return that type.
    pub fn default_backend(&self) -> Option<&str> {
        match self.default_backend.as_deref() {
            Some(ty) => Some(ty),
            None => {
                if self.backend_configs.len() == 1 {
                    self.backend_configs.keys().next().map(|ty| ty.as_str())
                } else {
                    None
                }
            }
        }
    }

    /// Sets the default backend type.
    ///
    /// To unset the default backend type, pass `None`.
    pub fn set_default_backend(&mut self, default_backend: Option<String>) {
        self.default_backend = default_backend;
    }

    /// Returns an iterator of configured backend types.
    pub fn configured_backend_types(&self) -> impl Iterator<Item = &str> {
        self.backend_configs.keys().map(|ty| ty.as_str())
    }

    /// Attempts to deserialize backend config with the given type.
    ///
    /// Returns `Ok(None)` if no configuration was provided.
    /// Returns `Err` if configuration was provided but deserialization failed.
    pub fn backend_config<'a, T: Deserialize<'a>>(
        &'a self,
        backend_type: &str,
    ) -> Result<Option<T>, Error> {
        let Some(table) = self.backend_configs.get(backend_type) else {
            return Ok(None);
        };
        let config = table.clone().try_into().map_err(Error::invalid_config)?;
        Ok(Some(config))
    }

    /// Set the backend config of the given type by serializing the given config.
    pub fn set_backend_config<T: Serialize>(
        &mut self,
        backend_type: impl Into<String>,
        backend_config: T,
    ) -> Result<(), Error> {
        let table = ::toml::Table::try_from(backend_config).map_err(Error::invalid_config)?;
        self.backend_configs.insert(backend_type.into(), table);
        Ok(())
    }
}

impl std::fmt::Debug for RegistryConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RegistryConfig")
            .field("backend_type", &self.default_backend)
            .field(
                "backend_configs",
                &DebugBackendConfigs(&self.backend_configs),
            )
            .finish()
    }
}

// Redact backend configs, which may contain sensitive values.
struct DebugBackendConfigs<'a>(&'a HashMap<String, ::toml::Table>);

impl std::fmt::Debug for DebugBackendConfigs<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map()
            .entries(self.0.keys().map(|ty| (ty, &"<HIDDEN>")))
            .finish()
    }
}