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

Commit

Permalink
Merge pull request #20 from libp2p/pull
Browse files Browse the repository at this point in the history
Migrate to pull-streams
  • Loading branch information
daviddias committed Sep 6, 2016
2 parents caa8d6d + da8ee21 commit 2f2ca0d
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 419 deletions.
69 changes: 48 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
js-libp2p-tcp
===============
# js-libp2p-tcp

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

> 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.
> 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.
## Description

`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.
`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.

**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.

## Example

```js
const TCP = require('libp2p-tcp')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')

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

const tcp = new TCP()

var listener = tcp.createListener(mh1, function handler (socket) {
console.log('connection')
socket.end('bye')
const listener = tcp.createListener(mh1, (socket) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
socket
)
})

listener.listen(mh1, function ready () {
console.log('ready')
listener.listen(() => {
console.log('listening')

const client = tcp.dial(mh1)
client.pipe(process.stdout)
client.on('end', () => {
listener.close()
})
pull(
tcp.dial(mh1),
pull.log,
pull.onEnd(() => {
tcp.close()
})
)
})
```

outputs

```
ready
connection
bye
listening
new connection opened
hello
```

## Installation
Expand All @@ -64,6 +67,30 @@ bye
> npm i libp2p-tcp
```

## 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 what took us to make this migration, 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 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:

```
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
```

To learn more about his utility, visit https://pull-stream.github.io/#pull-stream-to-stream

## API

[![](https://github.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/diasdavid/interface-transport)
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@
},
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
"devDependencies": {
"aegir": "^4.0.0",
"aegir": "^6.0.1",
"chai": "^3.5.0",
"interface-transport": "^0.2.0",
"pre-commit": "^1.1.2",
"tape": "^4.5.1"
"interface-transport": "^0.3.3",
"lodash.isfunction": "^3.0.8",
"pre-commit": "^1.1.2"
},
"dependencies": {
"interface-connection": "0.1.8",
"interface-connection": "0.2.1",
"ip-address": "^5.8.0",
"lodash.contains": "^2.4.3",
"mafmt": "^2.1.2",
"multiaddr": "^2.0.2",
"run-parallel": "^1.1.6"
"pull": "^2.1.1",
"stream-to-pull-stream": "^1.7.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand All @@ -53,4 +54,4 @@
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
22 changes: 22 additions & 0 deletions src/get-multiaddr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const multiaddr = require('multiaddr')
const Address6 = require('ip-address').Address6

module.exports = (socket) => {
var mh

if (socket.remoteFamily === 'IPv6') {
var addr = new Address6(socket.remoteAddress)
if (addr.v4) {
var ip4 = addr.to4().correctForm()
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + socket.remotePort)
} else {
mh = multiaddr('/ip6/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
}
} else {
mh = multiaddr('/ip4/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
}

return mh
}
Loading

0 comments on commit 2f2ca0d

Please sign in to comment.