Code coverage report for lib\streams\HttpStream.js

Statements: 65.52% (38 / 58)      Branches: 71.43% (10 / 14)      Functions: 71.43% (5 / 7)      Lines: 65.52% (38 / 58)      Ignored: none     

All files » lib\streams\ » HttpStream.js
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  1 1 1 1     1 15 11 11 8   3 3   4     1                               1   1 1 1 1     1 9 9 8                               9 2     9 9 9 9     1 2 2 2 2 2   2     1 4     1
'use strict';
var http = require('http');
var kefir = require('kefir');
var Request = require('../atoms/Request.js');
var BaseStream = require('./BaseStream.js');
 
 
var HttpStream = function HttpStream(port, host) {
    if (this instanceof HttpStream) {
        this.init();
        if (port === undefined) {
            return this;
        }
        this.start(port, host);
        return this;
    }
    return new HttpStream(port, host);
};
 
HttpStream.prototype.init = function init(emitter, isNew) {
    var self = this;
    if (isNew) {
        self = new self.constructor();
        self.port = this.port;
        self.host = this.host;
        self.http = this.http;
    }
    if (emitter === undefined) {
        self.emitter = kefir.emitter();
        return self;
    }
    self.emitter = emitter;
    return self;
};
 
HttpStream.prototype = new BaseStream();
 
HttpStream.prototype.http = null;
HttpStream.prototype.port = null;
HttpStream.prototype.host = null;
HttpStream.prototype.constructor = HttpStream;
// Kefir observable subscribe methods proxing
 
HttpStream.prototype.listen = function listenHttp(port, host) {
    var self = this;
    if (self.http === null) {
        self.http = http.createServer(function bindEmitter(req, res) {
            var request = new Request(self);
 
            request.url = req.url;
            request.res = res;
            request.headers = req.headers;
            request.method = req.method;
 
            request.type = 'text/plain';
            request.code = 200;
            request.body = '';
 
            self.emitter.emit(request);
        });
    }
 
    if (port === undefined) {
        port = 3002;
    }
 
    self.http.listen(port, host);
    self.port = port;
    self.host = host || '0.0.0.0';
    return this;
};
 
HttpStream.prototype.end = function end() {
    var self = this;
    this.http.close(function closeConnection() {
        delete self.http;
        self.http = null;
        self.emitter.end();
    });
    return this;
};
 
HttpStream.prototype.start = function startStream(port, host) {
    return this.listen(port, host);
};
 
module.exports = HttpStream;