-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: multiple proxy settting support #3228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,8 +89,12 @@ export class Config { | |
| return Config.getSettings<boolean>('useWebExt') || false; | ||
| } | ||
|
|
||
| public static get getProxy(): IProxy { | ||
| return Config.getSettings<IProxy>('proxy'); | ||
| public static get getProxy(): IProxy[] { | ||
| const val = Config.getSettings<IProxy | IProxy[]>('proxy'); | ||
| if (!val) { | ||
| return []; | ||
| } | ||
| return Array.isArray(val) ? val : [val]; | ||
|
Comment on lines
+92
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [VALIDATION] getProxy now returns IProxy[] and wraps single-object values into an array which is good for backward compatibility. However, you're trusting the value from configuration without validating its shape. Add a runtime guard that validates each array element (ensure enable is boolean, baseUri and proxyUri are strings and proxyUri matches the expected protocol pattern) before returning it. This prevents later code from throwing if users set an invalid custom config. public static get getProxy(): IProxy[] {
const raw = Config.getSettings<unknown>('proxy');
if (!raw) {
return [];
}
const toArray = Array.isArray(raw) ? raw : [raw];
const proxies: IProxy[] = [];
for (const item of toArray) {
if (!item || typeof item !== 'object') {
continue;
}
const { enable, baseUri, proxyUri } = item as Partial<IProxy>;
if (typeof enable !== 'boolean') {
continue;
}
if (typeof baseUri !== 'string') {
continue;
}
if (typeof proxyUri !== 'string') {
continue;
}
if (!/^https?:\/\/.+$/i.test(proxyUri)) {
continue;
}
proxies.push({ enable, baseUri, proxyUri } as IProxy);
}
return proxies;
} |
||
| } | ||
|
GHLandy marked this conversation as resolved.
|
||
|
|
||
| public static get getHttps(): IHttps { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,15 +144,28 @@ export class Helper { | |
| } | ||
|
|
||
| static getProxySetup() { | ||
| const proxySetup = Config.getProxy; | ||
| let proxy = [[]]; | ||
| if (proxySetup.enable === true) { | ||
| proxy[0].push(proxySetup.baseUri, proxySetup.proxyUri); | ||
| let proxySetup = Config.getProxy; | ||
|
|
||
| // Handle missing/undefined config | ||
| if (!proxySetup) { | ||
| return null; | ||
| } | ||
| else { | ||
| proxy = null; // required to change the type [[]] to black array []. | ||
|
|
||
| // Backward compatibility: old single-object config | ||
| if (!Array.isArray(proxySetup)) { | ||
| proxySetup = [proxySetup]; | ||
| } | ||
|
|
||
| const validProxy = proxySetup.filter(item => item && typeof item === 'object' && item.enable); | ||
|
|
||
| if (!validProxy.length) { | ||
| return null; | ||
| } | ||
|
|
||
| const proxy = validProxy.map(item => { | ||
| return [item.baseUri, item.proxyUri]; | ||
| }); | ||
|
|
||
|
GHLandy marked this conversation as resolved.
|
||
| return proxy; | ||
|
Comment on lines
146
to
169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [REFACTORING] This compatibility logic can be simplified and made safer: Config.getProxy already returns an array (empty if missing). Remove the redundant static getProxySetup() {
const proxies = Config.getProxy; // always an array
const validProxy = proxies
.filter((item) => item && typeof item === 'object' && item.enable)
.map((item) => {
let baseUri = item.baseUri || '/';
if (!baseUri.startsWith('/')) {
baseUri = `/${baseUri}`;
}
// Basic normalization: trim spaces
const proxyUri = (item.proxyUri || '').trim();
return { ...item, baseUri, proxyUri };
});
if (!validProxy.length) {
return null;
}
// Optional: dedupe by baseUri, keep first occurrence
const seen = new Set<string>();
const deduped = validProxy.filter((item) => {
if (seen.has(item.baseUri)) {
return false;
}
seen.add(item.baseUri);
return true;
});
return deduped.map((item) => [item.baseUri, item.proxyUri]);
} |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[NITPICK] You changed the settings schema from an object to an array (breaking). Update any user-facing documentation and examples to reflect the new array form (docs/settings.md still shows the old single-object example). Also add a clear migration note in README/CHANGELOG describing the breaking change and example of the new shape so users know how to update their settings.
After (multiple proxy objects):
Existing users must wrap their previous single proxy object in an array to avoid breakage.
Also update
docs/settings.mdto show the new array format underliveServer.settings.proxy.