-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from af/strict-proxy
Add strict-mode proxy wrapper
- Loading branch information
Showing
6 changed files
with
93 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Wrap the environment object with a Proxy that throws when: | ||
* a) trying to mutate an env var | ||
* b) trying to access an invalid (unset) env var | ||
* | ||
* @return {Object} - Proxied environment object with get/set traps | ||
*/ | ||
module.exports = envObj => new Proxy(envObj, { | ||
get(target, name) { | ||
// These checks are needed because calling console.log on a | ||
// proxy that throws crashes the entire process. This whitelists | ||
// the necessary properties for `console.log(envObj)` to work. | ||
if (['inspect', Symbol.toStringTag].includes(name)) return envObj[name] | ||
if (name.toString() === 'Symbol(util.inspect.custom)') return envObj[name] | ||
|
||
const varExists = envObj.hasOwnProperty(name) | ||
if (!varExists) throw new Error(`[envalid] Environment var not found: ${name}`) | ||
|
||
return envObj[name] | ||
}, | ||
|
||
set(name) { | ||
throw new Error(`[envalid] Attempt to mutate environment value: ${name}`) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require('fs') | ||
const { createGroup, assert } = require('painless') | ||
const { cleanEnv, str, num } = require('..') | ||
const test = createGroup() | ||
const strictOption = { strict: true } | ||
|
||
|
||
// assert.deepEqual() substitute for assertions on proxied strict-mode env objects | ||
// Chai's deepEqual() performs a few checks that the Proxy chokes on, so rather than | ||
// adding special-case code inside the proxy's get() trap, we use this custom assert | ||
// function | ||
const objStrictDeepEqual = (actual, desired) => { | ||
const desiredKeys = Object.keys(desired) | ||
assert.deepEqual(Object.keys(actual), desiredKeys) | ||
for (const k of desiredKeys) { | ||
assert.strictEqual(actual[k], desired[k]) | ||
} | ||
} | ||
|
||
test.beforeEach(() => fs.writeFileSync('.env', ` | ||
BAR=asdfasdf | ||
MYNUM=4 | ||
`)) | ||
test.afterEach(() => fs.unlinkSync('.env')) | ||
|
||
|
||
test('strict option: only specified fields are passed through', () => { | ||
const env = cleanEnv({ FOO: 'bar', BAZ: 'baz' }, { | ||
FOO: str() | ||
}, strictOption) | ||
objStrictDeepEqual(env, { FOO: 'bar' }) | ||
}) | ||
|
||
test('.env test in strict mode', () => { | ||
const env = cleanEnv({ FOO: 'bar', BAZ: 'baz' }, { | ||
MYNUM: num() | ||
}, strictOption) | ||
objStrictDeepEqual(env, { MYNUM: 4 }) | ||
}) | ||
|
||
test('strict mode objects throw when invalid attrs are accessed', () => { | ||
const env = cleanEnv({ FOO: 'bar', BAZ: 'baz' }, { | ||
FOO: str() | ||
}, strictOption) | ||
assert.strictEqual(env.FOO, 'bar') | ||
assert.throws(() => env.ASDF) | ||
}) | ||
|
||
test('strict mode objects throw when attempting to mutate', () => { | ||
const env = cleanEnv({ FOO: 'bar', BAZ: 'baz' }, { | ||
FOO: str() | ||
}, strictOption) | ||
assert.throws(() => env.FOO = 'foooooo') | ||
}) | ||
|