Skip to content

Commit 0524f79

Browse files
authored
feat: add support for http-path (#380)
See definition in: multiformats/multiaddr#164
1 parent bb0b785 commit 0524f79

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/convert.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin
7070
return bytes2onion(buf)
7171
case 466: // certhash
7272
return bytes2mb(buf)
73+
case 481: // http-path
74+
return globalThis.encodeURIComponent(bytes2str(buf))
7375
default:
7476
return uint8ArrayToString(buf, 'base16') // no clue. convert to hex
7577
}
@@ -108,6 +110,8 @@ export function convertToBytes (proto: string | number, str: string): Uint8Array
108110
return onion32bytes(str)
109111
case 466: // certhash
110112
return mb2bytes(str)
113+
case 481: // http-path
114+
return str2bytes(globalThis.decodeURIComponent(str))
111115
default:
112116
return uint8ArrayFromString(str, 'base16') // no clue. convert from hex
113117
}

src/protocols-table.ts

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

test/index.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,17 @@ describe('variants', () => {
438438
expect(addr.toString()).to.equal(str)
439439
})
440440

441+
it('http-path', () => {
442+
const str = '/ip4/127.0.0.1/tcp/9090/tls/http-path/tmp%2Ffoo%2F..%2Fbar'
443+
const addr = multiaddr(str)
444+
expect(addr).to.have.property('bytes')
445+
const parts = addr.tuples()
446+
const lastPart = parts[parts.length - 1]
447+
const httpPath = new TextDecoder().decode(lastPart[1]?.subarray(1)) // skip the first byte since it's the length prefix
448+
expect(httpPath).to.equal('tmp/foo/../bar')
449+
expect(addr.toString()).to.equal(str)
450+
})
451+
441452
it('onion', () => {
442453
const str = '/onion/timaq4ygg2iegci7:1234'
443454
const addr = multiaddr(str)

0 commit comments

Comments
 (0)