<commit_before>var util              = require('util')
  , AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
  , AbstractIterator  = require('abstract-leveldown').AbstractIterator
  , noop              = function () {}
  , setImmediate      = global.setImmediate || process.nextTick

function ldIterator (db, options) {

  AbstractIterator.call(this, db)
  var emptybuffer = new Buffer(0)

  this._dbsize = this.db.container.length();
  this._reverse = !!options.reverse

  // Test for empty buffer in end
  if(options.end instanceof Buffer){
    if(options.end.length = 0)
      this._end = this.db.container.key(this._dbsize - 1)
  }else{
    this._end = options.end
  }

  this._limit   = options.limit
  this._count   = 0

  if (options.start) {
    // if pos is more than the size of the database then set pos to the end
    var found = false;
    for (var i = 0; i < this._dbsize; i++) {
      if (this.db.container.key(i) >= options.start) {
          this._pos = i
          //Make sure we step back for mid values e.g 49.5 test
          if(this._reverse){
            if(this.db.container.key(i) > options.start){
              this._pos = i -1
            }else{
              this._pos = i
            }
          }
        found = true;
        break

      }
    }
    if(!found){
      this._pos = this._reverse ? this._dbsize - 1 : -1
    }

  } else {
    this._pos = this._reverse ? this._dbsize - 1 : 0
  }
}

util.inherits(ldIterator, AbstractIterator)

ldIterator.prototype._next = function (callback) {

  if (this._pos >= this.db.container.length() || this._pos < 0)
    return setImmediate(callback)
  var key   = this.db.container.key(this._pos)
    , value

  if (!!this._end && (this._reverse ? key < this._end : key > this._end))
    return setImmediate(callback)

  if (!!this._limit && this._limit > 0 && this._count++ >= this._limit)
    return setImmediate(callback)

  value = this.db.container.getItem(key)
  this._pos += this._reverse ? -1 : 1

  setImmediate(callback.bind(null, undefined, key, value))
}

function ld (location) {
  if (!(this instanceof ld)) return new ld(location)
  AbstractLevelDOWN.call(this, location)
  var wstore = require('./localstorage').localStorage;
  this.container = new wstore(location);
}

util.inherits(ld, AbstractLevelDOWN)

ld.prototype._open = function (options, callback) {
  setImmediate(function () { callback(null, this) }.bind(this))

}

ld.prototype._put = function (key, value, options, callback) {

  var err = checkKeyValue(key, 'key')

  if (err) return callback(err)

  err = checkKeyValue(value, 'value')

  if (err) return callback(err)

  if(typeof value == 'object' && value.buffer == undefined){
        var obj = {};
        obj.storetype = "json";
        obj.data = value;
        value = JSON.stringify(obj)
  }

  this.container.setItem(key, value);
  setImmediate(callback)
}

ld.prototype._get = function (key, options, callback) {

  var err = checkKeyValue(key, 'key')

  if (err) return callback(err)

  if (!isBuffer(key)){
    key = String(key)
  }
  var value = this.container.getItem(key);

  if (value === undefined) {
    // 'NotFound' error, consistent with LevelDOWN API
    return setImmediate(function () { callback(new Error('NotFound: ')) })
  }

  if (options.asBuffer !== false && !Buffer.isBuffer(value)){
    value = new Buffer(String(value))
  }

  if(options.asBuffer === false){
    if(value.indexOf("{\"storetype\":\"json\",\"data\"") > -1){
      var res = JSON.parse(value);
      value = res.data;
    }
  }

  setImmediate(function () {
    callback(null, value)
  })
}

ld.prototype._del = function (key, options, callback) {

  var err = checkKeyValue(key, 'key')

  if (err) return callback(err)
  if (!isBuffer(key)) key = String(key)

  this.container.removeItem(key);
  setImmediate(callback)
}

ld.prototype._batch = function (array, options, callback) {
  var err
    , i = 0
    , key
    , value
  if (Array.isArray(array)) {
    for (; i < array.length; i++) {
      if (array[i]) {
        key = Buffer.isBuffer(array[i].key) ? array[i].key : String(array[i].key)
        err = checkKeyValue(key, 'key')
        if (err) return setImmediate(callback.bind(null, err))
        if (array[i].type === 'del') {
          this._del(array[i].key, options, noop)
        } else if (array[i].type === 'put') {
          value = Buffer.isBuffer(array[i].value) ? array[i].value : String(array[i].value)
          err = checkKeyValue(value, 'value')
          if (err) return setImmediate(callback.bind(null, err))
          this._put(key, value, options, noop)
        }
      }
    }
  }
  setImmediate(callback)
}

ld.prototype._iterator = function (options) {
  return new ldIterator(this, options)
}

ld.destroy = function (name, callback) {
  try {
    Object.keys(localStorage)
      .forEach(function (key) {
        if (key.substring(0, name.length + 1) == (name + "!")) {
          localStorage.removeItem(key)
        }
      })
    callback()
  } catch (e) {
    // fail gracefully if no localStorage
  }
}

function subarray(start, end) {
  return this.slice(start, end)
}

function set_(array, offset) {

  if (arguments.length < 2) offset = 0

  for (var i = 0, n = array.length; i < n; ++i, ++offset)
    this[offset] = array[i] & 0xFF
}

// we need typed arrays
function TypedArray(arg1) {
var result;
  if (typeof arg1 === "number") {
    result = new Array(arg1);

    for (var i = 0; i < arg1; ++i)
        result[i] = 0;
  } else
    result = arg1.slice(0)

    result.subarray = subarray
    result.buffer = result
    result.byteLength = result.length
    result.set = set_

  if (typeof arg1 === "object" && arg1.buffer)
    result.buffer = arg1.buffer

  return result

}

/*
if(!window.Uint8Array){
  window.Uint8Array = TypedArray;
  window.Uint16Array = TypedArray;
  window.Uint32Array = TypedArray;
  window.Int32Array = TypedArray;
}
*/
function isBuffer(buf) {
  return buf instanceof ArrayBuffer
}

function checkKeyValue (obj, type) {
  if (obj === null || obj === undefined)
    return new Error(type + ' cannot be `null` or `undefined`')
  if (obj === null || obj === undefined)
    return new Error(type + ' cannot be `null` or `undefined`')

  if(type === 'key'){

    if(obj instanceof Boolean){
      return new Error(type + ' cannot be `null` or `undefined`')
    }
    if(obj === ''){
      return new Error(type + ' cannot be empty')
    }
/*
    if(isBuffer(obj)) {

      return new Error(type + 'cannot be an empty Buffer')
    }*/
  }
  if(obj.toString().indexOf("[object ArrayBuffer]") == 0){
      if(obj.byteLength == 0 || obj.byteLength == undefined){
        return new Error(type + ' cannot be an empty Buffer')
      }
  }

  /*if(obj.toString().indexOf("[object Uint8Array]") == 0){
      if(obj.byteLength == 0 || obj.byteLength == undefined){
        return new Error(type + ' cannot be an empty ArrayBuffer')
      }
  } */
  if (isBuffer(obj)) {

    if (obj.length === 0)
      return new Error(type + ' cannot be an empty Buffer')
  } else if (String(obj) === '')
    return new Error(type + ' cannot be an empty String')
}

module.exports = ld
<commit_msg>Additional buffer check in put<commit_after>var util              = require('util')
  , AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
  , AbstractIterator  = require('abstract-leveldown').AbstractIterator
  , noop              = function () {}
  , setImmediate      = global.setImmediate || process.nextTick

function ldIterator (db, options) {

  AbstractIterator.call(this, db)
  var emptybuffer = new Buffer(0)

  this._dbsize = this.db.container.length();
  this._reverse = !!options.reverse

  // Test for empty buffer in end
  if(options.end instanceof Buffer){
    if(options.end.length = 0)
      this._end = this.db.container.key(this._dbsize - 1)
  }else{
    this._end = options.end
  }

  this._limit   = options.limit
  this._count   = 0

  if (options.start) {
    // if pos is more than the size of the database then set pos to the end
    var found = false;
    for (var i = 0; i < this._dbsize; i++) {
      if (this.db.container.key(i) >= options.start) {
          this._pos = i
          //Make sure we step back for mid values e.g 49.5 test
          if(this._reverse){
            if(this.db.container.key(i) > options.start){
              this._pos = i -1
            }else{
              this._pos = i
            }
          }
        found = true;
        break

      }
    }
    if(!found){
      this._pos = this._reverse ? this._dbsize - 1 : -1
    }

  } else {
    this._pos = this._reverse ? this._dbsize - 1 : 0
  }
}

util.inherits(ldIterator, AbstractIterator)

ldIterator.prototype._next = function (callback) {

  if (this._pos >= this.db.container.length() || this._pos < 0)
    return setImmediate(callback)
  var key   = this.db.container.key(this._pos)
    , value

  if (!!this._end && (this._reverse ? key < this._end : key > this._end))
    return setImmediate(callback)

  if (!!this._limit && this._limit > 0 && this._count++ >= this._limit)
    return setImmediate(callback)

  value = this.db.container.getItem(key)
  this._pos += this._reverse ? -1 : 1

  setImmediate(callback.bind(null, undefined, key, value))
}

function ld (location) {
  if (!(this instanceof ld)) return new ld(location)
  AbstractLevelDOWN.call(this, location)
  var wstore = require('./localstorage').localStorage;
  this.container = new wstore(location);
}

util.inherits(ld, AbstractLevelDOWN)

ld.prototype._open = function (options, callback) {
  setImmediate(function () { callback(null, this) }.bind(this))

}

ld.prototype._put = function (key, value, options, callback) {

  var err = checkKeyValue(key, 'key')

  if (err) return callback(err)

  err = checkKeyValue(value, 'value')

  if (err) return callback(err)

  if(typeof value == 'object' && !Buffer.isBuffer(value) && value.buffer == undefined){
        var obj = {};
        obj.storetype = "json";
        obj.data = value;
        value = JSON.stringify(obj)
  }

  this.container.setItem(key, value);
  setImmediate(callback)
}

ld.prototype._get = function (key, options, callback) {

  var err = checkKeyValue(key, 'key')

  if (err) return callback(err)

  if (!isBuffer(key)){
    key = String(key)
  }
  var value = this.container.getItem(key);

  if (value === undefined) {
    // 'NotFound' error, consistent with LevelDOWN API
    return setImmediate(function () { callback(new Error('NotFound: ')) })
  }

  if (options.asBuffer !== false && !Buffer.isBuffer(value)){
    value = new Buffer(String(value))
  }

  if(options.asBuffer === false){
    if(value.indexOf("{\"storetype\":\"json\",\"data\"") > -1){
      var res = JSON.parse(value);
      value = res.data;
    }
  }

  setImmediate(function () {
    callback(null, value)
  })
}

ld.prototype._del = function (key, options, callback) {

  var err = checkKeyValue(key, 'key')

  if (err) return callback(err)
  if (!isBuffer(key)) key = String(key)

  this.container.removeItem(key);
  setImmediate(callback)
}

ld.prototype._batch = function (array, options, callback) {
  var err
    , i = 0
    , key
    , value
  if (Array.isArray(array)) {
    for (; i < array.length; i++) {
      if (array[i]) {
        key = Buffer.isBuffer(array[i].key) ? array[i].key : String(array[i].key)
        err = checkKeyValue(key, 'key')
        if (err) return setImmediate(callback.bind(null, err))
        if (array[i].type === 'del') {
          this._del(array[i].key, options, noop)
        } else if (array[i].type === 'put') {
          value = Buffer.isBuffer(array[i].value) ? array[i].value : String(array[i].value)
          err = checkKeyValue(value, 'value')
          if (err) return setImmediate(callback.bind(null, err))
          this._put(key, value, options, noop)
        }
      }
    }
  }
  setImmediate(callback)
}

