Server IP : 85.214.239.14 / Your IP : 3.147.58.83 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/task/3/root/proc/3/root/lib/node_modules/pm2/node_modules/needle/lib/ |
Upload File : |
////////////////////////////////////////// // Defines mappings between content-type // and the appropriate parsers. ////////////////////////////////////////// var Transform = require('stream').Transform; var sax = require('sax'); function parseXML(str, cb) { var obj, current, parser = sax.parser(true, { trim: true, lowercase: true }) parser.onerror = parser.onend = done; function done(err) { parser.onerror = parser.onend = function() { } cb(err, obj) } function newElement(name, attributes) { return { name: name || '', value: '', attributes: attributes || {}, children: [] } } parser.ontext = function(t) { if (current) current.value += t } parser.onopentag = function(node) { var element = newElement(node.name, node.attributes) if (current) { element.parent = current current.children.push(element) } else { // root object obj = element } current = element }; parser.onclosetag = function() { if (typeof current.parent !== 'undefined') { var just_closed = current current = current.parent delete just_closed.parent } } parser.write(str).close() } function parserFactory(name, fn) { function parser() { var chunks = [], stream = new Transform({ objectMode: true }); // Buffer all our data stream._transform = function(chunk, encoding, done) { chunks.push(chunk); done(); } // And call the parser when all is there. stream._flush = function(done) { var self = this, data = Buffer.concat(chunks); try { fn(data, function(err, result) { if (err) throw err; self.push(result); }); } catch (err) { self.push(data); // just pass the original data } finally { done(); } } return stream; } return { fn: parser, name: name }; } var parsers = {} function buildParser(name, types, fn) { var parser = parserFactory(name, fn); types.forEach(function(type) { parsers[type] = parser; }) } buildParser('json', [ 'application/json', 'text/javascript' ], function(buffer, cb) { var err, data; try { data = JSON.parse(buffer); } catch (e) { err = e; } cb(err, data); }); buildParser('xml', [ 'text/xml', 'application/xml', 'application/rdf+xml', 'application/rss+xml', 'application/atom+xml' ], function(buffer, cb) { parseXML(buffer.toString(), function(err, obj) { cb(err, obj) }) }); module.exports = parsers; module.exports.use = buildParser;