<commit_before>'use strict';

var util = require('./lib/util');

//
// ## exports
//

exports = module.exports = Hie;

//
// ## Hie.set
//

Hie.prototype.set = function(stems, opts){
  var stemsIs = util.type(stems);
  if( !stemsIs.match(/function|string|array|object/g) ){
   throw new util.Error('boil(stems [,options]) \n'+
     'should have at least one argument being: \n'+
     ' - a `string`\n'+
     ' - an `array` of `strings`\n'+
     ' - a `function`\n'+
     ' - an `object`'
   );
  }

  var optsIs = util.type(opts);
  opts = optsIs.plainObject || stemsIs.plainObject || {
    handle: optsIs.function || stemsIs.function
  };

  if(stemsIs.array){
    opts.aliases = stems.slice(1);
    stems = stems[0];
  }
  stems = this.boil('set', stems);
  stemsIs = optsIs = null; //wipe

  var node = this.cache, parent = this.cache;
  function createChildren(stem, index){
    node.children = node.children || { };
    if(stem && !node.children[stem]){
      node.children[stem] = {
        name: stem,
        depth: node.depth + 1,
        parent: ((node.parent || '') + ' ' + node.name).trim()
      };
    }
    node.completion = node.completion || [ ];
    if(node.completion.indexOf(stem) < 0){
      node.completion.push(stem);
    }
    node = node.chilren[stem];
    if(stems[index+1]){ parent = node; }
  }

  if(stems.length){ stems.forEach(createChildren); }

  var parse = this.parse.method;
  Object.keys(opts).forEach(function parseProps(prop){
    var value = opts[prop];
    if(value === void 0){ return ; }
    else if(value === null){ return delete node[prop]; }

    node[prop] = node[prop] || { };

    if(parse[prop]){
      parse[prop].call(this, parent, stems, value);
    } else if(util.type(value).plainObject){
      util.merge(node[prop], util.clone(value));
    } else {
      node[prop] = util.clone(value);
    }
  }, this);

  return this;
};

//
// ## Hie.get
//

Hie.prototype.get = function(stems){
  var boil = this.boil('get');
  var stem, index = 0, found = this.cache;

  stems = boil(stems);
  while(stems[index]){
    stem = stems[index];
    if(found.aliases && found.aliases[stem]){
      stems = stems.join(' ').replace(stem, found.aliases[stem]);
      stems = boil(stems);
       stem = stems[index];
    }
    if(found.children && found.children[stem]){
      index++; found = found.children[stem];
    } else { index = -1; }
  }

  var shallow = util.merge({ }, found);
  stem = null; // wipe
  return shallow;
};

//
// ## Hie.boil
//

Hie.prototype.boil = function(name, stems_, regexp_){
  if(typeof this[name] !== 'function'){
    throw new Error(
      'boil(method[, boiler, regexp]):\n > no method `'+name+'` at `this`');
  }

  var boiler = null, self = null;
  if(util.type(stems_).function){  // set
    self = this; boiler = stems_;
    this.boil.method[name] = function(stems, regex){
      regex = util.type(regex).regexp || /[ ]+/;
      stems = boiler.call(self, util.type(stems), regex);
      if(util.type(stems).array){ return stems; }
      throw new util.Error(
        'boil(method, boiler[, regexp]):\n > boiler should return an array');
    };
    return this;

  } else if(!this.boil.method[name]){ // ensure
    this.boil.method[name] = function(stems, regex){
      stems = util.type(stems);
      regex = util.type(regex).regexp || /[ ]+/;
      if(!stems.string && !stems.array){ return [ ]; }
      return (stems.string || stems.array.join(' ')).trim().split(regex);
    };
  }

  boiler = this.boil.method[name];
  if(stems_){ return boiler(stems_, regexp_); }
  return boiler;
};

//
// ## Hie.parse
//

Hie.prototype.parse = function(prop, parser){

  if(!parser){
    var copy = this.parser.method[prop];
    return copy;
  }

  if(typeof prop !== 'string'){
    throw new util.Error(
      'parse(prop[, parser]):\n > `prop` should be a string');
  }
  if(typeof parser !== 'function'){
    throw new util.Error(
      'parse(prop[, parser]):\n > If given, `parser` should be a function');
  }

  this.parser.method[prop] = parser;

  return this;
};

//
// ## Hie.prototype.constructor
//

function Hie(opt){
  opt = opt || { };
  if( !(this instanceof Hie) ){
    return new Hie(opt);
  }

  Object.defineProperty(this, 'cache', {
    writable: true,
    enumerable: false,
    configurable: false,
    value: {
      name: util.type(opt.name).string || '#rootNode',
      depth: 0
    }
  });

  this.boil.method = { };
  this.parse.method = { };

  //
  // #### parse `handle` props
  //

  this.parse('aliases', function (node, stems, aliases){
    aliases = util.boil(aliases);
    if(!aliases.length){  return this;  }

    node.aliases = node.aliases || { };
    node.completion = node.completion || [ ];
    aliases.forEach(function(alias){
      node.aliases[alias] = stems.join(' ');
      if(node.completion.indexOf(alias) < 0){
        node.completion.push(alias);
      }
    });
    return this;
  });

  //
  // #### parse `handle` props
  //

  this.parse('handle', function (node, stems, handle){
    var len;
    if((len = stems.length)){
      node.children[stems[len-1]].handle = handle;
    } else {
      node.handle = handle;
    }
    return this;
  });

  //
  // #### parse `completion` props
  //

  this.parse('completion', function (node, stems, completion){
    completion = util.boil(completion);
    if(!completion.length){  return this;  }
    completion.forEach(function(name){
      node.completion = node.completion || [ ];
      if(node.completion.indexOf(name) < 0){
        node.completion.push(name);
      }
    });
  });
}
<commit_msg>use the set boiler, shorter throw on Hie.set<commit_after>'use strict';

var util = require('./lib/util');

//
// ## exports
//

exports = module.exports = Hie;

//
// ## Hie.set
//

Hie.prototype.set = function(stems, opts){
  var stemsIs = util.type(stems);
  if( !stemsIs.match(/function|string|array|object/g) ){
   throw new util.Error('set(stems [,options]) \n'+
     'should have at least one argument being: \n'+
     '  a `string`, an `array`, a function or an object'
   );
  }

  var optsIs = util.type(opts);
  opts = optsIs.plainObject || stemsIs.plainObject || {
    handle: optsIs.function || stemsIs.function
  };

  if(stemsIs.array){
    opts.aliases = stems.slice(1);
    stems = stems[0];
  }
  stems = this.boil('set', stems);
  stemsIs = optsIs = null; //wipe

  var node = this.cache, parent = this.cache;
  function createChildren(stem, index){
    node.children = node.children || { };
    if(stem && !node.children[stem]){
      node.children[stem] = {
        name: stem,
        depth: node.depth + 1,
        parent: ((node.parent || '') + ' ' + node.name).trim()
      };
    }
    node.completion = node.completion || [ ];
    if(node.completion.indexOf(stem) < 0){
      node.completion.push(stem);
    }
    node = node.chilren[stem];
    if(stems[index+1]){ parent = node; }
  }

  if(stems.length){ stems.forEach(createChildren); }

  var parse = this.parse.method;
  Object.keys(opts).forEach(function parseProps(prop){
    var value = opts[prop];
    if(value === void 0){ return ; }
    else if(value === null){ return delete node[prop]; }

    node[prop] = node[prop] || { };

    if(parse[prop]){
      parse[prop].call(this, parent, stems, value);
    } else if(util.type(value).plainObject){
      util.merge(node[prop], util.clone(value, true));
    } else {
      node[prop] = util.clone(value);
    }
  }, this);

  return this;
};

//
// ## Hie.get
//

Hie.prototype.get = function(stems){
  var boil = this.boil('get');
  var stem, index = 0, found = this.cache;

  stems = boil(stems);
  while(stems[index]){
    stem = stems[index];
    if(found.aliases && found.aliases[stem]){
      stems = stems.join(' ').replace(stem, found.aliases[stem]);
      stems = boil(stems); stem = stems[index];
    }
    if(found.children && found.children[stem]){
      index++; found = found.children[stem];
    } else { index = -1; }
  }

  var shallow = util.merge({ }, found);
  stem = null; // wipe
  return shallow;
};

//
// ## Hie.boil
//

Hie.prototype.boil = function(name, stems_, regexp_){
  if(typeof this[name] !== 'function'){
    throw new Error(
      'boil(method[, boiler, regexp]):\n > no method `'+name+'` at `this`');
  }

  var boiler = null, self = null;
  if(util.type(stems_).function){  // set
    self = this; boiler = stems_;
    this.boil.method[name] = function(stems, regex){
      regex = util.type(regex).regexp || /[ ]+/;
      stems = boiler.call(self, util.type(stems), regex);
      if(util.type(stems).array){ return stems; }
      throw new util.Error(
        'boil(method, boiler[, regexp]):\n > boiler should return an array');
    };
    return this;

  } else if(!this.boil.method[name]){ // ensure
    this.boil.method[name] = function(stems, regex){
      stems = util.type(stems);
      regex = util.type(regex).regexp || /[ ]+/;
      if(!stems.string && !stems.array){ return [ ]; }
      return (stems.string || stems.array.join(' ')).trim().split(regex);
    };
  }

  boiler = this.boil.method[name];
  if(stems_){ return boiler(stems_, regexp_); }
  return boiler;
};

//
// ## Hie.parse
//

Hie.prototype.parse = function(prop, parser){

  if(!parser){
    var copy = this.parser.method[prop];
    return copy;
  }

  if(typeof prop !== 'string'){
    throw new util.Error(
      'parse(prop[, parser]):\n > `prop` should be a string');
  }
  if(typeof parser !== 'function'){
    throw new util.Error(
      'parse(prop[, parser]):\n > If given, `parser` should be a function');
  }

  this.parser.method[prop] = parser;

  return this;
};

//
// ## Hie.prototype.constructor
//

function Hie(opt){
  opt = opt || { };
  if( !(this instanceof Hie) ){
    return new Hie(opt);
  }

  Object.defineProperty(this, 'cache', {
    writable: true,
    enumerable: false,
    configurable: false,
    value: {
      name: util.type(opt.name).string || '#rootNode',
      depth: 0
    }
  });

  this.boil.method = { };
  this.parse.method = { };

  //
  // #### parse `handle` props
  //

  this.parse('aliases', function (node, stems, aliases){
    aliases = this.boil('set', aliases);
    if(!aliases.length){  return this;  }

    node.aliases = node.aliases || { };
    node.completion = node.completion || [ ];
    aliases.forEach(function(alias){
      node.aliases[alias] = stems.join(' ');
      if(node.completion.indexOf(alias) < 0){
        node.completion.push(alias);
      }
    });
  });

  //
  // #### parse `handle` props
  //

  this.parse('handle', function (node, stems, handle){
    var len = stems.length;
    if(len){  node.children[stems[len-1]].handle = handle;  }
      else {  node.handle = handle;  }
  });

  //
  // #### parse `completion` props
  //

  this.parse('completion', function (node, stems, completion){
    completion = util.boil('set', completion);
    if(!completion.length){  return this;  }
    completion.forEach(function(name){
      node.completion = node.completion || [ ];
      if(node.completion.indexOf(name) < 0){
        node.completion.push(name);
      }
    });
  });
}
<|endoftext|>


<commit_before>var fetch = fetch || require('node-fetch')

