Adapted for GUI.for.Cores

This commit is contained in:
onejibang 2024-05-11 17:32:01 +08:00
parent d073dfeef8
commit cf35afcab2
4 changed files with 6214 additions and 5131 deletions

11102
backend/pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@ const {
isShadowRocket,
isLanceX,
isEgern,
isGUIforCores,
} = ENV();
let backend = 'Node';
if (isNode) backend = 'Node';
@ -20,6 +21,7 @@ if (isStash) backend = 'Stash';
if (isShadowRocket) backend = 'ShadowRocket';
if (isEgern) backend = 'Egern';
if (isLanceX) backend = 'LanceX';
if (isGUIforCores) backend = 'GUI.for.Cores';
let meta = {};
@ -51,6 +53,9 @@ try {
}
}
}
if (isGUIforCores) {
meta.plugin = Plugin;
}
// eslint-disable-next-line no-empty
} catch (e) {}

View File

@ -54,6 +54,7 @@ export default function express({ substore: $, port, host }) {
// dispatch url to route
const dispatch = (request, start = 0) => {
let { method, url, headers, body } = request;
console.log(url, method, headers, body);
headers = formatHeaders(headers);
if (/json/i.test(headers['content-type'])) {
body = JSON.parse(body);
@ -161,7 +162,7 @@ export default function express({ substore: $, port, host }) {
function Response() {
let statusCode = 200;
const { isQX, isLoon, isSurge } = ENV();
const { isQX, isLoon, isSurge, isGUIforCores } = ENV();
const headers = DEFAULT_HEADERS;
const STATUS_CODE_MAP = {
200: 'HTTP/1.1 200 OK',
@ -184,7 +185,7 @@ export default function express({ substore: $, port, host }) {
body,
headers,
};
if (isQX) {
if (isQX || isGUIforCores) {
$done(response);
} else if (isLoon || isSurge) {
$done({

View File

@ -8,6 +8,7 @@ const isStash =
const isShadowRocket = 'undefined' !== typeof $rocket;
const isEgern = 'object' == typeof egern;
const isLanceX = 'undefined' != typeof $native;
const isGUIforCores = typeof $Plugins !== 'undefined';
export class OpenAPI {
constructor(name = 'untitled', debug = false) {
@ -79,6 +80,24 @@ export class OpenAPI {
this.cache = JSON.parse(this.node.fs.readFileSync(`${fpath}`));
}
}
if (isGUIforCores) {
const basePath = 'data/third/sub-store-v3';
this.cache = {};
this.root = {};
$Plugins
.ignoredError($Plugins.Readfile, `${basePath}/root.json`)
.then((text) => {
this.root = JSON.parse(text || '{}');
});
$Plugins
.ignoredError(
$Plugins.Readfile,
`${basePath}/${this.name}.json`,
)
.then((text) => {
this.cache = JSON.parse(text || '{}');
});
}
}
// store cache
@ -103,6 +122,18 @@ export class OpenAPI {
(err) => console.log(err),
);
}
if (isGUIforCores) {
const basePath = 'data/third/sub-store-v3';
$Plugins
.Writefile(`${basePath}/${this.name}.json`, data)
.catch((err) => console.log(err));
$Plugins
.Writefile(
`${basePath}/root.json`,
JSON.stringify(this.root, null, 2),
)
.catch((err) => console.log(err));
}
}
write(data, key) {
@ -118,6 +149,9 @@ export class OpenAPI {
if (isNode) {
this.root[key] = data;
}
if (isGUIforCores) {
this.root[key] = data;
}
} else {
this.cache[key] = data;
}
@ -137,6 +171,9 @@ export class OpenAPI {
if (isNode) {
return this.root[key];
}
if (isGUIforCores) {
return this.root[key];
}
} else {
return this.cache[key];
}
@ -155,6 +192,9 @@ export class OpenAPI {
if (isNode) {
delete this.root[key];
}
if (isGUIforCores) {
delete this.root[key];
}
} else {
delete this.cache[key];
}
@ -220,6 +260,9 @@ export class OpenAPI {
});
}
}
if (isGUIforCores) {
$Plugins.Notify(title, subtitle + '\n' + content);
}
}
// other helper functions
@ -240,7 +283,7 @@ export class OpenAPI {
}
done(value = {}) {
if (isQX || isLoon || isSurge) {
if (isQX || isLoon || isSurge || isGUIforCores) {
$done(value);
} else if (isNode) {
if (typeof $context !== 'undefined') {
@ -262,11 +305,12 @@ export function ENV() {
isShadowRocket,
isEgern,
isLanceX,
isGUIforCores,
};
}
export function HTTP(defaultOptions = { baseURL: '' }) {
const { isQX, isLoon, isSurge, isNode } = ENV();
const { isQX, isLoon, isSurge, isNode, isGUIforCores } = ENV();
const methods = [
'GET',
'POST',
@ -356,6 +400,31 @@ export function HTTP(defaultOptions = { baseURL: '' }) {
});
});
});
} else if (isGUIforCores) {
worker = new Promise(async (resolve, reject) => {
const requestHandler = {
get: $Plugins.HttpGet,
post: $Plugins.HttpPost,
put: $Plugins.HttpPut,
delete: $Plugins.HttpDelete,
};
const request = requestHandler[method.toLowerCase()];
if (!request) reject('GUI.for.Cores未实现当前方法' + method);
const body = ['put', 'post'].includes(method.toLowerCase())
? options.body
: {};
try {
const { url, headers } = options;
const response = await request(url, headers, body);
resolve({
statusCode: response.status ?? 200,
headers: response.header,
body: response.body,
});
} catch (error) {
reject(error);
}
});
}
let timeoutid;