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

Commit 4def4aa

Browse files
authored
refactor: switch to async iterators (#30)
BREAKING CHANGE: Switch to using async/await and async iterators. The transport and connection interfaces have changed.
1 parent 7309e3e commit 4def4aa

17 files changed

+535
-363
lines changed

.aegir.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
'use strict'
22

33
const WebRTCDirect = require('./src')
4-
const pull = require('pull-stream')
4+
const pipe = require('it-pipe')
55
const multiaddr = require('multiaddr')
66

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

10-
function boot (done) {
11-
const wd = new WebRTCDirect()
12-
listener = wd.createListener((conn) => pull(conn, conn))
13-
listener.listen(ma, done)
10+
const mockUpgrader = {
11+
upgradeInbound: maConn => maConn,
12+
upgradeOutbound: maConn => maConn
13+
}
14+
15+
function boot () {
16+
const wd = new WebRTCDirect({ upgrader: mockUpgrader })
17+
18+
listener = wd.createListener((conn) => pipe(conn, conn))
1419
listener.on('listening', () => {
15-
console.log('gulp listener started on:', ma.toString())
20+
console.log('listener started on:', ma.toString())
1621
})
22+
listener.on('error', console.error)
23+
return listener.listen(ma)
1724
}
1825

19-
function shutdown (done) {
20-
listener.close(done)
26+
function shutdown () {
27+
return listener.close()
2128
}
2229

2330
module.exports = {

.npmignore

Lines changed: 0 additions & 28 deletions
This file was deleted.

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ stages:
77

88
node_js:
99
- '10'
10+
- '12'
1011

1112
os:
1213
- linux
@@ -19,7 +20,7 @@ jobs:
1920
include:
2021
- stage: check
2122
script:
22-
- npx aegir commitlint --travis
23+
- npx aegir build --bundlesize
2324
- npx aegir dep-check -- -i wrtc -i electron-webrtc
2425
- npm run lint
2526

README.md

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,33 @@
3939
```js
4040
const WebRTCDirect = require('libp2p-webrtc-direct')
4141
const multiaddr = require('multiaddr')
42-
const pull = require('pull-stream')
42+
const pipe = require('pull-stream')
43+
const { collect } = require('streaming-iterables')
4344

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

4647
const webRTCDirect = new WebRTCDirect()
4748

4849
const listener = webRTCDirect.createListener((socket) => {
4950
console.log('new connection opened')
50-
pull(
51-
pull.values(['hello']),
51+
pipe(
52+
['hello'],
5253
socket
5354
)
5455
})
5556

56-
listener.listen(mh, () => {
57-
console.log('listening')
58-
59-
webRTCDirect.dial(mh, (err, conn) => {
60-
if (!err) {
61-
pull(
62-
conn,
63-
pull.collect((err, values) => {
64-
if (!err) {
65-
console.log(`Value: ${values.toString()}`)
66-
} else {
67-
console.log(`Error: ${err}`)
68-
}
69-
70-
// Close connection after reading
71-
listener.close()
72-
}),
73-
)
74-
} else {
75-
console.log(`Error: ${err}`)
76-
}
77-
})
78-
})
57+
await listener.listen(addr)
58+
console.log('listening')
59+
60+
const conn = await webRTCDirect.dial(addr)
61+
const values = await pipe(
62+
conn,
63+
collect
64+
)
65+
console.log(`Value: ${values.toString()}`)
66+
67+
// Close connection after reading
68+
await listener.close()
7969
```
8070

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

9080
## API
9181

92-
Follows the interface defined by `interface-transport`
93-
94-
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
82+
### Transport
9583

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

98-
### This module uses `pull-streams`
86+
### Connection
9987

100-
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).
88+
[![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)](https://github.com/libp2p/interface-connection)
10189

102-
You can learn more about pull-streams at:
90+
## Contribute
10391

104-
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
105-
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
106-
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
107-
- [pull-streams documentation](https://pull-stream.github.io/)
92+
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:
10893

109-
#### Converting `pull-streams` to Node.js Streams
94+
- 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.
95+
- **Perform code reviews**.
96+
- **Add tests**. There can never be enough tests.
11097

111-
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:
112-
113-
```JavaScript
114-
const pullToStream = require('pull-stream-to-stream')
115-
116-
const nodeStreamInstance = pullToStream(pullStreamInstance)
117-
// nodeStreamInstance is an instance of a Node.js Stream
118-
```
98+
## License
11999

120-
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
100+
[MIT](LICENSE) © Protocol Labs

gulpfile.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212
"scripts": {
1313
"lint": "aegir lint",
1414
"build": "aegir build",
15+
"docs": "aegir docs",
1516
"test": "aegir test --target node --target browser",
1617
"test:node": "aegir test --target node",
1718
"test:browser": "aegir test --target browser",
1819
"release": "aegir release --target node --target browser",
1920
"release-minor": "aegir release --type minor --target node --target browser",
2021
"release-major": "aegir release --type major --target node --target browser",
21-
"coverage": "aegir coverage",
22-
"coverage-publish": "aegir coverage --provider coveralls"
22+
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
2323
},
2424
"pre-push": [
25-
"lint",
26-
"test"
25+
"lint"
2726
],
2827
"repository": {
2928
"type": "git",
@@ -44,26 +43,27 @@
4443
},
4544
"homepage": "https://github.com/libp2p/js-libp2p-webrtc-direct#readme",
4645
"devDependencies": {
47-
"aegir": "^18.2.1",
46+
"aegir": "^20.3.1",
4847
"chai": "^4.2.0",
4948
"dirty-chai": "^2.0.1",
50-
"gulp": "^4.0.0",
51-
"multiaddr": "^6.0.6",
49+
"multiaddr": "^7.1.0",
5250
"webrtcsupport": "^2.2.0"
5351
},
5452
"dependencies": {
53+
"abortable-iterator": "^2.1.0",
5554
"class-is": "^1.1.0",
5655
"concat-stream": "^2.0.0",
5756
"detect-node": "^2.0.4",
58-
"interface-connection": "~0.3.3",
59-
"mafmt": "^6.0.7",
57+
"err-code": "^2.0.0",
58+
"interface-transport": "libp2p/interface-transport#chore/skip-abort-while-reading-for-webrtc",
59+
"libp2p-utils": "^0.1.0",
60+
"mafmt": "^7.0.0",
6061
"multibase": "~0.6.0",
6162
"once": "^1.4.0",
62-
"pull-stream": "^3.6.9",
6363
"request": "^2.88.0",
64-
"simple-peer": "9.3.0",
65-
"stream-to-pull-stream": "^1.7.3",
66-
"wrtc": "~0.2.1",
64+
"simple-peer": "9.6.0",
65+
"stream-to-it": "^0.1.1",
66+
"wrtc": "~0.4.2",
6767
"xhr": "^2.5.0"
6868
},
6969
"contributors": [

src/constants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
// p2p multi-address code
4+
exports.CODE_P2P = 421
5+
exports.CODE_CIRCUIT = 290
6+
7+
// Time to wait for a connection to close gracefully before destroying it
8+
// manually
9+
module.exports.CLOSE_TIMEOUT = 2000

0 commit comments

Comments
 (0)