<commit_before>var func_r = require('./eyebleach_reddit');
var func_t = require('./eyebleach_twitter');
var conf = require('./config');

function postEyeBleach() {
  func_t.postTweet(func_r.getUrl(conf.subreddit)[1], func_r.getUrl(conf.subreddit)[0]);
  console.log("Eyebleach Posted");
}

// Run to start
postEyeBleach()
// Run Every Hour
setInterval(function() {
  try {
    postEyeBleach()
  } catch (e) {
    log(e);
  }
}, 60000 * 60);<commit_msg>Add dat logging to the main file :D<commit_after>var func_r = require('./eyebleach_reddit');
var func_t = require('./eyebleach_twitter');
var func_help = require('./eyebleach_help');
var conf = require('./config');

function postEyeBleach() {
  func_t.postTweet(func_r.getUrl(conf.subreddit)[1], func_r.getUrl(conf.subreddit)[0]);
  func_help.log("Eyebleach Posted");
}

// Run to start
postEyeBleach()
// Run Every Hour
setInterval(function() {
  try {
    postEyeBleach()
  } catch (e) {
    log(e);
  }
}, 60000 * 60);<|endoftext|>


<commit_before>'use strict';
var React = require('react');
var isMobile = require('./utils/isMobile')();
var CodeMirror;
var jsLintAddon;
var index = 0;
var length = 0;
var initCount = 0;

if (!isMobile) {
  CodeMirror = require('codemirror');
  require('./mode/javascript')(CodeMirror);
  require('./addons/lint')(CodeMirror);
  require('./addons/edit/matchBrackets')(CodeMirror);
  require('./addons/edit/closeBrackets')(CodeMirror);
  jsLintAddon = require('./addons/lint/javascript');
}

var CodeMirrorEditor = React.createClass({

  propTypes: {
    className: React.PropTypes.string,
    config: React.PropTypes.object,
    defaultValue: React.PropTypes.string,
    forceTextArea: React.PropTypes.bool,
    linter: React.PropTypes.func,
    onChange: React.PropTypes.func,
    readOnly: React.PropTypes.bool,
    style: React.PropTypes.object,
    textAreaStyle: React.PropTypes.object,
    textAreaClass: React.PropTypes.string,
    textAreaClassName: React.PropTypes.string,
    value: React.PropTypes.string
  },

  getInitialState: function() {
    return { isControlled: typeof this.props.value !== 'undefined' };
  },

  componentDidMount: function() {
    var isTextArea = this.props.forceTextArea || isMobile;
    var config = this.props.config;
    if (isTextArea) {
      return;
    }

    jsLintAddon(CodeMirror, this.props.linter);
    this._editor =
      CodeMirror.fromTextArea(this.refs.editor.getDOMNode(), this.props);
    this._editor.on('change', this._handleChange);
    if (config) {
      Object.keys(config).map(function(prop) {
        var args = config[prop];
        if (!Array.isArray(args)) {
          args = [args];
        }
        if (prop.indexOf('set') !== -1) {
          return this._editor[prop].apply(this._editor, args);
        }
        args.unshift(prop);
        this._editor.setOption.apply(this._editor, args);
      }.bind(this));
    }
  },

  componentWillReceiveProps: function(nextProps) {
    initCount++;
    //default content init with empty store,reset default content when store content first time emitchange!
    if(initCount >= 2){
      if(index != nextProps.index || length != nextProps.defaultValue.length) {
          index = nextProps.index;
          length = nextProps.defaultValue.length;
          var value = nextProps.defaultValue[index - 1];
          if (this._editor) {
            if (value != null) {
              if (this._editor.getValue() !== value) {
                this._editor.setValue(value);
              }
            }
          }
      }
    }
  },

  _handleChange: function(doc,change) {
    delete change['from']
    delete change['to']
    if (this._editor) {
      var value = this._editor.getValue();

      if (value !== this.props.value) {
        if (this.props.onChange) {
          this.props.onChange({target: {value: value}, change: change});
        }

        if (this._editor.getValue() !== this.props.value) {
          if (this.state.isControlled) {
            this._editor.setValue(this.props.value);
          } else {
            this.props.value = value;
          }
        }
      }
    }
  },

  render: function() {
    var editor = React.createElement('textarea', {
      ref: 'editor',
      value: this.props.value,
      readOnly: this.props.readOnly,
      defaultValue: this.props.defaultValue,
      onChange: this.props.onChange,
      style: this.props.textAreaStyle,
      className: this.props.textAreaClassName || this.props.textAreaClass
    });

    return React.createElement('div', {
      style: this.props.style,
      className: this.props.className
    }, editor);
  }
});

module.exports = CodeMirrorEditor;
<commit_msg>Add props name content to instead default value<commit_after>'use strict';
var React = require('react');
var isMobile = require('./utils/isMobile')();
var CodeMirror;
var jsLintAddon;
var index = 0;
var length = 0;
var initCount = 0;

if (!isMobile) {
  CodeMirror = require('codemirror');
  require('./mode/javascript')(CodeMirror);
  require('./addons/lint')(CodeMirror);
  require('./addons/edit/matchBrackets')(CodeMirror);
  require('./addons/edit/closeBrackets')(CodeMirror);
  jsLintAddon = require('./addons/lint/javascript');
}

var CodeMirrorEditor = React.createClass({

  propTypes: {
    className: React.PropTypes.string,
    config: React.PropTypes.object,
    defaultValue: React.PropTypes.string,
    forceTextArea: React.PropTypes.bool,
    linter: React.PropTypes.func,
    onChange: React.PropTypes.func,
    readOnly: React.PropTypes.bool,
    style: React.PropTypes.object,
    textAreaStyle: React.PropTypes.object,
    textAreaClass: React.PropTypes.string,
    textAreaClassName: React.PropTypes.string,
    value: React.PropTypes.string
  },

  getInitialState: function() {
    return { isControlled: typeof this.props.value !== 'undefined' };
  },

  componentDidMount: function() {
    var isTextArea = this.props.forceTextArea || isMobile;
    var config = this.props.config;
    if (isTextArea) {
      return;
    }

    jsLintAddon(CodeMirror, this.props.linter);
    this._editor =
      CodeMirror.fromTextArea(this.refs.editor.getDOMNode(), this.props);
    this._editor.on('change', this._handleChange);
    if (config) {
      Object.keys(config).map(function(prop) {
        var args = config[prop];
        if (!Array.isArray(args)) {
          args = [args];
        }
        if (prop.indexOf('set') !== -1) {
          return this._editor[prop].apply(this._editor, args);
        }
        args.unshift(prop);
        this._editor.setOption.apply(this._editor, args);
      }.bind(this));
    }
  },

  componentWillReceiveProps: function(nextProps) {
    initCount++;
    //content init with empty store,reset content when store content first time emitchange!
    if(initCount >= 2){
      if(index != nextProps.index || length != nextProps.content.length) {
          index = nextProps.index;
          length = nextProps.content.length;
          var value = nextProps.content[index - 1];
          if (this._editor) {
            if (value != null) {
              if (this._editor.getValue() !== value) {
                this._editor.setValue(value);
              }
            }
          }
      }
    }
  },

  _handleChange: function(doc,change) {
    delete change['from']
    delete change['to']
    if (this._editor) {
      var value = this._editor.getValue();

      if (value !== this.props.value) {
        if (this.props.onChange) {
          this.props.onChange({target: {value: value}, change: change});
        }

        if (this._editor.getValue() !== this.props.value) {
          if (this.state.isControlled) {
            this._editor.setValue(this.props.value);
          } else {
            this.props.value = value;
          }
        }
      }
    }
  },

  render: function() {
    var editor = React.createElement('textarea', {
      ref: 'editor',
      value: this.props.value,
      readOnly: this.props.readOnly,
      defaultValue: this.props.defaultValue,
      onChange: this.props.onChange,
      style: this.props.textAreaStyle,
      className: this.props.textAreaClassName || this.props.textAreaClass
    });

    return React.createElement('div', {
      style: this.props.style,
      className: this.props.className
    }, editor);
  }
});

module.exports = CodeMirrorEditor;
<|endoftext|>


<commit_before>module.exports = merge

function merge() {
  var args = [].slice.call(arguments)
  if (!args.length) return {}
  if (args.length == 1) return args[0]
}
<commit_msg>bleh. lets think about this<commit_after>module.exports = merge

function merge() {
  var args = [].slice.call(arguments),
      l = args.length
  if (!l) return {}
  if (l == 1) return args[0]
  return parseObjects(args)
}

function parseObjects(objs) {
  var result = {},
      keys = []
  for (var i = 0, l = objs.length; i < l; ++i) {
    keys = keys.concat(Object.keys(objs[i]))
  }
  keys = keys.filter(function (k, idx, arr) { return arr.lastIndexOf(k) === idx })

}
<|endoftext|>


<commit_before>/* jshint node: true */
'use strict';

var path = require('path');
var fs = require('fs');

var denodeify = require('rsvp').denodeify;
var readFile  = denodeify(fs.readFile);

var DeployPluginBase = require('ember-cli-deploy-plugin');
var DynamoDBAdapter = require('./lib/dynamodb');
var Promise = require('ember-cli/lib/ext/promise');

var DEFAULT_MANIFEST_SIZE = 10;

