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

Migrate to pull-streams #20

Merged
merged 3 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)
![](https://raw.githubusercontent.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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe as a debug log with more info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uups looks like I forgot some console.logs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm this is the readme..


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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RichardLitt could you review this pull-streams segment. Thank you :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RichardLitt ping :)

## API

[![](https://raw.githubusercontent.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 <[email protected]>",
Expand All @@ -53,4 +54,4 @@
"Stephen Whitmore <[email protected]>",
"dignifiedquire <[email protected]>"
]
}
}
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