getconfig
getconfig copied to clipboard
implement some means of retrieving secret values
there is some desire to allow getconfig to retrieve values from somewhere other than a file on disk or environment variables, however this requires a bit of planning to come up with a reasonable solution since many things in node are by their very nature asynchronous and getconfig is intended to work synchronously.
some possible solutions:
- make getconfig asynchronous, this would be a new semver-major but would allow us to perform async actions without any additional work. instead of
const config = require('getconfig')
we could return a promise and haveconst config = await require('getconfig')
pros:
- minimal usage change
cons:
- node doesn't yet have top level
await
for commonjs (it does for esm modules) so there's some inconvenience factor - it would definitely be a breaking change
- use a built in worker thread and Atomics to run async actions and wait for their results
pros:
- can perform async actions (the desired effect) while appearing to still be synchronous
cons:
- requires built in implementation or working out a plugin system of some kind to allow multiple sources (or see 4 below)
- configuring external sources would require some additional syntax
- allow a value to be populated by an arbitrary external command
pros:
- would allow users to get a value in a very flexible way while maintaining synchronous loading thanks to spawnSync/execSync
cons:
- configuring a value to be read through an external command would require a reasonable way of expressing the desired command
- the entire command would have to be provided for every value coming from the given source
- (middle ground between 2 and 3) allow users to provide a javascript module within their project that retrieves a value however they choose, run their module in a worker thread and use atomics to wait for its result
pros:
- very flexible
- maintains overall synchronous nature
- non-breaking
cons:
- users would likely have to provide their own modules for data retrieval
- those modules will have to perform their own configuration somehow (who configures the config fetcher?)
I think I would lean towards the simplest option (which in my mind is the first). However I'm not sure I fully comprehend this disadvantage:
node doesn't yet have top level await for commonjs (it does for esm modules) so there's some inconvenience factor
Naive question alert: could you change the library to be async but provide a sync
method of calling it?
So usage could become something like:
// Get the config async:
const config = await require('getconfig').asyncConfig
// Get the config sync:
const config = require('getconfig').config
Maybe it could default to sync?
I assume that the complexities of the libraries inner workings means that this is not a feasible option but figured there's no harm asking 🙂