From 612c0a95ab9fcf9949ce8335bdd39911c6a99b7b Mon Sep 17 00:00:00 2001 From: ohager Date: Thu, 7 Sep 2023 10:29:44 -0300 Subject: [PATCH] fix: URI resolution issue - avoid ambiguities --- packages/standards/src/src47/URIResolver.ts | 16 ++++------------ .../src/src47/__tests__/URIResolver.spec.ts | 6 ++++++ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/standards/src/src47/URIResolver.ts b/packages/standards/src/src47/URIResolver.ts index 9e71f472..f7394d80 100644 --- a/packages/standards/src/src47/URIResolver.ts +++ b/packages/standards/src/src47/URIResolver.ts @@ -123,19 +123,11 @@ export class URIResolver { */ public static convertKnownTldUri(uri: string): string { uri = uri.toLowerCase(); - for (const knownTld of KnownTlds) { - const index = uri.lastIndexOf('.' + knownTld); - if (index > -1) { - const pathIndex = uri.indexOf('/', index); - const path = pathIndex > -1 ? uri.substring(pathIndex) : ''; - uri = uri.slice(0, index) + `@${knownTld}`; - if(path){ - uri += path; - } - break; - } + const expression = `\\.(${KnownTlds.join('|')})(/[a-zA-Z0-9_-]+)?$`; + const matches = new RegExp(expression).exec(uri); + if (matches) { + return uri.substring(0, matches.index) + '@' + uri.substring(matches.index + 1); } - return uri; } diff --git a/packages/standards/src/src47/__tests__/URIResolver.spec.ts b/packages/standards/src/src47/__tests__/URIResolver.spec.ts index ba4567f6..e429f949 100644 --- a/packages/standards/src/src47/__tests__/URIResolver.spec.ts +++ b/packages/standards/src/src47/__tests__/URIResolver.spec.ts @@ -353,6 +353,12 @@ describe('URIResolver', () => { expect(convertedUri).toEqual('http://example@com'); }); + it('should consider only extact TLDs', () => { + const uri = 'http://ohager.nfts.signum'; + const convertedUri = URIResolver.convertKnownTldUri(uri); + expect(convertedUri).toEqual('http://ohager.nfts@signum'); + }); + it('should handle append further (deep) field paths - domain only', () => { for (const tld of KnownTlds) { const uri = `https://domain.${tld}/ac`;