ld.prototype._iterator = function (options) {
  return new ldIterator(this, options)
}

ld.destroy = function (name, callback) {
  try {
    Object.keys(localStorage)
      .forEach(function (key) {
        if (key.substring(0, name.length + 1) == (name + "!")) {
          localStorage.removeItem(key)
        }
      })
    callback()
  } catch (e) {
    // fail gracefully if no localStorage
  }
}

function subarray(start, end) {
  return this.slice(start, end)
}

function set_(array, offset) {

  if (arguments.length < 2) offset = 0

  for (var i = 0, n = array.length; i < n; ++i, ++offset)
    this[offset] = array[i] & 0xFF
}

// we need typed arrays
function TypedArray(arg1) {
var result;
  if (typeof arg1 === "number") {
    result = new Array(arg1);

    for (var i = 0; i < arg1; ++i)
        result[i] = 0;
  } else
    result = arg1.slice(0)

    result.subarray = subarray
    result.buffer = result
    result.byteLength = result.length
    result.set = set_

  if (typeof arg1 === "object" && arg1.buffer)
    result.buffer = arg1.buffer

  return result

}

/*
if(!window.Uint8Array){
  window.Uint8Array = TypedArray;
  window.Uint16Array = TypedArray;
  window.Uint32Array = TypedArray;
  window.Int32Array = TypedArray;
}
*/
function isBuffer(buf) {
  return buf instanceof ArrayBuffer
}

function checkKeyValue (obj, type) {
  if (obj === null || obj === undefined)
    return new Error(type + ' cannot be `null` or `undefined`')
  if (obj === null || obj === undefined)
    return new Error(type + ' cannot be `null` or `undefined`')

  if(type === 'key'){

    if(obj instanceof Boolean){
      return new Error(type + ' cannot be `null` or `undefined`')
    }
    if(obj === ''){
      return new Error(type + ' cannot be empty')
    }
/*
    if(isBuffer(obj)) {

      return new Error(type + 'cannot be an empty Buffer')
    }*/
  }
  if(obj.toString().indexOf("[object ArrayBuffer]") == 0){
      if(obj.byteLength == 0 || obj.byteLength == undefined){
        return new Error(type + ' cannot be an empty Buffer')
      }
  }

  /*if(obj.toString().indexOf("[object Uint8Array]") == 0){
      if(obj.byteLength == 0 || obj.byteLength == undefined){
        return new Error(type + ' cannot be an empty ArrayBuffer')
      }
  } */
  if (isBuffer(obj)) {

    if (obj.length === 0)
      return new Error(type + ' cannot be an empty Buffer')
  } else if (String(obj) === '')
    return new Error(type + ' cannot be an empty String')
}

module.exports = ld
<|endoftext|>


<commit_before>#!/usr/bin/env node

var WebSocketServer = require('ws').Server
var freeport = require('freeport')
var request = require('request')
var websocket = require('websocket-stream')
var docker = require('docker-browser-console')
var root = require('root')
var url = require('url')
var send = require('send')
var path = require('path')
var pump = require('pump')
var cors = require('cors')
var net = require('net')
var xtend = require('xtend')
var run = require('docker-run')