function jsref(ob, opts) {
  if (typeof ob !== 'object') return ob
  opts = opts || {}
  var root = opts.root || 'http://localhost/'
  var $ref = opts.$ref || '$ref'
  var vals = []
  var refs = {}

  var find = opts.find || function(url) {
    url = url.indexOf('http') ? root+url : url
    var [url,ref] = url.split('#')
    ref = (ref && ref.length) ? ref : opts.frag
    var rec = fetch(url, opts.http).then(res => res.json()).then(rec => ref ? extRefs('#'+ref,rec) : rec)
    return opts.deep ? rec.then(rec => jsref(rec, opts)) : rec
  }

  function extRefs(ref, val) {
    if (opts.refs && opts.refs[ref]) return opts.refs[ref]
    val = val || ob
    if (ref[0] != '#') return opts.lazy ? find(ref) : vals.push(find(ref))
    var keys = ref.substring(1).split(/[\.\/]/)
    if (!keys[0][1]) keys.shift()
    while(val && keys.length) val = val[keys.shift()]
    return val
  }

  function getRefs(ob) {
    if (ob && ob[$ref]) {
      if (!refs[ob[$ref]]) refs[ob[$ref]] = extRefs(ob[$ref])
    } else for (var i in ob) if (typeof ob[i] === 'object') getRefs(ob[i])
  }

  function fixRefs(ob) {
    if (ob && ob[$ref]) return refs[ob[$ref]] || ob
    for (var k in ob) if (typeof ob[k] === 'object') ob[k] = fixRefs(ob[k])
    return ob
  }

  getRefs(ob)

  if (opts.lazy) return Promise.resolve(fixRefs(ob))

  return Promise.all(vals).then(recs => {
    for (var r in refs) if (!isNaN(refs[r])) refs[r] = recs[refs[r]-1]
    return fixRefs(ob)
  })
}

if (typeof module == 'object') module.exports = jsref
<commit_msg>logging<commit_after>var fetch = fetch || require('node-fetch')

function jsref(ob, opts) {
  if (typeof ob !== 'object') return ob
  opts = opts || {}
  var root = opts.root || 'http://localhost/'
  var $ref = opts.$ref || '$ref'
  var vals = []
  var refs = {}

  var find = opts.find || function(url) {
    console.log('find', url)
    url = url.indexOf('http') ? root+url : url
    var [url,ref] = url.split('#')
    ref = (ref && ref.length) ? ref : opts.frag
    var rec = fetch(url, opts.http).then(res => res.json()).then(rec => ref ? extRefs('#'+ref,rec) : rec)
    return opts.deep ? rec.then(rec => jsref(rec, opts)) : rec
  }

  function extRefs(ref, val) {
    console.log('extRefs', ref)
    if (opts.refs && opts.refs[ref]) return opts.refs[ref]
    val = val || ob
    if (ref[0] != '#') return opts.lazy ? find(ref) : vals.push(find(ref))
    var keys = ref.substring(1).split(/[\.\/]/)
    if (!keys[0][1]) keys.shift()
    while(val && keys.length) val = val[keys.shift()]
    return val
  }

  function getRefs(ob) {
    console.log('getRefs', ob)
    if (ob && ob[$ref]) {
      if (!refs[ob[$ref]]) refs[ob[$ref]] = extRefs(ob[$ref])
    } else for (var i in ob) if (typeof ob[i] === 'object') getRefs(ob[i])
  }

  function fixRefs(ob) {
    console.log('fixRefs', ob)
    if (ob && ob[$ref]) return refs[ob[$ref]] || ob
    for (var k in ob) if (typeof ob[k] === 'object') ob[k] = fixRefs(ob[k])
    return ob
  }

  getRefs(ob)

  if (opts.lazy) return Promise.resolve(fixRefs(ob))

  return Promise.all(vals).then(recs => {
    console.log('promAll', recs)
    for (var r in refs) if (!isNaN(refs[r])) refs[r] = recs[refs[r]-1]
    return fixRefs(ob)
  })
}

if (this.module) module.exports = jsref
<|endoftext|>


<commit_before>"use strict"
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const spawn = require('child_process').spawn
const bodyParser = require('body-parser')
const mkdirp = require('mkdirp');
const fs = require('fs');

const clients = {}
const users = {
  'ms2666': 51,
  'fc249': 52
}

const script = spawn('python', ['test.py'])

script.stderr.on('data', (data) => {
  console.log('stderr: ' + data)
})

script.on('close', (code) => {
  console.log('python script exited with code ' + code)
})

app.use(bodyParser.text({
  type: 'text/plain',
  limit: '4mb'
}))

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html')
})

app.post('/login/:id', (req, res) => {
  // id is the client we want to connect to
  if (!(req.params.id in clients)) {
    return res.status(404).json({ message: 'Client with ID: ' + req.params.id + ' not found.'})
  }

  let id = req.params.id

  // plaintext request to log in based on this
  let dir = './Data_test'
  let path = 'u000_w000'
  let filename = 'u000_w000.accelerometer.log'

  while(fs.existsSync('./Data_test/RUNNING')) {}

  console.log("No RUNNING file detected")

  writeToFile(dir, path, filename, req.body, function (err) {
    if (err) {
      return res.status(500).json({ message: 'Disaster! Internal Server Error' })
    }
    // Wait on the RESULT file
    waitOnResult(function(result) {
      authenticate(result, io, id)
      fs.unlinkSync('./Data_test/RESULT')
      return res.status(200).json({ attempted: true })
    })
  })
})

app.post('/train/:id', (req, res) => {
  if (!(req.params.id in users)) {
    return res.status(404).json({ message: 'User with ID: ' + req.params.id + ' not found.'})
  }

  console.log(req.params.id)
  console.log(users[req.params.id])
  formNextFileName(true, users[req.params.id], function (err, filename) {
    let dir = './Data'
    let path = filename
    // Let's get how many files there are...
    filename = filename + '_accelerometer.log'
    writeToFile(dir, path, filename, req.body, function (err) {
      if (err) {
        return res.status(500).json({ message: 'Disaster! Internal Server Error' })
      }
      return res.status(200).json({ message: 'Data posted!' })
    })
  })
})

io.on('connection', (socket) => {
  console.log('A client connected. Registering client...')
  registerClient(socket, (id) => {
    console.log('Client registered with id ' + id + '. Returning new client id.')
    // emit this id back to the client
    socket.emit('client_id', id)
  })

  // define the socket handlers
  socket.on('disconnect', () => {
    // how do we get an id
    // unregisterClient(socket, id)
  })

  socket.on('login', (id) => {
    console.log('Received login')
    setTimeout(function() {
      console.log('Doing work')
      attemptLogin(io, id)
    }, 0)
  })
})

app.use(express.static(__dirname))

http.listen(3000, () => {
  console.log('listening on *:3000')
})

function registerClient(socket, callback) {
  if (!callback || typeof callback !== "function") {
    throw new Error("registerClient(socket, callback) expected callback to be a function")
  }

  let id = 0
  do {
    id = Math.floor(Math.random() * 1000 + 1)
  } while (id in clients);

  // register this socket to a room with this name
  socket.join(id, () => {
    clients[id] = 'val'
    callback(id);
  })
}

function unregisterClient(socket, id) {
  // unregister this socket
  socket.leave(id, () => {
    delete clients[id]
  })
}

function authenticate(person, io, id) {
  console.log("Trying to authenticate person: " + person)
  if (person !== 'other') {
    io.to(id).emit('access', person)
  } else {
    io.to(id).emit('no-access')
  }
}

function formNextFileName(training, userId, callback) {
  if (!callback || typeof callback !== "function") {
    throw new Error("writeToFile(path, filename, contents, callback) expected callback to be a function")
  }
  // leftpad with 0's
  userId = zfill3(userId)
  if (training) {
    fs.readdir('./Data/', function(err, files) {
      if (err) {
        callback(err)
      }
      let walkId = files.length + 1
      walkId = zfill3(walkId)
      callback(null, 'u' + userId + '_w' + walkId)
    })
  } else {
    walkId = '000'
    callback(null, 'u' + userId + '_w' + walkId)
  }
}

function zfill3(num) {
  return ("000" + num).slice(-3)
}

function writeToFile(dir, path, filename, contents, callback) {
  if (!callback || typeof callback !== "function") {
    throw new Error("writeToFile(path, filename, contents, callback) expected callback to be a function")
  }
  mkdirp(dir + '/' + path, function (err) {
    fs.open(dir + '/' + path + '/' + filename,'r',function (err, fd) {
      if (err) {
        fs.writeFile(dir + '/' + path + '/' + filename, contents, function (err) {
            if(err) {
              callback(err)
            }
            console.log("File " + filename + " created!")
            callback(null)
        })
      } else {
        console.log("The file already exists!")
        callback(new Error("file already exists"))
      }
    })
  })
}

function waitOnResult(callback) {
  while(!fs.existsSync('./Data_test/RESULT')) { }
  fs.readFile('./Data_test/RESULT', function (err, data) {
    if (err) {
      console.log('Error: ' + err)
    }
    data = String.fromCharCode.apply(null, new Uint16Array(data)).trim()
    console.log('Result is: ' + data)
    callback(data)
  })
}
<commit_msg>update<commit_after>"use strict"
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const spawn = require('child_process').spawn
const bodyParser = require('body-parser')
const mkdirp = require('mkdirp');
const fs = require('fs');

const clients = {}
const users = {
  'ms2666': 51,
  'fc249': 52
}

const script = spawn('python', ['test.py'])

script.stdout.on('data', (data) => {
  console.log('stdout: ' + data)
})

script.stderr.on('data', (data) => {
  console.log('stderr: ' + data)
})

script.on('close', (code) => {
  console.log('python script exited with code ' + code)
})

app.use(bodyParser.text({
  type: 'text/plain',
  limit: '4mb'
}))

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html')
})

app.post('/login/:id', (req, res) => {
  console.log('LOGIN ROUTE REACHED')
  // id is the client we want to connect to
  if (!(req.params.id in clients)) {
    return res.status(404).json({ message: 'Client with ID: ' + req.params.id + ' not found.'})
  }

  let id = req.params.id

  // plaintext request to log in based on this
  let dir = './Data_test'
  let path = 'u000_w000'
  let filename = 'u000_w000.accelerometer.log'

  console.log('Checking if RUNNING file exists')

  while(fs.existsSync('./Data_test/RUNNING')) {}

  console.log("No RUNNING file detected")

  writeToFile(dir, path, filename, req.body, function (err) {
    if (err) {
      return res.status(500).json({ message: 'Disaster! Internal Server Error' })
    }
    // Wait on the RESULT file
    waitOnResult(function(result) {
      authenticate(result, io, id)
      fs.unlinkSync('./Data_test/RESULT')
      return res.status(200).json({ attempted: true })
    })
  })
})

app.post('/train/:id', (req, res) => {
  if (!(req.params.id in users)) {
    return res.status(404).json({ message: 'User with ID: ' + req.params.id + ' not found.'})
  }

  console.log(req.params.id)
  console.log(users[req.params.id])
  formNextFileName(true, users[req.params.id], function (err, filename) {
    let dir = './Data'
    let path = filename
    // Let's get how many files there are...
    filename = filename + '_accelerometer.log'
    writeToFile(dir, path, filename, req.body, function (err) {
      if (err) {
        return res.status(500).json({ message: 'Disaster! Internal Server Error' })
      }
      return res.status(200).json({ message: 'Data posted!' })
    })
  })
})

io.on('connection', (socket) => {
  console.log('A client connected. Registering client...')
  registerClient(socket, (id) => {
    console.log('Client registered with id ' + id + '. Returning new client id.')
    // emit this id back to the client
    socket.emit('client_id', id)
  })

  // define the socket handlers
  socket.on('disconnect', () => {
    // how do we get an id
    // unregisterClient(socket, id)
  })

  socket.on('login', (id) => {
    console.log('Received login')
    setTimeout(function() {
      console.log('Doing work')
      attemptLogin(io, id)
    }, 0)
  })
})

app.use(express.static(__dirname))

http.listen(3000, () => {
  console.log('listening on *:3000')
})

function registerClient(socket, callback) {
  if (!callback || typeof callback !== "function") {
    throw new Error("registerClient(socket, callback) expected callback to be a function")
  }

  let id = 0
  do {
    id = Math.floor(Math.random() * 1000 + 1)
  } while (id in clients);

  // register this socket to a room with this name
  socket.join(id, () => {
    clients[id] = 'val'
    callback(id);
  })
}

