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 Sep 29, 2019
1 parent 7309e3e commit 310730b
Show file tree
Hide file tree
Showing 17 changed files with 501 additions and 328 deletions.
16 changes: 11 additions & 5 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
'use strict'

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

const mockUpgrader = {
upgradeInbound: maConn => maConn,
upgradeOutbound: maConn => maConn
}

function boot (done) {
const wd = new WebRTCDirect()
listener = wd.createListener((conn) => pull(conn, conn))
listener.listen(ma, done)
const wd = new WebRTCDirect({ upgrader: mockUpgrader })
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
28 changes: 0 additions & 28 deletions .npmignore

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir build --bundlesize
- 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
27 changes: 14 additions & 13 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,26 +43,28 @@
},
"homepage": "https://github.com/libp2p/js-libp2p-webrtc-direct#readme",
"devDependencies": {
"aegir": "^18.2.1",
"aegir": "^20.3.1",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"gulp": "^4.0.0",
"multiaddr": "^6.0.6",
"gulp": "^4.0.2",
"multiaddr": "^7.1.0",
"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",
"interface-connection": "~0.3.3",
"mafmt": "^6.0.7",
"err-code": "^2.0.0",
"interface-transport": "libp2p/interface-transport#chore/skip-abort-while-reading-for-webrtc",
"libp2p-utils": "^0.1.0",
"mafmt": "^7.0.0",
"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",
"wrtc": "~0.2.1",
"simple-peer": "9.6.0",
"stream-to-it": "^0.1.1",
"wrtc": "~0.4.2",
"xhr": "^2.5.0"
},
"contributors": [
Expand Down
9 changes: 9 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

// p2p multi-address code
exports.CODE_P2P = 421
exports.CODE_CIRCUIT = 290

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

0 comments on commit 310730b

Please sign in to comment.