UNPKG

react-native-vast-client

Version:
50 lines (43 loc) 1.23 kB
const uri = require('url'); const fs = require('fs'); const http = require('http'); const https = require('https'); const DOMParser = require('xmldom').DOMParser; function get(url, options, cb) { url = uri.parse(url); const httpModule = url.protocol === 'https:' ? https : http; if (url.protocol === 'file:') { fs.readFile(url.pathname, 'utf8', function(err, data) { if (err) { return cb(err); } const xml = new DOMParser().parseFromString(data); cb(null, xml); }); } else { let timing; let data = ''; const timeoutWrapper = req => () => req.abort(); const req = httpModule.get(url.href, function(res) { res.on('data', function(chunk) { data += chunk; clearTimeout(timing); timing = setTimeout(fn, options.timeout || 120000); }); res.on('end', function() { clearTimeout(timing); const xml = new DOMParser().parseFromString(data); cb(null, xml); }); }); req.on('error', function(err) { clearTimeout(timing); cb(err); }); const fn = timeoutWrapper(req); timing = setTimeout(fn, options.timeout || 120000); } } export const nodeURLHandler = { get };