[6a3a178] | 1 | //////////////////////////////////////////
|
---|
| 2 | // Defines mappings between content-type
|
---|
| 3 | // and the appropriate parsers.
|
---|
| 4 | //////////////////////////////////////////
|
---|
| 5 |
|
---|
| 6 | var Transform = require('stream').Transform;
|
---|
| 7 | var sax = require('sax');
|
---|
| 8 |
|
---|
| 9 | function parseXML(str, cb) {
|
---|
| 10 | var obj, current, parser = sax.parser(true, { trim: true, lowercase: true })
|
---|
| 11 | parser.onerror = parser.onend = done;
|
---|
| 12 |
|
---|
| 13 | function done(err) {
|
---|
| 14 | parser.onerror = parser.onend = function() { }
|
---|
| 15 | cb(err, obj)
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | function newElement(name, attributes) {
|
---|
| 19 | return {
|
---|
| 20 | name: name || '',
|
---|
| 21 | value: '',
|
---|
| 22 | attributes: attributes || {},
|
---|
| 23 | children: []
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | parser.oncdata = parser.ontext = function(t) {
|
---|
| 28 | if (current) current.value += t
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | parser.onopentag = function(node) {
|
---|
| 32 | var element = newElement(node.name, node.attributes)
|
---|
| 33 | if (current) {
|
---|
| 34 | element.parent = current
|
---|
| 35 | current.children.push(element)
|
---|
| 36 | } else { // root object
|
---|
| 37 | obj = element
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | current = element
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | parser.onclosetag = function() {
|
---|
| 44 | if (typeof current.parent !== 'undefined') {
|
---|
| 45 | var just_closed = current
|
---|
| 46 | current = current.parent
|
---|
| 47 | delete just_closed.parent
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | parser.write(str).close()
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | function parserFactory(name, fn) {
|
---|
| 55 |
|
---|
| 56 | function parser() {
|
---|
| 57 | var chunks = [],
|
---|
| 58 | stream = new Transform({ objectMode: true });
|
---|
| 59 |
|
---|
| 60 | // Buffer all our data
|
---|
| 61 | stream._transform = function(chunk, encoding, done) {
|
---|
| 62 | chunks.push(chunk);
|
---|
| 63 | done();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // And call the parser when all is there.
|
---|
| 67 | stream._flush = function(done) {
|
---|
| 68 | var self = this,
|
---|
| 69 | data = Buffer.concat(chunks);
|
---|
| 70 |
|
---|
| 71 | try {
|
---|
| 72 | fn(data, function(err, result) {
|
---|
| 73 | if (err) throw err;
|
---|
| 74 | self.push(result);
|
---|
| 75 | });
|
---|
| 76 | } catch (err) {
|
---|
| 77 | self.push(data); // just pass the original data
|
---|
| 78 | } finally {
|
---|
| 79 | done();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return stream;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | return { fn: parser, name: name };
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | var parsers = {}
|
---|
| 90 |
|
---|
| 91 | function buildParser(name, types, fn) {
|
---|
| 92 | var parser = parserFactory(name, fn);
|
---|
| 93 | types.forEach(function(type) {
|
---|
| 94 | parsers[type] = parser;
|
---|
| 95 | })
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | buildParser('json', [
|
---|
| 99 | 'application/json',
|
---|
| 100 | 'text/javascript',
|
---|
| 101 | 'application/vnd.api+json'
|
---|
| 102 | ], function(buffer, cb) {
|
---|
| 103 | var err, data;
|
---|
| 104 | try { data = JSON.parse(buffer); } catch (e) { err = e; }
|
---|
| 105 | cb(err, data);
|
---|
| 106 | });
|
---|
| 107 |
|
---|
| 108 | buildParser('xml', [
|
---|
| 109 | 'text/xml',
|
---|
| 110 | 'application/xml',
|
---|
| 111 | 'application/rdf+xml',
|
---|
| 112 | 'application/rss+xml',
|
---|
| 113 | 'application/atom+xml'
|
---|
| 114 | ], function(buffer, cb) {
|
---|
| 115 | parseXML(buffer.toString(), function(err, obj) {
|
---|
| 116 | cb(err, obj)
|
---|
| 117 | })
|
---|
| 118 | });
|
---|
| 119 |
|
---|
| 120 | module.exports = parsers;
|
---|
| 121 | module.exports.use = buildParser;
|
---|