get-blacklist.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var https = require('https');
  3. var concat = require('concat-stream');
  4. var url = 'https://raw.githubusercontent.com/gulpjs/plugins/master/src/blackList.json';
  5. function collect(stream, cb) {
  6. stream.on('error', cb);
  7. stream.pipe(concat(onSuccess));
  8. function onSuccess(result) {
  9. cb(null, result);
  10. }
  11. }
  12. function parse(str, cb) {
  13. try {
  14. cb(null, JSON.parse(str));
  15. } catch (err) {
  16. /* istanbul ignore next */
  17. cb(new Error('Invalid Blacklist JSON.'));
  18. }
  19. }
  20. // TODO: Test this impl
  21. function getBlacklist(cb) {
  22. https.get(url, onRequest);
  23. function onRequest(res) {
  24. /* istanbul ignore if */
  25. if (res.statusCode !== 200) {
  26. // TODO: Test different status codes
  27. return cb(new Error('Request failed. Status Code: ' + res.statusCode));
  28. }
  29. res.setEncoding('utf8');
  30. collect(res, onCollect);
  31. }
  32. function onCollect(err, result) {
  33. /* istanbul ignore if */
  34. if (err) {
  35. return cb(err);
  36. }
  37. parse(result, onParse);
  38. }
  39. function onParse(err, blacklist) {
  40. /* istanbul ignore if */
  41. if (err) {
  42. return cb(err);
  43. }
  44. cb(null, blacklist);
  45. }
  46. }
  47. module.exports = getBlacklist;