XPath plugin

This commit is contained in:
Gregory Schier
2024-01-15 21:27:47 -08:00
parent 1e79f76701
commit c8bd4d0ae0
15 changed files with 8542 additions and 133 deletions

View File

@@ -9,7 +9,7 @@ extern crate objc;
use std::collections::HashMap;
use std::env::current_dir;
use std::fs::{create_dir_all, File, read_to_string};
use std::fs::{create_dir_all, read_to_string, File};
use std::process::exit;
use fern::colors::ColoredLevelConfig;
@@ -17,13 +17,13 @@ use log::{debug, info, warn};
use rand::random;
use serde::Serialize;
use serde_json::Value;
use sqlx::{Pool, Sqlite, SqlitePool};
use sqlx::migrate::Migrator;
use sqlx::types::Json;
use tauri::{AppHandle, RunEvent, State, Window, WindowUrl, Wry};
use tauri::{Manager, WindowEvent};
use sqlx::{Pool, Sqlite, SqlitePool};
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
use tauri::{AppHandle, RunEvent, State, Window, WindowUrl, Wry};
use tauri::{Manager, WindowEvent};
use tauri_plugin_log::{fern, LogTarget};
use tauri_plugin_window_state::{StateFlags, WindowExt};
use tokio::sync::Mutex;
@@ -37,10 +37,10 @@ use crate::plugin::{ImportResources, ImportResult};
use crate::updates::{update_mode_from_str, UpdateMode, YaakUpdater};
mod analytics;
mod http;
mod models;
mod plugin;
mod render;
mod http;
mod updates;
mod window_ext;
mod window_menu;
@@ -103,15 +103,26 @@ async fn filter_response(
return Err("Response body not found".to_string());
}
let mut content_type = "".to_string();
for header in response.headers.iter() {
if header.name.to_lowercase() == "content-type" {
content_type = header.value.to_string().to_lowercase();
break;
}
}
// TODO: Have plugins register their own content type (regex?)
let plugin_name = if content_type.contains("json") {
"filter-jsonpath"
} else {
"filter-xpath"
};
let body = read_to_string(response.body_path.unwrap()).unwrap();
let filter_result = plugin::run_plugin_filter(
&window.app_handle(),
"filter-jsonpath",
filter,
&body,
)
.await
.expect("Failed to run filter");
let filter_result =
plugin::run_plugin_filter(&window.app_handle(), plugin_name, filter, &body)
.await
.expect("Failed to run filter");
Ok(filter_result.filtered)
}
@@ -310,12 +321,10 @@ async fn create_workspace(
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
) -> Result<models::Workspace, String> {
let pool = &*db_instance.lock().await;
let created_workspace = models::upsert_workspace(
pool,
models::Workspace::new(name.to_string()),
)
.await
.expect("Failed to create Workspace");
let created_workspace =
models::upsert_workspace(pool, models::Workspace::new(name.to_string()))
.await
.expect("Failed to create Workspace");
emit_and_return(&window, "created_model", created_workspace)
}

View File

@@ -1,12 +1,11 @@
use std::fs;
use boa_engine::builtins::promise::PromiseState;
use boa_engine::{
js_string,
module::{ModuleLoader, SimpleModuleLoader},
property::Attribute,
Context, JsArgs, JsNativeError, JsValue, Module, NativeFunction, Source,
Context, js_string, JsNativeError, JsValue, Module, module::SimpleModuleLoader,
property::Attribute, Source,
};
use boa_engine::builtins::promise::PromiseState;
use boa_engine::module::ModuleLoader;
use boa_runtime::Console;
use log::{debug, error};
use serde::{Deserialize, Serialize};
@@ -43,10 +42,7 @@ pub async fn run_plugin_filter(
app_handle,
plugin_name,
"pluginHookResponseFilter",
&[
js_string!(response_body).into(),
js_string!(filter).into(),
],
&[js_string!(response_body).into(), js_string!(filter).into()],
);
if result_json.is_null() {
@@ -111,7 +107,6 @@ fn run_plugin(
.expect("failed to create context");
add_runtime(context);
add_globals(context);
let source = Source::from_filepath(&plugin_index_file).expect("Error opening file");
@@ -119,7 +114,6 @@ fn run_plugin(
let module = Module::parse(source, None, context).expect("failed to parse module");
// Insert parsed entrypoint into the module loader
// TODO: Is this needed if loaded from file already?
loader.insert(plugin_index_file, module.clone());
let promise_result = module
@@ -162,26 +156,9 @@ fn run_plugin(
}
}
fn add_runtime(context: &mut Context<'_>) {
fn add_runtime(context: &mut Context) {
let console = Console::init(context);
context
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
.expect("the console builtin shouldn't exist");
}
fn add_globals(context: &mut Context<'_>) {
context
.register_global_builtin_callable(
"sayHello",
1,
NativeFunction::from_fn_ptr(|_, args, context| {
let value: String = args
.get_or_undefined(0)
.try_js_into(context)
.expect("failed to convert arg");
println!("Hello {}!", value);
Ok(value.into())
}),
)
.expect("failed to register global");
}