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

Commit cf7d1b8

Browse files
Alan Shawjacobheun
Alan Shaw
authored andcommitted
feat: change api to async / await (#112)
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await. The API has also been updated according to the latest `interface-transport` version, https://github.com/libp2p/interface-transport/tree/v0.6.0#api.
1 parent 49c7f33 commit cf7d1b8

19 files changed

+587
-724
lines changed

.gitignore

+2-44
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,4 @@
1-
docs
2-
**/node_modules/
3-
**/*.log
4-
test/repo-tests*
5-
**/bundle.js
6-
7-
# yarn
8-
yarn.lock
9-
10-
# Logs
11-
logs
12-
*.log
13-
14-
coverage
15-
16-
# Runtime data
17-
pids
18-
*.pid
19-
*.seed
20-
21-
# Directory for instrumented libs generated by jscoverage/JSCover
22-
lib-cov
23-
24-
# Coverage directory used by tools like istanbul
25-
coverage
26-
27-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
28-
.grunt
29-
30-
# node-waf configuration
31-
.lock-wscript
32-
33-
build
34-
35-
# Dependency directory
36-
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
371
node_modules
38-
39-
lib
40-
dist
41-
test/test-data/go-ipfs-repo/LOCK
42-
test/test-data/go-ipfs-repo/LOG
43-
test/test-data/go-ipfs-repo/LOG.old
44-
45-
# while testing npm5
462
package-lock.json
3+
coverage
4+
.nyc_output

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ os:
1818

1919
before_script:
2020
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'; fi
21+
2122
script: npx nyc -s npm run test:node -- --bail
2223
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
2324

@@ -27,7 +28,6 @@ jobs:
2728
os: linux
2829
script:
2930
- npx aegir build --bundlesize
30-
- npx aegir commitlint --travis
3131
- npx aegir dep-check
3232
- npm run lint
3333

README.md

+20-24
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)](https://github.com/libp2p/interface-connection)
1414

1515

16-
> JavaScript implementation of the TCP module for libp2p. It exposes the [interface-transport](https://github.com/libp2p/interface-connection) for dial/listen. `libp2p-tcp` is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
16+
> JavaScript implementation of the TCP module for libp2p. It exposes the [interface-transport](https://github.com/libp2p/interface-connection) for dial/listen. `libp2p-tcp` is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other transports.
1717
1818
## Lead Maintainer
1919

@@ -41,37 +41,33 @@
4141
```js
4242
const TCP = require('libp2p-tcp')
4343
const multiaddr = require('multiaddr')
44-
const pull = require('pull-stream')
44+
const pipe = require('it-pipe')
45+
const { collect } = require('streaming-iterables')
4546

46-
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
47+
const addr = multiaddr('/ip4/127.0.0.1/tcp/9090')
4748

4849
const tcp = new TCP()
4950

5051
const listener = tcp.createListener((socket) => {
5152
console.log('new connection opened')
52-
pull(
53-
pull.values(['hello']),
53+
pipe(
54+
['hello'],
5455
socket
5556
)
5657
})
5758

58-
listener.listen(mh, () => {
59-
console.log('listening')
60-
61-
pull(
62-
tcp.dial(mh),
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-
})
59+
await listener.listen(addr)
60+
console.log('listening')
61+
62+
const socket = await tcp.dial(addr)
63+
const values = await pipe(
64+
socket,
65+
collect
66+
)
67+
console.log(`Value: ${values.toString()}`)
68+
69+
// Close connection after reading
70+
await listener.close()
7571
```
7672

7773
Outputs:
@@ -88,12 +84,12 @@ Value: hello
8884

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

91-
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
87+
`libp2p-tcp` accepts TCP addresses as both IPFS and non IPFS encapsulated addresses, i.e:
9288

9389
`/ip4/127.0.0.1/tcp/4001`
9490
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
9591

96-
Both for dialing and listening.
92+
(both for dialing and listening)
9793

9894
### Connection
9995

package.json

+9-13
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
"release": "aegir release -t node --no-build",
1212
"release-minor": "aegir release -t node --type minor --no-build",
1313
"release-major": "aegir-release -t node --type major --no-build",
14-
"coverage": "aegir coverage",
15-
"coverage-publish": "aegir coverage --provider coveralls"
14+
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
1615
},
1716
"pre-push": [
18-
"lint",
19-
"test"
17+
"lint"
2018
],
2119
"repository": {
2220
"type": "git",
@@ -38,20 +36,18 @@
3836
"aegir": "^20.0.0",
3937
"chai": "^4.2.0",
4038
"dirty-chai": "^2.0.1",
41-
"interface-transport": "~0.3.6",
42-
"pull-stream": "^3.6.14"
39+
"interface-transport": "^0.6.1",
40+
"sinon": "^7.3.1"
4341
},
4442
"dependencies": {
43+
"abortable-iterator": "^2.1.0",
4544
"class-is": "^1.1.0",
4645
"debug": "^4.1.1",
47-
"interface-connection": "~0.3.3",
46+
"err-code": "^2.0.0",
4847
"ip-address": "^6.1.0",
49-
"lodash.includes": "^4.3.0",
50-
"lodash.isfunction": "^3.0.9",
51-
"mafmt": "^6.0.7",
52-
"multiaddr": "^6.1.0",
53-
"once": "^1.4.0",
54-
"stream-to-pull-stream": "^1.7.3"
48+
"mafmt": "^6.0.9",
49+
"multiaddr": "^7.1.0",
50+
"stream-to-it": "^0.1.1"
5551
},
5652
"contributors": [
5753
"Alan Shaw <[email protected]>",

src/constants.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 manually
8+
exports.CLOSE_TIMEOUT = 2000

src/get-multiaddr.js

-33
This file was deleted.

0 commit comments

Comments
 (0)