From 87125606edb9c544413aafbfd9df91f9e3b3d2b9 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Thu, 30 May 2024 12:28:45 -0700 Subject: [PATCH 1/2] Add support for http-path --- src/convert.ts | 4 ++++ src/protocols-table.ts | 1 + test/index.spec.ts | 7 +++++++ 3 files changed, 12 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..543fd5e3 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -438,6 +438,13 @@ 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') + expect(addr.toString()).to.equal(str) + }) + it('onion', () => { const str = '/onion/timaq4ygg2iegci7:1234' const addr = multiaddr(str) From 94cc3879b37e7772d7a99cfc58a6d3c3149b543a Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Tue, 4 Jun 2024 18:51:37 -0700 Subject: [PATCH 2/2] Expand test --- test/index.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/index.spec.ts b/test/index.spec.ts index 543fd5e3..bc4e6211 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -442,6 +442,10 @@ describe('variants', () => { 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) })