function unregisterClient(socket, id) {
  // unregister this socket
  socket.leave(id, () => {
    delete clients[id]
  })
}

function authenticate(person, io, id) {
  console.log("Trying to authenticate person: " + person)
  if (person !== 'other') {
    io.to(id).emit('access', person)
  } else {
    io.to(id).emit('no-access')
  }
}

function formNextFileName(training, userId, callback) {
  if (!callback || typeof callback !== "function") {
    throw new Error("writeToFile(path, filename, contents, callback) expected callback to be a function")
  }
  // leftpad with 0's
  userId = zfill3(userId)
  if (training) {
    fs.readdir('./Data/', function(err, files) {
      if (err) {
        callback(err)
      }
      let walkId = files.length + 1
      walkId = zfill3(walkId)
      callback(null, 'u' + userId + '_w' + walkId)
    })
  } else {
    walkId = '000'
    callback(null, 'u' + userId + '_w' + walkId)
  }
}

function zfill3(num) {
  return ("000" + num).slice(-3)
}

function writeToFile(dir, path, filename, contents, callback) {
  if (!callback || typeof callback !== "function") {
    throw new Error("writeToFile(path, filename, contents, callback) expected callback to be a function")
  }
  mkdirp(dir + '/' + path, function (err) {
    fs.open(dir + '/' + path + '/' + filename,'r',function (err, fd) {
      if (err) {
        fs.writeFile(dir + '/' + path + '/' + filename, contents, function (err) {
            if(err) {
              callback(err)
            }
            console.log("File " + filename + " created!")
            callback(null)
        })
      } else {
        console.log("The file already exists!")
        callback(new Error("file already exists"))
      }
    })
  })
}

function waitOnResult(callback) {
  while(!fs.existsSync('./Data_test/RESULT')) { }
  fs.readFile('./Data_test/RESULT', function (err, data) {
    if (err) {
      console.log('Error: ' + err)
    }
    data = String.fromCharCode.apply(null, new Uint16Array(data)).trim()
    console.log('Result is: ' + data)
    callback(data)
  })
}
<|endoftext|>


<commit_before>var url = require('url');
var fs = require('fs');
var crypto = require('crypto');

var postcss = require('postcss');
var path = require('canonical-path');

var checksums = {};

module.exports = postcss.plugin('postcss-cachebuster', function (opts) {
  opts = opts || {};
  opts.imagesPath = opts.imagesPath || '';
  opts.type = opts.type || 'mtime';

  return function (css) {

    var inputFile = css.source.input.file;

    css.walkDecls(function(declaration){

      // only image and font related declarations
      if (declaration.prop !== 'background' &&
          declaration.prop !== 'background-image' &&
          declaration.prop !== 'border-image' &&
          declaration.prop !== 'src') {
        return;
      }

      // only url
      if (!/url\(('|")?[^'"\)]+('|")?\)/.test(declaration.value)) return;

      var background = /^(.*?)url\(('|")?([^'"\)]+)('|")?\)(.*?)$/.exec(declaration.value);
      var assetUrl = url.parse(background[3]);
      var assetPath = decodeURI(assetUrl.pathname);
      var inputPath = url.parse(inputFile);

      // only locals
      if (inputPath.host) return;
      if (assetUrl.pathname.indexOf('//') == 0) return;
      if (assetUrl.pathname.indexOf(';base64') !== -1) return;

      // resolve path
      if (/^\//.test(assetUrl.pathname)) {
        // absolute
        assetPath = path.normalize(process.cwd()+'/'+opts.imagesPath+'/'+assetPath)
      } else {
        // relative
        assetPath = path.dirname(inputFile)+'/'+assetPath;
        assetPath = path.normalize(assetPath);
      }

      // cachebuster
      var cachebuster = null;

      if (opts.type == 'checksum') {
        if (checksums[assetPath]) {
          cachebuster = checksums[assetPath];
        } else {
          var data = fs.readFileSync(assetPath).toString();
          cachebuster = crypto.createHash('md5')
            .update(data)
            .digest('hex');

          checksums[assetPath] = cachebuster;
        }

      } else {
        var mtime = fs.statSync(assetPath).mtime;
        cachebuster = mtime.getTime().toString(16);
      }

      // complete url with cachebuster
      if (assetUrl.search && assetUrl.search.length > 1) {
        assetUrl.search = assetUrl.search + '&v' + cachebuster;
      } else {
        assetUrl.search = '?v' + cachebuster;
      }

      // replace old value
      declaration.value = background[1]+"url('"+url.format(assetUrl)+"')"+background[5];

    })

  };
});
<commit_msg>Refactor to use global regex replace<commit_after>var url = require('url');
var fs = require('fs');
var crypto = require('crypto');

var postcss = require('postcss');
var path = require('canonical-path');

var checksums = {};

function createCachebuster(assetPath, type) {
  var cachebuster = null;

  if (type == 'checksum') {
    if (checksums[assetPath]) {
      cachebuster = checksums[assetPath];
    } else {
      var data = fs.readFileSync(assetPath).toString();
      cachebuster = crypto.createHash('md5')
        .update(data)
        .digest('hex');

      checksums[assetPath] = cachebuster;
    }

  } else {
    var mtime = fs.statSync(assetPath).mtime;
    cachebuster = mtime.getTime().toString(16);
  }

  return cachebuster
}

function resolveUrl(assetUrl, file, imagesPath) {
  var assetPath = decodeURI(assetUrl.pathname);

  if (/^\//.test(assetUrl.pathname)) {
    // absolute
    assetPath = path.join(process.cwd(), imagesPath, assetPath);
  } else {
    // relative
    assetPath = path.join(path.dirname(file), assetPath);
  }

  return assetPath;
}

module.exports = postcss.plugin('postcss-cachebuster', function (opts) {
  opts = opts || {};
  opts.imagesPath = opts.imagesPath || '';
  opts.type = opts.type || 'mtime';

  return function (css) {

    var inputFile = css.source.input.file;

    css.walkDecls(function(declaration){

      // only image and font related declarations
      if (declaration.prop !== 'background' &&
          declaration.prop !== 'background-image' &&
          declaration.prop !== 'border-image' &&
          declaration.prop !== 'src') {
        return;
      }

      var inputPath = url.parse(inputFile);
      var pattern = /url\(('|")?([^'"\)]+)('|")?\)/g;

      declaration.value = declaration.value.replace(pattern, function (match, quote, originalUrl) {
        var assetUrl = url.parse(originalUrl);
        quote = quote || '"';

        // only locals
        if (assetUrl.host ||
            assetUrl.pathname.indexOf('//') == 0 ||
            assetUrl.pathname.indexOf(';base64') !== -1) {
          return match;
        }

        var assetPath = resolveUrl(assetUrl, inputFile, opts.imagesPath);
        var cachebuster = createCachebuster(assetPath, opts.type);

        // complete url with cachebuster
        if (assetUrl.search && assetUrl.search.length > 1) {
          assetUrl.search = assetUrl.search + '&v' + cachebuster;
        } else {
          assetUrl.search = '?v' + cachebuster;
        }

        return 'url(' + quote + url.format(assetUrl) + quote + ')';
      });
    });

  };
});
<|endoftext|>


<commit_before>var _ = require('underscore');
var request = require('request');
var Url = require('url');

function Crawler(opts){

	this.thread = opts.thread || 5;
	this.headers = opts.headers || {};
	this.depth = opts.depth || 1;
	this.logs = opts.logs || false;
	this.onSuccess = opts.onSuccess;
	this.onError = opts.onError;
	this.onFinished = opts.onFinished;

	this.inQueue = [];
	this.crawledUrls = [];
	this.discoveredUrls = [];
	this.active = [];

	this.onlyCrawl = opts.onlyCrawl || [];
}

Crawler.prototype.log = function(status, url){
	if(this.logs)
		console.log('status : ', status, url);
}

Crawler.prototype.full = function(){
	return this.active.length >= this.thread;
}

Crawler.prototype.dequeue = function(){
	var next  = this.inQueue.shift();
	if(next){
		this.log('Dequeued : ', next.url);
		this.log("Loading url : ", next.url);
		this.load(next.url, next.depth);
	} else if(this.active.length == 0 && !next){
		this.log('Finished crawling :', '');

		if(this.onFinished){
			this.onFinished({crawled : this.crawledUrls, discovered : this.discoveredUrls});
		}

	}
}

Crawler.prototype.alreadyQueued = function(url){
	for (var i = 0; i < this.inQueue.length; i++) {
		if(this.inQueue[i].url == url){
			return true;
		}
	}
}

Crawler.prototype.alreadyCrawled = function(url){
	for (var i = 0; i < this.crawledUrls.length; i++) {
		if(this.crawledUrls[i] == url){
			return true;
		}

	};
}

Crawler.prototype.queue = function(url, depth){
	if(this.full() && !this.alreadyQueued(url) && !this.alreadyCrawled(url) && depth > 0){
		this.log('Adding url to queue : ', url);
		this.inQueue.push({url: url, depth : depth});
	} else if(this.alreadyCrawled(url)){
		this.log('Url already crawled : ', url );
	} else if(this.alreadyQueued(url) || depth < 1) {
		this.log('Url already exits or no depth :', url);
	} else {
		this.load(url, depth);
	}
}

Crawler.prototype.finished = function(url){
	var i = this.active.indexOf(url);
	this.active.splice(i, 1);

	if(!this.full()){
		this.dequeue();
	}
}

Crawler.prototype.getLinks = function(baseUrl, body){
	var regex = /<a[^>]+?href=".*?"/gm;
 	var links = body.match(regex);

 	links = _.map(links, function(link){
 		var match = /href=\"(.*?)[#\"]/.exec(link);

 		link = match[1];
 		link = Url.resolve(baseUrl, link);
 		return link;
 	});

 	return _.chain(links)
 		.filter(function(url){
 			if(this.onlyCrawl.length == 0)
 				return true;
 			else{
 				for (var i = 0; i < this.onlyCrawl.length; i++) {
 					if(url.indexOf(this.onlyCrawl[i]) > -1)
 						return true;
 				}
 			}
 		}.bind(this))
 		.uniq()
 		.value();

}

Crawler.prototype.load = function(url, depth) {
	this.log('Loading url : ', url);
	this.active.push(url);
	request({url : url,headers : this.headers}, function(error, response, body){
		if(!error && response.statusCode === 200){
			if(this.onSuccess){
				this.onSuccess({
					url : url,
					status : response.statusCode,
					content : body,
					response : response,
					body : body
				});
			}
			this.log('Url successfully loaded', url);
			this.crawledUrls.push(url);
		 	_.each(this.getLinks(url, body), function(link){
		 		this.discoveredUrls.push(link);
		 		this.queue(link, depth - 1);
		 	}.bind(this));

		}else if(error){
			this.log('Error occured', error);
			if(this.onError){
				this.onError({
					url : url,
					status : response.statusCode,
					error : error,
					content : body,
					response : response,
					body : body
				});
			}
		}

		this.finished(url);
	}.bind(this));
}

Crawler.prototype.crawl = function(url){
	this.log('Starting crawl :', url);
	// console.log(this.depth);
	this.queue(url, this.depth);

}

module.exports = Crawler;<commit_msg>Added Reject Url support + Better request handling<commit_after>var _ = require('underscore');
var request = require('request');
var Url = require('url');
var EventEmitter = require('events').EventEmitter;
var output = new EventEmitter();

function Crawler(opts){

	this.thread = opts.thread || 5;
	this.headers = opts.headers || {};
	this.depth = opts.depth || 1;
	this.logs = opts.logs || false;
	this.onSuccess = opts.onSuccess;
	this.onError = opts.onError;
	this.onFinished = opts.onFinished;

	this.inQueue = [];
	this.crawledUrls = [];
	this.discoveredUrls = [];
	this.active = [];

	this.onlyCrawl = opts.onlyCrawl || [];
	this.reject = opts.reject || [];
}

Crawler.prototype.log = function(status, url){
	if(this.logs)
		console.log('status : ', status, url);
}

