-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
441 additions
and
27 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,8 @@ | |
"name": "Gj", | ||
"email": "[email protected]" | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"@proxy/lib": "workspace:^1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { NewGLobalStateStruct, NewUploadStruct, OldGLobalStateStruct, OldInterceptorStruct, OldRedirectorStruct, OldUploadStruct } from "./types"; | ||
|
||
// 判断是否为老GlobalState 数据 | ||
function isOldGlobalState(x: any): x is OldGLobalStateStruct { | ||
return x && x.hasOwnProperty("globalSwitchOn") | ||
} | ||
|
||
// 判断是否为老上传数据结构 | ||
function isOldUploadState(x: any): x is OldUploadStruct { | ||
return x && | ||
( | ||
x.hasOwnProperty("proxy_routes") || | ||
x.hasOwnProperty("redirect") || | ||
x.hasOwnProperty("lang") | ||
) | ||
} | ||
|
||
// 判断是否为老 拦截列表 | ||
function isOldInterceptor(x: any): x is OldInterceptorStruct[] { | ||
if (!x) return false | ||
if (!Array.isArray(x)) return false | ||
for (let i = 0; i < x.length; i++) { | ||
const target = x[i]; | ||
if (!target.hasOwnProperty("match")) return false | ||
} | ||
return true | ||
} | ||
|
||
// 判断是否为老 重定向列表 | ||
function isOldRedirector(x: any): x is OldRedirectorStruct[] { | ||
if (!x) return false | ||
if (!Array.isArray(x)) return false | ||
for (let i = 0; i < x.length; i++) { | ||
const target = x[i]; | ||
if (!target.hasOwnProperty("redirect")) return false | ||
} | ||
return true | ||
} | ||
|
||
// 转义拦截列表 | ||
function interceptorConversion(target: any) { | ||
if (isOldInterceptor(target)) { | ||
const newInterceptor: NewGLobalStateStruct['interceptor_matching_content'] = target.map(interceptor => { | ||
const { | ||
id = "", switchOn = false, filterType = "normal", method = "ANY", remark = "", | ||
match = "", tagId = "", statusCode = "200", override = "", hit = 0 | ||
} = interceptor | ||
return { | ||
id, switch_on: switchOn, filter_type: filterType, method, remark, | ||
match_url: match, tagId, status_code: statusCode, override, hit | ||
} | ||
}) | ||
return newInterceptor | ||
} | ||
return target | ||
} | ||
|
||
// 转义重定向列表 | ||
function redirectorConversion(target: any) { | ||
if (isOldRedirector(target)) { | ||
const newInterceptor: NewGLobalStateStruct['redirector_matching_content'] = target.map(interceptor => { | ||
const { | ||
id = "", switchOn = false, filterType = "normal", method = "ANY", remark = "", | ||
domain = "", redirect = "", headers = [], whitelist = [] | ||
} = interceptor | ||
return { | ||
id, switch_on: switchOn, filter_type: filterType, method, remark, | ||
domain, redirect_url: redirect, headers, ignores: whitelist | ||
} | ||
}) | ||
return newInterceptor | ||
} | ||
return target | ||
} | ||
|
||
/** | ||
* 数据转换 | ||
* content-script 使用, 页面加载时 | ||
* 由于 v2.1.0 版本 项目重构,数据属性有变动。 | ||
* 需要将老数据预处理为新数据结构体 | ||
*/ | ||
export function onLoadForDataConversion(target: NewGLobalStateStruct | OldGLobalStateStruct) { | ||
if (isOldGlobalState(target)) { | ||
// 需要转换数据格式 | ||
const { | ||
globalSwitchOn, | ||
mode, | ||
proxy_routes, | ||
redirect | ||
} = target | ||
|
||
// 转义 拦截列表 | ||
const interceptors = interceptorConversion(proxy_routes) | ||
// 转义 重定向列表 | ||
const redirectors = redirectorConversion(redirect) | ||
const newData: NewGLobalStateStruct = { | ||
global_on: globalSwitchOn, | ||
mode, | ||
interceptor_matching_content: interceptors, | ||
redirector_matching_content: redirectors | ||
} | ||
return { | ||
changed: true, | ||
data: newData | ||
} | ||
} | ||
// 新数据结构,无需转换 | ||
return { | ||
changed: false, | ||
data: target | ||
} | ||
} | ||
|
||
/** | ||
* 数据转换 | ||
* panels 使用, 数据上传时 | ||
* 由于 v2.1.0 版本 项目重构,数据属性有变动。 | ||
* 需要将老数据预处理为新数据结构体 | ||
* 原数据格式: { lang, proxy_routes, tags, mode, redirect } | ||
* 转义为: { language, mode, tags, interceptors, redirectors } | ||
*/ | ||
export function onUploadForDataConversion(target: OldUploadStruct | NewUploadStruct) { | ||
if (isOldUploadState(target)) { | ||
const { | ||
lang = 'en', proxy_routes = [], redirect = [], mode = 'interceptor', tags = [] | ||
} = target | ||
|
||
// 转义 拦截列表 | ||
const interceptors = interceptorConversion(proxy_routes) | ||
// 转义 重定向列表 | ||
const redirectors = redirectorConversion(redirect) | ||
const newData: NewUploadStruct = { | ||
language: lang, | ||
mode, | ||
tags, | ||
interceptors, | ||
redirectors | ||
} | ||
return newData | ||
} | ||
// 新数据结构,无需转换 | ||
return target | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { | ||
IFilterType, | ||
IGlobalState, | ||
IMatchInterceptorContent, | ||
IMatchRedirectContent, | ||
IMode, | ||
IRedirectHeader, | ||
IRequestMethod | ||
} from "@proxy/lib/types/types"; | ||
|
||
/** | ||
* 标签结构体 | ||
* 新老数据没变化 | ||
* {id: "633f895c49960dd6iuyz3s3t", name: "asdf", used: false} | ||
* */ | ||
type TagStruct = { | ||
id: string | ||
name: string | ||
used: boolean | ||
} | ||
|
||
type OldCommonStruct = { | ||
id: string | ||
/**是否需要匹配 */ | ||
switchOn: boolean | ||
/**匹配规则 */ | ||
filterType: IFilterType | ||
/**请求协议 */ | ||
method?: IRequestMethod | ||
/**备注 */ | ||
remark?: string; | ||
} | ||
|
||
/**老数据- 拦截列表 */ | ||
export type OldInterceptorStruct = { | ||
/**匹配目标URL */ | ||
match: string | ||
/**标签id */ | ||
tagId?: TagStruct['id'] | ||
/**状态码 */ | ||
statusCode?: string; | ||
/**命中率 */ | ||
hit?: number; | ||
/**需要覆盖的内容 */ | ||
override?: string; | ||
} & OldCommonStruct | ||
|
||
/**老数据- 重定向列表 */ | ||
export type OldRedirectorStruct = { | ||
/**域名 */ | ||
domain: string | ||
/**重定向地址 */ | ||
redirect: string | ||
/**请求头 */ | ||
headers?: IRedirectHeader[]; | ||
/**忽略名单 */ | ||
whitelist?: string[]; | ||
} & OldCommonStruct | ||
|
||
/** | ||
* 老数据 GlobalState 结构体 | ||
*/ | ||
export type OldGLobalStateStruct = { | ||
/**全局开关 */ | ||
globalSwitchOn: boolean | ||
/**模式 */ | ||
mode: IMode | ||
/**拦截规则列表 */ | ||
proxy_routes?: OldInterceptorStruct[] | ||
/**重定向规则列表 */ | ||
redirect?: OldRedirectorStruct[] | ||
} | ||
|
||
/** | ||
* 新GlobalState数据结构体 | ||
*/ | ||
export type NewGLobalStateStruct = IGlobalState & { | ||
/**拦截规则列表 */ | ||
interceptor_matching_content?: ( | ||
IMatchInterceptorContent & { | ||
id: string | ||
tagId?: TagStruct['id'] | ||
} | ||
)[] | ||
redirector_matching_content?: ( | ||
IMatchRedirectContent & { | ||
id: string | ||
} | ||
)[] | ||
} | ||
|
||
/** | ||
* 上传 - 老数据格式 | ||
*/ | ||
export type OldUploadStruct = { | ||
/**语言 */ | ||
lang: string | ||
/**拦截规则列表 */ | ||
proxy_routes?: OldInterceptorStruct[] | ||
/**重定向规则列表 */ | ||
redirect?: OldRedirectorStruct[] | ||
/**模式 */ | ||
mode: IMode | ||
/**标签 */ | ||
tags?: TagStruct[] | ||
} | ||
|
||
/** | ||
* 上传 - 新数据格式 | ||
*/ | ||
export type NewUploadStruct = { | ||
language: string | ||
mode: IMode | ||
tags: TagStruct[] | ||
interceptors?: ( | ||
IMatchInterceptorContent & { | ||
id: string | ||
tagId?: TagStruct['id'] | ||
} | ||
)[] | ||
redirectors?: ( | ||
IMatchRedirectContent & { | ||
id: string | ||
} | ||
)[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NewGLobalStateStruct, NewUploadStruct, OldGLobalStateStruct, OldUploadStruct } from "./types"; | ||
/** | ||
* 数据转换 | ||
* content-script 使用, 页面加载时 | ||
* 由于 v2.1.0 版本 项目重构,数据属性有变动。 | ||
* 需要将老数据预处理为新数据结构体 | ||
*/ | ||
export declare function onLoadForDataConversion(target: NewGLobalStateStruct | OldGLobalStateStruct): { | ||
changed: boolean; | ||
data: NewGLobalStateStruct; | ||
}; | ||
/** | ||
* 数据转换 | ||
* panels 使用, 数据上传时 | ||
* 由于 v2.1.0 版本 项目重构,数据属性有变动。 | ||
* 需要将老数据预处理为新数据结构体 | ||
* 原数据格式: { lang, proxy_routes, tags, mode, redirect } | ||
* 转义为: { language, mode, tags, interceptors, redirectors } | ||
*/ | ||
export declare function onUploadForDataConversion(target: OldUploadStruct | NewUploadStruct): NewUploadStruct; |
Oops, something went wrong.