<commit_before>'use strict'

module.exports = makeTransform
module.exports.async = makeTransformAsync

function makeTransform(fn) {
  return function (file, source) {
    if (typeof source === 'string') {
      return fn(file, source)
    }
    return new FakeStream(function (buffer, destination, onError) {
      var res
      try {
        res = fn(file, buffer)
      } catch (ex) {
        return onError(ex)
      }
      destination.end(res)
    })
  }
}
function makeTransformAsync(fn) {
  return function (file, source, callback) {
    if (typeof source === 'string' && typeof callback === 'function') {
      return fn(file, source, callback)
    }
    return new FakeStream(function (buffer, destination, onError) {
      fn(file, buffer, function (err, res) {
        if (err) return onError(err)
        destination.end(res)
      })
    })
  }
}

function FakeStream(finished) {
  this.errorHandlers = []
  this.piped = null
  this.buffer = ''
  this.ended = false
  this.finished = finished
}
FakeStream.prototype.on = function (name, handler) {
  if (name !== 'error') return this
  this.errorHandlers.push(handler)
  return this
}
FakeStream.prototype.once = FakeStream.prototype.on
FakeStream.prototype.emit = function (name, data) {
  if (name !== 'error') return this
  for (var i = this.errorHandlers.length - 1; i >= 0; i--) {
    this.errorHandlers[i](data)
  }
  return this
}
FakeStream.prototype.pipe = function (strm) {
  if (arguments.length > 1) {
    throw new Error('make-transform returns a FakeStream, it doesn\'t support additional arguments to pipe')
  }
  if (this.piped) {
    throw new Error('make-transform returns a FakeStream, it doesn\'t support being piped multiple times')
  }
  this.piped = strm
  this.finish()
  return strm
}
FakeStream.prototype.write = function (str) {
  this.buffer += str.toString('utf8')
  return true
}
FakeStream.prototype.finish = function () {
  if (this.ended && this.piped) {
    this.finished(this.buffer, this.piped, this.emit.bind(this, 'error'))
  }
}
FakeStream.prototype.end = function (str) {
  if (str) this.write(str)
  this.ended = true
  this.finish()
}<commit_msg>Add noop removeListener<commit_after>'use strict'

module.exports = makeTransform
module.exports.async = makeTransformAsync

function makeTransform(fn) {
  return function (file, source) {
    if (typeof source === 'string') {
      return fn(file, source)
    }
    return new FakeStream(function (buffer, destination, onError) {
      var res
      try {
        res = fn(file, buffer)
      } catch (ex) {
        return onError(ex)
      }
      destination.end(res)
    })
  }
}
function makeTransformAsync(fn) {
  return function (file, source, callback) {
    if (typeof source === 'string' && typeof callback === 'function') {
      return fn(file, source, callback)
    }
    return new FakeStream(function (buffer, destination, onError) {
      fn(file, buffer, function (err, res) {
        if (err) return onError(err)
        destination.end(res)
      })
    })
  }
}

function FakeStream(finished) {
  this.errorHandlers = []
  this.piped = null
  this.buffer = ''
  this.ended = false
  this.finished = finished
}
FakeStream.prototype.on = function (name, handler) {
  if (name !== 'error') return this
  this.errorHandlers.push(handler)
  return this
}
FakeStream.prototype.once = FakeStream.prototype.on
FakeStream.prototype.emit = function (name, data) {
  if (name !== 'error') return this
  for (var i = this.errorHandlers.length - 1; i >= 0; i--) {
    this.errorHandlers[i](data)
  }
  return this
}
FakeStream.prototype.removeListener = function () {}
FakeStream.prototype.pipe = function (strm) {
  if (arguments.length > 1) {
    throw new Error('make-transform returns a FakeStream, it doesn\'t support additional arguments to pipe')
  }
  if (this.piped) {
    throw new Error('make-transform returns a FakeStream, it doesn\'t support being piped multiple times')
  }
  this.piped = strm
  this.finish()
  return strm
}
FakeStream.prototype.write = function (str) {
  this.buffer += str.toString('utf8')
  return true
}
FakeStream.prototype.finish = function () {
  if (this.ended && this.piped) {
    this.finished(this.buffer, this.piped, this.emit.bind(this, 'error'))
  }
}
FakeStream.prototype.end = function (str) {
  if (str) this.write(str)
  this.ended = true
  this.finish()
}<|endoftext|>


<commit_before>/**
 * es2015-proxy-shim
 *
 * @license
 * Released under MIT license <https://raw.githubusercontent.com/dbo/es2015-proxy-shim/master/LICENSE>
 * Copyright (c) Daniel Bölzle
 */

/* global global, self, console */
/* eslint-disable eqeqeq, no-console */

(function(global) {
    "use strict";

    if (global.Proxy) {
        return;
    }
    if (!global.Reflect) {
        console.warn("es2015-proxy-shim: Missing Reflect.");
        return;
    }

    var reflSet = Reflect.set,
        reflGet = Reflect.get,
        UNSUPPORTED_TRAPS = new Set([
            "has",
            "deleteProperty",
            "defineProperty",
            "ownKeys",
            "getOwnPropertyDescriptor",
            "getPrototypeOf",
            "isExtensible",
            "preventExtensions",
            "setPrototypeOf"
        ]),
        PROXY_DEF = Symbol("proxy-def"),
        propsMapCache = new WeakMap();

    /**
     * Patched Reflect.get and Reflect.set to properly handle proxies.
     */
    Reflect.get = function(target, key, receiver) {
        if (receiver && receiver !== target) {
            var pdef = target[PROXY_DEF];
            if (typeof pdef !== "undefined") { // a proxy
                if (!pdef) {
                    throw new TypeError("Reflect.get on revoked target proxy.");
                }
                if (pdef.h.get) {
                    return pdef.h.get(pdef.t, key, receiver);
                }
                return Reflect.get(pdef.t, key, receiver);
            }
        }
        // fallback to original:
        return Reflect.apply(reflGet, Reflect, arguments);
    };
    Reflect.set = function(target, key, value, receiver) {
        if (receiver && receiver !== target) {
            var pdef = receiver[PROXY_DEF];
            if (typeof pdef !== "undefined") { // a proxy
                if (!pdef) {
                    throw new TypeError("Reflect.set on revoked proxy.");
                }

                var descr = pdef.pmap[key];
                if (descr && pdef.t === target) {
                    descr = descr.tdescr;
                    if (descr.hasOwnProperty("value")) {
                        if (!descr.writable) {
                            return false;
                        }
                        descr = Object.assign({}, descr);
                        descr.value = value;
                        Object.defineProperty(target, key, descr);
                    } else {
                        if (!descr.set) {
                            return false;
                        }
                        descr.set.call(receiver, value);
                    }
                    return true;
                }
                return Reflect.set(pdef.t, key, value, receiver);
            }
        }
        // fallback to original:
        return Reflect.apply(reflSet, Reflect, arguments);
    };

    /**
     * Used for get.
     */
    function getter(key) {
        return function() {
            var pdef = this[PROXY_DEF];
            if (!pdef) {
                throw new TypeError("Getting property on revoked proxy: " + key);
            }
            if (pdef.h.get) {
                return pdef.h.get(pdef.t, key, this);
            }
            return Reflect.get(pdef.t, key, this);
        };
    }
    /**
     * Used for set.
     */
    function setter(key) {
        return function(value) {
            var pdef = this[PROXY_DEF];
            if (!pdef) {
                throw new TypeError("Setting property on revoked proxy: " + key);
            }
            if (pdef.h.set) {
                if (pdef.h.set(pdef.t, key, value, this) === false) {
                    // assuming strict mode
                    throw new TypeError("Setting " + key + " on " + pdef.t + " has failed.");
                }
            } else {
                Reflect.set(pdef.t, key, value, this);
            }
        };
    }

    /**
     * Used for apply, construct.
     */
    function createFnProxy() {
        var proxy = function() {
            var pdef = proxy[PROXY_DEF];
            if (!pdef) {
                throw new TypeError("Calling on revoked proxy.");
            }
            if (this && this.constructor === proxy) {
                if (pdef.h.construct) {
                    var ret = pdef.h.construct(pdef.t, arguments, pdef.t); // TODO Array.from?
                    if (typeof ret !== "object") {
                        throw new TypeError("Constructor must create an Object.");
                    }
                    return ret;
                }
                return Reflect.construct(pdef.t, arguments);
            }
            if (pdef.h.apply) {
                return pdef.h.apply(pdef.t, this, arguments); // TODO Array.from?
            }
            return Reflect.apply(pdef.t, this, arguments);
        };
        return proxy;
    }

    /**
     * Creates and caches a property descriptor map for the given object installing getter/setter pairs.
     */
    function getPropsObjFor(obj) {
        if (obj == null) {
            return {};
        }

        var ret = propsMapCache.get(obj);
        if (ret) {
            return ret;
        }

        ret = getPropsObjFor(Object.getPrototypeOf(obj)) || {};

        var ownNames = Object.getOwnPropertyNames(obj),
            ownSymbols = Object.getOwnPropertySymbols(obj),
            index;

        index = ownSymbols.indexOf(PROXY_DEF);
        if (index >= 0) {
            ownSymbols.splice(index, 1);
        }
        index = ownNames.indexOf("prototype");
        if (index >= 0) {
            ownNames.splice(index, 1);
        }

        if (ownNames.length || ownSymbols.length) {
            ret = Object.assign({}, ret);
            var p, tdescr;
            for (p of ownNames) {
                tdescr = Object.getOwnPropertyDescriptor(obj, p);
                ret[p] = {
                    tdescr: tdescr,
                    get: getter(p),
                    set: setter(p),
                    enumerable: !!tdescr.enumerable
                };
            }
            for (p of ownSymbols) {
                tdescr = Object.getOwnPropertyDescriptor(obj, p);
                ret[p] = {
                    tdescr: tdescr,
                    get: getter(p),
                    set: setter(p),
                    enumerable: !!tdescr.enumerable
                };
            }
        }

        propsMapCache.set(obj, ret);
        return ret;
    }

    function Proxy(target, handler) {
        Object.getOwnPropertyNames(handler).forEach(function(trap) {
            if (UNSUPPORTED_TRAPS.has(trap)) {
                throw new TypeError("Unsupported trap: " + trap);
            }
        });

        var propsMap = getPropsObjFor(target),
            proxy;

        if (typeof target === "function") {
            proxy = createFnProxy();
            Object.defineProperties(proxy, propsMap);
        } else {
            proxy = Object.create(Object.getPrototypeOf(target), propsMap);
        }

        proxy[PROXY_DEF] = {
            h: handler,
            t: target,
            pmap: propsMap
        };
        Object.seal(proxy); // to notice once new properties are defined on it, i.e. a problem

        return proxy;
    }

    function revoker() {
        this[PROXY_DEF] = null;
    }

    Proxy.revocable = function revocable(target, handler) {
        var proxy = new Proxy(target, handler);

        return {
            proxy: proxy,
            revoke: revoker.bind(proxy)
        };
    };

    global.Proxy = Proxy;

})(typeof global === "object" ? global : self);
<commit_msg>feat: avoiding for-of loops (uglifyjs)<commit_after>/**
 * es2015-proxy-shim
 *
 * @license
 * Released under MIT license <https://raw.githubusercontent.com/dbo/es2015-proxy-shim/master/LICENSE>
 * Copyright (c) Daniel Bölzle
 */

