Skip to content

Commit

Permalink
fix: surge script reformat incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Jun 13, 2020
1 parent 11da36c commit 1f14408
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/generator/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ test('convertNewSurgeScriptRuleToQuantumultXRewriteRule', t => {
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=http-request,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'),
'https://api.zhihu.com/people/ url script-request-header https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('JD = requires-body=1,max-size=0,script-path= https://raw.githubusercontent.com/NobyDa/Script/master/JD-DailyBonus/JD_DailyBonus.js,type=http-response,pattern=^https?://api\.m\.jd\.com/client\.action\?functionId=(start|signBean)'),
'^https?://api\.m\.jd\.com/client\.action\?functionId=(start|signBean) url script-response-body https://raw.githubusercontent.com/NobyDa/Script/master/JD-DailyBonus/JD_DailyBonus.js'
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=unknown-type'),
''
Expand Down
4 changes: 1 addition & 3 deletions lib/generator/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const convertSurgeScriptRuleToQuantumultXRewriteRule = (str: string): str
};

export const convertNewSurgeScriptRuleToQuantumultXRewriteRule = (str: string): string => {
const matched = str.match(/([\w\s]+)=(.+)/);
const matched = str.match(/^(.+?)=(.+?)$/);
const result: string[] = [];

if (!matched) {
Expand All @@ -178,7 +178,6 @@ export const convertNewSurgeScriptRuleToQuantumultXRewriteRule = (str: string):
const isRequireBody = 'requires-body' in params;

if (isRequireBody) {
// parts[1] => Effective URL Rule
result.push(params.pattern as string, 'url', 'script-response-body', params['script-path'] as string);
} else {
result.push(params.pattern as string, 'url', 'script-response-header', params['script-path'] as string);
Expand All @@ -190,7 +189,6 @@ export const convertNewSurgeScriptRuleToQuantumultXRewriteRule = (str: string):
const isRequireBody = 'requires-body' in params;

if (isRequireBody) {
// parts[1] => Effective URL Rule
result.push(params.pattern as string, 'url', 'script-request-body', params['script-path'] as string);
} else {
result.push(params.pattern as string, 'url', 'script-request-header', params['script-path'] as string);
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,14 @@ export const pickAndFormatStringList = (obj: object, keyList: readonly string[])
export const decodeStringList = <T = Record<string, string|boolean>>(stringList: ReadonlyArray<string>): T => {
const result = {};
stringList.forEach(item => {
const pair = item.split('=');
result[pair[0]] = pair[1] || true;
if (item.includes('=')) {
const match = item.match(/^(.*?)=(.*?)$/);
if (match) {
result[match[1].trim()] = match[2].trim() || true;
}
} else {
result[item.trim()] = true;
}
});
return result as T;
};
Expand Down

0 comments on commit 1f14408

Please sign in to comment.