From 61feeeb12f4f6394bcdc4c9d3f43e11ef6c006b5 Mon Sep 17 00:00:00 2001 From: JonasBa Date: Fri, 17 Dec 2021 11:23:20 +0100 Subject: [PATCH] ref(utils): get global url --- packages/core/src/api.ts | 2 +- packages/node/src/integrations/utils/http.ts | 2 +- packages/types/src/dsn.ts | 2 +- packages/utils/src/dsn.ts | 14 ++++---- packages/utils/test/dsn.test.ts | 36 ++++++++++---------- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index c8f1ef011ec5..7ea5a5f99ddb 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -98,7 +98,7 @@ export function initAPIDetails(dsn: DsnLike, metadata?: SdkMetadata, tunnel?: st function getBaseApiEndpoint(dsn: Dsn): string { const protocol = dsn.protocol ? `${dsn.protocol}:` : ''; const port = dsn.port ? `:${dsn.port}` : ''; - return `${protocol}//${dsn.host}${port}${dsn.path ? `/${dsn.path}` : ''}/api/`; + return `${protocol}//${dsn.hostname}${port}${dsn.path ? `/${dsn.path}` : ''}/api/`; } /** Returns the ingest API endpoint for target. */ diff --git a/packages/node/src/integrations/utils/http.ts b/packages/node/src/integrations/utils/http.ts index bb0e9a76cf00..fb3342815095 100644 --- a/packages/node/src/integrations/utils/http.ts +++ b/packages/node/src/integrations/utils/http.ts @@ -14,7 +14,7 @@ export function isSentryRequest(url: string): boolean { const dsn = getCurrentHub() .getClient() ?.getDsn(); - return dsn ? url.includes(dsn.host) : false; + return dsn ? url.includes(dsn.hostname) : false; } /** diff --git a/packages/types/src/dsn.ts b/packages/types/src/dsn.ts index a80ce99003d0..9f0aed000b1b 100644 --- a/packages/types/src/dsn.ts +++ b/packages/types/src/dsn.ts @@ -12,7 +12,7 @@ export interface DsnComponents { /** Private authorization key (deprecated, optional). */ pass?: string; /** Hostname of the Sentry instance. */ - host: string; + hostname: string; /** Port of the Sentry instance. */ port?: string; /** Sub path/ */ diff --git a/packages/utils/src/dsn.ts b/packages/utils/src/dsn.ts index 7bb71ed3e054..89ea16bf89fa 100644 --- a/packages/utils/src/dsn.ts +++ b/packages/utils/src/dsn.ts @@ -2,6 +2,7 @@ import { Dsn, DsnComponents, DsnLike, DsnProtocol } from '@sentry/types'; import { isDebugBuild } from './env'; import { SentryError } from './error'; +import { getGlobalObject } from './global'; function isValidProtocol(protocol?: string): protocol is DsnProtocol { return protocol === 'http' || protocol === 'https'; @@ -21,21 +22,22 @@ function normalizeProtocol(input: string): string { * @param withPassword When set to true, the password will be included. */ function dsntoString(dsn: Dsn, withPassword: boolean = false): string { - const { host, port, path, pass, projectId, protocol, publicKey } = dsn; + const { hostname, port, path, pass, projectId, protocol, publicKey } = dsn; return ( `${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` + - `@${host}${port ? `:${port}` : ''}${path}/${projectId}` + `@${hostname}${port ? `:${port}` : ''}${path}/${projectId}` ); } function dsnFromString(str: string): Dsn { - const url = new URL(str); + const global = getGlobalObject<{ URL: typeof URL }>(); + const url = new global.URL(str); const pathComponents = url.pathname.split('/'); const projectId = pathComponents.pop(); return dsnFromComponents({ - host: url.hostname, + hostname: url.hostname, pass: url.password, path: pathComponents.join('/'), projectId: projectId || '', @@ -56,7 +58,7 @@ function dsnFromComponents(components: DsnComponents): Dsn { protocol: components.protocol, publicKey: components.publicKey || '', pass: components.pass || '', - host: components.host, + hostname: components.hostname, port: components.port || '', path: components.path || '', projectId: components.projectId, @@ -67,7 +69,7 @@ function validateDsn(dsn: Dsn): boolean { if (isDebugBuild()) { const { port, projectId, protocol } = dsn; - const requiredComponents: ReadonlyArray = ['protocol', 'publicKey', 'host', 'projectId']; + const requiredComponents: ReadonlyArray = ['protocol', 'publicKey', 'hostname', 'projectId']; requiredComponents.forEach(component => { if (!dsn[component]) { throw new SentryError(`Invalid Dsn: ${component} missing`); diff --git a/packages/utils/test/dsn.test.ts b/packages/utils/test/dsn.test.ts index bb4e3d81548c..9aa88c151bab 100644 --- a/packages/utils/test/dsn.test.ts +++ b/packages/utils/test/dsn.test.ts @@ -11,7 +11,7 @@ describe('Dsn', () => { describe('fromComponents', () => { test('applies all components', () => { const dsn = makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', pass: 'xyz', port: '1234', projectId: '123', @@ -21,7 +21,7 @@ describe('Dsn', () => { expect(dsn.protocol).toBe('https'); expect(dsn.publicKey).toBe('abc'); expect(dsn.pass).toBe('xyz'); - expect(dsn.host).toBe('sentry.io'); + expect(dsn.hostname).toBe('sentry.io'); expect(dsn.port).toBe('1234'); expect(dsn.path).toBe(''); expect(dsn.projectId).toBe('123'); @@ -29,7 +29,7 @@ describe('Dsn', () => { test('applies partial components', () => { const dsn = makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', projectId: '123', protocol: 'https', publicKey: 'abc', @@ -37,7 +37,7 @@ describe('Dsn', () => { expect(dsn.protocol).toBe('https'); expect(dsn.publicKey).toBe('abc'); expect(dsn.pass).toBe(''); - expect(dsn.host).toBe('sentry.io'); + expect(dsn.hostname).toBe('sentry.io'); expect(dsn.port).toBe(''); expect(dsn.path).toBe(''); expect(dsn.projectId).toBe('123'); @@ -46,23 +46,23 @@ describe('Dsn', () => { testIf(isDebugBuild())('throws for missing components', () => { expect(() => makeDsn({ - host: '', + hostname: '', projectId: '123', - protocol: 'https:', + protocol: 'https', publicKey: 'abc', }), ).toThrow(SentryError); expect(() => makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', projectId: '', - protocol: 'https:', + protocol: 'https', publicKey: 'abc', }), ).toThrow(SentryError); expect(() => makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', projectId: '123', protocol: '' as 'http', // Trick the type checker here publicKey: 'abc', @@ -70,9 +70,9 @@ describe('Dsn', () => { ).toThrow(SentryError); expect(() => makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', projectId: '123', - protocol: 'https:', + protocol: 'https', publicKey: '', }), ).toThrow(SentryError); @@ -81,7 +81,7 @@ describe('Dsn', () => { testIf(isDebugBuild())('throws for invalid components', () => { expect(() => makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', projectId: '123', protocol: 'httpx' as 'http', // Trick the type checker here publicKey: 'abc', @@ -89,10 +89,10 @@ describe('Dsn', () => { ).toThrow(SentryError); expect(() => makeDsn({ - host: 'sentry.io', + hostname: 'sentry.io', port: 'xxx', projectId: '123', - protocol: 'https:', + protocol: 'https', publicKey: 'abc', }), ).toThrow(SentryError); @@ -105,7 +105,7 @@ describe('Dsn', () => { expect(dsn.protocol).toBe('https'); expect(dsn.publicKey).toBe('abc'); expect(dsn.pass).toBe('xyz'); - expect(dsn.host).toBe('sentry.io'); + expect(dsn.hostname).toBe('sentry.io'); expect(dsn.port).toBe('1234'); expect(dsn.path).toBe(''); expect(dsn.projectId).toBe('123'); @@ -116,7 +116,7 @@ describe('Dsn', () => { expect(dsn.protocol).toBe('https'); expect(dsn.publicKey).toBe('abc'); expect(dsn.pass).toBe(''); - expect(dsn.host).toBe('sentry.io'); + expect(dsn.hostname).toBe('sentry.io'); expect(dsn.port).toBe(''); expect(dsn.path).toBe('/123'); expect(dsn.projectId).toBe('321'); @@ -127,7 +127,7 @@ describe('Dsn', () => { expect(dsn.protocol).toBe('https'); expect(dsn.publicKey).toBe('abc'); expect(dsn.pass).toBe(''); - expect(dsn.host).toBe('sentry.io'); + expect(dsn.hostname).toBe('sentry.io'); expect(dsn.port).toBe(''); expect(dsn.path).toBe('/sentry/custom/installation'); expect(dsn.projectId).toBe('321'); @@ -138,7 +138,7 @@ describe('Dsn', () => { expect(dsn.protocol).toBe('https'); expect(dsn.publicKey).toBe('abc'); expect(dsn.pass).toBe(''); - expect(dsn.host).toBe('sentry.io'); + expect(dsn.hostname).toBe('sentry.io'); expect(dsn.port).toBe(''); expect(dsn.path).toBe(''); expect(dsn.projectId).toBe('321');