Skip to content

Commit 4818678

Browse files
authored
fix(js-client-sdk): better undefined handling (#1303)
This PR addresses SDK-2235 SDK-2236 changes: - changing `isDocument` to `isWindow` because that is more semanitcally correct (we are using `window` in the conditional block) - changing bare check to type check (`crypto` -> `typeof crypto !== 'undefined'`) so that the code doesn't throw due to `crypto` not existing (this should never happen unless running on some bad obscure browser) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk: small guard-condition fixes around `window` and `crypto` presence to avoid runtime errors in non-browser/limited environments. Behavior changes only when these globals are missing, where the previous code could throw or incorrectly skip listener registration. > > **Overview** > Fixes environment detection in the browser SDK by gating `addWindowEventListener` on `isWindow()` (matching the actual `window` usage) and updating the no-op comment accordingly. > > Hardens UUID v4 generation in both the SDK and browser-telemetry packages by replacing the unsafe `crypto` truthiness check with `typeof crypto !== 'undefined'` before calling `crypto.getRandomValues`, preventing ReferenceErrors when `crypto` is absent. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 3cee041. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent f4bd636 commit 4818678

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

packages/sdk/browser/src/BrowserApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export function addWindowEventListener(
5050
listener: (this: Document, ev: Event) => any,
5151
options?: boolean | AddEventListenerOptions,
5252
): () => void {
53-
if (isDocument()) {
53+
if (isWindow()) {
5454
window.addEventListener(type, listener, options);
5555
return () => {
5656
window.removeEventListener(type, listener, options);
5757
};
5858
}
59-
// No document, so no need to unregister anything.
59+
// No window, so no need to unregister anything.
6060
return () => {};
6161
}
6262

packages/sdk/browser/src/platform/randomUuidV4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const nodes = {
3636
};
3737

3838
function getRandom128bit(): number[] {
39-
if (crypto && crypto.getRandomValues) {
39+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
4040
const typedArray = new Uint8Array(16);
4141
crypto.getRandomValues(typedArray);
4242
return [...typedArray.values()];

packages/telemetry/browser-telemetry/src/randomUuidV4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const nodes = {
3939
};
4040

4141
function getRandom128bit(): number[] {
42-
if (crypto && crypto.getRandomValues) {
42+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
4343
const typedArray = new Uint8Array(16);
4444
crypto.getRandomValues(typedArray);
4545
return [...typedArray.values()];

0 commit comments

Comments
 (0)