This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move lsHandler out to its own module
- Loading branch information
Showing
2 changed files
with
41 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |