diff --git a/package.json b/package.json index c64e0a1..0544dc7 100644 --- a/package.json +++ b/package.json @@ -144,7 +144,7 @@ "docs": "aegir docs" }, "dependencies": { - "@multiformats/multiaddr": "^12.0.0" + "@multiformats/multiaddr": "^12.3.0" }, "devDependencies": { "aegir": "^42.2.2" diff --git a/src/index.ts b/src/index.ts index 41d7171..6e9d6c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -134,6 +134,15 @@ const interpreters: Record = { 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 diff --git a/test/test.spec.ts b/test/test.spec.ts index 172c252..00dd96f 100644 --- a/test/test.spec.ts +++ b/test/test.spec.ts @@ -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'], @@ -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'