Crawler.prototype.full = function(){
	return this.active.length >= this.thread;
}

Crawler.prototype.dequeue = function(){
	var next  = this.inQueue.shift();
	if(next){
		this.log('Dequeued : ', next.url);
		this.log("Loading url : ", next.url);
		this.load(next.url, next.depth);
	} else if(this.active.length == 0 && !next){
		this.log('Finished crawling :', '');

		if(this.onFinished){
			this.onFinished({crawled : this.crawledUrls, discovered : this.discoveredUrls});
		}

	}
}

Crawler.prototype.alreadyQueued = function(url){
	for (var i = 0; i < this.inQueue.length; i++) {
		if(this.inQueue[i].url == url){
			return true;
		}
	}
}

Crawler.prototype.alreadyCrawled = function(url){
	for (var i = 0; i < this.crawledUrls.length; i++) {
		if(this.crawledUrls[i] == url){
			return true;
		}

	};
}

Crawler.prototype.queue = function(url, depth){
	if(this.full() && !this.alreadyQueued(url) && !this.alreadyCrawled(url) && depth > 0){
		this.log('Adding url to queue : ', url);
		this.inQueue.push({url: url, depth : depth});
	} else if(this.alreadyCrawled(url)){
		this.log('Url already crawled : ', url );
	} else if(this.alreadyQueued(url) || depth < 1) {
		this.log('Url already exits or no depth :', url);
	} else {
		this.load(url, depth);
	}
}

Crawler.prototype.finished = function(url){
	var i = this.active.indexOf(url);
	this.active.splice(i, 1);
	// output.emit('crawlData', {crawled : this.crawledUrl})
	if(!this.full()){
		this.dequeue();
	}
}

Crawler.prototype.getLinks = function(baseUrl, body){
	var regex = /<a[^>]+?href=".*?"/gm;
 	var links = body.match(regex);

 	links = _.map(links, function(link){
 		var match = /href=\"(.*?)[#\"]/.exec(link);

 		link = match[1];
 		link = Url.resolve(baseUrl, link);
 		return link;
 	});

 	return _.chain(links)
 		.filter(function(url){
			if(this.onlyCrawl.length == 0){
 				return true;
			}
			else{
 				for (var i = 0; i < this.onlyCrawl.length; i++) {
 					if(url.indexOf(this.onlyCrawl[i]) > -1)
 						return true;
 				}
 			}
 		}.bind(this))
		.reject(function(url){
			if(this.reject.length > 0){
				for (var i = 0; i < this.reject.length; i++) {
 					if(url.indexOf(this.reject[i]) > -1)
 						return true;
 				}
			} else return false;
		}.bind(this))
 		.uniq()
 		.value();

}

Crawler.prototype.load = function(url, depth) {
	this.log('Loading url : ', url);
	this.active.push(url);

	try{
		request({url : url,headers : this.headers}, function(error, response, body){
			if(!error && response.statusCode === 200){
				if(this.onSuccess){
					this.onSuccess({
						url : url,
						status : response.statusCode,
						content : body,
						response : response,
						body : body
					});
				}
				this.log('Url successfully loaded', url);
				this.crawledUrls.push(url);
			 	_.each(this.getLinks(url, body), function(link){
			 		this.discoveredUrls.push(link);
			 		this.queue(link, depth - 1);
			 	}.bind(this));

			}else if(error){
				this.log('Error occured', error);
				if(this.onError && response){
					this.onError({
						url : url,
						status : response.statusCode || '400',
						error : error,
						content : body,
						response : response,
						body : body
					});
				}
			}
			this.finished(url);
		}.bind(this));
	}catch(e){
		this.log('Error occured', e.message);
		if(this.onError){
			this.onError({
				url : url,
				error : e.message
			});
		}
	}

}

Crawler.prototype.crawl = function(url){
	this.log('Starting crawl :', url);
	// console.log(this.depth);
	this.queue(url, this.depth);

}

module.exports = Crawler;
<|endoftext|>


<commit_before>/* globals process, __dirname */

// App Monitoring
require('newrelic');

// Modules
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var tracer = require('tracer').colorConsole({
	level: process.env.LOGLEVEL || 'info'
});
var _ = require('underscore');

// Libraries
var PlayerCollection = require('./lib/player_collection');
var Player = require('./lib/player');
var Game = require('./lib/game');
var Util = require('./lib/util');
var Insulter = require('./lib/insulter');
var TwitterOauth = require('./lib/twitter_oauth');

// Local
var defender, db, stats, emitTop10, emitTopCategories;
var players = new PlayerCollection();

io.set('logger', {
	debug: tracer.debug,
	info: tracer.info,
	warn: tracer.warn,
	error: tracer.error
});

// Monitoring
Util.initializeNodetime();

// Setup Express Server
app.use(express.static(__dirname + '/public'));
app.engine('jade', require('jade').__express);
app.set('views', __dirname + '/templates');
app.set('view engine', 'jade');

// Express Routes
app.get('/', function(req, res) {
	res.render('index', {
		host: process.env.CLIENT || 'http://localhost:8080',
		secure: !!process.env.SECURECLIENT
	});
});

emitTop10 = function() {
	// Read top 10 games and emit to stats channel
	Game.topScoreList(db, function(err, results) {
		if (err) {
			tracer.error(err);
			tracer.warn('top10 not transmitted.');
			return;
		}
		tracer.info({
			top10: results
		});
		stats.emit('top10', results);
	});
};

emitTopCategories = function() {
	Game.topCategoryList(db, function (err, results) {
		if (err) {
			tracer.error(err);
			tracer.warn('topCategories not transmitted.');
			return;
		}
		tracer.info({
			topCategories: results
		});
		stats.emit('topCategories', results);
	});
}

// Initiate the mongo connection
Util.mongoConnect(function(err, connection) {
	if (err) {
		tracer.error(err);
		tracer.warn('Mongo DB connection failed, no game results will be recorded.');
		return;
	}
	db = connection;
});

// Stat channel (for scoreboard)
server.listen(parseInt(process.env.PORT) || 8080);
stats = io
	.of('/stats')
	.on('connection', function() {
		emitTop10();
		emitTopCategories();
		// Get the player list and transmit
		stats.emit('playerRefresh', players.list());
	});

// Setup player collection listener
players.onCollectionUpdate(function(playerCollection) {
	stats.emit('playerRefresh', playerCollection.list());
});

// Node defender main channel
defender = io
	.of('/defender')
	.authorization(function(handshake, callback) {
		if (typeof handshake.query.username === 'undefined' && handshake.query.username.length > 0) {
			tracer.info('Username required for authorization.');
			return callback('unauthorized', false);
		}
		if (!/^[\w\-\(\) ]+$/.test(handshake.query.username)) {
			tracer.info('Invalid Username: invalid characters.');
			return callback('unauthorized', false);
		}
		if (handshake.query.username.length > 30) {
			tracer.info('Invalid Username: too long.');
			return callback('unauthorized', false);
		}
		if (players.byName(handshake.query.username)) {
			tracer.info('Username already registered playing a session.');
			return callback('unauthorized', false);
		}
		if (!!handshake.query.token && !!handshake.query.secret) {
			handshake.player = new Player(handshake.query.username, handshake.query.token, handshake.query.secret);
			handshake.player.validateTwitter(db, function(error, isValid) {
				if (error) {
					tracer.error(error);
					callback('unauthorized', false);
				}
				if (players.byName(handshake.player.name())) {
					tracer.info('Twitter user already registered playing a session.');
					return callback('unauthorized', false);
				}
				callback(isValid ? null : 'unauthorized', isValid);
			});
			return;
		}
		tracer.info('Authorizing anonymous player.');
		handshake.player = new Player(handshake.query.username);
		return callback(null, true);
	})
	.on('connection', function(socket) {
		var player = socket.handshake.player,
			game = new Game(player),
			gameEnded = false,
			roundResponseTimeout;
		players.add(player);

		// Handle the player's demise
		player.on('death', function(player) {
			// Stop the rounds
			clearTimeout(roundResponseTimeout);
			gameEnded = true;

			// Notify client of final player stats
			socket.emit('death', {
				'message': player.name().toUpperCase() + ' IS NOT IN MY BASE, KILLING MY DUDES ANYMORE.',
				'stats': _.extend(player.info(), {score: game.calculateScore()})
			});

			// Notify channel of player demise
			socket.broadcast.emit('mandown', {
				'message': player.name().toUpperCase() + ' IS SWIMMING WITH DA\' FISHES.'
			});

			// Kick the client
			socket.emit('disconnect', {});

			// Record player stats to DB
			game.recordGame(db, function (err) {
				if (err) {
					tracer.error(err);
					tracer.warn('Player stats not saved.');
					return;
				}

				emitTop10();
				emitTopCategories();
			});
		});

		// Welcome message
		socket.emit('handshake', {
			'message': 'Welcome ' + player.name() + '. prepare to be attacked!'
		});

		// Recieve action commands from the player
		socket.on('action', function(data) {
			var playerAttacks, enemyActions, enemy;
			if (gameEnded || !game.validateRoundToken(data.roundToken)) {
				return;
			}
			game.consumeRoundToken(data.roundToken);

			// Process player action first, then mob action and spawning
			player.attackMode(data.attack_mode);
			enemy = game.getEnemyById(data.target);
			if (enemy){
				playerAttacks = player.attackEnemy(enemy.enemy, game.waves);
			}

			enemyActions = game.processEnemyActions(player);

			game.setupRound();

			// Add a delay to not make the game instant
			roundResponseTimeout = setTimeout(function() {
				socket.emit('round', {
					player: player.info(),
					insult: Insulter.getAny(),
					round: game.getRound(),
					roundToken: game.getRoundToken(),
					attacks : playerAttacks,
					enemyActions : enemyActions,
					summary: game.summary(),
					mobs: game.getEnemies()
				});
			}, process.env.DELAY || 1000);
		});

		socket.on('disconnect', function() {
			players.remove(player.name());
		});

		// Initial round
		game.spawnEnemies();
		socket.emit('round', {
			player: player.info(),
			insult: null,
			round: game.getRound(),
			roundToken: game.getRoundToken(),
			attacks : null,
			enemyActions : null,
			summary: game.summary(),
			mobs: game.getEnemies()
		});
	});
<commit_msg>Implemented redis store for socket.io<commit_after>/* globals process, __dirname */

// App Monitoring
require('newrelic');

// Modules
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var tracer = require('tracer').colorConsole({
	level: process.env.LOGLEVEL || 'info'
});
var _ = require('underscore');
var RedisStore = require('socket.io/lib/stores/redis');
var redis = require('socket.io/node_modules/redis');
var async = require('async');

// Libraries
var PlayerCollection = require('./lib/player_collection');
var Player = require('./lib/player');
var Game = require('./lib/game');
var Util = require('./lib/util');
var Insulter = require('./lib/insulter');
var TwitterOauth = require('./lib/twitter_oauth');

// Local
var defender, db, stats, emitTop10, emitTopCategories;
var redisSocket;
var players = new PlayerCollection();

io.set('logger', {
	debug: tracer.debug,
	info: tracer.info,
	warn: tracer.warn,
	error: tracer.error
});
if (process.env.REDIS_HOST && process.env.REDIS_PORT) {
	redisSocket = {
		redis: redis,
		redisPub: redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST),
		redisSub: redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST),
		redisClient: redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST)
	}
	if (process.env.REDIS_AUTH) {
		async.map(
			['redisPub', 'redisSub', 'redisClient'],
			_.bind(function(redisClient, callback) {
				redisSocket[redisClient].auth(process.env.REDIS_AUTH, callback);
			}, redisSocket),
			function(err) {
				if (err) {
					tracer.error('Redis unable to authenticate.');
				}
				io.set('store', new RedisStore(redisSocket));
			}
		);
	} else {
		io.set('store', new RedisStore(redisSocket));
	}
} else {
	tracer.warn('Redis socket.io store not engaged - Using MemoryStore.');
}

// Monitoring
Util.initializeNodetime();

