mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-02 05:11:42 +00:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
79428f3e62 | |||
8a394b5286 | |||
b7f73bcda1 | |||
8b44aa28ee | |||
ded1f6831c | |||
5ef24695fc | |||
df0aa059ca | |||
bf74e9acad | |||
83c4617e8d | |||
3b7e68bc8a | |||
de1d7fe75c | |||
9c77a69ae3 | |||
50840a8067 | |||
d194ff0a3b | |||
41ca37e9ce | |||
71a28bb177 | |||
0d1861a1f8 | |||
929408eb38 | |||
b94f9c5d51 | |||
4b0851b4a4 | |||
74771b9504 | |||
48eefa1b40 | |||
f8f52665f7 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,6 +4,9 @@ docs
|
|||||||
test/repo-tests*
|
test/repo-tests*
|
||||||
**/bundle.js
|
**/bundle.js
|
||||||
|
|
||||||
|
# yarn
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
|
35
CHANGELOG.md
35
CHANGELOG.md
@ -1,3 +1,38 @@
|
|||||||
|
<a name="0.12.0"></a>
|
||||||
|
# [0.12.0](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.6...v0.12.0) (2018-04-05)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add class-is module ([ded1f68](https://github.com/libp2p/js-libp2p-tcp/commit/ded1f68))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.11.6"></a>
|
||||||
|
## [0.11.6](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.5...v0.11.6) (2018-02-20)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.11.5"></a>
|
||||||
|
## [0.11.5](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.4...v0.11.5) (2018-02-07)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.11.4"></a>
|
||||||
|
## [0.11.4](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.3...v0.11.4) (2018-02-07)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.11.3"></a>
|
||||||
|
## [0.11.3](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.2...v0.11.3) (2018-02-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* clearing timeout when closes ([#87](https://github.com/libp2p/js-libp2p-tcp/issues/87)) ([f8f5266](https://github.com/libp2p/js-libp2p-tcp/commit/f8f5266))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.11.2"></a>
|
<a name="0.11.2"></a>
|
||||||
## [0.11.2](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.1...v0.11.2) (2018-01-12)
|
## [0.11.2](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.1...v0.11.2) (2018-01-12)
|
||||||
|
|
||||||
|
71
README.md
71
README.md
@ -9,23 +9,17 @@
|
|||||||

|

|
||||||

|

|
||||||
|
|
||||||

