Comparing version 1.0.0 to 1.1.0
39
index.js
@@ -1,12 +0,37 @@ | ||
var hat = require('hat'); | ||
var hat = require('hat') | ||
var Buffer = require('safe-buffer').Buffer | ||
var SIZE_BYTES = 20 | ||
module.exports = peerid | ||
/** | ||
* @param {string} prefix optional - prefix for peer id | ||
* generate peer id / node id | ||
* @param {[string|buffer]} prefix - prefix for peer id | ||
* @return {Buffer} | ||
*/ | ||
module.exports = function peerid(prefix) { | ||
return prefix ? | ||
Buffer.concat([new Buffer(prefix, 'binary'), new Buffer(hat(160), 'hex')], 20) : | ||
new Buffer(hat(160), 'hex') | ||
; | ||
function peerid(prefix) { | ||
return is_str(prefix) ? | ||
concat(Buffer.from(prefix), random_hash()) : | ||
is_buf(prefix) ? | ||
concat(prefix, random_hash()) : | ||
random_hash() | ||
} | ||
function random_hash() { | ||
return Buffer.from(hat(160), 'hex') | ||
} | ||
function is_str(s) { | ||
return typeof s === 'string' | ||
} | ||
function is_buf(b) { | ||
return Buffer.isBuffer(b) | ||
} | ||
function concat() { | ||
var args = [].slice.call(arguments) | ||
return Buffer.concat(args, SIZE_BYTES) | ||
} |
{ | ||
"name": "peerid", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Peer id generator (for bittorrent)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node ./node_modules/mocha/bin/mocha -r should -R spec", | ||
"coverage": "istanbul cover --report lcovonly ./node_modules/mocha/bin/_mocha -- -u exports -r should -R spec test/*" | ||
"test": "ava test.js" | ||
}, | ||
@@ -27,8 +26,9 @@ "repository": { | ||
"dependencies": { | ||
"hat": "0.0.3" | ||
"hat": "^0.0.3", | ||
"safe-buffer": "^5.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.3.4", | ||
"should": "^8.0.2" | ||
"ava": "^0.17.0", | ||
"buffer-equals": "^1.0.4" | ||
} | ||
} |
@@ -6,6 +6,5 @@ ## peerid | ||
[![downloads](https://img.shields.io/npm/dm/peerid.svg)](https://npmjs.org/package/peerid) | ||
[![Code Climate](https://codeclimate.com/github/ReklatsMasters/peerid/badges/gpa.svg)](https://codeclimate.com/github/ReklatsMasters/peerid) | ||
[![Test Coverage](https://codeclimate.com/github/ReklatsMasters/peerid/badges/coverage.svg)](https://codeclimate.com/github/ReklatsMasters/peerid/coverage) | ||
> Generate peer id for bittorrent dht node | ||
Generate peer id / node id for dht / bt node | ||
@@ -15,16 +14,17 @@ ### Usage | ||
```js | ||
var BtClient = require('bittorrent-tracker'); | ||
var peerid = require('peerid'); | ||
const BtClient = require('bittorrent-tracker') | ||
const peerid = require('peerid') | ||
var client = new BtClient(peerid(), 6881 /*, torrent file */); | ||
const client = new BtClient(peerid(), 6881 /*, torrent file */) | ||
// do some ... | ||
peerid('-UT1800-') // with prefix | ||
``` | ||
### API | ||
* `peerid(prefix)` | ||
##### @param {string} prefix optional | ||
Prefix for generated peer id | ||
##### `peerid([prefix: String|Buffer]): Buffer` | ||
Generate random peer id / node id | ||
### License | ||
MIT, 2015 (c) Dmitry Tsvettsikh | ||
MIT, © Dmitry Tsvettsikh |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
106417
11
55
2
+ Addedsafe-buffer@^5.0.1
+ Addedsafe-buffer@5.2.1(transitive)
Updatedhat@^0.0.3