Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ console.log(await envParser.parse()) // { DB_PASSWORD: 'Value from file /run/sec

This can be useful when you are using secrets manager like `Docker Secret`, `HashiCorp Vault`, `Google Secrets Manager` and others to manage your secrets.

The parser also includes a built-in `base64` identifier. It decodes values using a Node.js `Buffer`.

```env
APP_KEY=base64:YWRvbmlzanMtcnVsZXM=
```

## Validating environment variables
Once you have the parsed objects, you can optionally validate them against a pre-defined schema. We recommend validation for the following reasons.

Expand Down
4 changes: 4 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export class EnvParser {
* Static collection of registered identifiers with their callbacks
*/
static #identifiers: Record<string, EnvIdentifierCallback> = {
base64(value) {
return Buffer.from(value, 'base64').toString('utf-8')
},

async file(value, key, appRoot) {
const filePath = new URL(value, appRoot)
try {
Expand Down
11 changes: 11 additions & 0 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ test.group('Env Parser', () => {
})
})

test('decode value using the base64 identifier', async ({ assert, fs, expectTypeOf }) => {
const envString = ['APP_KEY=base64:YWRvbmlzanMtcnVsZXM='].join('\n')
const parser = new EnvParser(envString, fs.baseUrl)
const parsed = await parser.parse()

expectTypeOf(parsed).toEqualTypeOf<NodeJS.Dict<string>>()
assert.deepEqual(parsed, {
APP_KEY: 'adonisjs-rules',
})
})

test('throw error when file is missing', async ({ fs, expectTypeOf }) => {
const envString = ['PACKAGE_FILE=file:./package.json'].join('\n')
const parser = new EnvParser(envString, fs.baseUrl)
Expand Down