@adonisjs/env 5.0.1 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -3
- package/build/index.js +67 -10
- package/build/index.js.map +1 -1
- package/build/src/env.d.ts +9 -0
- package/build/src/errors.d.ts +2 -0
- package/build/src/parser.d.ts +10 -1
- package/package.json +36 -25
package/README.md
CHANGED
|
@@ -12,8 +12,6 @@ Install the package from the npm packages registry as follows.
|
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
14
|
npm i @adonisjs/env
|
|
15
|
-
|
|
16
|
-
yarn add @adonisjs/env
|
|
17
15
|
```
|
|
18
16
|
|
|
19
17
|
## EnvLoader
|
|
@@ -52,7 +50,7 @@ const envParser = new EnvParser(`
|
|
|
52
50
|
HOST=localhost
|
|
53
51
|
`)
|
|
54
52
|
|
|
55
|
-
console.log(envParser.parse()) // { PORT: '3000', HOST: 'localhost' }
|
|
53
|
+
console.log(await envParser.parse()) // { PORT: '3000', HOST: 'localhost' }
|
|
56
54
|
```
|
|
57
55
|
|
|
58
56
|
The return value of `parser.parse` is an object with key-value pair. The parser also has support for interpolation.
|
|
@@ -63,6 +61,27 @@ By default, the parser prefers existing `process.env` values when they exist. Ho
|
|
|
63
61
|
new EnvParser(envContents, { ignoreProcessEnv: true })
|
|
64
62
|
```
|
|
65
63
|
|
|
64
|
+
### Identifier
|
|
65
|
+
|
|
66
|
+
You can define an "identifier" to be used for interpolation. The identifier is a string that prefix the environment variable value and let you customize the value resolution.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { readFile } from 'node:fs/promises'
|
|
70
|
+
import { EnvParser } from '@adonisjs/env'
|
|
71
|
+
|
|
72
|
+
EnvParser.identifier('file', (value) => {
|
|
73
|
+
return readFile(value, 'utf-8')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const envParser = new EnvParser(`
|
|
77
|
+
DB_PASSWORD=file:/run/secret/db_password
|
|
78
|
+
`)
|
|
79
|
+
|
|
80
|
+
console.log(await envParser.parse()) // { DB_PASSWORD: 'Value from file /run/secret/db_password' }
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This can be useful when you are using secrets manager like `Docker Secret`, `HashiCorp Vault`, `Google Secrets Manager` and others to manage your secrets.
|
|
84
|
+
|
|
66
85
|
## Validating environment variables
|
|
67
86
|
Once you have the parsed objects, you can optionally validate them against a pre-defined schema. We recommend validation for the following reasons.
|
|
68
87
|
|
package/build/index.js
CHANGED
|
@@ -10,14 +10,20 @@ import { schema as envSchema } from "@poppinss/validator-lite";
|
|
|
10
10
|
// src/errors.ts
|
|
11
11
|
var errors_exports = {};
|
|
12
12
|
__export(errors_exports, {
|
|
13
|
+
E_IDENTIFIER_ALREADY_DEFINED: () => E_IDENTIFIER_ALREADY_DEFINED,
|
|
13
14
|
E_INVALID_ENV_VARIABLES: () => E_INVALID_ENV_VARIABLES
|
|
14
15
|
});
|
|
15
|
-
import { Exception } from "@poppinss/utils";
|
|
16
|
+
import { createError, Exception } from "@poppinss/utils";
|
|
16
17
|
var E_INVALID_ENV_VARIABLES = class EnvValidationException extends Exception {
|
|
17
18
|
static message = "Validation failed for one or more environment variables";
|
|
18
19
|
static code = "E_INVALID_ENV_VARIABLES";
|
|
19
20
|
help = "";
|
|
20
21
|
};
|
|
22
|
+
var E_IDENTIFIER_ALREADY_DEFINED = createError(
|
|
23
|
+
'The identifier "%s" is already defined',
|
|
24
|
+
"E_IDENTIFIER_ALREADY_DEFINED",
|
|
25
|
+
500
|
|
26
|
+
);
|
|
21
27
|
|
|
22
28
|
// src/validator.ts
|
|
23
29
|
var EnvValidator = class {
|
|
@@ -58,15 +64,32 @@ var EnvValidator = class {
|
|
|
58
64
|
|
|
59
65
|
// src/parser.ts
|
|
60
66
|
import dotenv from "dotenv";
|
|
61
|
-
var EnvParser = class {
|
|
67
|
+
var EnvParser = class _EnvParser {
|
|
62
68
|
#envContents;
|
|
63
69
|
#preferProcessEnv = true;
|
|
70
|
+
static #identifiers = {};
|
|
64
71
|
constructor(envContents, options) {
|
|
65
72
|
if (options?.ignoreProcessEnv) {
|
|
66
73
|
this.#preferProcessEnv = false;
|
|
67
74
|
}
|
|
68
75
|
this.#envContents = envContents;
|
|
69
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
79
|
+
* when the value match the identifier to modify its interpolation.
|
|
80
|
+
*/
|
|
81
|
+
static identifier(name, callback) {
|
|
82
|
+
if (this.#identifiers[name]) {
|
|
83
|
+
throw new E_IDENTIFIER_ALREADY_DEFINED([name]);
|
|
84
|
+
}
|
|
85
|
+
this.#identifiers[name] = callback;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Remove an identifier
|
|
89
|
+
*/
|
|
90
|
+
static removeIdentifier(name) {
|
|
91
|
+
delete this.#identifiers[name];
|
|
92
|
+
}
|
|
70
93
|
/**
|
|
71
94
|
* Returns the value from the parsed object
|
|
72
95
|
*/
|
|
@@ -133,12 +156,32 @@ var EnvParser = class {
|
|
|
133
156
|
/**
|
|
134
157
|
* Parse the env string to an object of environment variables.
|
|
135
158
|
*/
|
|
136
|
-
parse() {
|
|
159
|
+
async parse() {
|
|
137
160
|
const envCollection = dotenv.parse(this.#envContents.trim());
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
161
|
+
const identifiers = Object.keys(_EnvParser.#identifiers);
|
|
162
|
+
let result = {};
|
|
163
|
+
$keyLoop:
|
|
164
|
+
for (const key in envCollection) {
|
|
165
|
+
const value = this.#getValue(key, envCollection);
|
|
166
|
+
if (value.includes(":")) {
|
|
167
|
+
for (const identifier of identifiers) {
|
|
168
|
+
if (value.startsWith(`${identifier}:`)) {
|
|
169
|
+
result[key] = await _EnvParser.#identifiers[identifier](
|
|
170
|
+
value.substring(identifier.length + 1)
|
|
171
|
+
);
|
|
172
|
+
continue $keyLoop;
|
|
173
|
+
}
|
|
174
|
+
if (value.startsWith(`${identifier}\\:`)) {
|
|
175
|
+
result[key] = identifier + value.substring(identifier.length + 1);
|
|
176
|
+
continue $keyLoop;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
result[key] = value;
|
|
180
|
+
} else {
|
|
181
|
+
result[key] = value;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
142
185
|
}
|
|
143
186
|
};
|
|
144
187
|
|
|
@@ -154,11 +197,12 @@ var EnvProcessor = class {
|
|
|
154
197
|
/**
|
|
155
198
|
* Parse env variables from raw contents
|
|
156
199
|
*/
|
|
157
|
-
#processContents(envContents, store) {
|
|
200
|
+
async #processContents(envContents, store) {
|
|
158
201
|
if (!envContents.trim()) {
|
|
159
202
|
return store;
|
|
160
203
|
}
|
|
161
|
-
const
|
|
204
|
+
const parser = new EnvParser(envContents);
|
|
205
|
+
const values = await parser.parse();
|
|
162
206
|
Object.keys(values).forEach((key) => {
|
|
163
207
|
let value = process.env[key];
|
|
164
208
|
if (!value) {
|
|
@@ -184,7 +228,7 @@ var EnvProcessor = class {
|
|
|
184
228
|
);
|
|
185
229
|
}
|
|
186
230
|
const envValues = {};
|
|
187
|
-
envFiles.
|
|
231
|
+
await Promise.all(envFiles.map(({ contents }) => this.#processContents(contents, envValues)));
|
|
188
232
|
return envValues;
|
|
189
233
|
}
|
|
190
234
|
/**
|
|
@@ -214,6 +258,19 @@ var Env = class _Env {
|
|
|
214
258
|
const validator = this.rules(schema);
|
|
215
259
|
return new _Env(validator.validate(values));
|
|
216
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
263
|
+
* when the value match the identifier to modify its interpolation.
|
|
264
|
+
*/
|
|
265
|
+
static identifier(name, callback) {
|
|
266
|
+
EnvParser.identifier(name, callback);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Remove an identifier
|
|
270
|
+
*/
|
|
271
|
+
static removeIdentifier(name) {
|
|
272
|
+
EnvParser.removeIdentifier(name);
|
|
273
|
+
}
|
|
217
274
|
/**
|
|
218
275
|
* The schema builder for defining validation rules
|
|
219
276
|
*/
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/env.ts","../src/errors.ts","../src/validator.ts","../src/parser.ts","../src/processor.ts"],"sourcesContent":["/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { schema as envSchema, type ValidateFn } from '@poppinss/validator-lite'\nimport { EnvValidator } from './validator.js'\nimport { EnvProcessor } from './processor.js'\n\n/**\n * A wrapper over \"process.env\" with types information.\n *\n * ```ts\n * const validate = Env.rules({\n * PORT: Env.schema.number()\n * })\n *\n * const validatedEnvVars = validate(process.env)\n *\n * const env = new EnvValues(validatedEnvVars)\n * env.get('PORT') // type === number\n * ```\n */\nexport class Env<EnvValues extends Record<string, any>> {\n /**\n * A cache of env values\n */\n #values: EnvValues\n\n constructor(values: EnvValues) {\n this.#values = values\n }\n\n /**\n * Create an instance of the env class by validating the\n * environment variables. Also, the `.env` files are\n * loaded from the appRoot\n */\n static async create<Schema extends { [key: string]: ValidateFn<unknown> }>(\n appRoot: URL,\n schema: Schema\n ): Promise<\n Env<{\n [K in keyof Schema]: ReturnType<Schema[K]>\n }>\n > {\n const values = await new EnvProcessor(appRoot).process()\n const validator = this.rules(schema)\n return new Env(validator.validate(values))\n }\n\n /**\n * The schema builder for defining validation rules\n */\n static schema = envSchema\n\n /**\n * Define the validation rules for validating environment\n * variables. The return value is an instance of the\n * env validator\n */\n static rules<T extends { [key: string]: ValidateFn<unknown> }>(schema: T): EnvValidator<T> {\n const validator = new EnvValidator<T>(schema)\n return validator\n }\n\n /**\n * Get the value of an environment variable by key. The values are\n * lookedup inside the validated environment and \"process.env\"\n * is used as a fallback.\n *\n * The second param is the default value, which is returned when\n * the environment variable does not exist.\n *\n * ```ts\n * Env.get('PORT')\n *\n * // With default value\n * Env.get('PORT', 3000)\n * ```\n */\n get<K extends keyof EnvValues>(key: K): EnvValues[K]\n get<K extends keyof EnvValues>(\n key: K,\n defaultValue: Exclude<EnvValues[K], undefined>\n ): Exclude<EnvValues[K], undefined>\n get(key: string): string | undefined\n get(key: string, defaultValue: string): string\n get(key: string, defaultValue?: any): any {\n /**\n * Return cached value\n */\n if (this.#values[key] !== undefined) {\n return this.#values[key]\n }\n\n /**\n * Get value from \"process.env\" and update the cache\n */\n const envValue = process.env[key]\n if (envValue) {\n return envValue\n }\n\n /**\n * Return default value when unable to lookup any other value\n */\n return defaultValue\n }\n\n /**\n * Update/set value of an environment variable.\n *\n * The value is not casted/validated using the validator, so make sure\n * to set the correct data type.\n *\n * ```ts\n * Env.set('PORT', 3000)\n *\n * Env.get('PORT') === 3000 // true\n * process.env.PORT === '3000' // true\n * ```\n */\n set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void\n set(key: string, value: string): void\n set(key: string | keyof EnvValues, value: any): void {\n this.#values[key] = value\n process.env[key as string] = value\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { Exception } from '@poppinss/utils'\n\n/**\n * Exception raised when one or more env variables\n * are invalid\n */\nexport const E_INVALID_ENV_VARIABLES = class EnvValidationException extends Exception {\n static message = 'Validation failed for one or more environment variables'\n static code = 'E_INVALID_ENV_VARIABLES'\n help: string = ''\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { Exception } from '@poppinss/utils'\nimport { ValidateFn } from '@poppinss/validator-lite'\n\nimport { E_INVALID_ENV_VARIABLES } from './errors.js'\n\n/**\n * Exposes the API to validate environment variables against a\n * pre-defined schema.\n *\n * The class is not exported in the main API and used internally.\n */\nexport class EnvValidator<Schema extends { [key: string]: ValidateFn<unknown> }> {\n #schema: Schema\n #error: Exception\n\n constructor(schema: Schema) {\n this.#schema = schema\n this.#error = new E_INVALID_ENV_VARIABLES()\n }\n\n /**\n * Accepts an object of values to validate against the pre-defined\n * schema.\n *\n * The return value is a merged copy of the original object and the\n * values mutated by the schema validator.\n */\n validate(values: { [K: string]: string | undefined }): {\n [K in keyof Schema]: ReturnType<Schema[K]>\n } {\n const help: string[] = []\n\n const validated = Object.keys(this.#schema).reduce(\n (result, key) => {\n const value = process.env[key] || values[key]\n\n try {\n result[key] = this.#schema[key](key, value) as any\n } catch (error) {\n help.push(`- ${error.message}`)\n }\n return result\n },\n { ...values }\n ) as { [K in keyof Schema]: ReturnType<Schema[K]> }\n\n if (help.length) {\n this.#error.help = help.join('\\n')\n throw this.#error\n }\n\n return validated\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport dotenv, { DotenvParseOutput } from 'dotenv'\n\n/**\n * Env parser parses the environment variables from a string formatted\n * as a key-value pair seperated using an `=`. For example:\n *\n * ```dotenv\n * PORT=3333\n * HOST=127.0.0.1\n * ```\n *\n * The variables can reference other environment variables as well using `$`.\n * For example:\n *\n * ```dotenv\n * PORT=3333\n * REDIS_PORT=$PORT\n * ```\n *\n * The variables using characters other than letters can wrap variable\n * named inside a curly brace.\n *\n * ```dotenv\n * APP-PORT=3333\n * REDIS_PORT=${APP-PORT}\n * ```\n *\n * You can escape the `$` sign with a backtick.\n *\n * ```dotenv\n * REDIS_PASSWORD=foo\\$123\n * ```\n *\n * ## Usage\n *\n * ```ts\n * const parser = new EnvParser(envContents)\n * const output = parser.parse()\n *\n * // The output is a key-value pair\n * ```\n */\nexport class EnvParser {\n #envContents: string\n #preferProcessEnv: boolean = true\n\n constructor(envContents: string, options?: { ignoreProcessEnv: boolean }) {\n if (options?.ignoreProcessEnv) {\n this.#preferProcessEnv = false\n }\n\n this.#envContents = envContents\n }\n\n /**\n * Returns the value from the parsed object\n */\n #getValue(key: string, parsed: DotenvParseOutput): string {\n if (this.#preferProcessEnv && process.env[key]) {\n return process.env[key]!\n }\n\n if (parsed[key]) {\n return this.#interpolate(parsed[key], parsed)\n }\n\n return process.env[key] || ''\n }\n\n /**\n * Interpolating the token wrapped inside the mustache braces.\n */\n #interpolateMustache(token: string, parsed: DotenvParseOutput) {\n /**\n * Finding the closing brace. If closing brace is missing, we\n * consider the block as a normal string\n */\n const closingBrace = token.indexOf('}')\n if (closingBrace === -1) {\n return token\n }\n\n /**\n * Then we pull everything until the closing brace, except\n * the opening brace and trim off all white spaces.\n */\n const varReference = token.slice(1, closingBrace).trim()\n\n /**\n * Getting the value of the reference inside the braces\n */\n return `${this.#getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`\n }\n\n /**\n * Interpolating the variable reference starting with a\n * `$`. We only capture numbers,letter and underscore.\n * For other characters, one can use the mustache\n * braces.\n */\n #interpolateVariable(token: string, parsed: any) {\n return token.replace(/[a-zA-Z0-9_]+/, (key) => {\n return this.#getValue(key, parsed)\n })\n }\n\n /**\n * Interpolates the referenced values\n */\n #interpolate(value: string, parsed: DotenvParseOutput): string {\n const tokens = value.split('$')\n\n let newValue = ''\n let skipNextToken = true\n\n tokens.forEach((token) => {\n /**\n * If the value is an escaped sequence, then we replace it\n * with a `$` and then skip the next token.\n */\n if (token === '\\\\') {\n newValue += '$'\n skipNextToken = true\n return\n }\n\n /**\n * Use the value as it is when \"skipNextToken\" is set to true.\n */\n if (skipNextToken) {\n /**\n * Replace the ending escape sequence with a $\n */\n newValue += token.replace(/\\\\$/, '$')\n /**\n * and then skip the next token if it ends with escape sequence\n */\n if (token.endsWith('\\\\')) {\n return\n }\n } else {\n /**\n * Handle mustache block\n */\n if (token.startsWith('{')) {\n newValue += this.#interpolateMustache(token, parsed)\n return\n }\n\n /**\n * Process all words as variable\n */\n newValue += this.#interpolateVariable(token, parsed)\n }\n\n /**\n * Process next token\n */\n skipNextToken = false\n })\n\n return newValue\n }\n\n /**\n * Parse the env string to an object of environment variables.\n */\n parse(): DotenvParseOutput {\n const envCollection = dotenv.parse(this.#envContents.trim())\n\n return Object.keys(envCollection).reduce<DotenvParseOutput>((result, key) => {\n result[key] = this.#getValue(key, envCollection)\n return result\n }, {})\n }\n}\n","/*\n * @adonisjs/application\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport debug from './debug.js'\nimport { EnvParser } from './parser.js'\nimport { EnvLoader } from './loader.js'\n\n/**\n * Env processors loads, parses and process environment variables.\n */\nexport class EnvProcessor {\n /**\n * App root is needed to load files\n */\n #appRoot: URL\n\n constructor(appRoot: URL) {\n this.#appRoot = appRoot\n }\n\n /**\n * Parse env variables from raw contents\n */\n #processContents(envContents: string, store: Record<string, any>) {\n /**\n * Collected env variables\n */\n if (!envContents.trim()) {\n return store\n }\n\n const values = new EnvParser(envContents).parse()\n Object.keys(values).forEach((key) => {\n let value = process.env[key]\n\n if (!value) {\n value = values[key]\n process.env[key] = values[key]\n }\n\n if (!store[key]) {\n store[key] = value\n }\n })\n\n return store\n }\n\n /**\n * Parse env variables by loading dot files.\n */\n async #loadAndProcessDotFiles() {\n const loader = new EnvLoader(this.#appRoot)\n const envFiles = await loader.load()\n\n if (debug.enabled) {\n debug(\n 'processing .env files (priority from top to bottom) %O',\n envFiles.map((file) => file.path)\n )\n }\n\n /**\n * Collected env variables\n */\n const envValues: Record<string, any> = {}\n envFiles.forEach(({ contents }) => this.#processContents(contents, envValues))\n return envValues\n }\n\n /**\n * Process env variables\n */\n async process() {\n return this.#loadAndProcessDotFiles()\n }\n}\n"],"mappings":";;;;;;;AASA,SAAS,UAAU,iBAAkC;;;ACTrD;AAAA;AAAA;AAAA;AASA,SAAS,iBAAiB;AAMnB,IAAM,0BAA0B,MAAM,+BAA+B,UAAU;AAAA,EACpF,OAAO,UAAU;AAAA,EACjB,OAAO,OAAO;AAAA,EACd,OAAe;AACjB;;;ACCO,IAAM,eAAN,MAA0E;AAAA,EA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I,aAAa,OAAO,EAAE,QAAQ;AACvD,UAAM,YAAY,KAAK,MAAM,MAAM;AACnC,WAAO,IAAI,KAAI,UAAU,SAAS,MAAM,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhB,OAAO,MAAwD,QAA4B;AACzF,UAAM,YAAY,IAAI,aAAgB,MAAM;AAC5C,WAAO;AAAA,EACT;AAAA,EAwBA,IAAI,KAAa,cAAyB;AAIxC,QAAI,KAAK,QAAQ,GAAG,MAAM,QAAW;AACnC,aAAO,KAAK,QAAQ,GAAG;AAAA,IACzB;AAKA,UAAM,WAAW,QAAQ,IAAI,GAAG;AAChC,QAAI,UAAU;AACZ,aAAO;AAAA,IACT;AAKA,WAAO;AAAA,EACT;AAAA,EAiBA,IAAI,KAA+B,OAAkB;AACnD,SAAK,QAAQ,GAAG,IAAI;AACpB,YAAQ,IAAI,GAAa,IAAI;AAAA,EAC/B;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/env.ts","../src/errors.ts","../src/validator.ts","../src/parser.ts","../src/processor.ts"],"sourcesContent":["/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { schema as envSchema, type ValidateFn } from '@poppinss/validator-lite'\nimport { EnvValidator } from './validator.js'\nimport { EnvProcessor } from './processor.js'\nimport { EnvParser } from './parser.js'\n\n/**\n * A wrapper over \"process.env\" with types information.\n *\n * ```ts\n * const validate = Env.rules({\n * PORT: Env.schema.number()\n * })\n *\n * const validatedEnvVars = validate(process.env)\n *\n * const env = new EnvValues(validatedEnvVars)\n * env.get('PORT') // type === number\n * ```\n */\nexport class Env<EnvValues extends Record<string, any>> {\n /**\n * A cache of env values\n */\n #values: EnvValues\n\n constructor(values: EnvValues) {\n this.#values = values\n }\n\n /**\n * Create an instance of the env class by validating the\n * environment variables. Also, the `.env` files are\n * loaded from the appRoot\n */\n static async create<Schema extends { [key: string]: ValidateFn<unknown> }>(\n appRoot: URL,\n schema: Schema\n ): Promise<\n Env<{\n [K in keyof Schema]: ReturnType<Schema[K]>\n }>\n > {\n const values = await new EnvProcessor(appRoot).process()\n const validator = this.rules(schema)\n return new Env(validator.validate(values))\n }\n\n /**\n * Define an identifier for any environment value. The callback is invoked\n * when the value match the identifier to modify its interpolation.\n */\n static identifier(name: string, callback: (value: string) => Promise<string> | string): void {\n EnvParser.identifier(name, callback)\n }\n\n /**\n * Remove an identifier\n */\n static removeIdentifier(name: string): void {\n EnvParser.removeIdentifier(name)\n }\n\n /**\n * The schema builder for defining validation rules\n */\n static schema = envSchema\n\n /**\n * Define the validation rules for validating environment\n * variables. The return value is an instance of the\n * env validator\n */\n static rules<T extends { [key: string]: ValidateFn<unknown> }>(schema: T): EnvValidator<T> {\n const validator = new EnvValidator<T>(schema)\n return validator\n }\n\n /**\n * Get the value of an environment variable by key. The values are\n * lookedup inside the validated environment and \"process.env\"\n * is used as a fallback.\n *\n * The second param is the default value, which is returned when\n * the environment variable does not exist.\n *\n * ```ts\n * Env.get('PORT')\n *\n * // With default value\n * Env.get('PORT', 3000)\n * ```\n */\n get<K extends keyof EnvValues>(key: K): EnvValues[K]\n get<K extends keyof EnvValues>(\n key: K,\n defaultValue: Exclude<EnvValues[K], undefined>\n ): Exclude<EnvValues[K], undefined>\n get(key: string): string | undefined\n get(key: string, defaultValue: string): string\n get(key: string, defaultValue?: any): any {\n /**\n * Return cached value\n */\n if (this.#values[key] !== undefined) {\n return this.#values[key]\n }\n\n /**\n * Get value from \"process.env\" and update the cache\n */\n const envValue = process.env[key]\n if (envValue) {\n return envValue\n }\n\n /**\n * Return default value when unable to lookup any other value\n */\n return defaultValue\n }\n\n /**\n * Update/set value of an environment variable.\n *\n * The value is not casted/validated using the validator, so make sure\n * to set the correct data type.\n *\n * ```ts\n * Env.set('PORT', 3000)\n *\n * Env.get('PORT') === 3000 // true\n * process.env.PORT === '3000' // true\n * ```\n */\n set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void\n set(key: string, value: string): void\n set(key: string | keyof EnvValues, value: any): void {\n this.#values[key] = value\n process.env[key as string] = value\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { createError, Exception } from '@poppinss/utils'\n\n/**\n * Exception raised when one or more env variables\n * are invalid\n */\nexport const E_INVALID_ENV_VARIABLES = class EnvValidationException extends Exception {\n static message = 'Validation failed for one or more environment variables'\n static code = 'E_INVALID_ENV_VARIABLES'\n help: string = ''\n}\n\nexport const E_IDENTIFIER_ALREADY_DEFINED = createError<[string]>(\n 'The identifier \"%s\" is already defined',\n 'E_IDENTIFIER_ALREADY_DEFINED',\n 500\n)\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { Exception } from '@poppinss/utils'\nimport { ValidateFn } from '@poppinss/validator-lite'\n\nimport { E_INVALID_ENV_VARIABLES } from './errors.js'\n\n/**\n * Exposes the API to validate environment variables against a\n * pre-defined schema.\n *\n * The class is not exported in the main API and used internally.\n */\nexport class EnvValidator<Schema extends { [key: string]: ValidateFn<unknown> }> {\n #schema: Schema\n #error: Exception\n\n constructor(schema: Schema) {\n this.#schema = schema\n this.#error = new E_INVALID_ENV_VARIABLES()\n }\n\n /**\n * Accepts an object of values to validate against the pre-defined\n * schema.\n *\n * The return value is a merged copy of the original object and the\n * values mutated by the schema validator.\n */\n validate(values: { [K: string]: string | undefined }): {\n [K in keyof Schema]: ReturnType<Schema[K]>\n } {\n const help: string[] = []\n\n const validated = Object.keys(this.#schema).reduce(\n (result, key) => {\n const value = process.env[key] || values[key]\n\n try {\n result[key] = this.#schema[key](key, value) as any\n } catch (error) {\n help.push(`- ${error.message}`)\n }\n return result\n },\n { ...values }\n ) as { [K in keyof Schema]: ReturnType<Schema[K]> }\n\n if (help.length) {\n this.#error.help = help.join('\\n')\n throw this.#error\n }\n\n return validated\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport dotenv, { DotenvParseOutput } from 'dotenv'\nimport { E_IDENTIFIER_ALREADY_DEFINED } from './errors.js'\n\n/**\n * Env parser parses the environment variables from a string formatted\n * as a key-value pair seperated using an `=`. For example:\n *\n * ```dotenv\n * PORT=3333\n * HOST=127.0.0.1\n * ```\n *\n * The variables can reference other environment variables as well using `$`.\n * For example:\n *\n * ```dotenv\n * PORT=3333\n * REDIS_PORT=$PORT\n * ```\n *\n * The variables using characters other than letters can wrap variable\n * named inside a curly brace.\n *\n * ```dotenv\n * APP-PORT=3333\n * REDIS_PORT=${APP-PORT}\n * ```\n *\n * You can escape the `$` sign with a backtick.\n *\n * ```dotenv\n * REDIS_PASSWORD=foo\\$123\n * ```\n *\n * ## Usage\n *\n * ```ts\n * const parser = new EnvParser(envContents)\n * const output = parser.parse()\n *\n * // The output is a key-value pair\n * ```\n */\nexport class EnvParser {\n #envContents: string\n #preferProcessEnv: boolean = true\n static #identifiers: Record<string, (value: string) => Promise<string> | string> = {}\n\n constructor(envContents: string, options?: { ignoreProcessEnv: boolean }) {\n if (options?.ignoreProcessEnv) {\n this.#preferProcessEnv = false\n }\n\n this.#envContents = envContents\n }\n\n /**\n * Define an identifier for any environment value. The callback is invoked\n * when the value match the identifier to modify its interpolation.\n */\n static identifier(name: string, callback: (value: string) => Promise<string> | string): void {\n if (this.#identifiers[name]) {\n throw new E_IDENTIFIER_ALREADY_DEFINED([name])\n }\n\n this.#identifiers[name] = callback\n }\n\n /**\n * Remove an identifier\n */\n static removeIdentifier(name: string): void {\n delete this.#identifiers[name]\n }\n\n /**\n * Returns the value from the parsed object\n */\n #getValue(key: string, parsed: DotenvParseOutput): string {\n if (this.#preferProcessEnv && process.env[key]) {\n return process.env[key]!\n }\n\n if (parsed[key]) {\n return this.#interpolate(parsed[key], parsed)\n }\n\n return process.env[key] || ''\n }\n\n /**\n * Interpolating the token wrapped inside the mustache braces.\n */\n #interpolateMustache(token: string, parsed: DotenvParseOutput) {\n /**\n * Finding the closing brace. If closing brace is missing, we\n * consider the block as a normal string\n */\n const closingBrace = token.indexOf('}')\n if (closingBrace === -1) {\n return token\n }\n\n /**\n * Then we pull everything until the closing brace, except\n * the opening brace and trim off all white spaces.\n */\n const varReference = token.slice(1, closingBrace).trim()\n\n /**\n * Getting the value of the reference inside the braces\n */\n return `${this.#getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`\n }\n\n /**\n * Interpolating the variable reference starting with a\n * `$`. We only capture numbers,letter and underscore.\n * For other characters, one can use the mustache\n * braces.\n */\n #interpolateVariable(token: string, parsed: any) {\n return token.replace(/[a-zA-Z0-9_]+/, (key) => {\n return this.#getValue(key, parsed)\n })\n }\n\n /**\n * Interpolates the referenced values\n */\n #interpolate(value: string, parsed: DotenvParseOutput): string {\n const tokens = value.split('$')\n\n let newValue = ''\n let skipNextToken = true\n\n tokens.forEach((token) => {\n /**\n * If the value is an escaped sequence, then we replace it\n * with a `$` and then skip the next token.\n */\n if (token === '\\\\') {\n newValue += '$'\n skipNextToken = true\n return\n }\n\n /**\n * Use the value as it is when \"skipNextToken\" is set to true.\n */\n if (skipNextToken) {\n /**\n * Replace the ending escape sequence with a $\n */\n newValue += token.replace(/\\\\$/, '$')\n /**\n * and then skip the next token if it ends with escape sequence\n */\n if (token.endsWith('\\\\')) {\n return\n }\n } else {\n /**\n * Handle mustache block\n */\n if (token.startsWith('{')) {\n newValue += this.#interpolateMustache(token, parsed)\n return\n }\n\n /**\n * Process all words as variable\n */\n newValue += this.#interpolateVariable(token, parsed)\n }\n\n /**\n * Process next token\n */\n skipNextToken = false\n })\n\n return newValue\n }\n\n /**\n * Parse the env string to an object of environment variables.\n */\n async parse(): Promise<DotenvParseOutput> {\n const envCollection = dotenv.parse(this.#envContents.trim())\n const identifiers = Object.keys(EnvParser.#identifiers)\n let result: DotenvParseOutput = {}\n\n $keyLoop: for (const key in envCollection) {\n const value = this.#getValue(key, envCollection)\n\n if (value.includes(':')) {\n for (const identifier of identifiers) {\n if (value.startsWith(`${identifier}:`)) {\n result[key] = await EnvParser.#identifiers[identifier](\n value.substring(identifier.length + 1)\n )\n\n continue $keyLoop\n }\n\n if (value.startsWith(`${identifier}\\\\:`)) {\n result[key] = identifier + value.substring(identifier.length + 1)\n\n continue $keyLoop\n }\n }\n\n result[key] = value\n } else {\n result[key] = value\n }\n }\n\n return result\n }\n}\n","/*\n * @adonisjs/application\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport debug from './debug.js'\nimport { EnvParser } from './parser.js'\nimport { EnvLoader } from './loader.js'\n\n/**\n * Env processors loads, parses and process environment variables.\n */\nexport class EnvProcessor {\n /**\n * App root is needed to load files\n */\n #appRoot: URL\n\n constructor(appRoot: URL) {\n this.#appRoot = appRoot\n }\n\n /**\n * Parse env variables from raw contents\n */\n async #processContents(envContents: string, store: Record<string, any>) {\n /**\n * Collected env variables\n */\n if (!envContents.trim()) {\n return store\n }\n\n const parser = new EnvParser(envContents)\n const values = await parser.parse()\n\n Object.keys(values).forEach((key) => {\n let value = process.env[key]\n\n if (!value) {\n value = values[key]\n process.env[key] = values[key]\n }\n\n if (!store[key]) {\n store[key] = value\n }\n })\n\n return store\n }\n\n /**\n * Parse env variables by loading dot files.\n */\n async #loadAndProcessDotFiles() {\n const loader = new EnvLoader(this.#appRoot)\n const envFiles = await loader.load()\n\n if (debug.enabled) {\n debug(\n 'processing .env files (priority from top to bottom) %O',\n envFiles.map((file) => file.path)\n )\n }\n\n /**\n * Collected env variables\n */\n const envValues: Record<string, any> = {}\n await Promise.all(envFiles.map(({ contents }) => this.#processContents(contents, envValues)))\n return envValues\n }\n\n /**\n * Process env variables\n */\n async process() {\n return this.#loadAndProcessDotFiles()\n }\n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AAK,MAAM,MAAM;AACnC,WAAO,IAAI,KAAI,UAAU,SAAS,MAAM,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,WAAW,MAAc,UAA6D;AAC3F,cAAU,WAAW,MAAM,QAAQ;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,iBAAiB,MAAoB;AAC1C,cAAU,iBAAiB,IAAI;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhB,OAAO,MAAwD,QAA4B;AACzF,UAAM,YAAY,IAAI,aAAgB,MAAM;AAC5C,WAAO;AAAA,EACT;AAAA,EAwBA,IAAI,KAAa,cAAyB;AAIxC,QAAI,KAAK,QAAQ,GAAG,MAAM,QAAW;AACnC,aAAO,KAAK,QAAQ,GAAG;AAAA,IACzB;AAKA,UAAM,WAAW,QAAQ,IAAI,GAAG;AAChC,QAAI,UAAU;AACZ,aAAO;AAAA,IACT;AAKA,WAAO;AAAA,EACT;AAAA,EAiBA,IAAI,KAA+B,OAAkB;AACnD,SAAK,QAAQ,GAAG,IAAI;AACpB,YAAQ,IAAI,GAAa,IAAI;AAAA,EAC/B;AACF;","names":[]}
|
package/build/src/env.d.ts
CHANGED
|
@@ -28,6 +28,15 @@ export declare class Env<EnvValues extends Record<string, any>> {
|
|
|
28
28
|
}>(appRoot: URL, schema: Schema): Promise<Env<{
|
|
29
29
|
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
30
30
|
}>>;
|
|
31
|
+
/**
|
|
32
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
33
|
+
* when the value match the identifier to modify its interpolation.
|
|
34
|
+
*/
|
|
35
|
+
static identifier(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Remove an identifier
|
|
38
|
+
*/
|
|
39
|
+
static removeIdentifier(name: string): void;
|
|
31
40
|
/**
|
|
32
41
|
* The schema builder for defining validation rules
|
|
33
42
|
*/
|
package/build/src/errors.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Exception } from '@poppinss/utils';
|
|
2
3
|
/**
|
|
3
4
|
* Exception raised when one or more env variables
|
|
4
5
|
* are invalid
|
|
@@ -26,3 +27,4 @@ export declare const E_INVALID_ENV_VARIABLES: {
|
|
|
26
27
|
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
27
28
|
stackTraceLimit: number;
|
|
28
29
|
};
|
|
30
|
+
export declare const E_IDENTIFIER_ALREADY_DEFINED: new (args: [string], options?: ErrorOptions | undefined) => Exception;
|
package/build/src/parser.d.ts
CHANGED
|
@@ -44,8 +44,17 @@ export declare class EnvParser {
|
|
|
44
44
|
constructor(envContents: string, options?: {
|
|
45
45
|
ignoreProcessEnv: boolean;
|
|
46
46
|
});
|
|
47
|
+
/**
|
|
48
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
49
|
+
* when the value match the identifier to modify its interpolation.
|
|
50
|
+
*/
|
|
51
|
+
static identifier(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Remove an identifier
|
|
54
|
+
*/
|
|
55
|
+
static removeIdentifier(name: string): void;
|
|
47
56
|
/**
|
|
48
57
|
* Parse the env string to an object of environment variables.
|
|
49
58
|
*/
|
|
50
|
-
parse(): DotenvParseOutput
|
|
59
|
+
parse(): Promise<DotenvParseOutput>;
|
|
51
60
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/env",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Environment variable manager for Node.js",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"precompile": "npm run lint && npm run clean",
|
|
26
26
|
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
|
|
27
27
|
"build": "npm run compile",
|
|
28
|
-
"release": "
|
|
28
|
+
"release": "release-it",
|
|
29
29
|
"version": "npm run build",
|
|
30
30
|
"prepublishOnly": "npm run build",
|
|
31
31
|
"lint": "eslint . --ext=.ts",
|
|
@@ -40,33 +40,33 @@
|
|
|
40
40
|
"author": "virk,adonisjs",
|
|
41
41
|
"license": "MIT",
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@adonisjs/eslint-config": "^1.
|
|
44
|
-
"@adonisjs/prettier-config": "^1.
|
|
45
|
-
"@adonisjs/tsconfig": "^1.
|
|
46
|
-
"@commitlint/cli": "^
|
|
47
|
-
"@commitlint/config-conventional": "^
|
|
48
|
-
"@japa/assert": "^
|
|
49
|
-
"@japa/expect-type": "^2.0.
|
|
50
|
-
"@japa/file-system": "^2.
|
|
51
|
-
"@japa/runner": "^3.1.
|
|
52
|
-
"@swc/core": "^1.
|
|
53
|
-
"@types/node": "^20.
|
|
43
|
+
"@adonisjs/eslint-config": "^1.3.0",
|
|
44
|
+
"@adonisjs/prettier-config": "^1.3.0",
|
|
45
|
+
"@adonisjs/tsconfig": "^1.3.0",
|
|
46
|
+
"@commitlint/cli": "^19.2.2",
|
|
47
|
+
"@commitlint/config-conventional": "^19.2.2",
|
|
48
|
+
"@japa/assert": "^3.0.0",
|
|
49
|
+
"@japa/expect-type": "^2.0.2",
|
|
50
|
+
"@japa/file-system": "^2.3.0",
|
|
51
|
+
"@japa/runner": "^3.1.4",
|
|
52
|
+
"@swc/core": "^1.4.16",
|
|
53
|
+
"@types/node": "^20.12.7",
|
|
54
54
|
"c8": "^9.1.0",
|
|
55
55
|
"cross-env": "^7.0.3",
|
|
56
56
|
"del-cli": "^5.1.0",
|
|
57
57
|
"eslint": "^8.56.0",
|
|
58
58
|
"github-label-sync": "^2.3.1",
|
|
59
|
-
"husky": "^
|
|
60
|
-
"
|
|
61
|
-
"
|
|
59
|
+
"husky": "^9.0.11",
|
|
60
|
+
"prettier": "^3.2.5",
|
|
61
|
+
"release-it": "^17.2.0",
|
|
62
62
|
"ts-node": "^10.9.2",
|
|
63
|
-
"tsup": "^8.0.
|
|
64
|
-
"typescript": "^5.
|
|
63
|
+
"tsup": "^8.0.2",
|
|
64
|
+
"typescript": "^5.4.5"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@poppinss/utils": "^6.7.
|
|
67
|
+
"@poppinss/utils": "^6.7.3",
|
|
68
68
|
"@poppinss/validator-lite": "^1.0.3",
|
|
69
|
-
"dotenv": "^16.
|
|
69
|
+
"dotenv": "^16.4.5",
|
|
70
70
|
"split-lines": "^3.0.0"
|
|
71
71
|
},
|
|
72
72
|
"repository": {
|
|
@@ -86,11 +86,22 @@
|
|
|
86
86
|
"access": "public",
|
|
87
87
|
"tag": "latest"
|
|
88
88
|
},
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
"release-it": {
|
|
90
|
+
"git": {
|
|
91
|
+
"commitMessage": "chore(release): ${version}",
|
|
92
|
+
"tagAnnotation": "v${version}",
|
|
93
|
+
"tagName": "v${version}"
|
|
94
|
+
},
|
|
95
|
+
"hooks": {
|
|
96
|
+
"before:init": [
|
|
97
|
+
"npm test"
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
"github": {
|
|
101
|
+
"release": true,
|
|
102
|
+
"releaseName": "v${version}",
|
|
103
|
+
"web": true
|
|
104
|
+
}
|
|
94
105
|
},
|
|
95
106
|
"c8": {
|
|
96
107
|
"reporter": [
|