Skip to content

Commit 44f5bbb

Browse files
chore(browser): Export ipAddress helpers for use in other SDKs
1 parent fc48f83 commit 44f5bbb

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

packages/browser/src/client.ts

+3-29
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import type {
88
ParameterizedString,
99
Scope,
1010
SeverityLevel,
11-
User,
1211
} from '@sentry/core';
1312
import { Client, applySdkMetadata, getSDKSource } from '@sentry/core';
1413
import { eventFromException, eventFromMessage } from './eventbuilder';
1514
import { WINDOW } from './helpers';
1615
import type { BrowserTransportOptions } from './transports/types';
16+
import { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress';
1717

1818
/**
1919
* Configuration options for the Sentry Browser SDK.
@@ -84,22 +84,8 @@ export class BrowserClient extends Client<BrowserClientOptions> {
8484
});
8585
}
8686

87-
this.on('postprocessEvent', event => {
88-
addAutoIpAddressToUser(event);
89-
});
90-
91-
this.on('beforeSendSession', session => {
92-
if ('aggregates' in session) {
93-
if (session.attrs?.['ip_address'] === undefined) {
94-
session.attrs = {
95-
...session.attrs,
96-
ip_address: '{{auto}}',
97-
};
98-
}
99-
} else {
100-
addAutoIpAddressToUser(session);
101-
}
102-
});
87+
this.on('postprocessEvent', addAutoIpAddressToUser);
88+
this.on('beforeSendSession', addAutoIpAddressToSession);
10389
}
10490

10591
/**
@@ -134,15 +120,3 @@ export class BrowserClient extends Client<BrowserClientOptions> {
134120
return super._prepareEvent(event, hint, currentScope, isolationScope);
135121
}
136122
}
137-
138-
// By default, we want to infer the IP address, unless this is explicitly set to `null`
139-
// We do this after all other processing is done
140-
// If `ip_address` is explicitly set to `null` or a value, we leave it as is
141-
function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void {
142-
if (objWithMaybeUser.user?.ip_address === undefined) {
143-
objWithMaybeUser.user = {
144-
...objWithMaybeUser.user,
145-
ip_address: '{{auto}}',
146-
};
147-
}
148-
}

packages/browser/src/exports.ts

+1
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ export { linkedErrorsIntegration } from './integrations/linkederrors';
9999
export { browserApiErrorsIntegration } from './integrations/browserapierrors';
100100

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

0 commit comments

Comments
 (0)