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

Commit

Permalink
refactor: use pull-streams
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 7, 2016
1 parent 0eabb18 commit 734bc4c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const multiaddr = require('multiaddr')
const Node = require('libp2p-ipfs').Node
const Peer = require('peer-info')
const Id = require('peer-id')
const pull = require('pull-stream')

const sigServer = require('libp2p-webrtc-star/src/signalling-server')
let sigS
Expand All @@ -21,7 +22,7 @@ gulp.task('libnode:start', (done) => {
node = new Node(peer)
node.start(() => {
node.handle('/echo/1.0.0', (conn) => {
conn.pipe(conn)
pull(conn, conn)
})
ready()
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-browser#readme",
"devDependencies": {
"aegir": "^8.0.0",
"bl": "^1.1.2",
"chai": "^3.5.0",
"gulp": "^3.9.1",
"libp2p-ipfs": "^0.12.1",
"peer-id": "^0.7.0",
"pre-commit": "^1.1.3",
"pull-goodbye": "0.0.1",
"pull-stream": "^3.4.5",
"run-parallel": "^1.1.6",
"webrtcsupport": "^2.2.0"
},
Expand All @@ -59,4 +60,4 @@
"dignifiedquire <dignifiedquire@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>"
]
}
}
95 changes: 47 additions & 48 deletions test/websockets-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const expect = require('chai').expect
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const pull = require('pull-stream')
const goodbye = require('pull-goodbye')

const libp2p = require('../src')
const rawPeer = require('./peer.json')
const id = PeerId.createFromPrivKey(rawPeer.privKey)
const bl = require('bl')

describe('libp2p-ipfs-browser (websockets only)', function () {
this.timeout(20 * 1000)
Expand Down Expand Up @@ -56,13 +57,16 @@ describe('libp2p-ipfs-browser (websockets only)', function () {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist
expect(Object.keys(peers)).to.have.length(1)
conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()

pull(
pull.values([Buffer('hey')]),
conn,
pull.collect((err, data) => {
expect(err).to.not.exist
expect(data).to.be.eql([Buffer('hey')])
done()
})
)
})
})

Expand Down Expand Up @@ -103,13 +107,16 @@ describe('libp2p-ipfs-browser (websockets only)', function () {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist
expect(Object.keys(peers)).to.have.length(1)
conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()

pull(
pull.values([Buffer('hey')]),
conn,
pull.collect((err, data) => {
expect(err).to.not.exist
expect(data).to.be.eql([Buffer('hey')])
done()
})
)
})
})

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

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

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

conn.write(message)
conn.write('STOP')

let result = ''

conn.on('data', (data) => {
if (data.toString() === 'STOP') {
conn.end()
return
}
result += data.toString()
const s = goodbye({
soruce: pull.values([message]),
sink: pull.collect((err, data) => {
expect(err).to.not.exist
expect(data).to.be.eql([message])
done()
})
})

conn.on('end', () => {
expect(result).to.equal(message)
done()
})
pull(s, conn, s)
})
})

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

const values = []
while (++counter < 10000) {
conn.write(`${counter} `)
values.push(Buffer(`${counter} `))
expected += `${counter} `
}

while (++counter < 20000) {
conn.write(`${counter} `)
values.push(Buffer(`${counter} `))
expected += `${counter} `
}

setTimeout(() => {
conn.write('STOP')
}, 2000)

let result = ''
conn.on('data', (data) => {
if (data.toString() === 'STOP') {
conn.end()
return
}
result += data.toString()
const s = goodbye({
soruce: pull.values(values),
sink: pull.collect((err, data) => {
expect(err).to.not.exist
expect(
Buffer.concat(data).toString()
).to.be.eql(
expected
)

done()
})
})

conn.on('end', () => {
expect(result).to.equal(expected)
done()
})
pull(s, conn, s)
})
})

Expand Down

0 comments on commit 734bc4c

Please sign in to comment.