Skip to content
Closed
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
183 changes: 165 additions & 18 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,60 @@ function parseRegex (src) {
return obj
}

// Expand \n and \r escape sequences in double-quoted values.
function expandDoubleQuoteEscapes (value) {
return value.replace(/\\n/g, '\n').replace(/\\r/g, '\r')
}

// Helper for parseFast: handle a quoted value that begins after the separator
// crossed at least one newline. Mirrors the default regex's behavior:
// - the quoted alternative may span newlines
// - junk after its closing quote (same line) kills the whole alternative
// because the raw fallback cannot cross newlines -> value ''
// - an unterminated quote also kills it -> value ''
// Returns [value, nextIndex].
function parseQuotedAfterNewline (str, start, len) {
const quote = str.charCodeAt(start)
const vStart = start + 1
let j = vStart
while (j < len) {
const cc = str.charCodeAt(j)
if (cc === 92 /* \ */ && j + 1 < len) {
const nc = str.charCodeAt(j + 1)
if (nc === quote || nc === 92) {
j += 2
continue
}
}
if (cc === quote) break
j++
}

// unterminated -> alternative dies, value ''
if (j >= len) return ['', len]

let value = str.slice(vStart, j)
let i = j + 1

// junk (non-ws, non-comment) on the closing-quote's line kills it
while (i < len) {
const cc = str.charCodeAt(i)
if (cc === 32 || cc === 9 || cc === 11 || cc === 12 || cc === 160) { i++; continue }
if (cc === 35 /* # */) {
while (i < len && str.charCodeAt(i) !== 10) i++
break
}
if (cc === 10) break
// junk found -> whole alternative dies
return ['', i]
}

if (quote === 34 && value.indexOf('\\') !== -1) {
value = expandDoubleQuoteEscapes(value)
}
return [value, i]
}