module.exports = {
  name: 'ember-cli-deploy-dynamodb',

  createDeployPlugin: function(options) {
    var DeployPlugin = DeployPluginBase.extend({
      name: options.name,

      defaultConfig: {
        distDir: function(context) {
          return context.distDir;
        },
        keyPrefix: function(context){
          return context.project.name() + ':index';
        },
        filePattern: 'index.html',
        activationSuffix: 'current',
        manifestSize: DEFAULT_MANIFEST_SIZE,
        revisionKey: function(context) {
          return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
        },
        dynamoDbClient: function(context) {
          return new DynamoDBAdapter(this.pluginConfig);
        },
        didDeployMessage: function(context){
          var revisionKey = context.revisionData && context.revisionData.revisionKey;
          var activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
          if (revisionKey && !activatedRevisionKey) {
            return "Deployed but did not activate revision " + revisionKey + ". "
                 + "To activate, run: "
                 + "ember deploy:activate " + context.deployTarget + " --revision=" + revisionKey + "\n";
          }
        }
      },

      requiredConfig: ['region', 'table', 'indexName'],

      upload: function(context) {
        var filePattern = this.readConfig('filePattern');
        var revisionKey = this.readConfig('revisionKey');
        var distDir = this.readConfig('distDir');
        var keyPrefix = this.readConfig('keyPrefix');
        var dynamoDbClient = this.readConfig('dynamoDbClient');
        var revision = this._makeKey(revisionKey);

        return this._readFileContents(path.join(distDir, filePattern))
          .then(function(indexContents) {
            return dynamoDbClient.upload(indexContents, revision);
          }).then(this._uploadSuccessMessage.bind(this))
          .then(function(key) {
           return { dynamodbKey: key };
         })
         .catch(this._errorMessage.bind(this));
      },

      activate: function(context) {
        var dynamoDbClient = this.readConfig('dynamoDbClient');
        var revisionKey = this.readConfig('revisionKey');
        var keyPrefix = this.readConfig('keyPrefix');
        var activationSuffix = this.readConfig('activationSuffix');
        var currentKey = this._makeKey(activationSuffix);
        var revision = this._makeKey(revisionKey);

        this.log('Activating revision `' + revisionKey + '`', { verbose: true });
        return dynamoDbClient.activate(revision, currentKey)
          .then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`', {}))
          .then(function(){
            return {
              revisionData: {
                activatedRevisionKey: revisionKey
              }
            };
          })
          .catch(this._errorMessage.bind(this));
      },

      fetchRevisions: function(context) {
        var keyPrefix = this.readConfig('keyPrefix');
        var activationSuffix  = this.readConfig('activationSuffix');
        var dynamoDbClient = this.readConfig('dynamoDbClient');

        this.log('Listing revisions');
        return dynamoDbClient.list(this._makeKey(activationSuffix), this._makeKey())
          .then(function(revisions) {
            return { revisions: revisions };
          })
          .catch(this._errorMessage.bind(this));
      },

      didDeploy: function(context){
        var didDeployMessage = this.readConfig('didDeployMessage');
        if (didDeployMessage) {
          this.log(didDeployMessage);
        }
      },

      _readFileContents: function(path) {
        return readFile(path)
        .then(function(buffer) {
          return buffer.toString();
        });
      },

      _uploadSuccessMessage: function(key) {
        this.log('Uploaded with key `' + key + '`', { verbose: true });
        return Promise.resolve(key);
      },

      _errorMessage: function(error) {
        this.log(error, { color: 'red' });
        return Promise.reject(error);
      },

      _makeKey: function(value) {
        value = value || '';
        var keyPrefix = this.readConfig('keyPrefix');
        return keyPrefix + ':' + value;
      }
    });

    return new DeployPlugin();
  }
};
<commit_msg>Replace deprecated Promise with rsvp<commit_after>/* jshint node: true */
'use strict';

var path = require('path');
var fs = require('fs');

var denodeify = require('rsvp').denodeify;
var readFile  = denodeify(fs.readFile);

var DeployPluginBase = require('ember-cli-deploy-plugin');
var DynamoDBAdapter = require('./lib/dynamodb');
var Promise = require('rsvp').Promise;

var DEFAULT_MANIFEST_SIZE = 10;

module.exports = {
  name: 'ember-cli-deploy-dynamodb',

  createDeployPlugin: function(options) {
    var DeployPlugin = DeployPluginBase.extend({
      name: options.name,

      defaultConfig: {
        distDir: function(context) {
          return context.distDir;
        },
        keyPrefix: function(context){
          return context.project.name() + ':index';
        },
        filePattern: 'index.html',
        activationSuffix: 'current',
        manifestSize: DEFAULT_MANIFEST_SIZE,
        revisionKey: function(context) {
          return context.commandOptions.revision || (context.revisionData && context.revisionData.revisionKey);
        },
        dynamoDbClient: function(context) {
          return new DynamoDBAdapter(this.pluginConfig);
        },
        didDeployMessage: function(context){
          var revisionKey = context.revisionData && context.revisionData.revisionKey;
          var activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
          if (revisionKey && !activatedRevisionKey) {
            return "Deployed but did not activate revision " + revisionKey + ". "
                 + "To activate, run: "
                 + "ember deploy:activate " + context.deployTarget + " --revision=" + revisionKey + "\n";
          }
        }
      },

      requiredConfig: ['region', 'table', 'indexName'],

      upload: function(context) {
        var filePattern = this.readConfig('filePattern');
        var revisionKey = this.readConfig('revisionKey');
        var distDir = this.readConfig('distDir');
        var keyPrefix = this.readConfig('keyPrefix');
        var dynamoDbClient = this.readConfig('dynamoDbClient');
        var revision = this._makeKey(revisionKey);

        return this._readFileContents(path.join(distDir, filePattern))
          .then(function(indexContents) {
            return dynamoDbClient.upload(indexContents, revision);
          }).then(this._uploadSuccessMessage.bind(this))
          .then(function(key) {
           return { dynamodbKey: key };
         })
         .catch(this._errorMessage.bind(this));
      },

      activate: function(context) {
        var dynamoDbClient = this.readConfig('dynamoDbClient');
        var revisionKey = this.readConfig('revisionKey');
        var keyPrefix = this.readConfig('keyPrefix');
        var activationSuffix = this.readConfig('activationSuffix');
        var currentKey = this._makeKey(activationSuffix);
        var revision = this._makeKey(revisionKey);

        this.log('Activating revision `' + revisionKey + '`', { verbose: true });
        return dynamoDbClient.activate(revision, currentKey)
          .then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`', {}))
          .then(function(){
            return {
              revisionData: {
                activatedRevisionKey: revisionKey
              }
            };
          })
          .catch(this._errorMessage.bind(this));
      },

      fetchRevisions: function(context) {
        var keyPrefix = this.readConfig('keyPrefix');
        var activationSuffix  = this.readConfig('activationSuffix');
        var dynamoDbClient = this.readConfig('dynamoDbClient');

        this.log('Listing revisions');
        return dynamoDbClient.list(this._makeKey(activationSuffix), this._makeKey())
          .then(function(revisions) {
            return { revisions: revisions };
          })
          .catch(this._errorMessage.bind(this));
      },

      didDeploy: function(context){
        var didDeployMessage = this.readConfig('didDeployMessage');
        if (didDeployMessage) {
          this.log(didDeployMessage);
        }
      },

      _readFileContents: function(path) {
        return readFile(path)
        .then(function(buffer) {
          return buffer.toString();
        });
      },

      _uploadSuccessMessage: function(key) {
        this.log('Uploaded with key `' + key + '`', { verbose: true });
        return Promise.resolve(key);
      },

      _errorMessage: function(error) {
        this.log(error, { color: 'red' });
        return Promise.reject(error);
      },

      _makeKey: function(value) {
        value = value || '';
        var keyPrefix = this.readConfig('keyPrefix');
        return keyPrefix + ':' + value;
      }
    });

    return new DeployPlugin();
  }
};
<|endoftext|>


<commit_before>var mysql = require('mysql'),
    util = require('util'),
    EventEmitter = require('events').EventEmitter,
    generateBinlog = require('./lib/sequence/binlog');

function ZongJi(dsn, options) {
  this.set(options);

  EventEmitter.call(this);

  // to send table info query
  var ctrlDsn = cloneObjectSimple(dsn);
  ctrlDsn.database = 'information_schema';
  this.ctrlConnection = mysql.createConnection(ctrlDsn);
  this.ctrlConnection.connect();
  this.ctrlCallbacks = [];

  this.connection = mysql.createConnection(dsn);

  this.tableMap = {};
  this.ready = false;
  this.useChecksum = false;
}

function cloneObjectSimple (obj){
  var out = {};
  for(var i in obj){
    if(obj.hasOwnProperty(i)){
      out[i] = obj[i];
    }
  }
  return out;
}

util.inherits(ZongJi, EventEmitter);

ZongJi.prototype.init = function(cb) {
  var self = this;
  var binlogOptions = {
    tableMap: self.tableMap
  };

  var asyncMethods = [
    {
      name: '_isChecksumEnabled',
      callback: function(checksumEnabled) {
        self.useChecksum = checksumEnabled;
        binlogOptions.useChecksum = checksumEnabled
      }
    },
    {
      name: '_findBinlogEnd',
      callback: function(result){
        if(result && self.options.startAtEnd){
          binlogOptions.filename = result.Log_name;
          binlogOptions.position = result.File_size;
        }
      }
    }
  ];

  var methodIndex = 0;
  var nextMethod = function(){
    var method = asyncMethods[methodIndex];
    self[method.name](function(/* args */){
      method.callback.apply(this, arguments);
      methodIndex++;
      if(methodIndex < asyncMethods.length){
        nextMethod();
      }else{
        ready();
      }
    });
  };
  nextMethod();

  var ready = function(){
    // Run asynchronously from _init(), as serverId option set in start()
    if(self.options.serverId !== undefined){
      binlogOptions.serverId = self.options.serverId;
    }

    self.binlog = generateBinlog.call(self, binlogOptions);
    self.ready = true;
    self._executeCtrlCallbacks();
    cb && cb(null);
  };
};

ZongJi.prototype._isChecksumEnabled = function(next) {
  var sql = 'select @@GLOBAL.binlog_checksum as checksum';
  var ctrlConnection = this.ctrlConnection;
  var connection = this.connection;

  ctrlConnection.query(sql, function(err, rows) {
    if (err) {
      if(err.toString().match(/ER_UNKNOWN_SYSTEM_VARIABLE/)){
        // MySQL < 5.6.2 does not support @@GLOBAL.binlog_checksum
        return next(false);
      }
      // TODO: Remove throws to make the module safe
      throw err;
    }

    var checksumEnabled = true;
    if (rows[0].checksum === 'NONE') {
      checksumEnabled = false;
    }

    var setChecksumSql = 'set @master_binlog_checksum=@@global.binlog_checksum';
    if (checksumEnabled) {
      connection.query(setChecksumSql, function(err) {
        if (err) {
          // TODO: Remove throws to make the module safe
          throw err;
        }
        next(checksumEnabled);
      });
    } else {
      next(checksumEnabled);
    }
  });
};

ZongJi.prototype._findBinlogEnd = function(next) {
  var self = this;
  self.ctrlConnection.query('SHOW BINARY LOGS', function(err, rows) {
    // TODO: Remove throws to make the module safe
    if(err) throw err;
    next(rows.length > 0 ? rows[rows.length - 1] : null);
  });
};

ZongJi.prototype._executeCtrlCallbacks = function() {
  if (this.ctrlCallbacks.length > 0) {
    this.ctrlCallbacks.forEach(function(cb) {
      setImmediate(cb);
    });
  }
};

var tableInfoQueryTemplate = 'SELECT ' +
  'COLUMN_NAME, COLLATION_NAME, CHARACTER_SET_NAME, ' +
  'COLUMN_COMMENT, COLUMN_TYPE ' +
  'FROM columns ' + 'WHERE table_schema="%s" AND table_name="%s"';

