-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add strict-mode proxy wrapper #44
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c2d079
In strict mode, wrap output in a proxy that throws for missing keys
af 0cea666
Add tests for new strict mode behavior
af 0a91f08
Document new strict mode behavior in the readme
af 300acbe
Improvements from code review
af beef35e
Proxy code simplifications
af File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,30 @@ | ||
/** | ||
* 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] | ||
|
||
// if (name === 'prototype') return {} | ||
// if (name === 'nodeType') return undefined | ||
|
||
const varExists = envObj.hasOwnProperty(name) | ||
if (!varExists) { | ||
throw new Error(`[envalid] Environment var not found: ${name}`) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unneeded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah, good point :) |
||
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') | ||
}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
varExists
can be inlined