Skip to content
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

feat: add support for http-path #380

Merged
merged 2 commits into from
Jun 5, 2024
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: 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
Loading