-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
70 lines (57 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
const xml2js = require('xml2js');
const DEFAULT_TYPES = ['*/xml', '+xml'];
module.exports = function (bodyParser) {
if (bodyParser.xml) {
// We already setup the XML parser.
// End early.
return;
}
function xml(options) {
options = options || {};
options.type = options.type || DEFAULT_TYPES;
const textParser = bodyParser.text(options);
return function xmlParser(req, res, next) {
// First, run the body through the text parser.
textParser(req, res, function (err) {
if (err) {
return next(err);
}
if (typeof req.body !== 'string') {
return next();
}
// Then, parse as XML.
const xmlParseOptions = {
...options.xmlParseOptions,
// Always opt-in to async mode.
async: true,
};
const parser = new xml2js.Parser(xmlParseOptions);
// In some cases xml2js.Parser parseString() can throw an error after executing the callback.
parser.parseString(req.body, function (err, xml) {
if (err) {
err.status = 400;
return next(err);
}
if (xml) {
// Guard against prototype pollution
delete xml.__proto__;
delete xml.constructor;
delete xml.prototype;
// Set result on the request body
req.body = xml;
}
next();
});
});
};
}
// Finally add the `xml` function to the bodyParser.
Object.defineProperty(bodyParser, 'xml', {
configurable: true,
enumerable: true,
get: function () {
return xml;
},
});
};