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

Migrate to pull-streams #12

Merged
merged 4 commits into from
Sep 5, 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
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,60 @@ js-libp2p-webrtc-star

[![](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)
[![Build Status](https://travis-ci.org/diasdavid/js-libp2p-webrtc-star.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-webrtc-star)
[![Build Status](https://travis-ci.org/libp2p/js-libp2p-webrtc-star.svg?style=flat-square)](https://travis-ci.org/libp2p/js-libp2p-webrtc-star)
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-webrtc-star.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-webrtc-star)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-webrtc-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-star)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)

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

> libp2p WebRTC transport that includes a discovery mechanism provided by the signalling-star

## Description

`libp2p-webrtc-star` is one of the WebRTC transports available for libp2p. `libp2p-webrtc-star incorporates both a transport and a discovery service that is facilitated by the signalling server, also part of this module.

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

## Example

```
TODO
```

## Installation

### npm

```sh
> npm i libp2p-webrtc-star
```

## 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://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/diasdavid/interface-transport)
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"repository": {
"type": "git",
"url": "git+https://github.com/diasdavid/js-libp2p-webrtc-star.git"
"url": "git+https://github.com/libp2p/js-libp2p-webrtc-star.git"
},
"keywords": [
"IPFS",
Expand All @@ -34,11 +34,11 @@
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/js-libp2p-webrtc-star/issues"
"url": "https://github.com/libp2p/js-libp2p-webrtc-star/issues"
},
"homepage": "https://github.com/diasdavid/js-libp2p-webrtc-star#readme",
"homepage": "https://github.com/libp2p/js-libp2p-webrtc-star#readme",
"devDependencies": {
"aegir": "^6.0.0",
"aegir": "^6.0.1",
"bl": "^1.1.2",
"chai": "^3.5.0",
"pre-commit": "^1.1.3",
Expand All @@ -47,20 +47,21 @@
},
"dependencies": {
"debug": "^2.2.0",
"duplexify": "^3.4.3",
"hapi": "^15.0.2",
"interface-connection": "^0.1.8",
"hapi": "^15.0.3",
"interface-connection": "^0.2.1",
"mafmt": "^2.1.0",
"minimist": "^1.2.0",
"peer-id": "^0.7.0",
"peer-info": "^0.7.0",
"pull-stream": "^3.4.3",
"run-parallel": "^1.1.6",
"simple-peer": "^6.0.4",
"socket.io": "^1.4.6",
"socket.io-client": "^1.4.6"
"socket.io-client": "^1.4.6",
"stream-to-pull-stream": "^1.7.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
15 changes: 9 additions & 6 deletions src/webrtc-star/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const SimplePeer = require('simple-peer')
const peerId = require('peer-id')
const PeerInfo = require('peer-info')
const Connection = require('interface-connection').Connection
const toPull = require('stream-to-pull-stream')

exports = module.exports = WebRTCStar

Expand Down Expand Up @@ -41,7 +42,8 @@ function WebRTCStar () {
const sioClient = listeners[Object.keys(listeners)[0]].io
const channel = new SimplePeer({ initiator: true, trickle: false })

const conn = new Connection(channel)
const conn = new Connection(toPull.duplex(channel))
let connected = false

channel.on('signal', function (signal) {
sioClient.emit('ss-handshake', {
Expand All @@ -53,12 +55,13 @@ function WebRTCStar () {
})

channel.on('timeout', () => {
conn.emit('timeout')
callback(new Error('timeout'))
})

channel.on('error', (err) => {
callback(err)
conn.emit('error', err)
if (!connected) {
callback(err)
}
})

sioClient.on('ws-handshake', (offer) => {
Expand All @@ -67,6 +70,7 @@ function WebRTCStar () {
}

channel.on('connect', () => {
connected = true
conn.destroy = channel.destroy.bind(channel)

channel.on('close', () => {
Expand All @@ -77,7 +81,6 @@ function WebRTCStar () {
return callback(null, [ma])
}

conn.emit('connect')
callback(null, conn)
})

Expand Down Expand Up @@ -117,7 +120,7 @@ function WebRTCStar () {
if (offer.answer) { return }

const channel = new SimplePeer({ trickle: false })
const conn = Connection(channel)
const conn = new Connection(toPull.duplex(channel))

channel.on('connect', () => {
conn.getObservedAddrs = (callback) => {
Expand Down
10 changes: 3 additions & 7 deletions test/webrtc-star/test-dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const expect = require('chai').expect
const multiaddr = require('multiaddr')
const series = require('run-series')
const pull = require('pull-stream')

const WebRTCStar = require('../../src/webrtc-star')

Expand All @@ -25,7 +26,7 @@ describe('dial', () => {
ws1 = new WebRTCStar()

const listener = ws1.createListener((conn) => {
conn.pipe(conn)
pull(conn, conn)
})

listener.listen(ma1, next)
Expand All @@ -35,7 +36,7 @@ describe('dial', () => {
ws2 = new WebRTCStar()

const listener = ws2.createListener((conn) => {
conn.pipe(conn)
pull(conn, conn)
})
listener.listen(ma2, next)
}
Expand All @@ -48,11 +49,6 @@ describe('dial', () => {
})
})

it('dial on IPv4, check for connect event', (done) => {
const conn = ws1.dial(ma2)
conn.on('connect', done)
})

it.skip('dial on IPv6', (done) => {
// TODO IPv6 not supported yet
})
Expand Down
8 changes: 4 additions & 4 deletions test/webrtc-star/test-valid-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const expect = require('chai').expect
const multiaddr = require('multiaddr')
const series = require('run-series')
const pull = require('pull-stream')

const WebRTCStar = require('../../src/webrtc-star')

Expand All @@ -26,7 +27,7 @@ describe('valid Connection', () => {
ws1 = new WebRTCStar()

const listener = ws1.createListener((conn) => {
conn.pipe(conn)
pull(conn, conn)
})

listener.listen(ma1, next)
Expand All @@ -36,14 +37,13 @@ describe('valid Connection', () => {
ws2 = new WebRTCStar()

const listener = ws2.createListener((conn) => {
conn.pipe(conn)
pull(conn, conn)
})
listener.listen(ma2, next)
}

function dial () {
conn = ws1.dial(ma2)
conn.on('connect', done)
conn = ws1.dial(ma2, done)
}
})

Expand Down