UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA Functions and Pages.

124 lines (123 loc) 4.83 kB
/* 操作符号枚举 */ export var OPERATOR_ENUM; (function (OPERATOR_ENUM) { OPERATOR_ENUM["Eq"] = "eq"; OPERATOR_ENUM["Ne"] = "ne"; OPERATOR_ENUM["Contains"] = "contains"; OPERATOR_ENUM["Negate_Contains"] = "negate_contains"; OPERATOR_ENUM["StartsWith"] = "starts_with"; OPERATOR_ENUM["Negate_StartsWith"] = "negate_starts_with"; OPERATOR_ENUM["EndsWith"] = "ends_with"; OPERATOR_ENUM["Negate_EndsWith"] = "negate_ends_with"; OPERATOR_ENUM["Matches"] = "matches"; OPERATOR_ENUM["Negate_Matches"] = "negate_matches"; OPERATOR_ENUM["In"] = "in"; OPERATOR_ENUM["Negate_In"] = "negate_in"; OPERATOR_ENUM["Gt"] = "gt"; OPERATOR_ENUM["Lt"] = "lt"; OPERATOR_ENUM["Ge"] = "ge"; OPERATOR_ENUM["Le"] = "le"; OPERATOR_ENUM["InList"] = "in_list"; OPERATOR_ENUM["Negate_InList"] = "negate_in_list"; })(OPERATOR_ENUM || (OPERATOR_ENUM = {})); const RuleMatchTypeHost = 'http.host'; const RuleMatchTypeUriPath = 'http.request.uri.path'; const RuleMatchOperatorEq = OPERATOR_ENUM.Eq; const RuleMatchOperatorStartsWith = OPERATOR_ENUM.StartsWith; const RuleMatchOperatorEndsWith = OPERATOR_ENUM.EndsWith; export const transferRouteToRuleString = (routePath) => { if (!routePath) { return ''; } const index = routePath.indexOf('/'); let host = ''; let uriPath = ''; if (index < 0) { host = routePath; uriPath = '/'; } else { host = routePath.substring(0, index); uriPath = routePath.substring(index); } let hostOperator = RuleMatchOperatorEq; if (host.startsWith('*')) { hostOperator = RuleMatchOperatorEndsWith; host = host.replace(/\*/g, ''); } let uriPathOperator = RuleMatchOperatorEq; if (uriPath.endsWith('*')) { uriPathOperator = RuleMatchOperatorStartsWith; uriPath = uriPath.replace(/\*$/, ''); } let ruleStr = ''; if (hostOperator === RuleMatchOperatorEq) { if (uriPathOperator === RuleMatchOperatorEq) { ruleStr = `(${RuleMatchTypeHost} ${hostOperator} "${host}" and ${RuleMatchTypeUriPath} ${uriPathOperator} "${uriPath}")`; } else if (uriPathOperator === RuleMatchOperatorStartsWith) { ruleStr = `(${RuleMatchTypeHost} ${hostOperator} "${host}" and ${RuleMatchOperatorStartsWith}(${RuleMatchTypeUriPath}, "${uriPath}"))`; } } else if (hostOperator === RuleMatchOperatorEndsWith) { if (uriPathOperator === RuleMatchOperatorEq) { ruleStr = `(${RuleMatchOperatorEndsWith}(${RuleMatchTypeHost}, "${host}") and ${RuleMatchTypeUriPath} ${uriPathOperator} "${uriPath}")`; } else if (uriPathOperator === RuleMatchOperatorStartsWith) { ruleStr = `(${RuleMatchOperatorEndsWith}(${RuleMatchTypeHost}, "${host}") and ${RuleMatchOperatorStartsWith}(${RuleMatchTypeUriPath}, "${uriPath}"))`; } } return ruleStr; }; export const transferRuleStringToRoute = (ruleStr) => { if (!ruleStr) { return ''; } // Remove outer brackets and split by " and " const cleanedRule = ruleStr.replace(/^\(|\)$/g, ''); const parts = cleanedRule.split(' and '); if (parts.length !== 2) { return ''; } let host = ''; let uriPath = ''; // Process host part const hostPart = parts[0].trim(); if (hostPart.startsWith(`${RuleMatchOperatorEndsWith}(${RuleMatchTypeHost},`)) { // Logic when host matches eq // ends_with(http.host, "value") const match = hostPart.match(/ends_with\(http\.host,\s*"([^"]+)"\)/); if (match) { host = `*${match[1]}`; // Add prefix * } } else if (hostPart.startsWith(`${RuleMatchTypeHost} ${RuleMatchOperatorEq}`)) { // Logic when host matches eq // http.host eq "value" const match = hostPart.match(/http\.host eq "([^"]+)"/); if (match) { host = match[1]; } } // Process uriPath part const uriPathPart = parts[1].trim(); if (uriPathPart.startsWith(`${RuleMatchOperatorStartsWith}(${RuleMatchTypeUriPath},`)) { // Logic when uriPath matches startsWith // starts_with(http.request.uri.path, "value") const match = uriPathPart.match(/starts_with\(http\.request\.uri\.path,\s*"([^"]+)"\)/); if (match) { uriPath = `${match[1]}*`; // Add suffix * } } else if (uriPathPart.startsWith(`${RuleMatchTypeUriPath} ${RuleMatchOperatorEq}`)) { // Logic when uriPath matches eq // http.request.uri.path eq "value" const match = uriPathPart.match(/http\.request\.uri\.path eq "([^"]+)"/); if (match) { uriPath = match[1]; } } if (!host || !uriPath) { return ''; } return `${host}${uriPath}`; };