|
[](https://github.com/libp2p/interface-transport)
|
||||||

|
[](https://github.com/libp2p/interface-connection)
|
||||||
|
|
||||||
> Node.js implementation of the TCP module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/interface-connection) interface for dial/listen.
|
|
||||||
|
|
||||||
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
> JavaScript implementation of the TCP module for libp2p. It exposes the [interface-transport](https://github.com/libp2p/interface-connection) for dial/listen. `libp2p-tcp` is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
||||||
|
|
||||||
**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
|
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
- [Install](#install)
|
- [Install](#install)
|
||||||
- [npm](#npm)
|
- [npm](#npm)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Example](#example)
|
|
||||||
- [This module uses `pull-streams`](#this-module-uses-pull-streams)
|
|
||||||
- [Converting `pull-streams` to Node.js Streams](#converting-pull-streams-to-nodejs-streams)
|
|
||||||
- [API](#api)
|
- [API](#api)
|
||||||
- [Contribute](#contribute)
|
- [Contribute](#contribute)
|
||||||
- [License](#license)
|
- [License](#license)
|
||||||
@ -35,24 +29,21 @@
|
|||||||
### npm
|
### npm
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
> npm i libp2p-tcp
|
> npm install libp2p-tcp
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Example
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const TCP = require('libp2p-tcp')
|
const TCP = require('libp2p-tcp')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
|
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
const mh2 = multiaddr('/ip6/::/tcp/9092')
|
|
||||||
|
|
||||||
const tcp = new TCP()
|
const tcp = new TCP()
|
||||||
|
|
||||||
const listener = tcp.createListener(mh1, (socket) => {
|
const listener = tcp.createListener((socket) => {
|
||||||
console.log('new connection opened')
|
console.log('new connection opened')
|
||||||
pull(
|
pull(
|
||||||
pull.values(['hello']),
|
pull.values(['hello']),
|
||||||
@ -60,15 +51,21 @@ const listener = tcp.createListener(mh1, (socket) => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
listener.listen(() => {
|
listener.listen(mh, () => {
|
||||||
console.log('listening')
|
console.log('listening')
|
||||||
|
|
||||||
pull(
|
pull(
|
||||||
tcp.dial(mh1),
|
tcp.dial(mh),
|
||||||
pull.log,
|
pull.collect((err, values) => {
|
||||||
pull.onEnd(() => {
|
if (!err) {
|
||||||
tcp.close()
|
console.log(`Value: ${values.toString()}`)
|
||||||
})
|
} else {
|
||||||
|
console.log(`Error: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close connection after reading
|
||||||
|
listener.close()
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -78,35 +75,13 @@ Outputs:
|
|||||||
```sh
|
```sh
|
||||||
listening
|
listening
|
||||||
new connection opened
|
new connection opened
|
||||||
hello
|
Value: hello
|
||||||
```
|
```
|
||||||
|
|
||||||
### 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/dominictarr/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.
|
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
### Transport
|
||||||
|
|
||||||
[](https://github.com/libp2p/interface-transport)
|
[](https://github.com/libp2p/interface-transport)
|
||||||
|
|
||||||
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
|
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
|
||||||
@ -116,6 +91,10 @@ To learn more about this utility, visit https://pull-stream.github.io/#pull-stre
|
|||||||
|
|
||||||
Both for dialing and listening.
|
Both for dialing and listening.
|
||||||
|
|
||||||
|
### Connection
|
||||||
|
|
||||||
|
[](https://github.com/libp2p/interface-connection)
|
||||||
|
|
||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
Contributions are welcome! The libp2p implementation in JavaScript is a work in progress. As such, there's a few things you can do right now to help out:
|
Contributions are welcome! The libp2p implementation in JavaScript is a work in progress. As such, there's a few things you can do right now to help out:
|
||||||
|
24
package.json
24
package.json
@ -1,18 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.11.2",
|
"version": "0.12.0",
|
||||||
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "aegir lint",
|
"lint": "aegir lint",
|
||||||
"test": "aegir test --t node",
|
"test": "aegir test -t node",
|
||||||
"release": "aegir release -t node --no-build",
|
"release": "aegir release -t node --no-build",
|
||||||
"release-minor": "aegir release -t node --type minor --no-build",
|
"release-minor": "aegir release -t node --type minor --no-build",
|
||||||
"release-major": "aegir-release -t node --type major --no-build",
|
"release-major": "aegir-release -t node --type major --no-build",
|
||||||
"coverage": "aegir coverage",
|
"coverage": "aegir coverage",
|
||||||
"coverage-publish": "aegir coverage --provider coveralls"
|
"coverage-publish": "aegir coverage --provider coveralls"
|
||||||
},
|
},
|
||||||
"pre-commit": [
|
"pre-push": [
|
||||||
"lint",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
@ -34,33 +34,37 @@
|
|||||||
"npm": ">=3.0.0"
|
"npm": ">=3.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^12.3.0",
|
"aegir": "^13.0.6",
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.1.2",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"interface-transport": "~0.3.5",
|
"interface-transport": "~0.3.6",
|
||||||
"lodash.isfunction": "^3.0.8",
|
"lodash.isfunction": "^3.0.9",
|
||||||
"pre-commit": "^1.2.2",
|
"pre-commit": "^1.2.2",
|
||||||
"pull-stream": "^3.6.1"
|
"pull-stream": "^3.6.7"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"class-is": "^1.1.0",
|
||||||
"debug": "^3.1.0",
|
"debug": "^3.1.0",
|
||||||
"interface-connection": "~0.3.2",
|
"interface-connection": "~0.3.2",
|
||||||
"ip-address": "^5.8.9",
|
"ip-address": "^5.8.9",
|
||||||
"lodash.includes": "^4.3.0",
|
"lodash.includes": "^4.3.0",
|
||||||
"lodash.isfunction": "^3.0.8",
|
"lodash.isfunction": "^3.0.9",
|
||||||
"mafmt": "^3.0.2",
|
"mafmt": "^6.0.0",
|
||||||
"multiaddr": "^3.0.2",
|
"multiaddr": "^4.0.0",
|
||||||
"once": "^1.4.0",
|
"once": "^1.4.0",
|
||||||
"stream-to-pull-stream": "^1.7.2"
|
"stream-to-pull-stream": "^1.7.2"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"Alan Shaw <alan@tableflip.io>",
|
"Alan Shaw <alan@tableflip.io>",
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"Diogo Silva <fsdiogo@gmail.com>",
|
||||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||||
|
"Drew Stone <drewstone329@gmail.com>",
|
||||||
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Greenkeeper <support@greenkeeper.io>",
|
"Greenkeeper <support@greenkeeper.io>",
|
||||||
"João Antunes <j.goncalo.antunes@gmail.com>",
|
"João Antunes <j.goncalo.antunes@gmail.com>",
|
||||||
|
"Pedro Teixeira <i@pgte.me>",
|
||||||
"Prashanth Chandra <coolshanth94@gmail.com>",
|
"Prashanth Chandra <coolshanth94@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"Stephen Whitmore <stephen.whitmore@gmail.com>"
|
"Stephen Whitmore <stephen.whitmore@gmail.com>"
|
||||||
|
@ -4,19 +4,25 @@ const multiaddr = require('multiaddr')
|
|||||||
const Address6 = require('ip-address').Address6
|
const Address6 = require('ip-address').Address6
|
||||||
|
|
||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
let mh
|
let ma
|
||||||
|
|
||||||
if (socket.remoteFamily === 'IPv6') {
|
if (socket.remoteFamily === 'IPv6') {
|
||||||
var addr = new Address6(socket.remoteAddress)
|
const addr = new Address6(socket.remoteAddress)
|
||||||
|
|
||||||
if (addr.v4) {
|
if (addr.v4) {
|
||||||
var ip4 = addr.to4().correctForm()
|
const ip4 = addr.to4().correctForm()
|
||||||
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + socket.remotePort)
|
ma = multiaddr('/ip4/' + ip4 +
|
||||||
|
'/tcp/' + socket.remotePort
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
mh = multiaddr('/ip6/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
|
ma = multiaddr('/ip6/' + socket.remoteAddress +
|
||||||
|
'/tcp/' + socket.remotePort
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mh = multiaddr('/ip4/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
|
ma = multiaddr('/ip4/' + socket.remoteAddress +
|
||||||
|
'/tcp/' + socket.remotePort)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mh
|
return ma
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
const net = require('net')
|
const net = require('net')
|
||||||
const toPull = require('stream-to-pull-stream')
|
const toPull = require('stream-to-pull-stream')
|
||||||
const mafmt = require('mafmt')
|
const mafmt = require('mafmt')
|
||||||
|
const withIs = require('class-is')
|
||||||
const includes = require('lodash.includes')
|
const includes = require('lodash.includes')
|
||||||
const isFunction = require('lodash.isfunction')
|
const isFunction = require('lodash.isfunction')
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
@ -21,9 +22,8 @@ class TCP {
|
|||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
callback = callback || noop
|
callback = once(callback || noop)
|
||||||
|
|
||||||
callback = once(callback)
|
|
||||||
const cOpts = ma.toOptions()
|
const cOpts = ma.toOptions()
|
||||||
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ class TCP {
|
|||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler = handler || (() => {})
|
handler = handler || noop
|
||||||
|
|
||||||
return createListener(handler)
|
return createListener(handler)
|
||||||
}
|
}
|
||||||
@ -82,4 +82,4 @@ class TCP {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TCP
|
module.exports = withIs(TCP, { className: 'TCP', symbolName: '@libp2p/js-libp2p-tcp/tcp' })
|
||||||
|
@ -55,23 +55,19 @@ module.exports = (handler) => {
|
|||||||
callback = callback || noop
|
callback = callback || noop
|
||||||
options = options || {}
|
options = options || {}
|
||||||
|
|
||||||
let closed = false
|
const timeout = setTimeout(() => {
|
||||||
server.close(callback)
|
|
||||||
|
|
||||||
server.once('close', () => {
|
|
||||||
closed = true
|
|
||||||
})
|
|
||||||
setTimeout(() => {
|
|
||||||
if (closed) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log('unable to close graciously, destroying conns')
|
log('unable to close graciously, destroying conns')
|
||||||
Object.keys(server.__connections).forEach((key) => {
|
Object.keys(server.__connections).forEach((key) => {
|
||||||
log('destroying %s', key)
|
log('destroying %s', key)
|
||||||
server.__connections[key].destroy()
|
server.__connections[key].destroy()
|
||||||
})
|
})
|
||||||
}, options.timeout || CLOSE_TIMEOUT)
|
}, options.timeout || CLOSE_TIMEOUT)
|
||||||
|
|
||||||
|
server.close(callback)
|
||||||
|
|
||||||
|
server.once('close', () => {
|
||||||
|
clearTimeout(timeout)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let ipfsId
|
let ipfsId
|
||||||
|
@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
const tests = require('interface-transport')
|
const tests = require('interface-transport')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Tcp = require('../src')
|
const TCP = require('../src')
|
||||||
|
|
||||||
describe('interface-transport compliance', () => {
|
describe('interface-transport compliance', () => {
|
||||||
tests({
|
tests({
|
||||||
setup (cb) {
|
setup (cb) {
|
||||||
let tcp = new Tcp()
|
let tcp = new TCP()
|
||||||
const addrs = [
|
const addrs = [
|
||||||
multiaddr('/ip4/127.0.0.1/tcp/9091'),
|
multiaddr('/ip4/127.0.0.1/tcp/9091'),
|
||||||
multiaddr('/ip4/127.0.0.1/tcp/9092'),
|
multiaddr('/ip4/127.0.0.1/tcp/9092'),
|
||||||
|
121
test/connection-wrap.spec.js
Normal file
121
test/connection-wrap.spec.js
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
/* 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 TCP = require('../src')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Connection = require('interface-connection').Connection
|
||||||
|
|
||||||
|
describe('Connection Wrap', () => {
|
||||||
|
let tcp
|
||||||
|
let listener
|
||||||
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
|
beforeEach((done) => {
|
||||||
|
tcp = new TCP()
|
||||||
|
listener = tcp.createListener((conn) => {
|
||||||
|
pull(conn, conn)
|
||||||
|
})
|
||||||
|
listener.on('listening', done)
|
||||||
|
listener.listen(ma)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach((done) => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('simple wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
conn.setPeerInfo('peerInfo')
|
||||||
|
const connWrap = new Connection(conn)
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(chunks).to.be.eql([Buffer.from('hey')])
|
||||||
|
|
||||||
|
connWrap.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(peerInfo).to.equal('peerInfo')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('buffer wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
const connWrap = new Connection()
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(chunks).to.be.eql([Buffer.from('hey')])
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
connWrap.setInnerConn(conn)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('overload wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
const connWrap = new Connection(conn)
|
||||||
|
connWrap.getPeerInfo = (callback) => {
|
||||||
|
callback(null, 'none')
|
||||||
|
}
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist()
|
||||||
|
})
|
||||||
|
connWrap.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(peerInfo).to.equal('none')
|
||||||
|
})
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(chunks).to.be.eql([Buffer.from('hey')])
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dial error', (done) => {
|
||||||
|
tcp.dial(multiaddr('/ip4/127.0.0.1/tcp/22234'), (err) => {
|
||||||
|
expect(err).to.exist()
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('matryoshka wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
const connWrap1 = new Connection(conn)
|
||||||
|
const connWrap2 = new Connection(connWrap1)
|
||||||
|
const connWrap3 = new Connection(connWrap2)
|
||||||
|
|
||||||
|
conn.getPeerInfo = (callback) => {
|
||||||
|
callback(null, 'inner doll')
|
||||||
|
}
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap3,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(chunks).to.eql([Buffer.from('hey')])
|
||||||
|
connWrap3.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(peerInfo).to.equal('inner doll')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
111
test/connection.spec.js
Normal file
111
test/connection.spec.js
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/* 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 TCP = require('../src')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
describe('valid Connection', () => {
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
tcp = new TCP()
|
||||||
|
})
|
||||||
|
|
||||||
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
|
it('get observed addrs', (done) => {
|
||||||
|
let dialerObsAddrs
|
||||||
|
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist()
|
||||||
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
dialerObsAddrs = addrs
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
pull(
|
||||||
|
conn,
|
||||||
|
pull.onEnd(endHandler)
|
||||||
|
)
|
||||||
|
|
||||||
|
function endHandler () {
|
||||||
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
closeAndAssert(listener, addrs)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeAndAssert (listener, addrs) {
|
||||||
|
listener.close(() => {
|
||||||
|
expect(addrs[0]).to.deep.equal(ma)
|
||||||
|
expect(dialerObsAddrs.length).to.equal(1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('get Peer Info', (done) => {
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist()
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist()
|
||||||
|
expect(peerInfo).to.not.exist()
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
pull(conn, pull.onEnd(endHandler))
|
||||||
|
|
||||||
|
function endHandler () {
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist()
|
||||||
|
expect(peerInfo).to.not.exist()
|
||||||
|
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('set Peer Info', (done) => {
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist()
|
||||||
|
conn.setPeerInfo('batatas')
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(peerInfo).to.equal('batatas')
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
pull(conn, pull.onEnd(endHandler))
|
||||||
|
|
||||||
|
function endHandler () {
|
||||||
|
conn.setPeerInfo('arroz')
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(peerInfo).to.equal('arroz')
|
||||||
|
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
15
test/constructor.spec.js
Normal file
15
test/constructor.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 TCP = require('../src')
|
||||||
|
|
||||||
|
describe('Constructor', () => {
|
||||||
|
it('create an instance', () => {
|
||||||
|
const tcp = new TCP()
|
||||||
|
expect(tcp).to.exist()
|
||||||
|
})
|
||||||
|
})
|
42
test/filter.spec.js
Normal file
42
test/filter.spec.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
const TCP = require('../src')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
describe('filter addrs', () => {
|
||||||
|
const base = '/ip4/127.0.0.1'
|
||||||
|
const ipfs = '/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw'
|
||||||
|
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
tcp = new TCP()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filter valid addrs for this transport', () => {
|
||||||
|
const ma1 = multiaddr(base + '/tcp/9090')
|
||||||
|
const ma2 = multiaddr(base + '/udp/9090')
|
||||||
|
const ma3 = multiaddr(base + '/tcp/9090/http')
|
||||||
|
const ma4 = multiaddr(base + '/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||||
|
const ma5 = multiaddr(base + '/tcp/9090/http' + ipfs)
|
||||||
|
const ma6 = multiaddr('/ip4/127.0.0.1/tcp/9090/p2p-circuit' + ipfs)
|
||||||
|
|
||||||
|
const valid = tcp.filter([ma1, ma2, ma3, ma4, ma5, ma6])
|
||||||
|
expect(valid.length).to.equal(2)
|
||||||
|
expect(valid[0]).to.deep.equal(ma1)
|
||||||
|
expect(valid[1]).to.deep.equal(ma4)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filter a single addr for this transport', () => {
|
||||||
|
const ma1 = multiaddr(base + '/tcp/9090')
|
||||||
|
|
||||||
|
const valid = tcp.filter(ma1)
|
||||||
|
expect(valid.length).to.equal(1)
|
||||||
|
expect(valid[0]).to.eql(ma1)
|
||||||
|
})
|
||||||
|
})
|
@ -9,16 +9,8 @@ chai.use(dirtyChai)
|
|||||||
const TCP = require('../src')
|
const TCP = require('../src')
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Connection = require('interface-connection').Connection
|
|
||||||
const isCI = process.env.CI
|
const isCI = process.env.CI
|
||||||
|
|
||||||
describe('instantiate the transport', () => {
|
|
||||||
it('create', () => {
|
|
||||||
const tcp = new TCP()
|
|
||||||
expect(tcp).to.exist()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('listen', () => {
|
describe('listen', () => {
|
||||||
let tcp
|
let tcp
|
||||||
|
|
||||||
@ -266,250 +258,3 @@ describe('dial', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('filter addrs', () => {
|
|
||||||
let tcp
|
|
||||||
|
|
||||||
before(() => {
|
|
||||||
tcp = new TCP()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('filter valid addrs for this transport', () => {
|
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
||||||
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
|
||||||
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http')
|
|
||||||
const mh4 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
|
||||||
const mh5 = multiaddr('/ip4/127.0.0.1/tcp/9090/http/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
|
||||||
const mh6 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw' +
|
|
||||||
'/p2p-circuit/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
|
||||||
|
|
||||||
const valid = tcp.filter([mh1, mh2, mh3, mh4, mh5, mh6])
|
|
||||||
expect(valid.length).to.equal(2)
|
|
||||||
expect(valid[0]).to.deep.equal(mh1)
|
|
||||||
expect(valid[1]).to.deep.equal(mh4)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('filter a single addr for this transport', () => {
|
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
||||||
|
|
||||||
const valid = tcp.filter(mh1)
|
|
||||||
expect(valid.length).to.equal(1)
|
|
||||||
expect(valid[0]).to.deep.equal(mh1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('valid Connection', () => {
|
|
||||||
let tcp
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
tcp = new TCP()
|
|
||||||
})
|
|
||||||
|
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
||||||
|
|
||||||
it('get observed addrs', (done) => {
|
|
||||||
let dialerObsAddrs
|
|
||||||
|
|
||||||
const listener = tcp.createListener((conn) => {
|
|
||||||
expect(conn).to.exist()
|
|
||||||
conn.getObservedAddrs((err, addrs) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
dialerObsAddrs = addrs
|
|
||||||
pull(pull.empty(), conn)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
listener.listen(ma, () => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
pull(
|
|
||||||
conn,
|
|
||||||
pull.onEnd(endHandler)
|
|
||||||
)
|
|
||||||
|
|
||||||
function endHandler () {
|
|
||||||
conn.getObservedAddrs((err, addrs) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
pull(pull.empty(), conn)
|
|
||||||
closeAndAssert(listener, addrs)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function closeAndAssert (listener, addrs) {
|
|
||||||
listener.close(() => {
|
|
||||||
expect(addrs[0]).to.deep.equal(ma)
|
|
||||||
expect(dialerObsAddrs.length).to.equal(1)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('get Peer Info', (done) => {
|
|
||||||
const listener = tcp.createListener((conn) => {
|
|
||||||
expect(conn).to.exist()
|
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.exist()
|
|
||||||
expect(peerInfo).to.not.exist()
|
|
||||||
pull(pull.empty(), conn)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
listener.listen(ma, () => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
|
|
||||||
pull(conn, pull.onEnd(endHandler))
|
|
||||||
|
|
||||||
function endHandler () {
|
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.exist()
|
|
||||||
expect(peerInfo).to.not.exist()
|
|
||||||
|
|
||||||
listener.close(done)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('set Peer Info', (done) => {
|
|
||||||
const listener = tcp.createListener((conn) => {
|
|
||||||
expect(conn).to.exist()
|
|
||||||
conn.setPeerInfo('batatas')
|
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(peerInfo).to.equal('batatas')
|
|
||||||
pull(pull.empty(), conn)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
listener.listen(ma, () => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
|
|
||||||
pull(conn, pull.onEnd(endHandler))
|
|
||||||
|
|
||||||
function endHandler () {
|
|
||||||
conn.setPeerInfo('arroz')
|
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(peerInfo).to.equal('arroz')
|
|
||||||
|
|
||||||
listener.close(done)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe.skip('turbolence', () => {
|
|
||||||
it('dialer - emits error on the other end is terminated abruptly', (done) => {})
|
|
||||||
it('listener - emits error on the other end is terminated abruptly', (done) => {})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Connection wrap', () => {
|
|
||||||
let tcp
|
|
||||||
let listener
|
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
||||||
|
|
||||||
beforeEach((done) => {
|
|
||||||
tcp = new TCP()
|
|
||||||
listener = tcp.createListener((conn) => {
|
|
||||||
pull(conn, conn)
|
|
||||||
})
|
|
||||||
listener.on('listening', done)
|
|
||||||
listener.listen(ma)
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach((done) => {
|
|
||||||
listener.close(done)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('simple wrap', (done) => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
conn.setPeerInfo('peerInfo')
|
|
||||||
const connWrap = new Connection(conn)
|
|
||||||
pull(
|
|
||||||
pull.values(['hey']),
|
|
||||||
connWrap,
|
|
||||||
pull.collect((err, chunks) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(chunks).to.be.eql([Buffer.from('hey')])
|
|
||||||
|
|
||||||
connWrap.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(peerInfo).to.equal('peerInfo')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('buffer wrap', (done) => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
const connWrap = new Connection()
|
|
||||||
pull(
|
|
||||||
pull.values(['hey']),
|
|
||||||
connWrap,
|
|
||||||
pull.collect((err, chunks) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(chunks).to.be.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
connWrap.setInnerConn(conn)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('overload wrap', (done) => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
const connWrap = new Connection(conn)
|
|
||||||
connWrap.getPeerInfo = (callback) => {
|
|
||||||
callback(null, 'none')
|
|
||||||
}
|
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.exist()
|
|
||||||
})
|
|
||||||
connWrap.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(peerInfo).to.equal('none')
|
|
||||||
})
|
|
||||||
pull(
|
|
||||||
pull.values(['hey']),
|
|
||||||
connWrap,
|
|
||||||
pull.collect((err, chunks) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(chunks).to.be.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('dial error', (done) => {
|
|
||||||
tcp.dial(multiaddr('/ip4/999.0.0.1/tcp/1234'), (err) => {
|
|
||||||
expect(err).to.exist()
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('matryoshka wrap', (done) => {
|
|
||||||
const conn = tcp.dial(ma)
|
|
||||||
const connWrap1 = new Connection(conn)
|
|
||||||
const connWrap2 = new Connection(connWrap1)
|
|
||||||
const connWrap3 = new Connection(connWrap2)
|
|
||||||
|
|
||||||
conn.getPeerInfo = (callback) => {
|
|
||||||
callback(null, 'inner doll')
|
|
||||||
}
|
|
||||||
pull(
|
|
||||||
pull.values(['hey']),
|
|
||||||
connWrap3,
|
|
||||||
pull.collect((err, chunks) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(chunks).to.be.eql([Buffer.from('hey')])
|
|
||||||
connWrap3.getPeerInfo((err, peerInfo) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(peerInfo).to.equal('inner doll')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
16
test/turbolence.spec.js
Normal file
16
test/turbolence.spec.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
// const TCP = require('../src')
|
||||||
|
|
||||||
|
describe.skip('turbolence', () => {
|
||||||
|
it('dialer - emits error on the other end is terminated abruptly', (done) => {
|
||||||
|
expect('ok').to.equal('ok')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listener - emits error on the other end is terminated abruptly', (done) => {})
|
||||||
|
})
|
Reference in New Issue
Block a user