mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-04 22:06:24 +01:00
Add Bruno imports and a file-tree importer API (#706)
This commit is contained in:
1 parent
46464cfacf
commit
aa10c44a45
109 files changed
+4684
-879
No files matched your search
@@ -240,7 +240,20 @@ pub struct ReloadResponse {
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_events.ts")]
|
||||
pub struct ImportRequest {
|
||||
/// Kept for text-only importers and older runtimes.
|
||||
pub content: String,
|
||||
#[ts(optional)]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub source: Option<ImportFileSource>,
|
||||
}
|
||||
|
||||
/// Transport descriptor; plugins see a scoped ImportFiles API, not this representation.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "snake_case", tag = "type")]
|
||||
#[ts(export, export_to = "gen_events.ts")]
|
||||
pub enum ImportFileSource {
|
||||
File { name: String, base64: String },
|
||||
Directory { path: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
use crate::events::{ImportFileSource, ImportRequest};
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
pub const MAX_IMPORT_BYTES: u64 = 64 * 1024 * 1024;
|
||||
|
||||
impl ImportRequest {
|
||||
pub fn from_text(content: &str) -> Self {
|
||||
Self { content: content.to_string(), source: None }
|
||||
}
|
||||
|
||||
pub fn from_bytes(name: &str, bytes: &[u8]) -> io::Result<Self> {
|
||||
if bytes.len() as u64 > MAX_IMPORT_BYTES {
|
||||
return Err(io::Error::other("Import file exceeds the 64 MiB limit"));
|
||||
}
|
||||
Ok(Self {
|
||||
content: std::str::from_utf8(bytes).unwrap_or_default().to_string(),
|
||||
source: Some(ImportFileSource::File {
|
||||
name: name.to_string(),
|
||||
base64: STANDARD.encode(bytes),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_path(path: &Path) -> io::Result<Self> {
|
||||
let path = fs::canonicalize(path)?;
|
||||
let metadata = fs::metadata(&path)?;
|
||||
if metadata.is_dir() {
|
||||
return Ok(Self {
|
||||
content: String::new(),
|
||||
source: Some(ImportFileSource::Directory {
|
||||
path: path
|
||||
.to_str()
|
||||
.ok_or_else(|| io::Error::other("Import path is not UTF-8"))?
|
||||
.to_string(),
|
||||
}),
|
||||
});
|
||||
}
|
||||
if !metadata.is_file() {
|
||||
return Err(io::Error::other("Import source must be a file or directory"));
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
File::open(&path)?.take(MAX_IMPORT_BYTES + 1).read_to_end(&mut bytes)?;
|
||||
Self::from_bytes(path.file_name().and_then(|v| v.to_str()).unwrap_or("input"), &bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn text_and_binary_inputs_preserve_bytes() {
|
||||
let input = ImportRequest::from_bytes("api.yml", "café".as_bytes()).unwrap();
|
||||
assert_eq!(input.content, "café");
|
||||
let input = ImportRequest::from_bytes("data.zip", &[0x50, 0x4b, 0xff]).unwrap();
|
||||
assert_eq!(input.content, "");
|
||||
let Some(ImportFileSource::File { name, base64 }) = input.source else {
|
||||
panic!("file input")
|
||||
};
|
||||
assert_eq!(name, "data.zip");
|
||||
assert_eq!(STANDARD.decode(base64).unwrap(), vec![0x50, 0x4b, 0xff]);
|
||||
assert!(ImportRequest::from_text("text").source.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn directory_input_is_lazy_and_canonical() {
|
||||
let input = ImportRequest::from_path(Path::new(".")).unwrap();
|
||||
let Some(ImportFileSource::Directory { path }) = input.source else {
|
||||
panic!("directory input")
|
||||
};
|
||||
assert_eq!(Path::new(&path), fs::canonicalize(".").unwrap());
|
||||
assert!(input.content.is_empty());
|
||||
assert!(ImportRequest::from_path(Path::new("does-not-exist-yaak-import")).is_err());
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ pub mod api;
|
||||
mod checksum;
|
||||
pub mod error;
|
||||
pub mod events;
|
||||
mod import_input;
|
||||
pub use import_input::MAX_IMPORT_BYTES;
|
||||
pub mod install;
|
||||
pub mod manager;
|
||||
pub mod native_template_functions;
|
||||
|
||||
@@ -1091,18 +1091,32 @@ impl PluginManager {
|
||||
&self,
|
||||
plugin_context: &PluginContext,
|
||||
content: &str,
|
||||
) -> Result<ImportResponse> {
|
||||
self.import_input(plugin_context, &ImportRequest::from_text(content)).await
|
||||
}
|
||||
|
||||
pub async fn import_input(
|
||||
&self,
|
||||
plugin_context: &PluginContext,
|
||||
input: &ImportRequest,
|
||||
) -> Result<ImportResponse> {
|
||||
let reply_events = self
|
||||
.send_and_wait(
|
||||
plugin_context,
|
||||
&InternalEventPayload::ImportRequest(ImportRequest {
|
||||
content: content.to_string(),
|
||||
}),
|
||||
&InternalEventPayload::ImportRequest(input.clone()),
|
||||
Duration::from_secs(60),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// TODO: Don't just return the first valid response
|
||||
let errors = reply_events
|
||||
.iter()
|
||||
.filter_map(|e| match &e.payload {
|
||||
InternalEventPayload::ErrorResponse(error) => Some(error.error.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ");
|
||||
let result = reply_events.into_iter().find_map(|e| match e {
|
||||
InternalEvent {
|
||||
plugin_name,
|
||||
@@ -1120,7 +1134,8 @@ impl PluginManager {
|
||||
});
|
||||
|
||||
match result {
|
||||
None => Err(PluginErr("No importers found for file contents".to_string())),
|
||||
None if !errors.is_empty() => Err(PluginErr(errors)),
|
||||
None => Err(PluginErr("No importers found for this input".to_string())),
|
||||
Some(resp) => Ok(resp),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user