Skip to content

Commit 0dcc288

Browse files
committed
ref(utils): get global url
1 parent c90b6f9 commit 0dcc288

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

packages/types/src/dsn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface DsnComponents {
1212
/** Private authorization key (deprecated, optional). */
1313
pass?: string;
1414
/** Hostname of the Sentry instance. */
15-
host: string;
15+
hostname: string;
1616
/** Port of the Sentry instance. */
1717
port?: string;
1818
/** Sub path/ */

packages/utils/src/dsn.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Dsn, DsnComponents, DsnLike, DsnProtocol } from '@sentry/types';
22

33
import { isDebugBuild } from './env';
44
import { SentryError } from './error';
5+
import { getGlobalObject } from './global';
56

67
function isValidProtocol(protocol?: string): protocol is DsnProtocol {
78
return protocol === 'http' || protocol === 'https';
@@ -21,21 +22,22 @@ function normalizeProtocol(input: string): string {
2122
* @param withPassword When set to true, the password will be included.
2223
*/
2324
function dsntoString(dsn: Dsn, withPassword: boolean = false): string {
24-
const { host, port, path, pass, projectId, protocol, publicKey } = dsn;
25+
const { hostname, port, path, pass, projectId, protocol, publicKey } = dsn;
2526
return (
2627
`${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` +
27-
`@${host}${port ? `:${port}` : ''}${path}/${projectId}`
28+
`@${hostname}${port ? `:${port}` : ''}${path}/${projectId}`
2829
);
2930
}
3031

3132
function dsnFromString(str: string): Dsn {
32-
const url = new URL(str);
33+
const global = getGlobalObject<{ URL: typeof URL }>();
34+
const url = new global.URL(str);
3335

3436
const pathComponents = url.pathname.split('/');
3537
const projectId = pathComponents.pop();
3638

3739
return dsnFromComponents({
38-
host: url.hostname,
40+
hostname: url.hostname,
3941
pass: url.password,
4042
path: pathComponents.join('/'),
4143
projectId: projectId || '',
@@ -56,7 +58,7 @@ function dsnFromComponents(components: DsnComponents): Dsn {
5658
protocol: components.protocol,
5759
publicKey: components.publicKey || '',
5860
pass: components.pass || '',
59-
host: components.host,
61+
hostname: components.hostname,
6062
port: components.port || '',
6163
path: components.path || '',
6264
projectId: components.projectId,
@@ -67,7 +69,7 @@ function validateDsn(dsn: Dsn): boolean {
6769
if (isDebugBuild()) {
6870
const { port, projectId, protocol } = dsn;
6971

70-
const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'host', 'projectId'];
72+
const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'hostname', 'projectId'];
7173
requiredComponents.forEach(component => {
7274
if (!dsn[component]) {
7375
throw new SentryError(`Invalid Dsn: ${component} missing`);

packages/utils/test/dsn.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Dsn', () => {
1111
describe('fromComponents', () => {
1212
test('applies all components', () => {
1313
const dsn = makeDsn({
14-
host: 'sentry.io',
14+
hostname: 'sentry.io',
1515
pass: 'xyz',
1616
port: '1234',
1717
projectId: '123',
@@ -21,23 +21,23 @@ describe('Dsn', () => {
2121
expect(dsn.protocol).toBe('https');
2222
expect(dsn.publicKey).toBe('abc');
2323
expect(dsn.pass).toBe('xyz');
24-
expect(dsn.host).toBe('sentry.io');
24+
expect(dsn.hostname).toBe('sentry.io');
2525
expect(dsn.port).toBe('1234');
2626
expect(dsn.path).toBe('');
2727
expect(dsn.projectId).toBe('123');
2828
});
2929

3030
test('applies partial components', () => {
3131
const dsn = makeDsn({
32-
host: 'sentry.io',
32+
hostname: 'sentry.io',
3333
projectId: '123',
3434
protocol: 'https',
3535
publicKey: 'abc',
3636
});
3737
expect(dsn.protocol).toBe('https');
3838
expect(dsn.publicKey).toBe('abc');
3939
expect(dsn.pass).toBe('');
40-
expect(dsn.host).toBe('sentry.io');
40+
expect(dsn.hostname).toBe('sentry.io');
4141
expect(dsn.port).toBe('');
4242
expect(dsn.path).toBe('');
4343
expect(dsn.projectId).toBe('123');
@@ -46,33 +46,33 @@ describe('Dsn', () => {
4646
testIf(isDebugBuild())('throws for missing components', () => {
4747
expect(() =>
4848
makeDsn({
49-
host: '',
49+
hostname: '',
5050
projectId: '123',
51-
protocol: 'https:',
51+
protocol: 'https',
5252
publicKey: 'abc',
5353
}),
5454
).toThrow(SentryError);
5555
expect(() =>
5656
makeDsn({
57-
host: 'sentry.io',
57+
hostname: 'sentry.io',
5858
projectId: '',
59-
protocol: 'https:',
59+
protocol: 'https',
6060
publicKey: 'abc',
6161
}),
6262
).toThrow(SentryError);
6363
expect(() =>
6464
makeDsn({
65-
host: 'sentry.io',
65+
hostname: 'sentry.io',
6666
projectId: '123',
6767
protocol: '' as 'http', // Trick the type checker here
6868
publicKey: 'abc',
6969
}),
7070
).toThrow(SentryError);
7171
expect(() =>
7272
makeDsn({
73-
host: 'sentry.io',
73+
hostname: 'sentry.io',
7474
projectId: '123',
75-
protocol: 'https:',
75+
protocol: 'https',
7676
publicKey: '',
7777
}),
7878
).toThrow(SentryError);
@@ -81,18 +81,18 @@ describe('Dsn', () => {
8181
testIf(isDebugBuild())('throws for invalid components', () => {
8282
expect(() =>
8383
makeDsn({
84-
host: 'sentry.io',
84+
hostname: 'sentry.io',
8585
projectId: '123',
8686
protocol: 'httpx' as 'http', // Trick the type checker here
8787
publicKey: 'abc',
8888
}),
8989
).toThrow(SentryError);
9090
expect(() =>
9191
makeDsn({
92-
host: 'sentry.io',
92+
hostname: 'sentry.io',
9393
port: 'xxx',
9494
projectId: '123',
95-
protocol: 'https:',
95+
protocol: 'https',
9696
publicKey: 'abc',
9797
}),
9898
).toThrow(SentryError);
@@ -105,7 +105,7 @@ describe('Dsn', () => {
105105
expect(dsn.protocol).toBe('https');
106106
expect(dsn.publicKey).toBe('abc');
107107
expect(dsn.pass).toBe('xyz');
108-
expect(dsn.host).toBe('sentry.io');
108+
expect(dsn.hostname).toBe('sentry.io');
109109
expect(dsn.port).toBe('1234');
110110
expect(dsn.path).toBe('');
111111
expect(dsn.projectId).toBe('123');
@@ -116,7 +116,7 @@ describe('Dsn', () => {
116116
expect(dsn.protocol).toBe('https');
117117
expect(dsn.publicKey).toBe('abc');
118118
expect(dsn.pass).toBe('');
119-
expect(dsn.host).toBe('sentry.io');
119+
expect(dsn.hostname).toBe('sentry.io');
120120
expect(dsn.port).toBe('');
121121
expect(dsn.path).toBe('/123');
122122
expect(dsn.projectId).toBe('321');
@@ -127,7 +127,7 @@ describe('Dsn', () => {
127127
expect(dsn.protocol).toBe('https');
128128
expect(dsn.publicKey).toBe('abc');
129129
expect(dsn.pass).toBe('');
130-
expect(dsn.host).toBe('sentry.io');
130+
expect(dsn.hostname).toBe('sentry.io');
131131
expect(dsn.port).toBe('');
132132
expect(dsn.path).toBe('/sentry/custom/installation');
133133
expect(dsn.projectId).toBe('321');
@@ -138,7 +138,7 @@ describe('Dsn', () => {
138138
expect(dsn.protocol).toBe('https');
139139
expect(dsn.publicKey).toBe('abc');
140140
expect(dsn.pass).toBe('');
141-
expect(dsn.host).toBe('sentry.io');
141+
expect(dsn.hostname).toBe('sentry.io');
142142
expect(dsn.port).toBe('');
143143
expect(dsn.path).toBe('');
144144
expect(dsn.projectId).toBe('321');

0 commit comments

Comments
 (0)