Commit f4bd636
fix: correct typeof comparisons in browser SDK (#1301)
## Summary
- Fix `typeof x !== undefined` (always true) to `typeof x !==
'undefined'` (correct) in 5 locations
- `isDocument()` and `isWindow()` now correctly return `false` in
service worker contexts
- `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 prevent this class of bug
## Context
These bugs were introduced in Sept-Oct 2024. Since `typeof` always
returns a string, comparing against the value `undefined` (not the
string `'undefined'`) made these checks always truthy. The bug was
masked because the previous ESLint config did not flag this pattern.
## Affected files
- `packages/sdk/browser/src/BrowserApi.ts` (3 locations)
- `packages/sdk/browser/src/platform/randomUuidV4.ts` (1 location)
- `packages/telemetry/browser-telemetry/src/randomUuidV4.ts` (1
location)
## Test plan
- [ ] `yarn workspaces foreach -p run lint` passes with 0 errors
- [ ] `yarn workspace @launchdarkly/js-client-sdk test` passes
- [ ] `yarn workspace @launchdarkly/browser-telemetry test` passes
Depends on #1299.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Fixes environment/crypto detection and UUID generation fallback logic
in browser-facing SDK code; while small, it can change behavior in
non-browser contexts (service workers/SSR) and affects ID generation
paths.
>
> **Overview**
> Corrects several invalid `typeof x !== undefined` checks to `typeof x
!== 'undefined'` in the browser SDK and browser telemetry UUID helper,
so non-browser contexts no longer incorrectly treat `document`,
`window`, or `crypto` as available.
>
> Re-enables ESLint `valid-typeof` to prevent regressions, and adds Jest
coverage to ensure `isDocument`/`isWindow` return `false` when globals
are missing and `randomUuidV4` properly falls back when
`crypto.randomUUID` is unavailable.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
074d3f3. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent eca74c5 commit f4bd636
6 files changed
Lines changed: 52 additions & 7 deletions
File tree
- packages
- sdk/browser
- __tests__
- platform
- src
- platform
- telemetry/browser-telemetry/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
Lines changed: 17 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
| 97 | + | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| |||
0 commit comments