Skip to content

Instantly share code, notes, and snippets.

@saibotsivad
Last active July 11, 2018 16:09
Show Gist options
  • Save saibotsivad/9fe1d76a7a788e18076ffd0ccbdc90c1 to your computer and use it in GitHub Desktop.
Save saibotsivad/9fe1d76a7a788e18076ffd0ccbdc90c1 to your computer and use it in GitHub Desktop.

Context:

  • using the mannish mediator
  • all the mediator providers are in a folder named provider
  • they're all globbed up with a create script (see below)
  • because of the limitations of github gists, the filenames can't have slashes, so I made it so all the slashes are dashes
const mannish = require('mannish')
module.exports = ({ configuration, providers }) => {
const mediator = mannish()
providers
.forEach(({ path: filename, export: { sync, initialize } }) => {
const name = filename.replace('provider/', '').replace(/\.js$/, '')
const provider = initialize({
call: mediator.call,
callSync: mediator.callSync,
configuration
})
if (sync) {
mediator.provideSync(name, provider)
} else {
mediator.provide(name, provider)
}
})
return mediator
}
module.exports = {
sync: false,
initialize: ({ configuration, callSync }) => async decrypted => {
const kms = callSync('kms/instance')
const result = await kms.encrypt({
KeyId: configuration.awsKmsKeyArn,
Plaintext: new Buffer(decrypted)
}).promise()
return result.CiphertextBlob.toString('base64')
}
}
module.exports = {
sync: true,
initialize: ({ configuration }) => {
const kms = new AWS.KMS(configuration.aws.kms)
return () => kms
}
}
module.exports = {
sync: true,
initialize: ({ configuration }) => {
const words = {
stuff: configuration.maybeOverrideStuff || 'some default'
}
return key => words[key] || 'no such word'
}
}
module.exports = {
sync: false,
initialize: ({ call, callSync }) => async params => {
const word = callSync('words', params.theKey)
return {
stuff: await call('kms/encrypt-string', word)
}
}
}
const mannish = require('mannish')
const test = require('tape')
const { initialize } = require('./work')
test('something about the work provider', async t => {
const mediator = mannish()
mediator.provideSync('words', key => {
t.equal(key, 'bbq', 'uses correct key')
return 'sauce'
})
mediator.provide('kms/encrypt-string', async string => {
t.equal(string, 'sauce', 'uses value from sync provider')
return 'secret'
})
const output = await initialize(mediator)({ theKey: 'bbq' })
t.equal(output, { stuff: 'secret' }, 'validate output')
t.end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment