<commit_before>/*-*- coding: utf-8 -*- vim: set ts=4 sw=4 expandtab
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##~ Copyright (C) 2002-2012 Bellite.io                            ##
##~                                                               ##
##~ This library is free software; you can redistribute it        ##
##~ and/or modify it under the terms of the MIT style License as  ##
##~ found in the LICENSE file included with this distribution.    ##
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##*/

"use strict";
var net = require('net'),
    util = require('util'),
    events = require('events'),
    deferred = require('fate').deferred;

exports.Bellite = Bellite;
function Bellite(cred, logging) {
    if (!(this instanceof Bellite))
        return new Bellite(cred, logging);

    events.EventEmitter.call(this);
    this._resultMap = {};

    cred = this.findCredentials(cred);
    if (cred == null)
        throw new Error("Invalid Bellite credentials");
    if (logging) {
        this.logSend = this._logSend
        this.logRecv = this._logRecv
    }

    this.on('connect', function(){ this.on_connect(cred) })
    this._connect_jsonrpc(cred);
    return this;
}
util.inherits(Bellite, events.EventEmitter);

Bellite.prototype._connect_jsonrpc = function(cred) {
    var self=this, connBuf='', conn = net.connect(cred);
    conn.setEncoding("UTF-8");
    conn.setNoDelay(true);
    conn.setKeepAlive(true, 0);

    conn.on('connect', function() {
        self.emit('connect') });
    conn.on('data', function(data) {
        data = (connBuf+data).split('\0')
        connBuf = data.pop()
        self._recvJsonRpc(data); });
    self._sendMessage = function sendMessage(msg) {
        return conn.write(msg+'\0') }
    self._shutdown = function() { conn.end(); }
    return conn;
}
Bellite.prototype.findCredentials = function(cred) {
    if (cred == null)
        cred = process.env.BELLITE_SERVER || '';
    else if (cred.split === undefined)
        return cred;
    var res={credentials:cred};<commit_msg>Added default for BELLITE_SERVER environment variable value<commit_after>/*-*- coding: utf-8 -*- vim: set ts=4 sw=4 expandtab
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##~ Copyright (C) 2002-2012 Bellite.io                            ##
##~                                                               ##
##~ This library is free software; you can redistribute it        ##
##~ and/or modify it under the terms of the MIT style License as  ##
##~ found in the LICENSE file included with this distribution.    ##
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##*/

"use strict";
var net = require('net'),
    util = require('util'),
    events = require('events'),
    deferred = require('fate').deferred;

exports.Bellite = Bellite;
function Bellite(cred, logging) {
    if (!(this instanceof Bellite))
        return new Bellite(cred, logging);

    events.EventEmitter.call(this);
    this._resultMap = {};

    cred = this.findCredentials(cred);
    if (cred == null)
        throw new Error("Invalid Bellite credentials");
    if (logging) {
        this.logSend = this._logSend
        this.logRecv = this._logRecv
    }

    this.on('connect', function(){ this.on_connect(cred) })
    this._connect_jsonrpc(cred);
    return this;
}
util.inherits(Bellite, events.EventEmitter);

Bellite.prototype._connect_jsonrpc = function(cred) {
    var self=this, connBuf='', conn = net.connect(cred);
    conn.setEncoding("UTF-8");
    conn.setNoDelay(true);
    conn.setKeepAlive(true, 0);

    conn.on('connect', function() {
        self.emit('connect') });
    conn.on('data', function(data) {
        data = (connBuf+data).split('\0')
        connBuf = data.pop()
        self._recvJsonRpc(data); });
    self._sendMessage = function sendMessage(msg) {
        return conn.write(msg+'\0') }
    self._shutdown = function() { conn.end(); }
    return conn;
}
Bellite.prototype.findCredentials = function(cred) {
    if (cred == null) {
        cred = process.env.BELLITE_SERVER;
        if (!cred) {
            cred = '127.0.0.1:3099/bellite-demo-host';
            console.warn('BELLITE_SERVER environment variable not found, using "'+cred+'"')
        }
    } else if (cred.split === undefined)
        return cred;
    var res={credentials:cred};
{'fraction_non_alphanumeric': 0.1989935956084172, 'fraction_numerical': 0.009149130832570906, 'mean_word_length': 2.8494718309859155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5640351', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1239, 'n_words': 292}