all files / lib/ vaticanResponse.js

22.22% Statements 10/45
0% Branches 0/20
0% Functions 0/9
22.22% Lines 10/45
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                                                                                                                                                                                
var _ = require("lodash"),
    logger = require("./logger");
 
//Custom response 
 
function VaticanResponse(originalResp, originalReq, options, postProcessChain) {
    this.response = originalResp;
    this.request = originalReq;
    this.statusCode = 200; //default response code, used on the send method
    this.options = options;
    this.headers = [];
    this.ppChain = postProcessChain;
    this.body = "";
}
 
VaticanResponse.prototype.send = function(txt) {
    var headers = {};
    var self = this;
    this.body = txt;
    this.ppChain.runChain(this.request, this, function(req, resp) {
        //Check for CORS config
        if(self.options.cors !== false) {
            headers = _getCORSHeaders(self.options.cors);
        }
 
        //Adds the rest of the headers
        for(var i in resp.headers) {
            headers = _.assign(headers, resp.headers[i]);
        }
 
        //Write the headers
        resp.response.writeHead(resp.statusCode, headers);
        if( typeof resp.body == 'object') {
            resp.body = JSON.stringify(resp.body);
        }
 
        //Write out the response text
        resp.response.write(resp.body);
        resp.response.end();
    })
    
};
 
VaticanResponse.prototype.write = function(txt) {
    this.response.write(txt);
}
 
VaticanResponse.prototype.end = function() {
    this.response.end();
}
 
VaticanResponse.prototype.setHeader = function(head) {
    if(!Array.isArray(this.headers)) {
        this.headers = [];
    }
    var header = {};
    header[head[0]] = head[1];
    this.headers.push(header);
};
 
///Module level methods
 
function _improveResponse(resp, req, options, postprocessChain) {
   var res = new VaticanResponse(resp, req, options, postprocessChain);
   return res;
}
/*
    Writes the 404 response
*/
function _writeNotFound( res ){ 
    logger.warn("404 Not found");
    res.writeHead(404);
    res.end();
}
 
function _getCORSHeaders(corsOpts) {
    if ( corsOpts === true ) {
       return {
        "Access-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "*"
       } 
    } else {
        return {
            "Access-Allow-Origin":              corsOpts.access_allow_origin || "",
            "Access-Control-Allow-Methods":     corsOpts.acess_control_allow_methods || "",
            "Access-Control-Expose-Headers":    corsOpts.access_control_expose_headers || "",
            "Access-Control-Max-Age":           corsOpts.access_control_max_age || "" ,
            "Access-Control-Allow-Credentials": corsOpts.access_control_allow_credentials || "",
            "Access-Control-Allow-Headers":     corsOpts.access_control_allow_headers || ""
        }
    }
}
 
 
module.exports = {
    improveResponse: _improveResponse,
    writeNotFound: _writeNotFound
};