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

refactor: switch to async iterators #30

Merged
merged 2 commits into from
Oct 2, 2019
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
23 changes: 15 additions & 8 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
'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

function boot (done) {
const wd = new WebRTCDirect()
listener = wd.createListener((conn) => pull(conn, conn))
listener.listen(ma, done)
const mockUpgrader = {
upgradeInbound: maConn => maConn,
upgradeOutbound: maConn => maConn
}

function boot () {
const wd = new WebRTCDirect({ upgrader: mockUpgrader })

listener = wd.createListener((conn) => pipe(conn, conn))
listener.on('listening', () => {
console.log('gulp listener started on:', ma.toString())
console.log('listener started on:', ma.toString())
})
listener.on('error', console.error)
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
return listener.listen(ma)
}

function shutdown (done) {
listener.close(done)
function shutdown () {
return listener.close()
}

module.exports = {
Expand Down
28 changes: 0 additions & 28 deletions .npmignore

This file was deleted.

3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ stages:

node_js:
- '10'
- '12'

os:
- linux
Expand All @@ -19,7 +20,7 @@ jobs:
include:
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
- 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
29 changes: 0 additions & 29 deletions gulpfile.js

This file was deleted.

26 changes: 13 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,27 @@
},
"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",
"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