feat(plugin): add plugin framework workflow
This commit is contained in:
247 files changed
+33841
-2256
No files matched your search
+319
-390
@@ -1,52 +1,98 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::timeout;
|
||||
use serde::Serialize;
|
||||
use tokio::process::Command;
|
||||
|
||||
const PLUGIN_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
pub const SUPPORTED_PLUGIN_PROTOCOL_VERSION: u32 = 1;
|
||||
mod assets;
|
||||
mod filesystem;
|
||||
mod host;
|
||||
mod installer;
|
||||
mod manifest;
|
||||
mod marketplace;
|
||||
mod runtime;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PluginManifest {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub version: String,
|
||||
#[serde(default = "default_plugin_protocol_version")]
|
||||
pub protocol_version: u32,
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
#[serde(default)]
|
||||
pub executable: Option<String>,
|
||||
#[serde(default)]
|
||||
pub drivers: Vec<PluginDriverManifest>,
|
||||
}
|
||||
pub use assets::PluginUiAsset;
|
||||
pub use filesystem::{
|
||||
PluginFilesystemEntry, PluginFilesystemEntryKind, PluginFilesystemListResult, PluginFilesystemMutationResult,
|
||||
PluginFilesystemReadResult, DEFAULT_PLUGIN_FILESYSTEM_PAGE_SIZE, DEFAULT_PLUGIN_FILESYSTEM_PREVIEW_BYTES,
|
||||
MAX_PLUGIN_FILESYSTEM_INLINE_WRITE_BYTES, MAX_PLUGIN_FILESYSTEM_PAGE_SIZE, MAX_PLUGIN_FILESYSTEM_PREVIEW_BYTES,
|
||||
PLUGIN_FILESYSTEM_CREATE_DIRECTORY_METHOD, PLUGIN_FILESYSTEM_DELETE_METHOD, PLUGIN_FILESYSTEM_LIST_METHOD,
|
||||
PLUGIN_FILESYSTEM_READ_METHOD, PLUGIN_FILESYSTEM_RENAME_METHOD, PLUGIN_FILESYSTEM_WRITE_METHOD,
|
||||
};
|
||||
pub use host::{ActivePluginSession, PluginConnectionActionResult, PluginConnectionHandle, PluginHost};
|
||||
pub use installer::{
|
||||
PluginInstallPolicy, PluginInstallResponse, PluginInstallResult, PluginPackageInstaller, PluginRollbackResponse,
|
||||
PluginRollbackResult, PluginSignatureStatus, PluginTrustStore, PluginTrustedKey, DBXP_EXTENSION,
|
||||
MAX_PLUGIN_PACKAGE_BYTES, PLUGIN_CHECKSUMS_FILE, PLUGIN_SIGNATURE_FILE,
|
||||
};
|
||||
pub use marketplace::{
|
||||
PluginMarketplace, PluginMarketplaceArtifact, PluginMarketplaceCatalog, PluginMarketplaceInstallRequest,
|
||||
PluginMarketplaceLocalization, PluginMarketplacePlugin, PluginMarketplaceRepositoryMetadata,
|
||||
PluginMarketplaceVersion, PluginRepository, PluginRepositoryCatalogResult, PluginRepositoryKind,
|
||||
PluginRepositoryStore, MAX_PLUGIN_CATALOG_BYTES, OFFICIAL_PLUGIN_REPOSITORY_ID, SUPPORTED_PLUGIN_CATALOG_VERSION,
|
||||
};
|
||||
|
||||
fn default_plugin_protocol_version() -> u32 {
|
||||
1
|
||||
}
|
||||
pub use manifest::{
|
||||
current_plugin_target, resolve_safe_plugin_path, PluginBackendEntrypoint, PluginBackendTransport,
|
||||
PluginCompatibility, PluginConnectionActionContribution, PluginConnectionActionVariant, PluginConnectionActionWhen,
|
||||
PluginConnectionCapability, PluginConnectionProviderContribution, PluginContribution, PluginDriverManifest,
|
||||
PluginEngines, PluginEntrypoints, PluginFilesystemCapability, PluginFilesystemProviderContribution,
|
||||
PluginFormFieldBinding, PluginFormFieldDefinition, PluginFormFieldOption, PluginFormFieldType, PluginManifest,
|
||||
PluginUiEntrypoint, PluginWorkbenchContribution, PLUGIN_CONNECTION_ACTION_METHOD, PLUGIN_CONNECTION_CONNECT_METHOD,
|
||||
PLUGIN_CONNECTION_DISCONNECT_METHOD, PLUGIN_CONNECTION_TEST_METHOD, SUPPORTED_PLUGIN_HOST_API_VERSION,
|
||||
SUPPORTED_PLUGIN_MANIFEST_VERSION, SUPPORTED_PLUGIN_PERMISSIONS, SUPPORTED_PLUGIN_PROTOCOL_VERSION,
|
||||
};
|
||||
pub use runtime::{
|
||||
PluginBinaryMessage, PluginEvent, PluginHandshake, PluginHandshakeIdentity, PluginSessionState,
|
||||
PluginSessionStatus, PluginSidecarSession, PLUGIN_REQUEST_TIMEOUT,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PluginDriverManifest {
|
||||
pub id: String,
|
||||
pub label: String,
|
||||
pub kind: String,
|
||||
#[serde(default)]
|
||||
pub database_type: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InstalledPlugin {
|
||||
pub manifest: PluginManifest,
|
||||
pub path: PathBuf,
|
||||
pub compatibility: PluginCompatibility,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct InstalledPluginInfo {
|
||||
pub manifest: PluginManifest,
|
||||
pub compatibility: PluginCompatibilityInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PluginCompatibilityInfo {
|
||||
pub compatible: bool,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub errors: Vec<String>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub warnings: Vec<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub target: Option<String>,
|
||||
}
|
||||
|
||||
impl InstalledPlugin {
|
||||
pub fn new(manifest: PluginManifest, path: PathBuf, app_version: &str) -> Self {
|
||||
let compatibility = manifest.compatibility(&path, app_version);
|
||||
Self { manifest, path, compatibility }
|
||||
}
|
||||
|
||||
pub fn info(&self) -> InstalledPluginInfo {
|
||||
InstalledPluginInfo {
|
||||
manifest: self.manifest.clone(),
|
||||
compatibility: PluginCompatibilityInfo {
|
||||
compatible: self.compatibility.compatible,
|
||||
errors: self.compatibility.errors.clone(),
|
||||
warnings: self.compatibility.warnings.clone(),
|
||||
target: self.compatibility.target.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
@@ -74,17 +120,26 @@ impl PluginRuntimeEnv {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PluginRegistry {
|
||||
root_dir: PathBuf,
|
||||
app_version: String,
|
||||
}
|
||||
|
||||
impl PluginRegistry {
|
||||
pub fn new(root_dir: PathBuf) -> Self {
|
||||
Self { root_dir }
|
||||
Self::new_with_app_version(root_dir, env!("CARGO_PKG_VERSION"))
|
||||
}
|
||||
|
||||
pub fn new_with_app_version(root_dir: PathBuf, app_version: impl Into<String>) -> Self {
|
||||
Self { root_dir, app_version: app_version.into() }
|
||||
}
|
||||
|
||||
pub fn root_dir(&self) -> &Path {
|
||||
&self.root_dir
|
||||
}
|
||||
|
||||
pub fn app_version(&self) -> &str {
|
||||
&self.app_version
|
||||
}
|
||||
|
||||
pub fn list_installed(&self) -> Result<Vec<InstalledPlugin>, String> {
|
||||
let entries = match std::fs::read_dir(&self.root_dir) {
|
||||
Ok(entries) => entries,
|
||||
@@ -95,18 +150,43 @@ impl PluginRegistry {
|
||||
let mut plugins = Vec::new();
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|err| err.to_string())?;
|
||||
let path = entry.path();
|
||||
if !path.is_dir() {
|
||||
let container_path = entry.path();
|
||||
if !container_path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let path = match installer::resolve_active_plugin_dir(&container_path) {
|
||||
Ok(Some(path)) => path,
|
||||
Ok(None) => continue,
|
||||
Err(error) => {
|
||||
plugins.push(unavailable_plugin(container_path, error));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let manifest_path = path.join("manifest.json");
|
||||
if !manifest_path.exists() {
|
||||
continue;
|
||||
}
|
||||
let raw = std::fs::read_to_string(&manifest_path).map_err(|err| err.to_string())?;
|
||||
let manifest: PluginManifest = serde_json::from_str(&raw)
|
||||
.map_err(|err| format!("Failed to parse plugin manifest {}: {err}", manifest_path.display()))?;
|
||||
plugins.push(InstalledPlugin { manifest, path });
|
||||
let raw = match std::fs::read_to_string(&manifest_path) {
|
||||
Ok(raw) => raw,
|
||||
Err(error) => {
|
||||
plugins.push(unavailable_plugin(
|
||||
container_path,
|
||||
format!("Failed to read plugin manifest {}: {error}", manifest_path.display()),
|
||||
));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let manifest: PluginManifest = match serde_json::from_str(&raw) {
|
||||
Ok(manifest) => manifest,
|
||||
Err(error) => {
|
||||
plugins.push(unavailable_plugin(
|
||||
container_path,
|
||||
format!("Failed to parse plugin manifest {}: {error}", manifest_path.display()),
|
||||
));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
plugins.push(InstalledPlugin::new(manifest, path, &self.app_version));
|
||||
}
|
||||
plugins.sort_by(|a, b| a.manifest.id.cmp(&b.manifest.id));
|
||||
Ok(plugins)
|
||||
@@ -114,14 +194,19 @@ impl PluginRegistry {
|
||||
|
||||
pub fn find_driver(&self, driver_id: &str) -> Result<Option<InstalledPlugin>, String> {
|
||||
Ok(self.list_installed()?.into_iter().find(|plugin| {
|
||||
plugin
|
||||
.manifest
|
||||
.drivers
|
||||
.iter()
|
||||
.any(|driver| driver.id == driver_id || driver.database_type.as_deref() == Some(driver_id))
|
||||
plugin.compatibility.compatible
|
||||
&& plugin
|
||||
.manifest
|
||||
.drivers
|
||||
.iter()
|
||||
.any(|driver| driver.id == driver_id || driver.database_type.as_deref() == Some(driver_id))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn find_plugin(&self, plugin_id: &str) -> Result<Option<InstalledPlugin>, String> {
|
||||
Ok(self.list_installed()?.into_iter().find(|plugin| plugin.manifest.id == plugin_id))
|
||||
}
|
||||
|
||||
pub async fn invoke_driver<T>(&self, driver_id: &str, method: &str, params: serde_json::Value) -> Result<T, String>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
@@ -155,14 +240,11 @@ impl PluginRegistry {
|
||||
{
|
||||
let plugin =
|
||||
self.find_driver(driver_id)?.ok_or_else(|| format!("Plugin driver '{driver_id}' is not installed"))?;
|
||||
ensure_plugin_protocol_compatible(&plugin.manifest)?;
|
||||
let invoke = invoke_plugin(&plugin, driver_id, method, params, &env);
|
||||
match timeout_duration {
|
||||
Some(duration) => timeout(duration, invoke).await.map_err(|_| {
|
||||
format!("Plugin '{}' timed out after {} seconds", plugin.manifest.id, duration.as_secs())
|
||||
})?,
|
||||
None => invoke.await,
|
||||
}
|
||||
ensure_plugin_compatible(&plugin)?;
|
||||
let session = PluginSidecarSession::start(plugin, self.app_version.clone(), env).await?;
|
||||
let result = session.invoke_with_timeout(method, params, Some(driver_id), timeout_duration).await;
|
||||
session.shutdown().await;
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn start_driver_session(&self, driver_id: &str) -> Result<Arc<PluginDriverSession>, String> {
|
||||
@@ -176,85 +258,57 @@ impl PluginRegistry {
|
||||
) -> Result<Arc<PluginDriverSession>, String> {
|
||||
let plugin =
|
||||
self.find_driver(driver_id)?.ok_or_else(|| format!("Plugin driver '{driver_id}' is not installed"))?;
|
||||
ensure_plugin_protocol_compatible(&plugin.manifest)?;
|
||||
PluginDriverSession::start(plugin, driver_id.to_string(), env).await.map(Arc::new)
|
||||
ensure_plugin_compatible(&plugin)?;
|
||||
PluginDriverSession::start(plugin, driver_id.to_string(), self.app_version.clone(), env).await.map(Arc::new)
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_plugin_protocol_compatible(manifest: &PluginManifest) -> Result<(), String> {
|
||||
if manifest.protocol_version == SUPPORTED_PLUGIN_PROTOCOL_VERSION {
|
||||
fn unavailable_plugin(path: PathBuf, error: String) -> InstalledPlugin {
|
||||
let id = path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.filter(|name| !name.is_empty())
|
||||
.unwrap_or("invalid-plugin")
|
||||
.to_string();
|
||||
InstalledPlugin {
|
||||
manifest: PluginManifest {
|
||||
id: id.clone(),
|
||||
name: id,
|
||||
version: "0.0.0-invalid".to_string(),
|
||||
description: error.clone(),
|
||||
..PluginManifest::default()
|
||||
},
|
||||
path,
|
||||
compatibility: PluginCompatibility {
|
||||
compatible: false,
|
||||
errors: vec![error],
|
||||
target: Some(current_plugin_target()),
|
||||
..PluginCompatibility::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_plugin_compatible(plugin: &InstalledPlugin) -> Result<(), String> {
|
||||
if plugin.compatibility.compatible {
|
||||
return Ok(());
|
||||
}
|
||||
Err(format!(
|
||||
"Plugin '{}' uses protocol version {}, but this DBX build supports protocol version {}",
|
||||
manifest.id, manifest.protocol_version, SUPPORTED_PLUGIN_PROTOCOL_VERSION
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PluginRequest {
|
||||
jsonrpc: &'static str,
|
||||
id: u64,
|
||||
driver: String,
|
||||
method: String,
|
||||
params: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PluginResponse {
|
||||
id: u64,
|
||||
#[serde(default)]
|
||||
result: Option<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
error: Option<PluginError>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PluginError {
|
||||
message: String,
|
||||
Err(format!("Plugin '{}' is incompatible: {}", plugin.manifest.id, plugin.compatibility.errors.join("; ")))
|
||||
}
|
||||
|
||||
pub struct PluginDriverSession {
|
||||
plugin: InstalledPlugin,
|
||||
sidecar: Arc<PluginSidecarSession>,
|
||||
driver_id: String,
|
||||
process: Mutex<PluginProcess>,
|
||||
next_request_id: AtomicU64,
|
||||
}
|
||||
|
||||
struct PluginProcess {
|
||||
child: Child,
|
||||
stdin: ChildStdin,
|
||||
stdout: BufReader<ChildStdout>,
|
||||
}
|
||||
|
||||
impl PluginDriverSession {
|
||||
async fn start(plugin: InstalledPlugin, driver_id: String, env: PluginRuntimeEnv) -> Result<Self, String> {
|
||||
let mut child = spawn_plugin_child(&plugin, &env)?;
|
||||
let stdin = child.stdin.take().ok_or("Plugin stdin unavailable")?;
|
||||
let stdout = child.stdout.take().ok_or("Plugin stdout unavailable")?;
|
||||
if let Some(stderr) = child.stderr.take() {
|
||||
let plugin_id = plugin.manifest.id.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut stderr = BufReader::new(stderr);
|
||||
loop {
|
||||
match read_plugin_line(&mut stderr, "stderr").await {
|
||||
Ok(line) => log::warn!("[plugin:{plugin_id}] {}", line.trim_end()),
|
||||
Err(err) if err.contains("end of stream") => break,
|
||||
Err(err) => {
|
||||
log::warn!("[plugin:{plugin_id}] failed to read stderr: {err}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
plugin,
|
||||
driver_id,
|
||||
process: Mutex::new(PluginProcess { child, stdin, stdout: BufReader::new(stdout) }),
|
||||
next_request_id: AtomicU64::new(1),
|
||||
})
|
||||
async fn start(
|
||||
plugin: InstalledPlugin,
|
||||
driver_id: String,
|
||||
app_version: String,
|
||||
env: PluginRuntimeEnv,
|
||||
) -> Result<Self, String> {
|
||||
let sidecar = PluginSidecarSession::start(plugin, app_version, env).await?;
|
||||
Ok(Self { sidecar, driver_id })
|
||||
}
|
||||
|
||||
pub async fn invoke<T>(&self, method: &str, params: serde_json::Value) -> Result<T, String>
|
||||
@@ -273,68 +327,27 @@ impl PluginDriverSession {
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let request_id = self.next_request_id.fetch_add(1, Ordering::Relaxed);
|
||||
let invoke = async {
|
||||
let mut process = self.process.lock().await;
|
||||
self.invoke_locked(&mut process, request_id, method, params).await
|
||||
};
|
||||
match timeout_duration {
|
||||
Some(duration) => match timeout(duration, invoke).await {
|
||||
Ok(result) => result,
|
||||
Err(_) => {
|
||||
self.kill().await;
|
||||
Err(format!("Plugin '{}' timed out after {} seconds", self.plugin.manifest.id, duration.as_secs()))
|
||||
}
|
||||
},
|
||||
None => invoke.await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn invoke_locked<T>(
|
||||
&self,
|
||||
process: &mut PluginProcess,
|
||||
request_id: u64,
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
) -> Result<T, String>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let request = PluginRequest {
|
||||
jsonrpc: "2.0",
|
||||
id: request_id,
|
||||
driver: self.driver_id.clone(),
|
||||
method: method.to_string(),
|
||||
params,
|
||||
};
|
||||
let line = encode_plugin_request_line(&request)?;
|
||||
process.stdin.write_all(&line).await.map_err(|err| err.to_string())?;
|
||||
process.stdin.flush().await.map_err(|err| err.to_string())?;
|
||||
|
||||
let stdout = &mut process.stdout;
|
||||
let child = &mut process.child;
|
||||
read_decoded_plugin_response(&self.plugin, request_id, stdout, || {
|
||||
let status = child.try_wait().map_err(|err| err.to_string())?;
|
||||
Ok(match status {
|
||||
Some(status) => format!("Plugin '{}' exited with status {}", self.plugin.manifest.id, status),
|
||||
None => format!("Plugin '{}' closed stdout without a response", self.plugin.manifest.id),
|
||||
})
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn kill(&self) {
|
||||
let mut process = self.process.lock().await;
|
||||
let _ = process.child.kill().await;
|
||||
self.sidecar.invoke_with_timeout(method, params, Some(&self.driver_id), timeout_duration).await
|
||||
}
|
||||
|
||||
pub async fn shutdown(&self) {
|
||||
self.kill().await;
|
||||
self.sidecar.shutdown().await;
|
||||
}
|
||||
|
||||
pub async fn pid(&self) -> Option<u32> {
|
||||
let process = self.process.lock().await;
|
||||
process.child.id()
|
||||
self.sidecar.pid().await
|
||||
}
|
||||
|
||||
pub fn subscribe_events(&self) -> tokio::sync::broadcast::Receiver<PluginEvent> {
|
||||
self.sidecar.subscribe_events()
|
||||
}
|
||||
|
||||
pub fn subscribe_binary(&self) -> tokio::sync::broadcast::Receiver<PluginBinaryMessage> {
|
||||
self.sidecar.subscribe_binary()
|
||||
}
|
||||
|
||||
pub async fn send_binary(&self, channel: &str, data: &[u8]) -> Result<(), String> {
|
||||
self.sidecar.send_binary(channel, data).await
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
@@ -343,216 +356,15 @@ impl PluginDriverSession {
|
||||
driver_id: String,
|
||||
env: PluginRuntimeEnv,
|
||||
) -> Result<Self, String> {
|
||||
Self::start(plugin, driver_id, env).await
|
||||
Self::start(plugin, driver_id, env!("CARGO_PKG_VERSION").to_string(), env).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn invoke_plugin<T>(
|
||||
plugin: &InstalledPlugin,
|
||||
driver_id: &str,
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
env: &PluginRuntimeEnv,
|
||||
) -> Result<T, String>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let mut child = spawn_plugin_child(plugin, env)?;
|
||||
|
||||
let request =
|
||||
PluginRequest { jsonrpc: "2.0", id: 1, driver: driver_id.to_string(), method: method.to_string(), params };
|
||||
let line = encode_plugin_request_line(&request)?;
|
||||
|
||||
let mut stdin = child.stdin.take().ok_or("Plugin stdin unavailable")?;
|
||||
stdin.write_all(&line).await.map_err(|err| err.to_string())?;
|
||||
drop(stdin);
|
||||
|
||||
let stdout = child.stdout.take().ok_or("Plugin stdout unavailable")?;
|
||||
let mut stderr = child.stderr.take().ok_or("Plugin stderr unavailable")?;
|
||||
let mut reader = BufReader::new(stdout);
|
||||
let response_result = read_decoded_plugin_response(plugin, request.id, &mut reader, || {
|
||||
Ok(format!("Plugin '{}' exited without a response", plugin.manifest.id))
|
||||
})
|
||||
.await;
|
||||
let mut stderr_bytes = Vec::new();
|
||||
stderr.read_to_end(&mut stderr_bytes).await.map_err(|err| err.to_string())?;
|
||||
let stderr_text = String::from_utf8_lossy(&stderr_bytes).into_owned();
|
||||
let status = child.wait().await.map_err(|err| err.to_string())?;
|
||||
|
||||
let response = match response_result {
|
||||
Ok(response) => response,
|
||||
Err(err) if err.contains("end of stream") => {
|
||||
let stderr = stderr_text.trim().to_string();
|
||||
return Err(if stderr.is_empty() {
|
||||
format!("Plugin '{}' exited without a response", plugin.manifest.id)
|
||||
} else {
|
||||
format!("Plugin '{}' exited without a response: {stderr}", plugin.manifest.id)
|
||||
});
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
if !status.success() {
|
||||
let stderr = stderr_text.trim().to_string();
|
||||
return Err(if stderr.is_empty() {
|
||||
format!("Plugin '{}' exited with status {}", plugin.manifest.id, status)
|
||||
} else {
|
||||
format!("Plugin '{}' failed: {stderr}", plugin.manifest.id)
|
||||
});
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn encode_plugin_request_line(request: &PluginRequest) -> Result<Vec<u8>, String> {
|
||||
let mut bytes = Vec::new();
|
||||
serde_json::to_writer(&mut bytes, request).map_err(|err| err.to_string())?;
|
||||
bytes.push(b'\n');
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
fn spawn_plugin_child(plugin: &InstalledPlugin, env: &PluginRuntimeEnv) -> Result<Child, String> {
|
||||
let executable = plugin
|
||||
.manifest
|
||||
.executable
|
||||
.as_deref()
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| format!("Plugin '{}' does not declare an executable", plugin.manifest.id))?;
|
||||
let executable_path = resolve_plugin_executable(&plugin.path, executable);
|
||||
|
||||
let mut command = crate::process::new_tokio_command(&executable_path);
|
||||
command
|
||||
.current_dir(&plugin.path)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.kill_on_drop(true);
|
||||
env.apply_to(&mut command);
|
||||
|
||||
command.spawn().map_err(|err| format!("Failed to start plugin '{}': {err}", plugin.manifest.id))
|
||||
}
|
||||
|
||||
async fn read_plugin_line<R>(reader: &mut R, context: &str) -> Result<String, String>
|
||||
where
|
||||
R: tokio::io::AsyncBufRead + Unpin,
|
||||
{
|
||||
let mut bytes = Vec::new();
|
||||
reader.read_until(b'\n', &mut bytes).await.map_err(|err| format!("Failed to read plugin {context}: {err}"))?;
|
||||
if bytes.is_empty() {
|
||||
return Err(format!("Failed to read plugin {context}: end of stream"));
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&bytes).into_owned())
|
||||
}
|
||||
|
||||
async fn read_decoded_plugin_response<T, R, F>(
|
||||
plugin: &InstalledPlugin,
|
||||
request_id: u64,
|
||||
reader: &mut R,
|
||||
end_of_stream_message: F,
|
||||
) -> Result<T, String>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
R: tokio::io::AsyncBufRead + Unpin,
|
||||
F: FnMut() -> Result<String, String>,
|
||||
{
|
||||
let mut end_of_stream_message = end_of_stream_message;
|
||||
const MAX_NOISY_LINES: usize = 20;
|
||||
for _ in 0..MAX_NOISY_LINES {
|
||||
let line = match read_plugin_line(reader, "response").await {
|
||||
Ok(line) => line,
|
||||
Err(err) if err.contains("end of stream") => return Err(end_of_stream_message()?),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
match decode_plugin_response(plugin, request_id, &line) {
|
||||
Ok(response) => return Ok(response),
|
||||
Err(err) if err.starts_with(&format!("Failed to parse plugin '{}' response:", plugin.manifest.id)) => {
|
||||
log::warn!("[plugin:{}] ignored non-protocol stdout: {}", plugin.manifest.id, line.trim_end());
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
match read_plugin_line(reader, "response").await {
|
||||
Ok(_) => {
|
||||
Err(format!("Plugin '{}' wrote too many non-protocol stdout lines before its response", plugin.manifest.id))
|
||||
}
|
||||
Err(err) if err.contains("end of stream") => Err(end_of_stream_message()?),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_plugin_response<T>(plugin: &InstalledPlugin, request_id: u64, response_line: &str) -> Result<T, String>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let response: PluginResponse = serde_json::from_str(response_line)
|
||||
.map_err(|err| format!("Failed to parse plugin '{}' response: {err}", plugin.manifest.id))?;
|
||||
if response.id != request_id {
|
||||
return Err(format!("Plugin '{}' returned mismatched response id", plugin.manifest.id));
|
||||
}
|
||||
if let Some(error) = response.error {
|
||||
return Err(error.message);
|
||||
}
|
||||
let result = response.result.unwrap_or(serde_json::Value::Null);
|
||||
serde_json::from_value(result)
|
||||
.map_err(|err| format!("Failed to decode plugin '{}' result: {err}", plugin.manifest.id))
|
||||
}
|
||||
|
||||
fn resolve_plugin_executable(plugin_dir: &Path, executable: &str) -> PathBuf {
|
||||
let path = PathBuf::from(executable);
|
||||
let resolved = if path.is_absolute() { path } else { plugin_dir.join(path) };
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let bat = resolved.with_extension("bat");
|
||||
if bat.exists() {
|
||||
return bat;
|
||||
}
|
||||
}
|
||||
|
||||
resolved
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{read_decoded_plugin_response, read_plugin_line, InstalledPlugin, PluginManifest};
|
||||
use super::{InstalledPlugin, PluginContribution, PluginManifest, PluginRegistry};
|
||||
#[cfg(unix)]
|
||||
use super::{PluginDriverManifest, PluginDriverSession, PluginRuntimeEnv};
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::BufReader;
|
||||
|
||||
#[tokio::test]
|
||||
async fn reads_non_utf8_plugin_lines_lossily() {
|
||||
let bytes = vec![b'{', b'"', b'e', b'r', b'r', b'o', b'r', b'"', b':', 0xB2, 0xE2, b'}', b'\n'];
|
||||
let mut reader = BufReader::new(std::io::Cursor::new(bytes));
|
||||
|
||||
let line = read_plugin_line(&mut reader, "response").await.expect("line should be readable");
|
||||
|
||||
assert_eq!(line, format!("{{\"error\":{}}}\n", "\u{fffd}\u{fffd}"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn skips_noisy_plugin_stdout_before_json_response() {
|
||||
let plugin = InstalledPlugin {
|
||||
manifest: PluginManifest {
|
||||
id: "jdbc".to_string(),
|
||||
name: "JDBC".to_string(),
|
||||
version: "test".to_string(),
|
||||
protocol_version: 1,
|
||||
description: String::new(),
|
||||
executable: None,
|
||||
drivers: Vec::new(),
|
||||
},
|
||||
path: PathBuf::new(),
|
||||
};
|
||||
let bytes = b"driver banner\n{\"id\":1,\"result\":{\"ok\":true}}\n";
|
||||
let mut reader = BufReader::new(std::io::Cursor::new(bytes));
|
||||
|
||||
let result: serde_json::Value =
|
||||
read_decoded_plugin_response(&plugin, 1, &mut reader, || Ok("closed".to_string()))
|
||||
.await
|
||||
.expect("response should decode after noisy line");
|
||||
|
||||
assert_eq!(result["ok"], true);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
@@ -568,8 +380,8 @@ mod tests {
|
||||
permissions.set_mode(0o755);
|
||||
std::fs::set_permissions(&executable, permissions).unwrap();
|
||||
}
|
||||
let plugin = InstalledPlugin {
|
||||
manifest: PluginManifest {
|
||||
let plugin = InstalledPlugin::new(
|
||||
PluginManifest {
|
||||
id: "jdbc".to_string(),
|
||||
name: "JDBC".to_string(),
|
||||
version: "test".to_string(),
|
||||
@@ -582,13 +394,21 @@ mod tests {
|
||||
kind: "external".to_string(),
|
||||
database_type: Some("jdbc".to_string()),
|
||||
}],
|
||||
contributions: Vec::new(),
|
||||
..PluginManifest::default()
|
||||
},
|
||||
path: dir.clone(),
|
||||
};
|
||||
dir.clone(),
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
);
|
||||
|
||||
let session = PluginDriverSession::start(plugin, "jdbc".to_string(), PluginRuntimeEnv::default())
|
||||
.await
|
||||
.expect("session should start");
|
||||
let session = PluginDriverSession::start(
|
||||
plugin,
|
||||
"jdbc".to_string(),
|
||||
env!("CARGO_PKG_VERSION").to_string(),
|
||||
PluginRuntimeEnv::default(),
|
||||
)
|
||||
.await
|
||||
.expect("session should start");
|
||||
let pid = session.pid().await.expect("child should have a pid");
|
||||
|
||||
session.shutdown().await;
|
||||
@@ -608,4 +428,113 @@ mod tests {
|
||||
.map(|status| status.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_typed_contributions_in_manifest_round_trip() {
|
||||
let manifest: PluginManifest = serde_json::from_value(serde_json::json!({
|
||||
"manifest_version": 1,
|
||||
"id": "sample",
|
||||
"name": "Sample",
|
||||
"version": "1.0.0",
|
||||
"publisher": "example",
|
||||
"engines": { "host_api": "1" },
|
||||
"contributions": [{
|
||||
"type": "connection-provider",
|
||||
"id": "sample.connection",
|
||||
"database_type": "sample",
|
||||
"fields": []
|
||||
}]
|
||||
}))
|
||||
.expect("manifest should parse");
|
||||
|
||||
assert!(matches!(
|
||||
&manifest.contributions[0],
|
||||
PluginContribution::ConnectionProvider(provider) if provider.id == "sample.connection"
|
||||
));
|
||||
let serialized = serde_json::to_value(manifest).expect("manifest should serialize");
|
||||
assert_eq!(serialized["contributions"][0]["database_type"], "sample");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_lists_frontend_contributions_from_disk() {
|
||||
let root = std::env::temp_dir().join(format!("dbx-plugin-registry-test-{}", uuid::Uuid::new_v4()));
|
||||
let plugin_dir = root.join("sample");
|
||||
std::fs::create_dir_all(&plugin_dir).expect("plugin directory should be created");
|
||||
std::fs::write(
|
||||
plugin_dir.join("manifest.json"),
|
||||
serde_json::json!({
|
||||
"manifest_version": 1,
|
||||
"id": "sample",
|
||||
"name": "Sample",
|
||||
"version": "1.0.0",
|
||||
"publisher": "example",
|
||||
"engines": { "host_api": "1" },
|
||||
"contributions": [{
|
||||
"type": "connection-provider",
|
||||
"id": "sample.connection",
|
||||
"database_type": "sample",
|
||||
"fields": []
|
||||
}]
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("manifest should be written");
|
||||
|
||||
let plugins = PluginRegistry::new(root.clone()).list_installed().expect("plugins should be listed");
|
||||
|
||||
assert_eq!(plugins.len(), 1);
|
||||
assert!(matches!(
|
||||
&plugins[0].manifest.contributions[0],
|
||||
PluginContribution::ConnectionProvider(provider) if provider.id == "sample.connection"
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn registry_keeps_other_plugins_visible_when_one_manifest_is_corrupt() {
|
||||
let root = std::env::temp_dir().join(format!("dbx-plugin-corrupt-registry-test-{}", uuid::Uuid::new_v4()));
|
||||
let valid = root.join("valid");
|
||||
let corrupt = root.join("corrupt");
|
||||
std::fs::create_dir_all(&valid).unwrap();
|
||||
std::fs::create_dir_all(&corrupt).unwrap();
|
||||
std::fs::write(
|
||||
valid.join("manifest.json"),
|
||||
serde_json::json!({ "id": "valid", "name": "Valid", "drivers": [] }).to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(corrupt.join("manifest.json"), "{").unwrap();
|
||||
|
||||
let plugins = PluginRegistry::new(root.clone()).list_installed().unwrap();
|
||||
|
||||
assert_eq!(plugins.len(), 2);
|
||||
assert!(plugins.iter().any(|plugin| plugin.manifest.id == "valid" && plugin.compatibility.compatible));
|
||||
assert!(plugins.iter().any(|plugin| {
|
||||
plugin.manifest.id == "corrupt"
|
||||
&& !plugin.compatibility.compatible
|
||||
&& plugin.compatibility.errors.iter().any(|error| error.contains("Failed to parse"))
|
||||
}));
|
||||
let _ = std::fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installed_plugin_info_hides_runtime_paths() {
|
||||
let plugin = InstalledPlugin::new(
|
||||
PluginManifest {
|
||||
manifest_version: 1,
|
||||
id: "sample".to_string(),
|
||||
name: "Sample".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
publisher: "example".to_string(),
|
||||
..PluginManifest::default()
|
||||
},
|
||||
std::path::PathBuf::from("/private/plugin/install"),
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
);
|
||||
let serialized = serde_json::to_value(plugin.info()).unwrap();
|
||||
|
||||
assert!(serialized.get("path").is_none());
|
||||
assert!(serialized["compatibility"].get("backend_executable").is_none());
|
||||
assert!(serialized["compatibility"].get("ui_entry").is_none());
|
||||
assert!(serialized["compatibility"].get("ui_root").is_none());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user