@webiny/aws-helpers 6.3.0-beta.4 → 6.4.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cloudfrontFunctions/cookies.js +15 -24
- package/cloudfrontFunctions/cookies.js.map +1 -1
- package/cloudfrontFunctions/headers.js +19 -20
- package/cloudfrontFunctions/headers.js.map +1 -1
- package/cloudfrontFunctions/index.js +0 -2
- package/cloudfrontFunctions/querystring.js +14 -24
- package/cloudfrontFunctions/querystring.js.map +1 -1
- package/cloudfrontFunctions/redirect.js +15 -17
- package/cloudfrontFunctions/redirect.js.map +1 -1
- package/cloudfrontFunctions/types.js +0 -3
- package/cloudfrontFunctions/utils.js +6 -19
- package/cloudfrontFunctions/utils.js.map +1 -1
- package/index.js +0 -2
- package/lambdaEdge/cookies.js +16 -17
- package/lambdaEdge/cookies.js.map +1 -1
- package/lambdaEdge/headers.js +7 -4
- package/lambdaEdge/headers.js.map +1 -1
- package/lambdaEdge/index.js +0 -2
- package/lambdaEdge/redirect.js +26 -26
- package/lambdaEdge/redirect.js.map +1 -1
- package/lambdaEdge/request.js +22 -17
- package/lambdaEdge/request.js.map +1 -1
- package/lambdaEdge/response.js +7 -6
- package/lambdaEdge/response.js.map +1 -1
- package/lambdaEdge/types.js +0 -3
- package/lambdaEdge/utils.js +5 -4
- package/lambdaEdge/utils.js.map +1 -1
- package/package.json +3 -3
- package/rslib-runtime.js +14 -0
- package/rslib-runtime.js.map +1 -0
- package/stagedRollouts/functions/adminOriginRequest.js +18 -22
- package/stagedRollouts/functions/adminOriginRequest.js.map +1 -1
- package/stagedRollouts/functions/configOriginRequest.js +44 -46
- package/stagedRollouts/functions/configOriginRequest.js.map +1 -1
- package/stagedRollouts/functions/originRequest.js +11 -10
- package/stagedRollouts/functions/originRequest.js.map +1 -1
- package/stagedRollouts/functions/viewerRequest.js +15 -35
- package/stagedRollouts/functions/viewerRequest.js.map +1 -1
- package/stagedRollouts/functions/viewerResponse.js +8 -10
- package/stagedRollouts/functions/viewerResponse.js.map +1 -1
- package/stagedRollouts/utils/common.js +5 -4
- package/stagedRollouts/utils/common.js.map +1 -1
- package/stagedRollouts/utils/headerBlacklist.js +27 -13
- package/stagedRollouts/utils/headerBlacklist.js.map +1 -1
- package/stagedRollouts/utils/loadOriginPage.js +70 -91
- package/stagedRollouts/utils/loadOriginPage.js.map +1 -1
- package/stagedRollouts/utils/loadTrafficSplittingConfig.js +35 -51
- package/stagedRollouts/utils/loadTrafficSplittingConfig.js.map +1 -1
- package/stagedRollouts/utils/loadVariantOrigin.js +41 -60
- package/stagedRollouts/utils/loadVariantOrigin.js.map +1 -1
- package/stagedRollouts/utils/log.js +3 -6
- package/stagedRollouts/utils/log.js.map +1 -1
- package/cloudfrontFunctions/index.js.map +0 -1
- package/cloudfrontFunctions/types.js.map +0 -1
- package/index.js.map +0 -1
- package/lambdaEdge/index.js.map +0 -1
- package/lambdaEdge/types.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"stagedRollouts/utils/loadOriginPage.js","sources":["../../../src/stagedRollouts/utils/loadOriginPage.ts"],"sourcesContent":["import type { CloudFrontResponse } from \"~/lambdaEdge/index.js\";\nimport { get } from \"https\";\nimport { load } from \"cheerio\";\nimport type { SrcSetDefinition } from \"srcset\";\nimport { parseSrcset, stringifySrcset } from \"srcset\";\nimport { isHeaderBlacklisted } from \"./headerBlacklist.js\";\nimport { logDebug } from \"./log.js\";\n\n// TODO cheerio does not have Element type, so we define it here\ninterface Element {\n attribs: {\n [key: string]: string;\n };\n}\n\n/**\n * Load HTML from origin and perform transformation of URLs to absolute\n * @param domain Domain of the origin cloudfront\n * @param path Path of the file we want to retrieve\n * @returns Response object\n */\nexport function loadOriginPage(domain: string, path: string) {\n return new Promise<CloudFrontResponse>((resolve, reject) => {\n logDebug(`Pulling page from ${domain}${path}`);\n let responseBody = \"\";\n const req = get(\n {\n hostname: domain,\n port: 443,\n path: path\n },\n res => {\n res.on(\"data\", chunk => (responseBody += chunk));\n res.on(\"end\", () => {\n logDebug(`Parsing page`);\n const html = parseHtml(responseBody, domain);\n const response: CloudFrontResponse = {\n body: html,\n status: \"200\",\n statusDescription: \"ok\",\n headers: {}\n };\n\n // Rewrite headers from the HTTP response\n for (const header of Object.keys(res.headers)) {\n // We cannot set any header we want.\n // CloudFront is restricting us from setting or changing some headers.\n if (isHeaderBlacklisted(header)) {\n continue;\n }\n\n const value = res.headers[header];\n if (Array.isArray(value)) {\n response.headers[header] = value.map(h => ({\n key: header,\n value: h\n }));\n } else if (value) {\n response.headers[header] = [\n {\n key: header,\n value: value\n }\n ];\n }\n }\n\n logDebug(`Page parsed`);\n resolve(response);\n });\n }\n );\n\n req.on(\"error\", e => {\n reject({\n statusCode: 500,\n body: e.message\n });\n });\n });\n}\n\nfunction parseHtml(html: string, domain: string) {\n const doc = load(html);\n const host = `https://${domain}`;\n\n doc(\"head > link\").each((_i, el) => {\n prefixUrl(el, \"href\", host);\n });\n\n doc(\"script\").each((_i, el) => {\n prefixUrl(el, \"src\", host);\n });\n\n doc(\"body\")\n .find(\"img\")\n .each((_i, el) => {\n prefixUrl(el, \"src\", host);\n\n // Handle also srcset property for responsive images.\n const srcset = el.attribs[\"srcset\"];\n if (srcset) {\n const srcsetParsed = parseSrcset(srcset);\n const srcsetRebased = srcsetParsed.map<SrcSetDefinition>(src => {\n if (src.url.startsWith(\"/\")) {\n return {\n url: host + src.url,\n density: src.density,\n width: src.width\n };\n }\n\n return src;\n });\n\n el.attribs[\"srcset\"] = stringifySrcset(srcsetRebased);\n }\n });\n\n return doc.html();\n}\n\nfunction prefixUrl(el: Element, attr: string, host: string) {\n const href = el.attribs[attr];\n if (href && href.startsWith(\"/\")) {\n el.attribs[attr] = host + href;\n }\n}\n"],"names":["loadOriginPage","domain","path","Promise","resolve","reject","logDebug","responseBody","req","get","res","chunk","html","parseHtml","response","header","Object","isHeaderBlacklisted","value","Array","h","e","doc","load","host","_i","el","prefixUrl","srcset","srcsetParsed","parseSrcset","srcsetRebased","src","stringifySrcset","attr","href"],"mappings":";;;;;AAqBO,SAASA,eAAeC,MAAc,EAAEC,IAAY;IACvD,OAAO,IAAIC,QAA4B,CAACC,SAASC;QAC7CC,SAAS,CAAC,kBAAkB,EAAEL,SAASC,MAAM;QAC7C,IAAIK,eAAe;QACnB,MAAMC,MAAMC,IACR;YACI,UAAUR;YACV,MAAM;YACN,MAAMC;QACV,GACAQ,CAAAA;YACIA,IAAI,EAAE,CAAC,QAAQC,CAAAA,QAAUJ,gBAAgBI;YACzCD,IAAI,EAAE,CAAC,OAAO;gBACVJ,SAAS;gBACT,MAAMM,OAAOC,UAAUN,cAAcN;gBACrC,MAAMa,WAA+B;oBACjC,MAAMF;oBACN,QAAQ;oBACR,mBAAmB;oBACnB,SAAS,CAAC;gBACd;gBAGA,KAAK,MAAMG,UAAUC,OAAO,IAAI,CAACN,IAAI,OAAO,EAAG;oBAG3C,IAAIO,oBAAoBF,SACpB;oBAGJ,MAAMG,QAAQR,IAAI,OAAO,CAACK,OAAO;oBACjC,IAAII,MAAM,OAAO,CAACD,QACdJ,SAAS,OAAO,CAACC,OAAO,GAAGG,MAAM,GAAG,CAACE,CAAAA,IAAM;4BACvC,KAAKL;4BACL,OAAOK;wBACX;yBACG,IAAIF,OACPJ,SAAS,OAAO,CAACC,OAAO,GAAG;wBACvB;4BACI,KAAKA;4BACL,OAAOG;wBACX;qBACH;gBAET;gBAEAZ,SAAS;gBACTF,QAAQU;YACZ;QACJ;QAGJN,IAAI,EAAE,CAAC,SAASa,CAAAA;YACZhB,OAAO;gBACH,YAAY;gBACZ,MAAMgB,EAAE,OAAO;YACnB;QACJ;IACJ;AACJ;AAEA,SAASR,UAAUD,IAAY,EAAEX,MAAc;IAC3C,MAAMqB,MAAMC,KAAKX;IACjB,MAAMY,OAAO,CAAC,QAAQ,EAAEvB,QAAQ;IAEhCqB,IAAI,eAAe,IAAI,CAAC,CAACG,IAAIC;QACzBC,UAAUD,IAAI,QAAQF;IAC1B;IAEAF,IAAI,UAAU,IAAI,CAAC,CAACG,IAAIC;QACpBC,UAAUD,IAAI,OAAOF;IACzB;IAEAF,IAAI,QACC,IAAI,CAAC,OACL,IAAI,CAAC,CAACG,IAAIC;QACPC,UAAUD,IAAI,OAAOF;QAGrB,MAAMI,SAASF,GAAG,OAAO,CAAC,SAAS;QACnC,IAAIE,QAAQ;YACR,MAAMC,eAAeC,YAAYF;YACjC,MAAMG,gBAAgBF,aAAa,GAAG,CAAmBG,CAAAA;gBACrD,IAAIA,IAAI,GAAG,CAAC,UAAU,CAAC,MACnB,OAAO;oBACH,KAAKR,OAAOQ,IAAI,GAAG;oBACnB,SAASA,IAAI,OAAO;oBACpB,OAAOA,IAAI,KAAK;gBACpB;gBAGJ,OAAOA;YACX;YAEAN,GAAG,OAAO,CAAC,SAAS,GAAGO,gBAAgBF;QAC3C;IACJ;IAEJ,OAAOT,IAAI,IAAI;AACnB;AAEA,SAASK,UAAUD,EAAW,EAAEQ,IAAY,EAAEV,IAAY;IACtD,MAAMW,OAAOT,GAAG,OAAO,CAACQ,KAAK;IAC7B,IAAIC,QAAQA,KAAK,UAAU,CAAC,MACxBT,GAAG,OAAO,CAACQ,KAAK,GAAGV,OAAOW;AAElC"}
|
|
@@ -1,63 +1,47 @@
|
|
|
1
1
|
import { get } from "https";
|
|
2
2
|
import { logDebug } from "./log.js";
|
|
3
|
-
|
|
4
|
-
// Config file has a fixed URL within CDN, so it can be properly cached.
|
|
5
|
-
// This way we achieve better performance, because CDN does not have to call WCP API for config every time,
|
|
6
|
-
// but it can use it's own cache for a lookup.
|
|
7
3
|
const configPath = "/_config";
|
|
8
|
-
|
|
9
|
-
// Config is locally cached within live lambda for a short time (1 minute).
|
|
10
|
-
// Config must be cached per domain.
|
|
11
|
-
// Otherwise cache will spill over different apps, because we may share this lambda.
|
|
12
4
|
const configCache = new Map();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (!config || isCacheExpired(config.timestamp)) {
|
|
26
|
-
logDebug("No config in cache");
|
|
27
|
-
config = {
|
|
28
|
-
config: await loadConfigCore(domain),
|
|
29
|
-
timestamp: Date.now()
|
|
30
|
-
};
|
|
31
|
-
configCache.set(domain, config);
|
|
32
|
-
}
|
|
33
|
-
return config.config;
|
|
5
|
+
async function loadTrafficSplittingConfig(event) {
|
|
6
|
+
const domain = event.Records[0].cf.config.distributionDomainName;
|
|
7
|
+
let config = configCache.get(domain);
|
|
8
|
+
if (!config || isCacheExpired(config.timestamp)) {
|
|
9
|
+
logDebug("No config in cache");
|
|
10
|
+
config = {
|
|
11
|
+
config: await loadConfigCore(domain),
|
|
12
|
+
timestamp: Date.now()
|
|
13
|
+
};
|
|
14
|
+
configCache.set(domain, config);
|
|
15
|
+
}
|
|
16
|
+
return config.config;
|
|
34
17
|
}
|
|
35
18
|
function loadConfigCore(domain) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
19
|
+
return new Promise((resolve, reject)=>{
|
|
20
|
+
let dataString = "";
|
|
21
|
+
const req = get({
|
|
22
|
+
hostname: domain,
|
|
23
|
+
port: 443,
|
|
24
|
+
path: configPath
|
|
25
|
+
}, function(res) {
|
|
26
|
+
res.on("data", (chunk)=>{
|
|
27
|
+
dataString += chunk;
|
|
28
|
+
});
|
|
29
|
+
res.on("end", ()=>{
|
|
30
|
+
resolve(JSON.parse(dataString));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
req.on("error", (e)=>{
|
|
34
|
+
reject({
|
|
35
|
+
statusCode: 500,
|
|
36
|
+
body: e.message
|
|
37
|
+
});
|
|
38
|
+
});
|
|
55
39
|
});
|
|
56
|
-
});
|
|
57
40
|
}
|
|
58
41
|
function isCacheExpired(timestamp) {
|
|
59
|
-
|
|
60
|
-
|
|
42
|
+
const ttl = 60000;
|
|
43
|
+
return Date.now() - timestamp > ttl;
|
|
61
44
|
}
|
|
45
|
+
export { loadTrafficSplittingConfig };
|
|
62
46
|
|
|
63
47
|
//# sourceMappingURL=loadTrafficSplittingConfig.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"stagedRollouts/utils/loadTrafficSplittingConfig.js","sources":["../../../src/stagedRollouts/utils/loadTrafficSplittingConfig.ts"],"sourcesContent":["import { get } from \"https\";\nimport type { CloudFrontRequestEvent } from \"~/lambdaEdge/index.js\";\nimport { logDebug } from \"./log.js\";\n\n// Config file has a fixed URL within CDN, so it can be properly cached.\n// This way we achieve better performance, because CDN does not have to call WCP API for config every time,\n// but it can use it's own cache for a lookup.\nconst configPath = \"/_config\";\n\n// Config is locally cached within live lambda for a short time (1 minute).\n// Config must be cached per domain.\n// Otherwise cache will spill over different apps, because we may share this lambda.\nconst configCache = new Map<string, GatewayConfigCache>();\n\ninterface GatewayConfigCache {\n config: GatewayConfig;\n timestamp: number;\n}\n\nexport interface VariantConfig {\n domain: string;\n weight: number;\n}\n\nexport type GatewayConfig = Record<string, VariantConfig>;\n\n/**\n * Loads traffic splitting config.\n * It will, however not call WCP directly, but serve it from a locally cached file,\n * as explained here https://www.notion.so/webiny/How-traffic-config-is-cached-2c8db57ca2b547a2b2fb1adf378cd191\n */\nexport async function loadTrafficSplittingConfig(event: CloudFrontRequestEvent) {\n // Retrieve domain of the CloudFront distribution.\n // We need it to make sure we cache config per application.\n // For example API and website could share the same lambda instance.\n // So we cache it separately for each domain (each CloudFront).\n const domain = event.Records[0].cf.config.distributionDomainName;\n\n let config = configCache.get(domain);\n if (!config || isCacheExpired(config.timestamp)) {\n logDebug(\"No config in cache\");\n config = {\n config: await loadConfigCore(domain),\n timestamp: Date.now()\n };\n\n configCache.set(domain, config);\n }\n\n return config.config;\n}\n\nfunction loadConfigCore(domain: string) {\n return new Promise<GatewayConfig>((resolve, reject) => {\n let dataString = \"\";\n\n const req = get(\n {\n hostname: domain,\n port: 443,\n path: configPath\n },\n function (res) {\n res.on(\"data\", chunk => {\n dataString += chunk;\n });\n res.on(\"end\", () => {\n resolve(JSON.parse(dataString));\n });\n }\n );\n\n req.on(\"error\", e => {\n reject({\n statusCode: 500,\n body: e.message\n });\n });\n });\n}\n\nfunction isCacheExpired(timestamp: number) {\n const ttl = 60 * 1000; // 1 minute\n return Date.now() - timestamp > ttl;\n}\n"],"names":["configPath","configCache","Map","loadTrafficSplittingConfig","event","domain","config","isCacheExpired","logDebug","loadConfigCore","Date","Promise","resolve","reject","dataString","req","get","res","chunk","JSON","e","timestamp","ttl"],"mappings":";;AAOA,MAAMA,aAAa;AAKnB,MAAMC,cAAc,IAAIC;AAmBjB,eAAeC,2BAA2BC,KAA6B;IAK1E,MAAMC,SAASD,MAAM,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,sBAAsB;IAEhE,IAAIE,SAASL,YAAY,GAAG,CAACI;IAC7B,IAAI,CAACC,UAAUC,eAAeD,OAAO,SAAS,GAAG;QAC7CE,SAAS;QACTF,SAAS;YACL,QAAQ,MAAMG,eAAeJ;YAC7B,WAAWK,KAAK,GAAG;QACvB;QAEAT,YAAY,GAAG,CAACI,QAAQC;IAC5B;IAEA,OAAOA,OAAO,MAAM;AACxB;AAEA,SAASG,eAAeJ,MAAc;IAClC,OAAO,IAAIM,QAAuB,CAACC,SAASC;QACxC,IAAIC,aAAa;QAEjB,MAAMC,MAAMC,IACR;YACI,UAAUX;YACV,MAAM;YACN,MAAML;QACV,GACA,SAAUiB,GAAG;YACTA,IAAI,EAAE,CAAC,QAAQC,CAAAA;gBACXJ,cAAcI;YAClB;YACAD,IAAI,EAAE,CAAC,OAAO;gBACVL,QAAQO,KAAK,KAAK,CAACL;YACvB;QACJ;QAGJC,IAAI,EAAE,CAAC,SAASK,CAAAA;YACZP,OAAO;gBACH,YAAY;gBACZ,MAAMO,EAAE,OAAO;YACnB;QACJ;IACJ;AACJ;AAEA,SAASb,eAAec,SAAiB;IACrC,MAAMC,MAAM;IACZ,OAAOZ,KAAK,GAAG,KAAKW,YAAYC;AACpC"}
|
|
@@ -1,70 +1,51 @@
|
|
|
1
1
|
import { getHeader, notFoundResponse } from "../../lambdaEdge/index.js";
|
|
2
|
-
import { variantFixedKey, variantRandomKey } from "
|
|
3
|
-
import { loadTrafficSplittingConfig } from "
|
|
2
|
+
import { variantFixedKey, variantRandomKey } from "./common.js";
|
|
3
|
+
import { loadTrafficSplittingConfig } from "./loadTrafficSplittingConfig.js";
|
|
4
4
|
import { logDebug } from "./log.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
5
|
+
async function loadVariantOrigin(event) {
|
|
6
|
+
const cf = event.Records[0].cf;
|
|
7
|
+
const request = cf.request;
|
|
8
|
+
const config = await loadTrafficSplittingConfig(event);
|
|
9
|
+
const variantFixed = getHeader(request.headers, variantFixedKey);
|
|
10
|
+
if (variantFixed) {
|
|
11
|
+
const variantConfig = config[variantFixed];
|
|
12
|
+
if (variantConfig) return {
|
|
13
|
+
variant: variantConfig
|
|
14
|
+
};
|
|
15
|
+
return notFoundResponse(`No variant ${variantFixed} found`);
|
|
16
|
+
}
|
|
17
|
+
const variantRandom = Number(getHeader(request.headers, variantRandomKey));
|
|
18
|
+
if (isNaN(variantRandom)) {
|
|
19
|
+
logDebug("No random variant passed, passing the request");
|
|
20
|
+
return request;
|
|
21
|
+
}
|
|
22
|
+
logDebug(`Variant random ${variantRandom}`);
|
|
23
|
+
const variantConfig = getRandomVariant(config, variantRandom);
|
|
24
|
+
if (!variantConfig) {
|
|
25
|
+
logDebug("No variant is found");
|
|
26
|
+
return notFoundResponse("No variant is found");
|
|
18
27
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
logDebug("No random variant passed, passing the request");
|
|
23
|
-
// Random variant header should be always present.
|
|
24
|
-
// It it's not, something bad happened, so we just pass request further.
|
|
25
|
-
return request;
|
|
26
|
-
}
|
|
27
|
-
logDebug(`Variant random ${variantRandom}`);
|
|
28
|
-
const variantConfig = getRandomVariant(config, variantRandom);
|
|
29
|
-
if (!variantConfig) {
|
|
30
|
-
// If no variant is matching the random value, just return 404.
|
|
31
|
-
// This should happen only if there is really not a single variant serving traffic.
|
|
32
|
-
logDebug(`No variant is found`);
|
|
33
|
-
return notFoundResponse(`No variant is found`);
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
variant: variantConfig
|
|
37
|
-
};
|
|
28
|
+
return {
|
|
29
|
+
variant: variantConfig
|
|
30
|
+
};
|
|
38
31
|
}
|
|
39
32
|
function getRandomVariant(config, random) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
// do not count bad or negative weights
|
|
46
|
-
totalWeight += variantConfig.weight;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (totalWeight <= 0 || random < 0) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Normalize random value to total weight of traffic splitting rates.
|
|
54
|
-
random = random * totalWeight / 100;
|
|
55
|
-
for (const variant of variants) {
|
|
56
|
-
const variantConfig = config[variant];
|
|
57
|
-
if (!variantConfig.weight) {
|
|
58
|
-
continue;
|
|
33
|
+
let totalWeight = 0;
|
|
34
|
+
const variants = Object.keys(config);
|
|
35
|
+
for (const variant of variants){
|
|
36
|
+
const variantConfig = config[variant];
|
|
37
|
+
if (variantConfig.weight) totalWeight += variantConfig.weight;
|
|
59
38
|
}
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
39
|
+
if (totalWeight <= 0 || random < 0) return;
|
|
40
|
+
random = random * totalWeight / 100;
|
|
41
|
+
for (const variant of variants){
|
|
42
|
+
const variantConfig = config[variant];
|
|
43
|
+
if (variantConfig.weight) if (random <= variantConfig.weight) {
|
|
44
|
+
logDebug(`Variant ${variant} selected`);
|
|
45
|
+
return config[variant];
|
|
46
|
+
} else random -= variantConfig.weight;
|
|
65
47
|
}
|
|
66
|
-
}
|
|
67
|
-
return;
|
|
68
48
|
}
|
|
49
|
+
export { loadVariantOrigin };
|
|
69
50
|
|
|
70
51
|
//# sourceMappingURL=loadVariantOrigin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"stagedRollouts/utils/loadVariantOrigin.js","sources":["../../../src/stagedRollouts/utils/loadVariantOrigin.ts"],"sourcesContent":["import type { CloudFrontRequestEvent } from \"~/lambdaEdge/index.js\";\nimport { getHeader, notFoundResponse } from \"~/lambdaEdge/index.js\";\n\nimport { variantFixedKey, variantRandomKey } from \"../utils/common.js\";\nimport type { GatewayConfig } from \"../utils/loadTrafficSplittingConfig.js\";\nimport { loadTrafficSplittingConfig } from \"../utils/loadTrafficSplittingConfig.js\";\nimport { logDebug } from \"./log.js\";\n\nexport async function loadVariantOrigin(event: CloudFrontRequestEvent) {\n const cf = event.Records[0].cf;\n const request = cf.request;\n\n const config = await loadTrafficSplittingConfig(event);\n\n const variantFixed = getHeader(request.headers, variantFixedKey);\n if (variantFixed) {\n const variantConfig = config[variantFixed];\n if (variantConfig) {\n return { variant: variantConfig };\n } else {\n return notFoundResponse(`No variant ${variantFixed} found`);\n }\n }\n\n const variantRandom = Number(getHeader(request.headers, variantRandomKey));\n if (isNaN(variantRandom)) {\n logDebug(\"No random variant passed, passing the request\");\n // Random variant header should be always present.\n // It it's not, something bad happened, so we just pass request further.\n return request;\n }\n\n logDebug(`Variant random ${variantRandom}`);\n\n const variantConfig = getRandomVariant(config, variantRandom);\n if (!variantConfig) {\n // If no variant is matching the random value, just return 404.\n // This should happen only if there is really not a single variant serving traffic.\n logDebug(`No variant is found`);\n return notFoundResponse(`No variant is found`);\n }\n\n return { variant: variantConfig };\n}\n\nfunction getRandomVariant(config: GatewayConfig, random: number) {\n let totalWeight = 0;\n\n const variants = Object.keys(config);\n for (const variant of variants) {\n const variantConfig = config[variant];\n if (variantConfig.weight) {\n // do not count bad or negative weights\n totalWeight += variantConfig.weight;\n }\n }\n\n if (totalWeight <= 0 || random < 0) {\n return;\n }\n\n // Normalize random value to total weight of traffic splitting rates.\n random = (random * totalWeight) / 100;\n\n for (const variant of variants) {\n const variantConfig = config[variant];\n if (!variantConfig.weight) {\n continue;\n }\n\n if (random <= variantConfig.weight) {\n logDebug(`Variant ${variant} selected`);\n return config[variant];\n } else {\n random -= variantConfig.weight;\n }\n }\n\n return;\n}\n"],"names":["loadVariantOrigin","event","cf","request","config","loadTrafficSplittingConfig","variantFixed","getHeader","variantFixedKey","variantConfig","notFoundResponse","variantRandom","Number","variantRandomKey","isNaN","logDebug","getRandomVariant","random","totalWeight","variants","Object","variant"],"mappings":";;;;AAQO,eAAeA,kBAAkBC,KAA6B;IACjE,MAAMC,KAAKD,MAAM,OAAO,CAAC,EAAE,CAAC,EAAE;IAC9B,MAAME,UAAUD,GAAG,OAAO;IAE1B,MAAME,SAAS,MAAMC,2BAA2BJ;IAEhD,MAAMK,eAAeC,UAAUJ,QAAQ,OAAO,EAAEK;IAChD,IAAIF,cAAc;QACd,MAAMG,gBAAgBL,MAAM,CAACE,aAAa;QAC1C,IAAIG,eACA,OAAO;YAAE,SAASA;QAAc;QAEhC,OAAOC,iBAAiB,CAAC,WAAW,EAAEJ,aAAa,MAAM,CAAC;IAElE;IAEA,MAAMK,gBAAgBC,OAAOL,UAAUJ,QAAQ,OAAO,EAAEU;IACxD,IAAIC,MAAMH,gBAAgB;QACtBI,SAAS;QAGT,OAAOZ;IACX;IAEAY,SAAS,CAAC,eAAe,EAAEJ,eAAe;IAE1C,MAAMF,gBAAgBO,iBAAiBZ,QAAQO;IAC/C,IAAI,CAACF,eAAe;QAGhBM,SAAS;QACT,OAAOL,iBAAiB;IAC5B;IAEA,OAAO;QAAE,SAASD;IAAc;AACpC;AAEA,SAASO,iBAAiBZ,MAAqB,EAAEa,MAAc;IAC3D,IAAIC,cAAc;IAElB,MAAMC,WAAWC,OAAO,IAAI,CAAChB;IAC7B,KAAK,MAAMiB,WAAWF,SAAU;QAC5B,MAAMV,gBAAgBL,MAAM,CAACiB,QAAQ;QACrC,IAAIZ,cAAc,MAAM,EAEpBS,eAAeT,cAAc,MAAM;IAE3C;IAEA,IAAIS,eAAe,KAAKD,SAAS,GAC7B;IAIJA,SAAUA,SAASC,cAAe;IAElC,KAAK,MAAMG,WAAWF,SAAU;QAC5B,MAAMV,gBAAgBL,MAAM,CAACiB,QAAQ;QACrC,IAAKZ,cAAc,MAAM,EAIzB,IAAIQ,UAAUR,cAAc,MAAM,EAAE;YAChCM,SAAS,CAAC,QAAQ,EAAEM,QAAQ,SAAS,CAAC;YACtC,OAAOjB,MAAM,CAACiB,QAAQ;QAC1B,OACIJ,UAAUR,cAAc,MAAM;IAEtC;AAGJ"}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// this function should be minified to an empty one and removed completely from the bundle.
|
|
4
|
-
if (process.env.DEBUG === "true") {
|
|
5
|
-
console.log(...args);
|
|
6
|
-
}
|
|
1
|
+
function logDebug(...args) {
|
|
2
|
+
if ("true" === process.env.DEBUG) console.log(...args);
|
|
7
3
|
}
|
|
4
|
+
export { logDebug };
|
|
8
5
|
|
|
9
6
|
//# sourceMappingURL=log.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"stagedRollouts/utils/log.js","sources":["../../../src/stagedRollouts/utils/log.ts"],"sourcesContent":["export function logDebug(...args: any[]) {\n // If DEBUG variable is not set during build,\n // this function should be minified to an empty one and removed completely from the bundle.\n if (process.env.DEBUG === \"true\") {\n console.log(...args);\n }\n}\n"],"names":["logDebug","args","process","console"],"mappings":"AAAO,SAASA,SAAS,GAAGC,IAAW;IAGnC,IAAIC,AAAsB,WAAtBA,QAAQ,GAAG,CAAC,KAAK,EACjBC,QAAQ,GAAG,IAAIF;AAEvB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./cookies.js\";\nexport * from \"./headers.js\";\nexport * from \"./querystring.js\";\nexport * from \"./redirect.js\";\nexport * from \"./types.js\";\nexport * from \"./utils.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["// These are types for CloudFront Functions.\n// They differ from Lambda@Edge types.\n// See also: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html\n\nexport interface SingleValue {\n value: string;\n}\n\nexport interface Header extends SingleValue {\n key: string;\n}\n\nexport interface ResponseCookie extends SingleValue {\n attributes?: string;\n}\n\n/**\n * In principle you may have multiple same headers and cookies in one request or response.\n * This is a wrapper interface for simpler use.\n */\nexport type MultiValue<T> = T & {\n /** Additional values for the same item (for example multiple values for the same cookie.) */\n multivalue?: T[];\n};\n\nexport type MultiValueDictionary<T> = Record<string, MultiValue<T> | undefined>;\n\nexport type CloudFrontHeaders = MultiValueDictionary<SingleValue>;\nexport type CloudFrontRequestCookies = MultiValueDictionary<SingleValue>;\nexport type CloudFrontResponseCookies = MultiValueDictionary<ResponseCookie>;\nexport type CloudFrontQuery = MultiValueDictionary<SingleValue>;\n\n/**\n * Interface type for request in CloudFront Functions\n * see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html\n */\nexport interface CloudFrontRequest {\n method: string;\n uri: string;\n querystring?: CloudFrontQuery;\n headers: CloudFrontHeaders;\n cookies?: CloudFrontRequestCookies;\n}\n\n/**\n * Interface type for response in CloudFront Functions\n * see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html\n */\nexport interface CloudFrontResponse {\n statusCode: number;\n statusDescription?: string;\n headers: CloudFrontHeaders;\n cookies?: CloudFrontResponseCookies;\n}\n\n/**\n * Interface type for request event in CloudFront Functions\n * see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html\n */\nexport interface CloudFrontRequestEvent {\n version: string;\n context: {\n eventType: \"viewer-request\";\n };\n viewer: {\n ip: string;\n };\n request: CloudFrontRequest;\n}\n\n/**\n * Interface type for response event in CloudFront Functions\n * see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html\n */\nexport interface CloudFrontResponseEvent {\n version: string;\n context: {\n eventType: \"viewer-response\";\n };\n viewer: {\n ip: string;\n };\n response: CloudFrontResponse;\n request: CloudFrontRequest;\n}\n\nexport interface CloudFrontRequestHandler {\n (event: CloudFrontRequestEvent): CloudFrontRequest | CloudFrontResponse;\n}\n\nexport interface CloudFrontResponseHandler {\n (event: CloudFrontResponseEvent): CloudFrontResponse;\n}\n"],"mappings":"","ignoreList":[]}
|
package/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./lambdaEdge/index.js\";\n"],"mappings":"AAAA","ignoreList":[]}
|
package/lambdaEdge/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./cookies.js\";\nexport * from \"./headers.js\";\nexport * from \"./redirect.js\";\nexport * from \"./request.js\";\nexport * from \"./response.js\";\nexport * from \"./types.js\";\nexport * from \"./utils.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
package/lambdaEdge/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type {\n CloudFrontEvent as BaseCloudFrontEvent,\n CloudFrontRequest as BaseCloudFrontRequest,\n CloudFrontRequestEvent as BaseCloudFrontRequestEvent,\n CloudFrontResponse as BaseCloudFrontResponse,\n CloudFrontResponseEvent as BaseCloudFrontResponseEvent,\n CloudFrontResultResponse as BaseCloudFrontResultResponse,\n CloudFrontHeaders as BaseCloudFrontHeaders\n} from \"aws-lambda\";\n\nexport type CloudFrontEvent = BaseCloudFrontEvent;\nexport type CloudFrontRequest = BaseCloudFrontRequest;\nexport type CloudFrontRequestEvent = BaseCloudFrontRequestEvent;\nexport type CloudFrontResponse = BaseCloudFrontResponse & { body?: string };\nexport type CloudFrontResponseEvent = BaseCloudFrontResponseEvent;\nexport type CloudFrontResultResponse = BaseCloudFrontResultResponse;\nexport type CloudFrontHeaders = BaseCloudFrontHeaders;\n"],"mappings":"","ignoreList":[]}
|