ZongJi.prototype._fetchTableInfo = function(tableMapEvent, next) {
  var self = this;
  var sql = util.format(tableInfoQueryTemplate,
    tableMapEvent.schemaName, tableMapEvent.tableName);

  this.ctrlConnection.query(sql, function(err, rows) {
    // TODO: Remove throws to make the module safe
    if (err) throw err;

    self.tableMap[tableMapEvent.tableId] = {
      columnSchemas: rows,
      parentSchema: tableMapEvent.schemaName,
      tableName: tableMapEvent.tableName
    };

    next();
  });
};

ZongJi.prototype.set = function(options){
  this.options = options || {};
};

ZongJi.prototype.start = function(options, cb) {
  var self = this;

  // Before we start, make sure that ZongJi was initialized
  if (!self.ready) {
    return self.init(function(err) {
      if (err) {
        return cb && cb(err);
      }

      self.start(options, cb);
    });
  }

  self.set(options);

  var _start = function() {
    self.connection._implyConnect();
    self.connection._protocol._enqueue(new self.binlog(function(error, event){
      if(error) return self.emit('error', error);
      // Do not emit events that have been filtered out
      if(event === undefined || event._filtered === true) return;

      if (event.getTypeName() === 'TableMap') {
        var tableMap = self.tableMap[event.tableId];

        if (!tableMap) {
          self.connection.pause();
          self._fetchTableInfo(event, function() {
            // merge the column info with metadata
            event.updateColumnInfo();
            self.emit('binlog', event);
            self.connection.resume();
          });
          return;
        }
      }
      self.emit('binlog', event);
    }));
  };

  if (this.ready) {
    _start();
  } else {
    this.ctrlCallbacks.push(_start);
  }
};

ZongJi.prototype.stop = function() {
  var self = this;

  self.ready = false;
  // Binary log connection does not end with destroy()
  self.connection.destroy();
  self.ctrlConnection.query(
    'KILL ' + self.connection.threadId,
    function(error, results){
      self.ctrlConnection.destroy();
    }
  );
};

ZongJi.prototype._skipEvent = function(eventName){
  var include = this.options.includeEvents;
  var exclude = this.options.excludeEvents;
  return !(
   (include === undefined ||
    (include instanceof Array && include.indexOf(eventName) !== -1)) &&
   (exclude === undefined ||
    (exclude instanceof Array && exclude.indexOf(eventName) === -1)));
};

ZongJi.prototype._skipSchema = function(database, table){
  var include = this.options.includeSchema;
  var exclude = this.options.excludeSchema;
  return !(
   (include === undefined ||
    (database !== undefined && (database in include) &&
     (include[database] === true ||
      (include[database] instanceof Array &&
       include[database].indexOf(table) !== -1)))) &&
   (exclude === undefined ||
      (database !== undefined &&
       (!(database in exclude) ||
        (exclude[database] !== true &&
          (exclude[database] instanceof Array &&
           exclude[database].indexOf(table) === -1))))));
};

module.exports = ZongJi;
<commit_msg>Revert "Separate connect initialization from starting bin log consumption. Set ready to false after calling stop (since it tears down the connection?)"<commit_after>var mysql = require('mysql');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var generateBinlog = require('./lib/sequence/binlog');

function ZongJi(dsn, options) {
  this.set(options);

  EventEmitter.call(this);

  // to send table info query
  var ctrlDsn = cloneObjectSimple(dsn);
  ctrlDsn.database = 'information_schema';
  this.ctrlConnection = mysql.createConnection(ctrlDsn);
  this.ctrlConnection.connect();
  this.ctrlCallbacks = [];

  this.connection = mysql.createConnection(dsn);

  this.tableMap = {};
  this.ready = false;
  this.useChecksum = false;

  this._init();
}

function cloneObjectSimple (obj){
  var out = {};
  for(var i in obj){
    if(obj.hasOwnProperty(i)){
      out[i] = obj[i];
    }
  }
  return out;
}

util.inherits(ZongJi, EventEmitter);

ZongJi.prototype._init = function() {
  var self = this;
  var binlogOptions = {
    tableMap: self.tableMap
  };

  var asyncMethods = [
    {
      name: '_isChecksumEnabled',
      callback: function(checksumEnabled) {
        self.useChecksum = checksumEnabled;
        binlogOptions.useChecksum = checksumEnabled
      }
    },
    {
      name: '_findBinlogEnd',
      callback: function(result){
        if(result && self.options.startAtEnd){
          binlogOptions.filename = result.Log_name;
          binlogOptions.position = result.File_size;
        }
      }
    }
  ];

  var methodIndex = 0;
  var nextMethod = function(){
    var method = asyncMethods[methodIndex];
    self[method.name](function(/* args */){
      method.callback.apply(this, arguments);
      methodIndex++;
      if(methodIndex < asyncMethods.length){
        nextMethod();
      }else{
        ready();
      }
    });
  };
  nextMethod();

  var ready = function(){
    // Run asynchronously from _init(), as serverId option set in start()
    if(self.options.serverId !== undefined){
      binlogOptions.serverId = self.options.serverId;
    }

    self.binlog = generateBinlog.call(self, binlogOptions);
    self.ready = true;
    self._executeCtrlCallbacks();
  };
};

ZongJi.prototype._isChecksumEnabled = function(next) {
  var sql = 'select @@GLOBAL.binlog_checksum as checksum';
  var ctrlConnection = this.ctrlConnection;
  var connection = this.connection;

  ctrlConnection.query(sql, function(err, rows) {
    if (err) {
      if(err.toString().match(/ER_UNKNOWN_SYSTEM_VARIABLE/)){
        // MySQL < 5.6.2 does not support @@GLOBAL.binlog_checksum
        return next(false);
      }
      // TODO: Remove throws to make the module safe
      throw err;
    }

    var checksumEnabled = true;
    if (rows[0].checksum === 'NONE') {
      checksumEnabled = false;
    }

    var setChecksumSql = 'set @master_binlog_checksum=@@global.binlog_checksum';
    if (checksumEnabled) {
      connection.query(setChecksumSql, function(err) {
        if (err) {
          // TODO: Remove throws to make the module safe
          throw err;
        }
        next(checksumEnabled);
      });
    } else {
      next(checksumEnabled);
    }
  });
};

ZongJi.prototype._findBinlogEnd = function(next) {
  var self = this;
  self.ctrlConnection.query('SHOW BINARY LOGS', function(err, rows) {
    // TODO: Remove throws to make the module safe
    if(err) throw err;
    next(rows.length > 0 ? rows[rows.length - 1] : null);
  });
};

ZongJi.prototype._executeCtrlCallbacks = function() {
  if (this.ctrlCallbacks.length > 0) {
    this.ctrlCallbacks.forEach(function(cb) {
      setImmediate(cb);
    });
  }
};

var tableInfoQueryTemplate = 'SELECT ' +
  'COLUMN_NAME, COLLATION_NAME, CHARACTER_SET_NAME, ' +
  'COLUMN_COMMENT, COLUMN_TYPE ' +
  'FROM columns ' + 'WHERE table_schema="%s" AND table_name="%s"';

ZongJi.prototype._fetchTableInfo = function(tableMapEvent, next) {
  var self = this;
  var sql = util.format(tableInfoQueryTemplate,
    tableMapEvent.schemaName, tableMapEvent.tableName);

  this.ctrlConnection.query(sql, function(err, rows) {
    // TODO: Remove throws to make the module safe
    if (err) throw err;

    self.tableMap[tableMapEvent.tableId] = {
      columnSchemas: rows,
      parentSchema: tableMapEvent.schemaName,
      tableName: tableMapEvent.tableName
    };

    next();
  });
};

ZongJi.prototype.set = function(options){
  this.options = options || {};
};

ZongJi.prototype.start = function(options) {
  var self = this;
  self.set(options);

  var _start = function() {
    self.connection._implyConnect();
    self.connection._protocol._enqueue(new self.binlog(function(error, event){
      if(error) return self.emit('error', error);
      // Do not emit events that have been filtered out
      if(event === undefined || event._filtered === true) return;

      if (event.getTypeName() === 'TableMap') {
        var tableMap = self.tableMap[event.tableId];

        if (!tableMap) {
          self.connection.pause();
          self._fetchTableInfo(event, function() {
            // merge the column info with metadata
            event.updateColumnInfo();
            self.emit('binlog', event);
            self.connection.resume();
          });
          return;
        }
      }
      self.emit('binlog', event);
    }));
  };

  if (this.ready) {
    _start();
  } else {
    this.ctrlCallbacks.push(_start);
  }
};

ZongJi.prototype.stop = function(){
  var self = this;
  // Binary log connection does not end with destroy()
  self.connection.destroy();
  self.ctrlConnection.query(
    'KILL ' + self.connection.threadId,
    function(error, results){
      self.ctrlConnection.destroy();
    }
  );
};

ZongJi.prototype._skipEvent = function(eventName){
  var include = this.options.includeEvents;
  var exclude = this.options.excludeEvents;
  return !(
   (include === undefined ||
    (include instanceof Array && include.indexOf(eventName) !== -1)) &&
   (exclude === undefined ||
    (exclude instanceof Array && exclude.indexOf(eventName) === -1)));
};

ZongJi.prototype._skipSchema = function(database, table){
  var include = this.options.includeSchema;
  var exclude = this.options.excludeSchema;
  return !(
   (include === undefined ||
    (database !== undefined && (database in include) &&
     (include[database] === true ||
      (include[database] instanceof Array &&
       include[database].indexOf(table) !== -1)))) &&
   (exclude === undefined ||
      (database !== undefined &&
       (!(database in exclude) ||
        (exclude[database] !== true &&
          (exclude[database] instanceof Array &&
           exclude[database].indexOf(table) === -1))))));
};

module.exports = ZongJi;
<|endoftext|>


<commit_before>var fs = require('fs');
var path = require('path');
var npm = require('npm');
var jsonFile = require('json-file-plus');
var Table = require('cli-table');
var chalk = require('chalk');
var Promise = require('promise');
var assign = require('object.assign');
var semver = require('semver');

var getTable = function () {
  return new Table({
    chars: {
      top: '',
      'top-mid': '',
      'top-left': '',
      'top-right': '',
      bottom: '',
      'bottom-mid': '',
      'bottom-left': '',
      'bottom-right': '',
      left: '',
      'left-mid': '',
      mid: '',
      'mid-mid': '',
      right: '',
      'right-mid': '',
      middle: ''
    }
  });
};

var createResultJSON = function (key) {
  return function (results) {
    var obj = {};
    obj[key] = results;
    return obj;
  };
};

