Skip to content

Commit 65aa537

Browse files
authored
feat: add support for http-path (#148)
Add support for http-path
1 parent 266001f commit 65aa537

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"docs": "aegir docs"
145145
},
146146
"dependencies": {
147-
"@multiformats/multiaddr": "^12.0.0"
147+
"@multiformats/multiaddr": "^12.3.0"
148148
},
149149
"devDependencies": {
150150
"aegir": "^43.0.1"

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ const interpreters: Record<string, Interpreter> = {
134134
baseVal = baseVal.replace('tcp://', '')
135135
return `${protocol}${baseVal}`
136136
},
137+
'http-path': (value: string, restMa: StringTuple[]) => {
138+
const tailProto = restMa.pop()
139+
if (tailProto === undefined) {
140+
throw new Error('Unexpected end of multiaddr')
141+
}
142+
const baseVal = interpretNext(tailProto[0], tailProto[1] ?? '', restMa)
143+
const decodedValue = decodeURIComponent(value)
144+
return `${baseVal}/${decodedValue}`
145+
},
137146
tls: (value: string, restMa: StringTuple[]) => {
138147
// Noop, the parent context knows that it's tls. We don't need to do
139148
// anything here

test/test.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('multiaddr-to-uri', () => {
1313
['/ip4/0.0.7.6/tcp/1234/https', 'https://0.0.7.6:1234'],
1414
['/ip4/0.0.7.6/tcp/1234/tls/http', 'https://0.0.7.6:1234'],
1515
['/ip4/1.2.3.4/tcp/1234/tls/sni/ipfs.io/http', 'https://ipfs.io'],
16+
['/ip4/1.2.3.4/tcp/1234/tls/sni/ipfs.io/http/http-path/foo%2fbar', 'https://ipfs.io/foo/bar'],
1617
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
1718
['/ip6/::/udp/0', 'udp://[::]:0'],
1819
['/dns/a.com/tcp/1234', 'http://a.com:1234'],
@@ -85,6 +86,10 @@ describe('multiaddr-to-uri', () => {
8586
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
8687
'tcp://1.2.3.4:3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'
8788
],
89+
[
90+
'/ip4/1.2.3.4/tcp/3456/http/http-path/foo%2fbar',
91+
'http://1.2.3.4:3456/foo/bar'
92+
],
8893
[
8994
'/ip4/1.2.3.4/tcp/3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK',
9095
'tcp://1.2.3.4:3456/p2p/QmcNwyju7SWoizsAuf6kjaaRoxe762ovsT3hz6qt3xxcsK'

0 commit comments

Comments
 (0)