Skip to content

Commit 10ab2b8

Browse files
joker23claude
andcommitted
fix: correct typeof comparisons in browser SDK
Fix typeof comparisons that compared against the value `undefined` instead of the string `'undefined'`. Since typeof always returns a string, comparing to the value `undefined` made these checks always truthy. Affected functions: - isDocument() and isWindow() in BrowserApi.ts now correctly return false in service worker contexts where document/window are unavailable - getCrypto() now correctly throws when crypto API is unavailable - randomUuidV4() now correctly falls back when crypto.randomUUID is missing Re-enables the valid-typeof ESLint rule to catch this class of bug. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent be5cd04 commit 10ab2b8

4 files changed

Lines changed: 6 additions & 6 deletions

File tree

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
'eqeqeq': ['error', 'always', { null: 'ignore' }],
2828
'prefer-const': ['error', { ignoreReadBeforeAssign: true }],
2929
'no-var': 'error',
30-
'valid-typeof': 'off',
30+
'valid-typeof': 'error',
3131
'no-restricted-syntax': [
3232
'error',
3333
{ selector: 'ForInStatement', message: 'Use Object.{keys,values,entries} instead.' },

packages/sdk/browser/src/BrowserApi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66

77
export function isDocument() {
8-
return typeof document !== undefined;
8+
return typeof document !== 'undefined';
99
}
1010

1111
export function isWindow() {
12-
return typeof window !== undefined;
12+
return typeof window !== 'undefined';
1313
}
1414

1515
/**
@@ -91,7 +91,7 @@ export function getLocationHash(): string {
9191
}
9292

9393
export function getCrypto(): Crypto {
94-
if (typeof crypto !== undefined) {
94+
if (typeof crypto !== 'undefined') {
9595
return crypto;
9696
}
9797
// This would indicate running in an environment that doesn't have window.crypto or self.crypto.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function fallbackUuidV4(): string {
9191
}
9292

9393
export default function randomUuidV4(): string {
94-
if (typeof crypto !== undefined && typeof crypto.randomUUID === 'function') {
94+
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
9595
return crypto.randomUUID();
9696
}
9797

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function fallbackUuidV4(): string {
9494
}
9595

9696
export default function randomUuidV4(): string {
97-
if (typeof crypto !== undefined && typeof crypto.randomUUID === 'function') {
97+
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
9898
return crypto.randomUUID();
9999
}
100100

0 commit comments

Comments
 (0)