From 0524f7901f7dc813463cf19e45bbd62bf581fb38 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 5 Jun 2024 09:22:51 -0700 Subject: [PATCH] feat: add support for http-path (#380) See definition in: https://github.com/multiformats/multiaddr/pull/164 --- src/convert.ts | 4 ++++ src/protocols-table.ts | 1 + test/index.spec.ts | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/src/convert.ts b/src/convert.ts index 65dd9aa5..7c48c56a 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -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 } @@ -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 } diff --git a/src/protocols-table.ts b/src/protocols-table.ts index cb810592..0d85beb5 100644 --- a/src/protocols-table.ts +++ b/src/protocols-table.ts @@ -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'] ] diff --git a/test/index.spec.ts b/test/index.spec.ts index db81f1c9..bc4e6211 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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)