-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
134 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { DsnComponents, Hub } from '@sentry/types'; | ||
|
||
/** | ||
* Checks whether given url points to Sentry server | ||
* @param url url to verify | ||
*/ | ||
export function isSentryRequestUrl(url: string, hub: Hub): boolean { | ||
const client = hub.getClient(); | ||
const dsn = client && client.getDsn(); | ||
const tunnel = client && client.getOptions().tunnel; | ||
|
||
return checkDsn(url, dsn) || checkTunnel(url, tunnel); | ||
} | ||
|
||
function checkTunnel(url: string, tunnel: string | undefined): boolean { | ||
if (!tunnel) { | ||
return false; | ||
} | ||
|
||
return removeTrailingSlash(url) === removeTrailingSlash(tunnel); | ||
} | ||
|
||
function checkDsn(url: string, dsn: DsnComponents | undefined): boolean { | ||
return dsn ? url.includes(dsn.host) : false; | ||
} | ||
|
||
function removeTrailingSlash(str: string): string { | ||
return str[str.length - 1] === '/' ? str.slice(0, -1) : str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Hub } from '@sentry/types'; | ||
|
||
import { isSentryRequestUrl } from '../../../src'; | ||
|
||
describe('isSentryRequestUrl', () => { | ||
it.each([ | ||
['', 'sentry-dsn.com', '', false], | ||
['http://sentry-dsn.com/my-url', 'sentry-dsn.com', '', true], | ||
['http://sentry-dsn.com', 'sentry-dsn.com', '', true], | ||
['http://tunnel:4200', 'sentry-dsn.com', 'http://tunnel:4200', true], | ||
['http://tunnel:4200', 'sentry-dsn.com', 'http://tunnel:4200/', true], | ||
['http://tunnel:4200/', 'sentry-dsn.com', 'http://tunnel:4200', true], | ||
['http://tunnel:4200/a', 'sentry-dsn.com', 'http://tunnel:4200', false], | ||
])('works with url=%s, dsn=%s, tunnel=%s', (url: string, dsn: string, tunnel: string, expected: boolean) => { | ||
const hub = { | ||
getClient: () => { | ||
return { | ||
getOptions: () => ({ tunnel }), | ||
getDsn: () => ({ host: dsn }), | ||
}; | ||
}, | ||
} as unknown as Hub; | ||
|
||
expect(isSentryRequestUrl(url, hub)).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { RequestOptions } from 'http'; | ||
|
||
/** Build a full URL from request options. */ | ||
export function getRequestUrl(requestOptions: RequestOptions): string { | ||
const protocol = requestOptions.protocol || ''; | ||
const hostname = requestOptions.hostname || requestOptions.host || ''; | ||
// Don't log standard :80 (http) and :443 (https) ports to reduce the noise | ||
// Also don't add port if the hostname already includes a port | ||
const port = | ||
!requestOptions.port || requestOptions.port === 80 || requestOptions.port === 443 || /^(.*):(\d+)$/.test(hostname) | ||
? '' | ||
: `:${requestOptions.port}`; | ||
const path = requestOptions.path ? requestOptions.path : '/'; | ||
return `${protocol}//${hostname}${port}${path}`; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/node-experimental/test/utils/getRequestUrl.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { RequestOptions } from 'http'; | ||
|
||
import { getRequestUrl } from '../../src/utils/getRequestUrl'; | ||
|
||
describe('getRequestUrl', () => { | ||
it.each([ | ||
[{ protocol: 'http:', hostname: 'localhost', port: 80 }, 'http://localhost/'], | ||
[{ protocol: 'http:', hostname: 'localhost', host: 'localhost:80', port: 80 }, 'http://localhost/'], | ||
[{ protocol: 'http:', hostname: 'localhost', port: 3000 }, 'http://localhost:3000/'], | ||
[{ protocol: 'http:', host: 'localhost:3000', port: 3000 }, 'http://localhost:3000/'], | ||
[{ protocol: 'https:', hostname: 'localhost', port: 443 }, 'https://localhost/'], | ||
[{ protocol: 'https:', hostname: 'localhost', port: 443, path: '/my-path' }, 'https://localhost/my-path'], | ||
[ | ||
{ protocol: 'https:', hostname: 'www.example.com', port: 443, path: '/my-path' }, | ||
'https://www.example.com/my-path', | ||
], | ||
])('works with %s', (input: RequestOptions, expected: string | undefined) => { | ||
expect(getRequestUrl(input)).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters