Skip to content

feat: add retrieval protocol code parsing #418

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin
case 54: // dns4
case 55: // dns6
case 56: // dnsaddr
case 384: // retrieval
case 400: // unix
case 449: // sni
case 777: // memory
Expand Down Expand Up @@ -97,6 +98,7 @@ export function convertToBytes (proto: string | number, str: string): Uint8Array
case 54: // dns4
case 55: // dns6
case 56: // dnsaddr
case 384: // retrieval
case 400: // unix
case 449: // sni
case 777: // memory
Expand Down
1 change: 1 addition & 0 deletions src/protocols-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const table: Array<[number, number, string, boolean?, boolean?]> = [
[479, 0, 'p2p-websocket-star'],
[480, 0, 'http'],
[481, V, 'http-path'],
[384, V, 'retrieval'],
[777, V, 'memory']
]

Expand Down
57 changes: 56 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,21 @@ describe('variants', () => {
expect(addr).to.have.property('bytes')
expect(addr.toString()).to.equal(str)
})

it('ip4 + tcp + http + retrieval', () => {
const str = '/ip4/127.0.0.1/tcp/8000/http/retrieval/http'
const addr = multiaddr(str)
expect(addr).to.have.property('bytes')
expect(addr.toString()).to.equal(str)
})

it('ws + p2p + retrieval tuple', () => {
const str =
'/ip4/127.0.0.1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/retrieval/bitswap/retrieval/graphsync'
const addr = multiaddr(str)
expect(addr).to.have.property('bytes')
expect(addr.toString()).to.equal(str)
})
})

describe('helpers', () => {
Expand Down Expand Up @@ -732,6 +747,26 @@ describe('helpers', () => {
])
})

it('returns the tuples for retrieval', () => {
expect(multiaddr('/ip4/127.0.0.1/tcp/8000/http/retrieval/http').tuples())
.to.eql([
[4, Uint8Array.from([127, 0, 0, 1])],
[6, Uint8Array.from([31, 64])],
[480],
[384, Uint8Array.from([4, 104, 116, 116, 112])]
])

expect(multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/retrieval/bitswap/retrieval/graphsync').tuples())
.to.eql([
[4, Uint8Array.from([127, 0, 0, 1])],
[6, Uint8Array.from([35, 130])],
[477],
[421, Uint8Array.from([34, 18, 32, 213, 46, 187, 137, 216, 91, 2, 162, 132, 148, 130, 3, 166, 47, 242, 131, 137, 197, 124, 159, 66, 190, 236, 78, 194, 13, 183, 106, 104, 145, 28, 11])],
[384, Uint8Array.from([7, 98, 105, 116, 115, 119, 97, 112])],
[384, Uint8Array.from([9, 103, 114, 97, 112, 104, 115, 121, 110, 99])]
])
})

it('does not allow modifying parts', () => {
const ma = multiaddr('/ip4/0.0.0.0/tcp/1234')
const tuples = ma.tuples()
Expand All @@ -742,14 +777,34 @@ describe('helpers', () => {
})

describe('.stringTuples', () => {
it('returns the string partss', () => {
it('returns the string parts', () => {
expect(multiaddr('/ip4/0.0.0.0/utp').stringTuples())
.to.eql([
[4, '0.0.0.0'],
[302]
])
})

it('returns the string parts for retrieval', () => {
expect(multiaddr('/ip4/127.0.0.1/tcp/8000/http/retrieval/http').stringTuples())
.to.eql([
[4, '127.0.0.1'],
[6, '8000'],
[480],
[384, 'http']
])

expect(multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/retrieval/bitswap/retrieval/graphsync').stringTuples())
.to.eql([
[4, '127.0.0.1'],
[6, '9090'],
[477],
[421, 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'],
[384, 'bitswap'],
[384, 'graphsync']
])
})

it('does not allow modifying string parts', () => {
const ma = multiaddr('/ip4/0.0.0.0/tcp/1234')
const tuples = ma.stringTuples()
Expand Down