This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
@@ -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') | ||
|
||
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 | ||
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RichardLitt could you review this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]>", | ||
|
@@ -53,4 +54,4 @@ | |
"Stephen Whitmore <[email protected]>", | ||
"dignifiedquire <[email protected]>" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.log
sThere was a problem hiding this comment.
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..