secure-env-ts
Version:
Use ENVs securely with encryption
29 lines (28 loc) • 1.11 kB
JavaScript
;
/*
* The parse function is the same as in https://github.com/motdotla/dotenv v4.0.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
const parser = (src) => {
const obj = {};
// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(line => {
// matching "KEY' and 'VAL' in 'KEY=VAL'
const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
// matched?
if (keyValueArr != null) {
const key = keyValueArr[1];
// default undefined or missing values to empty string
let value = keyValueArr[2] || '';
// expand newlines in quoted values
const len = value ? value.length : 0;
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"')
value = value.replace(/\\n/gm, '\n');
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim();
obj[key] = value;
}
});
return obj;
};
exports.default = parser;