Skip to content

Commit 74036ce

Browse files
committed
Allow transport to accept url for the request directly
1 parent 03d9ef0 commit 74036ce

File tree

1 file changed

+26
-18
lines changed
  • packages/node/src/transports

1 file changed

+26
-18
lines changed

packages/node/src/transports/base.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { API } from '@sentry/core';
22
import { Event, Response, Status, Transport, TransportOptions } from '@sentry/types';
3-
import { logger, parseRetryAfterHeader, PromiseBuffer, SentryError } from '@sentry/utils';
3+
import { logger, parseRetryAfterHeader, PromiseBuffer, SentryError, isString } from '@sentry/utils';
44
import * as fs from 'fs';
55
import * as http from 'http';
66
import * as https from 'https';
@@ -22,6 +22,14 @@ export interface HTTPRequest {
2222
options: http.RequestOptions | https.RequestOptions | string | url.URL,
2323
callback?: (res: http.IncomingMessage) => void,
2424
): http.ClientRequest;
25+
26+
// This is the new type for versions that handle URL argument correctly, but it's most likely not needed here just yet
27+
28+
// request(
29+
// url: string | url.URL,
30+
// options: http.RequestOptions | https.RequestOptions,
31+
// callback?: (res: http.IncomingMessage) => void,
32+
// ): http.ClientRequest;
2533
}
2634

2735
/** Base Transport class implementation */
@@ -47,30 +55,30 @@ export abstract class BaseTransport implements Transport {
4755
}
4856

4957
/** Returns a build request option object used by request */
50-
protected _getRequestOptions(): http.RequestOptions | https.RequestOptions {
58+
protected _getRequestOptions(address: string | url.URL): http.RequestOptions | https.RequestOptions {
59+
if (!isString(address) || !(address instanceof url.URL)) {
60+
throw new SentryError(`Incorrect transport url: ${address}`);
61+
}
62+
5163
const headers = {
5264
...this._api.getRequestHeaders(SDK_NAME, SDK_VERSION),
5365
...this.options.headers,
5466
};
55-
const dsn = this._api.getDsn();
67+
const addr = address instanceof url.URL ? address : new url.URL(address);
68+
const { hostname, pathname: path, port, protocol } = addr;
5669

57-
const options: {
58-
[key: string]: any;
59-
} = {
70+
return {
6071
agent: this.client,
61-
headers,
62-
hostname: dsn.host,
6372
method: 'POST',
64-
path: this._api.getStoreEndpointPath(),
65-
port: dsn.port,
66-
protocol: `${dsn.protocol}:`,
73+
headers,
74+
hostname,
75+
path,
76+
port,
77+
protocol,
78+
...(this.options.caCerts && {
79+
ca: fs.readFileSync(this.options.caCerts),
80+
}),
6781
};
68-
69-
if (this.options.caCerts) {
70-
options.ca = fs.readFileSync(this.options.caCerts);
71-
}
72-
73-
return options;
7482
}
7583

7684
/** JSDoc */
@@ -84,7 +92,7 @@ export abstract class BaseTransport implements Transport {
8492
}
8593
return this._buffer.add(
8694
new Promise<Response>((resolve, reject) => {
87-
const req = httpModule.request(this._getRequestOptions(), (res: http.IncomingMessage) => {
95+
const req = httpModule.request(this._getRequestOptions('http://foo.com/123'), (res: http.IncomingMessage) => {
8896
const statusCode = res.statusCode || 500;
8997
const status = Status.fromHttpCode(statusCode);
9098

0 commit comments

Comments
 (0)