All files / bintra/controllers Feeds.js

94.91% Statements 56/59
72.72% Branches 8/11
81.25% Functions 13/16
94.91% Lines 56/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202              1x 1x   1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x               6x                                             6x 6x           6x                 3x                 1x 1x                 2x   2x   2x 1x 1x                 2x     2x                         2x   2x   2x 1x 1x               2x     2x                         2x   2x   2x 1x 1x                 2x     2x                       1x 7x   7x   2x 2x   2x 2x   2x 2x   1x 1x 1x      
/**
 * API controller for feed support.
 * @module controllers/feeds
 * @license MIT
 * @author Kai KRETSCHMANN <kai@kretschmann.consulting>
 */
 
const PackagesService = require('../service/PackagesService');
const eventEmitter = require('../utils/eventer').em;
 
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = process.env.LOGLEVEL || /* istanbul ignore next */ 'warn'; // LCOV_EXCL_LINE
 
const Feed = require('feed').Feed;
const maxFeedItems = 25;
const maxFeedAge = 30;
const sortItemFeed = 'tsupdated';
const sortDirectionFeed = 'down';
 
const API_BASE_URL = 'https://api.binarytransparency.net/v1/';
const WEB_URL = 'https://bintra.directory/';
const MY_NAME = 'Kai KRETSCHMANN';
const MY_EMAIL = 'kai@kretschmann.consulting';
const MY_WEB_URL = 'https://kai.kretschmann.consulting';
const EVENTNAME = 'apihit';
 
/**
 * generate common meta data object..
 * @function getInitialFeed
 * @private
 */
function getInitialFeed () {
  const myfeed = new Feed({
    title: 'Binary Transparency Directory',
    description: 'Feed for hash code monitoring',
    id: WEB_URL,
    link: WEB_URL,
    language: 'en', // optional, used only in RSS 2.0, possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
    image: WEB_URL + 'image.png',
    favicon: WEB_URL + 'favicon.ico',
    copyright: 'All rights reserved 2021, ' + MY_NAME,
    updated: new Date(),
    generator: 'Feed for bintra', // optional, default = 'Feed for Node.js'
    feedLinks: {
      json: API_BASE_URL + 'feed.json',
      rss: API_BASE_URL + 'feed.rss',
      atom: API_BASE_URL + 'feed.atom'
    },
    author: {
      name: MY_NAME,
      email: MY_EMAIL,
      link: MY_WEB_URL
    }
  });
 
  myfeed.addCategory('Technology');
  myfeed.addContributor({
    name: MY_NAME,
    email: MY_EMAIL,
    link: MY_WEB_URL
  });
 
  return myfeed;
}
 
/**
 * Show single entry data.
 * @function createContent
 * @private
 */
function createContent (entry) {
  return `Archive ${entry.name}, version ${entry.version} for ${entry.arch}, ${entry.family} with hash ${entry.hash}`;
}
 
/**
 * Reply on error.
 * @function replyWithError
 * @private
 */
function replyWithError (res, payload) {
  res.writeHead(500, { 'Content-Type': 'text/plain' });
  return res.end('error ' + payload);
}
 
/**
 * Show feed data in wanted format.
 * @function feedRss
 * @private
 */
function feedRss (res) {
  const rssfeed = getInitialFeed();
 
  PackagesService.listPackages(0, maxFeedItems, sortItemFeed, sortDirectionFeed, maxFeedAge)
    .then(function (items) {
      items.forEach(function (entry) {
        const myid = entry._id;
        rssfeed.addItem({
          title: entry.name,
          id: API_BASE_URL + 'package/' + myid,
          link: WEB_URL + 'details/?id=' + myid,
          description: entry.name,
          date: entry.tsupdated || new Date(),
          content: createContent(entry)
        });
      });
      res.writeHead(200, {
        'Content-Type': 'application/rss+xml'
      });
      return res.end(rssfeed.rss2());
    })
    .catch(function (payload) {
      return replyWithError(res, payload); // LCOV_EXCL_LINE
    });
}
 
/**
 * Show feed data in wanted format.
 * @function feedAtom
 * @private
 */
function feedAtom (res) {
  const atomfeed = getInitialFeed();
 
  PackagesService.listPackages(0, maxFeedItems, sortItemFeed, sortDirectionFeed, maxFeedAge)
    .then(function (items) {
      items.forEach(function (entry) {
        const myid = entry._id;
        atomfeed.addItem({
          title: entry.name,
          link: API_BASE_URL + 'package/' + myid,
          description: entry.name,
          date: entry.tsupdated || new Date(),
          content: createContent(entry)
        });
      });
      res.writeHead(200, {
        'Content-Type': 'application/rss+xml'
      });
      return res.end(atomfeed.atom1());
    })
    .catch(function (payload) {
      return replyWithError(res, payload); // LCOV_EXCL_LINE
    });
}
 
/**
 * Show feed data in wanted format.
 * @function feedJson
 * @private
 */
function feedJson (res) {
  const jsonfeed = getInitialFeed();
 
  PackagesService.listPackages(0, maxFeedItems, sortItemFeed, sortDirectionFeed, maxFeedAge)
    .then(function (items) {
      items.forEach(function (entry) {
        const myid = entry._id;
        jsonfeed.addItem({
          title: entry.name,
          id: entry._id,
          link: API_BASE_URL + 'package/' + myid,
          description: entry.name,
          date: entry.tsupdated || new Date(),
          content: createContent(entry)
        });
      });
      res.writeHead(200, {
        'Content-Type': 'application/json'
      });
      return res.end(jsonfeed.json1());
    })
    .catch(function (payload) {
      return replyWithError(res, payload); // LCOV_EXCL_LINE
    });
}
 
/**
 * Show feed data in wanted format.
 * @function bintraFeed
 * @public
 */
module.exports.bintraFeed = function bintraFeed (req, res, _next, type) {
  eventEmitter.emit(EVENTNAME, req);
 
  switch (type) {
    case 'rss':
      feedRss(res);
      break;
    case 'atom':
      feedAtom(res);
      break;
    case 'json':
      feedJson(res);
      break;
    default:
      logger.error('Wrong type ' + type);
      replyWithError(res, 'Wrong type');
      break;
  }
};