var createResultTable = function (caption) {
  return function (results) {
    var table = getTable();
    if (results.length > 0) {
      var tableRows = results.map(function (result) {
        if (result.isChanged) {
          return [
            chalk.green('Changed: '),
            result.name,
            'from',
            chalk.yellow(result.before),
            'to',
            chalk.yellow(result.after)
          ];
        } else if (!result.isUpdateable && !result.isStar && !result.isPegged) {
          return [
            chalk.red('Requested range not satisfied by: '),
            result.name,
            'from',
            chalk.yellow(result.before),
            'to',
            chalk.yellow(result.after)
          ];
        } else {
          return [
            chalk.blue('Kept: '),
            result.name,
            'at',
            chalk.yellow(result.before)
          ];
        }
      });
      table.push.apply(table, tableRows);
      var sortByName = function (a, b) {
        return a[1].localeCompare(b[1]);
      };
      table.sort(sortByName);
    } else {
      table.push([chalk.gray('None found')]);
    }
    return [
      chalk.green.underline(caption + ':'),
      table
    ];
  };
};

/**
 * The main entry point.
 */
function salita(dir, options, callback) {
  chalk.enabled = !options['no-color'] && !options.json;
  // Package.json.
  var filename = path.join(dir, 'package.json');
  jsonFile(filename).then(function (pkg) {
    return loadNPM().then(function () { return pkg; });
  }).then(function (pkg) {
    if (pkg && !options.json) {
      console.log('Found package.json.');
    }

    // Check all the dependencies.
    var depLookups = Promise.all(dependenciesLookup(pkg.data, 'dependencies', options['ignore-stars'], options['ignore-pegged']));
    var deps = depLookups.then(options.json ? createResultJSON('dependencies') : createResultTable('Dependencies'));

    // Check all the devDependencies.
    var devDepLookups = Promise.all(dependenciesLookup(pkg.data, 'devDependencies', options['ignore-stars'], options['ignore-pegged']));
    var devDeps = devDepLookups.then(options.json ? createResultJSON('devDependencies') : createResultTable('Development Dependencies'));

    // Wait for all of them to resolve.
    Promise.all([deps, devDeps]).then(function (depResults) {
      if (options.json) {
        console.log(JSON.stringify(assign.apply(null, [{}].concat(depResults)), null, 2));
      } else {
        depResults.forEach(function (results) {
          results.map(String).forEach(function (result) {
            console.log(result);
          });
        });
      }

      var getDepCounts = function (results) {
        var totalDeps = results.length;
        var changedDeps = results.filter(function (result) { return result.isChanged; }).length;
        return [totalDeps, changedDeps];
      };
      var mapThen = function (a, b) { return function (promise) { return promise.then(a, b); }; };
      var counts = Promise.all([depLookups, devDepLookups].map(mapThen(getDepCounts)));

      // Write back the package.json.
      if (options['dry-run']) {
        callback(counts);
      } else {
        pkg.save(callback.bind(null, counts));
      }
    });
  }).done();
}

/**
 * createDependenciesLookup
 *
 * @param type
 * @return
 */
function dependenciesLookup(pkg, type, ignoreStars, ignorePegged) {
  // See if any dependencies of this type exist.
  if (pkg[type] && !Object.keys(pkg[type] || []).length) {
    return [];
  }

  var names = Object.keys(pkg[type] || []);
  // Loop through and map the "lookup latest" to promises.
  var names = Object.keys(pkg[type] || []);
  var untouched = [];
  var addUntouched = function (name, version, flags) {
    untouched.push(Promise.resolve(assign({
	  name: name,
      before: version,
      after: version,
      isChanged: false,
      isUpdateable: false
    }, flags)));
  };
  if (ignoreStars || ignorePegged) {
    names = names.filter(function (name) {
      var version = pkg[type][name];

      var isStar = version === '*';
      if (ignoreStars && isStar) {
        return addUntouched(name, version, { isStar: true });
      }

      var range = semver.Range(version);
      var isPegged = range.set.every(function (comparators) {
        return comparators.length === 1 && String(comparators[0].operator || '') === '';
      });
      if (ignorePegged && isPegged) {
        return addUntouched(name, version, { isPegged: true });
      }
      return true;
    });
  }
  var mapNameToLatest = function (name) {
    return new Promise(function (resolve, reject) {
      lookupDistTags(name, function (prefix, distTags) {
        var version = distTags.latest;
        var existing = pkg[type][name];
        var isUpdateable = false;
        try {
          var range = semver.Range(existing);
          isUpdateable = !semver.ltr(version, range);
        } catch (e) {}
        var updated = prefix + version;
        var result;

        // If there is no version or the version is the latest.
        result = {
          name: name,
          before: existing,
          after: updated,
          isChanged: version !== null && isUpdateable && existing !== updated,
          isUpdateable: isUpdateable
        };
        if (result.isChanged) {
          // Actually write to the package descriptor.
          pkg[type][name] = updated;
        }

        resolve(result);
      });
    });
  };
  return names.map(mapNameToLatest).concat(untouched);
}

function loadNPM() {
  return new Promise(function (resolve, reject) {
    npm.load({}, function (err, result) {
      if (err) {
        reject(err);
      } else {
        resolve(result);
      }
    });
  });
}

/**
 * Given a package name, lookup the semantic tags.
 *
 * @param {string} name - The module name.
 * @param {Function} callback - A function to call with the dist tags.
 */
function lookupDistTags(name, callback) {
  // Need to require here, because NPM does all sorts of funky global
  // attaching.
  var view = require('npm/lib/view');
  var prefix = npm.config.get('save-prefix');

  // Call View directly to ensure the arguments actually work.
  view([name, 'dist-tags'], true, function (err, desc) {
    if (err) { throw new Error(err); }
    var latest = Object.keys(desc);
    if (latest.length !== 1) {
      throw new Error('expected 1 version key, got: ' + latest);
    }
    var tags = desc[latest]['dist-tags'];
    callback(prefix, tags);
  });
}

module.exports = salita;
<commit_msg>Handle the case where a package is not found.<commit_after>var fs = require('fs');
var path = require('path');
var npm = require('npm');
var jsonFile = require('json-file-plus');
var Table = require('cli-table');
var chalk = require('chalk');
var Promise = require('promise');
var assign = require('object.assign');
var semver = require('semver');

var getTable = function () {
  return new Table({
    chars: {
      top: '',
      'top-mid': '',
      'top-left': '',
      'top-right': '',
      bottom: '',
      'bottom-mid': '',
      'bottom-left': '',
      'bottom-right': '',
      left: '',
      'left-mid': '',
      mid: '',
      'mid-mid': '',
      right: '',
      'right-mid': '',
      middle: ''
    }
  });
};

var createResultJSON = function (key) {
  return function (results) {
    var obj = {};
    obj[key] = results;
    return obj;
  };
};

var createResultTable = function (caption) {
  return function (results) {
    var table = getTable();
    if (results.length > 0) {
      var tableRows = results.map(function (result) {
        if (result.isChanged) {
          return [
            chalk.green('Changed: '),
            result.name,
            'from',
            chalk.yellow(result.before),
            'to',
            chalk.yellow(result.after)
          ];
        } else if (result.error) {
          return [
            chalk.red('Package not found: '),
            result.name,
            'at',
            chalk.yellow(result.before),
            chalk.bold.red('?')
          ];
        } else if (!result.isUpdateable && !result.isStar && !result.isPegged) {
          return [
            chalk.red('Requested range not satisfied by: '),
            result.name,
            'from',
            chalk.yellow(result.before),
            'to',
            chalk.yellow(result.after)
          ];
        } else {
          return [
            chalk.blue('Kept: '),
            result.name,
            'at',
            chalk.yellow(result.before)
          ];
        }
      });
      table.push.apply(table, tableRows);
      var sortByName = function (a, b) {
        return a[1].localeCompare(b[1]);
      };
      table.sort(sortByName);
    } else {
      table.push([chalk.gray('None found')]);
    }
    return [
      chalk.green.underline(caption + ':'),
      table
    ];
  };
};

/**
 * The main entry point.
 */
function salita(dir, options, callback) {
  chalk.enabled = !options['no-color'] && !options.json;
  // Package.json.
  var filename = path.join(dir, 'package.json');
  jsonFile(filename).then(function (pkg) {
    return loadNPM().then(function () { return pkg; });
  }).then(function (pkg) {
    if (pkg && !options.json) {
      console.log('Found package.json.');
    }

    // Check all the dependencies.
    var depLookups = Promise.all(dependenciesLookup(pkg.data, 'dependencies', options['ignore-stars'], options['ignore-pegged']));
    var deps = depLookups.then(options.json ? createResultJSON('dependencies') : createResultTable('Dependencies'));

    // Check all the devDependencies.
    var devDepLookups = Promise.all(dependenciesLookup(pkg.data, 'devDependencies', options['ignore-stars'], options['ignore-pegged']));
    var devDeps = devDepLookups.then(options.json ? createResultJSON('devDependencies') : createResultTable('Development Dependencies'));

    // Wait for all of them to resolve.
    Promise.all([deps, devDeps]).then(function (depResults) {
      if (options.json) {
        console.log(JSON.stringify(assign.apply(null, [{}].concat(depResults)), null, 2));
      } else {
        depResults.forEach(function (results) {
          results.map(String).forEach(function (result) {
            console.log(result);
          });
        });
      }

      var getDepCounts = function (results) {
        var totalDeps = results.length;
        var changedDeps = results.filter(function (result) { return result.isChanged; }).length;
        return [totalDeps, changedDeps];
      };
      var mapThen = function (a, b) { return function (promise) { return promise.then(a, b); }; };
      var counts = Promise.all([depLookups, devDepLookups].map(mapThen(getDepCounts)));

      // Write back the package.json.
      if (options['dry-run']) {
        callback(counts);
      } else {
        pkg.save(callback.bind(null, counts));
      }
    });
  }).done();
}

/**
 * createDependenciesLookup
 *
 * @param type
 * @return
 */