// Setup Express Server
app.use(express.static(__dirname + '/public'));
app.engine('jade', require('jade').__express);
app.set('views', __dirname + '/templates');
app.set('view engine', 'jade');

// Express Routes
app.get('/', function(req, res) {
	res.render('index', {
		host: process.env.CLIENT || 'http://localhost:8080',
		secure: !!process.env.SECURECLIENT
	});
});

emitTop10 = function() {
	// Read top 10 games and emit to stats channel
	Game.topScoreList(db, function(err, results) {
		if (err) {
			tracer.error(err);
			tracer.warn('top10 not transmitted.');
			return;
		}
		tracer.info({
			top10: results
		});
		stats.emit('top10', results);
	});
};

emitTopCategories = function() {
	Game.topCategoryList(db, function (err, results) {
		if (err) {
			tracer.error(err);
			tracer.warn('topCategories not transmitted.');
			return;
		}
		tracer.info({
			topCategories: results
		});
		stats.emit('topCategories', results);
	});
}

// Initiate the mongo connection
Util.mongoConnect(function(err, connection) {
	if (err) {
		tracer.error(err);
		tracer.warn('Mongo DB connection failed, no game results will be recorded.');
		return;
	}
	db = connection;
});

// Stat channel (for scoreboard)
server.listen(parseInt(process.env.PORT) || 8080);
stats = io
	.of('/stats')
	.on('connection', function() {
		emitTop10();
		emitTopCategories();
		// Get the player list and transmit
		stats.emit('playerRefresh', players.list());
	});

// Setup player collection listener
players.onCollectionUpdate(function(playerCollection) {
	stats.emit('playerRefresh', playerCollection.list());
});

// Node defender main channel
defender = io
	.of('/defender')
	.authorization(function(handshake, callback) {
		if (typeof handshake.query.username === 'undefined' && handshake.query.username.length > 0) {
			tracer.info('Username required for authorization.');
			return callback('unauthorized', false);
		}
		if (!/^[\w\-\(\) ]+$/.test(handshake.query.username)) {
			tracer.info('Invalid Username: invalid characters.');
			return callback('unauthorized', false);
		}
		if (handshake.query.username.length > 30) {
			tracer.info('Invalid Username: too long.');
			return callback('unauthorized', false);
		}
		if (players.byName(handshake.query.username)) {
			tracer.info('Username already registered playing a session.');
			return callback('unauthorized', false);
		}
		if (!!handshake.query.token && !!handshake.query.secret) {
			handshake.player = new Player(handshake.query.username, handshake.query.token, handshake.query.secret);
			handshake.player.validateTwitter(db, function(error, isValid) {
				if (error) {
					tracer.error(error);
					callback('unauthorized', false);
				}
				if (players.byName(handshake.player.name())) {
					tracer.info('Twitter user already registered playing a session.');
					return callback('unauthorized', false);
				}
				callback(isValid ? null : 'unauthorized', isValid);
			});
			return;
		}
		tracer.info('Authorizing anonymous player.');
		handshake.player = new Player(handshake.query.username);
		return callback(null, true);
	})
	.on('connection', function(socket) {
		var player = socket.handshake.player,
			game = new Game(player),
			gameEnded = false,
			roundResponseTimeout;
		players.add(player);

		// Handle the player's demise
		player.on('death', function(player) {
			// Stop the rounds
			clearTimeout(roundResponseTimeout);
			gameEnded = true;

			// Notify client of final player stats
			socket.emit('death', {
				'message': player.name().toUpperCase() + ' IS NOT IN MY BASE, KILLING MY DUDES ANYMORE.',
				'stats': _.extend(player.info(), {score: game.calculateScore()})
			});

			// Notify channel of player demise
			socket.broadcast.emit('mandown', {
				'message': player.name().toUpperCase() + ' IS SWIMMING WITH DA\' FISHES.'
			});

			// Kick the client
			socket.emit('disconnect', {});

			// Record player stats to DB
			game.recordGame(db, function (err) {
				if (err) {
					tracer.error(err);
					tracer.warn('Player stats not saved.');
					return;
				}

				emitTop10();
				emitTopCategories();
			});
		});

		// Welcome message
		socket.emit('handshake', {
			'message': 'Welcome ' + player.name() + '. prepare to be attacked!'
		});

		// Recieve action commands from the player
		socket.on('action', function(data) {
			var playerAttacks, enemyActions, enemy;
			if (gameEnded || !game.validateRoundToken(data.roundToken)) {
				return;
			}
			game.consumeRoundToken(data.roundToken);

			// Process player action first, then mob action and spawning
			player.attackMode(data.attack_mode);
			enemy = game.getEnemyById(data.target);
			if (enemy){
				playerAttacks = player.attackEnemy(enemy.enemy, game.waves);
			}

			enemyActions = game.processEnemyActions(player);

			game.setupRound();

			// Add a delay to not make the game instant
			roundResponseTimeout = setTimeout(function() {
				socket.emit('round', {
					player: player.info(),
					insult: Insulter.getAny(),
					round: game.getRound(),
					roundToken: game.getRoundToken(),
					attacks : playerAttacks,
					enemyActions : enemyActions,
					summary: game.summary(),
					mobs: game.getEnemies()
				});
			}, process.env.DELAY || 1000);
		});

		socket.on('disconnect', function() {
			players.remove(player.name());
		});

		// Initial round
		game.spawnEnemies();
		socket.emit('round', {
			player: player.info(),
			insult: null,
			round: game.getRound(),
			roundToken: game.getRoundToken(),
			attacks : null,
			enemyActions : null,
			summary: game.summary(),
			mobs: game.getEnemies()
		});
	});
<|endoftext|>


<commit_before>var CrbotReporter = function(helper, logger) {
  this.write = process.stdout.write.bind(process.stdout);

  this.onBrowserRegister = function(browser) {
    this.write('@@@SEED_STEP ' + browser.name + '@@@\n');
    this.write('@@@STEP_CURSOR ' + browser.name + '@@@\n');
    this.write('@@@STEP_STARTED @@@\n');
  };

  this.onBrowserComplete = function(browser) {
    this.write('@@@STEP_CURSOR ' + browser.name + '@@@\n');
    var results = browser.lastResult;
    if (results.failed || results.error) {
      this.write('@@@STEP_FAILURE @@@\n');
    }
    this.write('@@@STEP_CLOSED @@@\n');
  };

  this.onBrowserError = function(browser, error) {
    this.write(browser.name + ' ERROR\n');
    this.write('\t' + error.replace('\n', '\n\t') + '\n');
  };
};

module.exports = {
  'reporter:crbot': ['type', CrbotReporter]
};
<commit_msg>add a step cursor before a log line<commit_after>var CrbotReporter = function(helper, logger) {
  this.write = process.stdout.write.bind(process.stdout);

  this.onBrowserRegister = function(browser) {
    this.write('@@@SEED_STEP ' + browser.name + '@@@\n');
    this.write('@@@STEP_CURSOR ' + browser.name + '@@@\n');
    this.write('@@@STEP_STARTED @@@\n');
  };

  this.onBrowserComplete = function(browser) {
    this.write('@@@STEP_CURSOR ' + browser.name + '@@@\n');
    var results = browser.lastResult;
    if (results.failed || results.error) {
      this.write('@@@STEP_FAILURE @@@\n');
    }
    this.write('@@@STEP_CLOSED @@@\n');
  };

  this.onBrowserError = function(browser, error) {
    this.write('@@@STEP_CURSOR ' + browser.name + '@@@\n');
    this.write(browser.name + ' ERROR\n');
    this.write('\t' + error.replace('\n', '\n\t') + '\n');
  };
};

module.exports = {
  'reporter:crbot': ['type', CrbotReporter]
};
<|endoftext|>


<commit_before>import path from 'path';
import NLP from 'stanford-corenlp';
import _ from 'lodash';

const coreNLP = new NLP.StanfordNLP({
  nlpPath: path.join(__dirname, '../../vendor/corenlp/corenlp'),
  version: '3.5.2',
  annotators: ['tokenize', 'ssplit'],
});

function processWithCoreNLP(text) {
  return new Promise((resolve, reject) => {
    coreNLP.process(text, (err, result) => {
      /* istanbul ignore if */
      if (err) {
        return reject(err);
      }
      return resolve(result);
    });
  });
}

export default async function (text) {
  const emended = text
    // Remove unnecessary line breaks.
    .replace(/[\n\r]+/g, ' ')

    // Remove soft hyphens.
    .replace(/(\w)-\s+(\w)/g, '$1$2')

    // Replace some full-width signs with their half-width ones.
    .replace(/(?:‘|’)/g, '\'')
    .replace(/(?:“|”)/g, '"')
    .replace(/—/g, '--');

  const result = await processWithCoreNLP(emended);
  const sentences = [];

  if (Array.isArray(result.document.sentences.sentence)) {
    for (const sentenceData of result.document.sentences.sentence) {
      const begin = _.first(sentenceData.tokens.token).CharacterOffsetBegin;
      const end = _.last(sentenceData.tokens.token).CharacterOffsetEnd;

      sentences.push(emended.slice(begin, end));
    }

    return sentences.join('\n');
  }

  return emended;
}
<commit_msg>Fix a sentence couldn't be preprocessed when it contained only one token<commit_after>import path from 'path';
import NLP from 'stanford-corenlp';
import _ from 'lodash';

const coreNLP = new NLP.StanfordNLP({
  nlpPath: path.join(__dirname, '../../vendor/corenlp/corenlp'),
  version: '3.5.2',
  annotators: ['tokenize', 'ssplit'],
});

function processWithCoreNLP(text) {
  return new Promise((resolve, reject) => {
    coreNLP.process(text, (err, result) => {
      /* istanbul ignore if */
      if (err) {
        return reject(err);
      }
      return resolve(result);
    });
  });
}

export default async function (text) {
  const emended = text
    // Remove unnecessary line breaks.
    .replace(/[\n\r]+/g, ' ')

    // Remove soft hyphens.
    .replace(/(\w)-\s+(\w)/g, '$1$2')

    // Replace some full-width signs with their half-width ones.
    .replace(/(?:‘|’)/g, '\'')
    .replace(/(?:“|”)/g, '"')
    .replace(/—/g, '--');

  const result = await processWithCoreNLP(emended);
  const sentences = [];

  // there is just one sentence and don't need to split it!
  if (!Array.isArray(result.document.sentences.sentence)) {
    return emended;
  }

  for (const sentenceData of result.document.sentences.sentence) {
    const coreNLPToken = sentenceData.tokens.token;
    const tokens = Array.isArray(coreNLPToken) ? coreNLPToken : [coreNLPToken];
    const begin = _.first(tokens).CharacterOffsetBegin;
    const end = _.last(tokens).CharacterOffsetEnd;

    sentences.push(emended.slice(begin, end));
  }

  return sentences.join('\n');
}
<|endoftext|>


<commit_before>var defaultOptions = require("./defaultOptions");
var path = require("path");
var parser = require("./internal/parser");

// Get relative path to dependency inside dojo-webpack-loader
function loaderLibDependency(module, dep_str){
    return path.relative(path.dirname(module.resourcePath), path.join(__dirname, dep_str));
}

