Skip to main content

graphrt_bundle/
wasm.rs

1//! Registering bundled WebAssembly plugins.
2
3use graphrt_core::Registry;
4use graphrt_wasm::{WasmLimits, WasmPlugin};
5
6use crate::bundle::Bundle;
7use crate::error::BundleError;
8
9impl Bundle {
10    /// Load every bundled WebAssembly plugin and register its node kinds in
11    /// `registry`. Returns the registered kinds.
12    ///
13    /// Kinds must start with `<plugin name>.`, unless the plugin manifest sets
14    /// `allow_unprefixed_kinds` (then only plain names without `.` are allowed).
15    /// Kinds that already exist in `registry` are never replaced: a plugin whose
16    /// kinds are all registered already (e.g. the same plugin installed on the host)
17    /// is skipped, and a partial overlap is an error. Bundled plugins
18    /// never receive plaintext secrets: that requires host-side trust.
19    /// Non-WebAssembly plugins are ignored here; see [`Bundle::check`].
20    pub fn register_wasm_plugins(
21        &self,
22        registry: &mut Registry,
23        limits: &WasmLimits,
24    ) -> Result<Vec<String>, BundleError> {
25        let mut kinds = Vec::new();
26        for plugin in self.wasm_plugins()? {
27            let err = |message: String| BundleError::Plugin { plugin: plugin.name.clone(), message };
28            let module = plugin.module.as_deref().ok_or_else(|| err("missing module".into()))?;
29            let wasm =
30                WasmPlugin::load(module, limits.clone().with_expose_secrets(false)).map_err(|e| err(e.to_string()))?;
31            let allow_plain =
32                plugin.manifest.get("allow_unprefixed_kinds").and_then(serde_json::Value::as_bool).unwrap_or(false);
33            let prefix = format!("{}.", plugin.name);
34            for descriptor in wasm.descriptors() {
35                let plain = allow_plain && !descriptor.kind.contains('.');
36                if !descriptor.kind.starts_with(&prefix) && !plain {
37                    return Err(err(format!(
38                        "node kind `{}` must start with `{prefix}` (or be a plain name with `allow_unprefixed_kinds`)",
39                        descriptor.kind
40                    )));
41                }
42            }
43            let descriptors = wasm.descriptors();
44            if !descriptors.is_empty() && descriptors.iter().all(|d| registry.contains(&d.kind)) {
45                continue;
46            }
47            wasm.register(registry).map_err(|e| err(e.to_string()))?;
48            kinds.extend(wasm.descriptors().into_iter().map(|d| d.kind));
49        }
50        Ok(kinds)
51    }
52}