-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvProxy.js
More file actions
26 lines (23 loc) · 1.1 KB
/
Copy pathenvProxy.js
File metadata and controls
26 lines (23 loc) · 1.1 KB
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
//uses Proxy object to wrap process.env and sync keys between the keystore and the environment
//this is a unidirectional sync: reading process.env.KEY_NAME will return keymixer.getKey(KEY_NAME) if value(s) for that key are in the keystore
//but writing process.env.KEY_NAME will not affect the keystore...
function syncWithEnvironment(keymixer) {
// Create a proxy that wraps the original process.env
const envProxy = new Proxy(process.env, {
get(target, service) {
// Handle symbol and non-string keys
if (typeof service === 'symbol' || typeof service !== 'string') {
return target[service];
}
// If we have at least one key stored in our key mixer for the given environment variable,
// return from the mixer... otherwise just return the normal environment variable
if (keymixer.hasKeysFor(service)) {
return keymixer.getKey(service)
} else
return target[service]
},
});
// Replace process.env with our proxied version
process.env = envProxy;
}
module.exports= {syncWithEnvironment};