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
75 changes: 68 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ KEY_CHAR[45] = 1 // -
KEY_CHAR[46] = 1 // .
KEY_CHAR[95] = 1 // _

// Match the whitespace characters recognized by JavaScript's \\s. The fast
// parser cannot use a regular expression in its hot path, but it still needs
// to accept the same input as the classic parser.
function isWhitespace (c) {
return c === 9 || c === 10 || c === 11 || c === 12 || c === 13 || c === 32 ||
c === 160 || c === 5760 || (c >= 8192 && c <= 8202) || c === 8232 ||
c === 8233 || c === 8239 || c === 8287 || c === 12288 || c === 65279
}

// Classic regex parser (default)
function parseRegex (src) {
const obj = {}
Expand Down Expand Up @@ -80,6 +89,7 @@ function parseFast (src) {
i++
c = str.charCodeAt(i)
}
if (i < len && isWhitespace(c)) return parseRegex(str)
if (i >= len) break

// comment line
Expand All @@ -96,9 +106,12 @@ 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 === 10 || nc === 65279) {
i += 7
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9)) i++
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9 || c === 10 || c === 65279)) i++
if (i < len && isWhitespace(c)) return parseRegex(str)
} else if (isWhitespace(nc)) {
return parseRegex(str)
} else {
c = str.charCodeAt(i)
}
Expand Down Expand Up @@ -134,14 +147,47 @@ function parseFast (src) {
continue
}

// skip spaces/tabs after separator
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9)) i++
// Skip whitespace after the separator. A newline only belongs to the value
// when the next non-whitespace character starts a quoted value; otherwise
// it terminates an empty value and begins the next assignment.
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9 || c === 65279)) i++
if (i < len && c !== 10 && isWhitespace(c)) return parseRegex(str)
if (i < len && str.charCodeAt(i) === 10) {
let j = i
while (j < len && isWhitespace(str.charCodeAt(j))) j++
const next = j < len ? str.charCodeAt(j) : 0
if (next === 39 || next === 34 || next === 96) {
let k = j + 1
while (k < len) {
const cc = str.charCodeAt(k)
if (cc === 92 && k + 1 < len) {
const nc = str.charCodeAt(k + 1)
if (nc === next || nc === 92) {
k += 2
continue
}
}
if (cc === next) break
k++
}
if (k < len) {
let tail = k + 1
let tc = tail < len ? str.charCodeAt(tail) : 0
while (tail < len && isWhitespace(tc) && tc !== 10) {
tail++
tc = tail < len ? str.charCodeAt(tail) : 0
}
if (tail >= len || tc === 10 || tc === 35) i = j
}
}
}

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

if (c === 39 /* ' */ || c === 34 /* " */ || c === 96 /* ` */) {
const quote = c
const quoteStart = i
const vStart = i + 1
let j = vStart
while (j < len) {
Expand Down Expand Up @@ -190,9 +236,19 @@ function parseFast (src) {
value = value.replace(/\\n/g, '\n').replace(/\\r/g, '\r')
}
// trailing ws + optional comment
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9)) i++
while (i < len && ((c = str.charCodeAt(i)) === 32 || c === 9 || c === 65279)) i++
if (i < len && c !== 10 && isWhitespace(c)) return parseRegex(str)
if (i < len && str.charCodeAt(i) === 35) {
while (i < len && str.charCodeAt(i) !== 10) i++
} else if (i < len && str.charCodeAt(i) !== 10) {
// A closing quote followed by non-comment text is an unquoted value
// in the classic parser, including the quote characters themselves.
let end = str.indexOf('\n', i)
if (end === -1) end = len
const hash = str.indexOf('#', i)
if (hash !== -1 && hash < end) end = hash
value = str.slice(quoteStart, end).trim()
i = end
}
}
} else {
Expand All @@ -205,8 +261,13 @@ function parseFast (src) {
let end = hash
while (end > vStart) {
const cc = str.charCodeAt(end - 1)
if (cc === 32 || cc === 9) end--
else break
if (cc === 32 || cc === 9) {
end--
} else if (isWhitespace(cc)) {
return parseRegex(str)
} else {
break
}
}
value = vStart === end ? '' : str.slice(vStart, end)
i = hash === nl ? hash : nl
Expand Down
46 changes: 46 additions & 0 deletions tests/test-parse-fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,52 @@ t.test('fast parse matches classic parse for an escaped backslash before the clo
ct.end()
})

t.test('fast parse matches classic parse when quoted values start on a later line', ct => {
const cases = [
'A=\n\n"hello\nworld"',
"A=\n'b'",
'A=\n `value`\nB=two'
]

for (const src of cases) {
assertSameParse(ct, src, JSON.stringify(src))
}

ct.same(dotenv.parse('A=\n\n"hello\nworld"', { fast: true }), { A: 'hello\nworld' })
ct.end()
})

t.test('fast parse matches classic parse for JavaScript whitespace', ct => {
const cases = [
'\fA=1',
'\vA=1',
'\u00A0A=1',
'A=\f"value"',
'A=\u00A0"value"'
]

for (const src of cases) {
assertSameParse(ct, src, JSON.stringify(src))
}

ct.end()
})

t.test('fast parse matches classic parse for junk after a closing quote', ct => {
const cases = [
'TOKEN="abc" oops',
"TOKEN='abc' oops # comment",
'TOKEN=`abc` oops\nNEXT=ok'
]

for (const src of cases) {
assertSameParse(ct, src, JSON.stringify(src))
}

ct.same(dotenv.parse('TOKEN="abc" oops', { fast: true }), { TOKEN: '"abc" oops' })
ct.end()
})

t.test('config({ fast: true }) reads a .env written with a BOM', ct => {
const processEnv = {}
const result = dotenv.config({
Expand Down