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

Commit d9f65e0

Browse files
committed
feat(readme): add pull-streams documentation
1 parent 5e89a26 commit d9f65e0

File tree

6 files changed

+110
-33
lines changed

6 files changed

+110
-33
lines changed

README.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)
1111
![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)
1212

13-
> Node.js implementation of the TCP module that libp2p uses, which implements
14-
> the [interface-connection](https://github.com/libp2p/interface-connection)
15-
> interface for dial/listen.
13+
> Node.js implementation of the TCP module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/interface-connection) interface for dial/listen.
1614
1715
## Description
1816

19-
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a
20-
`multiaddr`. This small shim will enable libp2p to use other different
21-
transports.
17+
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
2218

2319
**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
2420

@@ -71,6 +67,30 @@ hello
7167
> npm i libp2p-tcp
7268
```
7369

70+
## This module uses `pull-streams`
71+
72+
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 what took us to make this migration, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
73+
74+
You can learn more about pull-streams at:
75+
76+
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
77+
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
78+
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
79+
- [pull-streams documentation](https://pull-stream.github.io/)
80+
81+
### Converting `pull-streams` to Node.js Streams
82+
83+
If you are a Node.js streams user, you can convert a pull-stream to Node.js Stream using the module `pull-stream-to-stream`, giving you an instance of a Node.js stream that is linked to the pull-stream. Example:
84+
85+
```
86+
const pullToStream = require('pull-stream-to-stream')
87+
88+
const nodeStreamInstance = pullToStream(pullStreamInstance)
89+
// nodeStreamInstance is an instance of a Node.js Stream
90+
```
91+
92+
To learn more about his utility, visit https://pull-stream.github.io/#pull-stream-to-stream
93+
7494
## API
7595

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"devDependencies": {
3535
"aegir": "^6.0.0",
3636
"chai": "^3.5.0",
37+
"interface-transport": "^0.2.0",
3738
"lodash.isfunction": "^3.0.8",
3839
"pre-commit": "^1.1.2"
3940
},

src/index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const contains = require('lodash.contains')
77
const isFunction = require('lodash.isfunction')
88
const Connection = require('interface-connection').Connection
99
const debug = require('debug')
10-
const log = debug('libp2p:tcp')
10+
const log = debug('libp2p:tcp:dial')
1111

1212
const createListener = require('./listener')
1313

@@ -24,13 +24,23 @@ module.exports = class TCP {
2424

2525
const cOpts = ma.toOptions()
2626
log('Connecting to %s %s', cOpts.port, cOpts.host)
27-
const socket = toPull.duplex(net.connect(cOpts, cb))
2827

29-
socket.getObservedAddrs = (cb) => {
28+
const rawSocket = net.connect(cOpts, cb)
29+
30+
rawSocket.once('timeout', () => {
31+
log('timeout')
32+
rawSocket.emit('error', new Error('Timeout'))
33+
})
34+
35+
const socket = toPull.duplex(rawSocket)
36+
37+
const conn = new Connection(socket)
38+
39+
conn.getObservedAddrs = (cb) => {
3040
return cb(null, [ma])
3141
}
3242

33-
return new Connection(socket)
43+
return conn
3444
}
3545

3646
createListener (options, handler) {
@@ -39,6 +49,8 @@ module.exports = class TCP {
3949
options = {}
4050
}
4151

52+
handler = handler || (() => {})
53+
4254
return createListener(handler)
4355
}
4456

src/listener.js

+41-20
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
11
'use strict'
22

33
const multiaddr = require('multiaddr')
4-
const debug = require('debug')
5-
const log = debug('libp2p:tcp')
64
const Connection = require('interface-connection').Connection
75
const os = require('os')
86
const contains = require('lodash.contains')
97
const net = require('net')
108
const toPull = require('stream-to-pull-stream')
9+
const EventEmitter = require('events').EventEmitter
10+
const debug = require('debug')
11+
const log = debug('libp2p:tcp:listen')
1112

1213
const getMultiaddr = require('./get-multiaddr')
1314

1415
const IPFS_CODE = 421
1516
const CLOSE_TIMEOUT = 2000
1617

1718
module.exports = (handler) => {
18-
const listener = net.createServer((socket) => {
19+
const listener = new EventEmitter()
20+
21+
const server = net.createServer((socket) => {
22+
const addr = getMultiaddr(socket)
23+
log('new connection', addr.toString())
24+
1925
const s = toPull.duplex(socket)
2026
s.getObservedAddrs = (cb) => {
21-
return cb(null, [getMultiaddr(socket)])
27+
return cb(null, [addr])
2228
}
2329

30+
trackSocket(server, socket)
31+
2432
const conn = new Connection(s)
2533
handler(conn)
34+
listener.emit('connection', conn)
2635
})
2736

28-
// Keep track of open connections to destroy in case of timeout
29-
listener.__connections = {}
30-
listener.on('connection', (socket) => {
31-
const key = `${socket.remoteAddress}:${socket.remotePort}`
32-
listener.__connections[key] = socket
37+
server.on('listening', () => {
38+
listener.emit('listening')
39+
})
3340

34-
socket.on('close', () => {
35-
delete listener.__connections[key]
36-
})
41+
server.on('error', (err) => {
42+
listener.emit('error', err)
3743
})
3844

39-
listener._close = listener.close
45+
server.on('close', () => {
46+
listener.emit('close')
47+
})
48+
49+
// Keep track of open connections to destroy in case of timeout
50+
server.__connections = {}
51+
4052
listener.close = (options, cb) => {
4153
if (typeof options === 'function') {
4254
cb = options
@@ -46,24 +58,24 @@ module.exports = (handler) => {
4658
options = options || {}
4759

4860
let closed = false
49-
listener._close(cb)
50-
listener.once('close', () => {
61+
server.close(cb)
62+
server.once('close', () => {
5163
closed = true
5264
})
5365
setTimeout(() => {
5466
if (closed) return
5567

5668
log('unable to close graciously, destroying conns')
57-
Object.keys(listener.__connections).forEach((key) => {
69+
Object.keys(server.__connections).forEach((key) => {
5870
log('destroying %s', key)
59-
listener.__connections[key].destroy()
71+
server.__connections[key].destroy()
6072
})
6173
}, options.timeout || CLOSE_TIMEOUT)
6274
}
75+
6376
let ipfsId
6477
let listeningAddr
6578

66-
listener._listen = listener.listen
6779
listener.listen = (ma, cb) => {
6880
listeningAddr = ma
6981
if (contains(ma.protoNames(), 'ipfs')) {
@@ -73,12 +85,12 @@ module.exports = (handler) => {
7385

7486
const lOpts = listeningAddr.toOptions()
7587
log('Listening on %s %s', lOpts.port, lOpts.host)
76-
return listener._listen(lOpts.port, lOpts.host, cb)
88+
return server.listen(lOpts.port, lOpts.host, cb)
7789
}
7890

7991
listener.getAddrs = (cb) => {
8092
const multiaddrs = []
81-
const address = listener.address()
93+
const address = server.address()
8294

8395
if (!address) {
8496
return cb(new Error('Listener is not ready yet'))
@@ -127,3 +139,12 @@ function getIpfsId (ma) {
127139
return tuple[0] === IPFS_CODE
128140
})[0][1]
129141
}
142+
143+
function trackSocket (server, socket) {
144+
const key = `${socket.remoteAddress}:${socket.remotePort}`
145+
server.__connections[key] = socket
146+
147+
socket.on('close', () => {
148+
delete server.__connections[key]
149+
})
150+
}

test/compliance.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const tests = require('interface-transport')
5+
const multiaddr = require('multiaddr')
6+
const Tcp = require('../src')
7+
8+
describe('compliance', () => {
9+
tests({
10+
setup (cb) {
11+
let tcp = new Tcp()
12+
const addrs = [
13+
multiaddr('/ip4/127.0.0.1/tcp/9091'),
14+
multiaddr('/ip4/127.0.0.1/tcp/9092'),
15+
multiaddr('/ip4/127.0.0.1/tcp/9093')
16+
]
17+
cb(null, tcp, addrs)
18+
},
19+
teardown (cb) {
20+
cb()
21+
}
22+
})
23+
})

test/index.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ describe('listen', () => {
2323
})
2424

2525
it('close listener with connections, through timeout', (done) => {
26-
const mh = multiaddr('/ip4/127.0.0.1/tcp/9091/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
26+
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
2727
const listener = tcp.createListener((conn) => {
2828
pull(conn, conn)
2929
})
3030

3131
listener.listen(mh, () => {
32-
const socket1 = net.connect(9091)
33-
const socket2 = net.connect(9091)
32+
const socket1 = net.connect(9090)
33+
const socket2 = net.connect(9090)
3434

3535
socket1.write('Some data that is never handled')
3636
socket1.end()

0 commit comments

Comments
 (0)