var docker_hosts_array=[
    '192.168.0.33:4243',
    '192.168.0.35:4243'
]
var load_flag = 0
module.exports = function(image, opts) {
  if (!opts) opts = {}

  var DOCKER_HOST = opts.docker || (process.env.DOCKER_HOST || '127.0.0.1').replace(/^.+:\/\//, '').replace(/:\d+$/, '').replace(/^\/.+$/, '127.0.0.1')

  var server = root()
  var wss = new WebSocketServer({server:server})
  var containers = {}
  var run_containers = {}
  var search_run_containers = function(res,image_id,cb){
      var container = run_containers[image_id]
      if(container != null) {
          console.log(container)
          if(container.host == null && container.status ==1)
            cb(res,{message:container.status_msg,code:3},null)
          else{
              cb(res,null,container)
          }
      }
      else{
              //create one
              var con = run_containers[image_id] = {
                  image_id:image_id,
                  TTL:180,
                  host:null,
                  port:4470,
                  status:1,
                  status_msg:'pulling image'
              }
              var child = run(image_id, xtend(opts, {
                  tty: false,
                  argv:["/run/docker-run"],
                  volumes:{
                      "/opt/docker-run/":"/run/"
                  },
                  host:docker_hosts_array[load_flag]
              }))

              //child.on('exit', function() {
              //  //pull over
              //
              //})
              child.on('error',function(err){
                  console.log('error in pull')
                  con.status_msg = 'pull is failed,maybe image is not exists!'
              })
              child.on('pbegin',function(){
                  console.log('begin pull image')
                  cb(res,{message:"pulling image"+image_id},null)
              })

              child.on('pend',function(){
                  con.status_msg = 'pull is successful!'
                  con.status = 2;
              })
              child.on('json',function(json){
                  var con = run_containers[image_id]
                  con.host = json.NetworkSettings.IPAddress
                  cb(res,null,con)
              })

              child.on('error', function(err) {
                  //create error
                  cb(res,err,null)
              })

          }
      }

  wss.on('connection', function(connection) {
    var req = connection.upgradeReq
    var url = req.url.slice(1)
    var persist = opts.persist && !!url
    var id = url || Math.random().toString(36).slice(2)
    //TODO add user id
    //get userid and imagename
    var params = url.split('/')
      if(params.length<3){
          //close ws
          console.log("error params");
          connection.close();
      }
      //get userid and check user
      var user = params[1]
      //mock check user
      if(false){
          //close ws
          console.log("invalid user!");
          connection.close();
      }
      id = user

      //get image and check it
      var image = params[2]
      if(false){
          //close ws
          console.log("invalid image!");
          connection.close();
      }
      console.log(req.url)
      var stream = websocket(connection)
      //check is exists
    var container = containers.hasOwnProperty(id) && containers[id]
    if (container){//request('http://'+DOCKER_HOST+':'+container.ports.http)
        if(container.image=== image){
            console.log("already open one!");
            connection.close();
            return
        }else{
            console.log("kill one!");
            container.connection.close();
        }
    }
        var startProxy = function(httpPort, cb) {
            if (!opts.offline) return cb(null, id+'.c.'+req.headers.host)

            var proxy = net.createServer(function(socket) {
                //TODO cancel
                pump(socket, net.connect(httpPort, DOCKER_HOST), socket)
            })

            proxy.once('error', cb)
            proxy.listen(0, function() {
                var port = proxy.address().port
                cb(null, req.headers.host.split(':')[0]+':'+port, proxy)
            })
        }

        freeport(function(err, filesPort) {
            if (err) return connection.destroy()
            freeport(function(err, httpPort) {
                if (err) return connection.destroy()
                startProxy(httpPort, function(err, subdomain, proxy) {
                    if (err) {
                        console.log("create start proxy error");
                        return connection.destroy();
                    }

                    var container = containers[id] = {
                        id: id,
                        image: image,
                        docker_run: null,
                        connection:connection,
                        host: 'http://'+subdomain,
                        ports: {http:httpPort, fs:filesPort}
                    }

                    server.emit('spawn', container)

                    var ports = {}

                    ports[httpPort] = 80
                    ports[filesPort] = 8441

                    load_flag =((load_flag+1)%2)
                    var dopts = {
                        tty: opts.tty === undefined ? true : opts.tty,
                        env: {
                            CONTAINER_ID: container.id,
                            HOST: container.host,
                            PORT: 80,
                            CONTAINER_OBJ: container
                        },
                        ports: ports,
                        host: docker_hosts_array[load_flag],
                        volumes: opts.volumes || {}
                    }

                    if (persist) dopts.volumes['/tmp/'+id] = '/root'
                    if (opts.trusted) dopts.volumes['/var/run/docker.sock'] = '/var/run/docker.sock'

                    //TODO replace image with you wanted
                    pump(stream, docker(image, dopts), stream, function(err) {
                        console.log('error in create docker');
                        if (proxy) proxy.close()
                        server.emit('kill', container)
                        delete containers[id]
                    })
                })
            })
        })
  })

  server.all(cors())

  server.all(function(req, res, next) {
    var host = req.headers.host || ''
    var i = host.indexOf('.c.')

    if (i > -1) {
      var id = host.slice(0, i)
      var container = containers.hasOwnProperty(id) && containers[id]
      if (container) return pump(req, request('http://'+DOCKER_HOST+':'+container.ports.http+req.url), res)
      return res.error(404, 'Could not find container')
    }

    next()
  })

  server.get('/-/*', function(req, res) {
    send(req, req.params.glob, {root:path.join(__dirname, 'web')}).pipe(res)
  })

  //server.get('/containers/{id}', function(req, res) {
  //  var id = req.params.id
  //  var container = containers.hasOwnProperty(id) && containers[id]
  //  if (!container) return res.error(404, 'Could not find container')
  //  res.send(container)
  //})
  //
  //server.all('/http/{id}/*', function(req, res) {
  //  var id = req.params.id
  //  var url = req.url.slice(('/http/'+id).length)
  //  var container = containers.hasOwnProperty(id) && containers[id]
  //  if (!container) return res.error(404, 'Could not find container')
  //  pump(req, request('http://'+DOCKER_HOST+':'+container.ports.http+url), res)
  //})

  //server.all('/files/{id}/*', function(req, res) {
  //  var id = req.params.id
  //  var url = req.url.slice(('/files/'+id).length)
  //  var container = containers.hasOwnProperty(id) && containers[id]
  //  if (!container) return res.error(404, 'Could not find container')
  //  pump(req, request('http://'+DOCKER_HOST+':'+container.ports.fs+url), res)
  //})

  server.all(function(req, res, next) {
    if (!opts.offline) return next()
    var id = req.connection.address().address
    //var container = containers.hasOwnProperty(id) && containers[id]
    //if (container) return pump(req, request('http://'+DOCKER_HOST+':'+container.ports.http+req.url), res)
    next()
  })

    //TODO check container is exist
  server.post('/runner/{imagename}',function(req,res,next){
        var image = req.params.imagename
        console.log("runner image is :"+image)
      //find a image
      search_run_containers(res,image,function(res,err,con){
          if(res.finished){
              return
          }else{
              if(err){
                  res.send(err)
              }else{
                  return pump(req, request('http://'+con.host+':'+con.port+'/api/coderunner'), res)
              }
          }

      })
    })
  server.get('/bundle.js', '/-/bundle.js')
  server.get('/index.html', '/-/index.html')
  server.get('/user/{userid}/{imagename}', '/-/index.html')

  return server
}
<commit_msg>fix bug in first start container<commit_after>#!/usr/bin/env node

var WebSocketServer = require('ws').Server
var freeport = require('freeport')
var request = require('request')
var websocket = require('websocket-stream')
var docker = require('docker-browser-console')
var root = require('root')
var url = require('url')
var send = require('send')
var path = require('path')
var pump = require('pump')
var cors = require('cors')
var net = require('net')
var xtend = require('xtend')
var run = require('docker-run')

var docker_hosts_array=[
    //'192.168.0.33:4243',
    //'192.168.0.35:4243'
    '127.0.0.1:4243',
    '127.0.0.1:4243'
]
var load_flag = 0
module.exports = function(image, opts) {
  if (!opts) opts = {}

  var DOCKER_HOST = opts.docker || (process.env.DOCKER_HOST || '127.0.0.1').replace(/^.+:\/\//, '').replace(/:\d+$/, '').replace(/^\/.+$/, '127.0.0.1')

  var server = root()
  var wss = new WebSocketServer({server:server})
  var containers = {}
  var run_containers = {}
  var search_run_containers = function(res,image_id,cb){
      var container = run_containers[image_id]
      if(container != null) {
          console.log(container)
          if(container.host == null && container.status ==1)
            cb(res,{message:container.status_msg,code:container.status},null)
          else{
              cb(res,null,container)
          }
      }
      else{
              //create one
              var con = run_containers[image_id] = {
                  image_id:image_id,
                  TTL:180,
                  host:null,
                  port:4470,
                  status:1,
                  status_msg:'pulling image'
              }
              var child = run(image_id, xtend(opts, {
                  tty: false,
                  argv:["/run/docker-run"],
                  volumes:{
                      "/opt/docker-run/":"/run/"
                  },
                  host:docker_hosts_array[load_flag]
              }))

              //child.on('exit', function() {
              //  //pull over
              //
              //})
              child.on('error',function(err){
                  console.log('error in pull')
                  con.status_msg = 'pull is failed,maybe image is not exists!'
              })
              child.on('pbegin',function(){
                  console.log('begin pull image')
                  cb(res,{message:"pulling image"+image_id},null)
              })

              child.on('pend',function(){
                  con.status_msg = 'pull is successful!'
                  con.status = 2;
              })
              child.on('json',function(json){
                  var con = run_containers[image_id]
                  con.host = json.NetworkSettings.IPAddress
                  con.status_msg = 'start is successful!'
                  con.status = 2;
                  cb(res,{message:con.status_msg,code:con.status},null)
              })

              child.on('error', function(err) {
                  //create error
                  cb(res,err,null)
              })

          }
      }

  wss.on('connection', function(connection) {
    var req = connection.upgradeReq
    var url = req.url.slice(1)
    var persist = opts.persist && !!url
    var id = url || Math.random().toString(36).slice(2)
    //TODO add user id
    //get userid and imagename
    var params = url.split('/')
      if(params.length<3){
          //close ws
          console.log("error params");
          connection.close();
      }
      //get userid and check user
      var user = params[1]
      //mock check user
      if(false){
          //close ws
          console.log("invalid user!");
          connection.close();
      }
      id = user

      //get image and check it
      var image = params[2]
      if(false){
          //close ws
          console.log("invalid image!");
          connection.close();
      }
      console.log(req.url)
      var stream = websocket(connection)
      //check is exists
    var container = containers.hasOwnProperty(id) && containers[id]
    if (container){//request('http://'+DOCKER_HOST+':'+container.ports.http)
        if(container.image=== image){
            console.log("already open one!");
            connection.close();
            return
        }else{
            console.log("kill one!");
            container.connection.close();
        }
    }
        var startProxy = function(httpPort, cb) {
            if (!opts.offline) return cb(null, id+'.c.'+req.headers.host)

            var proxy = net.createServer(function(socket) {
                //TODO cancel
                pump(socket, net.connect(httpPort, DOCKER_HOST), socket)
            })

            proxy.once('error', cb)
            proxy.listen(0, function() {
                var port = proxy.address().port
                cb(null, req.headers.host.split(':')[0]+':'+port, proxy)
            })
        }

        freeport(function(err, filesPort) {
            if (err) return connection.destroy()
            freeport(function(err, httpPort) {
                if (err) return connection.destroy()
                startProxy(httpPort, function(err, subdomain, proxy) {
                    if (err) {
                        console.log("create start proxy error");
                        return connection.destroy();
                    }

                    var container = containers[id] = {
                        id: id,
                        image: image,
                        docker_run: null,
                        connection:connection,
                        host: 'http://'+subdomain,
                        ports: {http:httpPort, fs:filesPort}
                    }

                    server.emit('spawn', container)

                    var ports = {}

                    ports[httpPort] = 80
                    ports[filesPort] = 8441

                    load_flag =((load_flag+1)%2)
                    var dopts = {
                        tty: opts.tty === undefined ? true : opts.tty,
                        env: {
                            CONTAINER_ID: container.id,
                            HOST: container.host,
                            PORT: 80,
                            CONTAINER_OBJ: container
                        },
                        ports: ports,
                        host: docker_hosts_array[load_flag],
                        volumes: opts.volumes || {}
                    }

                    if (persist) dopts.volumes['/tmp/'+id] = '/root'
                    if (opts.trusted) dopts.volumes['/var/run/docker.sock'] = '/var/run/docker.sock'

                    //TODO replace image with you wanted
                    pump(stream, docker(image, dopts), stream, function(err) {
                        console.log('error in create docker');
                        if (proxy) proxy.close()
                        server.emit('kill', container)
                        delete containers[id]
                    })
                })
            })
        })
  })

  server.all(cors())

  server.all(function(req, res, next) {
    var host = req.headers.host || ''
    var i = host.indexOf('.c.')

    if (i > -1) {
      var id = host.slice(0, i)
      var container = containers.hasOwnProperty(id) && containers[id]
      if (container) return pump(req, request('http://'+DOCKER_HOST+':'+container.ports.http+req.url), res)
      return res.error(404, 'Could not find container')
    }

    next()
  })

  server.get('/-/*', function(req, res) {
    send(req, req.params.glob, {root:path.join(__dirname, 'web')}).pipe(res)
  })

  //server.get('/containers/{id}', function(req, res) {
  //  var id = req.params.id
  //  var container = containers.hasOwnProperty(id) && containers[id]
  //  if (!container) return res.error(404, 'Could not find container')
  //  res.send(container)
  //})
  //
  //server.all('/http/{id}/*', function(req, res) {
  //  var id = req.params.id
  //  var url = req.url.slice(('/http/'+id).length)
  //  var container = containers.hasOwnProperty(id) && containers[id]
  //  if (!container) return res.error(404, 'Could not find container')
  //  pump(req, request('http://'+DOCKER_HOST+':'+container.ports.http+url), res)
  //})

  //server.all('/files/{id}/*', function(req, res) {
  //  var id = req.params.id
  //  var url = req.url.slice(('/files/'+id).length)
  //  var container = containers.hasOwnProperty(id) && containers[id]
  //  if (!container) return res.error(404, 'Could not find container')
  //  pump(req, request('http://'+DOCKER_HOST+':'+container.ports.fs+url), res)
  //})

  server.all(function(req, res, next) {
    if (!opts.offline) return next()
    var id = req.connection.address().address
    //var container = containers.hasOwnProperty(id) && containers[id]
    //if (container) return pump(req, request('http://'+DOCKER_HOST+':'+container.ports.http+req.url), res)
    next()
  })

    //TODO check container is exist
  server.post('/runner/{imagename}',function(req,res,next){
        var image = req.params.imagename
        console.log("runner image is :"+image)
      //find a image
      search_run_containers(res,image,function(res,err,con){
          if(res.finished){
              return
          }else{
              if(err){
                  res.send(err)
              }else{
                  return pump(req, request('http://'+con.host+':'+con.port+'/api/coderunner'), res)
              }
          }

      })
    })
  server.get('/bundle.js', '/-/bundle.js')
  server.get('/index.html', '/-/index.html')
  server.get('/user/{userid}/{imagename}', '/-/index.html')

  return server
}
<|endoftext|>


<commit_before>'use strict';

var npm = require('npm');
var path = require('path');
var which = require('which');
var findup = require('findup');
var type = require('utils-type');

exports = module.exports = whech;
exports.sync = whechSync;
exports.packageFields = ['version'];

var argv = process.argv;

function whech(env_, cb){
  var env = check(env_);
  env.cwd = env.cwd || process.cwd();

  which(env.name, function(err, whichName){
    env.which = err || whichName;
    env.runFromBin = env.which === argv[1];

    npm.load(function(cli, npm_){
      env.mainDir = npm_.dir;
      env.globalDir = npm_.globalDir;

      var cliPackage; env.cliPackage = { };
      var modulePackage; env.modulePackage = { };
      var pack = path.join(env.name, 'package');

      try {  modulePackage = require(path.join(env.mainDir, pack));  }
        catch(err){  env.modulePackage = err;  }
      try {  cliPackage = require(path.join(env.globalDir, pack));  }
        catch(err){  env.cliPackage = err;  }

      copyFields(env, modulePackage, cliPackage);

      if( env.runFromBin ){
        env.extension = env.extension || '.js';
        env.configFile = env.configFile || env.name + 'file';
        if( !path.extname(env.configFile) ){  env.configFile += env.extension;  }
        env.configFile = findup(env.configFile, { cwd : env.cwd });

        findup(process.cwd(), env.configFile, function(err, configDir){
          env.configFile = err || path.join(configDir, env.configFile);
          cb(err, env);
        });
      } else {
        env.configFile = argv[1];
        if( env.configFile !== path.basename(env.configFile) ){
          env.mainDir = path.join(env.cwd, path.dirname(env.configFile));
        }
        cb(err, env);
      }
    });
  });
}

function whechSync(env_){
  var env = check(env_);
  env.cwd = env.cwd || process.cwd();

  var which = require('which');
  var findup = require('findup-sync');

  env.which = which.sync(env.name);
  env.runFromBin = env.which === argv[1];

  env.mainDir = path.join(env.cwd, 'node_modules');
  env.globalDir = null;
  // TODO: find a non hacky way to do this
  try{
    env.globalDir = path.resolve(env.which, '..', '..', 'lib', 'node_modules');
  } catch(err){
      env.which  = new Error('not found');
      env.globalDir = new Error('counld not find globalDir');
    }

  var cliPackage; env.cliPackage = { };
  var modulePackage; env.modulePackage = { };
  var pack = path.join(env.name, 'package');
  try {  modulePackage = require(path.join(env.mainDir, pack));  }
    catch(err){  env.modulePackage = err;  }
  try {  cliPackage = require(path.join(env.globalDir, pack));  }
    catch(err){  env.cliPackage = err;  }

  copyFields(env, modulePackage, cliPackage);

  if( env.runFromBin ){
    env.extension = env.extension || '.js';
    env.configFile = env.configFile || env.name + 'file';
    if( !path.extname(env.configFile) ){  env.configFile += env.extension;  }
    env.configFile = findup(env.configFile, { cwd : env.cwd });
  } else {
    env.configFile = argv[1];
    if( env.configFile !== path.basename(env.configFile) ){
      env.mainDir = path.join(env.cwd, path.dirname(env.configFile));
    }
  }

  return env;
}

function check(env){
  var envIs = type(env);
  var typeError = !envIs.match(/string|plainObject/);
  if( typeError ){  throw new TypeError('`env` should  be string or object');  }
  env = envIs.plainObject || { name : envIs.string };
  if( !env.name ){  throw new Error('give a name to search');  }
  env.name = env.name.toLowerCase();
  return env;
}

function copyFields(env, modulePackage, cliPackage){
  if( !exports.packageFields.length ){ return ; }
  exports.packageFields.forEach(function(field){
    if( cliPackage && cliPackage[field] ){
      env.cliPackage[field] = cliPackage[field];
    }
    if( modulePackage && modulePackage[field] ){
      env.modulePackage[field] = modulePackage[field];
    }
  });
}
<commit_msg>fix that thing of runing from bin<commit_after>'use strict';

var npm = require('npm');
var path = require('path');
var which = require('which');
var findup = require('findup');
var type = require('utils-type');

exports = module.exports = whech;
exports.sync = whechSync;
exports.packageFields = ['version'];

var argv = process.argv;
var main = process.mainModule;

function whech(env_, cb){
  var env = check(env_);
  env.cwd = env.cwd || process.cwd();

  which(env.name, function(err, whichName){
    env.which = err || whichName;
    env.runFromBin = env.which === argv[1];

    npm.load(function(cli, npm_){
      env.mainDir = npm_.dir;
      env.globalDir = npm_.globalDir;

      var cliPackage; env.cliPackage = { };
      var modulePackage; env.modulePackage = { };
      var pack = path.join(env.name, 'package');

      try {  modulePackage = require(path.join(env.mainDir, pack));  }
        catch(err){  env.modulePackage = err;  }
      try {  cliPackage = require(path.join(env.globalDir, pack));  }
        catch(err){  env.cliPackage = err;  }

      copyFields(env, modulePackage, cliPackage);

      env.extension = env.extension || '.js';
      env.configFile = env.configFile || env.name + 'file';
      if( !path.extname(env.configFile) ){  env.configFile += env.extension;  }
      if( !env.runFromBin ){
        env.cwd = path.resolve(env.cwd, path.dirname(argv[1]));
        env.configFile = path.basename(argv[1]);
      }

      findup(process.cwd(), env.configFile, function(err, configDir){
        env.configFile = err || path.join(configDir, env.configFile);
        cb(err, env);
      });
    });
  });
}

function whechSync(env_){
  var env = check(env_);
  env.cwd = env.cwd || process.cwd();

  var which = require('which');
  var findup = require('findup-sync');

  env.which = which.sync(env.name);
  env.runFromBin = env.which === argv[1];

  env.mainDir = path.join(env.cwd, 'node_modules');
  env.globalDir = null;
  // TODO: find a non hacky way to do this
  try{
    env.globalDir = path.resolve(env.which, '..', '..', 'lib', 'node_modules');
  } catch(err){
      env.which  = new Error('not found');
      env.globalDir = new Error('counld not find globalDir');
    }

  var cliPackage; env.cliPackage = { };
  var modulePackage; env.modulePackage = { };
  var pack = path.join(env.name, 'package');
  try {  modulePackage = require(path.join(env.mainDir, pack));  }
    catch(err){  env.modulePackage = err;  }
  try {  cliPackage = require(path.join(env.globalDir, pack));  }
    catch(err){  env.cliPackage = err;  }

  copyFields(env, modulePackage, cliPackage);

  env.extension = env.extension || '.js';
  env.configFile = env.configFile || env.name + 'file';
  if( !path.extname(env.configFile) ){  env.configFile += env.extension;  }
  if( !env.runFromBin ){
    env.cwd = path.resolve(env.cwd, path.dirname(argv[1]));
    env.configFile = path.basename(argv[1]);
  }
  env.configFile = findup(env.configFile, { cwd : env.cwd });

  return env;
}

function check(env){
  var envIs = type(env);
  var typeError = !envIs.match(/string|plainObject/);
  if( typeError ){  throw new TypeError('`env` should  be string or object');  }
  env = envIs.plainObject || { name : envIs.string };
  if( !env.name ){  throw new Error('give a name to search');  }
  env.name = env.name.toLowerCase();
  return env;
}

function copyFields(env, modulePackage, cliPackage){
  if( !exports.packageFields.length ){ return ; }
  exports.packageFields.forEach(function(field){
    if( cliPackage && cliPackage[field] ){
      env.cliPackage[field] = cliPackage[field];
    }
    if( modulePackage && modulePackage[field] ){
      env.modulePackage[field] = modulePackage[field];
    }
  });
}
<|endoftext|>


<commit_before>var through = require('through2')
var protocol = require('dat-replication-protocol')
var pump = require('pump')

var noop = function() {}

module.exports = function(db, store) {
  var that = {}

  that.receive = function() {
    var blobs = 0
    var next = noop

    var onblob = function(stream) {
      blobs++
      pump(stream, store.createWriteStream(), function() {
        if (--blobs) return
        next()
        next = noop
      })
    }

    var ondrain = function(cb) {
      if (!blobs) cb()
      else next = cb
    }

    var readBlobs = function(data, enc, cb) {
      ondrain(function() {
        cb(null, data)
      })
    }

    var onchanges = function(stream) {
      pump(stream, through.obj(readBlobs), db.createChangesWriteStream({valueEncoding:'binary'}))
    }

    return protocol(function(type, stream) {
      if (type === protocol.CHANGES) onchanges(stream)
      if (type === protocol.BLOB) onblob(stream)
    })
  }

  that.send = function(opts) {
    if (!opts) opts = {}

    var p = protocol()

    var writeBlobs = function(data, enc, cb) {
      if (opts.blobs === false) return cb(null, data)
      if (data.subset !== 'blobs') return cb(null, data)

      var metadata = JSON.parse(data.value.toString())
      if (!metadata.from) metadata = metadata.slice(-1)

      var loop = function() {
        if (!metadata.length) return cb(null, data)

        var next = metadata.shift()
        var bl = store.createReadStream(next.hash)

        pump(bl, p.createBlobStream(next.size)).on('finish', loop)
      }

      loop()
    }

    pump(
      db.createChangesReadStream({since:opts.since, valueEncoding:'binary', data:true}),
      through.obj(writeBlobs),
      p.createChangesStream()
    )

    return p
  }

  return that
}<commit_msg>fix version check<commit_after>var through = require('through2')
var protocol = require('dat-replication-protocol')
var pump = require('pump')

var noop = function() {}

module.exports = function(db, store) {
  var that = {}

  that.receive = function() {
    var blobs = 0
    var next = noop

    var onblob = function(stream) {
      blobs++
      pump(stream, store.createWriteStream(), function() {
        if (--blobs) return
        next()
        next = noop
      })
    }

    var ondrain = function(cb) {
      if (!blobs) cb()
      else next = cb
    }

    var readBlobs = function(data, enc, cb) {
      ondrain(function() {
        cb(null, data)
      })
    }

    var onchanges = function(stream) {
      pump(stream, through.obj(readBlobs), db.createChangesWriteStream({valueEncoding:'binary'}))
    }

    return protocol(function(type, stream) {
      if (type === protocol.CHANGES) onchanges(stream)
      if (type === protocol.BLOB) onblob(stream)
    })
  }

  that.send = function(opts) {
    if (!opts) opts = {}

    var p = protocol()

    var writeBlobs = function(data, enc, cb) {
      if (opts.blobs === false) return cb(null, data)
      if (data.subset !== 'blobs') return cb(null, data)

      var metadata = JSON.parse(data.value.toString())
      if (!data.from) metadata = metadata.slice(-1)

      var loop = function() {
        if (!metadata.length) return cb(null, data)

        var next = metadata.shift()
        var bl = store.createReadStream(next.hash)

        pump(bl, p.createBlobStream(next.size)).on('finish', loop)
      }

      loop()
    }

    pump(
      db.createChangesReadStream({since:opts.since, valueEncoding:'binary', data:true}),
      through.obj(writeBlobs),
      p.createChangesStream()
    )

    return p
  }

  return that
}<|endoftext|>


<commit_before>module.exports = factorial;

function factorial(v) {
  return v === 0 ? 1 : v * factorial(v - 1);
}<commit_msg>Made it throw if given negative number<commit_after>module.exports = function(n) {
  if (n < 0) {
    throw new RangeError("Cannot take factorial out of a negative number.");
  } else {
    return factorial(n);
  }
}

function factorial(n) {
  return n === 0 ? 1 : n * factorial(n - 1);
}
<|endoftext|>


<commit_before>var jsdom = require('jsdom').jsdom,
	ngPath;

// create global vars angular assumes are available
global.document = jsdom('');
global.window = document.parentWindow;
global.navigator = {};

function compile() {
	// read angular source into memory
	var src = require('fs').readFileSync(ngPath, 'utf8');
	// replace implicit reference
	src = src.replace('angular.element(document)', 'window.angular.element(document)');
	// run it!
	eval(src);
}

// share the love
module.exports = function (path, overwrite) {
	if (global.window.angular && !overwrite) return global.window.angular;

	ngPath = path ? require('path').resolve(process.cwd(), path) : require.resolve('./lib/angular.min.js');
	compile();

	return global.window.angular;
}
<commit_msg>Update source replacement<commit_after>var jsdom = require('jsdom').jsdom,
	ngPath;

// create global vars angular assumes are available
global.document = jsdom('');
global.window = document.parentWindow;
global.navigator = {};

function compile() {
	// read angular source into memory
	var src = require('fs').readFileSync(ngPath, 'utf8');
	// replace implicit reference
	src = src.split('angular.$$csp()').join('window.angular.$$csp()');
	src = src.split('angular.element(document)').join('window.angular.element(document)');
	// run it!
	eval(src);
}

// share the love
module.exports = function (path, overwrite) {
	if (global.window.angular && !overwrite) return global.window.angular;

	ngPath = path ? require('path').resolve(process.cwd(), path) : require.resolve('./lib/angular.min.js');
	compile();

	return global.window.angular;
}
<|endoftext|>


<commit_before>var rd = require('rd');
var path = require('path');
var strip = require('strip-path');
var Router = require('koa-router');
var mount = require('koa-mount');
var compose = require('koa-compose');
var fs = require('fs');

var dirname = require('app-root-path').toString();
var used = false;

/**
 * add bootstrap
 * @param options {optional}
 * @returns {Function|*|exports}
 */
var bootstrap = function (options) {
  if (used) return function*(next) {
    yield next
  };
  used = true;
  var files = [];
  var routers = [];
  options = options || {};
  var directory = options.directory || 'controllers';
  directory = path.join(dirname, directory);
  if (!fs.existsSync(directory)) {
    var err = 'directory ' + directory + " doesn't exist";
    throw new Error(err);
  }
  rd.eachFileFilterSync(directory, /\.js$/, function (file) {
    var dir = path.dirname(file);
    var base = path.basename(file);
    files.push({dirname: dir, basename: base});
  });
  files.forEach(function (file) {
    if (file && file.basename != 'index.js') {
      file.basename = file.basename.substring(0, file.basename.length - 3)
      file.dirname = path.join(file.dirname, file.basename);
    }
    var route = '/' + strip(file.dirname, directory);
    var router = new Router();
    //require(file.dirname)(router);
    addRouter(file.dirname, router);
    routers.push(mount(route, router.middleware()));
  });
  this.larkBootstrap = true;
  return compose(routers);
};

function addRouter(dirname, router) {
  var controller = require(dirname);
  if ('function' === typeof controller) {
    if ('GeneratorFunction' === controller.constructor.name) {
      return router.get('/', controller);
    }
    else {
      return controller(router);
    }
  }
  else if ('object' === typeof controller) {
    for (var method in controller) {
      method = method.toLowerCase();
      if ('function' !== typeof router[method]) {
        throw new Error("Invalid method " + method + " defined in controller");
      }
      var paths = controller[method];
      for (var routePath in paths) {
        var handler = paths[routePath];
        if ('function' !== typeof handler || 'GeneratorFunction' === controller.constructor.name) {
          throw new Error('Router handler must be a generator function !');
        }
        router[method](routePath, handler);
      }
    }
  }
}

module.exports = bootstrap;

<commit_msg>fix code<commit_after>var rd = require('rd');
var path = require('path');
var strip = require('strip-path');
var Router = require('koa-router');
var mount = require('koa-mount');
var compose = require('koa-compose');
var fs = require('fs');

var dirname = require('app-root-path').toString();
var used = false;

/**
 * add bootstrap
 * @param options {optional}
 * @returns {Function|*|exports}
 */
var bootstrap = function (options) {
  if (used) return function*(next) {
    yield next
  };
  used = true;
  var files = [];
  var routers = [];
  options = options || {};
  var directory = options.directory || 'controllers';
  directory = path.join(dirname, directory);
  if (!fs.existsSync(directory)) {
    var err = 'directory ' + directory + " doesn't exist";
    throw new Error(err);
  }
  rd.eachFileFilterSync(directory, /\.js$/, function (file) {
    var dir = path.dirname(file);
    var base = path.basename(file);
    files.push({dirname: dir, basename: base});
  });
  files.forEach(function (file) {
    if (file && file.basename != 'index.js') {
      file.basename = file.basename.substring(0, file.basename.length - 3)
      file.dirname = path.join(file.dirname, file.basename);
    }
    if (file.basename[0] === '.') {
      return;
    }
    var route = '/' + strip(file.dirname, directory);
    var router = new Router();
    //require(file.dirname)(router);
    addRouter(file.dirname, router);
    routers.push(mount(route, router.middleware()));
  });
  this.larkBootstrap = true;
  return compose(routers);
};

function addRouter(dirname, router) {
  var controller = require(dirname);
  if (!controller) {
    throw new Error('Controller is empty in ' + dirname + ',' + router);
  }
  if ('object' !== typeof controller) {
    return addRoutePath(router, controller);
  }
  if (Array.isArray(controller)) {
    throw new Error('Controller can NOT be an array');
  }
  for (var method in controller) {
    method = method.toLowerCase();
    if (-1 === router.methods.indexOf(method.toUpperCase())) {
      throw new Error('Invalid method : ' + method);
    }
    var paths = controller[method];
    for (var routePath in paths) {
      var handler = paths[routePath];
      addRoutePath(router, method, routePath, handler);
    }
  }
  return;
}

function addRoutePath (router, _method, _routePath, _handler) {
  handler = _handler || _routePath || _method;
  routePath = !!_handler ? _routePath : '/';
  method  = !!_routePath ? _method.toLowerCase() : 'get';

  if ('function' === typeof handler && 'GeneratorFunction' === handler.constructor.name) {
    if (-1 === router.methods.indexOf(method.toUpperCase())) {
      throw new Error('Invalid method : ' + method);
    }
    return router[method](routePath, handler);
  }
  if ('function' === typeof handler) {
    return handler(router);
  }
  throw new Error((typeof handler) + ' can not be set as a router');
}

module.exports = bootstrap;

<|endoftext|>


<commit_before>'use strict'

const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const xhub = require('express-x-hub');
const app = express();

const PORT = process.env.PORT || 8080;

app.use(xhub({algorithm: 'sha1', secret: process.env.APP_SECRET }));
app.use(bodyParser.json());

var received_updates = [];

app.get('/', function(req, res) {
  res.send(JSON.stringify(received_updates, null, 2));
});

app.get('/webhooks', function(req, res) {
  if (req.param('hub.mode') != 'subscribe'
      || req.param('hub.verify_token') != process.env.VERIFY_TOKEN) {
    res.sendStatus(401);
    return;
  }

  res.send(req.param('hub.challenge'));
});

app.post('/webhooks', function(req, res) {
  if (!req.isXHubValid()) {
    console.log('Received webhooks update with invalid X-Hub-Signature');
    res.sendStatus(401);
    return;
  }
  console.log(JSON.stringify(req.body, null, 2));
  received_updates.unshift(req.body);
  res.sendStatus(200);
});

app.listen(PORT, function() {
  console.log('Starting webhooks server listening on port:' + PORT);
});<commit_msg>prettyprint<commit_after>'use strict'

const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const xhub = require('express-x-hub');
const app = express();

const PORT = process.env.PORT || 8080;

app.use(xhub({algorithm: 'sha1', secret: process.env.APP_SECRET }));
app.use(bodyParser.json());

var received_updates = [];

app.get('/', function(req, res) {
  res.send(
    '<pre>' + JSON.stringify(received_updates, null, 2) + '</pre>',
  );
});

app.get('/webhooks', function(req, res) {
  if (req.param('hub.mode') != 'subscribe'
      || req.param('hub.verify_token') != process.env.VERIFY_TOKEN) {
    res.sendStatus(401);
    return;
  }

  res.send(req.param('hub.challenge'));
});

app.post('/webhooks', function(req, res) {
  if (!req.isXHubValid()) {
    console.log('Received webhooks update with invalid X-Hub-Signature');
    res.sendStatus(401);
    return;
  }
  console.log(JSON.stringify(req.body, null, 2));
  received_updates.unshift(req.body);
  res.sendStatus(200);
});

app.listen(PORT, function() {
  console.log('Starting webhooks server listening on port:' + PORT);
});<|endoftext|>


<commit_before>#! /usr/bin/env node
const process = require("process");
const fs = require("fs");
const path = require("path");

const findUp = require("find-up");
const semver = require("semver");
const Config = require("truffle-config");
const Resolver = require("truffle-resolver");
const tsort = require("tsort");
const SolidityParser = require("solidity-parser");

const PRAGAMA_SOLIDITY_VERSION_REGEX = /^\s*pragma\ssolidity\s+(.*?)\s*;/;
const SUPPORTED_VERSION_DECLARATION_REGEX = /^\^?\d+(\.\d+){1,2}$/;
const IMPORT_SOLIDITY_REGEX = /^.*import.*$/mg;

function unique(array) {
  return [...new Set(array)];
}

function resolve(importPath) {
  const config = Config.default();
  const resolver = new Resolver(config);

  return new Promise((resolve, reject) => {
    resolver.resolve(importPath, (err, fileContents, filePath) => {
      if (err) {
        reject(err);
        return;
      }

      resolve({ fileContents, filePath });
    });
  });
}

function getDirPath(filePath) {
  return filePath.substring(0, filePath.lastIndexOf(path.sep));
}

function getDependencies(filePath, fileContents) {
  const dependencies = [];

  let imports;
  try {
    imports = SolidityParser.parse(fileContents, "imports");
  } catch (error) {
    throw new Error(
      "Could not parse " + filePath + " for extracting its imports."
    );
  }

  for (let dependency of imports) {
    if (dependency.startsWith("./") || dependency.startsWith("../")) {
      dependency = getDirPath(filePath) + path.sep + dependency;
      dependency = path.normalize(dependency);
    }

    dependencies.push(dependency);
  }

  return dependencies;
}

async function dependenciesDfs(graph, visitedFiles, filePath) {
  visitedFiles.push(filePath);

  const resolved = await resolve(filePath);

  const dependencies = getDependencies(
    resolved.filePath,
    resolved.fileContents
  );

  for (let dependency of dependencies) {
    graph.add(dependency, filePath);

    const resolvedDependency = await resolve(dependency);

    if (!visitedFiles.includes(dependency)) {
      await dependenciesDfs(graph, visitedFiles, dependency);
    }
  }
}

async function getSortedFilePaths(entryPoints) {
  const graph = tsort();
  const visitedFiles = [];

  for (const entryPoint of entryPoints) {
    await dependenciesDfs(graph, visitedFiles, entryPoint);
  }

  const topologicalSortedFiles = graph.sort();

  // If an enrty has no dependency it won't be included in the graph, so we
  // add them and then dedup the array
  const withEntries = topologicalSortedFiles.concat(entryPoints);

  const files = unique(withEntries);

  return files;
}

async function printFileWithoutPragma(filePath) {
  const resolved = await resolve(filePath);
  const output = resolved.fileContents.replace(
    PRAGAMA_SOLIDITY_VERSION_REGEX,
    ""
  ).replace(IMPORT_SOLIDITY_REGEX,"");

  console.log(output.trim());
}

async function getFileCompilerVersionDeclaration(filePath) {
  const resolved = await resolve(filePath);

  const matched = resolved.fileContents.match(PRAGAMA_SOLIDITY_VERSION_REGEX);

  if (matched === null) {
    return undefined;
  }

  const version = matched[1];

  if (!SUPPORTED_VERSION_DECLARATION_REGEX.test(version)) {
    throw new Error(
      "Unsupported compiler version declaration in  " +
        filePath +
        ": " +
        version +
        ". Only pinned or ^ versions are supported."
    );
  }

  return version;
}

async function normalizeCompilerVersionDeclarations(files) {
  let pinnedVersion;
  let pinnedVersionFile;

  let maxCaretVersion;
  let maxCaretVersionFile;

  for (const file of files) {
    const version = await getFileCompilerVersionDeclaration(file);

    if (version === undefined) {
      continue;
    }

    if (version.startsWith("^")) {
      if (maxCaretVersion == undefined) {
        maxCaretVersion = version;
        maxCaretVersionFile = file;
      } else {
        if (semver.gt(version.substr(1), maxCaretVersion.substr(1))) {
          maxCaretVersion = version;
          maxCaretVersionFile = file;
        }
      }
    } else {
      if (pinnedVersion === undefined) {
        pinnedVersion = version;
        pinnedVersionFile = file;
      } else if (pinnedVersion !== version) {
        throw new Error(
          "Differernt pinned compiler versions in " +
            pinnedVersionFile +
            " and " +
            file
        );
      }
    }

    if (maxCaretVersion !== undefined && pinnedVersion !== undefined) {
      if (!semver.satisfies(pinnedVersion, maxCaretVersion)) {
        throw new Error(
          "Incompatible compiler version declarations in " +
            maxCaretVersionFile +
            " and " +
            pinnedVersionFile
        );
      }
    }
  }

  if (pinnedVersion !== undefined) {
    return pinnedVersion;
  }

  return maxCaretVersion;
}

async function printContactenation(files) {
  const version = await normalizeCompilerVersionDeclarations(files);

  if (version) {
    console.log("pragma solidity " + version + ";");
  }

  for (const file of files) {
    console.log("\n// File: " + file + "\n");
    await printFileWithoutPragma(file);
  }
}

async function getTruffleRoot() {
    try {
        const truffleConfiPath = await findUp("truffle.js");
        return getDirPath(truffleConfiPath);
    } catch (error) {
        try {
            const truffleConfiPath = await findUp("truffle-config.js");
            return getDirPath(truffleConfiPath);
        }
        catch (error) {
            throw new Error(
                "Truffle Flattener must be run inside a Truffle project: truffle.js not found"
            );
        }
    }
}

function getFilePathsFromTruffleRoot(filePaths, truffleRoot) {
  return filePaths.map(f => path.relative(truffleRoot, path.resolve(f)));
}

async function main(filePaths) {
  if (filePaths.length == 0) {
    console.error("Usage: truffle-flattener <files>");
  }

  try {
    const truffleRoot = await getTruffleRoot();
    const filePathsFromTruffleRoot = getFilePathsFromTruffleRoot(
      filePaths,
      truffleRoot
    );

    process.chdir(truffleRoot);

    const sortedFiles = await getSortedFilePaths(filePathsFromTruffleRoot);
    await printContactenation(sortedFiles);
  } catch (error) {
    console.log(error);
  }
}

main(process.argv.slice(2));
<commit_msg>formatted by prettier<commit_after>#! /usr/bin/env node
const process = require("process");
const fs = require("fs");
const path = require("path");

const findUp = require("find-up");
const semver = require("semver");
const Config = require("truffle-config");
const Resolver = require("truffle-resolver");
const tsort = require("tsort");
const SolidityParser = require("solidity-parser");

const PRAGAMA_SOLIDITY_VERSION_REGEX = /^\s*pragma\ssolidity\s+(.*?)\s*;/;
const SUPPORTED_VERSION_DECLARATION_REGEX = /^\^?\d+(\.\d+){1,2}$/;
const IMPORT_SOLIDITY_REGEX = /^.*import.*$/mg;

function unique(array) {
  return [...new Set(array)];
}

function resolve(importPath) {
  const config = Config.default();
  const resolver = new Resolver(config);

  return new Promise((resolve, reject) => {
    resolver.resolve(importPath, (err, fileContents, filePath) => {
      if (err) {
        reject(err);
        return;
      }

      resolve({ fileContents, filePath });
    });
  });
}

function getDirPath(filePath) {
  return filePath.substring(0, filePath.lastIndexOf(path.sep));
}

function getDependencies(filePath, fileContents) {
  const dependencies = [];

  let imports;
  try {
    imports = SolidityParser.parse(fileContents, "imports");
  } catch (error) {
    throw new Error(
      "Could not parse " + filePath + " for extracting its imports."
    );
  }

  for (let dependency of imports) {
    if (dependency.startsWith("./") || dependency.startsWith("../")) {
      dependency = getDirPath(filePath) + path.sep + dependency;
      dependency = path.normalize(dependency);
    }

    dependencies.push(dependency);
  }

  return dependencies;
}

async function dependenciesDfs(graph, visitedFiles, filePath) {
  visitedFiles.push(filePath);

  const resolved = await resolve(filePath);

  const dependencies = getDependencies(
    resolved.filePath,
    resolved.fileContents
  );

  for (let dependency of dependencies) {
    graph.add(dependency, filePath);

    const resolvedDependency = await resolve(dependency);

    if (!visitedFiles.includes(dependency)) {
      await dependenciesDfs(graph, visitedFiles, dependency);
    }
  }
}

async function getSortedFilePaths(entryPoints) {
  const graph = tsort();
  const visitedFiles = [];

  for (const entryPoint of entryPoints) {
    await dependenciesDfs(graph, visitedFiles, entryPoint);
  }

  const topologicalSortedFiles = graph.sort();

  // If an enrty has no dependency it won't be included in the graph, so we
  // add them and then dedup the array
  const withEntries = topologicalSortedFiles.concat(entryPoints);

  const files = unique(withEntries);

  return files;
}

async function printFileWithoutPragma(filePath) {
  const resolved = await resolve(filePath);
  const output = resolved.fileContents.replace(
    PRAGAMA_SOLIDITY_VERSION_REGEX,
    ""
  ).replace(IMPORT_SOLIDITY_REGEX,"");

  console.log(output.trim());
}

async function getFileCompilerVersionDeclaration(filePath) {
  const resolved = await resolve(filePath);

  const matched = resolved.fileContents.match(PRAGAMA_SOLIDITY_VERSION_REGEX);

  if (matched === null) {
    return undefined;
  }

  const version = matched[1];

  if (!SUPPORTED_VERSION_DECLARATION_REGEX.test(version)) {
    throw new Error(
      "Unsupported compiler version declaration in  " +
        filePath +
        ": " +
        version +
        ". Only pinned or ^ versions are supported."
    );
  }

  return version;
}

async function normalizeCompilerVersionDeclarations(files) {
  let pinnedVersion;
  let pinnedVersionFile;

  let maxCaretVersion;
  let maxCaretVersionFile;

  for (const file of files) {
    const version = await getFileCompilerVersionDeclaration(file);

    if (version === undefined) {
      continue;
    }

    if (version.startsWith("^")) {
      if (maxCaretVersion == undefined) {
        maxCaretVersion = version;
        maxCaretVersionFile = file;
      } else {
        if (semver.gt(version.substr(1), maxCaretVersion.substr(1))) {
          maxCaretVersion = version;
          maxCaretVersionFile = file;
        }
      }
    } else {
      if (pinnedVersion === undefined) {
        pinnedVersion = version;
        pinnedVersionFile = file;
      } else if (pinnedVersion !== version) {
        throw new Error(
          "Differernt pinned compiler versions in " +
            pinnedVersionFile +
            " and " +
            file
        );
      }
    }

    if (maxCaretVersion !== undefined && pinnedVersion !== undefined) {
      if (!semver.satisfies(pinnedVersion, maxCaretVersion)) {
        throw new Error(
          "Incompatible compiler version declarations in " +
            maxCaretVersionFile +
            " and " +
            pinnedVersionFile
        );
      }
    }
  }

  if (pinnedVersion !== undefined) {
    return pinnedVersion;
  }

  return maxCaretVersion;
}

async function printContactenation(files) {
  const version = await normalizeCompilerVersionDeclarations(files);

  if (version) {
    console.log("pragma solidity " + version + ";");
  }

  for (const file of files) {
    console.log("\n// File: " + file + "\n");
    await printFileWithoutPragma(file);
  }
}

async function getTruffleRoot() {
  try {
      const truffleConfiPath = await findUp("truffle.js");
      return getDirPath(truffleConfiPath);
  } catch (error) {
    try {
      const truffleConfiPath = await findUp("truffle-config.js");
    }
    catch (error) {
      throw new Error(
        "Truffle Flattener must be run inside a Truffle project: truffle.js not found"
      );
    }
  }
}

function getFilePathsFromTruffleRoot(filePaths, truffleRoot) {
  return filePaths.map(f => path.relative(truffleRoot, path.resolve(f)));
}

async function main(filePaths) {
  if (filePaths.length == 0) {
    console.error("Usage: truffle-flattener <files>");
  }

  try {
    const truffleRoot = await getTruffleRoot();
    const filePathsFromTruffleRoot = getFilePathsFromTruffleRoot(
      filePaths,
      truffleRoot
    );

    process.chdir(truffleRoot);

    const sortedFiles = await getSortedFilePaths(filePathsFromTruffleRoot);
    await printContactenation(sortedFiles);
  } catch (error) {
    console.log(error);
  }
}

main(process.argv.slice(2));
<|endoftext|>


<commit_before>'use strict';

var async = require('async');
var restify = require('restify');

var onError = function(err, next) {
  if ('ValidationError' !== err.name) {
    return next(err);
  }

  return next(new restify.InvalidContentError('Validation failed' + err.errors));
};

var Resource = function (Model, options) {
  this.Model = Model;

  this.options = options || {};
  this.options.pageSize = this.options.pageSize || 100;
  this.options.listProjection = this.options.listProjection || function (req, item, cb) {
    cb(null, item);
  };
  this.options.detailProjection = this.options.detailProjection || function (req, item, cb) {
    cb(null, item);
  };
};

Resource.prototype.query = function (options) {
  var self = this;

  options = options || {};
  options.pageSize = options.pageSize || this.options.pageSize;
  options.projection = options.projection || this.options.listProjection;

  return function (req, res, next) {
    var query = self.Model.find({});

    if (req.query.q) {
      try {
        var q = JSON.parse(req.query.q);
        query = query.where(q);
      } catch (err) {
        return res.send(400, { message: 'Query is not a valid JSON object', errors: err });
      }
    }

    if (req.query.sort) {
      query = query.sort(req.query.sort);
    }

    if (req.query.select) {
      query = query.select(req.query.select);
    }

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    var page = req.query.p || 0;
    query.skip(options.pageSize * page);
    query.limit(options.pageSize);

    async.waterfall([
      function retrieveModels(cb) {
        query.exec(cb);
      },
      function buildProjections(models, cb) {
        var iterator = function(model, cb) {
          options.projection(req, model, cb);
        };

        async.map(models, iterator, cb);
      },
    ],
    function(err, models) {
      if(err) {
        return next(err);
      }

      res.send(models);
      next();
    });
  };
};

Resource.prototype.detail = function (options) {
  var self = this;

  options = options || {};
  options.projection = options.projection || this.options.detailProjection;
  return function (req, res, next) {
    var query = self.Model.findOne({ _id: req.params.id});

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    async.waterfall([
      function retrieveModel(cb) {
        query.exec(cb);
      },
      function buildProjection(model, cb) {
        if (!model) {
          return cb(new restify.ResourceNotFoundError(req.params.id));
        }

        options.projection(req, model, cb);
      }
    ],
    function(err, model) {
      if(err) {
        return next(err);
      }

      res.send(model);
      next();
    });
  };
};

Resource.prototype.insert = function () {
  var self = this;

  return function(req, res, next) {
    self.Model.create(req.body, function (err, model) {
      if (err) {
        return onError(err, next);
      }

      res.header('Location', req.url + '/' + model._id);
      res.send(200, model);
      next();
    });
  };
};

Resource.prototype.update = function () {
  var self = this;

  return function (req, res, next) {
    var query = self.Model.findOne({ _id: req.params.id});

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    query.exec(function (err, model) {
      if (err) {
        return next(err);
      }

      if (!model) {
        return next(new restify.ResourceNotFoundError(req.params.id));
      }

      if (!req.body) {
        return next(new restify.InvalidContentError('No update data sent'));
      }

      model.set(req.body);

      model.save(function (err) {
        if (err) {
          return onError(err, next);
        }

        res.send(200, model);
      });
    });
  };
};

Resource.prototype.remove = function () {
  var self = this;

  return function (req, res, next) {
    var query = self.Model.findOne({ _id: req.params.id});

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    query.exec(function (err, model) {
      if (err) {
        return next(err);
      }

      if (!model) {
        return next(new restify.ResourceNotFoundError(req.params.id));
      }

      model.remove(function (err) {
        if (err) {
          return next(err);
        }

        res.send(200, model);
      });
    });
  };
};

Resource.prototype.serve = function (path, server) {
  var closedPath = path[path.length - 1] === '/' ? path : path + '/';

  server.get(path, this.query());
  server.get(closedPath + ':id', this.detail());
  server.post(path, this.insert());
  server.del(closedPath + ':id', this.remove());
  server.patch(closedPath + ':id', this.update());
};

module.exports = function (Model, options) {
  if (!Model) {
    throw new Error('Model argument is required');
  }

  return new Resource(Model, options);
};
<commit_msg>Send datas in waterfall<commit_after>'use strict';

var async = require('async');
var restify = require('restify');

var onError = function(err, next) {
  if ('ValidationError' !== err.name) {
    return next(err);
  }

  return next(new restify.InvalidContentError('Validation failed' + err.errors));
};

var Resource = function (Model, options) {
  this.Model = Model;

  this.options = options || {};
  this.options.pageSize = this.options.pageSize || 100;
  this.options.listProjection = this.options.listProjection || function (req, item, cb) {
    cb(null, item);
  };
  this.options.detailProjection = this.options.detailProjection || function (req, item, cb) {
    cb(null, item);
  };
};

Resource.prototype.query = function (options) {
  var self = this;

  options = options || {};
  options.pageSize = options.pageSize || this.options.pageSize;
  options.projection = options.projection || this.options.listProjection;

  return function (req, res, next) {
    var query = self.Model.find({});

    if (req.query.q) {
      try {
        var q = JSON.parse(req.query.q);
        query = query.where(q);
      } catch (err) {
        return res.send(400, { message: 'Query is not a valid JSON object', errors: err });
      }
    }

    if (req.query.sort) {
      query = query.sort(req.query.sort);
    }

    if (req.query.select) {
      query = query.select(req.query.select);
    }

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    var page = req.query.p || 0;
    query.skip(options.pageSize * page);
    query.limit(options.pageSize);

    async.waterfall([
      function retrieveModels(cb) {
        query.exec(cb);
      },
      function buildProjections(models, cb) {
        var iterator = function(model, cb) {
          options.projection(req, model, cb);
        };

        async.map(models, iterator, cb);
      },
      function sendDatas(model, cb) {
        res.send(model);
        cb();
      }
    ], next);
  };
};

Resource.prototype.detail = function (options) {
  var self = this;

  options = options || {};
  options.projection = options.projection || this.options.detailProjection;
  return function (req, res, next) {
    var query = self.Model.findOne({ _id: req.params.id});

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    async.waterfall([
      function retrieveModel(cb) {
        query.exec(cb);
      },
      function buildProjection(model, cb) {
        if (!model) {
          return cb(new restify.ResourceNotFoundError(req.params.id));
        }

        options.projection(req, model, cb);
      },
      function sendDatas(model, cb) {
        res.send(model);
        cb();
      }
    ], next);
  };
};

Resource.prototype.insert = function () {
  var self = this;

  return function(req, res, next) {
    self.Model.create(req.body, function (err, model) {
      if (err) {
        return onError(err, next);
      }

      res.header('Location', req.url + '/' + model._id);
      res.send(200, model);
      next();
    });
  };
};

Resource.prototype.update = function () {
  var self = this;

  return function (req, res, next) {
    var query = self.Model.findOne({ _id: req.params.id});

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    query.exec(function (err, model) {
      if (err) {
        return next(err);
      }

      if (!model) {
        return next(new restify.ResourceNotFoundError(req.params.id));
      }

      if (!req.body) {
        return next(new restify.InvalidContentError('No update data sent'));
      }

      model.set(req.body);

      model.save(function (err) {
        if (err) {
          return onError(err, next);
        }

        res.send(200, model);
      });
    });
  };
};

Resource.prototype.remove = function () {
  var self = this;

  return function (req, res, next) {
    var query = self.Model.findOne({ _id: req.params.id});

    if (self.options.filter) {
      query = query.where(self.options.filter(req, res));
    }

    query.exec(function (err, model) {
      if (err) {
        return next(err);
      }

      if (!model) {
        return next(new restify.ResourceNotFoundError(req.params.id));
      }

      model.remove(function (err) {
        if (err) {
          return next(err);
        }

        res.send(200, model);
      });
    });
  };
};

Resource.prototype.serve = function (path, server) {
  var closedPath = path[path.length - 1] === '/' ? path : path + '/';

  server.get(path, this.query());
  server.get(closedPath + ':id', this.detail());
  server.post(path, this.insert());
  server.del(closedPath + ':id', this.remove());
  server.patch(closedPath + ':id', this.update());
};

module.exports = function (Model, options) {
  if (!Model) {
    throw new Error('Model argument is required');
  }

  return new Resource(Model, options);
};
<|endoftext|>


<commit_before>'use strict';

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

var downloadDir = path.join(__dirname, 'bin');

module.exports = function(installDir) {
	if (!fs.existsSync(installDir)) {
		mkdirp(installDir);
	}

	if (fs.existsSync(installDir) && fs.statSync(installDir).isDirectory()) {
		ncp(downloadDir, installDir, function(err) {
			if(err){throw new Error('Failed to install the latest version of the Google Closure library to the folder: ' + installDir + '.\nError: ' + err);}
		});
	} else {
		throw new Error('Installation directory not found and could not be created.');
	}
};<commit_msg>Changing the downloadDir path in index.js from /bin to /lib to mirror the downloadDir path in install.js.<commit_after>'use strict';

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

var downloadDir = path.join(__dirname, 'lib');

module.exports = function(installDir) {
	if (!fs.existsSync(installDir)) {
		mkdirp(installDir);
	}

	if (fs.existsSync(installDir) && fs.statSync(installDir).isDirectory()) {
		ncp(downloadDir, installDir, function(err) {
			if(err){throw new Error('Failed to install the latest version of the Google Closure library to the folder: ' + installDir + '.\nError: ' + err);}
		});
	} else {
		throw new Error('Installation directory not found and could not be created.');
	}
};<|endoftext|>


<commit_before>'use strict';

const sqlite3 = require('sqlite3');
const commandLineArgs = require('command-line-args');
const fs = require('fs');
const Promise = require('bluebird');

const optionDefinitions = [
  { name: 'database', alias: 'd', type: String },
  { name: 'structure', alias: 'f', type: String },
  { name: 'output', alias: 'o', type: String, defaultOption: 'output.json' }
];

const options = commandLineArgs(optionDefinitions);

let db;
let structure;
let outputFile = options.output || 'output.json';
let output = {};

const isDatabaseValid = () =>
  new Promise((resolve, reject) => {
    if (!options.database) {
      console.log('You have to define a valid sqlite database path (-d /path/to/file).');
      reject();
    }

    if (!fs.existsSync(options.database)) {
      console.log('Database file path is not valid.');
      reject();
    }

    try {
      db = new sqlite3.Database(options.database);
      resolve();
    } catch (err) {
      console.log('Database file is not valid or is corrupted.');
      reject();
    }
  });

const isStructureValid = () =>
  new Promise((resolve, reject) => {
    if (!options.structure) {
      //parse whole db
      resolve();
    }

    if (!fs.existsSync(options.structure)) {
      console.log('Structure file path is not valid.');
      reject();
    }

    fs.readFile(options.structure, 'utf8', (err, data) => {
      if (err) {
        console.log('Structure file path is not valid or its corrupted.');
        reject();
      }
      try {
        structure = JSON.parse(data);
        resolve();
      } catch (err) {
        console.log('Structure file has not a valid JSON structure.');
        reject();
      }
    });
  });

const buildStructure = () =>
  new Promise((resolve, reject) => {
    structure = { tables: [] };

    db.all(`select name from sqlite_master where type='table' and name not like 'sqlite_sequence';`, (err, tables) => {
      for (let i = 0, len = tables.length; i < len; ++i) {
        structure.tables.push({
          key: tables[i].name,
          query: `SELECT * from ${tables[i].name}`
        });
      }
      resolve();
    });
  });

const parse = () =>
  new Promise((resolve, reject) => {
    if (!structure.tables) {
      reject();
    }

    for (let i = 0, len = structure.tables.length; i < len; ++i) {
      const item = structure.tables[i];

      db.all(item.query, (err, items) => {
        output[item.key] = items;
        if (i === len - 1) {
          resolve();
        }
      });
    }
  });

const writeOutput = () =>
  new Promise((resolve, reject) => {
    let content = '';

    try {
      content = JSON.stringify(output);
    } catch (err) {
      console.log(err);
      reject(err);
    }

    fs.writeFile(outputFile, content, (err) => {
      if (err) {
        console.log(err);
        reject(err);
      }

      resolve();
    });
  });

const init = () => {
  if (options.structure) {
    isDatabaseValid()
      .then(isStructureValid)
      .then(parse)
      .then(writeOutput)
      .catch(e => console.log(e));
  } else {
    isDatabaseValid()
      .then(buildStructure)
      .then(parse)
      .then(writeOutput)
      .catch(e => console.log(e));
  }
}

init();
<commit_msg>Added the possibility to have both options, dump database and define a structure<commit_after>'use strict';

const sqlite3 = require('sqlite3');
const commandLineArgs = require('command-line-args');
const fs = require('fs');
const Promise = require('bluebird');

const optionDefinitions = [
  { name: 'database', alias: 'd', type: String },
  { name: 'structure', alias: 's', type: String },
  { name: 'output', alias: 'o', type: String, defaultOption: 'output.json' },
  { name: 'extend', alias: 'e', type: Boolean, defaultOption: false }
];

const options = commandLineArgs(optionDefinitions);

let db;
let structure;
let outputFile = options.output || 'output.json';
let output = {};

const isDatabaseValid = () =>
  new Promise((resolve, reject) => {
    if (!options.database) {
      console.log('You have to define a valid sqlite database path (-d /path/to/file).');
      reject();
    }

    if (!fs.existsSync(options.database)) {
      console.log('Database file path is not valid.');
      reject();
    }

    try {
      db = new sqlite3.Database(options.database);
      resolve();
    } catch (err) {
      console.log('Database file is not valid or is corrupted.');
      reject();
    }
  });

const isStructureValid = () =>
  new Promise((resolve, reject) => {
    if (!options.structure) {
      //parse whole db
      resolve();
    }

    if (!fs.existsSync(options.structure)) {
      console.log('Structure file path is not valid.');
      reject();
    }

    fs.readFile(options.structure, 'utf8', (err, data) => {
      if (err) {
        console.log('Structure file path is not valid or its corrupted.');
        reject();
      }
      try {
        structure = structure || { tables: [] };
        structure.tables = structure.tables.concat(JSON.parse(data).tables);
        resolve();
      } catch (err) {
        console.log('Structure file has not a valid JSON structure.');
        reject();
      }
    });
  });

const buildStructure = () =>
  new Promise((resolve, reject) => {
    structure = structure || { tables: [] };

    db.all(`select name from sqlite_master where type='table' and name not like 'sqlite_sequence';`, (err, tables) => {
      for (let i = 0, len = tables.length; i < len; ++i) {
        structure.tables.push({
          key: tables[i].name,
          query: `SELECT * from ${tables[i].name}`
        });
      }

      resolve();
    });
  });

const parse = () =>
  new Promise((resolve, reject) => {
    if (!structure.tables) {
      reject();
    }

    for (let i = 0, len = structure.tables.length; i < len; ++i) {
      const item = structure.tables[i];

      db.all(item.query, (err, items) => {
        output[item.key] = items;
        if (i === len - 1) {
          resolve();
        }
      });
    }
  });

const writeOutput = () =>
  new Promise((resolve, reject) => {
    let content = '';

    try {
      content = JSON.stringify(output);
    } catch (err) {
      console.log(err);
      reject(err);
    }

    fs.writeFile(outputFile, content, (err) => {
      if (err) {
        console.log(err);
        reject(err);
      }

      resolve();
    });
  });

const init = () => {
  if (options.extend) {
    isDatabaseValid()
      .then(buildStructure)
      .then(isStructureValid)
      .then(parse)
      .then(writeOutput)
      .catch(e => console.log(e));
  } else {
    if (options.structure) {
      isDatabaseValid()
        .then(isStructureValid)
        .then(parse)
        .then(writeOutput)
        .catch(e => console.log(e));
    } else {
      isDatabaseValid()
        .then(buildStructure)
        .then(parse)
        .then(writeOutput)
        .catch(e => console.log(e));
    }
  }
}

init();
<|endoftext|>


<commit_before>/**
 * @providesModule Notifications
 */

'use strict';

import { NativeModules, DeviceEventEmitter, NetInfo, Platform } from 'react-native';
import invariant from 'invariant';

const { RNOneSignal } = NativeModules;

var Notifications = {
	onError: false,
	onNotificationOpened: false,
	onNotificationsRegistered: false
};

var _pendingNotifications = [];
var DEVICE_NOTIF_EVENT = 'remoteNotificationOpened';
var DEVICE_NOTIF_REG_EVENT = 'remoteNotificationsRegistered';
var DEVICE_IDS_AVAILABLE = 'idsAvailable';

/**
 * Configure local and remote notifications
 * @param {Object}		options
 * @param {function}	options.onNotificationOpened - Fired when a remote notification is received.
 * @param {function} 	options.onError - None
 */
Notifications.configure = function(options: Object) {
	if ( typeof options.onError !== 'undefined' ) {
		this.onError = options.onError;
	}

	if ( typeof options.onNotificationOpened !== 'undefined' ) {
		this.onNotificationOpened = options.onNotificationOpened;

		if (_pendingNotifications.length > 0) {
			var notification = _pendingNotifications.pop();
			this._onNotificationOpened(notification.message, notification.data, notification.isActive);
		}
	}

	if( typeof options.onNotificationsRegistered !== 'undefined' ) {
		this.onNotificationsRegistered = options.onNotificationsRegistered;
	}

	if( typeof options.onIdsAvailable !== 'undefined' ) {
		this.onIdsAvailable = options.onIdsAvailable;
	}

	function handleConnectionStateChange(isConnected) {
    if(!isConnected) return;

    RNOneSignal.configure();
    NetInfo.isConnected.removeEventListener('change', handleConnectionStateChange);
  }

  NetInfo.isConnected.fetch().then(isConnected => {
    if(isConnected) return RNOneSignal.configure();
    NetInfo.isConnected.addEventListener('change', handleConnectionStateChange);
  });
};

/* Unregister */
Notifications.unregister = function() {
	this.onNotificationOpened = false;
};

Notifications.requestPermissions = function(permissions) {
	var requestedPermissions = {};
	if (Platform.OS == 'ios') {
		if (permissions) {
			requestedPermissions = {
				alert: !!permissions.alert,
				badge: !!permissions.badge,
				sound: !!permissions.sound
			};
		} else {
			requestedPermissions = {
				alert: true,
				badge: true,
				sound: true
			};
		}
		RNOneSignal.requestPermissions(requestedPermissions);
	} else {
		console.log("This function is not supported on Android");
	}
};

Notifications.registerForPushNotifications = function(){
	if (Platform.OS == 'ios') {
		RNOneSignal.registerForPushNotifications();
	} else {
		console.log("This function is not supported on Android");
	}
};

Notifications.checkPermissions = function(callback: Function) {
	if (Platform.OS == 'ios') {
		invariant(
			typeof callback === 'function',
			'Must provide a valid callback'
		);
		RNOneSignal.checkPermissions(callback);
	} else {
		console.log("This function is not supported on Android");
	}
};

Notifications._onNotificationOpened = function(message, data, isActive) {
	if ( this.onNotificationOpened === false ) {
		var notification = {message: message, data: data, isActive: isActive};
		_pendingNotifications.push(notification);
		return;
	}
	this.onNotificationOpened(message, data, isActive);
};

Notifications._onNotificationsRegistered = function(payload) {
	if ( this.onNotificationsRegistered === false ) {
		return;
 	}
 	this.onNotificationsRegistered(payload);
};

Notifications.sendTag = function(key, value) {
	RNOneSignal.sendTag(key, value);
};

Notifications.sendTags = function(tags) {
	RNOneSignal.sendTags(tags || {});
};

Notifications.getTags = function(next) {
	RNOneSignal.getTags(next);
};

Notifications.deleteTag = function(key) {
	RNOneSignal.deleteTag(key);
};

Notifications.enableVibrate = function(enable) {
	if (Platform.OS == 'android') {
		RNOneSignal.enableVibrate(enable);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.enableSound = function(enable) {
	if (Platform.OS == 'android') {
		RNOneSignal.enableSound(enable);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.enableNotificationsWhenActive = function(enable) {
	if (Platform.OS == 'android') {
		RNOneSignal.enableNotificationsWhenActive(enable);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.enableInAppAlertNotification = function(enable) {
	RNOneSignal.enableInAppAlertNotification(enable);
};

Notifications.setSubscription = function(enable) {
	RNOneSignal.setSubscription(enable);
};

Notifications.promptLocation = function() {
	if (Platform.OS == 'android') {
		RNOneSignal.promptLocation();
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.postNotification = function(contents, data, player_id) {
	if (Platform.OS == 'android') {
		RNOneSignal.postNotification(JSON.stringify(contents), JSON.stringify(data), player_id);
	} else {
		RNOneSignal.postNotification(contents, data, player_id);
	}
};

Notifications.clearOneSignalNotifications = function() {
	if (Platform.OS == 'android') {
		RNOneSignal.clearOneSignalNotifications();
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.cancelNotification = function(id) {
	if (Platform.OS == 'android') {
		RNOneSignal.cancelNotification(id);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.idsAvailable = function(idsAvailable) {
	console.log('Please use the onIdsAvailable event instead, it can be defined in the register options');
};

DeviceEventEmitter.addListener(DEVICE_NOTIF_EVENT, function(notifData) {
	var message = notifData.message;
	var data = (notifData.additionalData !== null && typeof notifData.additionalData === 'object') ? notifData.additionalData : JSON.parse(notifData.additionalData);
	var isActive = notifData.isActive;
	Notifications._onNotificationOpened(message, data, isActive);
});

DeviceEventEmitter.addListener(DEVICE_NOTIF_REG_EVENT, function(notifData) {
	Notifications._onNotificationsRegistered(notifData)
});

DeviceEventEmitter.addListener(DEVICE_IDS_AVAILABLE, function(idsAvailable) {
	if (Notifications.onIdsAvailable) {
		Notifications.onIdsAvailable(idsAvailable);
	}
});

module.exports = Notifications;
<commit_msg>fix(#83) add catch for rejection and passthrough to onError<commit_after>/**
 * @providesModule Notifications
 */

'use strict';

import { NativeModules, DeviceEventEmitter, NetInfo, Platform } from 'react-native';
import invariant from 'invariant';

const { RNOneSignal } = NativeModules;

var Notifications = {
	onError: false,
	onNotificationOpened: false,
	onNotificationsRegistered: false
};

var _pendingNotifications = [];
var DEVICE_NOTIF_EVENT = 'remoteNotificationOpened';
var DEVICE_NOTIF_REG_EVENT = 'remoteNotificationsRegistered';
var DEVICE_IDS_AVAILABLE = 'idsAvailable';

/**
 * Configure local and remote notifications
 * @param {Object}		options
 * @param {function}	options.onNotificationOpened - Fired when a remote notification is received.
 * @param {function} 	options.onError - None
 */
Notifications.configure = function(options: Object) {
	if ( typeof options.onError !== 'undefined' ) {
		this.onError = options.onError;
	}

	if ( typeof options.onNotificationOpened !== 'undefined' ) {
		this.onNotificationOpened = options.onNotificationOpened;

		if (_pendingNotifications.length > 0) {
			var notification = _pendingNotifications.pop();
			this._onNotificationOpened(notification.message, notification.data, notification.isActive);
		}
	}

	if( typeof options.onNotificationsRegistered !== 'undefined' ) {
		this.onNotificationsRegistered = options.onNotificationsRegistered;
	}

	if( typeof options.onIdsAvailable !== 'undefined' ) {
		this.onIdsAvailable = options.onIdsAvailable;
	}

	function handleConnectionStateChange(isConnected) {
    if(!isConnected) return;

    RNOneSignal.configure();
    NetInfo.isConnected.removeEventListener('change', handleConnectionStateChange);
  }

  NetInfo.isConnected.fetch().then(isConnected => {
    if(isConnected) return RNOneSignal.configure();
    NetInfo.isConnected.addEventListener('change', handleConnectionStateChange);
  }).catch((...args)=>this.onError(args));
};

/* Unregister */
Notifications.unregister = function() {
	this.onNotificationOpened = false;
};

Notifications.requestPermissions = function(permissions) {
	var requestedPermissions = {};
	if (Platform.OS == 'ios') {
		if (permissions) {
			requestedPermissions = {
				alert: !!permissions.alert,
				badge: !!permissions.badge,
				sound: !!permissions.sound
			};
		} else {
			requestedPermissions = {
				alert: true,
				badge: true,
				sound: true
			};
		}
		RNOneSignal.requestPermissions(requestedPermissions);
	} else {
		console.log("This function is not supported on Android");
	}
};

Notifications.registerForPushNotifications = function(){
	if (Platform.OS == 'ios') {
		RNOneSignal.registerForPushNotifications();
	} else {
		console.log("This function is not supported on Android");
	}
};

Notifications.checkPermissions = function(callback: Function) {
	if (Platform.OS == 'ios') {
		invariant(
			typeof callback === 'function',
			'Must provide a valid callback'
		);
		RNOneSignal.checkPermissions(callback);
	} else {
		console.log("This function is not supported on Android");
	}
};

Notifications._onNotificationOpened = function(message, data, isActive) {
	if ( this.onNotificationOpened === false ) {
		var notification = {message: message, data: data, isActive: isActive};
		_pendingNotifications.push(notification);
		return;
	}
	this.onNotificationOpened(message, data, isActive);
};

Notifications._onNotificationsRegistered = function(payload) {
	if ( this.onNotificationsRegistered === false ) {
		return;
 	}
 	this.onNotificationsRegistered(payload);
};

Notifications.sendTag = function(key, value) {
	RNOneSignal.sendTag(key, value);
};

Notifications.sendTags = function(tags) {
	RNOneSignal.sendTags(tags || {});
};

Notifications.getTags = function(next) {
	RNOneSignal.getTags(next);
};

Notifications.deleteTag = function(key) {
	RNOneSignal.deleteTag(key);
};

Notifications.enableVibrate = function(enable) {
	if (Platform.OS == 'android') {
		RNOneSignal.enableVibrate(enable);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.enableSound = function(enable) {
	if (Platform.OS == 'android') {
		RNOneSignal.enableSound(enable);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.enableNotificationsWhenActive = function(enable) {
	if (Platform.OS == 'android') {
		RNOneSignal.enableNotificationsWhenActive(enable);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.enableInAppAlertNotification = function(enable) {
	RNOneSignal.enableInAppAlertNotification(enable);
};

Notifications.setSubscription = function(enable) {
	RNOneSignal.setSubscription(enable);
};

Notifications.promptLocation = function() {
	if (Platform.OS == 'android') {
		RNOneSignal.promptLocation();
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.postNotification = function(contents, data, player_id) {
	if (Platform.OS == 'android') {
		RNOneSignal.postNotification(JSON.stringify(contents), JSON.stringify(data), player_id);
	} else {
		RNOneSignal.postNotification(contents, data, player_id);
	}
};

Notifications.clearOneSignalNotifications = function() {
	if (Platform.OS == 'android') {
		RNOneSignal.clearOneSignalNotifications();
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.cancelNotification = function(id) {
	if (Platform.OS == 'android') {
		RNOneSignal.cancelNotification(id);
	} else {
		console.log("This function is not supported on iOS");
	}
};

Notifications.idsAvailable = function(idsAvailable) {
	console.log('Please use the onIdsAvailable event instead, it can be defined in the register options');
};

DeviceEventEmitter.addListener(DEVICE_NOTIF_EVENT, function(notifData) {
	var message = notifData.message;
	var data = (notifData.additionalData !== null && typeof notifData.additionalData === 'object') ? notifData.additionalData : JSON.parse(notifData.additionalData);
	var isActive = notifData.isActive;
	Notifications._onNotificationOpened(message, data, isActive);
});

DeviceEventEmitter.addListener(DEVICE_NOTIF_REG_EVENT, function(notifData) {
	Notifications._onNotificationsRegistered(notifData)
});

DeviceEventEmitter.addListener(DEVICE_IDS_AVAILABLE, function(idsAvailable) {
	if (Notifications.onIdsAvailable) {
		Notifications.onIdsAvailable(idsAvailable);
	}
});

module.exports = Notifications;
<|endoftext|>


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

var utils = require('./lib/utils.js');

exports.Collection = require('./lib/Collection.js');
exports.generateUniqueId = utils.generateUniqueId();
<commit_msg>generateuniqueid binding fixed<commit_after>/*jslint node:true*/
'use strict';

var utils = require('./lib/utils.js');

exports.Collection = require('./lib/Collection.js');
exports.generateUniqueId = utils.generateUniqueId;
<|endoftext|>