This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86c64bc
commit 9729329
Showing
2 changed files
with
249 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
|
||
exports.register = (server, options, next) => { | ||
|
||
if (!options.prefix) { | ||
|
||
return next(new Error('Missing prefix')); | ||
} | ||
|
||
for (const envVars in process.env) { | ||
if (envVars.indexOf(options.prefix) === 0) { | ||
|
||
let appVar = {}; | ||
try { | ||
appVar = JSON.parse(process.env[envVars]); | ||
} | ||
catch (e) { | ||
|
||
return next(new Error(`Value unparsable: ${process.env[envVars]}`)); | ||
} | ||
|
||
if (appVar.path && appVar.hasOwnProperty('value')) { | ||
const appVarArr = appVar.path.split('.'); | ||
let tmp = server.app; | ||
|
||
for (let i = 0; i < appVarArr.length; ++i) { | ||
if (i === appVarArr.length - 1) { | ||
tmp[appVarArr[i]] = appVar.value; | ||
} | ||
else { | ||
tmp[appVarArr[i]] = tmp[appVarArr[i]] || {}; | ||
} | ||
tmp = tmp[appVarArr[i]]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return next(); | ||
}; | ||
|
||
|
||
exports.register.attributes = { | ||
pkg: require('../package.json') | ||
}; |
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,203 @@ | ||
'use strict'; | ||
|
||
const Rewire = require('rewire'); | ||
const Hapi = require('hapi'); | ||
const Code = require('code'); | ||
const Lab = require('lab'); | ||
const Plugin = Rewire('../'); | ||
|
||
const expect = Code.expect; | ||
const lab = exports.lab = Lab.script(); | ||
const beforeEach = lab.beforeEach; | ||
const describe = lab.describe; | ||
const it = lab.it; | ||
|
||
|
||
describe('registration and functionality', () => { | ||
|
||
let server; | ||
|
||
beforeEach((done) => { | ||
|
||
server = new Hapi.Server(); | ||
|
||
return done(); | ||
}); | ||
|
||
const register = (options, next) => { | ||
|
||
server.register({ | ||
register: Plugin, | ||
options: options | ||
}, (err) => { | ||
|
||
return next(err); | ||
}); | ||
}; | ||
|
||
it('registers', (done) => { | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('returns error if no option', (done) => { | ||
|
||
register({}, (err) => { | ||
|
||
expect(err).to.exist(); | ||
expect(err.message).to.equal('Missing prefix'); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('adds new server.app value', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}' | ||
} | ||
}); | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } }); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
|
||
it('adds only matching prefix', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}', | ||
SOME_VAR_test: 'nothingness' | ||
} | ||
}); | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } }); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('returns error on unparsable value', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}', | ||
FOO_VAR_test2: 'not a stringified object' | ||
} | ||
}); | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.exist(); | ||
expect(err.message).to.equal('Value unparsable: not a stringified object'); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('ignores if no value key', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}', | ||
FOO_VAR_test2: '{"path":"some","key": "test"}' | ||
} | ||
}); | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } }); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('ignores if no path key', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}', | ||
FOO_VAR_test2: '{"value":"some","key": "test"}' | ||
} | ||
}); | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } }); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('reuses existing objects in the path', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}' | ||
} | ||
}); | ||
|
||
server.app.some = {}; | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } }); | ||
|
||
return done(); | ||
}); | ||
}); | ||
|
||
it('overwrites existing value', (done) => { | ||
|
||
Plugin.__set__('process', { | ||
env: { | ||
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}' | ||
} | ||
}); | ||
|
||
server.app.some = { config: { stuff: 'Spock!' } }; | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Spock!' } } }); | ||
|
||
register({ | ||
prefix: 'FOO_VAR_' | ||
}, (err) => { | ||
|
||
expect(err).to.not.exist(); | ||
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } }); | ||
|
||
return done(); | ||
}); | ||
}); | ||
}); |