Skip to content

Commit c36de75

Browse files
authored
ref(dsn): trim dsn size (#4325)
* ref(utils): dsn * ref(utils): dsn * ref(utils): dsn * ref(core): simplify dsn parsing * ref(utils): get global url * ref(utils): fix test * Revert "ref(utils): fix test" This reverts commit 13cf627. * Revert "ref(utils): get global url" This reverts commit 61feeeb. * Revert "ref(core): simplify dsn parsing" This reverts commit c90b6f9. * fix(dsn): update test * ref(utils): drop dsn toString class method * ref(utils): drop export * fix import * fix import * fix import
1 parent 1c3f85a commit c36de75

File tree

13 files changed

+201
-225
lines changed

13 files changed

+201
-225
lines changed

packages/browser/src/transports/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@sentry/types';
1515
import {
1616
dateTimestampInSeconds,
17+
dsnToString,
1718
eventStatusFromHttpCode,
1819
getGlobalObject,
1920
logger,
@@ -116,7 +117,7 @@ export abstract class BaseTransport implements Transport {
116117

117118
const url = getEnvelopeEndpointWithUrlEncodedAuth(this._api.dsn, this._api.tunnel);
118119
// Envelope header is required to be at least an empty object
119-
const envelopeHeader = JSON.stringify({ ...(this._api.tunnel && { dsn: this._api.dsn.toString() }) });
120+
const envelopeHeader = JSON.stringify({ ...(this._api.tunnel && { dsn: dsnToString(this._api.dsn) }) });
120121
const itemHeaders = JSON.stringify({
121122
type: 'client_report',
122123
});

packages/core/src/api.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { DsnLike, SdkMetadata } from '@sentry/types';
2-
import { Dsn, urlEncode } from '@sentry/utils';
1+
import { DsnComponents, DsnLike, SdkMetadata } from '@sentry/types';
2+
import { dsnToString, makeDsn, urlEncode } from '@sentry/utils';
33

44
const SENTRY_API_VERSION = '7';
55

@@ -12,7 +12,7 @@ export interface APIDetails {
1212
/** Metadata about the SDK (name, version, etc) for inclusion in envelope headers */
1313
metadata: SdkMetadata;
1414
/** The internally used Dsn object. */
15-
readonly dsn: Dsn;
15+
readonly dsn: DsnComponents;
1616
/** The envelope tunnel to use. */
1717
readonly tunnel?: string;
1818
}
@@ -32,21 +32,21 @@ export class API {
3232
public metadata: SdkMetadata;
3333

3434
/** The internally used Dsn object. */
35-
private readonly _dsnObject: Dsn;
35+
private readonly _dsnObject: DsnComponents;
3636

3737
/** The envelope tunnel to use. */
3838
private readonly _tunnel?: string;
3939

4040
/** Create a new instance of API */
4141
public constructor(dsn: DsnLike, metadata: SdkMetadata = {}, tunnel?: string) {
4242
this.dsn = dsn;
43-
this._dsnObject = new Dsn(dsn);
43+
this._dsnObject = makeDsn(dsn);
4444
this.metadata = metadata;
4545
this._tunnel = tunnel;
4646
}
4747

4848
/** Returns the Dsn object. */
49-
public getDsn(): Dsn {
49+
public getDsn(): DsnComponents {
5050
return this._dsnObject;
5151
}
5252

@@ -89,25 +89,25 @@ export function initAPIDetails(dsn: DsnLike, metadata?: SdkMetadata, tunnel?: st
8989
return {
9090
initDsn: dsn,
9191
metadata: metadata || {},
92-
dsn: new Dsn(dsn),
92+
dsn: makeDsn(dsn),
9393
tunnel,
9494
} as APIDetails;
9595
}
9696

9797
/** Returns the prefix to construct Sentry ingestion API endpoints. */
98-
function getBaseApiEndpoint(dsn: Dsn): string {
98+
function getBaseApiEndpoint(dsn: DsnComponents): string {
9999
const protocol = dsn.protocol ? `${dsn.protocol}:` : '';
100100
const port = dsn.port ? `:${dsn.port}` : '';
101101
return `${protocol}//${dsn.host}${port}${dsn.path ? `/${dsn.path}` : ''}/api/`;
102102
}
103103

104104
/** Returns the ingest API endpoint for target. */
105-
function _getIngestEndpoint(dsn: Dsn, target: 'store' | 'envelope'): string {
105+
function _getIngestEndpoint(dsn: DsnComponents, target: 'store' | 'envelope'): string {
106106
return `${getBaseApiEndpoint(dsn)}${dsn.projectId}/${target}/`;
107107
}
108108

109109
/** Returns a URL-encoded string with auth config suitable for a query string. */
110-
function _encodedAuth(dsn: Dsn): string {
110+
function _encodedAuth(dsn: DsnComponents): string {
111111
return urlEncode({
112112
// We send only the minimum set of required information. See
113113
// https://github.com/getsentry/sentry-javascript/issues/2572.
@@ -117,7 +117,7 @@ function _encodedAuth(dsn: Dsn): string {
117117
}
118118

119119
/** Returns the store endpoint URL. */
120-
function getStoreEndpoint(dsn: Dsn): string {
120+
function getStoreEndpoint(dsn: DsnComponents): string {
121121
return _getIngestEndpoint(dsn, 'store');
122122
}
123123

@@ -126,12 +126,12 @@ function getStoreEndpoint(dsn: Dsn): string {
126126
*
127127
* Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
128128
*/
129-
export function getStoreEndpointWithUrlEncodedAuth(dsn: Dsn): string {
129+
export function getStoreEndpointWithUrlEncodedAuth(dsn: DsnComponents): string {
130130
return `${getStoreEndpoint(dsn)}?${_encodedAuth(dsn)}`;
131131
}
132132

133133
/** Returns the envelope endpoint URL. */
134-
function _getEnvelopeEndpoint(dsn: Dsn): string {
134+
function _getEnvelopeEndpoint(dsn: DsnComponents): string {
135135
return _getIngestEndpoint(dsn, 'envelope');
136136
}
137137

@@ -140,15 +140,19 @@ function _getEnvelopeEndpoint(dsn: Dsn): string {
140140
*
141141
* Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests.
142142
*/
143-
export function getEnvelopeEndpointWithUrlEncodedAuth(dsn: Dsn, tunnel?: string): string {
143+
export function getEnvelopeEndpointWithUrlEncodedAuth(dsn: DsnComponents, tunnel?: string): string {
144144
return tunnel ? tunnel : `${_getEnvelopeEndpoint(dsn)}?${_encodedAuth(dsn)}`;
145145
}
146146

147147
/**
148148
* Returns an object that can be used in request headers.
149149
* This is needed for node and the old /store endpoint in sentry
150150
*/
151-
export function getRequestHeaders(dsn: Dsn, clientName: string, clientVersion: string): { [key: string]: string } {
151+
export function getRequestHeaders(
152+
dsn: DsnComponents,
153+
clientName: string,
154+
clientVersion: string,
155+
): { [key: string]: string } {
152156
// CHANGE THIS to use metadata but keep clientName and clientVersion compatible
153157
const header = [`Sentry sentry_version=${SENTRY_API_VERSION}`];
154158
header.push(`sentry_client=${clientName}/${clientVersion}`);
@@ -171,10 +175,10 @@ export function getReportDialogEndpoint(
171175
user?: { name?: string; email?: string };
172176
},
173177
): string {
174-
const dsn = new Dsn(dsnLike);
178+
const dsn = makeDsn(dsnLike);
175179
const endpoint = `${getBaseApiEndpoint(dsn)}embed/error-page/`;
176180

177-
let encodedOptions = `dsn=${dsn.toString()}`;
181+
let encodedOptions = `dsn=${dsnToString(dsn)}`;
178182
for (const key in dialogOptions) {
179183
if (key === 'dsn') {
180184
continue;

packages/core/src/baseclient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { Scope, Session } from '@sentry/hub';
33
import {
44
Client,
5+
DsnComponents,
56
Event,
67
EventHint,
78
Integration,
@@ -13,11 +14,11 @@ import {
1314
import {
1415
checkOrSetAlreadyCaught,
1516
dateTimestampInSeconds,
16-
Dsn,
1717
isPlainObject,
1818
isPrimitive,
1919
isThenable,
2020
logger,
21+
makeDsn,
2122
normalize,
2223
rejectedSyncPromise,
2324
resolvedSyncPromise,
@@ -76,7 +77,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
7677
protected readonly _options: O;
7778

7879
/** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */
79-
protected readonly _dsn?: Dsn;
80+
protected readonly _dsn?: DsnComponents;
8081

8182
/** Array of used integrations. */
8283
protected _integrations: IntegrationIndex = {};
@@ -95,7 +96,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
9596
this._options = options;
9697

9798
if (options.dsn) {
98-
this._dsn = new Dsn(options.dsn);
99+
this._dsn = makeDsn(options.dsn);
99100
}
100101
}
101102

@@ -187,7 +188,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
187188
/**
188189
* @inheritDoc
189190
*/
190-
public getDsn(): Dsn | undefined {
191+
public getDsn(): DsnComponents | undefined {
191192
return this._dsn;
192193
}
193194

packages/core/src/request.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types';
2+
import { dsnToString } from '@sentry/utils';
23

34
import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
45

@@ -33,7 +34,7 @@ export function sessionToSentryRequest(session: Session | SessionAggregates, api
3334
const envelopeHeaders = JSON.stringify({
3435
sent_at: new Date().toISOString(),
3536
...(sdkInfo && { sdk: sdkInfo }),
36-
...(!!api.tunnel && { dsn: api.dsn.toString() }),
37+
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
3738
});
3839
// I know this is hacky but we don't want to add `session` to request type since it's never rate limited
3940
const type: SentryRequestType = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
@@ -81,7 +82,7 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
8182
event_id: event.event_id,
8283
sent_at: new Date().toISOString(),
8384
...(sdkInfo && { sdk: sdkInfo }),
84-
...(!!api.tunnel && { dsn: api.dsn.toString() }),
85+
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
8586
});
8687
const itemHeaders = JSON.stringify({
8788
type: eventType,

packages/core/test/lib/api.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable deprecation/deprecation */
2-
import { Dsn } from '@sentry/utils';
2+
import { makeDsn } from '@sentry/utils';
33

44
import { API, getReportDialogEndpoint, getRequestHeaders } from '../../src/api';
55

@@ -25,12 +25,12 @@ describe('API', () => {
2525
});
2626

2727
test('getRequestHeaders', () => {
28-
expect(getRequestHeaders(new Dsn(dsnPublic), 'a', '1.0')).toMatchObject({
28+
expect(getRequestHeaders(makeDsn(dsnPublic), 'a', '1.0')).toMatchObject({
2929
'Content-Type': 'application/json',
3030
'X-Sentry-Auth': expect.stringMatching(/^Sentry sentry_version=\d, sentry_client=a\/1\.0, sentry_key=abc$/),
3131
});
3232

33-
expect(getRequestHeaders(new Dsn(legacyDsn), 'a', '1.0')).toMatchObject({
33+
expect(getRequestHeaders(makeDsn(legacyDsn), 'a', '1.0')).toMatchObject({
3434
'Content-Type': 'application/json',
3535
'X-Sentry-Auth': expect.stringMatching(
3636
/^Sentry sentry_version=\d, sentry_client=a\/1\.0, sentry_key=abc, sentry_secret=123$/,
@@ -119,6 +119,12 @@ describe('API', () => {
119119
});
120120

121121
test('getDsn', () => {
122-
expect(new API(dsnPublic).getDsn()).toEqual(new Dsn(dsnPublic));
122+
expect(new API(dsnPublic).getDsn().host).toEqual(makeDsn(dsnPublic).host);
123+
expect(new API(dsnPublic).getDsn().path).toEqual(makeDsn(dsnPublic).path);
124+
expect(new API(dsnPublic).getDsn().pass).toEqual(makeDsn(dsnPublic).pass);
125+
expect(new API(dsnPublic).getDsn().port).toEqual(makeDsn(dsnPublic).port);
126+
expect(new API(dsnPublic).getDsn().protocol).toEqual(makeDsn(dsnPublic).protocol);
127+
expect(new API(dsnPublic).getDsn().projectId).toEqual(makeDsn(dsnPublic).projectId);
128+
expect(new API(dsnPublic).getDsn().publicKey).toEqual(makeDsn(dsnPublic).publicKey);
123129
});
124130
});

packages/core/test/lib/base.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hub, Scope, Session } from '@sentry/hub';
22
import { Event, Span, Transport } from '@sentry/types';
3-
import { logger, SentryError, SyncPromise } from '@sentry/utils';
3+
import { dsnToString, logger, SentryError, SyncPromise } from '@sentry/utils';
44

55
import * as integrationModule from '../../src/integration';
66
import { TestBackend } from '../mocks/backend';
@@ -67,7 +67,7 @@ describe('BaseClient', () => {
6767
test('returns the Dsn', () => {
6868
expect.assertions(1);
6969
const client = new TestClient({ dsn: PUBLIC_DSN });
70-
expect(client.getDsn()!.toString()).toBe(PUBLIC_DSN);
70+
expect(dsnToString(client.getDsn())).toBe(PUBLIC_DSN);
7171
});
7272

7373
test('allows missing Dsn', () => {

packages/nextjs/test/integration/next-env.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/// <reference types="next" />
2-
/// <reference types="next/types/global" />
32
/// <reference types="next/image-types/global" />
43

54
// NOTE: This file should not be edited

packages/node/src/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseBackend } from '@sentry/core';
22
import { Event, EventHint, SeverityLevel, Transport, TransportOptions } from '@sentry/types';
3-
import { Dsn } from '@sentry/utils';
3+
import { makeDsn } from '@sentry/utils';
44

55
import { eventFromException, eventFromMessage } from './eventbuilder';
66
import { HTTPSTransport, HTTPTransport } from './transports';
@@ -35,7 +35,7 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
3535
return super._setupTransport();
3636
}
3737

38-
const dsn = new Dsn(this._options.dsn);
38+
const dsn = makeDsn(this._options.dsn);
3939

4040
const transportOptions: TransportOptions = {
4141
...this._options.transportOptions,

packages/types/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Dsn } from './dsn';
1+
import { DsnComponents } from './dsn';
22
import { Event, EventHint } from './event';
33
import { Integration, IntegrationClass } from './integration';
44
import { Options } from './options';
@@ -55,7 +55,7 @@ export interface Client<O extends Options = Options> {
5555
captureSession?(session: Session): void;
5656

5757
/** Returns the current Dsn. */
58-
getDsn(): Dsn | undefined;
58+
getDsn(): DsnComponents | undefined;
5959

6060
/** Returns the current options. */
6161
getOptions(): O;

packages/types/src/dsn.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,3 @@ export interface DsnComponents {
2323

2424
/** Anything that can be parsed into a Dsn. */
2525
export type DsnLike = string | DsnComponents;
26-
27-
/** The Sentry Dsn, identifying a Sentry instance and project. */
28-
export interface Dsn extends DsnComponents {
29-
/**
30-
* Renders the string representation of this Dsn.
31-
*
32-
* By default, this will render the public representation without the password
33-
* component. To get the deprecated private representation, set `withPassword`
34-
* to true.
35-
*
36-
* @param withPassword When set to true, the password will be included.
37-
*/
38-
toString(withPassword: boolean): string;
39-
}

0 commit comments

Comments
 (0)