UNPKG

tencentcloud-edgeone-migration-nodejs-v2

Version:

tencentcloud cdn config copy to edgeone

432 lines (423 loc) 11.1 kB
const utils = require("../utils"); const { t } = require("../../i18n/trans"); const genLog = require("../../logGenerator"); function Cache(domain, domainConfig, ruleTransferLog) { const keyConfig = domainConfig.Cache; if ( (!keyConfig || !keyConfig.RuleCache || !keyConfig.RuleCache.length) && (!keyConfig || !keyConfig?.SimpleCache) ) { ruleTransferLog.push({ config: t("节点缓存过期配置(Cache)"), result: t("未配置"), detail: "", }); genLog.defaultLog(`${t("节点缓存过期配置(Cache)")} ${t("未配置")}`); return null; } const { RuleCache } = keyConfig; const eoRules = []; if (keyConfig?.SimpleCache) { const { CacheRules, IgnoreCacheControl } = keyConfig?.SimpleCache; CacheRules.forEach((rule) => { let eoRule = {}; if ( rule.CacheType === "file" && rule.CacheContents.length && rule.CacheContents.some((item) => item.includes("$")) ) { const regxRulePath = rule.CacheContents.map((value) => value.replace("$", "") ).join("|"); eoRule = { Conditions: [ { Conditions: [ { Operator: "regular", Target: "url", IgnoreCase: false, Values: [`^[^?]*\\.(${regxRulePath})($)`], }, ], }, ], Actions: [], }; } else if (rule.CacheType === "all") { eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: "host", Values: ["all"], }, ], }, ], Actions: [], }; } else if (rule.CacheType === "regex") { eoRule = { Conditions: [], Actions: [], }; rule.CacheContents.forEach((path) => { eoRule.Conditions.push({ Conditions: [ { Operator: "regular", Target: "full_url", IgnoreCase: false, Values: [path], }, ], }); }); } else if (rule.CacheType === "directory") { const formatUrl = (rule) => { let curRule = []; rule.forEach((path) => { if (path.includes(",")) { curRule = curRule.concat(formatUrl(path.split(","))); } else { if (!path.endsWith("/*")) { const result = path.replace(/\/$/, ""); path = `${result}/*`; } curRule.push(path); } }); return curRule; }; eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: "url", IgnoreCase: false, Values: formatUrl(rule.CacheContents), }, ], }, ], Actions: [], }; } else if (rule.RuleType === "index") { eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: utils.getTarget(rule.RuleType), IgnoreCase: false, Values: ["/"], }, ], }, ], Actions: [], }; } else { eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: utils.getTarget(rule.CacheType), IgnoreCase: false, Values: rule.CacheType === "path" ? rule.CacheContents.map((path) => { return `https://${domain}${path}`; }) : rule.CacheContents, }, ], }, ], Actions: [], }; } eoRule.Actions.push({ NormalAction: { Action: "Cache", Parameters: [ { Name: "Type", Values: ["Cache"], }, { Name: "Switch", Values: ["on"], }, { Name: "IgnoreCacheControl", Values: [IgnoreCacheControl], }, { Name: "CacheTime", Values: [String(rule.CacheTime)], }, ], }, }); eoRules.push(eoRule); }); ruleTransferLog.push({ config: t("节点缓存过期配置(Cache)"), result: t("成功"), detail: "", }); genLog.successLog(`${t("节点缓存过期配置(Cache)")} ${t("成功")}`); return { Rules: eoRules, Tags: [], }; } RuleCache.forEach((rule) => { let eoRule = {}; if ( rule.RuleType === "file" && rule.RulePaths.length && rule.RulePaths.some((item) => item.includes("$")) ) { const regxRulePath = rule.RulePaths.map((value) => value.replace("$", "") ).join("|"); eoRule = { Conditions: [ { Conditions: [ { Operator: "regular", Target: "full_url", IgnoreCase: false, Values: [`^https?:\/\/[^?]+\.(${regxRulePath})($)`], }, ], }, ], Actions: [], }; } else if (rule.RuleType === "all") { eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: "host", Values: ["all"], }, ], }, ], Actions: [], }; } else if (rule.RuleType === "regex") { eoRule = { Conditions: [], Actions: [], }; rule.RulePaths.forEach((path) => { eoRule.Conditions.push({ Conditions: [ { Operator: "regular", Target: "full_url", IgnoreCase: false, Values: [path], }, ], }); }); } else if (rule.RuleType === "directory") { const formatUrl = (rule) => { let curRule = []; rule.forEach((path) => { if (path.includes(",")) { curRule = curRule.concat(formatUrl(path.split(","))); } else { if (!path.endsWith("/*")) { const result = path.replace(/\/$/, ""); path = `${result}/*`; } curRule.push(path); } }); return curRule; }; eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: "url", IgnoreCase: false, Values: formatUrl(rule.RulePaths), }, ], }, ], Actions: [], }; } else if (rule.RuleType === "index") { eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: utils.getTarget(rule.RuleType), IgnoreCase: false, Values: ["/"], }, ], }, ], Actions: [], }; } else { eoRule = { Conditions: [ { Conditions: [ { Operator: "equal", Target: rule.RuleType === 'path' ? 'url' : utils.getTarget(rule.RuleType), IgnoreCase: false, Values: utils.genOriginValue(rule, true) }, ], }, ], Actions: [], }; } if (rule.CacheConfig.NoCache.Switch === "on") { eoRule.Actions.push({ NormalAction: { Action: "Cache", Parameters: [ { Name: "Type", Values: ["NoCache"], }, { Name: "Switch", Values: ["on"], }, ], }, }); } else if ( rule.CacheConfig.FollowOrigin.Switch === "on" && rule.CacheConfig.FollowOrigin.HeuristicCache.Switch === "on" ) { eoRule.Actions.push({ NormalAction: { Action: "Cache", Parameters: [ { Name: "Type", Values: ["FollowOrigin"], }, { Name: "Switch", Values: ["on"], }, { Name: "DefaultCache", Values: [rule.CacheConfig.FollowOrigin.HeuristicCache.Switch], }, { Name: "DefaultCacheStrategy", Values: [ rule.CacheConfig.FollowOrigin.HeuristicCache.CacheConfig .HeuristicCacheTimeSwitch === "on" ? "off" : "on", ], }, { Name: "DefaultCacheTime", Values: [ String( rule.CacheConfig.FollowOrigin.HeuristicCache.CacheConfig .HeuristicCacheTime ), ], }, ], }, }); } else if ( rule.CacheConfig.FollowOrigin.Switch === "on" && rule.CacheConfig.FollowOrigin.HeuristicCache.Switch === "off" ) { eoRule.Actions.push({ NormalAction: { Action: "Cache", Parameters: [ { Name: "Type", Values: ["FollowOrigin"], }, { Name: "Switch", Values: ["on"], }, { Name: "DefaultCache", Values: ["off"], }, ], }, }); } else { eoRule.Actions.push({ NormalAction: { Action: "Cache", Parameters: [ { Name: "Type", Values: ["Cache"], }, { Name: "Switch", Values: ["on"], }, { Name: "IgnoreCacheControl", Values: [rule.CacheConfig.Cache.IgnoreCacheControl], }, { Name: "CacheTime", Values: [String(rule.CacheConfig.Cache.CacheTime)], }, ], }, }); } eoRules.push(eoRule); }); ruleTransferLog.push({ config: t("节点缓存过期配置(Cache)"), result: t("成功"), detail: "", }); genLog.successLog(`${t("节点缓存过期配置(Cache)")} ${t("成功")}`); return { Rules: eoRules, Tags: [], }; } module.exports = Cache;