mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-01 04:24:03 +01:00
Move response filters into Rust (#755)
This commit is contained in:
1 parent
19908b1902
commit
9b3715024d
42 files changed
+15538
-399
No files matched your search
@@ -69,6 +69,8 @@ pub enum InternalEventPayload {
|
||||
|
||||
FilterRequest(FilterRequest),
|
||||
FilterResponse(FilterResponse),
|
||||
FilterTextRequest(FilterTextRequest),
|
||||
FilterTextResponse(FilterResponse),
|
||||
|
||||
ExportHttpRequestRequest(ExportHttpRequestRequest),
|
||||
ExportHttpRequestResponse(ExportHttpRequestResponse),
|
||||
@@ -286,9 +288,28 @@ pub struct FilterRequest {
|
||||
pub struct FilterResponse {
|
||||
pub content: String,
|
||||
#[ts(optional)]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// A plugin asking the host to run a JSONPath or XPath query over some text.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_events.ts")]
|
||||
pub struct FilterTextRequest {
|
||||
pub language: FilterLanguage,
|
||||
pub text: String,
|
||||
pub query: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[ts(export, export_to = "gen_events.ts")]
|
||||
pub enum FilterLanguage {
|
||||
JsonPath,
|
||||
XPath,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_events.ts")]
|
||||
|
||||
@@ -9,14 +9,13 @@ use crate::events::{
|
||||
CallHttpAuthenticationRequest, CallHttpAuthenticationResponse, CallHttpRequestActionRequest,
|
||||
CallTemplateFunctionArgs, CallTemplateFunctionRequest, CallTemplateFunctionResponse,
|
||||
CallWebsocketRequestActionRequest, CallWorkspaceActionRequest, EmptyPayload, ErrorResponse,
|
||||
FilterRequest, FilterResponse, GetFolderActionsResponse, GetGrpcRequestActionsResponse,
|
||||
GetHttpAuthenticationConfigRequest, GetHttpAuthenticationConfigResponse,
|
||||
GetHttpAuthenticationSummaryResponse, GetHttpRequestActionsResponse,
|
||||
GetTemplateFunctionConfigRequest, GetTemplateFunctionConfigResponse,
|
||||
GetTemplateFunctionSummaryResponse, GetThemesRequest, GetThemesResponse,
|
||||
GetWebsocketRequestActionsResponse, GetWorkspaceActionsResponse, ImportRequest, ImportResponse,
|
||||
InternalEvent, InternalEventPayload, JsonPrimitive, PluginContext, RenderPurpose,
|
||||
ShowToastRequest,
|
||||
GetFolderActionsResponse, GetGrpcRequestActionsResponse, GetHttpAuthenticationConfigRequest,
|
||||
GetHttpAuthenticationConfigResponse, GetHttpAuthenticationSummaryResponse,
|
||||
GetHttpRequestActionsResponse, GetTemplateFunctionConfigRequest,
|
||||
GetTemplateFunctionConfigResponse, GetTemplateFunctionSummaryResponse, GetThemesRequest,
|
||||
GetThemesResponse, GetWebsocketRequestActionsResponse, GetWorkspaceActionsResponse,
|
||||
ImportRequest, ImportResponse, InternalEvent, InternalEventPayload, JsonPrimitive,
|
||||
PluginContext, RenderPurpose, ShowToastRequest,
|
||||
};
|
||||
use crate::native_template_functions::{template_function_keyring, template_function_secure};
|
||||
use crate::nodejs::start_nodejs_plugin_runtime;
|
||||
@@ -1139,46 +1138,6 @@ impl PluginManager {
|
||||
Some(resp) => Ok(resp),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn filter_data(
|
||||
&self,
|
||||
plugin_context: &PluginContext,
|
||||
filter: &str,
|
||||
content: &str,
|
||||
content_type: &str,
|
||||
) -> Result<FilterResponse> {
|
||||
let ct = content_type.to_lowercase();
|
||||
let plugin_name = if ct.contains("xml") || ct.contains("html") {
|
||||
"@yaak/filter-xpath"
|
||||
} else {
|
||||
"@yaak/filter-jsonpath"
|
||||
};
|
||||
|
||||
let plugin = self
|
||||
.get_plugin_by_name(plugin_name)
|
||||
.await
|
||||
.ok_or(PluginNotFoundErr(plugin_name.to_string()))?;
|
||||
|
||||
let event = self
|
||||
.send_to_plugin_and_wait(
|
||||
plugin_context,
|
||||
&plugin,
|
||||
&InternalEventPayload::FilterRequest(FilterRequest {
|
||||
filter: filter.to_string(),
|
||||
content: content.to_string(),
|
||||
}),
|
||||
Duration::from_secs(5),
|
||||
)
|
||||
.await?;
|
||||
|
||||
match event.payload {
|
||||
InternalEventPayload::FilterResponse(resp) => Ok(resp),
|
||||
InternalEventPayload::EmptyResponse(_) => {
|
||||
Err(PluginErr("Filter returned empty".to_string()))
|
||||
}
|
||||
e => Err(PluginErr(format!("Export returned invalid event {:?}", e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn source_priority(source: &PluginSource) -> i32 {
|
||||
@@ -1207,8 +1166,9 @@ fn prefer_plugin(candidate: &Plugin, existing: &Plugin) -> bool {
|
||||
/// whose directory isn't in this list are filtered out by `resolve_plugins_for_runtime`.
|
||||
///
|
||||
/// `exporter-curl` was renamed to `action-copy-curl` in 19ffcd18, which is why affected
|
||||
/// installs show "Copy as cURL" twice.
|
||||
const REMOVED_BUNDLED_PLUGIN_DIRS: &[&str] = &["exporter-curl"];
|
||||
/// installs show "Copy as cURL" twice. `filter-jsonpath` and `filter-xpath` moved into
|
||||
/// the `yaak-filter` crate.
|
||||
const REMOVED_BUNDLED_PLUGIN_DIRS: &[&str] = &["exporter-curl", "filter-jsonpath", "filter-xpath"];
|
||||
|
||||
/// Whether a plugin directory path is one of the known-removed bundled plugins.
|
||||
fn is_removed_bundled_plugin_dir(dir: &str) -> bool {
|
||||
@@ -1256,6 +1216,8 @@ mod tests {
|
||||
r"C:\Users\me\AppData\Local\Yaak\vendored\plugins\exporter-curl"
|
||||
));
|
||||
assert!(is_removed_bundled_plugin_dir("vendored/plugins/exporter-curl/"));
|
||||
assert!(is_removed_bundled_plugin_dir("vendored/plugins/filter-jsonpath"));
|
||||
assert!(is_removed_bundled_plugin_dir("vendored/plugins/filter-xpath"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user