Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified cli.js
100755 → 100644
Empty file.
18 changes: 13 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { URL, fileURLToPath } = require('url')

const { parseBoolean, optionsFromEnv } = require('./config-options')

const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:(?:[^\S\r\n]+?|(?=\r|\n|$)))(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg

// From #1010 (homanp) — hand-written character scanner
const KEY_CHAR = new Uint8Array(256)
Expand Down Expand Up @@ -141,10 +141,18 @@ function parseFast (src) {

if (stop === 61 /* = */) {
i++
} else if (stop === 58 /* : */ && i === keyEnd && i + 1 < len && isWhitespace(str.charCodeAt(i + 1))) {
// Unlike '=', ':' requires at least one whitespace character. That
// character belongs to the separator, even when it is a newline.
i += 2
} else if (stop === 58 /* : */ && i === keyEnd && (i + 1 >= len || isWhitespace(str.charCodeAt(i + 1)))) {
// Unlike '=', ':' requires at least one whitespace character. A line
// ending after the colon ends the line with an empty value instead of
// belonging to the separator, so the next line stays its own
// assignment. A quoted value on the next line still counts: the quote
// scan below skips the line ending to find it.
const afterColon = i + 1 < len ? str.charCodeAt(i + 1) : 0
if (isLineEnd(afterColon) || i + 1 >= len) {
i += 1
} else {
i += 2
}
} else {
// invalid line — skip
// Whitespace lookahead may have crossed a newline; do not discard
Expand Down
28 changes: 26 additions & 2 deletions tests/test-parse-fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ t.test('fast parse handles whitespace without consuming the next assignment', ct

t.test('fast parse matches classic for colon newlines and export keys', ct => {
const cases = [
{ src: 'A:\nfoo', expected: { A: 'foo' } },
{ src: 'A:\r\nfoo\nNEXT=ok', expected: { A: 'foo', NEXT: 'ok' } },
{ src: 'A:\nfoo', expected: { A: '' } },
{ src: 'A:\r\nfoo\nNEXT=ok', expected: { A: '', NEXT: 'ok' } },
{ src: 'export =value', expected: { export: 'value' } },
{ src: 'export\t=value', expected: { export: 'value' } },
{ src: 'export : value\nNEXT=ok', expected: { NEXT: 'ok' } },
Expand All @@ -195,6 +195,30 @@ t.test('fast parse matches classic for colon newlines and export keys', ct => {
ct.end()
})

t.test('an empty colon value does not consume the next line', ct => {
const cases = [
{ src: 'A:\nB=2', expected: { A: '', B: '2' } },
{ src: 'A: \nB=2', expected: { A: '', B: '2' } },
{ src: 'A:\r\nB=2', expected: { A: '', B: '2' } },
{ src: 'A:', expected: { A: '' } },
{ src: 'A:\nB:\nC=3', expected: { A: '', B: '', C: '3' } },
{ src: 'A:\n# comment\nB=2', expected: { A: '', B: '2' } },
{ src: 'A:\n"q"', expected: { A: 'q' } },
{ src: "A:\n'q'", expected: { A: 'q' } },
{ src: 'A:\n`q`', expected: { A: 'q' } },
{ src: 'A: value', expected: { A: 'value' } },
{ src: 'A: value\nB=2', expected: { A: 'value', B: '2' } },
{ src: 'A=value', expected: { A: 'value' } },
{ src: 'A=\nB=2', expected: { A: '', B: '2' } },
{ src: 'URL:http://x', expected: {} }
]
for (const { src, expected } of cases) {
ct.same(dotenv.parse(src), expected, `classic: ${JSON.stringify(src)}`)
ct.same(dotenv.parse(src, { fast: true }), expected, `fast: ${JSON.stringify(src)}`)
}
ct.end()
})

t.test('fast parse respects Unicode line separators around quotes and comments', ct => {
for (const separator of ['\u2028', '\u2029']) {
const cases = [
Expand Down