gm api通信
Some checks failed
test / Run tests (push) Failing after 9s
build / Build (push) Failing after 16s

This commit is contained in:
2025-01-26 15:39:14 +08:00
parent 415f00a3d1
commit 9f8f7c8347
29 changed files with 1941 additions and 112 deletions

View File

@ -1,6 +1,6 @@
import { WindowMessage } from "@Packages/message/window_message";
import { sendMessage } from "../utils";
import { SCRIPT_RUN_STATUS } from "@App/app/repo/scripts";
import { sendMessage } from "@Packages/message/client";
export function preparationSandbox(msg: WindowMessage) {
return sendMessage(msg, "preparationSandbox");

View File

@ -1,4 +1,4 @@
import { Server } from "@Packages/message/server";
import { forwardMessage, Server } from "@Packages/message/server";
import { ScriptService } from "./script";
import { Broker, MessageQueue } from "@Packages/message/message_queue";
import { Logger, LoggerDAO } from "@App/app/repo/logger";
@ -21,6 +21,8 @@ export class OffscreenManager {
private broker: Broker = new Broker(this.extensionMessage);
private serviceWorker = new ServiceWorkerClient(this.extensionMessage);
logger(data: Logger) {
const dao = new LoggerDAO();
dao.save(data);
@ -28,12 +30,11 @@ export class OffscreenManager {
preparationSandbox() {
// 通知初始化好环境了
const serviceWorker = new ServiceWorkerClient();
serviceWorker.preparationOffscreen();
this.serviceWorker.preparationOffscreen();
}
sendMessageToServiceWorker(data: { action: string; data: any }) {
return sendMessage(data.action, data.data);
return sendMessage(this.extensionMessage, data.action, data.data);
}
initManager() {
@ -44,5 +45,7 @@ export class OffscreenManager {
this.windowApi.on("sendMessageToServiceWorker", this.sendMessageToServiceWorker.bind(this));
const script = new ScriptService(group.group("script"), this.mq, this.windowMessage, this.broker);
script.init();
// 转发gm api请求
forwardMessage("serviceWorker/runtime/gmApi", this.windowApi, this.extensionMessage);
}
}

View File

@ -1,6 +1,6 @@
import { ScriptRunResouce } from "@App/app/repo/scripts";
import { sendMessage } from "@Packages/message/client";
import { WindowMessage } from "@Packages/message/window_message";
import { sendMessage } from "../utils";
export function enableScript(msg: WindowMessage, data: ScriptRunResouce) {
return sendMessage(msg, "enableScript", data);

View File

@ -1,9 +1,6 @@
import { Server } from "@Packages/message/server";
import { WindowMessage } from "@Packages/message/window_message";
import { preparationSandbox } from "../offscreen/client";
import { Script, SCRIPT_TYPE_BACKGROUND } from "@App/app/repo/scripts";
import { CronJob } from "cron";
import ExecScript from "@App/runtime/content/exec_script";
import { Runtime } from "./runtime";
// sandbox环境的管理器

View File

@ -107,7 +107,7 @@ export class Runtime {
// 暂未实现执行完成后立马释放,会在下一次执行时释放
await this.stopScript(script.uuid);
}
const exec = new BgExecScriptWarp(script);
const exec = new BgExecScriptWarp(script, this.windowMessage);
this.execScripts.set(script.uuid, exec);
proxyUpdateRunStatus(this.windowMessage, { uuid: script.uuid, runStatus: SCRIPT_RUN_STATUS_RUNNING });
// 修改掉脚本掉最后运行时间, 数据库也需要修改

View File

@ -3,10 +3,11 @@ import { Client } from "@Packages/message/client";
import { InstallSource } from ".";
import { Broker } from "@Packages/message/message_queue";
import { Resource } from "@App/app/repo/resource";
import { Message } from "@Packages/message/server";
export class ServiceWorkerClient extends Client {
constructor() {
super("serviceWorker");
constructor(msg: Message) {
super(msg, "serviceWorker");
}
preparationOffscreen() {
@ -15,8 +16,8 @@ export class ServiceWorkerClient extends Client {
}
export class ScriptClient extends Client {
constructor() {
super("serviceWorker/script");
constructor(msg: Message) {
super(msg, "serviceWorker/script");
}
// 获取安装信息
@ -50,8 +51,8 @@ export class ScriptClient extends Client {
}
export class ResourceClient extends Client {
constructor() {
super("serviceWorker/resource");
constructor(msg: Message) {
super(msg, "serviceWorker/resource");
}
getScriptResources(script: Script): Promise<{ [key: string]: Resource }> {
@ -60,8 +61,8 @@ export class ResourceClient extends Client {
}
export class ValueClient extends Client {
constructor() {
super("serviceWorker/value");
constructor(msg: Message) {
super(msg, "serviceWorker/value");
}
getScriptValue(script: Script) {

View File

@ -29,7 +29,7 @@ export default class ServiceWorkerManager {
value.init();
const script = new ScriptService(group.group("script"), this.mq, value, resource);
script.init();
const runtime = new RuntimeService(group.group("runtime"), this.mq);
const runtime = new RuntimeService(group.group("runtime"), this.mq, value);
runtime.init();
}
}

View File

@ -1,20 +1,23 @@
import { MessageQueue } from "@Packages/message/message_queue";
import { ScriptEnableCallbackValue } from "./client";
import { Group } from "@Packages/message/server";
import { Script, SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL, ScriptDAO } from "@App/app/repo/scripts";
import { Script, SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL, ScriptAndCode, ScriptDAO } from "@App/app/repo/scripts";
import GMApi from "@App/runtime/service_worker/gm_api";
import { ValueService } from "./value";
export class RuntimeService {
scriptDAO: ScriptDAO = new ScriptDAO();
constructor(
private group: Group,
private mq: MessageQueue
private mq: MessageQueue,
private value: ValueService
) {}
async init() {
// 监听脚本开启
this.mq.addListener("enableScript", async (data: ScriptEnableCallbackValue) => {
const script = await this.scriptDAO.get(data.uuid);
const script = await this.scriptDAO.getAndCode(data.uuid);
if (!script) {
return;
}
@ -48,9 +51,17 @@ export class RuntimeService {
this.mq.publish("enableScript", { uuid: script.uuid, enable: true });
});
});
// 初始化gm api
const gmApi = new GMApi(this.value);
gmApi.start();
// 处理请求
this.group.on("gmApi", gmApi.handlerRequest);
}
registryPageScript(script: Script) {}
registryPageScript(script: ScriptAndCode) {
console.log(script);
}
unregistryPageScript(script: Script) {}
}

View File

@ -11,14 +11,12 @@ import {
SCRIPT_RUN_STATUS,
SCRIPT_STATUS_DISABLE,
SCRIPT_STATUS_ENABLE,
SCRIPT_TYPE_NORMAL,
ScriptCodeDAO,
ScriptDAO,
ScriptRunResouce,
} from "@App/app/repo/scripts";
import { MessageQueue } from "@Packages/message/message_queue";
import { InstallSource } from ".";
import { ScriptEnableCallbackValue } from "./client";
import { ResourceService } from "./resource";
import { ValueService } from "./value";
import { compileScriptCode } from "@App/runtime/content/utils";
@ -163,23 +161,26 @@ export class ScriptService {
upsertBy,
});
let update = false;
const dao = new ScriptDAO();
// 判断是否已经安装
const oldScript = await dao.findByUUID(script.uuid);
const oldScript = await this.scriptDAO.get(script.uuid);
if (oldScript) {
// 执行更新逻辑
update = true;
script.selfMetadata = oldScript.selfMetadata;
}
return dao
return this.scriptDAO
.save(script)
.then(() => {
.then(async () => {
await this.scriptCodeDAO.save({
uuid: script.uuid,
code: param.code,
});
logger.info("install success");
// 广播一下
this.mq.publish("installScript", { script, update });
return {};
})
.catch((e) => {
.catch((e: any) => {
logger.error("install error", Logger.E(e));
throw e;
});
@ -187,13 +188,12 @@ export class ScriptService {
async deleteScript(uuid: string) {
const logger = this.logger.with({ uuid });
const dao = new ScriptDAO();
const script = await dao.findByUUID(uuid);
const script = await this.scriptDAO.get(uuid);
if (!script) {
logger.error("script not found");
throw new Error("script not found");
}
return dao
return this.scriptDAO
.delete(uuid)
.then(() => {
logger.info("delete success");
@ -208,13 +208,12 @@ export class ScriptService {
async enableScript(param: { uuid: string; enable: boolean }) {
const logger = this.logger.with({ uuid: param.uuid, enable: param.enable });
const dao = new ScriptDAO();
const script = await dao.findByUUID(param.uuid);
const script = await this.scriptDAO.get(param.uuid);
if (!script) {
logger.error("script not found");
throw new Error("script not found");
}
return dao
return this.scriptDAO
.update(param.uuid, { status: param.enable ? SCRIPT_STATUS_ENABLE : SCRIPT_STATUS_DISABLE })
.then(() => {
logger.info("enable success");
@ -228,7 +227,7 @@ export class ScriptService {
}
async fetchInfo(uuid: string) {
const script = await new ScriptDAO().findByUUID(uuid);
const script = await this.scriptDAO.get(uuid);
if (!script) {
return null;
}
@ -236,7 +235,7 @@ export class ScriptService {
}
async updateRunStatus(params: { uuid: string; runStatus: SCRIPT_RUN_STATUS; error?: string; nextruntime?: number }) {
await new ScriptDAO().update(params.uuid, {
await this.scriptDAO.update(params.uuid, {
runStatus: params.runStatus,
lastruntime: new Date().getTime(),
error: params.error,

View File

@ -1,12 +1,14 @@
import LoggerCore from "@App/app/logger/core";
import Logger from "@App/app/logger/logger";
import { Script } from "@App/app/repo/scripts";
import { Script, ScriptDAO } from "@App/app/repo/scripts";
import { ValueDAO } from "@App/app/repo/value";
import { storageKey } from "@App/runtime/utils";
import { MessageQueue } from "@Packages/message/message_queue";
import { Group } from "@Packages/message/server";
export class ValueService {
logger: Logger;
scriptDAO: ScriptDAO = new ScriptDAO();
valueDAO: ValueDAO = new ValueDAO();
constructor(
@ -16,21 +18,37 @@ export class ValueService {
this.logger = LoggerCore.logger().with({ service: "value" });
}
storageKey(script: Script): string {
if (script.metadata.storagename) {
return script.metadata.storagename[0];
}
return script.uuid;
}
async getScriptValue(script: Script) {
const ret = await this.valueDAO.get(this.storageKey(script));
const ret = await this.valueDAO.get(storageKey(script));
if (!ret) {
return {};
}
return Promise.resolve(ret?.data);
}
async setValue(uuid: string, key: string, value: any): Promise<boolean> {
// 查询出脚本
const script = await this.scriptDAO.get(uuid);
if (!script) {
return Promise.reject(new Error("script not found"));
}
// 查询老的值
const oldValue = await this.valueDAO.get(storageKey(script));
if (!oldValue) {
this.valueDAO.save(storageKey(script), {
uuid: script.uuid,
storageName: storageKey(script),
data: { [key]: value },
createtime: Date.now(),
updatetime: Date.now(),
});
} else {
oldValue.data[key] = value;
this.valueDAO.save(storageKey(script), oldValue);
}
return Promise.resolve(true);
}
init() {
this.group.on("getScriptValue", this.getScriptValue.bind(this));
}

View File

@ -1,8 +0,0 @@
import { WindowMessage } from "@Packages/message/window_message";
export function sendMessage(msg: WindowMessage, action: string, data?: any) {
return msg.sendMessage({
action,
data,
});
}