function dependenciesLookup(pkg, type, ignoreStars, ignorePegged) {
  // See if any dependencies of this type exist.
  if (pkg[type] && !Object.keys(pkg[type] || []).length) {
    return [];
  }

  var names = Object.keys(pkg[type] || []);
  // Loop through and map the "lookup latest" to promises.
  var names = Object.keys(pkg[type] || []);
  var untouched = [];
  var addUntouched = function (name, version, flags) {
    untouched.push(Promise.resolve(assign({
      name: name,
      before: version,
      after: version,
      isChanged: false,
      isUpdateable: false
    }, flags)));
  };
  if (ignoreStars || ignorePegged) {
    names = names.filter(function (name) {
      var version = pkg[type][name];

      var isStar = version === '*';
      if (ignoreStars && isStar) {
        return addUntouched(name, version, { isStar: true });
      }

      var range = semver.Range(version);
      var isPegged = range.set.every(function (comparators) {
        return comparators.length === 1 && String(comparators[0].operator || '') === '';
      });
      if (ignorePegged && isPegged) {
        return addUntouched(name, version, { isPegged: true });
      }
      return true;
    });
  }
  var mapNameToLatest = function (name) {
    return new Promise(function (resolve) {
      lookupDistTags(name, function (error, prefix, distTags) {
        var existing = pkg[type][name];
        if (error) {
          return resolve({
            name: name,
            before: existing,
            after: existing,
            isChanged: false,
            isUpdateable: false,
            error: error
          });
        }
        var version = distTags.latest;
        var isUpdateable = false;
        try {
          var range = semver.Range(existing);
          isUpdateable = !semver.ltr(version, range);
        } catch (e) {}
        var updated = prefix + version;
        var result;

        // If there is no version or the version is the latest.
        result = {
          name: name,
          before: existing,
          after: updated,
          isChanged: version !== null && isUpdateable && existing !== updated,
          isUpdateable: isUpdateable
        };
        if (result.isChanged) {
          // Actually write to the package descriptor.
          pkg[type][name] = updated;
        }

        resolve(result);
      });
    });
  };
  return names.map(mapNameToLatest).concat(untouched);
}

function loadNPM() {
  return new Promise(function (resolve, reject) {
    npm.load({}, function (err, result) {
      if (err) {
        reject(err);
      } else {
        resolve(result);
      }
    });
  });
}

/**
 * Given a package name, lookup the semantic tags.
 *
 * @param {string} name - The module name.
 * @param {Function} callback - A function to call with the dist tags.
 */
function lookupDistTags(name, callback) {
  // Need to require here, because NPM does all sorts of funky global
  // attaching.
  var view = require('npm/lib/view');
  var prefix = npm.config.get('save-prefix');

  // Call View directly to ensure the arguments actually work.
  view([name, 'dist-tags'], true, function (err, desc) {
    if (err) { return callback(err); }
    var latest = Object.keys(desc);
    if (latest.length !== 1) {
      throw new Error('expected 1 version key, got: ' + latest);
    }
    var tags = desc[latest]['dist-tags'];
    callback(null, prefix, tags);
  });
}

module.exports = salita;
<|endoftext|>


<commit_before>'use strict';

var assign            = require('es5-ext/object/assign')
  , normalizeOptions  = require('es5-ext/object/normalize-options')
  , setPrototypeOf    = require('es5-ext/object/set-prototype-of')
  , ensureString      = require('es5-ext/object/validate-stringifiable-value')
  , ensureObject      = require('es5-ext/object/valid-object')
  , d                 = require('d')
  , lazy              = require('d/lazy')
  , deferred          = require('deferred')
  , resolveKeyPath    = require('dbjs/_setup/utils/resolve-key-path')
  , resolve           = require('path').resolve
  , mkdir             = require('fs2/mkdir')
  , rmdir             = require('fs2/rmdir')
  , level             = require('levelup')
  , PersistenceDriver = require('dbjs-persistence/abstract')

  , isArray = Array.isArray, create = Object.create, stringify = JSON.stringify, parse = JSON.parse
  , getOpts = { fillCache: false };

var makeDb = function (path, options) {
	return mkdir(path, { intermediate: true })(function () { return level(path, options); });
};

var LevelDriver = module.exports = function (dbjs, data) {
	if (!(this instanceof LevelDriver)) return new LevelDriver(dbjs, data);
	this._dbOptions = normalizeOptions(ensureObject(data));
	// Below is workaround for https://github.com/Raynos/xtend/pull/28
	this._dbOptions.hasOwnProperty = Object.prototype.hasOwnProperty;
	this._dbOptions.path = ensureString(this._dbOptions.path);
	PersistenceDriver.call(this, dbjs, data);
};
setPrototypeOf(LevelDriver, PersistenceDriver);

LevelDriver.prototype = Object.create(PersistenceDriver.prototype, assign({
	constructor: d(LevelDriver),

	// Any data
	__getRaw: d(function (cat, ns, path) {
		if (cat === 'reduced') return this._getReduced(ns + (path ? ('/' + path) : ''));
		if (cat === 'computed') return this._getComputed(path, ns);
		return this._getDirect(ns, path);
	}),
	__storeRaw: d(function (cat, ns, path, data) {
		if (cat === 'reduced') return this._storeReduced(ns + (path ? ('/' + path) : ''), data);
		if (cat === 'computed') return this._storeComputed(path, ns, data);
		return this._storeDirect(ns, path, data);
	}),

	// Database data
	__getDirectObject: d(function (objId, keyPaths) {
		return this._loadDirect({ gte: objId, lte: objId + '/\uffff' },
			keyPaths && function (ownerId, path) { return keyPaths.has(resolveKeyPath(path)); });
	}),
	__getDirectAll: d(function () { return this._loadDirect(); }),

	// Size tracking
	__searchDirect: d(function (callback) {
		return this.directDb(function (db) {
			var def = deferred();
			db.createReadStream().on('data', function (data) {
				var index;
				index = data.value.indexOf('.');
				callback(data.key, {
					stamp: Number(data.value.slice(0, index)),
					value: data.value.slice(index + 1)
				});
			}).on('error', def.reject).on('end', def.resolve);
			return def.promise;
		});
	}),
	__searchComputed: d(function (keyPath, callback) {
		return this.computedDb(function (db) {
			var def = deferred();
			db.createReadStream({ gte: keyPath + ':', lte: keyPath + ':\uffff' })
				.on('data', function (data) {
					var index, value, ownerId = data.key.slice(data.key.lastIndexOf(':') + 1);
					index = data.value.indexOf('.');
					value = data.value.slice(index + 1);
					if (value[0] === '[') value = parse(value);
					callback(ownerId, { value: value, stamp: Number(data.value.slice(0, index)) });
				}).on('error', def.reject).on('end', def.resolve);
			return def.promise;
		});
	}),

	// Reduced data
	__getReducedNs: d(function (ns, keyPaths) {
		return this.reducedDb(function (db) {
			var def, result;
			def = deferred();
			result = create(null);
			db.createReadStream({ gte: ns, lte: ns + '/\uffff' })
				.on('data', function (data) {
					var index, path;
					if (keyPaths) {
						index = data.key.indexOf('/');
						path = (index !== -1) ? data.key.slice(index + 1) : null;
						if (!keyPaths.has(path)) return; // filtered
					}
					index = data.value.indexOf('.');
					result[data.key] = {
						stamp: Number(data.value.slice(0, index)),
						value: data.value.slice(index + 1)
					};
				}).on('error', def.reject).on('end', function () { def.resolve(result); });
			return def.promise;
		});
	}),

	// Storage import/export
	__exportAll: d(function (destDriver) {
		var count = 0;
		var promise = deferred(
			this.directDb(function (db) {
				var def, promises = [];
				def = deferred();
				db.createReadStream().on('data', function (record) {
					var index, ns, path, data;
					if (!(++count % 1000)) promise.emit('progress');
					index = record.value.indexOf('.');
					data = {
						value: record.value.slice(index + 1),
						stamp: Number(record.value.slice(0, index))
					};
					index = record.key.indexOf('/');
					ns = (index === -1) ? record.key : record.key.slice(0, index);
					path = (index === -1) ? null : record.key.slice(index + 1);
					promises.push(destDriver.__storeRaw('direct', ns, path, data));
				}.bind(this)).on('error', function (err) { def.reject(err); }).on('end', function () {
					def.resolve(deferred.map(promises));
				});
				return def.promise;
			}),
			this.computedDb(function (db) {
				var def, promises = [];
				def = deferred();
				db.createReadStream().on('data', function (record) {
					var index, ns, path, data;
					if (!(++count % 1000)) promise.emit('progress');
					index = record.value.indexOf('.');
					data = {
						value: record.value.slice(index + 1),
						stamp: Number(record.value.slice(0, index))
					};
					index = record.key.lastIndexOf(':');
					ns = record.key.slice(0, index);
					path = record.key.slice(index + 1);
					promises.push(destDriver.__storeRaw('computed', ns, path, data));
				}.bind(this)).on('error', function (err) { def.reject(err); }).on('end', function () {
					def.resolve(deferred.map(promises));
				});
				return def.promise;
			}),
			this.reducedDb(function (db) {
				var def, promises = [];
				def = deferred();
				db.createReadStream().on('data', function (record) {
					var index, ns, path, data;
					if (!(++count % 1000)) promise.emit('progress');
					index = record.value.indexOf('.');
					data = {
						value: record.value.slice(index + 1),
						stamp: Number(record.value.slice(0, index))
					};
					index = record.key.indexOf('/');
					ns = (index === -1) ? record.key : record.key.slice(0, index);
					path = (index === -1) ? null : record.key.slice(index + 1);
					promises.push(destDriver.__storeRaw('reduced', ns, path, data));
				}.bind(this)).on('error', function (err) { def.reject(err); }).on('end', function () {
					def.resolve(deferred.map(promises));
				});
				return def.promise;
			})
		)(Function.prototype);
		return promise;
	}),
	__clear: d(function () {
		return this.__close()(function () {
			return rmdir(this._dbOptions.path, { recursive: true, force: true })(function () {
				delete this.directDb;
				delete this.computedDb;
				delete this.reducedDb;
			}.bind(this));
		}.bind(this));
	}),

	// Connection related
	__close: d(function () {
		return deferred(
			this.hasOwnProperty('directDb') && this.directDb.invokeAsync('close'),
			this.hasOwnProperty('computedDb') && this.computedDb.invokeAsync('close'),
			this.hasOwnProperty('reducedDb') && this.reducedDb.invokeAsync('close')
		);
	}),

	// Driver specific
	_getDirect: d(function (ownerId, path) {
		var id = ownerId + (path ? ('/' + path) : '');
		return this.directDb.invokeAsync('get', id, getOpts)(function (value) {
			var index = value.indexOf('.');
			return { stamp: Number(value.slice(0, index)), value: value.slice(index + 1) };
		}.bind(this), function (err) {
			if (err.notFound) return null;
			throw err;
		});
	}),
	_storeDirect: d(function (ownerId, path, data) {
		return this.directDb.invokeAsync('put', ownerId + (path ? ('/' + path) : ''),
			data.stamp + '.' + data.value);
	}),
	_getComputed: d(function (ownerId, keyPath) {
		return this.computedDb.invokeAsync('get', keyPath + ':' + ownerId, getOpts)(function (data) {
			var index = data.indexOf('.'), value = data.slice(index + 1);
			if (value[0] === '[') value = parse(value);
			return { value: value, stamp: Number(data.slice(0, index)) };
		}.bind(this), function (err) {
			if (err.notFound) return null;
			throw err;
		});
	}),
	_storeComputed: d(function (objId, keyPath, data) {
		return this.computedDb.invokeAsync('put', keyPath + ':' + objId,
			data.stamp + '.' + (isArray(data.value) ? stringify(data.value) : data.value));
	}),
	_getReduced: d(function (key) {
		return this.reducedDb.invokeAsync('get', key, getOpts)(function (value) {
			var index = value.indexOf('.');
			return { stamp: Number(value.slice(0, index)), value: value.slice(index + 1) };
		}, function (err) {
			if (err.notFound) return;
			throw err;
		});
	}),
	_storeReduced: d(function (key, data) {
		return this.reducedDb.invokeAsync('put', key, data.stamp + '.' + data.value);
	}),
	_loadDirect: d(function (data, filter) {
		return this.directDb(function (db) {
			var def, result;
			def = deferred();
			result = create(null);
			db.createReadStream(data).on('data', function (data) {
				var index, ownerId, path;
				if (filter) {
					index = data.key.indexOf('/');
					ownerId = (index !== -1) ? data.key.slice(0, index) : data.key;
					path = (index !== -1) ? data.key.slice(index + 1) : null;
					if (!filter(ownerId, path)) return; // filtered
				}
				index = data.value.indexOf('.');
				result[data.key] = {
					stamp: Number(data.value.slice(0, index)),
					value: data.value.slice(index + 1)
				};
			}.bind(this)).on('error', def.reject).on('end', function () { def.resolve(result); });
			return def.promise;
		});
	})
}, lazy({
	directDb: d(function () {
		return makeDb(resolve(this._dbOptions.path, 'direct'), this._dbOptions);
	}),
	computedDb: d(function () {
		return makeDb(resolve(this._dbOptions.path, 'computed'), this._dbOptions);
	}),
	reducedDb: d(function () {
		return makeDb(resolve(this._dbOptions.path, 'reduced'), this._dbOptions);
	})
})));
<commit_msg>Rename objId to ownerId<commit_after>'use strict';

