@adonisjs/env 4.2.0-4 → 4.2.0-6

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.
@@ -1,166 +0,0 @@
1
- /*
2
- * @adonisjs/env
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import dotenv from 'dotenv';
10
- /**
11
- * Env parser parses the environment variables from a string formatted
12
- * as a key-value pair seperated using an `=`. For example:
13
- *
14
- * ```dotenv
15
- * PORT=3333
16
- * HOST=127.0.0.1
17
- * ```
18
- *
19
- * The variables can reference other environment variables as well using `$`.
20
- * For example:
21
- *
22
- * ```dotenv
23
- * PORT=3333
24
- * REDIS_PORT=$PORT
25
- * ```
26
- *
27
- * The variables using characters other than letters can wrap variable
28
- * named inside a curly brace.
29
- *
30
- * ```dotenv
31
- * APP-PORT=3333
32
- * REDIS_PORT=${APP-PORT}
33
- * ```
34
- *
35
- * You can escape the `$` sign with a backtick.
36
- *
37
- * ```dotenv
38
- * REDIS_PASSWORD=foo\$123
39
- * ```
40
- *
41
- * ## Usage
42
- *
43
- * ```ts
44
- * const parser = new EnvParser(envContents)
45
- * const output = parser.parse()
46
- *
47
- * // The output is a key-value pair
48
- * ```
49
- */
50
- export class EnvParser {
51
- #envContents;
52
- #preferProcessEnv = true;
53
- constructor(envContents, options) {
54
- if (options?.ignoreProcessEnv) {
55
- this.#preferProcessEnv = false;
56
- }
57
- this.#envContents = envContents;
58
- }
59
- /**
60
- * Returns the value from the parsed object
61
- */
62
- #getValue(key, parsed) {
63
- if (this.#preferProcessEnv && process.env[key]) {
64
- return process.env[key];
65
- }
66
- if (parsed[key]) {
67
- return this.#interpolate(parsed[key], parsed);
68
- }
69
- return process.env[key] || '';
70
- }
71
- /**
72
- * Interpolating the token wrapped inside the mustache braces.
73
- */
74
- #interpolateMustache(token, parsed) {
75
- /**
76
- * Finding the closing brace. If closing brace is missing, we
77
- * consider the block as a normal string
78
- */
79
- const closingBrace = token.indexOf('}');
80
- if (closingBrace === -1) {
81
- return token;
82
- }
83
- /**
84
- * Then we pull everything until the closing brace, except
85
- * the opening brace and trim off all white spaces.
86
- */
87
- const varReference = token.slice(1, closingBrace).trim();
88
- /**
89
- * Getting the value of the reference inside the braces
90
- */
91
- return `${this.#getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`;
92
- }
93
- /**
94
- * Interpolating the variable reference starting with a
95
- * `$`. We only capture numbers,letter and underscore.
96
- * For other characters, one can use the mustache
97
- * braces.
98
- */
99
- #interpolateVariable(token, parsed) {
100
- return token.replace(/[a-zA-Z0-9_]+/, (key) => {
101
- return this.#getValue(key, parsed);
102
- });
103
- }
104
- /**
105
- * Interpolates the referenced values
106
- */
107
- #interpolate(value, parsed) {
108
- const tokens = value.split('$');
109
- let newValue = '';
110
- let skipNextToken = true;
111
- tokens.forEach((token) => {
112
- /**
113
- * If the value is an escaped sequence, then we replace it
114
- * with a `$` and then skip the next token.
115
- */
116
- if (token === '\\') {
117
- newValue += '$';
118
- skipNextToken = true;
119
- return;
120
- }
121
- /**
122
- * Use the value as it is when "skipNextToken" is set to true.
123
- */
124
- if (skipNextToken) {
125
- /**
126
- * Replace the ending escape sequence with a $
127
- */
128
- newValue += token.replace(/\\$/, '$');
129
- /**
130
- * and then skip the next token if it ends with escape sequence
131
- */
132
- if (token.endsWith('\\')) {
133
- return;
134
- }
135
- }
136
- else {
137
- /**
138
- * Handle mustache block
139
- */
140
- if (token.startsWith('{')) {
141
- newValue += this.#interpolateMustache(token, parsed);
142
- return;
143
- }
144
- /**
145
- * Process all words as variable
146
- */
147
- newValue += this.#interpolateVariable(token, parsed);
148
- }
149
- /**
150
- * Process next token
151
- */
152
- skipNextToken = false;
153
- });
154
- return newValue;
155
- }
156
- /**
157
- * Parse the env string to an object of environment variables.
158
- */
159
- parse() {
160
- const envCollection = dotenv.parse(this.#envContents.trim());
161
- return Object.keys(envCollection).reduce((result, key) => {
162
- result[key] = this.#getValue(key, envCollection);
163
- return result;
164
- }, {});
165
- }
166
- }
@@ -1,12 +0,0 @@
1
- /// <reference types="@types/node" resolution-mode="require"/>
2
- /**
3
- * Env processors loads, parses and process environment variables.
4
- */
5
- export declare class EnvProcessor {
6
- #private;
7
- constructor(appRoot: URL);
8
- /**
9
- * Process env variables
10
- */
11
- process(): Promise<Record<string, any>>;
12
- }
@@ -1,68 +0,0 @@
1
- /*
2
- * @adonisjs/application
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import debug from './debug.js';
10
- import { EnvParser } from './parser.js';
11
- import { EnvLoader } from './loader.js';
12
- /**
13
- * Env processors loads, parses and process environment variables.
14
- */
15
- export class EnvProcessor {
16
- /**
17
- * App root is needed to load files
18
- */
19
- #appRoot;
20
- constructor(appRoot) {
21
- this.#appRoot = appRoot;
22
- }
23
- /**
24
- * Parse env variables from raw contents
25
- */
26
- #processContents(envContents, store) {
27
- /**
28
- * Collected env variables
29
- */
30
- if (!envContents.trim()) {
31
- return store;
32
- }
33
- const values = new EnvParser(envContents).parse();
34
- Object.keys(values).forEach((key) => {
35
- let value = process.env[key];
36
- if (!value) {
37
- value = values[key];
38
- process.env[key] = values[key];
39
- }
40
- if (!store[key]) {
41
- store[key] = value;
42
- }
43
- });
44
- return store;
45
- }
46
- /**
47
- * Parse env variables by loading dot files.
48
- */
49
- async #loadAndProcessDotFiles() {
50
- const loader = new EnvLoader(this.#appRoot);
51
- const envFiles = await loader.load();
52
- if (debug.enabled) {
53
- debug('processing .env files (priority from top to bottom) %O', envFiles.map((file) => file.path));
54
- }
55
- /**
56
- * Collected env variables
57
- */
58
- const envValues = {};
59
- envFiles.forEach(({ contents }) => this.#processContents(contents, envValues));
60
- return envValues;
61
- }
62
- /**
63
- * Process env variables
64
- */
65
- async process() {
66
- return this.#loadAndProcessDotFiles();
67
- }
68
- }
@@ -1,25 +0,0 @@
1
- import { ValidateFn } from '@poppinss/validator-lite';
2
- /**
3
- * Exposes the API to validate environment variables against a
4
- * pre-defined schema.
5
- *
6
- * The class is not exported in the main API and used internally.
7
- */
8
- export declare class EnvValidator<Schema extends {
9
- [key: string]: ValidateFn<unknown>;
10
- }> {
11
- #private;
12
- constructor(schema: Schema);
13
- /**
14
- * Accepts an object of values to validate against the pre-defined
15
- * schema.
16
- *
17
- * The return value is a merged copy of the original object and the
18
- * values mutated by the schema validator.
19
- */
20
- validate(values: {
21
- [K: string]: string | undefined;
22
- }): {
23
- [K in keyof Schema]: ReturnType<Schema[K]>;
24
- };
25
- }
@@ -1,48 +0,0 @@
1
- /*
2
- * @adonisjs/env
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { E_INVALID_ENV_VARIABLES } from './exceptions.js';
10
- /**
11
- * Exposes the API to validate environment variables against a
12
- * pre-defined schema.
13
- *
14
- * The class is not exported in the main API and used internally.
15
- */
16
- export class EnvValidator {
17
- #schema;
18
- #error;
19
- constructor(schema) {
20
- this.#schema = schema;
21
- this.#error = new E_INVALID_ENV_VARIABLES();
22
- }
23
- /**
24
- * Accepts an object of values to validate against the pre-defined
25
- * schema.
26
- *
27
- * The return value is a merged copy of the original object and the
28
- * values mutated by the schema validator.
29
- */
30
- validate(values) {
31
- const help = [];
32
- const validated = Object.keys(this.#schema).reduce((result, key) => {
33
- const value = process.env[key] || values[key];
34
- try {
35
- result[key] = this.#schema[key](key, value);
36
- }
37
- catch (error) {
38
- help.push(`- ${error.message}`);
39
- }
40
- return result;
41
- }, { ...values });
42
- if (help.length) {
43
- this.#error.help = help.join('\n');
44
- throw this.#error;
45
- }
46
- return validated;
47
- }
48
- }