Skip to content

Commit

Permalink
feat: add support for http-path (#148)
Browse files Browse the repository at this point in the history
Add support for http-path
  • Loading branch information
MarcoPolo authored Jun 6, 2024
1 parent 266001f commit 65aa537
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"docs": "aegir docs"
},
"dependencies": {
"@multiformats/multiaddr": "^12.0.0"
"@multiformats/multiaddr": "^12.3.0"
},
"devDependencies": {
"aegir": "^43.0.1"
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ const interpreters: Record<string, Interpreter> = {
baseVal = baseVal.replace('tcp://', '')
return `${protocol}${baseVal}`
},
'http-path': (value: string, restMa: StringTuple[]) => {
const tailProto = restMa.pop()
if (tailProto === undefined) {
throw new Error('Unexpected end of multiaddr')
}
const baseVal = interpretNext(tailProto[0], tailProto[1] ?? '', restMa)
const decodedValue = decodeURIComponent(value)
return `${baseVal}/${decodedValue}`
},
tls: (value: string, restMa: StringTuple[]) => {
// Noop, the parent context knows that it's tls. We don't need to do
// anything here
Expand Down
5 changes: 5 additions & 0 deletions test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('multiaddr-to-uri', () => {
['/ip4/0.0.7.6/tcp/1234/https', 'https://0.0.7.6:1234'],
['/ip4/0.0.7.6/tcp/1234/tls/http', 'https://0.0.7.6:1234'],
['/ip4/1.2.3.4/tcp/1234/tls/sni/ipfs.io/http', 'https://ipfs.io'],
['/ip4/1.2.3.4/tcp/1234/tls/sni/ipfs.io/http/http-path/foo%2fbar', 'https://ipfs.io/foo/bar'],
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
['/ip6/::/udp/0', 'udp://[::]:0'],
['/dns/a.com/tcp/1234', 'http://a.com:1234'],
Expand Down Expand Up @@ -85,6 +86,10 @@ describe('multiaddr-to-uri', () => {
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
'tcp://1.2.3.4:3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
],
[
'/ip4/1.2.3.4/tcp/3456/http/http-path/foo%2fbar',
'http://1.2.3.4:3456/foo/bar'
],
[
'/ip4/1.2.3.4/tcp/3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
'tcp://1.2.3.4:3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
Expand Down

0 comments on commit 65aa537

Please sign in to comment.