From 43e9be3389d31cc1c1ec5848598ad935988bcfc0 Mon Sep 17 00:00:00 2001 From: Ezequiel Boehler Date: Wed, 12 May 2021 09:32:32 -0400 Subject: [PATCH 1/4] Get getQueryString to expect relative URLs too Change-type: Minor Signed-off-by: Ezequiel Boehler ezequiel@balena.io --- src/url-params.ts | 37 +++++++++++++++++++++++++------------ test/url-params.test.ts | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/url-params.ts b/src/url-params.ts index a82e361..d58226f 100644 --- a/src/url-params.ts +++ b/src/url-params.ts @@ -175,12 +175,34 @@ export class AnalyticsUrlParams { /** * @return full query parameter string that can be appended to URLs */ - getQueryString(destinationUrl?: URL, currentUrl?: URL): string { + getQueryString(destinationUrl?: URL | string, currentUrl?: URL): string { + // we first check if destionationUrl is a relative URL string. If it is, we exit. + const relativeRegex = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/g; + if ( + typeof destinationUrl === 'string' && + destinationUrl.match(relativeRegex) + ) { + return ''; + } + + if (typeof destinationUrl === 'string') { + try { + destinationUrl = new URL(destinationUrl); + } catch (err) { + return ''; //I dont know what we should do when an error like this happens. Any ideas?? + } + } + // this regex is based on the assumption that we wont be using TLDs longer than 3 characters. If we do, it will break // the logic and take that longer TLD as the main domain, for example hub.balena.edge.io -> edge.io const regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g; - let actualDomainMatch; + const destinationDomainMatch = destinationUrl + ? destinationUrl.hostname.match(regex) + : null; + const destinationDomain = destinationDomainMatch?.[0]; + + let actualDomainMatch: RegExpMatchArray | null; if (currentUrl) { actualDomainMatch = currentUrl.hostname.match(regex); } else if (typeof window !== 'undefined') { @@ -189,16 +211,7 @@ export class AnalyticsUrlParams { actualDomainMatch = null; } - const destinationDomainMatch = destinationUrl - ? destinationUrl.hostname.match(regex) - : null; - - const actualDomain = actualDomainMatch - ? actualDomainMatch.toString() - : null; - const destinationDomain = destinationDomainMatch - ? destinationDomainMatch.toString() - : null; + const actualDomain = actualDomainMatch?.[0]; if (!destinationDomain || actualDomain !== destinationDomain) { return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] diff --git a/test/url-params.test.ts b/test/url-params.test.ts index 0fb4766..4ba4b7c 100644 --- a/test/url-params.test.ts +++ b/test/url-params.test.ts @@ -137,6 +137,27 @@ test('parsing and matching destination and actual URL to regex', () => { new URL('https://domain2.edge.io'), ), ).toBe(''); + + // Case when passing a relative URL as destinationUrl + expect( + urlParams.getQueryString('/etcher', new URL('https://domain2.edge.io')), + ).toBe(''); + + // Case when passing an absolute URL as a string for destinationUrl and no passing is expected + expect( + urlParams.getQueryString( + 'https://test.domain.io', + new URL('https://domain.io'), + ), + ).toBe(''); + + // Case when passing an absolute URL as a string for destinationUrl and passing is expected + expect( + urlParams.getQueryString( + 'https://test.domain.io', + new URL('https://otherdomain.com'), + ), + ).toBe('d_id=d1&s_id=123'); }); interface AnalyticsMock { From 652d25357c1160f62c78ae6105fead8a81e67916 Mon Sep 17 00:00:00 2001 From: Ezequiel Boehler Date: Fri, 13 Aug 2021 10:29:27 -0400 Subject: [PATCH 2/4] log error before returning --- src/url-params.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/url-params.ts b/src/url-params.ts index d58226f..e67a85e 100644 --- a/src/url-params.ts +++ b/src/url-params.ts @@ -189,7 +189,8 @@ export class AnalyticsUrlParams { try { destinationUrl = new URL(destinationUrl); } catch (err) { - return ''; //I dont know what we should do when an error like this happens. Any ideas?? + console.error(err) + return ''; } } From 8faf51bfe26c320e59ff2aa88b50d155b515fe5a Mon Sep 17 00:00:00 2001 From: Ezequiel Boehler Date: Fri, 13 Aug 2021 11:23:32 -0400 Subject: [PATCH 3/4] added node types dependency --- package.json | 1 + tsconfig.json | 3 +++ 2 files changed, 4 insertions(+) diff --git a/package.json b/package.json index ece9d87..316bba5 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@types/js-cookie": "^2.2.5", "@types/lodash": "^4.14.142", "@types/mixpanel-browser": "2.35.1", + "@types/node": "^16.6.1", "husky": "^3.0.9", "jest": "^24.9.0", "lint-staged": "^9.4.2", diff --git a/tsconfig.json b/tsconfig.json index 4547fe8..ba311f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,8 @@ { "compilerOptions": { + "lib": [ + "ES6","DOM" + ], "module": "commonjs", "target": "es5", "outDir": "dist", From ef48d4d2102007e73dbcd8e7620c7004904f374d Mon Sep 17 00:00:00 2001 From: Ezequiel Boehler Date: Fri, 13 Aug 2021 11:38:17 -0400 Subject: [PATCH 4/4] prettified --- src/url-params.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/url-params.ts b/src/url-params.ts index e67a85e..04e8683 100644 --- a/src/url-params.ts +++ b/src/url-params.ts @@ -189,8 +189,8 @@ export class AnalyticsUrlParams { try { destinationUrl = new URL(destinationUrl); } catch (err) { - console.error(err) - return ''; + console.error(err); + return ''; } }