var assign            = require('es5-ext/object/assign')
  , normalizeOptions  = require('es5-ext/object/normalize-options')
  , setPrototypeOf    = require('es5-ext/object/set-prototype-of')
  , ensureString      = require('es5-ext/object/validate-stringifiable-value')
  , ensureObject      = require('es5-ext/object/valid-object')
  , d                 = require('d')
  , lazy              = require('d/lazy')
  , deferred          = require('deferred')
  , resolveKeyPath    = require('dbjs/_setup/utils/resolve-key-path')
  , resolve           = require('path').resolve
  , mkdir             = require('fs2/mkdir')
  , rmdir             = require('fs2/rmdir')
  , level             = require('levelup')
  , PersistenceDriver = require('dbjs-persistence/abstract')

  , isArray = Array.isArray, create = Object.create, stringify = JSON.stringify, parse = JSON.parse
  , getOpts = { fillCache: false };

var makeDb = function (path, options) {
	return mkdir(path, { intermediate: true })(function () { return level(path, options); });
};

var LevelDriver = module.exports = function (dbjs, data) {
	if (!(this instanceof LevelDriver)) return new LevelDriver(dbjs, data);
	this._dbOptions = normalizeOptions(ensureObject(data));
	// Below is workaround for https://github.com/Raynos/xtend/pull/28
	this._dbOptions.hasOwnProperty = Object.prototype.hasOwnProperty;
	this._dbOptions.path = ensureString(this._dbOptions.path);
	PersistenceDriver.call(this, dbjs, data);
};
setPrototypeOf(LevelDriver, PersistenceDriver);

LevelDriver.prototype = Object.create(PersistenceDriver.prototype, assign({
	constructor: d(LevelDriver),

	// Any data
	__getRaw: d(function (cat, ns, path) {
		if (cat === 'reduced') return this._getReduced(ns + (path ? ('/' + path) : ''));
		if (cat === 'computed') return this._getComputed(path, ns);
		return this._getDirect(ns, path);
	}),
	__storeRaw: d(function (cat, ns, path, data) {
		if (cat === 'reduced') return this._storeReduced(ns + (path ? ('/' + path) : ''), data);
		if (cat === 'computed') return this._storeComputed(path, ns, data);
		return this._storeDirect(ns, path, data);
	}),

	// Database data
	__getDirectObject: d(function (ownerId, keyPaths) {
		return this._loadDirect({ gte: ownerId, lte: ownerId + '/\uffff' },
			keyPaths && function (ownerId, path) { return keyPaths.has(resolveKeyPath(path)); });
	}),
	__getDirectAll: d(function () { return this._loadDirect(); }),

	// Size tracking
	__searchDirect: d(function (callback) {
		return this.directDb(function (db) {
			var def = deferred();
			db.createReadStream().on('data', function (data) {
				var index;
				index = data.value.indexOf('.');
				callback(data.key, {
					stamp: Number(data.value.slice(0, index)),
					value: data.value.slice(index + 1)
				});
			}).on('error', def.reject).on('end', def.resolve);
			return def.promise;
		});
	}),
	__searchComputed: d(function (keyPath, callback) {
		return this.computedDb(function (db) {
			var def = deferred();
			db.createReadStream({ gte: keyPath + ':', lte: keyPath + ':\uffff' })
				.on('data', function (data) {
					var index, value, ownerId = data.key.slice(data.key.lastIndexOf(':') + 1);
					index = data.value.indexOf('.');
					value = data.value.slice(index + 1);
					if (value[0] === '[') value = parse(value);
					callback(ownerId, { value: value, stamp: Number(data.value.slice(0, index)) });
				}).on('error', def.reject).on('end', def.resolve);
			return def.promise;
		});
	}),

	// Reduced data
	__getReducedNs: d(function (ns, keyPaths) {
		return this.reducedDb(function (db) {
			var def, result;
			def = deferred();
			result = create(null);
			db.createReadStream({ gte: ns, lte: ns + '/\uffff' })
				.on('data', function (data) {
					var index, path;
					if (keyPaths) {
						index = data.key.indexOf('/');
						path = (index !== -1) ? data.key.slice(index + 1) : null;
						if (!keyPaths.has(path)) return; // filtered
					}
					index = data.value.indexOf('.');
					result[data.key] = {
						stamp: Number(data.value.slice(0, index)),
						value: data.value.slice(index + 1)
					};
				}).on('error', def.reject).on('end', function () { def.resolve(result); });
			return def.promise;
		});
	}),

	// Storage import/export
	__exportAll: d(function (destDriver) {
		var count = 0;
		var promise = deferred(
			this.directDb(function (db) {
				var def, promises = [];
				def = deferred();
				db.createReadStream().on('data', function (record) {
					var index, ns, path, data;
					if (!(++count % 1000)) promise.emit('progress');
					index = record.value.indexOf('.');
					data = {
						value: record.value.slice(index + 1),
						stamp: Number(record.value.slice(0, index))
					};
					index = record.key.indexOf('/');
					ns = (index === -1) ? record.key : record.key.slice(0, index);
					path = (index === -1) ? null : record.key.slice(index + 1);
					promises.push(destDriver.__storeRaw('direct', ns, path, data));
				}.bind(this)).on('error', function (err) { def.reject(err); }).on('end', function () {
					def.resolve(deferred.map(promises));
				});
				return def.promise;
			}),
			this.computedDb(function (db) {
				var def, promises = [];
				def = deferred();
				db.createReadStream().on('data', function (record) {
					var index, ns, path, data;
					if (!(++count % 1000)) promise.emit('progress');
					index = record.value.indexOf('.');
					data = {
						value: record.value.slice(index + 1),
						stamp: Number(record.value.slice(0, index))
					};
					index = record.key.lastIndexOf(':');
					ns = record.key.slice(0, index);
					path = record.key.slice(index + 1);
					promises.push(destDriver.__storeRaw('computed', ns, path, data));
				}.bind(this)).on('error', function (err) { def.reject(err); }).on('end', function () {
					def.resolve(deferred.map(promises));
				});
				return def.promise;
			}),
			this.reducedDb(function (db) {
				var def, promises = [];
				def = deferred();
				db.createReadStream().on('data', function (record) {
					var index, ns, path, data;
					if (!(++count % 1000)) promise.emit('progress');
					index = record.value.indexOf('.');
					data = {
						value: record.value.slice(index + 1),
						stamp: Number(record.value.slice(0, index))
					};
					index = record.key.indexOf('/');
					ns = (index === -1) ? record.key : record.key.slice(0, index);
					path = (index === -1) ? null : record.key.slice(index + 1);
					promises.push(destDriver.__storeRaw('reduced', ns, path, data));
				}.bind(this)).on('error', function (err) { def.reject(err); }).on('end', function () {
					def.resolve(deferred.map(promises));
				});
				return def.promise;
			})
		)(Function.prototype);
		return promise;
	}),
	__clear: d(function () {
		return this.__close()(function () {
			return rmdir(this._dbOptions.path, { recursive: true, force: true })(function () {
				delete this.directDb;
				delete this.computedDb;
				delete this.reducedDb;
			}.bind(this));
		}.bind(this));
	}),

	// Connection related
	__close: d(function () {
		return deferred(
			this.hasOwnProperty('directDb') && this.directDb.invokeAsync('close'),
			this.hasOwnProperty('computedDb') && this.computedDb.invokeAsync('close'),
			this.hasOwnProperty('reducedDb') && this.reducedDb.invokeAsync('close')
		);
	}),

	// Driver specific
	_getDirect: d(function (ownerId, path) {
		var id = ownerId + (path ? ('/' + path) : '');
		return this.directDb.invokeAsync('get', id, getOpts)(function (value) {
			var index = value.indexOf('.');
			return { stamp: Number(value.slice(0, index)), value: value.slice(index + 1) };
		}.bind(this), function (err) {
			if (err.notFound) return null;
			throw err;
		});
	}),
	_storeDirect: d(function (ownerId, path, data) {
		return this.directDb.invokeAsync('put', ownerId + (path ? ('/' + path) : ''),
			data.stamp + '.' + data.value);
	}),
	_getComputed: d(function (ownerId, keyPath) {
		return this.computedDb.invokeAsync('get', keyPath + ':' + ownerId, getOpts)(function (data) {
			var index = data.indexOf('.'), value = data.slice(index + 1);
			if (value[0] === '[') value = parse(value);
			return { value: value, stamp: Number(data.slice(0, index)) };
		}.bind(this), function (err) {
			if (err.notFound) return null;
			throw err;
		});
	}),
	_storeComputed: d(function (ownerId, keyPath, data) {
		return this.computedDb.invokeAsync('put', keyPath + ':' + ownerId,
			data.stamp + '.' + (isArray(data.value) ? stringify(data.value) : data.value));
	}),
	_getReduced: d(function (key) {
		return this.reducedDb.invokeAsync('get', key, getOpts)(function (value) {
			var index = value.indexOf('.');
			return { stamp: Number(value.slice(0, index)), value: value.slice(index + 1) };
		}, function (err) {
			if (err.notFound) return;
			throw err;
		});
	}),
	_storeReduced: d(function (key, data) {
		return this.reducedDb.invokeAsync('put', key, data.stamp + '.' + data.value);
	}),
	_loadDirect: d(function (data, filter) {
		return this.directDb(function (db) {
			var def, result;
			def = deferred();
			result = create(null);
			db.createReadStream(data).on('data', function (data) {
				var index, ownerId, path;
				if (filter) {
					index = data.key.indexOf('/');
					ownerId = (index !== -1) ? data.key.slice(0, index) : data.key;
					path = (index !== -1) ? data.key.slice(index + 1) : null;
					if (!filter(ownerId, path)) return; // filtered
				}
				index = data.value.indexOf('.');
				result[data.key] = {
					stamp: Number(data.value.slice(0, index)),
					value: data.value.slice(index + 1)
				};
			}.bind(this)).on('error', def.reject).on('end', function () { def.resolve(result); });
			return def.promise;
		});
	})
}, lazy({
	directDb: d(function () {
		return makeDb(resolve(this._dbOptions.path, 'direct'), this._dbOptions);
	}),
	computedDb: d(function () {
		return makeDb(resolve(this._dbOptions.path, 'computed'), this._dbOptions);
	}),
	reducedDb: d(function () {
		return makeDb(resolve(this._dbOptions.path, 'reduced'), this._dbOptions);
	})
})));
<|endoftext|>


