Skip to content

Commit d4a5603

Browse files
committed
Revert "fix files changed"
This reverts commit f71e413.
1 parent 5efe1ad commit d4a5603

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { DsnComponents, Hub } from '@sentry/types';
2+
3+
/**
4+
* Checks whether given url points to Sentry server
5+
* @param url url to verify
6+
*/
7+
export function isSentryRequestUrl(url: string, hub: Hub): boolean {
8+
const client = hub.getClient();
9+
const dsn = client && client.getDsn();
10+
const tunnel = client && client.getOptions().tunnel;
11+
12+
return checkDsn(url, dsn) || checkTunnel(url, tunnel);
13+
}
14+
15+
function checkTunnel(url: string, tunnel: string | undefined): boolean {
16+
if (!tunnel) {
17+
return false;
18+
}
19+
20+
return removeTrailingSlash(url) === removeTrailingSlash(tunnel);
21+
}
22+
23+
function checkDsn(url: string, dsn: DsnComponents | undefined): boolean {
24+
return dsn ? url.includes(dsn.host) : false;
25+
}
26+
27+
function removeTrailingSlash(str: string): string {
28+
return str[str.length - 1] === '/' ? str.slice(0, -1) : str;
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Hub } from '@sentry/types';
2+
3+
import { isSentryRequestUrl } from '../../../src';
4+
5+
describe('isSentryRequestUrl', () => {
6+
it.each([
7+
['', 'sentry-dsn.com', '', false],
8+
['http://sentry-dsn.com/my-url', 'sentry-dsn.com', '', true],
9+
['http://sentry-dsn.com', 'sentry-dsn.com', '', true],
10+
['http://tunnel:4200', 'sentry-dsn.com', 'http://tunnel:4200', true],
11+
['http://tunnel:4200', 'sentry-dsn.com', 'http://tunnel:4200/', true],
12+
['http://tunnel:4200/', 'sentry-dsn.com', 'http://tunnel:4200', true],
13+
['http://tunnel:4200/a', 'sentry-dsn.com', 'http://tunnel:4200', false],
14+
])('works with url=%s, dsn=%s, tunnel=%s', (url: string, dsn: string, tunnel: string, expected: boolean) => {
15+
const hub = {
16+
getClient: () => {
17+
return {
18+
getOptions: () => ({ tunnel }),
19+
getDsn: () => ({ host: dsn }),
20+
};
21+
},
22+
} as unknown as Hub;
23+
24+
expect(isSentryRequestUrl(url, hub)).toBe(expected);
25+
});
26+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { RequestOptions } from 'http';
2+
3+
/** Build a full URL from request options. */
4+
export function getRequestUrl(requestOptions: RequestOptions): string {
5+
const protocol = requestOptions.protocol || '';
6+
const hostname = requestOptions.hostname || requestOptions.host || '';
7+
// Don't log standard :80 (http) and :443 (https) ports to reduce the noise
8+
// Also don't add port if the hostname already includes a port
9+
const port =
10+
!requestOptions.port || requestOptions.port === 80 || requestOptions.port === 443 || /^(.*):(\d+)$/.test(hostname)
11+
? ''
12+
: `:${requestOptions.port}`;
13+
const path = requestOptions.path ? requestOptions.path : '/';
14+
return `${protocol}//${hostname}${port}${path}`;
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { RequestOptions } from 'http';
2+
3+
import { getRequestUrl } from '../../src/utils/getRequestUrl';
4+
5+
describe('getRequestUrl', () => {
6+
it.each([
7+
[{ protocol: 'http:', hostname: 'localhost', port: 80 }, 'http://localhost/'],
8+
[{ protocol: 'http:', hostname: 'localhost', host: 'localhost:80', port: 80 }, 'http://localhost/'],
9+
[{ protocol: 'http:', hostname: 'localhost', port: 3000 }, 'http://localhost:3000/'],
10+
[{ protocol: 'http:', host: 'localhost:3000', port: 3000 }, 'http://localhost:3000/'],
11+
[{ protocol: 'https:', hostname: 'localhost', port: 443 }, 'https://localhost/'],
12+
[{ protocol: 'https:', hostname: 'localhost', port: 443, path: '/my-path' }, 'https://localhost/my-path'],
13+
[
14+
{ protocol: 'https:', hostname: 'www.example.com', port: 443, path: '/my-path' },
15+
'https://www.example.com/my-path',
16+
],
17+
])('works with %s', (input: RequestOptions, expected: string | undefined) => {
18+
expect(getRequestUrl(input)).toBe(expected);
19+
});
20+
});

0 commit comments

Comments
 (0)