Skip to content

fix: handle variable sized protocols in protoCodes #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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