-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (150 loc) · 5.03 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
const errors = {
unknown_feature: (flagName) =>
`There is no feature named '${flagName}'. Check in your features file to see if there was a spelling mistake.`,
missing_description: (flagName) =>
`The feature flag ${flagName} did not have a 'description'. Please ensure you describe a feature flag in a few words to allow fellow engineers to avoid guessing`,
missing_enabled: (flagName) =>
`The feature flag ${flagName} has no key 'enabled'. It is a healthy practice to make statements about the state of the flag explicit. If you rely on a source to enable it, at least mark it as 'enabled: false' in the config.`,
unknown_env_enabled: (flagName, envInFlag, configuredEnvs) =>
`The feature flag ${flagName} is configured for ${envInFlag} that is not listed in environments.available: ${configuredEnvs}. Please check if this is an error.`,
missing_environment: () =>
'You need to configure which environments are available to your application under config.environments.available as an array of strings. This allows us to check your config for consistency'
}
const checkConsistency = (config) => {
for (const flagName in config.flags) {
const flag = config.flags[flagName]
if (!flag.hasOwnProperty('description')) {
throw errors.missing_description(flagName)
}
if (!flag.hasOwnProperty('enabled')) {
throw errors.missing_enabled(flagName)
}
if (typeof flag.enabled === 'object') {
if (config.environments) {
const configuredEnvs = config.environments.available || []
for (const envInFlag in flag.enabled) {
if (!configuredEnvs.includes(envInFlag)) {
throw errors.unknown_env_enabled(
flagName,
envInFlag,
configuredEnvs
)
}
}
} else {
throw errors.missing_environment()
}
}
}
}
const checkSources = (sources, flag) => {
for (const source of sources) {
if (typeof source === 'function' && source(flag)) {
return true
}
if (typeof source === 'object' && source.isEnabled(flag)) {
return true
}
}
}
const hardEnabled = (environments, flag) => {
if (typeof flag.enabled === 'boolean') {
return flag.enabled
}
if (environments) {
return flag.enabled[environments.current]
}
}
module.exports = {
wrap: (config) => {
checkConsistency(config)
const { flags, sources = [], environments = false } = config
const toggle = (self, flagName, { to: value, around: closure }) => {
const oldFlagValue = self.isEnabled(flagName)
flags[flagName].enabled = value
try {
return closure()
} finally {
flags[flagName].enabled = oldFlagValue
}
}
const toggleAsync = async (
self,
flagName,
{ to: value, around: closure }
) => {
const oldFlagValue = self.isEnabled(flagName)
flags[flagName].enabled = value
try {
return await closure()
} finally {
flags[flagName].enabled = oldFlagValue
}
}
return {
isEnabled: function(flagName) {
const flag = flags[flagName]
if (flag) {
flag.name = flagName
return (
hardEnabled(environments, flag) ||
checkSources(sources, flag) ||
false
)
} else {
throw errors.unknown_feature(flagName)
}
},
state: function() {
return Object.keys(flags).map((flagName) => {
return {
name: flagName,
enabled: this.isEnabled(flagName),
description: flags[flagName].description
}
})
},
enabling: function(flagName, closure) {
return toggle(this, flagName, { to: true, around: closure })
},
disabling: function(flagName, closure) {
return toggle(this, flagName, { to: false, around: closure })
},
enablingAsync: async function(flagName, closure) {
return toggleAsync(this, flagName, { to: true, around: closure })
},
disablingAsync: async function(flagName, closure) {
return toggleAsync(this, flagName, { to: false, around: closure })
},
testBox: function() {
const features = this
let changedFlags = []
const set = (flagName, { to: value }) => {
const isEnabled = features.isEnabled(flagName)
changedFlags = changedFlags.concat({
flag: flagName,
originalValue: isEnabled
})
flags[flagName].enabled = value
}
return {
enable: (flagName) => set(flagName, { to: true }),
disable: (flagName) => set(flagName, { to: false }),
reset: () => {
changedFlags.forEach(
({ flag, originalValue }) => (flags[flag].enabled = originalValue)
)
changedFlags = []
}
}
}
}
},
sources: {
processEnvSource: (flag) => {
const value = process.env[flag.environment_flag]
return value === '1' || value === 'true'
}
}
}