Skip to content

Commit 9a16acf

Browse files
committed
feat: add retrieval protocol code parsing
1 parent 5e82b69 commit 9a16acf

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/convert.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin
5656
case 56: // dnsaddr
5757
case 400: // unix
5858
case 449: // sni
59+
case 496: // retrieval
5960
case 777: // memory
6061
return bytes2str(buf)
6162

@@ -99,6 +100,7 @@ export function convertToBytes (proto: string | number, str: string): Uint8Array
99100
case 56: // dnsaddr
100101
case 400: // unix
101102
case 449: // sni
103+
case 496: // retrieval
102104
case 777: // memory
103105
return str2bytes(str)
104106

src/protocols-table.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const table: Array<[number, number, string, boolean?, boolean?]> = [
4747
[479, 0, 'p2p-websocket-star'],
4848
[480, 0, 'http'],
4949
[481, V, 'http-path'],
50+
[496, V, 'retrieval'],
5051
[777, V, 'memory']
5152
]
5253

test/index.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,21 @@ describe('variants', () => {
548548
expect(addr).to.have.property('bytes')
549549
expect(addr.toString()).to.equal(str)
550550
})
551+
552+
it('ip4 + tcp + http + retrieval', () => {
553+
const str = '/ip4/127.0.0.1/tcp/8000/http/retrieval/http'
554+
const addr = multiaddr(str)
555+
expect(addr).to.have.property('bytes')
556+
expect(addr.toString()).to.equal(str)
557+
})
558+
559+
it('ws + p2p + retrieval tuple', () => {
560+
const str =
561+
'/ip4/127.0.0.1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/retrieval/bitswap/retrieval/graphsync'
562+
const addr = multiaddr(str)
563+
expect(addr).to.have.property('bytes')
564+
expect(addr.toString()).to.equal(str)
565+
})
551566
})
552567

553568
describe('helpers', () => {
@@ -732,6 +747,26 @@ describe('helpers', () => {
732747
])
733748
})
734749

750+
it('returns the tuples for retrieval', () => {
751+
expect(multiaddr('/ip4/127.0.0.1/tcp/8000/http/retrieval/http').tuples())
752+
.to.eql([
753+
[4, Uint8Array.from([127, 0, 0, 1])],
754+
[6, Uint8Array.from([31, 64])],
755+
[480],
756+
[496, Uint8Array.from([4, 104, 116, 116, 112])]
757+
])
758+
759+
expect(multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/retrieval/bitswap/retrieval/graphsync').tuples())
760+
.to.eql([
761+
[4, Uint8Array.from([127, 0, 0, 1])],
762+
[6, Uint8Array.from([35, 130])],
763+
[477],
764+
[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])],
765+
[496, Uint8Array.from([7, 98, 105, 116, 115, 119, 97, 112])],
766+
[496, Uint8Array.from([9, 103, 114, 97, 112, 104, 115, 121, 110, 99])]
767+
])
768+
})
769+
735770
it('does not allow modifying parts', () => {
736771
const ma = multiaddr('/ip4/0.0.0.0/tcp/1234')
737772
const tuples = ma.tuples()
@@ -742,14 +777,34 @@ describe('helpers', () => {
742777
})
743778

744779
describe('.stringTuples', () => {
745-
it('returns the string partss', () => {
780+
it('returns the string parts', () => {
746781
expect(multiaddr('/ip4/0.0.0.0/utp').stringTuples())
747782
.to.eql([
748783
[4, '0.0.0.0'],
749784
[302]
750785
])
751786
})
752787

788+
it('returns the string parts for retrieval', () => {
789+
expect(multiaddr('/ip4/127.0.0.1/tcp/8000/http/retrieval/http').stringTuples())
790+
.to.eql([
791+
[4, '127.0.0.1'],
792+
[6, '8000'],
793+
[480],
794+
[496, 'http']
795+
])
796+
797+
expect(multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/retrieval/bitswap/retrieval/graphsync').stringTuples())
798+
.to.eql([
799+
[4, '127.0.0.1'],
800+
[6, '9090'],
801+
[477],
802+
[421, 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'],
803+
[496, 'bitswap'],
804+
[496, 'graphsync']
805+
])
806+
})
807+
753808
it('does not allow modifying string parts', () => {
754809
const ma = multiaddr('/ip4/0.0.0.0/tcp/1234')
755810
const tuples = ma.stringTuples()

0 commit comments

Comments
 (0)