proxied fetch support

This commit is contained in:
josc146
2024-03-26 21:23:09 +08:00
parent c2799c9494
commit 08bc342fd6
6 changed files with 96 additions and 0 deletions

View File

@@ -63,6 +63,7 @@ class CommonStore {
monitorData: MonitorData | null = null;
depComplete: boolean = false;
platform: Platform = 'windows';
proxyPort: number = 0;
lastModelName: string = '';
// presets manager
editingPreset: Preset | null = null;
@@ -323,6 +324,10 @@ class CommonStore {
this.platform = value;
}
setProxyPort(value: number) {
this.proxyPort = value;
}
setCurrentInput(value: string) {
this.currentInput = value;
}

View File

@@ -310,6 +310,24 @@ export function bytesToReadable(size: number) {
else return bytesToGb(size) + ' GB';
}
export async function getReqUrl(port: number, path: string, isCore: boolean = false): Promise<{
url: string,
headers: { [key: string]: string }
}> {
const realUrl = getServerRoot(port, isCore) + path;
if (commonStore.platform === 'web')
return {
url: realUrl,
headers: {}
};
if (!commonStore.proxyPort)
await GetProxyPort().then(p => commonStore.setProxyPort(p));
return {
url: `http://127.0.0.1:${commonStore.proxyPort}`,
headers: { 'Real-Target': realUrl }
};
}
export function getServerRoot(defaultLocalPort: number, isCore: boolean = false) {
const coreCustomApiUrl = commonStore.settings.coreApiUrl.trim().replace(/\/$/, '');
if (isCore && coreCustomApiUrl)

View File

@@ -115,6 +115,12 @@ if (!window.go) {
defineApp('ListDirFiles', async () => {
return []
})
defineApp('GetAbsPath', async (path) => {
return path
})
defineApp('GetProxyPort', async () => {
return 0
})
defineApp('OpenOpenFileDialog', webOpenOpenFileDialog)
defineApp('OpenSaveFileDialog', async (filterPattern, defaultFileName, savedContent) => {
const saver = await import('file-saver')