<commit_before>"use strict";

var path = require("path");

var async = require("async"),
    knox = require("knox"),
    env = require("require-env");

var client = knox.createClient({
  key: env.require("AWS_ACCESS_KEY_ID"),
  secret: env.require("AWS_SECRET_ACCESS_KEY"),
  bucket: env.require("S3_BUCKET")
});

module.exports = function(options, callback) {
  if (options.extension &&
      options.extension.indexOf(".") !== 0) {
    options.extension = "." + options.extension;
  }

  var count,
      deletedKeyCount = 0,
      seenKeyCount = 0,
      marker;

  var interval = setInterval(function() {
    console.log("Deleted %d/%d keys from '%s'.", deletedKeyCount, seenKeyCount, options.prefix);
  }, 10000).unref();

  return async.doWhilst(
    function(next) {
      return client.list({
        marker: marker || "",
        prefix: options.prefix
      }, function(err, data) {
        if (err) {
          return next(err);
        }

        seenKeyCount += data.Contents.length;

        var keys = data.Contents
          .filter(function(x) {
            // only match the provided extension
            return !options.extension ||
                   options.extension === path.extname(x.Key);
          })
          .filter(function(x) {
            // only match objects older than minAge
            var age = ~~(Date.now() / 1000 - x.LastModified.getTime() / 1000);

            return !options.minAge ||
                  age > options.minAge;
          })
          .map(function(x) {
            return x.Key;
          });

        count = keys.length;
        marker = data.Marker;

        return client.deleteMultiple(keys, function(err) {
          process.stdout.write(".");
          deletedKeyCount += keys.length;

          return next(err);
        });
      });
    },
    function() { return count > 0; },
    function(err) {
      clearInterval(interval);

      if (err) {
        console.error(err);
      }

      console.log();
      console.log("Deleted %d/%d keys from '%s'.", deletedKeyCount, seenKeyCount, options.prefix);
      return callback(null, deletedKeyCount);
    });
};
<commit_msg>Move deletions to a separate queue<commit_after>"use strict";

var path = require("path");

var async = require("async"),
    knox = require("knox"),
    env = require("require-env");

var client = knox.createClient({
  key: env.require("AWS_ACCESS_KEY_ID"),
  secret: env.require("AWS_SECRET_ACCESS_KEY"),
  bucket: env.require("S3_BUCKET")
});

module.exports = function(options, callback) {
  if (options.extension &&
      options.extension.indexOf(".") !== 0) {
    options.extension = "." + options.extension;
  }

  var count,
      deletedKeyCount = 0,
      seenKeyCount = 0,
      marker;

  var interval = setInterval(function() {
    console.log("Deleted %d/%d keys from '%s'.", deletedKeyCount, seenKeyCount, options.prefix);
  }, 10000).unref();

  var deleteQueue = async.queue(function(task, done) {
    return client.deleteMultiple(task.keys, function(err) {
      process.stdout.write(".");
      deletedKeyCount += task.keys.length;

      return done(err);
    });
  }, 20);

  return async.doWhilst(
    function(next) {
      return client.list({
        marker: marker || "",
        prefix: options.prefix
      }, function(err, data) {
        if (err) {
          return next(err);
        }

        seenKeyCount += data.Contents.length;

        var keys = data.Contents
          .filter(function(x) {
            // only match the provided extension
            return !options.extension ||
                   options.extension === path.extname(x.Key);
          })
          .filter(function(x) {
            // only match objects older than minAge
            var age = ~~(Date.now() / 1000 - x.LastModified.getTime() / 1000);

            return !options.minAge ||
                  age > options.minAge;
          })
          .map(function(x) {
            return x.Key;
          });

        count = keys.length;
        marker = data.Marker;

        if (keys.length > 0) {
          deleteQueue.push({ keys: keys });
        }

        return next();
      });
    },
    function() { return count > 0; },
    function(err) {
      if (err) {
        console.error(err);
      }

      var done = function() {
        clearInterval(interval);

        console.log();
        console.log("Deleted %d/%d keys from '%s'.", deletedKeyCount, seenKeyCount, options.prefix);
        return callback.apply(null, arguments);
      };

      if (deleteQueue.length() === 0 &&
          deleteQueue.running() === 0) {
        return done(null, deletedKeyCount);
      }

      // at this point, potentially many deletions have been queued up, so we
      // can wait for the queue to announce that it has drained and return
      deleteQueue.drain = function() {
        return done(null, deletedKeyCount);
      };
    });
};
<|endoftext|>


<commit_before>// dependencies
var url = require('url');
// express
var _ = require('lomath');
var express = require('express');
var app = express();
// express middlewares
var morgan = require('morgan');
var ejs = require('ejs');
var bodyParser = require('body-parser');
var multer = require('multer');
// telegram bot
var bot = require(__dirname + '/lib/bot.js');
//redis
var redis = require('redis');

// global settings
var token = process.env.TOKEN || 'your example Telegram Bot token';
var webhookUrl = process.env.WEBHOOK || 'your app webhook url';
var clientID = process.env.CLIENT_ID || 'your Spotware Connect Client Public ID';
var clientSecret = process.env.CLIENT_SECRET || 'your Spotware Connect Client Secret';
var redisUrl = process.env.REDIS_URL || 'A redis instance URL for your app';

console.log("token=" + token);
console.log("webhookUrl=" + webhookUrl);
console.log("clientID=" + clientID);
console.log("clientSecret=" + clientSecret);
console.log("redisUrl=" + redisUrl);

var oauth2 = require('simple-oauth2')({
    clientID: clientID,
    clientSecret: clientSecret,
    site: 'https://connect.spotware.com',
    tokenPath: '/oauth/v2/token',
    authorizationPath: '/oauth/v2/auth'
});

var redisClient = redis.createClient(process.env.REDIS_URL);
redisClient.on("error", function (err) {
    console.log("Error " + err);
});

var cTraderBot = new bot(app, redisClient, token, webhookUrl);

// engine to render HTML
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
// set the port number
//app.set('port', process.env.PORT || 8443);
app.set('port', 8443);

// Mount middlewares defaulted for root:
// log all HTTP calls for debugging
app.use(morgan('combined'));
// use resources for html: HTML, JS and CSS etc.
app.use(express.static(__dirname + '/views'));
// parse incoming formData into JSON
app.use(bodyParser.json());

// Initial page redirecting to Github
app.get('/auth', function (req, res) {
    // Authorization uri definition
    var authorizationUri = oauth2.authCode.authorizeURL({
        redirect_uri: url.resolve(webhookUrl, '/callback'),
        access_type: 'online',
        approval_prompt: 'auto',
        scope: 'trading',
        state: req.query.state
    });
    res.redirect(authorizationUri);
});

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', function (req, res) {
    var code = req.query.code;
    var state = req.query.state;

    if (code && state) {
        oauth2.authCode.getToken({
            code: code,
            redirect_uri: url.resolve(webhookUrl, '/callback')
        }, saveToken);

        function saveToken(error, result) {
            if (error) {
                console.log('Access Token Error', error.message);
            }
            token = oauth2.accessToken.create(result);
            cTraderBot.saveToken(token, state);
            res.render('index');
        }
    } else {
        res.render('index');
    }
});
/*
app.get('/test', function(req, res) {
        console.log("******************");
        var data = fs.readFileSync("test/accounts.json");
        var jsonContent = JSON.parse(data);
        console.log(jsonContent);
        res.render('accounts', jsonContent)
});*/

// route: concise way to group all HTTP methods for a path
app.route('/')
    .get(function(req, res) {
        // console.log("you GET")
        res.render('index')
    })
    .post(function(req, res) {
        // send back to end req-res cycle
        res.json('okay, received\n');
        // robot handle as middleware for POST
        cTraderBot.handle(req, res)
    })
    .put(function(req, res) {
        res.send("you just called PUT\n")
    })

// finally, listen to the specific port for any calls
app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});
<commit_msg>Moving the bot initialization below app initialization<commit_after>// dependencies
var url = require('url');
// express
var _ = require('lomath');
var express = require('express');
var app = express();
// express middlewares
var morgan = require('morgan');
var ejs = require('ejs');
var bodyParser = require('body-parser');
var multer = require('multer');
// telegram bot
var bot = require(__dirname + '/lib/bot.js');
//redis
var redis = require('redis');

// global settings
var token = process.env.TOKEN || 'your example Telegram Bot token';
var webhookUrl = process.env.WEBHOOK || 'your app webhook url';
var clientID = process.env.CLIENT_ID || 'your Spotware Connect Client Public ID';
var clientSecret = process.env.CLIENT_SECRET || 'your Spotware Connect Client Secret';
var redisUrl = process.env.REDIS_URL || 'A redis instance URL for your app';

console.log("token=" + token);
console.log("webhookUrl=" + webhookUrl);
console.log("clientID=" + clientID);
console.log("clientSecret=" + clientSecret);
console.log("redisUrl=" + redisUrl);

