Skip to content

Commit

Permalink
fix: use length-prefixed-stream for messages for go interop
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jun 7, 2016
1 parent 532ded2 commit 22d6f25
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -57,6 +57,7 @@
"heap": "^0.2.6",
"highland": "^3.0.0-beta.1",
"ipfs-block": "^0.3.0",
"length-prefixed-stream": "^1.5.0",
"lodash.isequalwith": "^4.2.0",
"lodash.isundefined": "^3.0.1",
"multihashes": "^0.2.2",
Expand All @@ -66,4 +67,4 @@
"David Dias <daviddias.p@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>"
]
}
}
11 changes: 8 additions & 3 deletions src/network/index.js
Expand Up @@ -3,6 +3,7 @@
const bl = require('bl')
const async = require('async')
const debug = require('debug')
const lps = require('length-prefixed-stream')

const Message = require('../message')
const cs = require('../constants')
Expand Down Expand Up @@ -47,7 +48,8 @@ module.exports = class Network {
}

_onConnection (conn) {
conn.pipe(bl((err, data) => {
const decode = lps.decode()
conn.pipe(decode).pipe(bl((err, data) => {
conn.end()
if (err) {
return this.bitswap._receiveError(err)
Expand Down Expand Up @@ -106,10 +108,13 @@ module.exports = class Network {
return done(err)
}

conn.write(msg.toProto())
conn.once('error', (err) => done(err))
conn.once('finish', done)
conn.end()

const encode = lps.encode()
encode.pipe(conn)
encode.write(msg.toProto())
encode.end()
})
}
}
10 changes: 7 additions & 3 deletions test/network/network.node.js
Expand Up @@ -8,6 +8,7 @@ const multiaddr = require('multiaddr')
const expect = require('chai').expect
const PeerBook = require('peer-book')
const Block = require('ipfs-block')
const lps = require('length-prefixed-stream')

const Network = require('../../src/network')
const Message = require('../../src/message')
Expand Down Expand Up @@ -148,10 +149,13 @@ describe('network', function () {
}

libp2pNodeA.dialByPeerInfo(peerInfoB, '/ipfs/bitswap/1.0.0', (err, conn) => {
const msgEncoded = msg.toProto()
conn.write(msgEncoded)
conn.end()
expect(err).to.not.exist

const msgEncoded = msg.toProto()
const enc = lps.encode()
enc.pipe(conn)
enc.write(msgEncoded)
enc.end()
})
})

Expand Down

0 comments on commit 22d6f25

Please sign in to comment.