Skip to content

Commit 97c09f6

Browse files
committed
Handle stream errors gracefully
Now should catch and emit errors from lib/expr.js, avoid infinite loops on unclosed braces, and stop emitting data after running into an error
1 parent 34c2293 commit 97c09f6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function parser() {
115115

116116
var stream = through(write, end)
117117
, check = arguments.length ? [].slice.call(arguments) : []
118+
, ended = false
118119
, depth = 0
119120
, state = []
120121
, tokens = []
@@ -266,8 +267,8 @@ function parser() {
266267
emit = true
267268
}
268269

269-
if(emit) stream.emit('data', _node)
270-
270+
if(emit && !errored) stream.emit('data', _node)
271+
271272
node = _node.parent
272273
return _node
273274
}
@@ -307,8 +308,8 @@ function parser() {
307308
return tokens.shift(), state.shift()
308309
}
309310
switch(token.type) {
310-
case 'eof': return state.shift()
311-
case 'keyword':
311+
case 'eof': return got_eof()
312+
case 'keyword':
312313
switch(token.data) {
313314
case 'for': return state.unshift(forstmt());
314315
case 'if': return state.unshift(ifstmt());
@@ -346,6 +347,12 @@ function parser() {
346347
}
347348
}
348349

350+
function got_eof() {
351+
if (ended) errored = true
352+
ended = true
353+
return state.shift()
354+
}
355+
349356
function parse_decl() {
350357
var stmt = state[0]
351358

@@ -584,7 +591,14 @@ function parser() {
584591
return
585592

586593
function parseexpr(tokens) {
587-
return full_parse_expr(state, tokens), state.shift()
594+
try {
595+
full_parse_expr(state, tokens)
596+
} catch(err) {
597+
stream.emit('error', err)
598+
errored = true
599+
}
600+
601+
return state.shift()
588602
}
589603
}
590604

0 commit comments

Comments
 (0)