/* global global, self, console */
/* eslint-disable eqeqeq, no-console */

(function(global) {
    "use strict";

    if (global.Proxy) {
        return;
    }
    if (!global.Reflect) {
        console.warn("es2015-proxy-shim: Missing Reflect.");
        return;
    }

    var reflSet = Reflect.set,
        reflGet = Reflect.get,
        UNSUPPORTED_TRAPS = new Set([
            "has",
            "deleteProperty",
            "defineProperty",
            "ownKeys",
            "getOwnPropertyDescriptor",
            "getPrototypeOf",
            "isExtensible",
            "preventExtensions",
            "setPrototypeOf"
        ]),
        PROXY_DEF = Symbol("proxy-def"),
        propsMapCache = new WeakMap();

    /**
     * Patched Reflect.get and Reflect.set to properly handle proxies.
     */
    Reflect.get = function(target, key, receiver) {
        if (receiver && receiver !== target) {
            var pdef = target[PROXY_DEF];
            if (typeof pdef !== "undefined") { // a proxy
                if (!pdef) {
                    throw new TypeError("Reflect.get on revoked target proxy.");
                }
                if (pdef.h.get) {
                    return pdef.h.get(pdef.t, key, receiver);
                }
                return Reflect.get(pdef.t, key, receiver);
            }
        }
        // fallback to original:
        return Reflect.apply(reflGet, Reflect, arguments);
    };
    Reflect.set = function(target, key, value, receiver) {
        if (receiver && receiver !== target) {
            var pdef = receiver[PROXY_DEF];
            if (typeof pdef !== "undefined") { // a proxy
                if (!pdef) {
                    throw new TypeError("Reflect.set on revoked proxy.");
                }

                var descr = pdef.pmap[key];
                if (descr && pdef.t === target) {
                    descr = descr.tdescr;
                    if (descr.hasOwnProperty("value")) {
                        if (!descr.writable) {
                            return false;
                        }
                        descr = Object.assign({}, descr);
                        descr.value = value;
                        Object.defineProperty(target, key, descr);
                    } else {
                        if (!descr.set) {
                            return false;
                        }
                        descr.set.call(receiver, value);
                    }
                    return true;
                }
                return Reflect.set(pdef.t, key, value, receiver);
            }
        }
        // fallback to original:
        return Reflect.apply(reflSet, Reflect, arguments);
    };

    /**
     * Used for get.
     */
    function getter(key) {
        return function() {
            var pdef = this[PROXY_DEF];
            if (!pdef) {
                throw new TypeError("Getting property on revoked proxy: " + key);
            }
            if (pdef.h.get) {
                return pdef.h.get(pdef.t, key, this);
            }
            return Reflect.get(pdef.t, key, this);
        };
    }
    /**
     * Used for set.
     */
    function setter(key) {
        return function(value) {
            var pdef = this[PROXY_DEF];
            if (!pdef) {
                throw new TypeError("Setting property on revoked proxy: " + key);
            }
            if (pdef.h.set) {
                if (pdef.h.set(pdef.t, key, value, this) === false) {
                    // assuming strict mode
                    throw new TypeError("Setting " + key + " on " + pdef.t + " has failed.");
                }
            } else {
                Reflect.set(pdef.t, key, value, this);
            }
        };
    }

    /**
     * Used for apply, construct.
     */
    function createFnProxy() {
        var proxy = function() {
            var pdef = proxy[PROXY_DEF];
            if (!pdef) {
                throw new TypeError("Calling on revoked proxy.");
            }
            if (this && this.constructor === proxy) {
                if (pdef.h.construct) {
                    var ret = pdef.h.construct(pdef.t, arguments, pdef.t); // TODO Array.from?
                    if (typeof ret !== "object") {
                        throw new TypeError("Constructor must create an Object.");
                    }
                    return ret;
                }
                return Reflect.construct(pdef.t, arguments);
            }
            if (pdef.h.apply) {
                return pdef.h.apply(pdef.t, this, arguments); // TODO Array.from?
            }
            return Reflect.apply(pdef.t, this, arguments);
        };
        return proxy;
    }

    /**
     * Creates and caches a property descriptor map for the given object installing getter/setter pairs.
     */
    function getPropsObjFor(obj) {
        if (obj == null) {
            return {};
        }

        var ret = propsMapCache.get(obj);
        if (ret) {
            return ret;
        }

        ret = getPropsObjFor(Object.getPrototypeOf(obj)) || {};

        var ownNames = Object.getOwnPropertyNames(obj),
            ownSymbols = Object.getOwnPropertySymbols(obj),
            index;

        index = ownSymbols.indexOf(PROXY_DEF);
        if (index >= 0) {
            ownSymbols.splice(index, 1);
        }
        index = ownNames.indexOf("prototype");
        if (index >= 0) {
            ownNames.splice(index, 1);
        }

        if (ownNames.length || ownSymbols.length) {
            ret = Object.assign({}, ret);
            var p, tdescr,
                i, len;

            for (i = 0, len = ownNames.length; i < len; ++i) {
                p = ownNames[i];
                tdescr = Object.getOwnPropertyDescriptor(obj, p);
                ret[p] = {
                    tdescr: tdescr,
                    get: getter(p),
                    set: setter(p),
                    enumerable: !!tdescr.enumerable
                };
            }
            for (i = 0, len = ownSymbols.length; i < len; ++i) {
                p = ownSymbols[i];
                tdescr = Object.getOwnPropertyDescriptor(obj, p);
                ret[p] = {
                    tdescr: tdescr,
                    get: getter(p),
                    set: setter(p),
                    enumerable: !!tdescr.enumerable
                };
            }
        }

        propsMapCache.set(obj, ret);
        return ret;
    }

    function Proxy(target, handler) {
        Object.getOwnPropertyNames(handler).forEach(function(trap) {
            if (UNSUPPORTED_TRAPS.has(trap)) {
                throw new TypeError("Unsupported trap: " + trap);
            }
        });

        var propsMap = getPropsObjFor(target),
            proxy;

        if (typeof target === "function") {
            proxy = createFnProxy();
            Object.defineProperties(proxy, propsMap);
        } else {
            proxy = Object.create(Object.getPrototypeOf(target), propsMap);
        }

        proxy[PROXY_DEF] = {
            h: handler,
            t: target,
            pmap: propsMap
        };
        Object.seal(proxy); // to notice once new properties are defined on it, i.e. a problem

        return proxy;
    }

    function revoker() {
        this[PROXY_DEF] = null;
    }

    Proxy.revocable = function revocable(target, handler) {
        var proxy = new Proxy(target, handler);

        return {
            proxy: proxy,
            revoke: revoker.bind(proxy)
        };
    };

    global.Proxy = Proxy;

})(typeof global === "object" ? global : self);
<|endoftext|>


<commit_before>// log the error as soon as possible
process.on('uncaughtException', function(err) {
    console.error('msg %s, name %s, stack->\n%s', err.message, err.name, err.stack || 'NONE');
    process.exit(-1);
});

var _os = require('os');
var _param = require('./param.json');
var _run = require('child_process').exec;

var DEFAULT_TOTAL_CONNECTIONS = (_os.totalmem()/1024) * 64;
var STATES = ['ESTABLISHED', 'FIN_WAIT', 'TIME_WAIT', 'SYN_SENT', 'UDP'];

var _maxConnections; // the maximum number of connection available
var _mode; // the level of detail to report
var _pollInterval; // the interval to poll the metrics
var _source; // the source of the metrics

// ==========
// VALIDATION
// ==========

// Which mode should we report 'basic' or 'advanced'
var _mode = _param.mode || 'basic';

// If you do not have a poll intevarl, use 5000 as the default
var _pollInterval = _param.pollInterval || 5000;

// if we do not have a source, then set it
var _source = _param.source || _os.hostname();

// to call conntrack we need root access
var _isRoot = process.env.USER === 'root';
if (!_isRoot)
{
	console.error('This plugin requires root/sudo access');
	process.exit(1);
}

// get the max value from the system so we can view the ratio of used connections
function getMaximumConnections(cb)
{
	function handleOutput(err, stderr, stdout)
	{
		if (err)
			return cb(err);
		if (stderr || !stdout)
			return cb('It does not look like conntrack is installed, please install it and re-run the relay');

		var value = parseInt((stdout.split('=')[1] || '').trim());
		if (isNaN(value))
			return cb(null, DEFAULT_TOTAL_CONNECTIONS);
		else
			return cb(null, value);
	}

	_run('sysctl net.netfilter.nf_conntrack_max', function(err, stdout, stderr)
	{
		// try the other conntrack key
		if (!err && stderr && stderr.indexOf('is an unknown key') !== -1)
			_run('sysctl net.ipv4.netfilter.ip_conntrack_max', handleOutput);
		else
			handleOutput(err, stderr, stdout);
	});
}

