mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 10:40:14 +08:00
refactor(profile): extract script validation and execution
Share the Boa validation and execution API while preserving the save-side timeout and diagnostics.
This commit is contained in:
@@ -38,6 +38,7 @@ clash-verge-i18n = { workspace = true }
|
||||
clash-verge-limiter = { workspace = true }
|
||||
clash-verge-subscription = { workspace = true }
|
||||
clash-verge-runtime-bundle = { workspace = true }
|
||||
clash-verge-profile-script = { workspace = true }
|
||||
tauri-plugin-clash-verge-sysinfo = { workspace = true }
|
||||
tauri-plugin-clipboard-manager = { workspace = true }
|
||||
tauri = { workspace = true, features = [
|
||||
@@ -64,7 +65,7 @@ dirs = "7.0.0"
|
||||
dunce = "1.0.5"
|
||||
nanoid = "0.5"
|
||||
chrono = { workspace = true }
|
||||
boa_engine = "0.22.0"
|
||||
boa_engine = { workspace = true }
|
||||
http-body-util = "0.1.5"
|
||||
percent-encoding = "2.3.2"
|
||||
reqwest = { workspace = true }
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::core::handle;
|
||||
use crate::singleton;
|
||||
use crate::utils::{dirs, help};
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use clash_verge_profile_script::MainDeclaration;
|
||||
|
||||
const SYNTAX_CHECK_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
|
||||
|
||||
@@ -255,22 +256,8 @@ impl CoreConfigValidator {
|
||||
};
|
||||
|
||||
logging!(debug, Type::Validate, "验证脚本文件: {}", path);
|
||||
let has_main =
|
||||
content.contains("function main") || content.contains("const main") || content.contains("let main");
|
||||
|
||||
// Boa parsing is pure CPU; keep it off the async worker, with a timeout.
|
||||
let syntax = crate::process::AsyncHandler::spawn_blocking(move || {
|
||||
use boa_engine::{Context, Source};
|
||||
|
||||
let mut context = Context::default();
|
||||
let _ = context.eval(Source::from_bytes(
|
||||
"var console = Object.freeze({log(...data){},info(...data){},error(...data){},debug(...data){}});",
|
||||
));
|
||||
context
|
||||
.eval(Source::from_bytes(&content))
|
||||
.map(|_| ())
|
||||
.map_err(|err| err.to_string())
|
||||
});
|
||||
let syntax =
|
||||
crate::process::AsyncHandler::spawn_blocking(move || clash_verge_profile_script::validate(&content));
|
||||
let result = match tokio::time::timeout(SYNTAX_CHECK_TIMEOUT, syntax).await {
|
||||
Ok(Ok(evaluated)) => evaluated,
|
||||
Ok(Err(join_err)) => Err(format!("syntax check task failed: {join_err}")),
|
||||
@@ -278,10 +265,10 @@ impl CoreConfigValidator {
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(()) => {
|
||||
Ok(main) => {
|
||||
logging!(debug, Type::Validate, "脚本语法验证通过: {}", path);
|
||||
|
||||
if !has_main {
|
||||
if main == MainDeclaration::Missing {
|
||||
let error_msg = "Script must contain a main function";
|
||||
logging!(warn, Type::Validate, "脚本缺少main函数: {}", path);
|
||||
return Ok(ValidationOutcome::invalid_from_message(error_msg));
|
||||
|
||||
Reference in New Issue
Block a user