Skip to content

Commit

Permalink
ref(utils): get global url
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Dec 17, 2021
1 parent c90b6f9 commit 61feeeb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/integrations/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/ */
Expand Down
14 changes: 8 additions & 6 deletions packages/utils/src/dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 || '',
Expand All @@ -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,
Expand All @@ -67,7 +69,7 @@ function validateDsn(dsn: Dsn): boolean {
if (isDebugBuild()) {
const { port, projectId, protocol } = dsn;

const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'host', 'projectId'];
const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'hostname', 'projectId'];
requiredComponents.forEach(component => {
if (!dsn[component]) {
throw new SentryError(`Invalid Dsn: ${component} missing`);
Expand Down
36 changes: 18 additions & 18 deletions packages/utils/test/dsn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -21,23 +21,23 @@ 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');
});

test('applies partial components', () => {
const dsn = makeDsn({
host: 'sentry.io',
hostname: 'sentry.io',
projectId: '123',
protocol: 'https',
publicKey: 'abc',
});
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');
Expand All @@ -46,33 +46,33 @@ 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',
}),
).toThrow(SentryError);
expect(() =>
makeDsn({
host: 'sentry.io',
hostname: 'sentry.io',
projectId: '123',
protocol: 'https:',
protocol: 'https',
publicKey: '',
}),
).toThrow(SentryError);
Expand All @@ -81,18 +81,18 @@ 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',
}),
).toThrow(SentryError);
expect(() =>
makeDsn({
host: 'sentry.io',
hostname: 'sentry.io',
port: 'xxx',
projectId: '123',
protocol: 'https:',
protocol: 'https',
publicKey: 'abc',
}),
).toThrow(SentryError);
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit 61feeeb

Please sign in to comment.