mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-09 05:41:33 +00:00
Compare commits
45 Commits
Author | SHA1 | Date | |
---|---|---|---|
f686a3a010 | |||
92946a59c3 | |||
65e8fd5f24 | |||
f07b35f4d4 | |||
85f601f809 | |||
bb6d3dfc36 | |||
4312acf49a | |||
45c0e957c3 | |||
7d4294b279 | |||
228c2b09de | |||
faa5433f8d | |||
de53a00e4d | |||
cf45341754 | |||
8c75a1fe3c | |||
79f8ccb6fe | |||
4b1b8cdf49 | |||
ac77cbfa16 | |||
0764da9485 | |||
d94c205b94 | |||
02483376b4 | |||
ee22cec1a6 | |||
78704d8f0a | |||
533d36a161 | |||
38171cd396 | |||
cf48d49c59 | |||
945ac3b520 | |||
414a449ec1 | |||
2d527b6244 | |||
08a883c153 | |||
3224e9faa8 | |||
9bcc5055a2 | |||
c34f2a17df | |||
ce0f6fd73f | |||
2b0477c5b3 | |||
79f4edf9e2 | |||
1af8355a88 | |||
82bc0f2171 | |||
3b751c2247 | |||
cf1a4c90e8 | |||
413f50fbbe | |||
1885c773c5 | |||
74748d91fa | |||
4ccea11e6c | |||
cc8b68b659 | |||
db6808113c |
16
.aegir.js
16
.aegir.js
@ -1,16 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
webpack: {
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
'node-forge': path.resolve(
|
|
||||||
path.dirname(require.resolve('libp2p-crypto')),
|
|
||||||
'../vendor/forge.bundle.js'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,4 +31,3 @@ node_modules
|
|||||||
coverage
|
coverage
|
||||||
|
|
||||||
dist
|
dist
|
||||||
lib
|
|
25
.travis.yml
25
.travis.yml
@ -1,9 +1,15 @@
|
|||||||
|
|
||||||
sudo: false
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
matrix:
|
||||||
- 4
|
include:
|
||||||
- 5
|
- node_js: 4
|
||||||
|
env: CXX=g++-4.8
|
||||||
|
- node_js: 6
|
||||||
|
env:
|
||||||
|
- SAUCE=true
|
||||||
|
- CXX=g++-4.8
|
||||||
|
- node_js: stable
|
||||||
|
env: CXX=g++-4.8
|
||||||
|
|
||||||
# Make sure we have new NPM.
|
# Make sure we have new NPM.
|
||||||
before_install:
|
before_install:
|
||||||
@ -14,12 +20,17 @@ script:
|
|||||||
- npm test
|
- npm test
|
||||||
- npm run coverage
|
- npm run coverage
|
||||||
|
|
||||||
addons:
|
|
||||||
firefox: 'latest'
|
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- export DISPLAY=:99.0
|
- export DISPLAY=:99.0
|
||||||
- sh -e /etc/init.d/xvfb start
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- npm run coverage-publish
|
- npm run coverage-publish
|
||||||
|
|
||||||
|
addons:
|
||||||
|
firefox: 'latest'
|
||||||
|
apt:
|
||||||
|
sources:
|
||||||
|
- ubuntu-toolchain-r-test
|
||||||
|
packages:
|
||||||
|
- g++-4.8
|
47
README.md
47
README.md
@ -1,4 +1,47 @@
|
|||||||
libp2p-identify
|
# js-libp2p-identify
|
||||||
===============
|
|
||||||
|
[](http://ipn.io)
|
||||||
|
[](http://ipfs.io/)
|
||||||
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
|
[](https://coveralls.io/github/libp2p/js-libp2p-identify?branch=master)
|
||||||
|
[](https://travis-ci.org/libp2p/js-libp2p-identify)
|
||||||
|
[](https://circleci.com/gh/libp2p/js-libp2p-identify)
|
||||||
|
[](https://david-dm.org/libp2p/js-libp2p-identify) [](https://github.com/feross/standard)
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
[](https://saucelabs.com/u/libp2p-js-identify)
|
||||||
|
|
||||||
> libp2p Identify Protocol
|
> libp2p Identify Protocol
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
Identify is a STUN protocol, used by libp2p-swarmin order to broadcast and learn about the `ip:port` pairs a specific peer is available through and to know when a new stream muxer is established, so a conn can be reused.
|
||||||
|
|
||||||
|
## How does it work
|
||||||
|
|
||||||
|
Best way to understand the current design is through this issue: https://github.com/libp2p/js-libp2p-swarm/issues/78
|
||||||
|
|
||||||
|
### This module uses `pull-streams`
|
||||||
|
|
||||||
|
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
|
||||||
|
|
||||||
|
You can learn more about pull-streams at:
|
||||||
|
|
||||||
|
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
|
||||||
|
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
|
||||||
|
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
|
||||||
|
- [pull-streams documentation](https://pull-stream.github.io/)
|
||||||
|
|
||||||
|
#### Converting `pull-streams` to Node.js Streams
|
||||||
|
|
||||||
|
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const pullToStream = require('pull-stream-to-stream')
|
||||||
|
|
||||||
|
const nodeStreamInstance = pullToStream(pullStreamInstance)
|
||||||
|
// nodeStreamInstance is an instance of a Node.js Stream
|
||||||
|
```
|
||||||
|
|
||||||
|
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
|
||||||
|
35
package.json
35
package.json
@ -1,15 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-identify",
|
"name": "libp2p-identify",
|
||||||
"version": "0.1.2",
|
"version": "0.4.2",
|
||||||
"description": "libp2p Identify Protocol",
|
"description": "libp2p Identify Protocol",
|
||||||
"main": "lib/index.js",
|
"main": "src/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "aegir-lint",
|
"lint": "aegir-lint",
|
||||||
"build": "aegir-build",
|
"build": "aegir-build",
|
||||||
"test": "aegir-test",
|
"test": "aegir-test",
|
||||||
"test:node": "aegir-test test:node",
|
"test:node": "aegir-test --env node",
|
||||||
"test:browser": "aegir-test test:browser",
|
"test:browser": "aegir-test --env browser",
|
||||||
"release": "aegir-release",
|
"release": "aegir-release",
|
||||||
"release-minor": "aegir-release --type minor",
|
"release-minor": "aegir-release --type minor",
|
||||||
"release-major": "aegir-release --type major",
|
"release-major": "aegir-release --type major",
|
||||||
@ -21,7 +20,7 @@
|
|||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^4.3.0"
|
"node": ">=4.0.0"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -38,18 +37,24 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-libp2p-identify#readme",
|
"homepage": "https://github.com/libp2p/js-libp2p-identify#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^3.2.0",
|
"aegir": "^11.0.2",
|
||||||
"pre-commit": "^1.1.3",
|
"chai": "^4.0.2",
|
||||||
"stream-pair": "^1.0.3"
|
"dirty-chai": "^2.0.0",
|
||||||
|
"pre-commit": "^1.2.2",
|
||||||
|
"pull-pair": "^1.1.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"length-prefixed-stream": "^1.5.0",
|
"multiaddr": "^2.3.0",
|
||||||
"multiaddr": "^2.0.2",
|
"peer-id": "~0.8.7",
|
||||||
"peer-id": "^0.7.0",
|
"peer-info": "~0.9.2",
|
||||||
"peer-info": "^0.7.0",
|
"protocol-buffers": "^3.2.1",
|
||||||
"protocol-buffers": "^3.1.6"
|
"pull-length-prefixed": "^1.3.0",
|
||||||
|
"pull-stream": "^3.6.0"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>"
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
|
"Greenkeeper <support@greenkeeper.io>",
|
||||||
|
"Richard Littauer <richard.littauer@gmail.com>"
|
||||||
]
|
]
|
||||||
}
|
}
|
59
src/dialer.js
Normal file
59
src/dialer.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
'use strict'
|
||||||
|
const PeerInfo = require('peer-info')
|
||||||
|
const PeerId = require('peer-id')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
const lp = require('pull-length-prefixed')
|
||||||
|
|
||||||
|
const msg = require('./message')
|
||||||
|
|
||||||
|
module.exports = (conn, callback) => {
|
||||||
|
pull(
|
||||||
|
conn,
|
||||||
|
lp.decode(),
|
||||||
|
pull.take(1),
|
||||||
|
pull.collect((err, data) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// connection got closed graciously
|
||||||
|
if (data.length === 0) {
|
||||||
|
return callback(new Error('conn was closed, did not receive data'))
|
||||||
|
}
|
||||||
|
|
||||||
|
const input = msg.decode(data[0])
|
||||||
|
|
||||||
|
PeerId.createFromPubKey(input.publicKey, (err, id) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const peerInfo = new PeerInfo(id)
|
||||||
|
input.listenAddrs
|
||||||
|
.map(multiaddr)
|
||||||
|
.forEach((ma) => peerInfo.multiaddrs.add(ma))
|
||||||
|
|
||||||
|
callback(null, peerInfo, getObservedAddrs(input))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getObservedAddrs (input) {
|
||||||
|
if (!hasObservedAddr(input)) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
let addrs = input.observedAddr
|
||||||
|
|
||||||
|
if (!Array.isArray(input.observedAddr)) {
|
||||||
|
addrs = [addrs]
|
||||||
|
}
|
||||||
|
|
||||||
|
return addrs.map((oa) => multiaddr(oa))
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasObservedAddr (input) {
|
||||||
|
return input.observedAddr && input.observedAddr.length > 0
|
||||||
|
}
|
87
src/index.js
87
src/index.js
@ -1,89 +1,6 @@
|
|||||||
/*
|
|
||||||
* Identify is one of the protocols swarms speaks in order to
|
|
||||||
* broadcast and learn about the ip:port pairs a specific peer
|
|
||||||
* is available through and to know when a new stream muxer is
|
|
||||||
* established, so a conn can be reused
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
const PeerId = require('peer-id')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
const bl = require('bl')
|
|
||||||
|
|
||||||
const lpstream = require('length-prefixed-stream')
|
|
||||||
const protobuf = require('protocol-buffers')
|
|
||||||
const schema = fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
|
||||||
const idPb = protobuf(schema)
|
|
||||||
|
|
||||||
exports = module.exports
|
exports = module.exports
|
||||||
exports.multicodec = '/ipfs/id/1.0.0'
|
exports.multicodec = '/ipfs/id/1.0.0'
|
||||||
|
exports.listener = require('./listener')
|
||||||
exports.exec = (conn, callback) => {
|
exports.dialer = require('./dialer')
|
||||||
const decode = lpstream.decode()
|
|
||||||
|
|
||||||
conn
|
|
||||||
.pipe(decode)
|
|
||||||
.pipe(bl((err, data) => {
|
|
||||||
if (err) {
|
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
const msg = idPb.Identify.decode(data)
|
|
||||||
let observedAddrs = []
|
|
||||||
if (hasObservedAddr(msg)) {
|
|
||||||
if (!Array.isArray(msg.observedAddr)) {
|
|
||||||
msg.observedAddr = [msg.observedAddr]
|
|
||||||
}
|
|
||||||
observedAddrs = msg.observedAddr.map((oa) => {
|
|
||||||
return multiaddr(oa)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const pId = PeerId.createFromPubKey(msg.publicKey)
|
|
||||||
const pInfo = new PeerInfo(pId)
|
|
||||||
msg.listenAddrs.forEach((ma) => {
|
|
||||||
pInfo.multiaddr.add(multiaddr(ma))
|
|
||||||
})
|
|
||||||
|
|
||||||
callback(null, pInfo, observedAddrs)
|
|
||||||
}))
|
|
||||||
|
|
||||||
conn.end()
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.handler = (pInfoSelf) => {
|
|
||||||
return (conn) => {
|
|
||||||
// send what I see from the other + my Info
|
|
||||||
const encode = lpstream.encode()
|
|
||||||
|
|
||||||
encode.pipe(conn)
|
|
||||||
|
|
||||||
conn.getObservedAddrs((err, observedAddrs) => {
|
|
||||||
if (err) { return }
|
|
||||||
observedAddrs = observedAddrs[0]
|
|
||||||
|
|
||||||
let publicKey = new Buffer(0)
|
|
||||||
if (pInfoSelf.id.pubKey) {
|
|
||||||
publicKey = pInfoSelf.id.pubKey.bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
const msgSend = idPb.Identify.encode({
|
|
||||||
protocolVersion: 'ipfs/0.1.0',
|
|
||||||
agentVersion: 'na',
|
|
||||||
publicKey: publicKey,
|
|
||||||
listenAddrs: pInfoSelf.multiaddrs.map((ma) => ma.buffer),
|
|
||||||
observedAddr: observedAddrs ? observedAddrs.buffer : new Buffer('')
|
|
||||||
})
|
|
||||||
|
|
||||||
encode.write(msgSend)
|
|
||||||
encode.end()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function hasObservedAddr (msg) {
|
|
||||||
return msg.observedAddr && msg.observedAddr.length > 0
|
|
||||||
}
|
|
||||||
|
33
src/listener.js
Normal file
33
src/listener.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
const lp = require('pull-length-prefixed')
|
||||||
|
|
||||||
|
const msg = require('./message')
|
||||||
|
|
||||||
|
module.exports = (conn, pInfoSelf) => {
|
||||||
|
// send what I see from the other + my Info
|
||||||
|
conn.getObservedAddrs((err, observedAddrs) => {
|
||||||
|
if (err) { return }
|
||||||
|
observedAddrs = observedAddrs[0]
|
||||||
|
|
||||||
|
let publicKey = new Buffer(0)
|
||||||
|
if (pInfoSelf.id.pubKey) {
|
||||||
|
publicKey = pInfoSelf.id.pubKey.bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgSend = msg.encode({
|
||||||
|
protocolVersion: 'ipfs/0.1.0',
|
||||||
|
agentVersion: 'na',
|
||||||
|
publicKey: publicKey,
|
||||||
|
listenAddrs: pInfoSelf.multiaddrs.toArray().map((ma) => ma.buffer),
|
||||||
|
observedAddr: observedAddrs ? observedAddrs.buffer : new Buffer('')
|
||||||
|
})
|
||||||
|
|
||||||
|
pull(
|
||||||
|
pull.values([msgSend]),
|
||||||
|
lp.encode(),
|
||||||
|
conn
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
message Identify {
|
'use strict'
|
||||||
|
|
||||||
|
const protobuf = require('protocol-buffers')
|
||||||
|
const schema = new Buffer(`
|
||||||
|
message Identify {
|
||||||
// protocolVersion determines compatibility between peers
|
// protocolVersion determines compatibility between peers
|
||||||
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
|
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
|
||||||
|
|
||||||
@ -20,6 +23,8 @@ message Identify {
|
|||||||
// determine whether its connection to the local peer goes through NAT.
|
// determine whether its connection to the local peer goes through NAT.
|
||||||
optional bytes observedAddr = 4;
|
optional bytes observedAddr = 4;
|
||||||
|
|
||||||
// (DEPRECATED) protocols are the services this node is running
|
repeated string protocols = 3;
|
||||||
// repeated string protocols = 3;
|
|
||||||
}
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
|
module.exports = protobuf(schema).Identify
|
61
test/dialer.spec.js
Normal file
61
test/dialer.spec.js
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
const pair = require('pull-pair/duplex')
|
||||||
|
const PeerInfo = require('peer-info')
|
||||||
|
const lp = require('pull-length-prefixed')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
const msg = require('../src/message')
|
||||||
|
const identify = require('../src')
|
||||||
|
|
||||||
|
describe('identify.dialer', () => {
|
||||||
|
let original
|
||||||
|
beforeEach((done) => {
|
||||||
|
PeerInfo.create((err, info) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
original = info
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('works', (done) => {
|
||||||
|
const p = pair()
|
||||||
|
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||||
|
const input = msg.encode({
|
||||||
|
protocolVersion: 'ipfs/0.1.0',
|
||||||
|
agentVersion: 'na',
|
||||||
|
publicKey: original.id.pubKey.bytes,
|
||||||
|
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||||
|
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer
|
||||||
|
})
|
||||||
|
|
||||||
|
pull(
|
||||||
|
pull.values([input]),
|
||||||
|
lp.encode(),
|
||||||
|
p[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
identify.dialer(p[1], (err, info, observedAddrs) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(info.id.pubKey.bytes)
|
||||||
|
.to.eql(original.id.pubKey.bytes)
|
||||||
|
|
||||||
|
expect(info.multiaddrs.toArray())
|
||||||
|
.to.eql(original.multiaddrs.toArray())
|
||||||
|
|
||||||
|
expect(observedAddrs)
|
||||||
|
.to.eql([multiaddr('/ip4/127.0.0.1/tcp/5001')])
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -1,4 +0,0 @@
|
|||||||
/* eslint-env mocha */
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
describe('identify', () => {})
|
|
15
test/index.spec.js
Normal file
15
test/index.spec.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
|
||||||
|
const identify = require('../src')
|
||||||
|
|
||||||
|
describe('identify', () => {
|
||||||
|
it('multicodec', () => {
|
||||||
|
expect(identify.multicodec).to.eql('/ipfs/id/1.0.0')
|
||||||
|
})
|
||||||
|
})
|
62
test/listener.spec.js
Normal file
62
test/listener.spec.js
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
const pair = require('pull-pair/duplex')
|
||||||
|
const PeerInfo = require('peer-info')
|
||||||
|
const lp = require('pull-length-prefixed')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
const msg = require('../src/message')
|
||||||
|
const identify = require('../src')
|
||||||
|
|
||||||
|
describe('identify.listener', () => {
|
||||||
|
let info
|
||||||
|
|
||||||
|
beforeEach((done) => {
|
||||||
|
PeerInfo.create((err, _info) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
info = _info
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('works', (done) => {
|
||||||
|
const p = pair()
|
||||||
|
info.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
|
||||||
|
pull(
|
||||||
|
p[1],
|
||||||
|
lp.decode(),
|
||||||
|
pull.collect((err, result) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
|
const input = msg.decode(result[0])
|
||||||
|
expect(
|
||||||
|
input
|
||||||
|
).to.be.eql({
|
||||||
|
protocolVersion: 'ipfs/0.1.0',
|
||||||
|
agentVersion: 'na',
|
||||||
|
publicKey: info.id.pubKey.bytes,
|
||||||
|
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
|
||||||
|
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
|
||||||
|
protocols: []
|
||||||
|
})
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const conn = p[0]
|
||||||
|
conn.getObservedAddrs = (cb) => {
|
||||||
|
cb(null, [multiaddr('/ip4/127.0.0.1/tcp/5001')])
|
||||||
|
}
|
||||||
|
|
||||||
|
identify.listener(conn, info)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user