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

Commit adfbd40

Browse files
committed
refactor: switch to async iterators
BREAKING CHANGE: Switch to using async/await and async iterators. The transport and connection interfaces have changed.
1 parent 7309e3e commit adfbd40

File tree

12 files changed

+325
-217
lines changed

12 files changed

+325
-217
lines changed

.aegir.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
const WebRTCDirect = require('./src')
44
const pull = require('pull-stream')
5+
const pipe = require('it-pipe')
56
const multiaddr = require('multiaddr')
67

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

1011
function boot (done) {
1112
const wd = new WebRTCDirect()
12-
listener = wd.createListener((conn) => pull(conn, conn))
13-
listener.listen(ma, done)
13+
listener = wd.createListener((conn) => pipe(conn, conn))
1414
listener.on('listening', () => {
1515
console.log('gulp listener started on:', ma.toString())
1616
})
17+
listener.listen(ma).then(() => done()).catch(done)
18+
listener.on('error', console.error)
1719
}
1820

1921
function shutdown (done) {
20-
listener.close(done)
22+
listener.close().then(done).catch(done)
2123
}
2224

2325
module.exports = {

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
include:
2020
- stage: check
2121
script:
22-
- npx aegir commitlint --travis
2322
- npx aegir dep-check -- -i wrtc -i electron-webrtc
2423
- npm run lint
2524

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ function boot (done) {
1818
listener = wd.createListener((conn) => pull(conn, conn))
1919
listener.listen(ma, done)
2020
listener.on('listening', () => {
21+
/* eslint-disable no-console */
2122
console.log('gulp listener started on:', ma.toString())
23+
/* eslint-enable no-console */
2224
})
2325
}
2426

package.json

Lines changed: 12 additions & 10 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,25 +43,28 @@
4443
},
4544
"homepage": "https://github.com/libp2p/js-libp2p-webrtc-direct#readme",
4645
"devDependencies": {
47-
"aegir": "^18.2.1",
46+
"aegir": "^20.0.0",
4847
"chai": "^4.2.0",
4948
"dirty-chai": "^2.0.1",
50-
"gulp": "^4.0.0",
49+
"gulp": "^4.0.2",
5150
"multiaddr": "^6.0.6",
5251
"webrtcsupport": "^2.2.0"
5352
},
5453
"dependencies": {
54+
"abortable-iterator": "^2.1.0",
5555
"class-is": "^1.1.0",
5656
"concat-stream": "^2.0.0",
5757
"detect-node": "^2.0.4",
58+
"err-code": "^2.0.0",
5859
"interface-connection": "~0.3.3",
59-
"mafmt": "^6.0.7",
60+
"interface-transport": "^0.5.2",
61+
"lodash.includes": "^4.3.0",
62+
"mafmt": "^6.0.8",
6063
"multibase": "~0.6.0",
6164
"once": "^1.4.0",
62-
"pull-stream": "^3.6.9",
6365
"request": "^2.88.0",
64-
"simple-peer": "9.3.0",
65-
"stream-to-pull-stream": "^1.7.3",
66+
"simple-peer": "9.5.0",
67+
"stream-to-it": "^0.1.0",
6668
"wrtc": "~0.2.1",
6769
"xhr": "^2.5.0"
6870
},

src/adapter.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const { Adapter } = require('interface-transport')
4+
const withIs = require('class-is')
5+
const WebRTCDirect = require('.')
6+
7+
// Legacy adapter to old transport & connection interface
8+
class WebRTCDirectAdapter extends Adapter {
9+
constructor () {
10+
super(new WebRTCDirect())
11+
}
12+
}
13+
14+
module.exports = withIs(WebRTCDirectAdapter, {
15+
className: 'WebRTCDirect',
16+
symbolName: '@libp2p/js-libp2p-webrtc-direct/webrtcdirect'
17+
})

src/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
// Time to wait for a connection to close gracefully before destroying it
4+
// manually
5+
module.exports.CLOSE_TIMEOUT = 2000

0 commit comments

Comments
 (0)