All files / lib/agent parsers.js

100% Statements 52/52
87.5% Branches 7/8
100% Functions 17/17
100% Lines 52/52

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    10x   10x                   10x 1x 1x 1x 1x   1x 1x                       10x 2x 2x 2x 6x   2x 2x 2x 1x   1x                             10x 1x 1x 1x 3x   1x 1x                         10x 2x 2x 2x 5x   2x     2x 2x   1x 1x 1x   2x                             1x 1x 3x   1x 1x     10x 10x 10x                         10x 4x   4x 1x     3x     10x  
"use strict";
 
const parseString = require("xml2js").parseString;
 
const parsers = {};
 
/**
 * Plain text parser for superagent
 *
 * @static
 *
 * @param {Object} res response from the HTTP server
 * @param {Function} done fuunction which is done when all response is readed
 */
parsers["text/plain"] = function parsePlainText(res, done) {
  res.text = "";
  res.setEncoding("utf8");
  res.on("data", function (chunk) {
    res.text += chunk;
  });
  res.on("end", function () {
    done(null, res.text);
  });
};
 
/**
 * XML parser for superagent
 *
 * @static
 *
 * @param {Object} res response from the HTTP server
 * @param {Function} done fuunction which is done when all response is readed and parase
 */
parsers["application/xml"] = function parseXML(res, done) {
  res.text = "";
  res.setEncoding("utf8");
  res.on("data", function (chunk) {
    res.text += chunk;
  });
  res.on("end", function () {
    parseString(res.text, function (err, output) {
      if (err) {
        done(err);
      } else {
        done(null, output);
      }
    });
  });
};
 
/**
 * HTML parser for superagent. Just collect text. HTML parseng by jsdom
 * is postponed for performace issues
 *
 * @static
 *
 * @param {Object} res response from the HTTP server
 * @param {Function} done fuunction which is done when all response is readed and parase
 */
parsers["text/html"] = function parseHTML(res, done) {
  res.text = "";
  res.setEncoding("utf8");
  res.on("data", function (chunk) {
    res.text += chunk;
  });
  res.on("end", function () {
    done(null, res.text);
  });
};
 
/**
 * JSON parser for superagent. Just collect text and parse
 * JSON when all response is received
 *
 * @static
 *
 * @param {Object} res response from the HTTP server
 * @param {Function} done fuunction which is done when all response is readed and parase
 */
parsers["application/json"] = function parseJSON(res, done) {
  res.text = "";
  res.setEncoding("utf8");
  res.on("data", (chunk) => {
    res.text += chunk;
  });
  res.on("end", () => {
    let body;
    let err;
    try {
      body = res.text && JSON.parse(res.text);
    } catch (e) {
      err = e;
      err.rawResponse = res.text || null;
      err.statusCode = res.statusCode;
    } finally {
      done(err, body);
    }
  });
};
 
/**
 * JSON parser for superagent. Just collect text and parse
 * JSON when all response is received
 *
 * @static
 *
 * @param {Object} res response from the HTTP server
 * @param {Function} done fuunction which is done when all response is readed and parase
 */
function parseBinary(res, done) {
  let binaryBuffer = Buffer.alloc(0);
  res.on("data", (chunk) => {
    binaryBuffer = Buffer.concat([binaryBuffer, chunk]);
  });
  res.on("end", () => {
    done(null, binaryBuffer);
  });
}
parsers["application/pdf"] = parseBinary;
parsers["application/vnd.ms-excel"] = parseBinary;
parsers["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"] =
  parseBinary;
 
/**
 * Parse response from count. Raises error for invalid
 * number in body
 *
 * @param {String} responseText response
 *
 * @return {Number} returns
 *
 * @memberof QueryableResource
 */
parsers.count = function (responseText) {
  const count = parseInt(responseText, 10);
 
  if (isNaN(count)) {
    throw new Error("Backend returns invalid count value.");
  }
 
  return count;
};
 
module.exports = parsers;