Skip to content

Commit f8a6980

Browse files
committed
Guard against multiple calls of done callback
1 parent 8db74d2 commit f8a6980

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ module.exports = function(bodyParser) {
2727
if(typeof req.body !== 'string') { return next(); }
2828

2929
// Then, parse as XML.
30-
const parser = new xml2js.Parser(options.xmlParseOptions);
30+
const xmlParseOptions = {
31+
...options.xmlParseOptions,
32+
// Always opt-in to async mode.
33+
async: true
34+
};
35+
const parser = new xml2js.Parser(xmlParseOptions);
36+
37+
// In some cases xml2js.Parser parseString() can throw an error after executing the callback.
38+
3139
parser.parseString(req.body, function(err, xml) {
3240
if(err) {
3341
err.status = 400;

test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ describe('XML Body Parser', function() {
9393

9494
it('should ignore non-XML', function(done) {
9595
createServer();
96-
9796
request(app)
9897
.post('/')
9998
.set('Content-Type', 'text/plain')
@@ -109,4 +108,14 @@ describe('XML Body Parser', function() {
109108
.send('<invalid-xml>')
110109
.expect(400, done);
111110
});
111+
112+
it('should not throw an exception after the callback is returned', function(done) {
113+
// Guards against https://github.com/Leonidas-from-XIV/node-xml2js/issues/400
114+
createServer();
115+
request(app)
116+
.post('/')
117+
.set('Content-Type', 'text/xml')
118+
.send('x<foo>test</foo><bar>test</bar></data>')
119+
.expect(400, done);
120+
})
112121
});

0 commit comments

Comments
 (0)