-
Notifications
You must be signed in to change notification settings - Fork 37
fix: correct typeof comparisons in browser SDK #1301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { isDocument, isWindow } from '../src/BrowserApi'; | ||
|
|
||
| it('isDocument returns true when document is defined', () => { | ||
| expect(isDocument()).toBe(true); | ||
| }); | ||
|
|
||
| it('isDocument returns false when document is not defined', () => { | ||
| const original = Object.getOwnPropertyDescriptor(globalThis, 'document'); | ||
| Object.defineProperty(globalThis, 'document', { value: undefined, configurable: true }); | ||
| try { | ||
| expect(isDocument()).toBe(false); | ||
| } finally { | ||
| Object.defineProperty(globalThis, 'document', original!); | ||
| } | ||
| }); | ||
|
|
||
| it('isWindow returns true when window is defined', () => { | ||
| expect(isWindow()).toBe(true); | ||
| }); | ||
|
|
||
| it('isWindow returns false when window is not defined', () => { | ||
| const original = Object.getOwnPropertyDescriptor(globalThis, 'window'); | ||
| Object.defineProperty(globalThis, 'window', { value: undefined, configurable: true }); | ||
| try { | ||
| expect(isWindow()).toBe(false); | ||
| } finally { | ||
| Object.defineProperty(globalThis, 'window', original!); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,7 @@ export function fallbackUuidV4(): string { | |
| } | ||
|
|
||
| export default function randomUuidV4(): string { | ||
| if (typeof crypto !== undefined && typeof crypto.randomUUID === 'function') { | ||
| if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 In both Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. existing issue will address in another PR |
||
| return crypto.randomUUID(); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡
addWindowEventListenerguards onisDocument()instead ofisWindow(), now exposed by the typeof fixThe
addWindowEventListenerfunction atpackages/sdk/browser/src/BrowserApi.ts:53checksisDocument()instead ofisWindow()before accessingwindow. Before this PR, this didn't matter because bothisDocument()andisWindow()always returnedtrue(due to the brokentypeof x !== undefinedcomparison). Now that the typeof checks are fixed, these functions correctly distinguish betweendocumentandwindowavailability. In an environment wherewindowis available butdocumentis not,addWindowEventListenerwould incorrectly return a no-op instead of registering the event. This function is used byBrowserStateDetector.ts:21(forpagehideevents) andLocationWatcher.ts:46(forpopstateevents).(Refers to line 53)
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will address separately (this is an existing bug)