// get the conntrack connection values
function getConnTrackValues(cb)
{
	_run('conntrack -L', function(err, stdout, stderr)
	{
		if (err)
			return cb(err);
		if (!stdout)
			return cb('No output from $ conntrack -L');
		if (stderr && !stderr.match(/flow entries have been shown/)
			return cb(stderr);

		var assured = 0;
		var total = 0;
		var unreplied = 0;

		var state = {};
		STATES.forEach(function(s) { state[s] = 0; });

		// parse the output
		var lines = stdout.split('\n');
		lines.forEach(function(line)
		{
			var match = line.match(/^(tcp|udp)\s+(\d+)\s+(\d+)\s+(\w+)/);
			if (!match)
				return;

			if (match[1] === 'tcp' && STATES.indexOf(match[3]) !== -1)
				state[match[3]]++;

			if (match[1] === 'udp')
				state.UDP++;

			if (line.match(/ASSURED/))
				assured++;

			if (line.match(/UNREPLIED/))
				unreplied++;

			total++;
		});

		return cb && cb(null, {
			assured: assured,
			unreplied: unreplied,
			total: total,
			state: state
		});

	});
}

function poll(cb)
{
	getConnTrackValues(function(err, ct)
	{
		if (err)
			return console.error(err);

		ct = ct || {};
		ct.state = ct.state || {};

		var ratio = (ct.total || 0) / _maxConnections;

		if (_mode === 'basic' || _mode === 'advanced')
		{
			console.log('CONNTRACK_CONNECTIONS %d %s', ct.total, _source);
			console.log('CONNTRACK_LIMIT %d %s', ratio, _source); // precentage
		}

		if (_mode === 'advanced')
		{
			console.log('CONNTRACK_ESTABLISHED %d %s', ct.state.ESTABLISHED || 0, _source);
			console.log('CONNTRACK_FINWAIT %d %s', ct.state.FIN_WAIT || 0, _source);
			console.log('CONNTRACK_TIMEWAIT %d %s', ct.state.TIME_WAIT || 0, _source);
			console.log('CONNTRACK_SYNSENT %d %s', ct.state.SYN_SENT || 0, _source);
			console.log('CONNTRACK_UDP %d %s', ct.state.UDP || 0, _source);

			console.log('CONNTRACK_ASSURED %d %s', ct.assured || 0, _source);
			console.log('CONNTRACK_UNREPLIED %d %s', ct.unreplied || 0, _source);
		}

    setTimeout(poll, _pollInterval);
   });
}

// get the max number of connections the system
// allows so we can calculate the ratio
getMaximumConnections(function(err, maxConnections)
{
	if (err)
	{
		console.error(err);
		process.exit(1);
	}

	_maxConnections = maxConnections;
	poll();
});
<commit_msg>fix typo<commit_after>// log the error as soon as possible
process.on('uncaughtException', function(err) {
    console.error('msg %s, name %s, stack->\n%s', err.message, err.name, err.stack || 'NONE');
    process.exit(-1);
});

var _os = require('os');
var _param = require('./param.json');
var _run = require('child_process').exec;

var DEFAULT_TOTAL_CONNECTIONS = (_os.totalmem()/1024) * 64;
var STATES = ['ESTABLISHED', 'FIN_WAIT', 'TIME_WAIT', 'SYN_SENT', 'UDP'];

var _maxConnections; // the maximum number of connection available
var _mode; // the level of detail to report
var _pollInterval; // the interval to poll the metrics
var _source; // the source of the metrics

// ==========
// VALIDATION
// ==========

// Which mode should we report 'basic' or 'advanced'
var _mode = _param.mode || 'basic';

// If you do not have a poll intevarl, use 5000 as the default
var _pollInterval = _param.pollInterval || 5000;

// if we do not have a source, then set it
var _source = _param.source || _os.hostname();

// to call conntrack we need root access
var _isRoot = process.env.USER === 'root';
if (!_isRoot)
{
	console.error('This plugin requires root/sudo access');
	process.exit(1);
}

// get the max value from the system so we can view the ratio of used connections
function getMaximumConnections(cb)
{
	function handleOutput(err, stderr, stdout)
	{
		if (err)
			return cb(err);
		if (stderr || !stdout)
			return cb('It does not look like conntrack is installed, please install it and re-run the relay');

		var value = parseInt((stdout.split('=')[1] || '').trim());
		if (isNaN(value))
			return cb(null, DEFAULT_TOTAL_CONNECTIONS);
		else
			return cb(null, value);
	}

	_run('sysctl net.netfilter.nf_conntrack_max', function(err, stdout, stderr)
	{
		// try the other conntrack key
		if (!err && stderr && stderr.indexOf('is an unknown key') !== -1)
			_run('sysctl net.ipv4.netfilter.ip_conntrack_max', handleOutput);
		else
			handleOutput(err, stderr, stdout);
	});
}

// get the conntrack connection values
function getConnTrackValues(cb)
{
	_run('conntrack -L', function(err, stdout, stderr)
	{
		if (err)
			return cb(err);
		if (!stdout)
			return cb('No output from $ conntrack -L');
		if (stderr && !stderr.match(/flow entries have been shown/))
			return cb(stderr);

		var assured = 0;
		var total = 0;
		var unreplied = 0;

		var state = {};
		STATES.forEach(function(s) { state[s] = 0; });

		// parse the output
		var lines = stdout.split('\n');
		lines.forEach(function(line)
		{
			var match = line.match(/^(tcp|udp)\s+(\d+)\s+(\d+)\s+(\w+)/);
			if (!match)
				return;

			if (match[1] === 'tcp' && STATES.indexOf(match[3]) !== -1)
				state[match[3]]++;

			if (match[1] === 'udp')
				state.UDP++;

			if (line.match(/ASSURED/))
				assured++;

			if (line.match(/UNREPLIED/))
				unreplied++;

			total++;
		});

		return cb && cb(null, {
			assured: assured,
			unreplied: unreplied,
			total: total,
			state: state
		});

	});
}

function poll(cb)
{
	getConnTrackValues(function(err, ct)
	{
		if (err)
			return console.error(err);

		ct = ct || {};
		ct.state = ct.state || {};

		var ratio = (ct.total || 0) / _maxConnections;

		if (_mode === 'basic' || _mode === 'advanced')
		{
			console.log('CONNTRACK_CONNECTIONS %d %s', ct.total, _source);
			console.log('CONNTRACK_LIMIT %d %s', ratio, _source); // precentage
		}

		if (_mode === 'advanced')
		{
			console.log('CONNTRACK_ESTABLISHED %d %s', ct.state.ESTABLISHED || 0, _source);
			console.log('CONNTRACK_FINWAIT %d %s', ct.state.FIN_WAIT || 0, _source);
			console.log('CONNTRACK_TIMEWAIT %d %s', ct.state.TIME_WAIT || 0, _source);
			console.log('CONNTRACK_SYNSENT %d %s', ct.state.SYN_SENT || 0, _source);
			console.log('CONNTRACK_UDP %d %s', ct.state.UDP || 0, _source);

			console.log('CONNTRACK_ASSURED %d %s', ct.assured || 0, _source);
			console.log('CONNTRACK_UNREPLIED %d %s', ct.unreplied || 0, _source);
		}

    setTimeout(poll, _pollInterval);
   });
}

// get the max number of connections the system
// allows so we can calculate the ratio
getMaximumConnections(function(err, maxConnections)
{
	if (err)
	{
		console.error(err);
		process.exit(1);
	}

	_maxConnections = maxConnections;
	poll();
});
<|endoftext|>


<commit_before>require('dotenv').load();

var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
var Twitter = require('twitter');
var havenondemand = require('havenondemand');
var async = require("async");
var RateLimiter = require('limiter').RateLimiter;

if (process.env.staging === 'true') {
  var staging = true
} else {
  var staging = false
}

// var hodClient = new havenondemand.HODClient(process.env.hpe_apikey, 'v1', staging);
var hodClient = new havenondemand.HODClient(process.env.hpe_apikey);

var twitterClient = new Twitter({
  consumer_key: process.env.consumer_key,
  consumer_secret: process.env.consumer_secret,
  access_token_key: process.env.access_token,
  access_token_secret: process.env.access_token_secret
});

port = process.env.PORT || 5000;
var limiter = new RateLimiter(2, 'second') //first parameter is max number of calls per second

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

var candidateString = "HillaryClinton,realDonaldTrump"
var window1 = 10;

// Data used to store and calculate sentiment for candidates
// Each key is the candidate's Twitter handle which is checked after a Tweet is streamed in so we know which candidate we're talking about
var candidateNumbers = {
  "HillaryClinton": {
    averages: {newAvg: 0, oldAvg: 0},
    n: 0,
    nPositive: 0,
    nNegative: 0,
    nNeutral: 0,
    runningAverageWindow1: 0,
    nWindow1: 0,
    runningAverageWindow1Array: []
  },
  "realDonaldTrump": {
      averages: {newAvg: 0, oldAvg: 0},
      n: 0,
      nPositive: 0,
      nNegative: 0,
      nNeutral: 0,
      runningAverageWindow1: 0,
      nWindow1: 0,
      runningAverageWindow1Array: []
  }
}

var articleUpdateInterval = 60000*15; //15 minutes

// Object which stores the articles and concepts from these articles for each of the candidates
var candidateArticles = {
  "Hillary Clinton": {articles: [], concepts: []},
  "Donald Trump": {articles: [], concepts: []},
};

app.get("/", function(req, res) {
  res.render('index', {
    candidateArticles: candidateArticles,
    candidateConcepts: candidateArticles["Hillary Clinton"].concepts
  });
});

// Route for HTML of candidate articles
app.get("/candidatehtml", function(req, res) {
  var payload = candidateArticles
  res.status(200).send(payload)
})

// Route for mobile app HTML of candidate articles
app.get("/candidatehtml_mobile", function(req, res) {
  var payload ={candidates: [
      {
        name: "Hillary Clinton",
        articles: candidateArticles["Hillary Clinton"].articles
      },
      {
        name: "Donald Trump",
        articles: candidateArticles["Donald Trump"].articles
      },
    ]}
    res.status(200).send(payload)
})

// Route for the app explanation at the top
app.get("/whats_this", function(req, res) {
  res.status(200).sendFile(path.join(__dirname, 'views', 'whats_this.html'))
})

// Route for open source contribution explanation
app.get("/contribute", function(req, res) {
  res.status(200).sendFile(path.join(__dirname, 'views', 'contribute.html'))
})

// Route for third party developers to hit if they want the candidate sentiment data
app.get('/candidatedata', function(req, res) {
  // var payload = candidateNumbers
  var payload = {candidates: [
    {
      name: "HillaryClinton",
      averages: {newAvg: candidateNumbers["HillaryClinton"].averages.newAvg, oldAvg: candidateNumbers["HillaryClinton"].averages.oldAvg},
      n: candidateNumbers["HillaryClinton"].n,
      nPositive: candidateNumbers["HillaryClinton"].nPositive,
      nNegative: candidateNumbers["HillaryClinton"].nNegative,
      nNeutral: candidateNumbers["HillaryClinton"].nNeutral,
      runningAverageWindow1: candidateNumbers["HillaryClinton"].runningAverageWindow1,
      runningAverageWindow1Array: candidateNumbers["HillaryClinton"].runningAverageWindow1Array
    },
    {
      name: "realDonaldTrump",
      averages: {newAvg: candidateNumbers["realDonaldTrump"].averages.newAvg, oldAvg: candidateNumbers["realDonaldTrump"].averages.oldAvg},
      n: candidateNumbers["realDonaldTrump"].n,
      nPositive: candidateNumbers["realDonaldTrump"].nPositive,
      nNegative: candidateNumbers["realDonaldTrump"].nNegative,
      nNeutral: candidateNumbers["realDonaldTrump"].nNeutral,
      runningAverageWindow1: candidateNumbers["realDonaldTrump"].runningAverageWindow1,
      runningAverageWindow1Array: candidateNumbers["realDonaldTrump"].runningAverageWindow1Array
    },
  ]}
  res.status(200).send(payload)
})

http.listen(port, function(){
  console.log("Listening on port: "+port);
});

//Stream Tweets
twitterClient.stream('statuses/filter', {track: candidateString}, function(stream) {
  stream.on('data', function(tweet) {
    if (tweet.entities !== undefined) {
      var userMentions = tweet.entities.user_mentions;
      for (var i=0; i<userMentions.length; i++) {
        var screenName = userMentions[i].screen_name;
        if (candidateNumbers[screenName] !== undefined) {
          twitterStream(screenName, candidateNumbers[screenName], tweet)
        }
      }
    }
  });

  stream.on('disconnect', function (disconnectMessage) {
    console.log(disconnectMessage);
  });

  stream.on('error', function(error) {
    throw error;
  });
});

//Helper function to process a tweet for a particular candidate
// Accepts the candidate Twitter handle, an object of his or her sentiment data, and the tweet object returned from the Twitter Stream API
//Analyzes the sentiment, computes the average, computes the Instantaneous average (averageWindow1), maps to colors (no longer using in front end), emits all to the client through websockets and stores it in a Haven OnDemand Index
function twitterStream(candidate, candidateData, tweetObject) {
  var data = {text: tweetObject.text};
  limiter.removeTokens(1, function(err, remainingRequests) {
    console.log("Remaining requests per second: " + remainingRequests);
    hodClient.call('analyzesentiment', data, function(err, resp){
      // debugger;
      if (!err) {
        console.log(resp.body)
        candidateData.n += 1; //increase n by one
        candidateData.nWindow1 +=1 ; //increase by one
        var sentiment = resp.body.aggregate.sentiment;
        // var score = 10.0/3.0*(resp.body.aggregate.score*100.0)+50.0; //map from -15 to 15 to 0 to 100 ... y =10/3*x+50
        // var score = 50.0*(resp.body.aggregate.score)+50.0; //map from -1.0 to 1.0 to 0 to 100 ... y =50*x+50
        var score = 100.0*(resp.body.aggregate.score)+50.0; //map from -0.5 to 0.5 to 0 to 100 ... y =50*x+50
        if (score > 50) {
          candidateData.nPositive += 1;
        } else if(score < 50) {
          candidateData.nNegative += 1;
        } else {
          candidateData.nNeutral += 1;
        }
        //perform running averages window
        candidateData.runningAverageWindow1Array.push(score);
        if (candidateData.runningAverageWindow1Array.length > window1) { //if there is enough data points in the window
          candidateData.runningAverageWindow1Array.splice(0,1);
          candidateData.runningAverageWindow1 = calculateRunningAverageWindow(candidateData.runningAverageWindow1Array, window1)
        }
        //
        candidateData.averages = calculateRunningAverage(score, candidateData.n, candidateData.averages);
        rgbInstantaneous = mapColor(score);
        rgbAverage = mapColor(candidateData.averages.newAvg);
        console.log("------------------------------");
        console.log(tweetObject.text + " | " + sentiment + " | " + score);
        var tweetData = {candidate: candidate, tweet: tweetObject, positive: resp.body.positive, negative: resp.body.negative, aggregate: resp.body.aggregate, rgbInstantaneous: rgbInstantaneous, rgbAverage: rgbAverage, average: candidateData.averages.newAvg, averageWindow1: candidateData.runningAverageWindow1, n: candidateData.n, nNeutral: candidateData.nNeutral, nNegative: candidateData.nNegative, nPositive: candidateData.nPositive};
        io.emit('message', tweetData);
        var data2 = {
          index: 'ourfeelingsaboutpoliticiansa',
          json: JSON.stringify({
            document: [{
              title: candidate + candidateData.n,
              content: tweetObject.text,
              candidate: candidate, tweet: tweetObject, positive: resp.body.positive, negative: resp.body.negative, aggregate: resp.body.aggregate, rgbInstantaneous: rgbInstantaneous, rgbAverage: rgbAverage, average: candidateData.averages.newAvg, averageWindow1: candidateData.runningAverageWindow1, n: candidateData.n, nNeutral: candidateData.nNeutral, nNegative: candidateData.nNegative, nPositive: candidateData.nPositive,
              score: score,
              date: Date.now()
            }]
          })
        }
        if (process.env.add_text_index === 'true') {
          hodClient.call('addtotextindex', data2, function(err2, resp2, body2) {
            if (resp2) {
              if (resp2.body) {
                console.log(resp2.body)
              }
            }
          })
        }
      } else {
        console.log(err);
      }
    });
  });
}

// Functions for updating articles
updateCandidateArticles();
// setInterval(updateCandidateArticles(), articleUpdateInterval);

// Function to retrieve articles about each candidate. Results are stored in the object, candidateArticles, created at the top of this document
function updateCandidateArticles() {
  // Loop through each candidate and obtain articles pertaining to them from Haven OnDemand using Query Text Index API
  async.forEachOf(candidateArticles, function (value1, key1, callback1) {
    candidateArticles[key1].concepts = []; //delete the old concepts for the candidate
    var data1 = {text: key1, indexes: ['news_eng'], summary: 'quick', total_results: 'false'};
    hodClient.call('querytextindex', data1, function(err1, resp1) {
      if (!err1) {
        console.log(resp1.body.documents);
        var articles = resp1.body.documents;
        candidateArticles[key1].articles = articles;
        // Loop through each article and obtain the HTML and concepts from Haven OnDemand using View Document API and Extract Concepts API
        async.each(articles, function (article, callback2) {
          console.log(article)
          var data2 = {url: article.reference};
          hodClient.call('viewdocument', data2, function(err3, resp3) {
            var articleIndex = candidateArticles[key1].articles.indexOf(article);
            if (!err3 && articleIndex >= 0) {
              // add real html content
              var html = resp3.body;
              candidateArticles[key1].articles[articleIndex].html = html
              console.log("worked")
            }
          });
          hodClient.call('extractconcepts', data2, function(err2, resp2) {
            var concepts = resp2.body.concepts;
            async.each(concepts, function(concept, callback) {
              var newDict = {"text": concept.concept, "size": concept.occurrences}
              candidateArticles[key1].concepts.push(newDict);
            }, function (err) {
              if (err) console.error(err.message);
            });
          });
        }, function (err) {
          if (err) console.error(err.message);
        });
      } else {
        console.log("------------------");
        if (err1) {console.log(err1);}
        console.log(err1);
      }
    });
  }, function (err) {
      if (err) console.error(err.message);
  });
}

// For debugging
// setInterval(function(){debugger;}, 6000);

// Function for mapping the color of the sentiment (no longer usingin front-end)
// Accepts the sentiment score
function mapColor(score) {
  weight = Math.floor(((0.5*score + 0.5)*100));
  r = Math.floor( (255 * (100 - weight)) / 100 );
  g = Math.floor( (255 * weight) / 100 );
  b = 0;
  return {r: r, g: g, b:b};
}

// Function for finding the daily average sentiment
// Accepts the new score, the number of total tweets, and the an object of the previous average and the new average
function calculateRunningAverage(score, n, averages) {
  averages.newAvg = averages.oldAvg * (n-1)/n + score/n;   // New average = old average * (n-1)/n + new value /n
  averages.oldAvg = averages.newAvg; //set equal to new average for next go around of calling this function
  return averages;
}

// Function for calculating the instantaneous average sentiment
// Accepts the array of sentiments and the window used for calculate (i.e. look at most recent 10 tweets)
function calculateRunningAverageWindow(array, win) {
  var runsum = 0.0;
  for (var i=0; i<win; i++) {
    runsum += array[i];
  }
  var avg = runsum/win;
  return avg;
}
<commit_msg>updated error for adding to text index<commit_after>require('dotenv').load();

var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
var Twitter = require('twitter');
var havenondemand = require('havenondemand');
var async = require("async");
var RateLimiter = require('limiter').RateLimiter;

if (process.env.staging === 'true') {
  var staging = true
} else {
  var staging = false
}

// var hodClient = new havenondemand.HODClient(process.env.hpe_apikey, 'v1', staging);
var hodClient = new havenondemand.HODClient(process.env.hpe_apikey);

var twitterClient = new Twitter({
  consumer_key: process.env.consumer_key,
  consumer_secret: process.env.consumer_secret,
  access_token_key: process.env.access_token,
  access_token_secret: process.env.access_token_secret
});

port = process.env.PORT || 5000;
var limiter = new RateLimiter(2, 'second') //first parameter is max number of calls per second

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

var candidateString = "HillaryClinton,realDonaldTrump"
var window1 = 10;

// Data used to store and calculate sentiment for candidates
// Each key is the candidate's Twitter handle which is checked after a Tweet is streamed in so we know which candidate we're talking about
var candidateNumbers = {
  "HillaryClinton": {
    averages: {newAvg: 0, oldAvg: 0},
    n: 0,
    nPositive: 0,
    nNegative: 0,
    nNeutral: 0,
    runningAverageWindow1: 0,
    nWindow1: 0,
    runningAverageWindow1Array: []
  },
  "realDonaldTrump": {
      averages: {newAvg: 0, oldAvg: 0},
      n: 0,
      nPositive: 0,
      nNegative: 0,
      nNeutral: 0,
      runningAverageWindow1: 0,
      nWindow1: 0,
      runningAverageWindow1Array: []
  }
}

var articleUpdateInterval = 60000*15; //15 minutes

// Object which stores the articles and concepts from these articles for each of the candidates
var candidateArticles = {
  "Hillary Clinton": {articles: [], concepts: []},
  "Donald Trump": {articles: [], concepts: []},
};

app.get("/", function(req, res) {
  res.render('index', {
    candidateArticles: candidateArticles,
    candidateConcepts: candidateArticles["Hillary Clinton"].concepts
  });
});

// Route for HTML of candidate articles
app.get("/candidatehtml", function(req, res) {
  var payload = candidateArticles
  res.status(200).send(payload)
})

// Route for mobile app HTML of candidate articles
app.get("/candidatehtml_mobile", function(req, res) {
  var payload ={candidates: [
      {
        name: "Hillary Clinton",
        articles: candidateArticles["Hillary Clinton"].articles
      },
      {
        name: "Donald Trump",
        articles: candidateArticles["Donald Trump"].articles
      },
    ]}
    res.status(200).send(payload)
})

// Route for the app explanation at the top
app.get("/whats_this", function(req, res) {
  res.status(200).sendFile(path.join(__dirname, 'views', 'whats_this.html'))
})

// Route for open source contribution explanation
app.get("/contribute", function(req, res) {
  res.status(200).sendFile(path.join(__dirname, 'views', 'contribute.html'))
})

// Route for third party developers to hit if they want the candidate sentiment data
app.get('/candidatedata', function(req, res) {
  // var payload = candidateNumbers
  var payload = {candidates: [
    {
      name: "HillaryClinton",
      averages: {newAvg: candidateNumbers["HillaryClinton"].averages.newAvg, oldAvg: candidateNumbers["HillaryClinton"].averages.oldAvg},
      n: candidateNumbers["HillaryClinton"].n,
      nPositive: candidateNumbers["HillaryClinton"].nPositive,
      nNegative: candidateNumbers["HillaryClinton"].nNegative,
      nNeutral: candidateNumbers["HillaryClinton"].nNeutral,
      runningAverageWindow1: candidateNumbers["HillaryClinton"].runningAverageWindow1,
      runningAverageWindow1Array: candidateNumbers["HillaryClinton"].runningAverageWindow1Array
    },
    {
      name: "realDonaldTrump",
      averages: {newAvg: candidateNumbers["realDonaldTrump"].averages.newAvg, oldAvg: candidateNumbers["realDonaldTrump"].averages.oldAvg},
      n: candidateNumbers["realDonaldTrump"].n,
      nPositive: candidateNumbers["realDonaldTrump"].nPositive,
      nNegative: candidateNumbers["realDonaldTrump"].nNegative,
      nNeutral: candidateNumbers["realDonaldTrump"].nNeutral,
      runningAverageWindow1: candidateNumbers["realDonaldTrump"].runningAverageWindow1,
      runningAverageWindow1Array: candidateNumbers["realDonaldTrump"].runningAverageWindow1Array
    },
  ]}
  res.status(200).send(payload)
})

http.listen(port, function(){
  console.log("Listening on port: "+port);
});

//Stream Tweets
twitterClient.stream('statuses/filter', {track: candidateString}, function(stream) {
  stream.on('data', function(tweet) {
    if (tweet.entities !== undefined) {
      var userMentions = tweet.entities.user_mentions;
      for (var i=0; i<userMentions.length; i++) {
        var screenName = userMentions[i].screen_name;
        if (candidateNumbers[screenName] !== undefined) {
          twitterStream(screenName, candidateNumbers[screenName], tweet)
        }
      }
    }
  });

  stream.on('disconnect', function (disconnectMessage) {
    console.log(disconnectMessage);
  });

  stream.on('error', function(error) {
    throw error;
  });
});

//Helper function to process a tweet for a particular candidate
// Accepts the candidate Twitter handle, an object of his or her sentiment data, and the tweet object returned from the Twitter Stream API
//Analyzes the sentiment, computes the average, computes the Instantaneous average (averageWindow1), maps to colors (no longer using in front end), emits all to the client through websockets and stores it in a Haven OnDemand Index
function twitterStream(candidate, candidateData, tweetObject) {
  var data = {text: tweetObject.text};
  limiter.removeTokens(1, function(err, remainingRequests) {
    console.log("Remaining requests per second: " + remainingRequests);
    hodClient.call('analyzesentiment', data, function(err, resp){
      // debugger;
      if (!err) {
        console.log(resp.body)
        candidateData.n += 1; //increase n by one
        candidateData.nWindow1 +=1 ; //increase by one
        var sentiment = resp.body.aggregate.sentiment;
        // var score = 10.0/3.0*(resp.body.aggregate.score*100.0)+50.0; //map from -15 to 15 to 0 to 100 ... y =10/3*x+50
        // var score = 50.0*(resp.body.aggregate.score)+50.0; //map from -1.0 to 1.0 to 0 to 100 ... y =50*x+50
        var score = 100.0*(resp.body.aggregate.score)+50.0; //map from -0.5 to 0.5 to 0 to 100 ... y =50*x+50
        if (score > 50) {
          candidateData.nPositive += 1;
        } else if(score < 50) {
          candidateData.nNegative += 1;
        } else {
          candidateData.nNeutral += 1;
        }
        //perform running averages window
        candidateData.runningAverageWindow1Array.push(score);
        if (candidateData.runningAverageWindow1Array.length > window1) { //if there is enough data points in the window
          candidateData.runningAverageWindow1Array.splice(0,1);
          candidateData.runningAverageWindow1 = calculateRunningAverageWindow(candidateData.runningAverageWindow1Array, window1)
        }
        //
        candidateData.averages = calculateRunningAverage(score, candidateData.n, candidateData.averages);
        rgbInstantaneous = mapColor(score);
        rgbAverage = mapColor(candidateData.averages.newAvg);
        console.log("------------------------------");
        console.log(tweetObject.text + " | " + sentiment + " | " + score);
        var tweetData = {candidate: candidate, tweet: tweetObject, positive: resp.body.positive, negative: resp.body.negative, aggregate: resp.body.aggregate, rgbInstantaneous: rgbInstantaneous, rgbAverage: rgbAverage, average: candidateData.averages.newAvg, averageWindow1: candidateData.runningAverageWindow1, n: candidateData.n, nNeutral: candidateData.nNeutral, nNegative: candidateData.nNegative, nPositive: candidateData.nPositive};
        io.emit('message', tweetData);
        var data2 = {
          index: 'ourfeelingsaboutpoliticiansa',
          json: JSON.stringify({
            document: [{
              title: candidate + candidateData.n,
              content: tweetObject.text,
              candidate: candidate, tweet: tweetObject, positive: resp.body.positive, negative: resp.body.negative, aggregate: resp.body.aggregate, rgbInstantaneous: rgbInstantaneous, rgbAverage: rgbAverage, average: candidateData.averages.newAvg, averageWindow1: candidateData.runningAverageWindow1, n: candidateData.n, nNeutral: candidateData.nNeutral, nNegative: candidateData.nNegative, nPositive: candidateData.nPositive,
              score: score,
              date: Date.now()
            }]
          })
        }
        if (process.env.add_text_index === 'true') {
          hodClient.call('addtotextindex', data2, function(err2, resp2, body2) {
            if (err2) {
              console.log(err2)
            } else {
              console.log(resp2.body)
            }
          })
        }
      } else {
        console.log(err);
      }
    });
  });
}

// Functions for updating articles
updateCandidateArticles();
// setInterval(updateCandidateArticles(), articleUpdateInterval);

// Function to retrieve articles about each candidate. Results are stored in the object, candidateArticles, created at the top of this document
function updateCandidateArticles() {
  // Loop through each candidate and obtain articles pertaining to them from Haven OnDemand using Query Text Index API
  async.forEachOf(candidateArticles, function (value1, key1, callback1) {
    candidateArticles[key1].concepts = []; //delete the old concepts for the candidate
    var data1 = {text: key1, indexes: ['news_eng'], summary: 'quick', total_results: 'false'};
    hodClient.call('querytextindex', data1, function(err1, resp1) {
      if (!err1) {
        console.log(resp1.body.documents);
        var articles = resp1.body.documents;
        candidateArticles[key1].articles = articles;
        // Loop through each article and obtain the HTML and concepts from Haven OnDemand using View Document API and Extract Concepts API
        async.each(articles, function (article, callback2) {
          console.log(article)
          var data2 = {url: article.reference};
          hodClient.call('viewdocument', data2, function(err3, resp3) {
            var articleIndex = candidateArticles[key1].articles.indexOf(article);
            if (!err3 && articleIndex >= 0) {
              // add real html content
              var html = resp3.body;
              candidateArticles[key1].articles[articleIndex].html = html
              console.log("worked")
            }
          });
          hodClient.call('extractconcepts', data2, function(err2, resp2) {
            var concepts = resp2.body.concepts;
            async.each(concepts, function(concept, callback) {
              var newDict = {"text": concept.concept, "size": concept.occurrences}
              candidateArticles[key1].concepts.push(newDict);
            }, function (err) {
              if (err) console.error(err.message);
            });
          });
        }, function (err) {
          if (err) console.error(err.message);
        });
      } else {
        console.log("------------------");
        if (err1) {console.log(err1);}
        console.log(err1);
      }
    });
  }, function (err) {
      if (err) console.error(err.message);
  });
}

// For debugging
// setInterval(function(){debugger;}, 6000);

// Function for mapping the color of the sentiment (no longer usingin front-end)
// Accepts the sentiment score
function mapColor(score) {
  weight = Math.floor(((0.5*score + 0.5)*100));
  r = Math.floor( (255 * (100 - weight)) / 100 );
  g = Math.floor( (255 * weight) / 100 );
  b = 0;
  return {r: r, g: g, b:b};
}

// Function for finding the daily average sentiment
// Accepts the new score, the number of total tweets, and the an object of the previous average and the new average
function calculateRunningAverage(score, n, averages) {
  averages.newAvg = averages.oldAvg * (n-1)/n + score/n;   // New average = old average * (n-1)/n + new value /n
  averages.oldAvg = averages.newAvg; //set equal to new average for next go around of calling this function
  return averages;
}

// Function for calculating the instantaneous average sentiment
// Accepts the array of sentiments and the window used for calculate (i.e. look at most recent 10 tweets)
function calculateRunningAverageWindow(array, win) {
  var runsum = 0.0;
  for (var i=0; i<win; i++) {
    runsum += array[i];
  }
  var avg = runsum/win;
  return avg;
}
<|endoftext|>


<commit_before>new Clipboard(".clipboard");
var pingId;
var parser = require("socket.io-parser");
var decoder = new parser.Decoder();
var previewDecoder = new parser.Decoder();
var parameters = localStorage.getItem("parameters");
var bookmarks = localStorage.getItem("bookmarks");

if (!localStorage.getItem("tour")) {
    var tour = new Shepherd.Tour({
        defaults: {
            classes: "shepherd-theme-arrows",
            showCancelLink: true
        }
    });

    tour.addStep("input url", {
        title: "input url",
        text: "input url of your websocket services here",
        attachTo: ".tour-input-url bottom",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("check", {
        title: "check",
        text: "check this if you are connecting a socket.io service",
        attachTo: ".tour-check right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("connect", {
        title: "connect",
        text: "press this button to connect your websocket service",
        attachTo: ".tour-connect right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("input message", {
        title: "input message",
        text: "input message that is about to send",
        attachTo: ".tour-input-message right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("send message", {
        title: "send message",
        text: "press this button to send the message",
        attachTo: ".tour-send-message right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("view messages", {
        title: "view messages",
        text: "all the messages in and out will be here",
        attachTo: ".tour-view-messages top",
        buttons: [
            {
                text: "Done",
                action: tour.next
            }
        ]
    });

    tour.start();
    localStorage.setItem("tour", 1);
}

function getNow() {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    return (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
}

vue = new Vue({
    el: "#body",
    data: {
        websocket: undefined,
        messages: [],
        isSocketIOInternally: !!localStorage.getItem("isSocketIO"),
        ignorePingInternally: !!localStorage.getItem("ignorePing"),
        baseUrl: localStorage.getItem("baseUrl") || "ws://localhost",
        parameters: parameters ? JSON.parse(parameters) : [],
        anchor: localStorage.getItem("anchor") || "",
        messageInternally: localStorage.getItem("message") || "",
        showRawInternally: !!localStorage.getItem("showRaw"),
        showFormattedInternally: !!localStorage.getItem("showFormatted"),
        previewResult: "",
        isPreview: false,
        bookmarks: bookmarks ? JSON.parse(bookmarks) : [],
        isEditing: false,
        bookmarkName: "",
    },
    computed: {
        canSaveAsBookmark: {
            get: function () {
                if (this.bookmarkName.trim() === "") {
                    return false;
                }
                for (var i = 0; i > this.bookmarks.length; i++) {
                    if (this.bookmarks[i].name === this.bookmarkName) {
                        return false;
                    }
                }
                return true;
            },
        },
        isSocketIO: {
            get: function () {
                return this.isSocketIOInternally;
            },
            set: function (value) {
                localStorage.setItem("isSocketIO", value ? "1" : "");
                this.isSocketIOInternally = value;
            }
        },
        ignorePing: {
            get: function () {
                return this.ignorePingInternally;
            },
            set: function (value) {
                localStorage.setItem("ignorePing", value ? "1" : "");
                this.ignorePingInternally = value;
            }
        },
        showRaw: {
            get: function () {
                return this.showRawInternally;
            },
            set: function (value) {
                localStorage.setItem("showRaw", value ? "1" : "");
                this.showRawInternally = value;
            }
        },
        showFormatted: {
            get: function () {
                return this.showFormattedInternally;
            },
            set: function (value) {
                localStorage.setItem("showFormatted", value ? "1" : "");
                this.showFormattedInternally = value;
            }
        },
        message: {
            get: function () {
                return this.messageInternally;
            },
            set: function (value) {
                localStorage.setItem("message", value);
                this.messageInternally = value;
            }
        },
        url: {
            get: function () {
                var url = this.baseUrl;
                if (this.parameters.length > 0) {
                    url += "?";
                    for (var i = 0; i < this.parameters.length; i++) {
                        var parameter = this.parameters[i];
                        url += parameter.key + "=" + parameter.value + "&";
                    }
                    url = url.substring(0, url.length - 1);
                }
                if (this.anchor) {
                    url += "#" + this.anchor;
                }
                return url;
            },
            set: function (url) {
                var index = url.indexOf("#");
                if (index > -1) {
                    url = url.substring(0, index);
                    this.anchor = url.substring(index + 1);
                } else {
                    this.anchor = "";
                }

                index = url.indexOf("?");
                if (index > -1) {
                    this.baseUrl = url.substring(0, index);
                    var array = url.substring(index + 1).split("&");
                    var parameters = [];
                    for (var i = 0; i < array.length; i++) {
                        var tmp = array[i];
                        index = tmp.indexOf("=");
                        if (index === -1) {
                            parameters.push({
                                key: tmp,
                                value: ""
                            });
                        } else {
                            parameters.push({
                                key: tmp.substring(0, index),
                                value: tmp.substring(index + 1)
                            });
                        }
                    }
                    this.parameters = parameters;
                } else {
                    this.baseUrl = url;
                    this.parameters = [];
                }

                localStorage.setItem("baseUrl", this.baseUrl);
                localStorage.setItem("parameters", JSON.stringify(this.parameters));
                localStorage.setItem("anchor", this.anchor);
            }
        }
    },
    methods: {
        savingAsBookmark: function () {
            this.isEditing = !this.isEditing;
            Vue.nextTick(function () {
                document.getElementById("bookmarkName").focus();
            });
        },
        saveAsBookmark: function () {
            this.isEditing = false;
            this.bookmarks.unshift({
                name: this.bookmarkName,
                isSocketIO: this.isSocketIO,
                ignorePing: this.ignorePing,
                baseUrl: this.baseUrl,
                parameters: this.parameters,
                anchor: this.anchor,
                message: this.message,
                showRaw: this.showRaw,
                showFormatted: this.showFormatted,
            });
            localStorage.setItem("bookmarks", JSON.stringify(this.bookmarks));
        },
        deleteBookmark: function (index) {
            this.bookmarks.splice(index, 1);
            localStorage.setItem("bookmarks", JSON.stringify(this.bookmarks));
        },
        useBookmark: function (index) {
            var bookmark = this.bookmarks[index];
            this.isSocketIO = bookmark.isSocketIO;
            this.ignorePing = bookmark.ignorePing;
            this.showRaw = bookmark.showRaw;
            this.showFormatted = bookmark.showFormatted;
            this.message = bookmark.message;
            this.baseUrl = bookmark.baseUrl;
            var parameters = JSON.stringify(bookmark.parameters);
            this.parameters = JSON.parse(parameters);
            this.anchor = bookmark.anchor;
            localStorage.setItem("baseUrl", bookmark.baseUrl);
            localStorage.setItem("parameters", parameters);
            localStorage.setItem("anchor", bookmark.anchor);
        },
        setParameter: function (index, key, value) {
            this.parameters[index].key = key;
            this.parameters[index].value = value;
            localStorage.setItem("parameters", JSON.stringify(this.parameters));
        },
        deleteParameter: function (index) {
            this.parameters.splice(index, 1);
            localStorage.setItem("parameters", JSON.stringify(this.parameters));
        },
        addParameter: function () {
            this.parameters.push({
                key: "",
                value: ""
            });
        },
        connect: function () {
            this.websocket = new WebSocket(this.url);
            this.websocket.onopen = this.onopen;
            this.websocket.onclose = this.onclose;
            this.websocket.onmessage = this.onmessage;
            this.websocket.onerror = this.onerror;
            if (this.isSocketIO) {
                pingId = setInterval(this.ping, 25000);
            }
        },
        sendMessage: function () {
            this.send(this.message);
        },
        send: function (message) {
            if (this.websocket) {
                if (!this.ignorePing || message !== "2") {
                    this.messages.unshift({
                        moment: getNow(),
                        type: "out",
                        data: message
                    });
                }
                this.websocket.send(message);
            }
        },
        ping: function () {
            this.send("2");
        },
        clear: function () {
            this.messages = [];
        },
        previewMessage: function () {
            this.isPreview = true;
            if (this.isSocketIO) {
                this.previewResult = "";
                previewDecoder.add(this.message);
            } else {
                try {
                    this.previewResult = JSON.stringify(JSON.parse(this.message), null, "    ");
                } catch (error) {
                    this.previewResult = error;
                }
            }
        },
        cancelPreview: function () {
            this.isPreview = false;
        },
        showTips: function () {
            this.messages.unshift({
                moment: getNow(),
                type: "tips",
                tips: "Tips: \n" +
                "1. for socket.io, if you connect http://localhost, in ws's perspective, you connected ws://localhost/socket.io?transport=websocket\n" +
                "2. for socket.io, if you connect https://localhost, in ws's perspective, you connected wss://localhost/socket.io?transport=websocket\n" +
                "3. for socket.io, if you send a message(eg: {a_key:\"a_value\"}) in an event(eg: \"a_event\"), in ws's perspective, the actual message you send is: 42[\"a_event\",{\"a_key\":\"a_value\"}]\n" +
                "4. chrome's developer tool is a good tool to view ws connection and messages"
            });
        },
        onopen: function (e) {
            this.messages.unshift({
                moment: getNow(),
                type: e.type
            });
        },
        onclose: function (e) {
            this.messages.unshift({
                moment: getNow(),
                type: e.type,
                reason: e.reason
            });
            this.websocket = undefined;
            clearInterval(pingId);
        },
        onmessage: function (e) {
            if (this.ignorePing && e.data === "3") {
                return;
            }

            if (e.data === "3") {
                this.messages.unshift({
                    moment: getNow(),
                    type: e.type,
                    data: e.data
                });
                return;
            }

            vue.messages.unshift({
                moment: getNow(),
                type: e.type,
                rawData: e.data
            });

            if (this.isSocketIOInternally) {
                decoder.add(e.data);
            } else {
                try {
                    var json = JSON.parse(e.data);
                    this.messages.unshift({
                        moment: getNow(),
                        type: e.type,
                        formattedData: json
                    });
                } catch (error) { }
            }
        },
        onerror: function (e) {
            this.messages.unshift({
                moment: getNow(),
                type: e.type
            });
            this.websocket = undefined;
            clearInterval(pingId);
        }
    }
});

if (!window.WebSocket) {
    vue.messages.unshift({
        moment: getNow(),
        type: "tips",
        tips: "current browser doesn't support WebSocket"
    });
}

decoder.on("decoded", function (decodedPacket) {
    vue.messages.unshift({
        moment: getNow(),
        type: "message",
        formattedData: JSON.stringify(decodedPacket, null, "    ")
    });
});

previewDecoder.on("decoded", function (decodedPacket) {
    vue.previewResult = JSON.stringify(decodedPacket, null, "    ");
});
<commit_msg>add default url and message<commit_after>new Clipboard(".clipboard");
var pingId;
var parser = require("socket.io-parser");
var decoder = new parser.Decoder();
var previewDecoder = new parser.Decoder();
var parameters = localStorage.getItem("parameters");
var bookmarks = localStorage.getItem("bookmarks");

if (!localStorage.getItem("tour")) {
    var tour = new Shepherd.Tour({
        defaults: {
            classes: "shepherd-theme-arrows",
            showCancelLink: true
        }
    });

    tour.addStep("input url", {
        title: "input url",
        text: "input url of your websocket services here",
        attachTo: ".tour-input-url bottom",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("check", {
        title: "check",
        text: "check this if you are connecting a socket.io service",
        attachTo: ".tour-check right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("connect", {
        title: "connect",
        text: "press this button to connect your websocket service",
        attachTo: ".tour-connect right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("input message", {
        title: "input message",
        text: "input message that is about to send",
        attachTo: ".tour-input-message right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("send message", {
        title: "send message",
        text: "press this button to send the message",
        attachTo: ".tour-send-message right",
        buttons: [
            {
                text: "Next",
                action: tour.next
            }
        ]
    });
    tour.addStep("view messages", {
        title: "view messages",
        text: "all the messages in and out will be here",
        attachTo: ".tour-view-messages top",
        buttons: [
            {
                text: "Done",
                action: tour.next
            }
        ]
    });

    tour.start();
    localStorage.setItem("tour", 1);
}

function getNow() {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    return (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
}

vue = new Vue({
    el: "#body",
    data: {
        websocket: undefined,
        messages: [],
        isSocketIOInternally: !!localStorage.getItem("isSocketIO"),
        ignorePingInternally: !!localStorage.getItem("ignorePing"),
        baseUrl: localStorage.getItem("baseUrl") || "ws://slack.socket.io/socket.io?transport=websocket",
        parameters: parameters ? JSON.parse(parameters) : [],
        anchor: localStorage.getItem("anchor") || "",
        messageInternally: localStorage.getItem("message") || "42[\"new message\",{\"username\":\"hello\",\"message\":\"world\"}]",
        showRawInternally: !!localStorage.getItem("showRaw"),
        showFormattedInternally: !!localStorage.getItem("showFormatted"),
        previewResult: "",
        isPreview: false,
        bookmarks: bookmarks ? JSON.parse(bookmarks) : [],
        isEditing: false,
        bookmarkName: "",
    },
    computed: {
        canSaveAsBookmark: {
            get: function () {
                if (this.bookmarkName.trim() === "") {
                    return false;
                }
                for (var i = 0; i > this.bookmarks.length; i++) {
                    if (this.bookmarks[i].name === this.bookmarkName) {
                        return false;
                    }
                }
                return true;
            },
        },
        isSocketIO: {
            get: function () {
                return this.isSocketIOInternally;
            },
            set: function (value) {
                localStorage.setItem("isSocketIO", value ? "1" : "");
                this.isSocketIOInternally = value;
            }
        },
        ignorePing: {
            get: function () {
                return this.ignorePingInternally;
            },
            set: function (value) {
                localStorage.setItem("ignorePing", value ? "1" : "");
                this.ignorePingInternally = value;
            }
        },
        showRaw: {
            get: function () {
                return this.showRawInternally;
            },
            set: function (value) {
                localStorage.setItem("showRaw", value ? "1" : "");
                this.showRawInternally = value;
            }
        },
        showFormatted: {
            get: function () {
                return this.showFormattedInternally;
            },
            set: function (value) {
                localStorage.setItem("showFormatted", value ? "1" : "");
                this.showFormattedInternally = value;
            }
        },
        message: {
            get: function () {
                return this.messageInternally;
            },
            set: function (value) {
                localStorage.setItem("message", value);
                this.messageInternally = value;
            }
        },
        url: {
            get: function () {
                var url = this.baseUrl;
                if (this.parameters.length > 0) {
                    url += "?";
                    for (var i = 0; i < this.parameters.length; i++) {
                        var parameter = this.parameters[i];
                        url += parameter.key + "=" + parameter.value + "&";
                    }
                    url = url.substring(0, url.length - 1);
                }
                if (this.anchor) {
                    url += "#" + this.anchor;
                }
                return url;
            },
            set: function (url) {
                var index = url.indexOf("#");
                if (index > -1) {
                    url = url.substring(0, index);
                    this.anchor = url.substring(index + 1);
                } else {
                    this.anchor = "";
                }

                index = url.indexOf("?");
                if (index > -1) {
                    this.baseUrl = url.substring(0, index);
                    var array = url.substring(index + 1).split("&");
                    var parameters = [];
                    for (var i = 0; i < array.length; i++) {
                        var tmp = array[i];
                        index = tmp.indexOf("=");
                        if (index === -1) {
                            parameters.push({
                                key: tmp,
                                value: ""
                            });
                        } else {
                            parameters.push({
                                key: tmp.substring(0, index),
                                value: tmp.substring(index + 1)
                            });
                        }
                    }
                    this.parameters = parameters;
                } else {
                    this.baseUrl = url;
                    this.parameters = [];
                }

                localStorage.setItem("baseUrl", this.baseUrl);
                localStorage.setItem("parameters", JSON.stringify(this.parameters));
                localStorage.setItem("anchor", this.anchor);
            }
        }
    },
    methods: {
        savingAsBookmark: function () {
            this.isEditing = !this.isEditing;
            Vue.nextTick(function () {
                document.getElementById("bookmarkName").focus();
            });
        },
        saveAsBookmark: function () {
            this.isEditing = false;
            this.bookmarks.unshift({
                name: this.bookmarkName,
                isSocketIO: this.isSocketIO,
                ignorePing: this.ignorePing,
                baseUrl: this.baseUrl,
                parameters: this.parameters,
                anchor: this.anchor,
                message: this.message,
                showRaw: this.showRaw,
                showFormatted: this.showFormatted,
            });
            localStorage.setItem("bookmarks", JSON.stringify(this.bookmarks));
        },
        deleteBookmark: function (index) {
            this.bookmarks.splice(index, 1);
            localStorage.setItem("bookmarks", JSON.stringify(this.bookmarks));
        },
        useBookmark: function (index) {
            var bookmark = this.bookmarks[index];
            this.isSocketIO = bookmark.isSocketIO;
            this.ignorePing = bookmark.ignorePing;
            this.showRaw = bookmark.showRaw;
            this.showFormatted = bookmark.showFormatted;
            this.message = bookmark.message;
            this.baseUrl = bookmark.baseUrl;
            var parameters = JSON.stringify(bookmark.parameters);
            this.parameters = JSON.parse(parameters);
            this.anchor = bookmark.anchor;
            localStorage.setItem("baseUrl", bookmark.baseUrl);
            localStorage.setItem("parameters", parameters);
            localStorage.setItem("anchor", bookmark.anchor);
        },
        setParameter: function (index, key, value) {
            this.parameters[index].key = key;
            this.parameters[index].value = value;
            localStorage.setItem("parameters", JSON.stringify(this.parameters));
        },
        deleteParameter: function (index) {
            this.parameters.splice(index, 1);
            localStorage.setItem("parameters", JSON.stringify(this.parameters));
        },
        addParameter: function () {
            this.parameters.push({
                key: "",
                value: ""
            });
        },
        connect: function () {
            this.websocket = new WebSocket(this.url);
            this.websocket.onopen = this.onopen;
            this.websocket.onclose = this.onclose;
            this.websocket.onmessage = this.onmessage;
            this.websocket.onerror = this.onerror;
            if (this.isSocketIO) {
                pingId = setInterval(this.ping, 25000);
            }
        },
        sendMessage: function () {
            this.send(this.message);
        },
        send: function (message) {
            if (this.websocket) {
                if (!this.ignorePing || message !== "2") {
                    this.messages.unshift({
                        moment: getNow(),
                        type: "out",
                        data: message
                    });
                }
                this.websocket.send(message);
            }
        },
        ping: function () {
            this.send("2");
        },
        clear: function () {
            this.messages = [];
        },
        previewMessage: function () {
            this.isPreview = true;
            if (this.isSocketIO) {
                this.previewResult = "";
                previewDecoder.add(this.message);
            } else {
                try {
                    this.previewResult = JSON.stringify(JSON.parse(this.message), null, "    ");
                } catch (error) {
                    this.previewResult = error;
                }
            }
        },
        cancelPreview: function () {
            this.isPreview = false;
        },
        showTips: function () {
            this.messages.unshift({
                moment: getNow(),
                type: "tips",
                tips: "Tips: \n" +
                "1. for socket.io, if you connect http://localhost, in ws's perspective, you connected ws://localhost/socket.io?transport=websocket\n" +
                "2. for socket.io, if you connect https://localhost, in ws's perspective, you connected wss://localhost/socket.io?transport=websocket\n" +
                "3. for socket.io, if you send a message(eg: {a_key:\"a_value\"}) in an event(eg: \"a_event\"), in ws's perspective, the actual message you send is: 42[\"a_event\",{\"a_key\":\"a_value\"}]\n" +
                "4. chrome's developer tool is a good tool to view ws connection and messages"
            });
        },
        onopen: function (e) {
            this.messages.unshift({
                moment: getNow(),
                type: e.type
            });
        },
        onclose: function (e) {
            this.messages.unshift({
                moment: getNow(),
                type: e.type,
                reason: e.reason
            });
            this.websocket = undefined;
            clearInterval(pingId);
        },
        onmessage: function (e) {
            if (this.ignorePing && e.data === "3") {
                return;
            }

            if (e.data === "3") {
                this.messages.unshift({
                    moment: getNow(),
                    type: e.type,
                    data: e.data
                });
                return;
            }

            vue.messages.unshift({
                moment: getNow(),
                type: e.type,
                rawData: e.data
            });

            if (this.isSocketIOInternally) {
                decoder.add(e.data);
            } else {
                try {
                    var json = JSON.parse(e.data);
                    this.messages.unshift({
                        moment: getNow(),
                        type: e.type,
                        formattedData: json
                    });
                } catch (error) { }
            }
        },
        onerror: function (e) {
            this.messages.unshift({
                moment: getNow(),
                type: e.type
            });
            this.websocket = undefined;
            clearInterval(pingId);
        }
    }
});

if (!window.WebSocket) {
    vue.messages.unshift({
        moment: getNow(),
        type: "tips",
        tips: "current browser doesn't support WebSocket"
    });
}

decoder.on("decoded", function (decodedPacket) {
    vue.messages.unshift({
        moment: getNow(),
        type: "message",
        formattedData: JSON.stringify(decodedPacket, null, "    ")
    });
});

previewDecoder.on("decoded", function (decodedPacket) {
    vue.previewResult = JSON.stringify(decodedPacket, null, "    ");
});
<|endoftext|>


<commit_before>var path = require('path');

module.exports = {
    mapPath: function (paths) {

        var dir = __dirname;
        if (dir.indexOf('node_modules') !== -1)
            dir = dir.substring(0, dir.indexOf('node_modules'));

        return path.join(dir, paths);
    }
};<commit_msg>caching the path<commit_after>var path = require('path');
var currentPath = null;

module.exports = {
    mapPath: function (paths) {
        if(currentPath !== null)
        return path.join(currentPath, paths);

        var dir = __dirname;
        if (dir.indexOf('node_modules') !== -1)
            dir = dir.substring(0, dir.indexOf('node_modules'));

        currentPath = dir;

        return path.join(dir, paths);
    }
};<|endoftext|>


<commit_before>var curl = require('curlrequest')

module.exports = function (access_token) {
  var compileUrl = 'http://api.compilers.sphere-engine.com/api/v3/submissions?access_token=' + access_token
  var ID = ''
  var answer = {}
  var boolResult = true
  var languages = {
    'Ada': 7,
    'Nasm': 13,
    'Gawk': 104,
    'Mawk': 105,
    'Bash': 28,
    'Brainfuck': 12,
    'C': 11,
    'CSharp': 27,
    'Cpp': 1,
    'Cpp14': 44,
    'CLIPS': 14,
    'Clojure': 111,
    'COBOL': 118,
    'CommonLisp': 32,
    'Erlang': 36,
    'FSharp': 124,
    'Factor': 123,
    'Falcon': 125,
    'Forth': 107,
    'Fortran': 5,
    'Go': 114,
    'Groovy': 121,
    'Haskell': 21,
    'Icon': 16,
    'Intercal': 9,
    'Java': 10,
    'Java7': 55,
    'Lua': 26,
    'Nemerle': 30,
    'Nice': 25,
    'Nimrod': 122,
    'Nodejs': 56,
    'ObjectiveC': 43,
    'Ocaml': 8,
    'Octave': 127,
    'Oz': 119,
    'Pascalfpc': 22,
    'Pascalgpc': 2,
    'Perl': 3,
    'Pike': 19,
    'Prolog': 108,
    'Python': 4,
    'Python3': 116,
    'R': 117,
    'Ruby': 17,
    'Scala': 39,
    'SchemeChicken': 128,
    'SchemeGuile': 33,
    'Smalltalk': 23,
    'SQL': 40,
    'Tcl': 38,
    'Unlambda': 115,
    'VB.NET': 101
  }

  var subRoutines = {
    Run: function (code, lang, inp) {
      var infoRun = {
        'sourceCode': code,
        'language': languages[lang],
        'input': inp
      }
      var optionsRun = {
        method: 'POST',
        url: compileUrl,
        data: infoRun
      }

      return new Promise((resolve, reject) => {
        curl.request(optionsRun, function (error, response) {
          if (error) {
            reject(error)
            return
          }

          ID += JSON.parse(response).id
          var reqUrl1 = 'http://api.compilers.sphere-engine.com/api/v3/submissions/'.concat(ID)
          var reqUrl2 = '?access_token='.concat(access_token)
          var reqUrl12 = reqUrl1.concat(reqUrl2)

          var reqUrl = reqUrl12 + '&withSource=1&withInput=1&withOutput=1&withStderr=1&withCmpinfo=1'

          var optionsRecv = {
            method: 'GET',
            url: reqUrl12
          }

          var optionsRecv2 = {
            method: 'GET',
            url: reqUrl
          }

          curl.request(optionsRecv, function (error2, response2) {
            if (error2) {
              reject(error)
            }

            var statuscheck = JSON.parse(response2)
            if (statuscheck.status == 0) {
              curl.request(optionsRecv2, function (error3, response3) {
                if (error3) {
                  reject(error3)
                  return
                }
                resolve(JSON.parse(response3))
              })
            }else {
              setTimeout(function () {
                curl.request(optionsRecv, function (error2, response2) {
                  if (error2) {
                    reject(error2)
                    return
                  }

                  var statuscheck = JSON.parse(response2)
                  if (statuscheck.status == 0) {
                    curl.request(optionsRecv2, function (error3, response3) {
                      if (error3) {
                        reject(error3)
                        return
                      }
                      resolve(JSON.parse(response3))
                    })
                  }
                })
              }, 4500)
            }
          })
        })
      });
    },

    languageSupport: function () {
      return Promise.resolve(languages)
    }
  }

  return subRoutines
}
<commit_msg>Remove useless `boolResult`<commit_after>var curl = require('curlrequest')

module.exports = function (access_token) {
  var compileUrl = 'http://api.compilers.sphere-engine.com/api/v3/submissions?access_token=' + access_token
  var ID = ''
  var answer = {}
  var languages = {
    'Ada': 7,
    'Nasm': 13,
    'Gawk': 104,
    'Mawk': 105,
    'Bash': 28,
    'Brainfuck': 12,
    'C': 11,
    'CSharp': 27,
    'Cpp': 1,
    'Cpp14': 44,
    'CLIPS': 14,
    'Clojure': 111,
    'COBOL': 118,
    'CommonLisp': 32,
    'Erlang': 36,
    'FSharp': 124,
    'Factor': 123,
    'Falcon': 125,
    'Forth': 107,
    'Fortran': 5,
    'Go': 114,
    'Groovy': 121,
    'Haskell': 21,
    'Icon': 16,
    'Intercal': 9,
    'Java': 10,
    'Java7': 55,
    'Lua': 26,
    'Nemerle': 30,
    'Nice': 25,
    'Nimrod': 122,
    'Nodejs': 56,
    'ObjectiveC': 43,
    'Ocaml': 8,
    'Octave': 127,
    'Oz': 119,
    'Pascalfpc': 22,
    'Pascalgpc': 2,
    'Perl': 3,
    'Pike': 19,
    'Prolog': 108,
    'Python': 4,
    'Python3': 116,
    'R': 117,
    'Ruby': 17,
    'Scala': 39,
    'SchemeChicken': 128,
    'SchemeGuile': 33,
    'Smalltalk': 23,
    'SQL': 40,
    'Tcl': 38,
    'Unlambda': 115,
    'VB.NET': 101
  }

  var subRoutines = {
    Run: function (code, lang, inp) {
      var infoRun = {
        'sourceCode': code,
        'language': languages[lang],
        'input': inp
      }
      var optionsRun = {
        method: 'POST',
        url: compileUrl,
        data: infoRun
      }

      return new Promise((resolve, reject) => {
        curl.request(optionsRun, function (error, response) {
          if (error) {
            reject(error)
            return
          }

          ID += JSON.parse(response).id
          var reqUrl1 = 'http://api.compilers.sphere-engine.com/api/v3/submissions/'.concat(ID)
          var reqUrl2 = '?access_token='.concat(access_token)
          var reqUrl12 = reqUrl1.concat(reqUrl2)

          var reqUrl = reqUrl12 + '&withSource=1&withInput=1&withOutput=1&withStderr=1&withCmpinfo=1'

          var optionsRecv = {
            method: 'GET',
            url: reqUrl12
          }

          var optionsRecv2 = {
            method: 'GET',
            url: reqUrl
          }

          curl.request(optionsRecv, function (error2, response2) {
            if (error2) {
              reject(error)
            }

            var statuscheck = JSON.parse(response2)
            if (statuscheck.status == 0) {
              curl.request(optionsRecv2, function (error3, response3) {
                if (error3) {
                  reject(error3)
                  return
                }
                resolve(JSON.parse(response3))
              })
            }else {
              setTimeout(function () {
                curl.request(optionsRecv, function (error2, response2) {
                  if (error2) {
                    reject(error2)
                    return
                  }

                  var statuscheck = JSON.parse(response2)
                  if (statuscheck.status == 0) {
                    curl.request(optionsRecv2, function (error3, response3) {
                      if (error3) {
                        reject(error3)
                        return
                      }
                      resolve(JSON.parse(response3))
                    })
                  }
                })
              }, 4500)
            }
          })
        })
      });
    },

    languageSupport: function () {
      return Promise.resolve(languages)
    }
  }

  return subRoutines
}
<|endoftext|>


<commit_before>'use strict';

const got = require('got');
const user = require('./user.js');

const highDefinition = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	return user(username).then(userid => {
		const url = `https://i.instagram.com/api/v1/users/${userid}/info/`;

		return got(url, {json: true}).then(res => {
			return res.body.user.hd_profile_pic_url_info.url;
		});
	});
};

const instaProfileImage = module.exports = username => highDefinition(username);

instaProfileImage.medium = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	const url = `https://www.instagram.com/${username}/?__a=1`;

	return got(`${url}`, {json: true}).then(res => {
		return res.body.graphql.user.profile_pic_url_hd || '';
	});
};

instaProfileImage.small = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	const url = `https://www.instagram.com/${username}/?__a=1`;

	return got(`${url}`, {json: true}).then(res => {
		return res.body.graphql.user.profile_pic_url || '';
	});
};

instaProfileImage.image = source => {
	if (typeof source !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof source}`);
	}

	const url = source.split('?')[0];

	return got(`${url}?__a=1`, {json: true}).then(res => {
		return res.body.graphql.shortcode_media.display_url || '';
	});
};

instaProfileImage.video = source => {
	if (typeof source !== 'string') {
		throw new TypeError(`Epxected a string, got ${typeof source}`);
	}

	const url = source.split('?')[0];

	return got(`${url}?__a=1`, {json: true}).then(res => {
		return res.body.graphql.shortcode_media.video_url || '';
	});
};
<commit_msg>new feature and minor fixes<commit_after>'use strict';

const got = require('got');
const user = require('./user.js');

const highDefinition = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	return user(username).then(userid => {
		const url = `https://i.instagram.com/api/v1/users/${userid}/info/`;

		return got(url, {json: true}).then(res => {
			return res.body.user.hd_profile_pic_url_info.url;
		});
	});
};

const instaProfileImage = module.exports = username => highDefinition(username);

instaProfileImage.regular = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	return user(username).then(userid => {
		const url = `https://i.instagram.com/api/v1/users/${userid}/info/`;

		return got(url, {json: true}).then(res => {
			return res.body.user.hd_profile_pic_versions[1].url;
		}).catch(err => {
			if (err) {
				return {
					error: 'The requested size is currently not available'
				};
			}
		});
	});
};

instaProfileImage.medium = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	const url = `https://www.instagram.com/${username}/?__a=1`;

	return got(`${url}`, {json: true}).then(res => {
		return res.body.graphql.user.profile_pic_url_hd || '';
	});
};

instaProfileImage.small = username => {
	if (typeof username !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof username}`);
	}

	const url = `https://www.instagram.com/${username}/?__a=1`;

	return got(`${url}`, {json: true}).then(res => {
		return res.body.graphql.user.profile_pic_url || '';
	});
};

instaProfileImage.image = source => {
	if (typeof source !== 'string') {
		throw new TypeError(`Expected a string, got ${typeof source}`);
	}

	const url = source.split('?')[0];

	return got(`${url}?__a=1`, {json: true}).then(res => {
		return res.body.graphql.shortcode_media.display_url || '';
	});
};

instaProfileImage.video = source => {
	if (typeof source !== 'string') {
		throw new TypeError(`Epxected a string, got ${typeof source}`);
	}

	const url = source.split('?')[0];

	return got(`${url}?__a=1`, {json: true}).then(res => {
		return res.body.graphql.shortcode_media.video_url || '';
	});
};
<|endoftext|>