Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
feat: move lsHandler out to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 3, 2016
1 parent 6699023 commit 8f32cba
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
36 changes: 3 additions & 33 deletions src/listener/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const pull = require('pull-stream')
const pullLP = require('pull-length-prefixed')
const varint = require('varint')
const isFunction = require('lodash.isfunction')
const assert = require('assert')
const select = require('../select')
const selectHandler = require('./selectHandler')
const lsHandler = require('./lsHandler')

const util = require('./../util')
const Connection = require('interface-connection').Connection

Expand All @@ -15,7 +15,7 @@ const PROTOCOL_ID = require('./../constants').PROTOCOL_ID
module.exports = class Listener {
constructor () {
this.handlers = {
ls: (conn) => this._ls(conn)
ls: (conn) => lsHandler(this, conn)
}
this.log = util.log.listener()
}
Expand Down Expand Up @@ -60,34 +60,4 @@ module.exports = class Listener {

this.handlers[protocol] = handler
}

// inner function - handler for `ls`
_ls (conn) {
const protos = Object.keys(this.handlers)
.filter((key) => key !== 'ls')
const nProtos = protos.length
// total size of the list of protocols, including varint and newline
const size = protos.reduce((size, proto) => {
const p = new Buffer(proto + '\n')
const el = varint.encodingLength(p.length)
return size + el
}, 0)

const buf = Buffer.concat([
new Buffer(varint.encode(nProtos)),
new Buffer(varint.encode(size)),
new Buffer('\n')
])

const encodedProtos = protos.map((proto) => {
return new Buffer(proto + '\n')
})
const values = [buf].concat(encodedProtos)

pull(
pull.values(values),
pullLP.encode(),
conn
)
}
}
38 changes: 38 additions & 0 deletions src/listener/lsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const pull = require('pull-stream')
const pullLP = require('pull-length-prefixed')
const varint = require('varint')

function lsHandler (self, conn) {
const protos = Object
.keys(self.handlers)
.filter((key) => key !== 'ls')

const nProtos = protos.length
// total size of the list of protocols, including varint and newline
const size = protos.reduce((size, proto) => {
const p = new Buffer(proto + '\n')
const el = varint.encodingLength(p.length)
return size + el
}, 0)

const buf = Buffer.concat([
new Buffer(varint.encode(nProtos)),
new Buffer(varint.encode(size)),
new Buffer('\n')
])

const encodedProtos = protos.map((proto) => {
return new Buffer(proto + '\n')
})
const values = [buf].concat(encodedProtos)

pull(
pull.values(values),
pullLP.encode(),
conn
)
}

module.exports = lsHandler

0 comments on commit 8f32cba

Please sign in to comment.