Skip to content

Commit 21c759c

Browse files
committed
remove noop transport
1 parent 4c4b2af commit 21c759c

File tree

6 files changed

+58
-46
lines changed

6 files changed

+58
-46
lines changed

packages/browser/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class BrowserClient extends BaseClient<BrowserOptions> {
124124
protected _setupTransport(): Transport {
125125
if (!this._options.dsn) {
126126
// We return the noop transport here in case there is no Dsn.
127-
return super._setupTransport();
127+
return undefined;
128128
}
129129

130130
const transportOptions = {

packages/core/src/baseclient.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
} from '@sentry/utils';
2929

3030
import { IntegrationIndex, setupIntegrations } from './integration';
31-
import { NoopTransport } from './transports/noop';
3231

3332
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
3433

@@ -76,7 +75,7 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
7675
protected _numProcessing: number = 0;
7776

7877
/** Cached transport used internally. */
79-
protected _transport: Transport;
78+
protected _transport: Transport | undefined;
8079

8180
/**
8281
* Initializes this client instance.
@@ -194,7 +193,7 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
194193
/**
195194
* @inheritDoc
196195
*/
197-
public getTransport(): Transport {
196+
public getTransport(): Transport | undefined {
198197
return this._transport;
199198
}
200199

@@ -203,9 +202,11 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
203202
*/
204203
public flush(timeout?: number): PromiseLike<boolean> {
205204
return this._isClientDoneProcessing(timeout).then(clientFinished => {
206-
return this.getTransport()
207-
.close(timeout)
208-
.then(transportFlushed => clientFinished && transportFlushed);
205+
const transport = this.getTransport();
206+
if (transport) {
207+
return transport.close(timeout).then(transportFlushed => clientFinished && transportFlushed);
208+
}
209+
return false;
209210
});
210211
}
211212

@@ -244,21 +245,28 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
244245
* @inheritDoc
245246
*/
246247
public sendEvent(event: Event): void {
247-
void this._transport.sendEvent(event).then(null, reason => {
248-
logger.error(`Error while sending event: ${reason}`);
249-
});
248+
const transport = this.getTransport();
249+
if (transport) {
250+
void transport.sendEvent(event).then(null, reason => {
251+
logger.error(`Error while sending event: ${reason}`);
252+
});
253+
}
250254
}
251255

252256
/**
253257
* @inheritDoc
254258
*/
255259
public sendSession(session: Session): void {
256-
if (!this._transport.sendSession) {
260+
const transport = this.getTransport();
261+
if (!transport) {
262+
return;
263+
}
264+
if (!transport.sendSession) {
257265
logger.warn("Dropping session because custom transport doesn't implement sendSession");
258266
return;
259267
}
260268

261-
void this._transport.sendSession(session).then(null, reason => {
269+
void transport.sendSession(session).then(null, reason => {
262270
logger.error(`Error while sending session: ${reason}`);
263271
});
264272
}
@@ -525,7 +533,7 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
525533
type RecordLostEventParams = Parameters<RecordLostEvent>;
526534

527535
function recordLostEvent(outcome: RecordLostEventParams[0], category: RecordLostEventParams[1]): void {
528-
if (transport.recordLostEvent) {
536+
if (transport && transport.recordLostEvent) {
529537
transport.recordLostEvent(outcome, category);
530538
}
531539
}
@@ -613,8 +621,8 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
613621
/**
614622
* Sets up the transport so it can be used later to send requests.
615623
*/
616-
protected _setupTransport(): Transport {
617-
return new NoopTransport();
624+
protected _setupTransport(): Transport | undefined {
625+
return undefined;
618626
}
619627

620628
/**

packages/core/src/transports/noop.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/core/test/mocks/client.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
import { Options, EventHint, Event, Session, SeverityLevel, Transport } from '@sentry/types';
1+
import { Options, EventHint, Event, Session, SeverityLevel, Transport, Response } from '@sentry/types';
22

33
import { BaseClient } from '../../src/baseclient';
44
import { initAndBind } from '../../src/sdk';
55
import { resolvedSyncPromise } from '@sentry/utils';
66

7+
export class NoopTransport implements Transport {
8+
/**
9+
* @inheritDoc
10+
*/
11+
public sendEvent(_: Event): PromiseLike<Response> {
12+
return resolvedSyncPromise({
13+
reason: `NoopTransport: Event has been skipped because no Dsn is configured.`,
14+
status: 'skipped',
15+
});
16+
}
17+
18+
/**
19+
* @inheritDoc
20+
*/
21+
public close(_?: number): PromiseLike<boolean> {
22+
return resolvedSyncPromise(true);
23+
}
24+
}
25+
726
interface TestOptions extends Options {
827
test?: boolean;
928
mockInstallFailure?: boolean;
@@ -39,7 +58,7 @@ export class TestClient extends BaseClient<TestOptions> {
3958
protected _setupTransport(): Transport {
4059
if (!this._options.dsn) {
4160
// We return the noop transport here in case there is no Dsn.
42-
return super._setupTransport();
61+
return new NoopTransport();
4362
}
4463

4564
const transportOptions = this._options.transportOptions
@@ -50,7 +69,7 @@ export class TestClient extends BaseClient<TestOptions> {
5069
return new this._options.transport(transportOptions);
5170
}
5271

53-
return super._setupTransport();
72+
return new NoopTransport();
5473
}
5574

5675
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types

packages/node/src/client.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,21 @@ export class NodeClient extends BaseClient<NodeOptions> {
9696
/** Method that initialises an instance of SessionFlusher on Client */
9797
public initSessionFlusher(): void {
9898
const { release, environment } = this._options;
99+
const transport = this.getTransport();
100+
101+
if (!transport) {
102+
logger.warn('Cannot initialise an instance of SessionFlusher if no transport is initialized!');
103+
return;
104+
}
99105
if (!release) {
100106
logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
101-
} else {
102-
this._sessionFlusher = new SessionFlusher(this.getTransport(), {
103-
release,
104-
environment,
105-
});
107+
return;
106108
}
109+
110+
this._sessionFlusher = new SessionFlusher(transport, {
111+
release,
112+
environment,
113+
});
107114
}
108115

109116
/**

packages/types/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface Client<O extends Options = Options> {
6161
getOptions(): O;
6262

6363
/** Returns clients transport. */
64-
getTransport?(): Transport;
64+
getTransport?(): Transport | undefined;
6565

6666
/**
6767
* Flush the event queue and set the client to `enabled = false`. See {@link Client.flush}.

0 commit comments

Comments
 (0)