Skip to content

Commit

Permalink
feat: add support for http-path (#380)
Browse files Browse the repository at this point in the history
See definition in: multiformats/multiaddr#164
  • Loading branch information
MarcoPolo authored Jun 5, 2024
1 parent bb0b785 commit 0524f79
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export function convertToString (proto: number | string, buf: Uint8Array): strin
return bytes2onion(buf)
case 466: // certhash
return bytes2mb(buf)
case 481: // http-path
return globalThis.encodeURIComponent(bytes2str(buf))
default:
return uint8ArrayToString(buf, 'base16') // no clue. convert to hex
}
Expand Down Expand Up @@ -108,6 +110,8 @@ export function convertToBytes (proto: string | number, str: string): Uint8Array
return onion32bytes(str)
case 466: // certhash
return mb2bytes(str)
case 481: // http-path
return str2bytes(globalThis.decodeURIComponent(str))
default:
return uint8ArrayFromString(str, 'base16') // no clue. convert from hex
}
Expand Down
1 change: 1 addition & 0 deletions src/protocols-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const table: Array<[number, number, string, boolean?, boolean?]> = [
[478, 0, 'wss'],
[479, 0, 'p2p-websocket-star'],
[480, 0, 'http'],
[481, V, 'http-path'],
[777, V, 'memory']
]

Expand Down
11 changes: 11 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ describe('variants', () => {
expect(addr.toString()).to.equal(str)
})

it('http-path', () => {
const str = '/ip4/127.0.0.1/tcp/9090/tls/http-path/tmp%2Ffoo%2F..%2Fbar'
const addr = multiaddr(str)
expect(addr).to.have.property('bytes')
const parts = addr.tuples()
const lastPart = parts[parts.length - 1]
const httpPath = new TextDecoder().decode(lastPart[1]?.subarray(1)) // skip the first byte since it's the length prefix
expect(httpPath).to.equal('tmp/foo/../bar')
expect(addr.toString()).to.equal(str)
})

it('onion', () => {
const str = '/onion/timaq4ygg2iegci7:1234'
const addr = multiaddr(str)
Expand Down

0 comments on commit 0524f79

Please sign in to comment.