Skip to content

Commit

Permalink
using standard for code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
allain committed May 26, 2016
1 parent e3c491f commit 53278ec
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 50 deletions.
9 changes: 4 additions & 5 deletions bin/grape.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@ if (argv.help) {
'--bn <IP:PORT[,IP:PORT]>: DHT Bootstrap Nodes\n' +
'--ap <INT> : Grape API port\n' +
'\nExamples:\n' +
'grape --dp 20001 --ap 30001 --bn \'127.0.0.1:20002,127.0.0.1:20003\'\n' +
'grape --dp 20002 --ap 30002 --bn \'127.0.0.1:20001,127.0.0.1:20003\'\n' +
'grape --dp 20003 --ap 30003 --bn \'127.0.0.1:20001,127.0.0.1:20002\'\n' +
"grape --dp 20001 --ap 30001 --bn '127.0.0.1:20002,127.0.0.1:20003'\n" +
"grape --dp 20002 --ap 30002 --bn '127.0.0.1:20001,127.0.0.1:20003'\n" +
"grape --dp 20003 --ap 30003 --bn '127.0.0.1:20001,127.0.0.1:20002'\n" +
''
)
process.exit(-1)
}


var dht_port = argv.dp
var api_port = argv.ap
var dht_bootstrap = _.reduce((argv.bn || '').split(','), (acc, e) => {
if (e) {
acc.push(e)
}
return acc
return acc
}, [])

var g = new Grape({
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
Grape: require('./lib/Grape.js')
Grape: require('./lib/Grape.js')
}
61 changes: 28 additions & 33 deletions lib/Grape.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ var Events = require('events')
var UWS = require('uws')
var debug = require('debug')('grenache:grape')

var noop = () => {}
var noop = () => {
}

class Grape extends Events {

constructor(conf) {
constructor (conf) {
super()

this.conf = {
Expand All @@ -27,7 +28,7 @@ class Grape extends Events {
this._mem = {}
}

create_node(port, bootstrap, cb) {
create_node (port, bootstrap, cb) {
var dht = new DHT({
bootstrap: bootstrap
})
Expand Down Expand Up @@ -88,23 +89,21 @@ class Grape extends Events {
cb(null, dht)
})

function handleBootstrapError(err) {
function handleBootstrapError (err) {
cb(err)
cb = noop
}

return dht
}

hex2Str(val) {
hex2Str (val) {
return (new Buffer(val, 'hex')).toString()
}

str2Hex(val) {
str2Hex (val) {
return (new Buffer(val)).toString('hex')
}

timeslot(offset) {
timeslot (offset) {
if (!offset) {
offset = 0
}
Expand All @@ -115,7 +114,7 @@ class Grape extends Events {
return ts
}

onRequest(type, data, cb) {
onRequest (type, data, cb) {
var met = 'handle' + _s.camelize('-' + type)

if (!this[met]) {
Expand All @@ -128,7 +127,7 @@ class Grape extends Events {
})
}

handleLookup(_val, cb) {
handleLookup (_val, cb) {
if (!_val || !_.isString(_val)) {
cb('ERR_GRAPE_LOOKUP')
return
Expand All @@ -154,7 +153,7 @@ class Grape extends Events {
})
}

handleAnnounce(val, cb) {
handleAnnounce (val, cb) {
if (!val || !_.isArray(val)) {
cb('ERR_GRAPE_ANNOUNCE')
return
Expand All @@ -165,12 +164,11 @@ class Grape extends Events {
this.announce(val[0], val[1], cb)
}

announce(val, port, cb) {
this.node.announce(this.str2Hex(val), port || this.conf.dht_port, cb ? cb : () => {
})
announce (val, port, cb) {
this.node.announce(this.str2Hex(val), port || this.conf.dht_port, cb ? cb : noop)
}

lookup(val, cb) {
lookup (val, cb) {
var ih = this.str2Hex(val)

this.node.lookup(ih, (err) => {
Expand All @@ -188,7 +186,7 @@ class Grape extends Events {
})
}

start(cb) {
start (cb) {
this.create_node(this.conf.dht_port, this.conf.dht_bootstrap, (err, node) => {
if (err) return cb(err)

Expand Down Expand Up @@ -218,36 +216,33 @@ class Grape extends Events {

// We have to do this because UWS.Server does not call the callback with an error if the port is already in use.
this.transport.httpServer.once('error', handleApiBootstrapError)
function handleApiBootstrapError(err) {
function handleApiBootstrapError (err) {
cb(err)
cb = noop
}
})
}

stop(cb) {
var node = this.node
var transport = this.transport

// this is done to maintain the same API from before the callback as added
delete this.node
delete this.transport

stop (cb) {
async.series([
(cb) => node ? node.destroy(cb) : cb(),
(cb) => this.node ? this.node.destroy(cb) : cb(),
(cb) => {
// Transport.close does not accept a callback, but should since its underlying implementation accepts one
//transport ? transport.close(cb) : cb()
// transport ? transport.close(cb) : cb()

if (!this.transport) return cb()

if (!transport) return cb()

transport.close()
this.transport.close()

// Under the hood it creates a httpServer instance then doesn't clean it up
transport.httpServer.close();
this.transport.httpServer.close()
cb()
}
], cb)
], (err) => {
delete this.node
delete this.transport
cb(err)
})
}
}

Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
},
"devDependencies": {
"chai": "^3.2.0",
"mocha": "^2.2.5"
"mocha": "^2.2.5",
"standard-format": "^2.2.1"
},
"scripts": {
"test": "istanbul test ./node_modules/.bin/_mocha -- -R spec",
"lint": "eslint --config .eslintrc --fix index.js lib/ test/"
"lint": "standard-format --write"
},
"license": "MIT",
"repository": {
Expand All @@ -46,8 +47,5 @@
"bugs": {
"url": "https://github.com/bitfinexcom/grenache-grape/issues"
},
"homepage": "https://github.com/bitfinexcom/grenache-grape",
"optionalDependencies": {
"eslint": "~1.6.0"
}
"homepage": "https://github.com/bitfinexcom/grenache-grape"
}
10 changes: 5 additions & 5 deletions test/Grape.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var assert = require('chai').assert;
var assert = require('chai').assert

var Events = require('events')

Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Grape', () => {
dht_port: 20000,
api_port: 20001
})

grape.stop(done)
})

Expand All @@ -50,7 +50,7 @@ describe('Grape', () => {

var grape2 = new Grape({
dht_port: 30000,
api_port: 20001 //same
api_port: 20001 // same
})

grape1.start((err) => {
Expand All @@ -70,7 +70,7 @@ describe('Grape', () => {
})

var grape2 = new Grape({
dht_port: 20000, //same
dht_port: 20000, // same
api_port: 30000
})

Expand All @@ -84,4 +84,4 @@ describe('Grape', () => {
})
})
})
})
})

0 comments on commit 53278ec

Please sign in to comment.