getsentry / getsentry/sentry-javascript

Deprecate and remove `parseUrl` and refactor `getSanitizedUrlString`

未关闭
#15,767 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Core Improvement
主要语言
TypeScript
星标
8.7k
派生
1.8k
平均合并
1 天 17 小时
30 天内合并 PR
515

描述

### Description

Now that we support ES2020 (aka not IE11 anymore) and Node.js 18+, we can get rid of `parseUrl` in favor of a method that just uses the built-in URL object. This will save us some bundle size (given we can remove that regex), and we get performance benefits from using native code.

https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/core/src/utils-hoist/url.ts#L17

Instead of just blanket replacing `parseUrl`, we'll slowly convert all it's usages to using a new helper, which looks something like so:

```js
/**
* Parses string to a URL object
*
* @param url - The URL to parse
* @returns The parsed URL object or undefined if the URL is invalid
*/
export function parseStringToURL(url: string): URL | undefined {
try {
// Node 20+, Chrome 120+, Firefox 115+, Safari 17+
if ('canParse' in URL) {
// Use `canParse` to short-circuit the URL constructor if it's not a valid URL
// This is faster than trying to construct the URL and catching the error
return (URL as unknown as URLwithCanParse).canParse(url) ? new URL(url) : undefined;
}
} catch {
// empty body
}

return undefined;
}
```

- [x] Remove all usages in `@sentry/core`, and implement `parseStringToURL`
- [x] Replace https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/browser-utils/src/instrument/xhr.ts#L150
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/browser/src/integrations/breadcrumbs.ts#L36
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/opentelemetry/src/utils/parseSpanDescription.ts#L23
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/node/src/integrations/http/SentryHttpInstrumentation.ts#L17
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/node/src/integrations/node-fetch/SentryNodeFetchInstrumentation.ts#L6
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/browser/src/tracing/request.ts#L22
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/browser-utils/src/metrics/browserMetrics.ts#L9

With that, we can also refactor `getSanitizedUrlString`, which should provide some performance benefits.

https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/core/src/utils-hoist/url.ts#L55

- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/bun/src/integrations/bunserver.ts#L10
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/opentelemetry/src/utils/getRequestSpanData.ts#L9
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/node/src/integrations/node-fetch/SentryNodeFetchInstrumentation.ts#L6
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/node/src/integrations/http/SentryHttpInstrumentation.ts#L14
- [ ] https://github.com/getsentry/sentry-javascript/blob/3d63621714b31c1ea4c2ab2d90d5684a36805a43/packages/opentelemetry/src/utils/parseSpanDescription.ts#L22

Reminder list:
- Can we remove `extractQueryParamsFromUrl`?

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。