This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedgeside-feature-flags.js
72 lines (65 loc) · 1.67 KB
/
edgeside-feature-flags.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/******************************/
/* Edge-side feature toggling */
/******************************/
const FEATURE_TOGGLES = [
{
market: "SE",
newFeatureActive: true,
abSplitPercentage: { new: 50, current: 50 }
},
{
market: "US",
newFeatureActive: false,
abSplitPercentage: { new: 30, current: 70 }
},
{
market: "JP",
newFeatureActive: true,
abSplitPercentage: { new: 80, current: 20 }
},
{
market: "default",
newFeatureActive: false,
abSplitPercentage: { new: 0, current: 100 }
}
];
/**
* Worker logic
* @async
* @function
* @param {Request} request - The incoming request data
* @returns {Response} - Returns markup and headers
*/
async function handleRequest(request) {
// We want to know where the user is coming from
// If this is null, return 'default' so the API will pick a fallback/default flag set
const USER_COUNTRY = (() => {
if (request.headers.get("CF-IPCountry")) {
return request.headers.get("CF-IPCountry");
} else return "default";
})();
const FLAGS = (() => {
if (
FEATURE_TOGGLES.find(marketFlags => marketFlags.market === USER_COUNTRY)
) {
return FEATURE_TOGGLES.find(
marketFlags => marketFlags.market === USER_COUNTRY
);
} else
return FEATURE_TOGGLES.find(
marketFlags => marketFlags.market === "default"
);
})();
console.log(FLAGS);
const DATA = {
headers: {
...request.headers,
"X-User-Country": USER_COUNTRY,
"Access-Control-Allow-Origin": "*"
}
};
return new Response(JSON.stringify(FLAGS), DATA);
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});