ZIm/crates/extension/src/capabilities/npm_install_package_capability.rs
Marshall Bowers 89e88c245e
extension_host: Add npm:install capability (#35144)
This PR adds a new `npm:install` capability for installing npm packges
in extensions.

Currently all npm packages are allowed.

Release Notes:

- N/A
2025-07-26 22:40:02 +00:00

39 lines
1 KiB
Rust

use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct NpmInstallPackageCapability {
pub package: String,
}
impl NpmInstallPackageCapability {
/// Returns whether the capability allows installing the given NPM package.
pub fn allows(&self, package: &str) -> bool {
self.package == "*" || self.package == package
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn test_allows() {
let capability = NpmInstallPackageCapability {
package: "*".to_string(),
};
assert_eq!(capability.allows("package"), true);
let capability = NpmInstallPackageCapability {
package: "react".to_string(),
};
assert_eq!(capability.allows("react"), true);
let capability = NpmInstallPackageCapability {
package: "react".to_string(),
};
assert_eq!(capability.allows("malicious-package"), false);
}
}