Skip to content

Commit

Permalink
fix: handle variable sized protocols in protoCodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Apr 20, 2016
1 parent e3071d0 commit 1bce576
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module.exports = {
cleanPath: cleanPath,

ParseError: ParseError,
protoFromTuple: protoFromTuple
protoFromTuple: protoFromTuple,

sizeForAddr: sizeForAddr
}

// string -> [[str name, str addr]... ]
Expand Down
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ Multiaddr.prototype.protos = function protos () {

// get the multiaddr protocol codes
Multiaddr.prototype.protoCodes = function protoCodes () {
var codes = []
for (var i = 0; i < this.buffer.length; i++) {
var code = varint.decode(this.buffer, i)
var size = protocols(code).size / 8
i = i + varint.decode.bytes - 1
i += size // skip over proto data
const codes = []
const buf = this.buffer
let i = 0
while (i < buf.length) {
const code = varint.decode(buf, i)
const n = varint.decode.bytes

const p = protocols(code)
const size = codec.sizeForAddr(p, buf.slice(i + n))

i += (size + n)
codes.push(code)
}

return codes
}

Expand Down
18 changes: 18 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ describe('helpers', () => {
size: 0
}])
})

it('works with ipfs', () => {
expect(
multiaddr('/ip4/0.0.0.0/utp/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').protos()
).to.be.eql([{
code: 4,
name: 'ip4',
size: 32
}, {
code: 302,
name: 'utp',
size: 0
}, {
code: 421,
name: 'ipfs',
size: -1
}])
})
})

describe('.tuples', () => {
Expand Down

0 comments on commit 1bce576

Please sign in to comment.