diff --git a/lib/main.js b/lib/main.js index 38f36f31..dff25954 100644 --- a/lib/main.js +++ b/lib/main.js @@ -31,6 +31,7 @@ function log (message /*: string */) { const NEWLINE = '\n' const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/ const RE_NEWLINES = /\\n/g +const NEWLINES_MATCH = /\n|\r|\r\n/ // Parses src into an Object function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ { @@ -38,7 +39,7 @@ function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) const obj = {} // convert Buffers before splitting into lines and processing - src.toString().split(NEWLINE).forEach(function (line, idx) { + src.toString().split(NEWLINES_MATCH).forEach(function (line, idx) { // matching "KEY' and 'VAL' in 'KEY=VAL' const keyValueArr = line.match(RE_INI_KEY_VAL) // matched? diff --git a/tests/test-parse.js b/tests/test-parse.js index 5c8690c6..5a859dfa 100644 --- a/tests/test-parse.js +++ b/tests/test-parse.js @@ -9,7 +9,7 @@ const dotenv = require('../lib/main') const parsed = dotenv.parse(fs.readFileSync('tests/.env', { encoding: 'utf8' })) -t.plan(24) +t.plan(27) t.type(parsed, Object, 'should return an object') @@ -58,6 +58,17 @@ t.equal(parsed.SPACED_KEY, 'parsed', 'parses keys and values surrounded by space const payload = dotenv.parse(Buffer.from('BUFFER=true')) t.equal(payload.BUFFER, 'true', 'should parse a buffer into an object') +const expectedPayload = { SERVER: 'localhost', PASSWORD: 'password', DB: 'tests' } + +const RPayload = dotenv.parse(Buffer.from('SERVER=localhost\rPASSWORD=password\rDB=tests\r')) +t.same(RPayload, expectedPayload, 'can parse (\\r) line endings') + +const NPayload = dotenv.parse(Buffer.from('SERVER=localhost\nPASSWORD=password\nDB=tests\n')) +t.same(NPayload, expectedPayload, 'can parse (\\n) line endings') + +const RNPayload = dotenv.parse(Buffer.from('SERVER=localhost\r\nPASSWORD=password\r\nDB=tests\r\n')) +t.same(RNPayload, expectedPayload, 'can parse (\\r\\n) line endings') + // test debug path const logStub = sinon.stub(console, 'log') dotenv.parse(Buffer.from('what is this'), { debug: true })