// Change content of module on load
function preprocessModule(module, options, content){
    var register_in_dojo_require = module.isNls && module.normalizedName;

    switch (module.normalizedName) {
        // for dojo/has - inject staticHasFeatures
        case 'dojo/has':
        {
            // replace first line (for dojo 1.10)
            var line_pos = content.search(/\r|\n/);
            if (line_pos < 0) throw new Error("DojoWebpackLoader: dojo/has assumed to be multiline");
            var incjected_has_features = JSON.stringify(options.staticHasFeatures);
            content = 'define(["require"], function(require){' +
                'var module = { config: function(){ return ' + incjected_has_features + '; } };' +
                content.substr(line_pos);
            break;
        }
    }
    if (module.isDijit){
        // for dijit/* - cut legacy dynamic loading functionality
        var cut_line = 'if(has("dijit-legacy-requires")){';
        var cut_index = content.indexOf(cut_line);
        if (cut_index >= 0) {
            var end_index = parser.getCodeBlockEnd(content, cut_index + cut_line.length - 1);
            content = content.substr(0, cut_index) + content.substr(end_index + 1);
        }
        if (module.normalizedName == 'dijit/form/ComboBoxMixin'){
            cut_index = content.indexOf("require(shim");
            if (cut_index >= 0){
                var end_index = parser.getCodeBlockEnd(content, cut_index + "require".length, "()");
                content = content.substr(0, cut_index) + " throw new Error('DojoWebpackLoader: dynamic loading is not supported') " + content.substr(end_index + 1);
            }
        }
        else if (module.normalizedName == "dijit/_AttachMixin"){
            // Add "./a11yclick" dependency to prevent dynamic loading
            module.inject.dependencies.push({dep: "./a11yclick", name: "a11yclick"});
            content = content.replace("var a11yclick;", '');
        }
        register_in_dojo_require = register_in_dojo_require || options.dojoParserDijitWidgets;
    }

    if (register_in_dojo_require){
        // Inject
        // 1. dojo-webpack-loader/lib/dojo-require dependency
        module.inject.dependencies.push({dep: loaderLibDependency(module, './lib/dojo-require'), name: "dojoWebpackLoaderRequire"});
        // 2. register module in dojo-webpack-loader/lib/dojo-require
        module.inject.prepend = "return dojoWebpackLoaderRequire.register(" +
            JSON.stringify(module.normalizedName) +
            ", (function(){";
        module.inject.append = "})());";
    }

    return content;
}

// transform relative dependencies to dojo/* for dojo core module
function resolveCoreModuleDependency(package_name, core_path, resource, dep_str){
    if (!dep_str || dep_str[0] != '.') return dep_str;
    if (dep_str == '.') dep_str = path.basename(resource, path.extname(resource));
    // for relative paths only:
    var res = path.resolve(path.dirname(resource), dep_str).substr(core_path.length).replace(/\\/g, '/');
    if (res[0] != '/') res = '/' + res[0];
    return package_name + res;
}

// Normalize module paths (resolve relative paths in dojo core module)
function normalizeDependency(module, options, dep){
    if (module.isCore){
        // resource from core module
        var loaders = dep.loaders.map(function(m){ return resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, m); });
        return {
            loaders: loaders,
            main: resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, dep.main),
            conditionTrue: resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, dep.conditionTrue),
            conditionFalse: resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, dep.conditionFalse)
        }
    }
    return dep;
}

// Check value of has feature in options.staticHasFeatures
function checkHasFeature(options, feature){
    return options.staticHasFeatures[feature];
}

// Method to get module name from engine id
function dojoSelectorEngineModule(options, engine_id){
    engine_id = engine_id == "default" ? checkHasFeature(options, "config-selectorEngine") || "css3" : engine_id;
    switch (engine_id){
        case 'acme':
        case 'lite':
            return engine_id;
        case 'css2':
            return checkHasFeature(options, "dom-qsa3") ? 'lite' : 'acme';
        case 'css2.1':
            return checkHasFeature(options, "dom-qsa2.1") ? 'lite' : 'acme';
    }
    return 'lite';
}

function mapDependency(module, options, dep){
    // console.log(dep.dep, dep.name);
    var dep = parser.parseDependency(dep);
    var norm_dep = normalizeDependency(module, options, dep);
    var result_module = dep.main;
    var result_loaders = [];
    for (var i = 0; i < norm_dep.loaders.length; i++){
        switch (norm_dep.loaders[i]){
            case 'dojo/has':
                // resolve resource by dojoWebpackLoader.staticHasFeatures
                var condition = dep.condition;
                while (true){
                    var branch = options.staticHasFeatures[condition.ifTest] ? condition.ifTrue : condition.ifFalse;
                    if (typeof branch == 'object') condition = branch;
                    else {
                        result_module = branch;
                        break;
                    }
                }
                break;
            case 'dojo/node':
                // assume modules will be loaded by webpack correctly
                result_module = dep.main;
                break;
            case 'dojo/ready':
                // Sorry, not supported, just ignoring
                break;
            case 'dojo/selector/_loader':
            case 'dojo/query':
                // Use selector engine specified in options
                result_module = 'dojo/selector/' + dojoSelectorEngineModule(options, options.selectorEngine);
                break;
            case 'dojo/request/default':
                // use module by has features
                if(checkHasFeature(options, 'config-requestProvider')){
                    result_module = checkHasFeature(options, 'config-requestProvider')
                }else if(checkHasFeature(options, 'host-browser') || checkHasFeature(options, 'host-webworker')){
                    result_module = "dojo/request/xhr";
                }else if(checkHasFeature(options, 'host-node')) {
                    result_module = "dojo/request/node";
                }
                else throw new Error('DojoWebpackLoader: dojo/request/default cannot choose loader');
                break;
            case 'dojo/text':
                // use webpack raw-loader instead of dojo/text
                result_loaders.push("raw-loader");
                break;
            case 'dojo/i18n':
                // Will be loaded via DojoWebpackLoader
                break;
            default:
                debugger;
                break;
        }
    }

    if (norm_dep.main == 'dojo/dojo') result_module = 'dojo/main';
    else if (norm_dep.main == 'require') result_module = loaderLibDependency(module, './lib/dojo-require');
    else if (norm_dep.main == 'module'){
        module.inject.prepend += dep.name + "={id:" + JSON.stringify(module.normalizedName ? module.normalizedName : '') + "}";
        result_module = norm_dep.main;
    }
    if (!result_module) result_module = loaderLibDependency(module, './lib/no-module');

    return (result_loaders.length ? result_loaders.join("!") + "!" : "") + result_module;
}

// Set map of
function processNlsModule(module, parsed, options){
    try {
        var f = new Function("'using strict'; return " + parsed.moduleBody + ";");
        var nls_descr = f();
        if (nls_descr && nls_descr.root){
            var res_nls = {
                root: nls_descr.root
            };
            if (options.includeLanguages){
                options.includeLanguages.forEach(function(lang){
                   if (nls_descr[lang]){
                       module.inject.dependencies.push({
                           dep: "./" + lang + "/" + path.basename(module.resourcePath, ".js"),
                           name: "lang_" + lang.replace(/-/g, "_")
                       });
                       res_nls[lang] = true;
                   }
                });
            }
            parsed.moduleBody = JSON.stringify(res_nls);
        }
    }
    catch (err) {
        throw new Error("DojoWebpackLoader cannot load nls module " + module.resourcePath + ". Error" + err.message);
    }
}

function DojoWebpackLoader(content){
    if (this.cacheable) this.cacheable();
    // console.log("-----------------------")
    // console.log(this.resourcePath);

    // Prepare options
    var dojoWebpackLoaderOptions = this.options.dojoWebpackLoader;
    var options = Object.assign({}, defaultOptions, dojoWebpackLoaderOptions);
    options.staticHasFeatures = Object.assign({}, defaultOptions.staticHasFeatures, dojoWebpackLoaderOptions.staticHasFeatures);

    if (!options.dojoCorePath) throw new Error("DojoWebpackLoader: dojoWebpackLoader.dojoCorePath option is not set");

    // Prepare module
    var module = {
        isCore: this.resourcePath.substr(0, options.dojoCorePath.length) == options.dojoCorePath,
        isDijit: options.dojoDijitPath ? this.resourcePath.substr(0, options.dojoDijitPath.length) == options.dojoDijitPath : false,
        isNls: this.resourcePath.match(/nls[\\\/](.*?[\\\/])?.*$/) != null,
        resourcePath: this.resourcePath,
        normalizedName: null,
        inject: {
            // dependencies which will be injected to module additionally
            // [ { dep: "module", name: "var name in module def"} ... ]
            dependencies: [],
            // Add js code at beginning of module
            prepend: "",
            // Add js code at ending of module
            append: ""
        }
    };
    if (module.isCore) module.normalizedName = resolveCoreModuleDependency('dojo', options.dojoCorePath, this.resourcePath, '.');
    else if (module.isDijit) module.normalizedName = resolveCoreModuleDependency('dijit', options.dojoDijitPath, this.resourcePath, '.');

    // Parse module
    var content = preprocessModule(module, options, content);
    var parsed = parser.parseModule(content);
    var new_deps = [];
    if (!module.isNls) {
        new_deps = parsed.deps.map(function (dep) {
            return mapDependency(module, options, dep);
        });
    }
    else {
        processNlsModule(module, parsed, options);
    }
    return parsed.save(new_deps, module.inject);
}

module.exports = DojoWebpackLoader;
<commit_msg>Update index.js<commit_after>var defaultOptions = require("./defaultOptions"),
    loaderUtils = require("loader-utils"),
    path = require("path"),
    parser = require("./internal/parser");

// Get relative path to dependency inside dojo-webpack-loader
function loaderLibDependency(module, dep_str){
    return path.relative(path.dirname(module.resourcePath), path.join(__dirname, dep_str));
}

// Change content of module on load
function preprocessModule(module, options, content){
    var register_in_dojo_require = module.isNls && module.normalizedName;

    switch (module.normalizedName) {
        // for dojo/has - inject staticHasFeatures
        case 'dojo/has':
        {
            // replace first line (for dojo 1.10)
            var line_pos = content.search(/\r|\n/);
            if (line_pos < 0) throw new Error("DojoWebpackLoader: dojo/has assumed to be multiline");
            var incjected_has_features = JSON.stringify(options.staticHasFeatures);
            content = 'define(["require"], function(require){' +
                'var module = { config: function(){ return ' + incjected_has_features + '; } };' +
                content.substr(line_pos);
            break;
        }
    }
    if (module.isDijit){
        // for dijit/* - cut legacy dynamic loading functionality
        var cut_line = 'if(has("dijit-legacy-requires")){';
        var cut_index = content.indexOf(cut_line);
        if (cut_index >= 0) {
            var end_index = parser.getCodeBlockEnd(content, cut_index + cut_line.length - 1);
            content = content.substr(0, cut_index) + content.substr(end_index + 1);
        }
        if (module.normalizedName == 'dijit/form/ComboBoxMixin'){
            cut_index = content.indexOf("require(shim");
            if (cut_index >= 0){
                var end_index = parser.getCodeBlockEnd(content, cut_index + "require".length, "()");
                content = content.substr(0, cut_index) + " throw new Error('DojoWebpackLoader: dynamic loading is not supported') " + content.substr(end_index + 1);
            }
        }
        else if (module.normalizedName == "dijit/_AttachMixin"){
            // Add "./a11yclick" dependency to prevent dynamic loading
            module.inject.dependencies.push({dep: "./a11yclick", name: "a11yclick"});
            content = content.replace("var a11yclick;", '');
        }
        register_in_dojo_require = register_in_dojo_require || options.dojoParserDijitWidgets;
    }

    if (register_in_dojo_require){
        // Inject
        // 1. dojo-webpack-loader/lib/dojo-require dependency
        module.inject.dependencies.push({dep: loaderLibDependency(module, './lib/dojo-require'), name: "dojoWebpackLoaderRequire"});
        // 2. register module in dojo-webpack-loader/lib/dojo-require
        module.inject.prepend = "return dojoWebpackLoaderRequire.register(" +
            JSON.stringify(module.normalizedName) +
            ", (function(){";
        module.inject.append = "})());";
    }

    return content;
}

// transform relative dependencies to dojo/* for dojo core module
function resolveCoreModuleDependency(package_name, core_path, resource, dep_str){
    if (!dep_str || dep_str[0] != '.') return dep_str;
    if (dep_str == '.') dep_str = path.basename(resource, path.extname(resource));
    // for relative paths only:
    var res = path.resolve(path.dirname(resource), dep_str).substr(core_path.length).replace(/\\/g, '/');
    if (res[0] != '/') res = '/' + res[0];
    return package_name + res;
}

