@pdz/df
Version:
Distant function loader for NodeJS
27 lines • 797 B
JavaScript
const pdzdf = (url) => {
return new Promise((pres, prej) => {
try {
require(url.match(/^https:/) ? 'https' : 'http')[typeof url == 'string' ? 'get' : 'request'](url, (resp) => {
if([301, 302, 303, 307, 308].includes(resp.statusCode) && resp.headers.location) {
try {
const u = new URL(resp.headers.location, url).href;
pdzdf(u).then(pres).catch(prej);
return;
} catch(e) { prej(e) }
}
if(resp.statusCode == 200) {
resp.setEncoding('utf8');
let buf = '';
resp.on('data', c => buf += c);
resp.on('end', () => {
try {
const f = new Function(buf);
pres(f);
} catch(e) { prej(e) }
});
}
}).on('error', e => prej(e));
} catch(e) { prej(e) }
});
}
module.exports = pdzdf;