var oauth2 = require('simple-oauth2')({
    clientID: clientID,
    clientSecret: clientSecret,
    site: 'https://connect.spotware.com',
    tokenPath: '/oauth/v2/token',
    authorizationPath: '/oauth/v2/auth'
});

// engine to render HTML
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
// set the port number
app.set('port', process.env.PORT || 8443);

// Mount middlewares defaulted for root:
// log all HTTP calls for debugging
app.use(morgan('combined'));
// use resources for html: HTML, JS and CSS etc.
app.use(express.static(__dirname + '/views'));
// parse incoming formData into JSON
app.use(bodyParser.json());

// Redis initialization
var redisClient = redis.createClient(process.env.REDIS_URL);
redisClient.on("error", function (err) {
    console.log("Error " + err);
});
var cTraderBot = new bot(app, redisClient, token, webhookUrl);

// Initial page redirecting to Github
app.get('/auth', function (req, res) {
    // Authorization uri definition
    var authorizationUri = oauth2.authCode.authorizeURL({
        redirect_uri: url.resolve(webhookUrl, '/callback'),
        access_type: 'online',
        approval_prompt: 'auto',
        scope: 'trading',
        state: req.query.state
    });
    res.redirect(authorizationUri);
});

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', function (req, res) {
    var code = req.query.code;
    var state = req.query.state;

    if (code && state) {
        oauth2.authCode.getToken({
            code: code,
            redirect_uri: url.resolve(webhookUrl, '/callback')
        }, saveToken);

        function saveToken(error, result) {
            if (error) {
                console.log('Access Token Error', error.message);
            }
            token = oauth2.accessToken.create(result);
            cTraderBot.saveToken(token, state);
            res.render('index');
        }
    } else {
        res.render('index');
    }
});
/*
app.get('/test', function(req, res) {
        console.log("******************");
        var data = fs.readFileSync("test/accounts.json");
        var jsonContent = JSON.parse(data);
        console.log(jsonContent);
        res.render('accounts', jsonContent)
});*/

// route: concise way to group all HTTP methods for a path
app.route('/')
    .get(function(req, res) {
        // console.log("you GET")
        res.render('index')
    })
    .post(function(req, res) {
        // send back to end req-res cycle
        res.json('okay, received\n');
        // robot handle as middleware for POST
        cTraderBot.handle(req, res)
    })
    .put(function(req, res) {
        res.send("you just called PUT\n")
    })

// finally, listen to the specific port for any calls
app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});
<|endoftext|>


<commit_before>var average = require('turf-average');
var sum = require('turf-sum');
var median = require('turf-median');
var min = require('turf-min');
var max = require('turf-max');
var deviation = require('turf-deviation');
var variance = require('turf-variance');
var count = require('turf-count');
var operations = {};
operations.average = average;
operations.sum = sum;
operations.median = median;
operations.min = min;
operations.max = max;
operations.deviation = deviation;
operations.variance = variance;
operations.count = count;

/**
* Calculates a series of aggregations for a set of {@link Point|points} within a set of {@link Polygon|polygons}. Sum, average, count, min, max, and deviation are supported.
*
* @module turf/aggregate
* @category aggregation
* @param {FeatureCollection<Polygon>} polygons polygons with values on which to aggregate
* @param {FeatureCollection<Point>} points points to be aggregated
* @param {Array} aggregations an array of aggregation objects
* @return {FeatureCollection<Polygon>} polygons with properties listed based on `outField` values in `aggregations`
* @example
* var polygons = {
*   "type": "FeatureCollection",
*   "features": [
*     {
*       "type": "Feature",
*       "properties": {},
*       "geometry": {
*         "type": "Polygon",
*         "coordinates": [[
*           [1.669921, 48.632908],
*           [1.669921, 49.382372],
*           [3.636474, 49.382372],
*           [3.636474, 48.632908],
*           [1.669921, 48.632908]
*         ]]
*       }
*     }, {
*       "type": "Feature",
*       "properties": {},
*       "geometry": {
*         "type": "Polygon",
*         "coordinates": [[
*           [2.230224, 47.85003],
*           [2.230224, 48.611121],
*           [4.361572, 48.611121],
*           [4.361572, 47.85003],
*           [2.230224, 47.85003]
*         ]]
*       }
*     }
*   ]
* };
* var points = {
*   "type": "FeatureCollection",
*   "features": [
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 200
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [2.054443,49.138596]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 600
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [3.065185,48.850258]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 100
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [2.329101,48.79239]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 200
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [2.614746,48.334343]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 300
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [3.416748,48.056053]
*       }
*     }
*   ]
* };
* var aggregations = [
*   {
*     aggregation: 'sum',
*     inField: 'population',
*     outField: 'pop_sum'
*   },
*   {
*     aggregation: 'average',
*     inField: 'population',
*     outField: 'pop_avg'
*   },
*   {
*     aggregation: 'median',
*     inField: 'population',
*     outField: 'pop_median'
*   },
*   {
*     aggregation: 'min',
*     inField: 'population',
*     outField: 'pop_min'
*   },
*   {
*     aggregation: 'max',
*     inField: 'population',
*     outField: 'pop_max'
*   },
*   {
*     aggregation: 'deviation',
*     inField: 'population',
*     outField: 'pop_deviation'
*   },
*   {
*     aggregation: 'variance',
*     inField: 'population',
*     outField: 'pop_variance'
*   },
*   {
*     aggregation: 'count',
*     inField: '',
*     outField: 'point_count'
*   }
* ];
*
* var aggregated = turf.aggregate(
*   polygons, points, aggregations);
*
* var result = turf.featurecollection(
*   points.features.concat(aggregated.features));
*
* //=result
*/

module.exports = function(polygons, points, aggregations){
  for (var i = 0, len = aggregations.length; i < len; i++) {
    var agg = aggregations[i],
      operation = agg.aggregation;

    if (isAggregationOperation(operation)) {
      if (operation === 'count') {
        polygons = operations[operation](polygons, points, agg.outField);
      } else {
        polygons = operations[operation](polygons, points, agg.inField, agg.outField);
      }
    } else {
      throw new Error('"'+ operation +'" is not a recognized aggregation operation.');
    }
  }

  return polygons;
};

function isAggregationOperation(operation) {
  return operation === 'average' ||
    operation === 'sum' ||
    operation === 'median' ||
    operation === 'min' ||
    operation === 'max' ||
    operation === 'deviation' ||
    operation === 'variance' ||
    operation === 'count';
}
<commit_msg>eslint fixes<commit_after>var average = require('turf-average');
var sum = require('turf-sum');
var median = require('turf-median');
var min = require('turf-min');
var max = require('turf-max');
var deviation = require('turf-deviation');
var variance = require('turf-variance');
var count = require('turf-count');
var operations = {};
operations.average = average;
operations.sum = sum;
operations.median = median;
operations.min = min;
operations.max = max;
operations.deviation = deviation;
operations.variance = variance;
operations.count = count;

/**
* Calculates a series of aggregations for a set of {@link Point|points} within a set of {@link Polygon|polygons}. Sum, average, count, min, max, and deviation are supported.
*
* @module turf/aggregate
* @category aggregation
* @param {FeatureCollection<Polygon>} polygons polygons with values on which to aggregate
* @param {FeatureCollection<Point>} points points to be aggregated
* @param {Array} aggregations an array of aggregation objects
* @return {FeatureCollection<Polygon>} polygons with properties listed based on `outField` values in `aggregations`
* @example
* var polygons = {
*   "type": "FeatureCollection",
*   "features": [
*     {
*       "type": "Feature",
*       "properties": {},
*       "geometry": {
*         "type": "Polygon",
*         "coordinates": [[
*           [1.669921, 48.632908],
*           [1.669921, 49.382372],
*           [3.636474, 49.382372],
*           [3.636474, 48.632908],
*           [1.669921, 48.632908]
*         ]]
*       }
*     }, {
*       "type": "Feature",
*       "properties": {},
*       "geometry": {
*         "type": "Polygon",
*         "coordinates": [[
*           [2.230224, 47.85003],
*           [2.230224, 48.611121],
*           [4.361572, 48.611121],
*           [4.361572, 47.85003],
*           [2.230224, 47.85003]
*         ]]
*       }
*     }
*   ]
* };
* var points = {
*   "type": "FeatureCollection",
*   "features": [
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 200
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [2.054443,49.138596]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 600
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [3.065185,48.850258]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 100
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [2.329101,48.79239]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 200
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [2.614746,48.334343]
*       }
*     },
*     {
*       "type": "Feature",
*       "properties": {
*         "population": 300
*       },
*       "geometry": {
*         "type": "Point",
*         "coordinates": [3.416748,48.056053]
*       }
*     }
*   ]
* };
* var aggregations = [
*   {
*     aggregation: 'sum',
*     inField: 'population',
*     outField: 'pop_sum'
*   },
*   {
*     aggregation: 'average',
*     inField: 'population',
*     outField: 'pop_avg'
*   },
*   {
*     aggregation: 'median',
*     inField: 'population',
*     outField: 'pop_median'
*   },
*   {
*     aggregation: 'min',
*     inField: 'population',
*     outField: 'pop_min'
*   },
*   {
*     aggregation: 'max',
*     inField: 'population',
*     outField: 'pop_max'
*   },
*   {
*     aggregation: 'deviation',
*     inField: 'population',
*     outField: 'pop_deviation'
*   },
*   {
*     aggregation: 'variance',
*     inField: 'population',
*     outField: 'pop_variance'
*   },
*   {
*     aggregation: 'count',
*     inField: '',
*     outField: 'point_count'
*   }
* ];
*
* var aggregated = turf.aggregate(
*   polygons, points, aggregations);
*
* var result = turf.featurecollection(
*   points.features.concat(aggregated.features));
*
* //=result
*/

module.exports = function(polygons, points, aggregations) {
  for (var i = 0, len = aggregations.length; i < len; i++) {
    var agg = aggregations[i],
      operation = agg.aggregation;

    if (isAggregationOperation(operation)) {
      if (operation === 'count') {
        polygons = operations[operation](polygons, points, agg.outField);
      } else {
        polygons = operations[operation](polygons, points, agg.inField, agg.outField);
      }
    } else {
      throw new Error('"'+ operation +'" is not a recognized aggregation operation.');
    }
  }

  return polygons;
};

function isAggregationOperation(operation) {
  return operation === 'average' ||
    operation === 'sum' ||
    operation === 'median' ||
    operation === 'min' ||
    operation === 'max' ||
    operation === 'deviation' ||
    operation === 'variance' ||
    operation === 'count';
}
<|endoftext|>