// Normalize module paths (resolve relative paths in dojo core module)
function normalizeDependency(module, options, dep){
    if (module.isCore){
        // resource from core module
        var loaders = dep.loaders.map(function(m){ return resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, m); });
        return {
            loaders: loaders,
            main: resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, dep.main),
            conditionTrue: resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, dep.conditionTrue),
            conditionFalse: resolveCoreModuleDependency('dojo', options.dojoCorePath, module.resourcePath, dep.conditionFalse)
        }
    }
    return dep;
}

// Check value of has feature in options.staticHasFeatures
function checkHasFeature(options, feature){
    return options.staticHasFeatures[feature];
}

// Method to get module name from engine id
function dojoSelectorEngineModule(options, engine_id){
    engine_id = engine_id == "default" ? checkHasFeature(options, "config-selectorEngine") || "css3" : engine_id;
    switch (engine_id){
        case 'acme':
        case 'lite':
            return engine_id;
        case 'css2':
            return checkHasFeature(options, "dom-qsa3") ? 'lite' : 'acme';
        case 'css2.1':
            return checkHasFeature(options, "dom-qsa2.1") ? 'lite' : 'acme';
    }
    return 'lite';
}

function mapDependency(module, options, dep){
    // console.log(dep.dep, dep.name);
    var dep = parser.parseDependency(dep);
    var norm_dep = normalizeDependency(module, options, dep);
    var result_module = dep.main;
    var result_loaders = [];
    for (var i = 0; i < norm_dep.loaders.length; i++){
        switch (norm_dep.loaders[i]){
            case 'dojo/has':
                // resolve resource by dojoWebpackLoader.staticHasFeatures
                var condition = dep.condition;
                while (true){
                    var branch = options.staticHasFeatures[condition.ifTest] ? condition.ifTrue : condition.ifFalse;
                    if (typeof branch == 'object') condition = branch;
                    else {
                        result_module = branch;
                        break;
                    }
                }
                break;
            case 'dojo/node':
                // assume modules will be loaded by webpack correctly
                result_module = dep.main;
                break;
            case 'dojo/ready':
                // Sorry, not supported, just ignoring
                break;
            case 'dojo/selector/_loader':
            case 'dojo/query':
                // Use selector engine specified in options
                result_module = 'dojo/selector/' + dojoSelectorEngineModule(options, options.selectorEngine);
                break;
            case 'dojo/request/default':
                // use module by has features
                if(checkHasFeature(options, 'config-requestProvider')){
                    result_module = checkHasFeature(options, 'config-requestProvider')
                }else if(checkHasFeature(options, 'host-browser') || checkHasFeature(options, 'host-webworker')){
                    result_module = "dojo/request/xhr";
                }else if(checkHasFeature(options, 'host-node')) {
                    result_module = "dojo/request/node";
                }
                else throw new Error('DojoWebpackLoader: dojo/request/default cannot choose loader');
                break;
            case 'dojo/text':
                // use webpack raw-loader instead of dojo/text
                result_loaders.push("raw-loader");
                break;
            case 'dojo/i18n':
                // Will be loaded via DojoWebpackLoader
                break;
            default:
                debugger;
                break;
        }
    }

    if (norm_dep.main == 'dojo/dojo') result_module = 'dojo/main';
    else if (norm_dep.main == 'require') result_module = loaderLibDependency(module, './lib/dojo-require');
    else if (norm_dep.main == 'module'){
        module.inject.prepend += dep.name + "={id:" + JSON.stringify(module.normalizedName ? module.normalizedName : '') + "}";
        result_module = norm_dep.main;
    }
    if (!result_module) result_module = loaderLibDependency(module, './lib/no-module');

    return (result_loaders.length ? result_loaders.join("!") + "!" : "") + result_module;
}

// Set map of
function processNlsModule(module, parsed, options){
    try {
        var f = new Function("'using strict'; return " + parsed.moduleBody + ";");
        var nls_descr = f();
        if (nls_descr && nls_descr.root){
            var res_nls = {
                root: nls_descr.root
            };
            if (options.includeLanguages){
                options.includeLanguages.forEach(function(lang){
                   if (nls_descr[lang]){
                       module.inject.dependencies.push({
                           dep: "./" + lang + "/" + path.basename(module.resourcePath, ".js"),
                           name: "lang_" + lang.replace(/-/g, "_")
                       });
                       res_nls[lang] = true;
                   }
                });
            }
            parsed.moduleBody = JSON.stringify(res_nls);
        }
    }
    catch (err) {
        throw new Error("DojoWebpackLoader cannot load nls module " + module.resourcePath + ". Error" + err.message);
    }
}

function DojoWebpackLoader(content){
    if (this.cacheable) this.cacheable();
    // console.log("-----------------------")
    // console.log(this.resourcePath);

    // Prepare options
    var dojoWebpackLoaderOptions = loaderUtils.getOptions(this);
    var options = Object.assign({}, defaultOptions, dojoWebpackLoaderOptions);
    options.staticHasFeatures = Object.assign({}, defaultOptions.staticHasFeatures, dojoWebpackLoaderOptions.staticHasFeatures);

    if (!options.dojoCorePath) throw new Error("DojoWebpackLoader: dojoWebpackLoader.dojoCorePath option is not set");

    // Prepare module
    var module = {
        isCore: this.resourcePath.substr(0, options.dojoCorePath.length) == options.dojoCorePath,
        isDijit: options.dojoDijitPath ? this.resourcePath.substr(0, options.dojoDijitPath.length) == options.dojoDijitPath : false,
        isNls: this.resourcePath.match(/nls[\\\/](.*?[\\\/])?.*$/) != null,
        resourcePath: this.resourcePath,
        normalizedName: null,
        inject: {
            // dependencies which will be injected to module additionally
            // [ { dep: "module", name: "var name in module def"} ... ]
            dependencies: [],
            // Add js code at beginning of module
            prepend: "",
            // Add js code at ending of module
            append: ""
        }
    };
    if (module.isCore) module.normalizedName = resolveCoreModuleDependency('dojo', options.dojoCorePath, this.resourcePath, '.');
    else if (module.isDijit) module.normalizedName = resolveCoreModuleDependency('dijit', options.dojoDijitPath, this.resourcePath, '.');

    // Parse module
    var content = preprocessModule(module, options, content);
    var parsed = parser.parseModule(content);
    var new_deps = [];
    if (!module.isNls) {
        new_deps = parsed.deps.map(function (dep) {
            return mapDependency(module, options, dep);
        });
    }
    else {
        processNlsModule(module, parsed, options);
    }
    return parsed.save(new_deps, module.inject);
}

module.exports = DojoWebpackLoader;
<|endoftext|>


<commit_before>process.on('unhandledRejection', function (reason, p) {
  /* eslint-disable no-console */
  console.error('Unhandled Rejection at: Promise ', p)
  console.error('reason: ', reason)
  process.exit(-1)
});

const fs = require('fs')
const path = require('path')
const Hexo = require('hexo')
const chalk = require('chalk')
const minimist = require('minimist')

const args = minimist(process.argv.slice(2))

const cmd = args._.shift()

const hexo = new Hexo(process.cwd(), {})

function initHexo () {
  // defaults outside of _config.yml
  Object.assign(hexo.config, {
    hfc_useref: { enable: true, concat: true, exclude: null },
    hfc_html: { enable: false, exclude: null },
    hfc_css: { enable: true, exclude: ['*.min.css'] },
    hfc_favicons: { enable: false },
    hfc_js: {
      enable: true,
      mangle: true,
      output: null,
      compress: null,
      exclude: ['*.min.js'],
    },
    hfc_img: {
      enable: true,
      interlaced: false,
      multipass: false,
      optimizationLevel: 2,
      pngquant: false,
      progressive: false,
      webp: true,
      webpQuality: 75,
      gifslice: true,
      jpegtran: true,
      jpegrecompress: false,
      jpegrecompressQuality: 'medium',
      optipng: true,
      svgo: false,
    },
  })

  // hexo does this weird thing where it literally sets
  // an 'env' property on the 'env' object
  // so we take that into account (and any other way its set)
  const env = hexo.env.NODE_ENV || hexo.env.env || process.env.NODE_ENV || 'development'

  // https://github.com/mamboer/hexo-filter-cleanup
  // only run the filter_cleanup if we are in
  // production mode -- deploying static asset
  // this will combine multiple JS files into single "application.js"
  // note that --debug mode in hexo disables clean up automatically
  if (env !== 'production' && env !== 'staging') {
    console.log('disabling filter cleanup for environment', env)
    hexo.config.filter_cleanup = false
  }

  let url

  if (env === 'staging') {
    url = 'https://docs-staging.cypress.io'
  } else {
    url = 'https://docs.cypress.io'
  }

  console.log('in environment %s site url is %s', env, url)
  hexo.config.url = url

  // set this on both our process + hexo
  process.env.NODE_ENV = hexo.env.NODE_ENV = env

  console.log('NODE_ENV is:', chalk.cyan(env))

  return hexo.init()
  .then(() => {
    return hexo.call(cmd, args)
  })

}

const pathToBustedDjango = path.resolve('node_modules', 'prismjs', 'components', 'prism-django.js')

const pathToBustedDjangoMin = path.resolve('node_modules', 'prismjs', 'components', 'prism-django.min.js')

try {
  fs.unlinkSync(pathToBustedDjango)
} catch (err) {
  // noop
}

try {
  fs.unlinkSync(pathToBustedDjangoMin)
} catch (err) {
  // noop
}

return initHexo()
<commit_msg>add comments [skip ci]<commit_after>process.on('unhandledRejection', function (reason, p) {
  /* eslint-disable no-console */
  console.error('Unhandled Rejection at: Promise ', p)
  console.error('reason: ', reason)
  process.exit(-1)
});

const fs = require('fs')
const path = require('path')
const Hexo = require('hexo')
const chalk = require('chalk')
const minimist = require('minimist')

// these are the args like --port
const args = minimist(process.argv.slice(2))

// this is the command passed in such as 'server' or 'generate'
const cmd = args._.shift()

const hexo = new Hexo(process.cwd(), {})

function initHexo () {
  // defaults outside of _config.yml
  Object.assign(hexo.config, {
    hfc_useref: { enable: true, concat: true, exclude: null },
    hfc_html: { enable: false, exclude: null },
    hfc_css: { enable: true, exclude: ['*.min.css'] },
    hfc_favicons: { enable: false },
    hfc_js: {
      enable: true,
      mangle: true,
      output: null,
      compress: null,
      exclude: ['*.min.js'],
    },
    hfc_img: {
      enable: true,
      interlaced: false,
      multipass: false,
      optimizationLevel: 2,
      pngquant: false,
      progressive: false,
      webp: true,
      webpQuality: 75,
      gifslice: true,
      jpegtran: true,
      jpegrecompress: false,
      jpegrecompressQuality: 'medium',
      optipng: true,
      svgo: false,
    },
  })

  // hexo does this weird thing where it literally sets
  // an 'env' property on the 'env' object
  // so we take that into account (and any other way its set)
  const env = hexo.env.NODE_ENV || hexo.env.env || process.env.NODE_ENV || 'development'

  // https://github.com/mamboer/hexo-filter-cleanup
  // only run the filter_cleanup if we are in
  // production mode -- deploying static asset
  // this will combine multiple JS files into single "application.js"
  // note that --debug mode in hexo disables clean up automatically
  if (env !== 'production' && env !== 'staging') {
    console.log('disabling filter cleanup for environment', env)
    hexo.config.filter_cleanup = false
  }

  let url

  if (env === 'staging') {
    url = 'https://docs-staging.cypress.io'
  } else {
    url = 'https://docs.cypress.io'
  }

  console.log('in environment %s site url is %s', env, url)
  hexo.config.url = url

  // set this on both our process + hexo
  process.env.NODE_ENV = hexo.env.NODE_ENV = env

  console.log('NODE_ENV is:', chalk.cyan(env))

  return hexo.init()
  .then(() => {
    return hexo.call(cmd, args)
  })

}

// remove these files they are causing errors
// https://github.com/cypress-io/cypress-documentation/issues/54
const pathToBustedDjango = path.resolve('node_modules', 'prismjs', 'components', 'prism-django.js')

