Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/src/utils/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
isShadowRocket,
isLanceX,
isEgern,
isGUIforCores,
} = ENV();
let backend = 'Node';
if (isNode) backend = 'Node';
Expand All @@ -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 = {};

Expand All @@ -36,6 +38,10 @@ try {
// eslint-disable-next-line no-undef
meta.script = $script;
}
if (typeof $Plugin !== 'undefined') {
// eslint-disable-next-line no-undef
meta.plugin = $Plugin;
}
if (isNode) {
meta.node = {
version: eval('process.version'),
Expand Down
4 changes: 2 additions & 2 deletions backend/src/vendor/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,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',
Expand All @@ -184,7 +184,7 @@ export default function express({ substore: $, port, host }) {
body,
headers,
};
if (isQX) {
if (isQX || isGUIforCores) {
$done(response);
} else if (isLoon || isSurge) {
$done({
Expand Down
51 changes: 48 additions & 3 deletions backend/src/vendor/open-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -48,7 +49,10 @@ export class OpenAPI {
this.cache = JSON.parse($prefs.valueForKey(this.name) || '{}');
if (isLoon || isSurge)
this.cache = JSON.parse($persistentStore.read(this.name) || '{}');

if (isGUIforCores)
this.cache = JSON.parse(
$Plugins.SubStoreCache.get(this.name) || '{}',
);
if (isNode) {
// create a json for root cache
const basePath =
Expand Down Expand Up @@ -86,6 +90,7 @@ export class OpenAPI {
const data = JSON.stringify(this.cache, null, 2);
if (isQX) $prefs.setValueForKey(data, this.name);
if (isLoon || isSurge) $persistentStore.write(data, this.name);
if (isGUIforCores) $Plugins.SubStoreCache.set(this.name, data);
if (isNode) {
const basePath =
eval('process.env.SUB_STORE_DATA_BASE_PATH') || '.';
Expand Down Expand Up @@ -118,6 +123,9 @@ export class OpenAPI {
if (isNode) {
this.root[key] = data;
}
if (isGUIforCores) {
return $Plugins.SubStoreCache.set(key, data);
}
} else {
this.cache[key] = data;
}
Expand All @@ -137,6 +145,9 @@ export class OpenAPI {
if (isNode) {
return this.root[key];
}
if (isGUIforCores) {
return $Plugins.SubStoreCache.get(key);
}
} else {
return this.cache[key];
}
Expand All @@ -155,6 +166,9 @@ export class OpenAPI {
if (isNode) {
delete this.root[key];
}
if (isGUIforCores) {
return $Plugins.SubStoreCache.remove(key);
}
} else {
delete this.cache[key];
}
Expand Down Expand Up @@ -220,6 +234,9 @@ export class OpenAPI {
});
}
}
if (isGUIforCores) {
$Plugins.Notify(title, subtitle + '\n' + content);
}
}

// other helper functions
Expand All @@ -240,7 +257,7 @@ export class OpenAPI {
}

done(value = {}) {
if (isQX || isLoon || isSurge) {
if (isQX || isLoon || isSurge || isGUIforCores) {
$done(value);
} else if (isNode) {
if (typeof $context !== 'undefined') {
Expand All @@ -262,11 +279,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',
Expand Down Expand Up @@ -356,6 +374,33 @@ export function HTTP(defaultOptions = { baseURL: '' }) {
});
});
});
} else if (isGUIforCores) {
worker = new Promise(async (resolve, reject) => {
const requestHandler = {
get: $Plugins.HttpGet,
head: $Plugins.HttpHead,
post: $Plugins.HttpPost,
put: $Plugins.HttpPut,
delete: $Plugins.HttpDelete,
};
const request = requestHandler[method.toLowerCase()];
if (!request)
reject(
'[GUI.for.Cores] This method is not implemented: ' +
method,
);
try {
const { url, headers } = options;
const response = await request(url, headers, options.body);
resolve({
statusCode: response.status ?? 200,
headers: response.header,
body: response.body,
});
} catch (error) {
reject(error);
}
});
}

let timeoutid;
Expand Down