// Parse src into an Object — hand-written character scanner (no regex in hot path)
// Via https://github.com/motdotla/dotenv/pull/1010 — opt-in via { fast: true }
function parseFast (src) {
Expand All @@ -76,7 +130,7 @@ function parseFast (src) {
// skip whitespace / blank lines (\r already normalized out)
// 65279 is a BOM (U+FEFF), which editors on Windows write ahead of the
// first key — the classic parser skips it as part of \s*, so do the same
while (i < len && (c === 32 || c === 9 || c === 10 || c === 65279)) {
while (i < len && (c === 32 || c === 9 || c === 10 || c === 65279 || c === 12 || c === 11 || c === 160)) {
i++
c = str.charCodeAt(i)
}
Expand All @@ -96,9 +150,9 @@ function parseFast (src) {
str.charCodeAt(i + 4) === 114 &&
str.charCodeAt(i + 5) === 116) {
const nc = str.charCodeAt(i + 6)
if (nc === 32 || nc === 9) {
if (nc === 32 || nc === 9 || nc === 11 || nc === 12 || nc === 160) {
i += 7
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9)) i++
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9 || c === 11 || c === 12 || c === 160)) i++
} else {
c = str.charCodeAt(i)
}
Expand All @@ -107,6 +161,8 @@ function parseFast (src) {
// key: [A-Za-z0-9_.-]+ via lookup
const keyStart = i
let stop = 0
let afterSeparator = ''
let sawSpaceBeforeSeparator = false
while (i < len) {
stop = str.charCodeAt(i)
if (KEY_CHAR[stop]) i++
Expand All @@ -120,24 +176,79 @@ function parseFast (src) {
if (i >= len) stop = 0

// skip spaces/tabs before separator
if (stop === 32 || stop === 9) {
do { i++; stop = i < len ? str.charCodeAt(i) : 0 } while (stop === 32 || stop === 9)
if (stop === 32 || stop === 9 || stop === 11 || stop === 12 || stop === 160) {
sawSpaceBeforeSeparator = true
do { i++; stop = i < len ? str.charCodeAt(i) : 0 } while (stop === 32 || stop === 9 || stop === 11 || stop === 12 || stop === 160)
}

if (stop === 61 /* = */) {
i++
} else if (stop === 58 /* : */ && i + 1 < len && (str.charCodeAt(i + 1) === 32 || str.charCodeAt(i + 1) === 9)) {
afterSeparator = '='
} else if (stop === 58 /* : */ && !sawSpaceBeforeSeparator && i + 1 < len && (str.charCodeAt(i + 1) === 32 || str.charCodeAt(i + 1) === 9 || str.charCodeAt(i + 1) === 11 || str.charCodeAt(i + 1) === 12 || str.charCodeAt(i + 1) === 160)) {
i++
afterSeparator = ': '
} else {
// invalid line — skip
while (i < len && str.charCodeAt(i) !== 10) i++
continue
}

// skip spaces/tabs after separator
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9)) i++
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9 || c === 11 || c === 12 || c === 160)) i++

// With a ':' separator the default regex requires exactly ':' + \s+? and
// the raw alternative starts right after the FIRST space: 'A: x' keeps
// ' x' (one leading space), 'A : x' does not match at all (space before
// ':' is invalid), and quoted values stay verbatim ('A: "q"' -> '"q"').
if (afterSeparator === ': ') {
// re-insert exactly one of the consumed spaces
if (i > 0 && (str.charCodeAt(i - 1) === 32 || str.charCodeAt(i - 1) === 9 || str.charCodeAt(i - 1) === 11 || str.charCodeAt(i - 1) === 12 || str.charCodeAt(i - 1) === 160)) i--
}

// The default parser's quoted-value alternative carries a greedy leading
// \s* that may cross newlines: after `A=` the value's opening quote may
// live on a later line (past blank lines). Detect whether we crossed a
// newline to reach the first non-blank char; if so, the quoted form is
// allowed to span lines, but any junk after its closing quote on that
// line kills the alternative entirely (the raw fallback cannot cross
// newlines) and the value ends up empty.
let crossedNewline = false
{
let scan = i
while (scan < len) {
const sc = str.charCodeAt(scan)
if (sc === 32 || sc === 9 || sc === 10 || sc === 65279 || sc === 12 || sc === 11 || sc === 160) {
if (sc === 10) crossedNewline = true
scan++
continue
}
break
}
if (crossedNewline) {
// A newline was crossed after the separator. Mirror the regex engine:
// the quoted alternative may start here (spanning lines), but its raw
// fallback cannot cross newlines, so any junk after the closing quote
// on that line kills the quoted form AND leaves EMPTY's value ''.
// Crucially the regex then retries a fresh match at that later line —
// so resume scanning AT scan, not at end-of-line.
c = scan < len ? str.charCodeAt(scan) : 0
if (c === 39 || c === 34 || c === 96) {
i = scan
const [qValue, next] = parseQuotedAfterNewline(str, i, len)
obj[key] = qValue
i = next
continue
}
// non-quote after the jump: EMPTY stays '', and the text at scan is
// re-parsed from scratch as its own entry (the regex retries ^ there).
obj[key] = ''
i = scan
continue
}
}

let value
let quotedOk = true
c = i < len ? str.charCodeAt(i) : 0

if (c === 39 /* ' */ || c === 34 /* " */ || c === 96 /* ` */) {
Expand Down Expand Up @@ -175,7 +286,7 @@ function parseFast (src) {
let end = k
while (end > uStart) {
const cc = str.charCodeAt(end - 1)
if (cc === 32 || cc === 9) end--
if (cc === 32 || cc === 9 || cc === 11 || cc === 12 || cc === 160) end--
else break
}
value = str.slice(uStart, end)
Expand All @@ -184,15 +295,37 @@ function parseFast (src) {
while (i < len && str.charCodeAt(i) !== 10) i++
}
} else {
value = str.slice(vStart, j)
i = j + 1
if (quote === 34 && value.indexOf('\\') !== -1) {
value = value.replace(/\\n/g, '\n').replace(/\\r/g, '\r')
// A quoted alternative only survives when nothing but spaces/tabs (or
// a comment) follows its closing quote. Any other trailing junk on
// the same line kills the quoted form and the raw single-line
// alternative takes over, starting at the opening quote.
let k = j + 1
while (k < len) {
const cc = str.charCodeAt(k)
if (cc === 32 || cc === 9 || cc === 11 || cc === 12 || cc === 160) { k++; continue }
if (cc === 35 /* # */ || cc === 10) break
// junk found — fall back to raw single-line starting at the quote
const rStart = i
let rEnd = rStart
while (rEnd < len) {
const rc = str.charCodeAt(rEnd)
if (rc === 35 || rc === 10) break
rEnd++
}
value = str.slice(rStart, rEnd)
i = rEnd
quotedOk = false
break
}
// trailing ws + optional comment
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9)) i++
if (i < len && str.charCodeAt(i) === 35) {
while (i < len && str.charCodeAt(i) !== 10) i++
if (quotedOk) {
value = str.slice(vStart, j)
i = k
if (quote === 34 && value.indexOf('\\') !== -1) {
value = expandDoubleQuoteEscapes(value)
}
if (i < len && str.charCodeAt(i) === 35) {
while (i < len && str.charCodeAt(i) !== 10) i++
}
}
}
} else {
Expand All @@ -205,10 +338,24 @@ function parseFast (src) {
let end = hash
while (end > vStart) {
const cc = str.charCodeAt(end - 1)
if (cc === 32 || cc === 9) end--
if (cc === 32 || cc === 9 || cc === 11 || cc === 12 || cc === 160) end--
else break
}
value = vStart === end ? '' : str.slice(vStart, end)
value = vStart === end ? '' : str.slice(vStart, end).trim()
// The default parser trims the captured value, strips surrounding
// quotes and expands escapes for double-quoted values. Mirror that
// for values whose trimmed form is wrapped in matching quotes.
const trimmed = value.trim()
if (trimmed.length >= 2) {
const first = trimmed.charCodeAt(0)
const last = trimmed.charCodeAt(trimmed.length - 1)
if ((first === 39 && last === 39) || (first === 34 && last === 34) || (first === 96 && last === 96)) {
const inner = trimmed.slice(1, -1)
value = first === 34 && inner.indexOf('\\') !== -1
? expandDoubleQuoteEscapes(inner)
: inner
}
}
i = hash === nl ? hash : nl
}

Expand Down
40 changes: 40 additions & 0 deletions tests/test-fast-parity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const t = require('tap')
const dotenv = require('../lib/main')

t.test('fast parser: blank line before quoted multiline value', (t) => {
const src = 'A=\n\n"hello\nworld"'
t.same(dotenv.parse(src), { A: 'hello\nworld' })
t.same(dotenv.parse(src, { fast: true }), { A: 'hello\nworld' }, 'fast must match default')

const singleSrc = "A=\n'hello\nworld'"
t.same(dotenv.parse(singleSrc), { A: 'hello\nworld' })
t.same(dotenv.parse(singleSrc, { fast: true }), { A: 'hello\nworld' }, 'fast must match default for single quotes')

const backtickSrc = 'A=\n\n`hello\nworld`'
t.same(dotenv.parse(backtickSrc), { A: 'hello\nworld' })
t.same(dotenv.parse(backtickSrc, { fast: true }), { A: 'hello\nworld' }, 'fast must match default for backticks')

const junkSrc = 'A=\n\n"x" junk'
t.same(dotenv.parse(junkSrc), { A: '' })
t.same(dotenv.parse(junkSrc, { fast: true }), { A: '' }, 'fast must match default when trailing junk is present after newline quoted value')

t.end()
})

t.test('fast parser: whitespace variants before key', (t) => {
for (const ws of ['\f', '\v', '\u00A0', ' ']) {
const src = `${ws}A=1`
t.same(dotenv.parse(src), { A: '1' })
t.same(dotenv.parse(src, { fast: true }), { A: '1' }, `fast handles key after ${JSON.stringify(ws)}`)
}
t.end()
})

t.test('fast parser: trailing junk after closing quote', (t) => {
const src = 'TOKEN="abc" oops'
t.same(dotenv.parse(src), { TOKEN: '"abc" oops' })
t.same(dotenv.parse(src, { fast: true }), { TOKEN: '"abc" oops' }, 'fast must match default')
t.end()
})