const pathToBustedDjangoMin = path.resolve('node_modules', 'prismjs', 'components', 'prism-django.min.js')

try {
  fs.unlinkSync(pathToBustedDjango)
} catch (err) {
  // noop
}

try {
  fs.unlinkSync(pathToBustedDjangoMin)
} catch (err) {
  // noop
}

return initHexo()
<|endoftext|>


<commit_before>var EventEmitter = require('events').EventEmitter
var util = require('util')

/*
 * This API Exposes both Async and Sync Methods.
 * Sync Methods should be used with caution.
 * The Iterator class bubbles up some of the events of the underlying
   stream ( readable, end )
*/

var LevelIterator = function(opts, db) {
    EventEmitter.call(this)
    if ( typeof opts.put == 'function' ) {
	db = opts
	options = {}
    } else {
	options = {}
    }
    this.readStream = db.createReadStream(options)
    this.readable = false
    this.ended = false

    this.buffer = []

    //Faster than bind
    var ref = this;

    this.readStream.on('readable', function() {
	ref.readable = true
	ref.emit('readable')
    })

    this.readStream.on('end', function() {
	this.ended = false
	ref.emit('end')
    })
}

util.inherits(LevelIterator, EventEmitter)

LevelIterator.prototype._read = function() {
    if ( this.buffer.length > 0 )
	return this.buffer.splice(0,1)[0]
    return this.readStream.read()
}

LevelIterator.prototype.validateResult = function(result){
    return (result === null || result === undefined)
}

LevelIterator.prototype.read = function() {
    return this._read();
}

LevelIterator.prototype.next = function(cb) {
    if ( this.ended ) return cb(null, undefined)

    if ( this.readable ) {
	var result = this._read();
	if ( this.validateResult(result)  ) {
	    this.readable = false
	    return this.next(cb)
	}
	return cb(null, result)
    }

    var ref = this;
    var onEnd = function() { ref.next(cb) }

    this.readStream.once('readable', function() {
	ref.readable = true
	ref.readStream.removeListener('end', onEnd)
	ref.next(cb)
    })
    this.readStream.once('end', onEnd)
}

LevelIterator.prototype._checkReadable = function() {
    if ( this.readable === false )
	throw new Error('Stream is not readable yet')
}

LevelIterator.prototype.hasNextSync = function(useBuffer) {
    useBuffer = useBuffer || ( useBuffer = true )

    this._checkReadable()
    while(true){
	if ( this.ended )
	    return false
	if ( this.readable ) {
	    var value = this._read()
	    var isValid = !this.validateResult(value)
	    if ( useBuffer && isValid )
		this.buffer.push(value)

	    return isValid

	}
    }
}

LevelIterator.prototype.seekSync = function(i) {
    i = (i || 1)

    this._checkReadable()
    while( true ) {
	if ( this.ended ) return false
	if ( this.readable ) {
	    for ( var j = 0; j < i; j++){
		this._read()
	    }
	    return
	}
    }
}

module.exports = LevelIterator<commit_msg>Some minor syntax adjustments<commit_after>var EventEmitter = require('events').EventEmitter
var util = require('util')

/*
 * This API Exposes both Async and Sync Methods.
 * Sync Methods should be used with caution.
 * The Iterator class bubbles up some of the events of the underlying
   stream ( readable, end )
*/

var LevelIterator = function(opts, db) {
    EventEmitter.call(this)
    if ( typeof opts.put == 'function' ) {
	db = opts
	options = {}
    } else {
	options = {}
    }
    this.readStream = db.createReadStream(options)
    this.readable = false
    this.ended = false

    this.buffer = []

    //Faster than bind
    var ref = this;

    this.readStream.on('readable', function() {
	ref.readable = true
	ref.emit('readable')
    })

    this.readStream.on('end', function() {
	this.ended = false
	ref.emit('end')
    })
}

util.inherits(LevelIterator, EventEmitter)

LevelIterator.prototype._read = function() {
    if ( this.buffer.length > 0 )
	return this.buffer.splice(0,1)[0]
    return this.readStream.read()
}

LevelIterator.prototype.validateResult = function(result){
    return (result === null || result === undefined)
}

LevelIterator.prototype.read = function() {
    return this._read();
}

LevelIterator.prototype.next = function(cb) {
    if ( this.ended ) return cb(null, undefined)

    if ( this.readable ) {
	var result = this._read();
	if ( this.validateResult(result)  ) {
	    this.readable = false
	    return this.next(cb)
	}
	return cb(null, result)
    }

    var ref = this;
    var onEnd = function() { ref.next(cb) }

    this.readStream.once('readable', function() {
	ref.readable = true
	ref.readStream.removeListener('end', onEnd)
	ref.next(cb)
    })
    this.readStream.once('end', onEnd)
}

LevelIterator.prototype._checkReadable = function() {
    if ( this.readable === false )
	throw new Error('Stream is not readable yet')
}

LevelIterator.prototype.hasNextSync = function(useBuffer) {
    useBuffer = useBuffer || ( useBuffer = true )

    this._checkReadable()
    while(true){
	if ( this.ended ) return false
	if ( this.readable ) {
	    var value = this._read()
	    var isValid = !this.validateResult(value)
	    if ( useBuffer && isValid ) this.buffer.push(value)
	    return isValid
	}
    }
}

LevelIterator.prototype.seekSync = function(i) {
    i = (i || 1)

    this._checkReadable()
    while( true ) {
	if ( this.ended ) return false
	if ( this.readable ) {
	    for ( var j = 0; j < i; j++){
		this._read()
	    }
	    return
	}
    }
}

module.exports = LevelIterator<|endoftext|>


<commit_before>/*!
 * helper-link-to <https://github.com/helpers/helper-link-to>
 *
 * Copyright (c) 2016-2017, Brian Woodward.
 * Released under the MIT License.
 */

'use strict';

var relativePath = require('relative-dest');
var isObject = require('isobject');
var koalas = require('koalas');
var get = require('get-value');

/**
 * [templates][] helper that creates a link from the current view to the specified view
 * on the specified collection.
 *
 *
 * ```hbs
 * {{! Handlebars example linking from "home.html" to "about.html" in the default "pages" collection }}
 * <a href="{{link-to 'about'}}">About</a>
 * ```
 *
 *
 * ```hbs
 * {{! Handlebars example linking from "home.html" to "blog/post-1.html" in the "posts" collection }}
 * <a href="{{link-to 'post-1' 'posts'}}">Post 1</a>
 * ```
 * @param  {String|Object} `key` Name of the view to lookup to link to. May also pass in a view instance link to.
 * @param  {String} `collectionName` Name of the collection to lookup the view on. (Defaults to "pages")
 * @param  {Array} `props` Optional array of properties to check when getting the destination path from a view. May use dot notication (data.permalink). (Defaults to ["dest", "path"])
 * @return {String} Relative path to the specified view from the current view.
 * @api public
 */

module.exports = function linkTo(key, collectionName, props, options) {
  if (isOptions(props)) {
    options = props;
    if (Array.isArray(collectionName)) {
      props = collectionName;
      collectionName = null;
    } else {
      props = null;
    }
  }

  if (isOptions(collectionName)) {
    options = collectionName;
    collectionName = null;
  }

  var name = collectionName;
  props = props || ['dest', 'path'];

  // handlebars options
  if (typeof name === 'object') {
    name = null;
  }

  name = name || 'pages';

  if (!this || typeof this.app === 'undefined') {
    var msg = '[helper-link-to]: Requires an "app" created with "templates".';
    console.error(msg);
    return '';
  }

  var current = this.view || this.context.view;
  if (typeof key === 'object' && (key.isView || key.isItem)) {
    return link(current, key, props);
  }

  var collection = this.app[name];
  if (typeof collection === 'undefined') {
    var msg = '[helper-link-to]: Unable to find collection "' + name + '".';
    console.error(msg);
    return '';
  }

  var target;
  try {
    target = collection.getView(key);
  } catch (err) {
    var msg = '[helper-link-to]: Unable to find view "' + key + '" in collection "' + name + '".\n' + err.message;
    console.log(msg);
    return '';
  }

  if (!target || !Object.keys(target).length) {
    var msg = '[helper-link-to]: Unable to find view "' + key + '" in collection "' + name + '".';
    console.error(msg);
    return '';
  }

  return link(current, target, props);
};

function link(from, to, props) {
  var fromDest = dest(from, props);
  var toDest = dest(to, props);
  return relativePath(fromDest, toDest);
}

function dest(view, props) {
  if (typeof view === 'string') {
    return view;
  }
  return koalas.apply(koalas, props)
    .use(function(prop) {
      return get(view, prop);
    })
    .value();
}

function isOptions(val) {
  return isObject(val) && val.hash && isObject(val.hash);
}
<commit_msg>support `data.path` by default<commit_after>/*!
 * helper-link-to <https://github.com/helpers/helper-link-to>
 *
 * Copyright (c) 2016-2017, Brian Woodward.
 * Released under the MIT License.
 */

'use strict';

var relativePath = require('relative-dest');
var isObject = require('isobject');
var koalas = require('koalas');
var get = require('get-value');

/**
 * [templates][] helper that creates a link from the current view to the specified view
 * on the specified collection.
 *
 *
 * ```hbs
 * {{! Handlebars example linking from "home.html" to "about.html" in the default "pages" collection }}
 * <a href="{{link-to 'about'}}">About</a>
 * ```
 *
 *
 * ```hbs
 * {{! Handlebars example linking from "home.html" to "blog/post-1.html" in the "posts" collection }}
 * <a href="{{link-to 'post-1' 'posts'}}">Post 1</a>
 * ```
 * @param  {String|Object} `key` Name of the view to lookup to link to. May also pass in a view instance link to.
 * @param  {String} `collectionName` Name of the collection to lookup the view on. (Defaults to "pages")
 * @param  {Array} `props` Optional array of properties to check when getting the destination path from a view. May use dot notication (data.permalink). (Defaults to ["dest", "path"])
 * @return {String} Relative path to the specified view from the current view.
 * @api public
 */

module.exports = function linkTo(key, collectionName, props, options) {
  if (isOptions(props)) {
    options = props;
    if (Array.isArray(collectionName)) {
      props = collectionName;
      collectionName = null;
    } else {
      props = null;
    }
  }

  if (isOptions(collectionName)) {
    options = collectionName;
    collectionName = null;
  }

  var name = collectionName;
  props = props || ['data.path', 'dest', 'path'];

  // handlebars options
  if (typeof name === 'object') {
    name = null;
  }

  name = name || 'pages';

  if (!this || typeof this.app === 'undefined') {
    var msg = '[helper-link-to]: Requires an "app" created with "templates".';
    console.error(msg);
    return '';
  }

  var current = this.view || this.context.view;
  if (typeof key === 'object' && (key.isView || key.isItem)) {
    return link(current, key, props);
  }

  var collection = this.app[name];
  if (typeof collection === 'undefined') {
    var msg = '[helper-link-to]: Unable to find collection "' + name + '".';
    console.error(msg);
    return '';
  }

  var target;
  try {
    target = collection.getView(key);
  } catch (err) {
    var msg = '[helper-link-to]: Unable to find view "' + key + '" in collection "' + name + '".\n' + err.message;
    console.log(msg);
    return '';
  }

  if (!target || !Object.keys(target).length) {
    var msg = '[helper-link-to]: Unable to find view "' + key + '" in collection "' + name + '".';
    console.error(msg);
    return '';
  }

  return link(current, target, props);
};

function link(from, to, props) {
  var fromDest = dest(from, props);
  var toDest = dest(to, props);
  return relativePath(fromDest, toDest);
}

function dest(view, props) {
  if (typeof view === 'string') {
    return view;
  }
  return koalas.apply(koalas, props)
    .use(function(prop) {
      return get(view, prop);
    })
    .value();
}

function isOptions(val) {
  return isObject(val) && val.hash && isObject(val.hash);
}
<|endoftext|>