Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 2f2ca0d

Browse files
authored
Merge pull request #20 from libp2p/pull
Migrate to pull-streams
2 parents caa8d6d + da8ee21 commit 2f2ca0d

8 files changed

+427
-419
lines changed

README.md

+48-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
js-libp2p-tcp
2-
===============
1+
# js-libp2p-tcp
32

43
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
54
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
@@ -11,49 +10,53 @@ js-libp2p-tcp
1110
![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)
1211
![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)
1312

14-
> Node.js implementation of the TCP module that libp2p uses, which implements
15-
> the [interface-connection](https://github.com/libp2p/interface-connection)
16-
> interface for dial/listen.
13+
> 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.
1714
1815
## Description
1916

20-
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a
21-
`multiaddr`. This small shim will enable libp2p to use other different
22-
transports.
17+
`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.
18+
19+
**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
2320

2421
## Example
2522

2623
```js
2724
const TCP = require('libp2p-tcp')
2825
const multiaddr = require('multiaddr')
26+
const pull = require('pull-stream')
2927

3028
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
3129
const mh2 = multiaddr('/ip6/::/tcp/9092')
3230

3331
const tcp = new TCP()
3432

35-
var listener = tcp.createListener(mh1, function handler (socket) {
36-
console.log('connection')
37-
socket.end('bye')
33+
const listener = tcp.createListener(mh1, (socket) => {
34+
console.log('new connection opened')
35+
pull(
36+
pull.values(['hello']),
37+
socket
38+
)
3839
})
3940

40-
listener.listen(mh1, function ready () {
41-
console.log('ready')
41+
listener.listen(() => {
42+
console.log('listening')
4243

43-
const client = tcp.dial(mh1)
44-
client.pipe(process.stdout)
45-
client.on('end', () => {
46-
listener.close()
47-
})
44+
pull(
45+
tcp.dial(mh1),
46+
pull.log,
47+
pull.onEnd(() => {
48+
tcp.close()
49+
})
50+
)
4851
})
4952
```
5053

5154
outputs
5255

5356
```
54-
ready
55-
connection
56-
bye
57+
listening
58+
new connection opened
59+
hello
5760
```
5861

5962
## Installation
@@ -64,6 +67,30 @@ bye
6467
> npm i libp2p-tcp
6568
```
6669

70+
## This module uses `pull-streams`
71+
72+
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 what took us to make this migration, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
73+
74+
You can learn more about pull-streams at:
75+
76+
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
77+
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
78+
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
79+
- [pull-streams documentation](https://pull-stream.github.io/)
80+
81+
### Converting `pull-streams` to Node.js Streams
82+
83+
If you are a Node.js streams user, you can convert a pull-stream to Node.js Stream using the module `pull-stream-to-stream`, giving you an instance of a Node.js stream that is linked to the pull-stream. Example:
84+
85+
```
86+
const pullToStream = require('pull-stream-to-stream')
87+
88+
const nodeStreamInstance = pullToStream(pullStreamInstance)
89+
// nodeStreamInstance is an instance of a Node.js Stream
90+
```
91+
92+
To learn more about his utility, visit https://pull-stream.github.io/#pull-stream-to-stream
93+
6794
## API
6895

6996
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/diasdavid/interface-transport)

package.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@
3232
},
3333
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
3434
"devDependencies": {
35-
"aegir": "^4.0.0",
35+
"aegir": "^6.0.1",
3636
"chai": "^3.5.0",
37-
"interface-transport": "^0.2.0",
38-
"pre-commit": "^1.1.2",
39-
"tape": "^4.5.1"
37+
"interface-transport": "^0.3.3",
38+
"lodash.isfunction": "^3.0.8",
39+
"pre-commit": "^1.1.2"
4040
},
4141
"dependencies": {
42-
"interface-connection": "0.1.8",
42+
"interface-connection": "0.2.1",
4343
"ip-address": "^5.8.0",
4444
"lodash.contains": "^2.4.3",
4545
"mafmt": "^2.1.2",
4646
"multiaddr": "^2.0.2",
47-
"run-parallel": "^1.1.6"
47+
"pull": "^2.1.1",
48+
"stream-to-pull-stream": "^1.7.0"
4849
},
4950
"contributors": [
5051
"David Dias <[email protected]>",
@@ -53,4 +54,4 @@
5354
"Stephen Whitmore <[email protected]>",
5455
"dignifiedquire <[email protected]>"
5556
]
56-
}
57+
}

src/get-multiaddr.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const multiaddr = require('multiaddr')
4+
const Address6 = require('ip-address').Address6
5+
6+
module.exports = (socket) => {
7+
var mh
8+
9+
if (socket.remoteFamily === 'IPv6') {
10+
var addr = new Address6(socket.remoteAddress)
11+
if (addr.v4) {
12+
var ip4 = addr.to4().correctForm()
13+
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + socket.remotePort)
14+
} else {
15+
mh = multiaddr('/ip6/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
16+
}
17+
} else {
18+
mh = multiaddr('/ip4/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
19+
}
20+
21+
return mh
22+
}

0 commit comments

Comments
 (0)