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

11262
backend/pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

@ -8,6 +8,7 @@ const isStash =
const isShadowRocket = 'undefined' !== typeof $rocket; const isShadowRocket = 'undefined' !== typeof $rocket;
const isEgern = 'object' == typeof egern; const isEgern = 'object' == typeof egern;
const isLanceX = 'undefined' != typeof $native; const isLanceX = 'undefined' != typeof $native;
const isGUIforCores = typeof $Plugins !== 'undefined';
export class OpenAPI { export class OpenAPI {
constructor(name = 'untitled', debug = false) { constructor(name = 'untitled', debug = false) {
@ -79,6 +80,24 @@ export class OpenAPI {
this.cache = JSON.parse(this.node.fs.readFileSync(`${fpath}`)); 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 // store cache
@ -103,6 +122,18 @@ export class OpenAPI {
(err) => console.log(err), (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) { write(data, key) {
@ -118,6 +149,9 @@ export class OpenAPI {
if (isNode) { if (isNode) {
this.root[key] = data; this.root[key] = data;
} }
if (isGUIforCores) {
this.root[key] = data;
}
} else { } else {
this.cache[key] = data; this.cache[key] = data;
} }
@ -137,6 +171,9 @@ export class OpenAPI {
if (isNode) { if (isNode) {
return this.root[key]; return this.root[key];
} }
if (isGUIforCores) {
return this.root[key];
}
} else { } else {
return this.cache[key]; return this.cache[key];
} }
@ -155,6 +192,9 @@ export class OpenAPI {
if (isNode) { if (isNode) {
delete this.root[key]; delete this.root[key];
} }
if (isGUIforCores) {
delete this.root[key];
}
} else { } else {
delete this.cache[key]; delete this.cache[key];
} }
@ -220,6 +260,9 @@ export class OpenAPI {
}); });
} }
} }
if (isGUIforCores) {
$Plugins.Notify(title, subtitle + '\n' + content);
}
} }
// other helper functions // other helper functions
@ -240,7 +283,7 @@ export class OpenAPI {
} }
done(value = {}) { done(value = {}) {
if (isQX || isLoon || isSurge) { if (isQX || isLoon || isSurge || isGUIforCores) {
$done(value); $done(value);
} else if (isNode) { } else if (isNode) {
if (typeof $context !== 'undefined') { if (typeof $context !== 'undefined') {
@ -262,11 +305,12 @@ export function ENV() {
isShadowRocket, isShadowRocket,
isEgern, isEgern,
isLanceX, isLanceX,
isGUIforCores,
}; };
} }
export function HTTP(defaultOptions = { baseURL: '' }) { export function HTTP(defaultOptions = { baseURL: '' }) {
const { isQX, isLoon, isSurge, isNode } = ENV(); const { isQX, isLoon, isSurge, isNode, isGUIforCores } = ENV();
const methods = [ const methods = [
'GET', 'GET',
'POST', '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; let timeoutid;