-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from 3 commits
0c2d079
0cea666
0a91f08
300acbe
beef35e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 val = envObj[name] | ||
if (val === undefined) { | ||
throw new Error(`Environment var not found: ${name}`) | ||
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. Add |
||
} 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 val | ||
} | ||
}, | ||
|
||
set(name) { | ||
throw new Error(`[envalid] Attempt to mutate environment value: ${name}`) | ||
}, | ||
}) |
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') | ||
}) | ||
|
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.
Use
hasOwnProperty
instead?