调整通信
Some checks failed
test / Run tests (push) Failing after 15s
build / Build (push) Failing after 19s
Some checks failed
test / Run tests (push) Failing after 15s
build / Build (push) Failing after 19s
This commit is contained in:
@ -1,19 +1,20 @@
|
||||
import { WindowMessage } from "@Packages/message/window_message";
|
||||
import { SCRIPT_RUN_STATUS } from "@App/app/repo/scripts";
|
||||
import { SCRIPT_RUN_STATUS, ScriptRunResouce } from "@App/app/repo/scripts";
|
||||
import { sendMessage } from "@Packages/message/client";
|
||||
import { MessageSend } from "@Packages/message/server";
|
||||
|
||||
export function preparationSandbox(msg: WindowMessage) {
|
||||
return sendMessage(msg, "preparationSandbox");
|
||||
return sendMessage(msg, "offscreen/preparationSandbox");
|
||||
}
|
||||
|
||||
// 代理发送消息到ServiceWorker
|
||||
export function sendMessageToServiceWorker(msg: WindowMessage, action: string, data?: any) {
|
||||
return sendMessage(msg, "sendMessageToServiceWorker", { action, data });
|
||||
return sendMessage(msg, "offscreen/sendMessageToServiceWorker", { action, data });
|
||||
}
|
||||
|
||||
// 代理连接ServiceWorker
|
||||
export function connectServiceWorker(msg: WindowMessage) {
|
||||
return sendMessage(msg, "connectServiceWorker");
|
||||
return sendMessage(msg, "offscreen/connectServiceWorker");
|
||||
}
|
||||
|
||||
export function proxyUpdateRunStatus(
|
||||
@ -22,3 +23,11 @@ export function proxyUpdateRunStatus(
|
||||
) {
|
||||
return sendMessageToServiceWorker(msg, "script/updateRunStatus", data);
|
||||
}
|
||||
|
||||
export function runScript(msg: MessageSend, data: ScriptRunResouce) {
|
||||
return sendMessage(msg, "offscreen/script/runScript", data);
|
||||
}
|
||||
|
||||
export function stopScript(msg: MessageSend, uuid: string) {
|
||||
return sendMessage(msg, "offscreen/script/stopScript", uuid);
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { forwardMessage, MessageSend, Server } from "@Packages/message/server";
|
||||
import { ScriptService } from "./script";
|
||||
import { Broker } from "@Packages/message/message_queue";
|
||||
import { Logger, LoggerDAO } from "@App/app/repo/logger";
|
||||
import { WindowMessage } from "@Packages/message/window_message";
|
||||
import { ExtensionMessageSend } from "@Packages/message/extension_message";
|
||||
import { ServiceWorkerClient } from "../service_worker/client";
|
||||
import { sendMessage } from "@Packages/message/client";
|
||||
import GMApi from "./gm_api";
|
||||
import { MessageQueue } from "@Packages/message/message_queue";
|
||||
|
||||
// offscreen环境的管理器
|
||||
export class OffscreenManager {
|
||||
@ -14,9 +14,9 @@ export class OffscreenManager {
|
||||
|
||||
private windowMessage = new WindowMessage(window, sandbox, true);
|
||||
|
||||
private windowApi: Server = new Server(this.windowMessage);
|
||||
private windowApi: Server = new Server("offscreen", this.windowMessage);
|
||||
|
||||
private broker: Broker = new Broker(this.extensionMessage);
|
||||
private messageQueue: MessageQueue = new MessageQueue();
|
||||
|
||||
private serviceWorker = new ServiceWorkerClient(this.extensionMessage);
|
||||
|
||||
@ -31,7 +31,7 @@ export class OffscreenManager {
|
||||
}
|
||||
|
||||
sendMessageToServiceWorker(data: { action: string; data: any }) {
|
||||
return sendMessage(this.extensionMessage, data.action, data.data);
|
||||
return sendMessage(this.extensionMessage, "serviceWorker/" + data.action, data.data);
|
||||
}
|
||||
|
||||
async initManager() {
|
||||
@ -39,10 +39,17 @@ export class OffscreenManager {
|
||||
this.windowApi.on("logger", this.logger.bind(this));
|
||||
this.windowApi.on("preparationSandbox", this.preparationSandbox.bind(this));
|
||||
this.windowApi.on("sendMessageToServiceWorker", this.sendMessageToServiceWorker.bind(this));
|
||||
const script = new ScriptService(this.extensionMessage, this.windowMessage, this.broker);
|
||||
const script = new ScriptService(
|
||||
this.windowApi.group("script"),
|
||||
this.extensionMessage,
|
||||
this.windowMessage,
|
||||
this.messageQueue
|
||||
);
|
||||
script.init();
|
||||
// 转发gm api请求
|
||||
forwardMessage("runtime/gmApi", this.windowApi, this.extensionMessage);
|
||||
// 转发从sandbox来的gm api请求
|
||||
forwardMessage("serviceWorker", "runtime/gmApi", this.windowApi, this.extensionMessage);
|
||||
// 转发message queue请求
|
||||
|
||||
const gmApi = new GMApi(this.windowApi.group("gmApi"));
|
||||
gmApi.init();
|
||||
|
||||
|
@ -1,17 +1,12 @@
|
||||
import LoggerCore from "@App/app/logger/core";
|
||||
import Logger from "@App/app/logger/logger";
|
||||
import { Broker } from "@Packages/message/message_queue";
|
||||
import { MessageQueue } from "@Packages/message/message_queue";
|
||||
import { WindowMessage } from "@Packages/message/window_message";
|
||||
import {
|
||||
ResourceClient,
|
||||
ScriptClient,
|
||||
subscribeScriptEnable,
|
||||
subscribeScriptInstall,
|
||||
ValueClient,
|
||||
} from "../service_worker/client";
|
||||
import { SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL } from "@App/app/repo/scripts";
|
||||
import { disableScript, enableScript } from "../sandbox/client";
|
||||
import { MessageSend } from "@Packages/message/server";
|
||||
import { ResourceClient, ScriptClient, ValueClient } from "../service_worker/client";
|
||||
import { SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL, ScriptRunResouce } from "@App/app/repo/scripts";
|
||||
import { disableScript, enableScript, runScript, stopScript } from "../sandbox/client";
|
||||
import { Group, MessageSend } from "@Packages/message/server";
|
||||
import { subscribeScriptEnable, subscribeScriptInstall } from "../queue";
|
||||
|
||||
export class ScriptService {
|
||||
logger: Logger;
|
||||
@ -21,15 +16,25 @@ export class ScriptService {
|
||||
valueClient: ValueClient = new ValueClient(this.extensionMessage);
|
||||
|
||||
constructor(
|
||||
private group: Group,
|
||||
private extensionMessage: MessageSend,
|
||||
private windowMessage: WindowMessage,
|
||||
private broker: Broker
|
||||
private messageQueue: MessageQueue
|
||||
) {
|
||||
this.logger = LoggerCore.logger().with({ service: "script" });
|
||||
}
|
||||
|
||||
runScript(script: ScriptRunResouce) {
|
||||
runScript(this.windowMessage, script);
|
||||
}
|
||||
|
||||
stopScript(uuid: string) {
|
||||
stopScript(this.windowMessage, uuid);
|
||||
}
|
||||
|
||||
async init() {
|
||||
subscribeScriptEnable(this.broker, async (data) => {
|
||||
subscribeScriptEnable(this.messageQueue, async (data) => {
|
||||
console.log("subscribeScriptEnable", data);
|
||||
const script = await this.scriptClient.info(data.uuid);
|
||||
if (script.type === SCRIPT_TYPE_NORMAL) {
|
||||
return;
|
||||
@ -42,7 +47,8 @@ export class ScriptService {
|
||||
disableScript(this.windowMessage, script.uuid);
|
||||
}
|
||||
});
|
||||
subscribeScriptInstall(this.broker, async (data) => {
|
||||
subscribeScriptInstall(this.messageQueue, async (data) => {
|
||||
console.log("subscribeScriptInstall", data);
|
||||
// 判断是开启还是关闭
|
||||
if (data.script.status === SCRIPT_STATUS_ENABLE) {
|
||||
// 构造脚本运行资源,发送给沙盒运行
|
||||
@ -52,5 +58,8 @@ export class ScriptService {
|
||||
disableScript(this.windowMessage, data.script.uuid);
|
||||
}
|
||||
});
|
||||
|
||||
this.group.on("runScript", this.runScript.bind(this));
|
||||
this.group.on("stopScript", this.stopScript.bind(this));
|
||||
}
|
||||
}
|
||||
|
29
src/app/service/queue.ts
Normal file
29
src/app/service/queue.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { MessageQueue } from "@Packages/message/message_queue";
|
||||
import { Script, SCRIPT_RUN_STATUS } from "../repo/scripts";
|
||||
|
||||
export function subscribeScriptInstall(
|
||||
messageQueue: MessageQueue,
|
||||
callback: (message: { script: Script; update: boolean }) => void
|
||||
) {
|
||||
return messageQueue.subscribe("installScript", callback);
|
||||
}
|
||||
|
||||
export function subscribeScriptDelete(messageQueue: MessageQueue, callback: (message: { uuid: string }) => void) {
|
||||
return messageQueue.subscribe("deleteScript", callback);
|
||||
}
|
||||
|
||||
export type ScriptEnableCallbackValue = { uuid: string; enable: boolean };
|
||||
|
||||
export function subscribeScriptEnable(
|
||||
messageQueue: MessageQueue,
|
||||
callback: (message: ScriptEnableCallbackValue) => void
|
||||
) {
|
||||
return messageQueue.subscribe("enableScript", callback);
|
||||
}
|
||||
|
||||
export function subscribeScriptRunStatus(
|
||||
messageQueue: MessageQueue,
|
||||
callback: (message: { uuid: string; runStatus: SCRIPT_RUN_STATUS }) => void
|
||||
) {
|
||||
return messageQueue.subscribe("scriptRunStatus", callback);
|
||||
}
|
@ -3,9 +3,17 @@ import { sendMessage } from "@Packages/message/client";
|
||||
import { WindowMessage } from "@Packages/message/window_message";
|
||||
|
||||
export function enableScript(msg: WindowMessage, data: ScriptRunResouce) {
|
||||
return sendMessage(msg, "enableScript", data);
|
||||
return sendMessage(msg, "sandbox/enableScript", data);
|
||||
}
|
||||
|
||||
export function disableScript(msg: WindowMessage, uuid: string) {
|
||||
return sendMessage(msg, "disableScript", uuid);
|
||||
return sendMessage(msg, "sandbox/disableScript", uuid);
|
||||
}
|
||||
|
||||
export function runScript(msg: WindowMessage, data: ScriptRunResouce) {
|
||||
return sendMessage(msg, "sandbox/runScript", data);
|
||||
}
|
||||
|
||||
export function stopScript(msg: WindowMessage, uuid: string) {
|
||||
return sendMessage(msg, "sandbox/stopScript", uuid);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import { Runtime } from "./runtime";
|
||||
|
||||
// sandbox环境的管理器
|
||||
export class SandboxManager {
|
||||
api: Server = new Server(this.windowMessage);
|
||||
api: Server = new Server("sandbox", this.windowMessage);
|
||||
|
||||
constructor(private windowMessage: WindowMessage) {}
|
||||
|
||||
|
@ -269,18 +269,32 @@ export class Runtime {
|
||||
}
|
||||
}
|
||||
|
||||
stopScript(uuid: string) {
|
||||
async stopScript(uuid: string) {
|
||||
const exec = this.execScripts.get(uuid);
|
||||
if (!exec) {
|
||||
proxyUpdateRunStatus(this.windowMessage, { uuid: uuid, runStatus: SCRIPT_RUN_STATUS_COMPLETE });
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
exec.stop();
|
||||
this.execScripts.delete(uuid);
|
||||
proxyUpdateRunStatus(this.windowMessage, { uuid: uuid, runStatus: SCRIPT_RUN_STATUS_COMPLETE });
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
async runScript(script: ScriptRunResouce) {
|
||||
console.log("runScript", script);
|
||||
const exec = this.execScripts.get(script.uuid);
|
||||
// 如果正在运行,先释放
|
||||
if (exec) {
|
||||
await this.stopScript(script.uuid);
|
||||
}
|
||||
return this.execScript(script, true);
|
||||
}
|
||||
|
||||
init() {
|
||||
this.api.on("enableScript", this.enableScript.bind(this));
|
||||
this.api.on("disableScript", this.disableScript.bind(this));
|
||||
this.api.on("runScript", this.runScript.bind(this));
|
||||
this.api.on("stopScript", this.stopScript.bind(this));
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,12 @@
|
||||
import { Script, ScriptCode, ScriptRunResouce } from "@App/app/repo/scripts";
|
||||
import { Client } from "@Packages/message/client";
|
||||
import { InstallSource } from ".";
|
||||
import { Broker } from "@Packages/message/message_queue";
|
||||
import { Resource } from "@App/app/repo/resource";
|
||||
import { MessageSend } from "@Packages/message/server";
|
||||
|
||||
export class ServiceWorkerClient extends Client {
|
||||
constructor(msg: MessageSend) {
|
||||
super(msg);
|
||||
super(msg, "serviceWorker");
|
||||
}
|
||||
|
||||
preparationOffscreen() {
|
||||
@ -17,7 +16,7 @@ export class ServiceWorkerClient extends Client {
|
||||
|
||||
export class ScriptClient extends Client {
|
||||
constructor(msg: MessageSend) {
|
||||
super(msg, "script");
|
||||
super(msg, "serviceWorker/script");
|
||||
}
|
||||
|
||||
// 获取安装信息
|
||||
@ -52,7 +51,7 @@ export class ScriptClient extends Client {
|
||||
|
||||
export class ResourceClient extends Client {
|
||||
constructor(msg: MessageSend) {
|
||||
super(msg, "resource");
|
||||
super(msg, "serviceWorker/resource");
|
||||
}
|
||||
|
||||
getScriptResources(script: Script): Promise<{ [key: string]: Resource }> {
|
||||
@ -62,7 +61,7 @@ export class ResourceClient extends Client {
|
||||
|
||||
export class ValueClient extends Client {
|
||||
constructor(msg: MessageSend) {
|
||||
super(msg, "value");
|
||||
super(msg, "serviceWorker/value");
|
||||
}
|
||||
|
||||
getScriptValue(script: Script) {
|
||||
@ -70,19 +69,16 @@ export class ValueClient extends Client {
|
||||
}
|
||||
}
|
||||
|
||||
export function subscribeScriptInstall(
|
||||
border: Broker,
|
||||
callback: (message: { script: Script; update: boolean }) => void
|
||||
) {
|
||||
return border.subscribe("installScript", callback);
|
||||
}
|
||||
export class RuntimeClient extends Client {
|
||||
constructor(msg: MessageSend) {
|
||||
super(msg, "serviceWorker/runtime");
|
||||
}
|
||||
|
||||
export function subscribeScriptDelete(border: Broker, callback: (message: { uuid: string }) => void) {
|
||||
return border.subscribe("deleteScript", callback);
|
||||
}
|
||||
runScript(uuid: string) {
|
||||
return this.do("runScript", uuid);
|
||||
}
|
||||
|
||||
export type ScriptEnableCallbackValue = { uuid: string; enable: boolean };
|
||||
|
||||
export function subscribeScriptEnable(border: Broker, callback: (message: ScriptEnableCallbackValue) => void) {
|
||||
return border.subscribe("enableScript", callback);
|
||||
stopScript(uuid: string) {
|
||||
return this.do("stopScript", uuid);
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ export default class GMApi {
|
||||
this.logger.trace("GM API request", { api: data.api, uuid: data.uuid, param: data.params });
|
||||
const api = PermissionVerify.apis.get(data.api);
|
||||
if (!api) {
|
||||
return Promise.reject(new Error("api is not found"));
|
||||
return Promise.reject(new Error("gm api is not found"));
|
||||
}
|
||||
const req = await this.parseRequest(data, { tabId: 0 });
|
||||
try {
|
||||
|
@ -25,11 +25,11 @@ export default class ServiceWorkerManager {
|
||||
|
||||
const resource = new ResourceService(this.api.group("resource"), this.mq);
|
||||
resource.init();
|
||||
const value = new ValueService(this.api.group("value"), this.mq);
|
||||
const value = new ValueService(this.api.group("value"));
|
||||
value.init();
|
||||
const script = new ScriptService(this.api.group("script"), this.mq, value, resource);
|
||||
script.init();
|
||||
const runtime = new RuntimeService(this.api.group("runtime"), this.sender, this.mq, value);
|
||||
const runtime = new RuntimeService(this.api.group("runtime"), this.sender, this.mq, value, script);
|
||||
runtime.init();
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
import { MessageQueue } from "@Packages/message/message_queue";
|
||||
import { ScriptEnableCallbackValue } from "./client";
|
||||
import { Group, MessageSend } from "@Packages/message/server";
|
||||
import { Script, SCRIPT_STATUS_ENABLE, SCRIPT_TYPE_NORMAL, ScriptAndCode, ScriptDAO } from "@App/app/repo/scripts";
|
||||
import { ValueService } from "./value";
|
||||
import GMApi from "./gm_api";
|
||||
import { subscribeScriptEnable } from "../queue";
|
||||
import { ScriptService } from "./script";
|
||||
import { runScript, stopScript } from "../offscreen/client";
|
||||
|
||||
export class RuntimeService {
|
||||
scriptDAO: ScriptDAO = new ScriptDAO();
|
||||
@ -12,12 +14,13 @@ export class RuntimeService {
|
||||
private group: Group,
|
||||
private sender: MessageSend,
|
||||
private mq: MessageQueue,
|
||||
private value: ValueService
|
||||
private value: ValueService,
|
||||
private script: ScriptService
|
||||
) {}
|
||||
|
||||
async init() {
|
||||
// 监听脚本开启
|
||||
this.mq.addListener("enableScript", async (data: ScriptEnableCallbackValue) => {
|
||||
subscribeScriptEnable(this.mq, async (data) => {
|
||||
const script = await this.scriptDAO.getAndCode(data.uuid);
|
||||
if (!script) {
|
||||
return;
|
||||
@ -44,7 +47,7 @@ export class RuntimeService {
|
||||
this.mq.publish("enableScript", { uuid: script.uuid, enable: true });
|
||||
});
|
||||
// 监听offscreen环境初始化, 初始化完成后, 再将后台脚本运行起来
|
||||
this.mq.addListener("preparationOffscreen", () => {
|
||||
this.mq.subscribe("preparationOffscreen", () => {
|
||||
list.forEach((script) => {
|
||||
if (script.status !== SCRIPT_STATUS_ENABLE || script.type === SCRIPT_TYPE_NORMAL) {
|
||||
return;
|
||||
@ -56,6 +59,24 @@ export class RuntimeService {
|
||||
// 启动gm api
|
||||
const gmApi = new GMApi(this.group, this.sender, this.value);
|
||||
gmApi.start();
|
||||
|
||||
this.group.on("stopScript", this.stopScript.bind(this));
|
||||
this.group.on("runScript", this.runScript.bind(this));
|
||||
}
|
||||
|
||||
// 停止脚本
|
||||
stopScript(uuid: string) {
|
||||
return stopScript(this.sender, uuid);
|
||||
}
|
||||
|
||||
// 运行脚本
|
||||
async runScript(uuid: string) {
|
||||
const script = await this.scriptDAO.get(uuid);
|
||||
if (!script) {
|
||||
return;
|
||||
}
|
||||
const res = await this.script.buildScriptRunResource(script);
|
||||
return runScript(this.sender, res);
|
||||
}
|
||||
|
||||
registryPageScript(script: ScriptAndCode) {
|
||||
|
@ -235,7 +235,6 @@ export class ScriptService {
|
||||
}
|
||||
|
||||
async updateRunStatus(params: { uuid: string; runStatus: SCRIPT_RUN_STATUS; error?: string; nextruntime?: number }) {
|
||||
this.mq.publish("updateRunStatus", params);
|
||||
if (
|
||||
(await this.scriptDAO.update(params.uuid, {
|
||||
runStatus: params.runStatus,
|
||||
@ -246,6 +245,7 @@ export class ScriptService {
|
||||
) {
|
||||
return Promise.reject("update error");
|
||||
}
|
||||
this.mq.publish("scriptRunStatus", params);
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ import Logger from "@App/app/logger/logger";
|
||||
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 {
|
||||
@ -11,10 +10,7 @@ export class ValueService {
|
||||
scriptDAO: ScriptDAO = new ScriptDAO();
|
||||
valueDAO: ValueDAO = new ValueDAO();
|
||||
|
||||
constructor(
|
||||
private group: Group,
|
||||
private mq: MessageQueue
|
||||
) {
|
||||
constructor(private group: Group) {
|
||||
this.logger = LoggerCore.logger().with({ service: "value" });
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user