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

Commit 734bc4c

Browse files
refactor: use pull-streams
1 parent 0eabb18 commit 734bc4c

File tree

3 files changed

+52
-51
lines changed

3 files changed

+52
-51
lines changed

gulpfile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const multiaddr = require('multiaddr')
55
const Node = require('libp2p-ipfs').Node
66
const Peer = require('peer-info')
77
const Id = require('peer-id')
8+
const pull = require('pull-stream')
89

910
const sigServer = require('libp2p-webrtc-star/src/signalling-server')
1011
let sigS
@@ -21,7 +22,7 @@ gulp.task('libnode:start', (done) => {
2122
node = new Node(peer)
2223
node.start(() => {
2324
node.handle('/echo/1.0.0', (conn) => {
24-
conn.pipe(conn)
25+
pull(conn, conn)
2526
})
2627
ready()
2728
})

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-browser#readme",
3333
"devDependencies": {
3434
"aegir": "^8.0.0",
35-
"bl": "^1.1.2",
3635
"chai": "^3.5.0",
3736
"gulp": "^3.9.1",
3837
"libp2p-ipfs": "^0.12.1",
3938
"peer-id": "^0.7.0",
4039
"pre-commit": "^1.1.3",
40+
"pull-goodbye": "0.0.1",
41+
"pull-stream": "^3.4.5",
4142
"run-parallel": "^1.1.6",
4243
"webrtcsupport": "^2.2.0"
4344
},
@@ -59,4 +60,4 @@
5960
"dignifiedquire <[email protected]>",
6061
"greenkeeperio-bot <[email protected]>"
6162
]
62-
}
63+
}

test/websockets-only.js

+47-48
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ const expect = require('chai').expect
55
const multiaddr = require('multiaddr')
66
const PeerInfo = require('peer-info')
77
const PeerId = require('peer-id')
8+
const pull = require('pull-stream')
9+
const goodbye = require('pull-goodbye')
810

911
const libp2p = require('../src')
1012
const rawPeer = require('./peer.json')
1113
const id = PeerId.createFromPrivKey(rawPeer.privKey)
12-
const bl = require('bl')
1314

1415
describe('libp2p-ipfs-browser (websockets only)', function () {
1516
this.timeout(20 * 1000)
@@ -56,13 +57,16 @@ describe('libp2p-ipfs-browser (websockets only)', function () {
5657
const peers = nodeA.peerBook.getAll()
5758
expect(err).to.not.exist
5859
expect(Object.keys(peers)).to.have.length(1)
59-
conn.pipe(bl((err, data) => {
60-
expect(err).to.not.exist
61-
expect(data.toString()).to.equal('hey')
62-
done()
63-
}))
64-
conn.write('hey')
65-
conn.end()
60+
61+
pull(
62+
pull.values([Buffer('hey')]),
63+
conn,
64+
pull.collect((err, data) => {
65+
expect(err).to.not.exist
66+
expect(data).to.be.eql([Buffer('hey')])
67+
done()
68+
})
69+
)
6670
})
6771
})
6872

@@ -103,13 +107,16 @@ describe('libp2p-ipfs-browser (websockets only)', function () {
103107
const peers = nodeA.peerBook.getAll()
104108
expect(err).to.not.exist
105109
expect(Object.keys(peers)).to.have.length(1)
106-
conn.pipe(bl((err, data) => {
107-
expect(err).to.not.exist
108-
expect(data.toString()).to.equal('hey')
109-
done()
110-
}))
111-
conn.write('hey')
112-
conn.end()
110+
111+
pull(
112+
pull.values([Buffer('hey')]),
113+
conn,
114+
pull.collect((err, data) => {
115+
expect(err).to.not.exist
116+
expect(data).to.be.eql([Buffer('hey')])
117+
done()
118+
})
119+
)
113120
})
114121
})
115122

@@ -135,28 +142,21 @@ describe('libp2p-ipfs-browser (websockets only)', function () {
135142
it.skip('libp2p.hangupById nodeA to nodeB', (done) => {})
136143

137144
it('stress test: one big write', (done) => {
138-
const message = new Buffer(1000000).fill('a').toString('hex')
145+
const message = new Buffer(1000000).fill('a')
139146

140147
nodeA.dialByPeerInfo(peerB, '/echo/1.0.0', (err, conn) => {
141148
expect(err).to.not.exist
142149

143-
conn.write(message)
144-
conn.write('STOP')
145-
146-
let result = ''
147-
148-
conn.on('data', (data) => {
149-
if (data.toString() === 'STOP') {
150-
conn.end()
151-
return
152-
}
153-
result += data.toString()
150+
const s = goodbye({
151+
soruce: pull.values([message]),
152+
sink: pull.collect((err, data) => {
153+
expect(err).to.not.exist
154+
expect(data).to.be.eql([message])
155+
done()
156+
})
154157
})
155158

156-
conn.on('end', () => {
157-
expect(result).to.equal(message)
158-
done()
159-
})
159+
pull(s, conn, s)
160160
})
161161
})
162162

@@ -167,33 +167,32 @@ describe('libp2p-ipfs-browser (websockets only)', function () {
167167
nodeA.dialByPeerInfo(peerB, '/echo/1.0.0', (err, conn) => {
168168
expect(err).to.not.exist
169169

170+
const values = []
170171
while (++counter < 10000) {
171-
conn.write(`${counter} `)
172+
values.push(Buffer(`${counter} `))
172173
expected += `${counter} `
173174
}
174175

175176
while (++counter < 20000) {
176-
conn.write(`${counter} `)
177+
values.push(Buffer(`${counter} `))
177178
expected += `${counter} `
178179
}
179180

180-
setTimeout(() => {
181-
conn.write('STOP')
182-
}, 2000)
183-
184-
let result = ''
185-
conn.on('data', (data) => {
186-
if (data.toString() === 'STOP') {
187-
conn.end()
188-
return
189-
}
190-
result += data.toString()
181+
const s = goodbye({
182+
soruce: pull.values(values),
183+
sink: pull.collect((err, data) => {
184+
expect(err).to.not.exist
185+
expect(
186+
Buffer.concat(data).toString()
187+
).to.be.eql(
188+
expected
189+
)
190+
191+
done()
192+
})
191193
})
192194

193-
conn.on('end', () => {
194-
expect(result).to.equal(expected)
195-
done()
196-
})
195+
pull(s, conn, s)
197196
})
198197
})
199198

0 commit comments

Comments
 (0)