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

Commit

Permalink
refactor: switch to async iterators
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Switch to using async/await and async iterators. The transport and connection interfaces have changed.
  • Loading branch information
vasco-santos committed Aug 21, 2019
1 parent 7309e3e commit adfbd40
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 217 deletions.
8 changes: 5 additions & 3 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

const WebRTCDirect = require('./src')
const pull = require('pull-stream')
const pipe = require('it-pipe')
const multiaddr = require('multiaddr')

const ma = multiaddr('/ip4/127.0.0.1/tcp/12345/http/p2p-webrtc-direct')
let listener

function boot (done) {
const wd = new WebRTCDirect()
listener = wd.createListener((conn) => pull(conn, conn))
listener.listen(ma, done)
listener = wd.createListener((conn) => pipe(conn, conn))
listener.on('listening', () => {
console.log('gulp listener started on:', ma.toString())
})
listener.listen(ma).then(() => done()).catch(done)
listener.on('error', console.error)
}

function shutdown (done) {
listener.close(done)
listener.close().then(done).catch(done)
}

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check -- -i wrtc -i electron-webrtc
- npm run lint

Expand Down
76 changes: 28 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,33 @@
```js
const WebRTCDirect = require('libp2p-webrtc-direct')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const pipe = require('pull-stream')
const { collect } = require('streaming-iterables')

const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const addr = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')

const webRTCDirect = new WebRTCDirect()

const listener = webRTCDirect.createListener((socket) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
pipe(
['hello'],
socket
)
})

listener.listen(mh, () => {
console.log('listening')

webRTCDirect.dial(mh, (err, conn) => {
if (!err) {
pull(
conn,
pull.collect((err, values) => {
if (!err) {
console.log(`Value: ${values.toString()}`)
} else {
console.log(`Error: ${err}`)
}

// Close connection after reading
listener.close()
}),
)
} else {
console.log(`Error: ${err}`)
}
})
})
await listener.listen(addr)
console.log('listening')

const conn = await webRTCDirect.dial(addr)
const values = await pipe(
conn,
collect
)
console.log(`Value: ${values.toString()}`)

// Close connection after reading
await listener.close()
```

Outputs:
Expand All @@ -89,32 +79,22 @@ Note that it may take some time for the connection to be established.

## API

Follows the interface defined by `interface-transport`

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

## Pull-streams
[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)

### This module uses `pull-streams`
### Connection

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 why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
[![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)](https://github.com/libp2p/interface-connection)

You can learn more about pull-streams at:
## Contribute

- [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/)
The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:

#### Converting `pull-streams` to Node.js Streams
- Go through the modules and **check out existing issues**. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
- **Perform code reviews**.
- **Add tests**. There can never be enough tests.

If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:

```JavaScript
const pullToStream = require('pull-stream-to-stream')

const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
```
## License

To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
[MIT](LICENSE) © Protocol Labs
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function boot (done) {
listener = wd.createListener((conn) => pull(conn, conn))
listener.listen(ma, done)
listener.on('listening', () => {
/* eslint-disable no-console */
console.log('gulp listener started on:', ma.toString())
/* eslint-enable no-console */
})
}

Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"docs": "aegir docs",
"test": "aegir test --target node --target browser",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
"release": "aegir release --target node --target browser",
"release-minor": "aegir release --type minor --target node --target browser",
"release-major": "aegir release --type major --target node --target browser",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider coveralls"
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
},
"pre-push": [
"lint",
"test"
"lint"
],
"repository": {
"type": "git",
Expand All @@ -44,25 +43,28 @@
},
"homepage": "https://github.com/libp2p/js-libp2p-webrtc-direct#readme",
"devDependencies": {
"aegir": "^18.2.1",
"aegir": "^20.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"gulp": "^4.0.0",
"gulp": "^4.0.2",
"multiaddr": "^6.0.6",
"webrtcsupport": "^2.2.0"
},
"dependencies": {
"abortable-iterator": "^2.1.0",
"class-is": "^1.1.0",
"concat-stream": "^2.0.0",
"detect-node": "^2.0.4",
"err-code": "^2.0.0",
"interface-connection": "~0.3.3",
"mafmt": "^6.0.7",
"interface-transport": "^0.5.2",
"lodash.includes": "^4.3.0",
"mafmt": "^6.0.8",
"multibase": "~0.6.0",
"once": "^1.4.0",
"pull-stream": "^3.6.9",
"request": "^2.88.0",
"simple-peer": "9.3.0",
"stream-to-pull-stream": "^1.7.3",
"simple-peer": "9.5.0",
"stream-to-it": "^0.1.0",
"wrtc": "~0.2.1",
"xhr": "^2.5.0"
},
Expand Down
17 changes: 17 additions & 0 deletions src/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const { Adapter } = require('interface-transport')
const withIs = require('class-is')
const WebRTCDirect = require('.')

// Legacy adapter to old transport & connection interface
class WebRTCDirectAdapter extends Adapter {
constructor () {
super(new WebRTCDirect())
}
}

module.exports = withIs(WebRTCDirectAdapter, {
className: 'WebRTCDirect',
symbolName: '@libp2p/js-libp2p-webrtc-direct/webrtcdirect'
})
5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

// Time to wait for a connection to close gracefully before destroying it
// manually
module.exports.CLOSE_TIMEOUT = 2000
Loading

0 comments on commit adfbd40

Please sign in to comment.