secure-env-loader 8.6.0

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.
@@ -0,0 +1 @@
1
+ tidelift: "npm/dotenv"
package/CHANGELOG.md ADDED
@@ -0,0 +1,184 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+ ## [8.6.0](https://github.com/motdotla/dotenv/compare/v8.5.1...v8.6.0) (2021-05-05)
6
+
7
+ ### Added
8
+
9
+ - define package.json in exports
10
+
11
+ ## [8.5.1](https://github.com/motdotla/dotenv/compare/v8.5.0...v8.5.1) (2021-05-05)
12
+
13
+ ### Changed
14
+
15
+ - updated dev dependencies via npm audit
16
+
17
+ ## [8.5.0](https://github.com/motdotla/dotenv/compare/v8.4.0...v8.5.0) (2021-05-05)
18
+
19
+ ### Added
20
+
21
+ - allow for `import "dotenv/config"`
22
+
23
+ ## [8.4.0](https://github.com/motdotla/dotenv/compare/v8.3.0...v8.4.0) (2021-05-05)
24
+
25
+ ### Changed
26
+
27
+ - point to exact types file to work with VS Code
28
+
29
+ ## [8.3.0](https://github.com/motdotla/dotenv/compare/v8.2.0...v8.3.0) (2021-05-05)
30
+
31
+ ### Changed
32
+
33
+ - _Breaking:_ drop support for Node v8
34
+
35
+ ## [8.2.0](https://github.com/motdotla/dotenv/compare/v8.1.0...v8.2.0) (2019-10-16)
36
+
37
+ ### Added
38
+
39
+ - TypeScript types
40
+
41
+ ## [8.1.0](https://github.com/motdotla/dotenv/compare/v8.0.0...v8.1.0) (2019-08-18)
42
+
43
+ ### Changed
44
+
45
+ - _Breaking:_ drop support for Node v6 ([#392](https://github.com/motdotla/dotenv/issues/392))
46
+
47
+ # [8.0.0](https://github.com/motdotla/dotenv/compare/v7.0.0...v8.0.0) (2019-05-02)
48
+
49
+ ### Changed
50
+
51
+ - _Breaking:_ drop support for Node v6 ([#302](https://github.com/motdotla/dotenv/issues/392))
52
+
53
+ ## [7.0.0] - 2019-03-12
54
+
55
+ ### Fixed
56
+
57
+ - Fix removing unbalanced quotes ([#376](https://github.com/motdotla/dotenv/pull/376))
58
+
59
+ ### Removed
60
+
61
+ - Removed `load` alias for `config` for consistency throughout code and documentation.
62
+
63
+ ## [6.2.0] - 2018-12-03
64
+
65
+ ### Added
66
+
67
+ - Support preload configuration via environment variables ([#351](https://github.com/motdotla/dotenv/issues/351))
68
+
69
+ ## [6.1.0] - 2018-10-08
70
+
71
+ ### Added
72
+
73
+ - `debug` option for `config` and `parse` methods will turn on logging
74
+
75
+ ## [6.0.0] - 2018-06-02
76
+
77
+ ### Changed
78
+
79
+ - _Breaking:_ drop support for Node v4 ([#304](https://github.com/motdotla/dotenv/pull/304))
80
+
81
+ ## [5.0.0] - 2018-01-29
82
+
83
+ ### Added
84
+
85
+ - Testing against Node v8 and v9
86
+ - Documentation on trim behavior of values
87
+ - Documentation on how to use with `import`
88
+
89
+ ### Changed
90
+
91
+ - _Breaking_: default `path` is now `path.resolve(process.cwd(), '.env')`
92
+ - _Breaking_: does not write over keys already in `process.env` if the key has a falsy value
93
+ - using `const` and `let` instead of `var`
94
+
95
+ ### Removed
96
+
97
+ - Testing against Node v7
98
+
99
+ ## [4.0.0] - 2016-12-23
100
+
101
+ ### Changed
102
+
103
+ - Return Object with parsed content or error instead of false ([#165](https://github.com/motdotla/dotenv/pull/165)).
104
+
105
+ ### Removed
106
+
107
+ - `verbose` option removed in favor of returning result.
108
+
109
+ ## [3.0.0] - 2016-12-20
110
+
111
+ ### Added
112
+
113
+ - `verbose` option will log any error messages. Off by default.
114
+ - parses email addresses correctly
115
+ - allow importing config method directly in ES6
116
+
117
+ ### Changed
118
+
119
+ - Suppress error messages by default ([#154](https://github.com/motdotla/dotenv/pull/154))
120
+ - Ignoring more files for NPM to make package download smaller
121
+
122
+ ### Fixed
123
+
124
+ - False positive test due to case-sensitive variable ([#124](https://github.com/motdotla/dotenv/pull/124))
125
+
126
+ ### Removed
127
+
128
+ - `silent` option removed in favor of `verbose`
129
+
130
+ ## [2.0.0] - 2016-01-20
131
+
132
+ ### Added
133
+
134
+ - CHANGELOG to ["make it easier for users and contributors to see precisely what notable changes have been made between each release"](http://keepachangelog.com/). Linked to from README
135
+ - LICENSE to be more explicit about what was defined in `package.json`. Linked to from README
136
+ - Testing nodejs v4 on travis-ci
137
+ - added examples of how to use dotenv in different ways
138
+ - return parsed object on success rather than boolean true
139
+
140
+ ### Changed
141
+
142
+ - README has shorter description not referencing ruby gem since we don't have or want feature parity
143
+
144
+ ### Removed
145
+
146
+ - Variable expansion and escaping so environment variables are encouraged to be fully orthogonal
147
+
148
+ ## [1.2.0] - 2015-06-20
149
+
150
+ ### Added
151
+
152
+ - Preload hook to require dotenv without including it in your code
153
+
154
+ ### Changed
155
+
156
+ - clarified license to be "BSD-2-Clause" in `package.json`
157
+
158
+ ### Fixed
159
+
160
+ - retain spaces in string vars
161
+
162
+ ## [1.1.0] - 2015-03-31
163
+
164
+ ### Added
165
+
166
+ - Silent option to silence `console.log` when `.env` missing
167
+
168
+ ## [1.0.0] - 2015-03-13
169
+
170
+ ### Removed
171
+
172
+ - support for multiple `.env` files. should always use one `.env` file for the current environment
173
+
174
+ [7.0.0]: https://github.com/motdotla/dotenv/compare/v6.2.0...v7.0.0
175
+ [6.2.0]: https://github.com/motdotla/dotenv/compare/v6.1.0...v6.2.0
176
+ [6.1.0]: https://github.com/motdotla/dotenv/compare/v6.0.0...v6.1.0
177
+ [6.0.0]: https://github.com/motdotla/dotenv/compare/v5.0.0...v6.0.0
178
+ [5.0.0]: https://github.com/motdotla/dotenv/compare/v4.0.0...v5.0.0
179
+ [4.0.0]: https://github.com/motdotla/dotenv/compare/v3.0.0...v4.0.0
180
+ [3.0.0]: https://github.com/motdotla/dotenv/compare/v2.0.0...v3.0.0
181
+ [2.0.0]: https://github.com/motdotla/dotenv/compare/v1.2.0...v2.0.0
182
+ [1.2.0]: https://github.com/motdotla/dotenv/compare/v1.1.0...v1.2.0
183
+ [1.1.0]: https://github.com/motdotla/dotenv/compare/v1.0.0...v1.1.0
184
+ [1.0.0]: https://github.com/motdotla/dotenv/compare/v0.4.0...v1.0.0
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015, Scott Motte
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,273 @@
1
+ **Announcement 📣** - From the makers that brought you .env, introducing [.env.me](http://npmjs.org/package/dotenv-me). Sync your .env files across machines. [Join the early access list.](https://me.dotenv.org/)
2
+
3
+ # dotenv
4
+
5
+ <img src="https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.png" alt="dotenv" align="right" />
6
+
7
+ Dotenv is a zero-dependency module that loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env). Storing configuration in the environment separate from code is based on [The Twelve-Factor App](http://12factor.net/config) methodology.
8
+
9
+ [![BuildStatus](https://img.shields.io/travis/motdotla/dotenv/master.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv)
10
+ [![Build status](https://ci.appveyor.com/api/projects/status/github/motdotla/dotenv?svg=true)](https://ci.appveyor.com/project/motdotla/dotenv/branch/master)
11
+ [![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv)
12
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
13
+ [![Coverage Status](https://img.shields.io/coveralls/motdotla/dotenv/master.svg?style=flat-square)](https://coveralls.io/github/motdotla/dotenv?branch=coverall-intergration)
14
+ [![LICENSE](https://img.shields.io/github/license/motdotla/dotenv.svg)](LICENSE)
15
+ [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ # with npm
21
+ npm install dotenv
22
+
23
+ # or with Yarn
24
+ yarn add dotenv
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ As early as possible in your application, require and configure dotenv.
30
+
31
+ ```javascript
32
+ require('dotenv').config()
33
+ ```
34
+
35
+ Create a `.env` file in the root directory of your project. Add
36
+ environment-specific variables on new lines in the form of `NAME=VALUE`.
37
+ For example:
38
+
39
+ ```dosini
40
+ DB_HOST=localhost
41
+ DB_USER=root
42
+ DB_PASS=s1mpl3
43
+ ```
44
+
45
+ `process.env` now has the keys and values you defined in your `.env` file.
46
+
47
+ ```javascript
48
+ const db = require('db')
49
+ db.connect({
50
+ host: process.env.DB_HOST,
51
+ username: process.env.DB_USER,
52
+ password: process.env.DB_PASS
53
+ })
54
+ ```
55
+
56
+ ### Preload
57
+
58
+ You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code. This is the preferred approach when using `import` instead of `require`.
59
+
60
+ ```bash
61
+ $ node -r dotenv/config your_script.js
62
+ ```
63
+
64
+ The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
65
+
66
+ ```bash
67
+ $ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
68
+ ```
69
+
70
+ Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
71
+
72
+ ```bash
73
+ $ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
74
+ ```
75
+
76
+ ```bash
77
+ $ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
78
+ ```
79
+
80
+ ## Config
81
+
82
+ `config` will read your `.env` file, parse the contents, assign it to
83
+ [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
84
+ and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
85
+
86
+ ```js
87
+ const result = dotenv.config()
88
+
89
+ if (result.error) {
90
+ throw result.error
91
+ }
92
+
93
+ console.log(result.parsed)
94
+ ```
95
+
96
+ You can additionally, pass options to `config`.
97
+
98
+ ### Options
99
+
100
+ #### Path
101
+
102
+ Default: `path.resolve(process.cwd(), '.env')`
103
+
104
+ You may specify a custom path if your file containing environment variables is located elsewhere.
105
+
106
+ ```js
107
+ require('dotenv').config({ path: '/custom/path/to/.env' })
108
+ ```
109
+
110
+ #### Encoding
111
+
112
+ Default: `utf8`
113
+
114
+ You may specify the encoding of your file containing environment variables.
115
+
116
+ ```js
117
+ require('dotenv').config({ encoding: 'latin1' })
118
+ ```
119
+
120
+ #### Debug
121
+
122
+ Default: `false`
123
+
124
+ You may turn on logging to help debug why certain keys or values are not being set as you expect.
125
+
126
+ ```js
127
+ require('dotenv').config({ debug: process.env.DEBUG })
128
+ ```
129
+
130
+ ## Parse
131
+
132
+ The engine which parses the contents of your file containing environment
133
+ variables is available to use. It accepts a String or Buffer and will return
134
+ an Object with the parsed keys and values.
135
+
136
+ ```js
137
+ const dotenv = require('dotenv')
138
+ const buf = Buffer.from('BASIC=basic')
139
+ const config = dotenv.parse(buf) // will return an object
140
+ console.log(typeof config, config) // object { BASIC : 'basic' }
141
+ ```
142
+
143
+ ### Options
144
+
145
+ #### Debug
146
+
147
+ Default: `false`
148
+
149
+ You may turn on logging to help debug why certain keys or values are not being set as you expect.
150
+
151
+ ```js
152
+ const dotenv = require('dotenv')
153
+ const buf = Buffer.from('hello world')
154
+ const opt = { debug: true }
155
+ const config = dotenv.parse(buf, opt)
156
+ // expect a debug message because the buffer is not in KEY=VAL form
157
+ ```
158
+
159
+ ### Rules
160
+
161
+ The parsing engine currently supports the following rules:
162
+
163
+ - `BASIC=basic` becomes `{BASIC: 'basic'}`
164
+ - empty lines are skipped
165
+ - lines beginning with `#` are treated as comments
166
+ - empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
167
+ - inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
168
+ - whitespace is removed from both ends of unquoted values (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO= some value ` becomes `{FOO: 'some value'}`)
169
+ - single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`)
170
+ - single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`)
171
+ - double quoted values expand new lines (`MULTILINE="new\nline"` becomes
172
+
173
+ ```
174
+ {MULTILINE: 'new
175
+ line'}
176
+ ```
177
+
178
+ ## FAQ
179
+
180
+ ### Should I commit my `.env` file?
181
+
182
+ No. We **strongly** recommend against committing your `.env` file to version
183
+ control. It should only include environment-specific values such as database
184
+ passwords or API keys. Your production database should have a different
185
+ password than your development database.
186
+
187
+ ### Should I have multiple `.env` files?
188
+
189
+ No. We **strongly** recommend against having a "main" `.env` file and an "environment" `.env` file like `.env.test`. Your config should vary between deploys, and you should not be sharing values between environments.
190
+
191
+ > In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.
192
+ >
193
+ > – [The Twelve-Factor App](http://12factor.net/config)
194
+
195
+ ### What happens to environment variables that were already set?
196
+
197
+ We will never modify any environment variables that have already been set. In particular, if there is a variable in your `.env` file which collides with one that already exists in your environment, then that variable will be skipped. This behavior allows you to override all `.env` configurations with a machine-specific environment, although it is not recommended.
198
+
199
+ If you want to override `process.env` you can do something like this:
200
+
201
+ ```javascript
202
+ const fs = require('fs')
203
+ const dotenv = require('dotenv')
204
+ const envConfig = dotenv.parse(fs.readFileSync('.env.override'))
205
+ for (const k in envConfig) {
206
+ process.env[k] = envConfig[k]
207
+ }
208
+ ```
209
+
210
+ ### Can I customize/write plugins for dotenv?
211
+
212
+ For `dotenv@2.x.x`: Yes. `dotenv.config()` now returns an object representing
213
+ the parsed `.env` file. This gives you everything you need to continue
214
+ setting values on `process.env`. For example:
215
+
216
+ ```js
217
+ const dotenv = require('dotenv')
218
+ const variableExpansion = require('dotenv-expand')
219
+ const myEnv = dotenv.config()
220
+ variableExpansion(myEnv)
221
+ ```
222
+
223
+ ### What about variable expansion?
224
+
225
+ Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
226
+
227
+ ### How do I use dotenv with `import`?
228
+
229
+ ES2015 and beyond offers modules that allow you to `export` any top-level `function`, `class`, `var`, `let`, or `const`.
230
+
231
+ > When you run a module containing an `import` declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.
232
+ >
233
+ > – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
234
+
235
+ You must run `dotenv.config()` before referencing any environment variables. Here's an example of problematic code:
236
+
237
+ `errorReporter.js`:
238
+
239
+ ```js
240
+ import { Client } from 'best-error-reporting-service'
241
+
242
+ export const client = new Client(process.env.BEST_API_KEY)
243
+ ```
244
+
245
+ `index.js`:
246
+
247
+ ```js
248
+ import dotenv from 'dotenv'
249
+ import errorReporter from './errorReporter'
250
+
251
+ dotenv.config()
252
+ errorReporter.client.report(new Error('faq example'))
253
+ ```
254
+
255
+ `client` will not be configured correctly because it was constructed before `dotenv.config()` was executed. There are (at least) 3 ways to make this work.
256
+
257
+ 1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
258
+ 2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line or environment variables with this approach_)
259
+ 3. Create a separate file that will execute `config` first as outlined in [this comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-255298822)
260
+
261
+ ## Contributing Guide
262
+
263
+ See [CONTRIBUTING.md](CONTRIBUTING.md)
264
+
265
+ ## Change Log
266
+
267
+ See [CHANGELOG.md](CHANGELOG.md)
268
+
269
+ ## Who's using dotenv?
270
+
271
+ [These npm modules depend on it.](https://www.npmjs.com/browse/depended/dotenv)
272
+
273
+ Projects that expand it often use the [keyword "dotenv" on npm](https://www.npmjs.com/search?q=keywords:dotenv).
package/config.js ADDED
@@ -0,0 +1,11 @@
1
+ /* @flow */
2
+
3
+ (function () {
4
+ require('./lib/main').config(
5
+ Object.assign(
6
+ {},
7
+ require('./lib/env-options'),
8
+ require('./lib/cli-options')(process.argv)
9
+ )
10
+ )
11
+ })()
@@ -0,0 +1,13 @@
1
+ /* @flow */
2
+
3
+ const re = /^dotenv_config_(encoding|path|debug)=(.+)$/
4
+
5
+ module.exports = function optionMatcher (args /*: Array<string> */) {
6
+ return args.reduce(function (acc, cur) {
7
+ const matches = cur.match(re)
8
+ if (matches) {
9
+ acc[matches[1]] = matches[2]
10
+ }
11
+ return acc
12
+ }, {})
13
+ }
@@ -0,0 +1,18 @@
1
+ /* @flow */
2
+
3
+ // ../config.js accepts options via environment variables
4
+ const options = {}
5
+
6
+ if (process.env.DOTENV_CONFIG_ENCODING != null) {
7
+ options.encoding = process.env.DOTENV_CONFIG_ENCODING
8
+ }
9
+
10
+ if (process.env.DOTENV_CONFIG_PATH != null) {
11
+ options.path = process.env.DOTENV_CONFIG_PATH
12
+ }
13
+
14
+ if (process.env.DOTENV_CONFIG_DEBUG != null) {
15
+ options.debug = process.env.DOTENV_CONFIG_DEBUG
16
+ }
17
+
18
+ module.exports = options
package/lib/main.js ADDED
@@ -0,0 +1,117 @@
1
+ /* @flow */
2
+ /*::
3
+
4
+ type DotenvParseOptions = {
5
+ debug?: boolean
6
+ }
7
+
8
+ // keys and values from src
9
+ type DotenvParseOutput = { [string]: string }
10
+
11
+ type DotenvConfigOptions = {
12
+ path?: string, // path to .env file
13
+ encoding?: string, // encoding of .env file
14
+ debug?: string // turn on logging for debugging purposes
15
+ }
16
+
17
+ type DotenvConfigOutput = {
18
+ parsed?: DotenvParseOutput,
19
+ error?: Error
20
+ }
21
+
22
+ */
23
+
24
+ const fs = require('fs')
25
+ const path = require('path')
26
+
27
+ function hello_world(){
28
+ console.log("hello, world")
29
+ }
30
+
31
+ function log (message /*: string */) {
32
+ console.log(`[dotenv][DEBUG] ${message}`)
33
+ }
34
+
35
+ const NEWLINE = '\n'
36
+ const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/
37
+ const RE_NEWLINES = /\\n/g
38
+ const NEWLINES_MATCH = /\n|\r|\r\n/
39
+
40
+ // Parses src into an Object
41
+ function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ {
42
+ const debug = Boolean(options && options.debug)
43
+ const obj = {}
44
+
45
+ // convert Buffers before splitting into lines and processing
46
+ src.toString().split(NEWLINES_MATCH).forEach(function (line, idx) {
47
+ // matching "KEY' and 'VAL' in 'KEY=VAL'
48
+ const keyValueArr = line.match(RE_INI_KEY_VAL)
49
+ // matched?
50
+ if (keyValueArr != null) {
51
+ const key = keyValueArr[1]
52
+ // default undefined or missing values to empty string
53
+ let val = (keyValueArr[2] || '')
54
+ const end = val.length - 1
55
+ const isDoubleQuoted = val[0] === '"' && val[end] === '"'
56
+ const isSingleQuoted = val[0] === "'" && val[end] === "'"
57
+
58
+ // if single or double quoted, remove quotes
59
+ if (isSingleQuoted || isDoubleQuoted) {
60
+ val = val.substring(1, end)
61
+
62
+ // if double quoted, expand newlines
63
+ if (isDoubleQuoted) {
64
+ val = val.replace(RE_NEWLINES, NEWLINE)
65
+ }
66
+ } else {
67
+ // remove surrounding whitespace
68
+ val = val.trim()
69
+ }
70
+
71
+ obj[key] = val
72
+ } else if (debug) {
73
+ log(`did not match key and value when parsing line ${idx + 1}: ${line}`)
74
+ }
75
+ })
76
+
77
+ return obj
78
+ }
79
+
80
+ // Populates process.env from .env file
81
+ function config (options /*: ?DotenvConfigOptions */) /*: DotenvConfigOutput */ {
82
+ let dotenvPath = path.resolve(process.cwd(), '.env')
83
+ let encoding /*: string */ = 'utf8'
84
+ let debug = false
85
+
86
+ if (options) {
87
+ if (options.path != null) {
88
+ dotenvPath = options.path
89
+ }
90
+ if (options.encoding != null) {
91
+ encoding = options.encoding
92
+ }
93
+ if (options.debug != null) {
94
+ debug = true
95
+ }
96
+ }
97
+
98
+ try {
99
+ // specifying an encoding returns a string instead of a buffer
100
+ const parsed = parse(fs.readFileSync(dotenvPath, { encoding }), { debug })
101
+
102
+ Object.keys(parsed).forEach(function (key) {
103
+ if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
104
+ process.env[key] = parsed[key]
105
+ } else if (debug) {
106
+ log(`"${key}" is already defined in \`process.env\` and will not be overwritten`)
107
+ }
108
+ })
109
+
110
+ return { parsed }
111
+ } catch (e) {
112
+ return { error: e }
113
+ }
114
+ }
115
+
116
+ module.exports.config = config
117
+ module.exports.parse = parse
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "secure-env-loader",
3
+ "version": "8.6.0",
4
+ "description": "Loads environment variables from .env file",
5
+ "main": "lib/main.js",
6
+ "exports": {
7
+ ".": "./lib/main.js",
8
+ "./config": "./config.js",
9
+ "./package.json": "./package.json"
10
+ },
11
+ "types": "types/index.d.ts",
12
+ "scripts": {
13
+ "flow": "flow",
14
+ "dtslint": "dtslint types",
15
+ "lint": "standard",
16
+ "postlint": "standard-markdown",
17
+ "pretest": "npm run lint && npm run dtslint",
18
+ "test": "tap tests/*.js --100",
19
+ "prerelease": "npm test",
20
+ "release": "standard-version"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git://github.com/motdotla/dotenv.git"
25
+ },
26
+ "keywords": [
27
+ "dotenv",
28
+ "env",
29
+ ".env",
30
+ "environment",
31
+ "variables",
32
+ "config",
33
+ "settings"
34
+ ],
35
+ "readmeFilename": "README.md",
36
+ "license": "BSD-2-Clause",
37
+ "devDependencies": {
38
+ "decache": "^4.5.1",
39
+ "dtslint": "^0.9.8",
40
+ "flow-bin": "^0.109.0",
41
+ "sinon": "^7.5.0",
42
+ "standard": "^13.1.0",
43
+ "standard-markdown": "^5.1.0",
44
+ "standard-version": "^7.0.0",
45
+ "tap": "^14.7.0"
46
+ },
47
+ "dependencies": {},
48
+ "engines": {
49
+ "node": ">=10"
50
+ },
51
+ "standard": {
52
+ "ignore": [
53
+ "flow-typed/"
54
+ ]
55
+ }
56
+ }
@@ -0,0 +1,59 @@
1
+ // TypeScript Version: 3.0
2
+ /// <reference types="node" />
3
+
4
+ export interface DotenvParseOptions {
5
+ /**
6
+ * You may turn on logging to help debug why certain keys or values are not being set as you expect.
7
+ */
8
+ debug?: boolean;
9
+ }
10
+
11
+ export interface DotenvParseOutput {
12
+ [name: string]: string;
13
+ }
14
+
15
+ /**
16
+ * Parses a string or buffer in the .env file format into an object.
17
+ *
18
+ * @param src - contents to be parsed
19
+ * @param options - additional options
20
+ * @returns an object with keys and values based on `src`
21
+ */
22
+ export function parse(
23
+ src: string | Buffer,
24
+ options?: DotenvParseOptions
25
+ ): DotenvParseOutput;
26
+
27
+ export interface DotenvConfigOptions {
28
+ /**
29
+ * You may specify a custom path if your file containing environment variables is located elsewhere.
30
+ */
31
+ path?: string;
32
+
33
+ /**
34
+ * You may specify the encoding of your file containing environment variables.
35
+ */
36
+ encoding?: string;
37
+
38
+ /**
39
+ * You may turn on logging to help debug why certain keys or values are not being set as you expect.
40
+ */
41
+ debug?: boolean;
42
+ }
43
+
44
+ export interface DotenvConfigOutput {
45
+ error?: Error;
46
+ parsed?: DotenvParseOutput;
47
+ }
48
+
49
+ /**
50
+ * Loads `.env` file contents into {@link https://nodejs.org/api/process.html#process_process_env `process.env`}.
51
+ * Example: 'KEY=value' becomes { parsed: { KEY: 'value' } }
52
+ *
53
+ * @param options - controls behavior
54
+ * @returns an object with a `parsed` key if successful or `error` key if an error occurred
55
+ *
56
+ */
57
+ export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
58
+ /** @deprecated since v7.0.0 Use config instead. */
59
+ export const load: typeof config;
package/types/test.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { config, parse } from "dotenv";
2
+
3
+ const env = config();
4
+ const dbUrl: string | null =
5
+ env.error || !env.parsed ? null : env.parsed["BASIC"];
6
+
7
+ config({
8
+ path: ".env-example",
9
+ encoding: "utf8",
10
+ debug: true
11
+ });
12
+
13
+ const parsed = parse("NODE_ENV=production\nDB_HOST=a.b.c");
14
+ const dbHost: string = parsed["DB_HOST"];
15
+
16
+ const parsedFromBuffer = parse(new Buffer("JUSTICE=league\n"), {
17
+ debug: true
18
+ });
19
+ const justice: string = parsedFromBuffer["JUSTICE"];
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "lib": ["es6"],
5
+ "noImplicitAny": true,
6
+ "noImplicitThis": true,
7
+ "strictNullChecks": true,
8
+ "strictFunctionTypes": true,
9
+ "noEmit": true,
10
+ "baseUrl": ".",
11
+ "paths": {
12
+ "dotenv": ["."]
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "dtslint/dtslint.json",
3
+ "rules": {
4
+ "strict-export-declare-modifiers": false
5
+ }
6
+ }