Skip to content

Commit 3c4df06

Browse files
chore(browser): Export ipAddress helpers for use in other SDKs (#15079)
- Related to #15008 Export helpers to avoid duplications in sentry/react-native. - RN PR getsentry/sentry-react-native#4466
1 parent e8c0260 commit 3c4df06

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

packages/browser/src/client.ts

+9-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import type {
99
Scope,
1010
SeverityLevel,
1111
} from '@sentry/core';
12-
import { Client, applySdkMetadata, getSDKSource } from '@sentry/core';
12+
import {
13+
Client,
14+
addAutoIpAddressToSession,
15+
addAutoIpAddressToUser,
16+
applySdkMetadata,
17+
getSDKSource,
18+
} from '@sentry/core';
1319
import { eventFromException, eventFromMessage } from './eventbuilder';
1420
import { WINDOW } from './helpers';
1521
import type { BrowserTransportOptions } from './transports/types';
@@ -84,29 +90,8 @@ export class BrowserClient extends Client<BrowserClientOptions> {
8490
}
8591

8692
if (this._options.sendDefaultPii) {
87-
this.on('postprocessEvent', event => {
88-
if (event.user?.ip_address === undefined) {
89-
event.user = {
90-
...event.user,
91-
ip_address: '{{auto}}',
92-
};
93-
}
94-
});
95-
96-
this.on('beforeSendSession', session => {
97-
if ('aggregates' in session) {
98-
if (session.attrs?.['ip_address'] === undefined) {
99-
session.attrs = {
100-
...session.attrs,
101-
ip_address: '{{auto}}',
102-
};
103-
}
104-
} else {
105-
if (session.ipAddress === undefined) {
106-
session.ipAddress = '{{auto}}';
107-
}
108-
}
109-
});
93+
this.on('postprocessEvent', addAutoIpAddressToUser);
94+
this.on('beforeSendSession', addAutoIpAddressToSession);
11095
}
11196
}
11297

packages/core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export { hasTracingEnabled } from './utils/hasTracingEnabled';
7272
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
7373
export { handleCallbackErrors } from './utils/handleCallbackErrors';
7474
export { parameterize } from './utils/parameterize';
75+
export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress';
7576
export {
7677
spanToTraceHeader,
7778
spanToJSON,

packages/core/src/utils/ipAddress.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Session, SessionAggregates, User } from '../types-hoist';
2+
3+
// By default, we want to infer the IP address, unless this is explicitly set to `null`
4+
// We do this after all other processing is done
5+
// If `ip_address` is explicitly set to `null` or a value, we leave it as is
6+
7+
/**
8+
* @internal
9+
*/
10+
export function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void {
11+
if (objWithMaybeUser.user?.ip_address === undefined) {
12+
objWithMaybeUser.user = {
13+
...objWithMaybeUser.user,
14+
ip_address: '{{auto}}',
15+
};
16+
}
17+
}
18+
19+
/**
20+
* @internal
21+
*/
22+
export function addAutoIpAddressToSession(session: Session | SessionAggregates): void {
23+
if ('aggregates' in session) {
24+
if (session.attrs?.['ip_address'] === undefined) {
25+
session.attrs = {
26+
...session.attrs,
27+
ip_address: '{{auto}}',
28+
};
29+
}
30+
} else {
31+
if (session.ipAddress === undefined) {
32+
session.ipAddress = '{{auto}}';
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)