fix(browser): guard Browser.create against undefined launch options (#117) - #121
Open
linhongyu510 wants to merge 1 commit into
Open
Conversation
…eb-infra-dev#117) `Browser.create()` accepts `{ launchOrConnect: LaunchOptions }` and forwards `options.launchOrConnect` to the private `#init`. When a caller passes options at the top level (e.g. `Browser.create({ headless: false })`, as the bundled `example/browser.ts` did) or omits `launchOrConnect`, `#init` received `undefined` and `#processOptions` threw: TypeError: Cannot read properties of undefined (reading 'defaultViewport') at Browser.#processOptions (browser.ts:149) Fixes web-infra-dev#117. Changes: - src/browser/browser.ts: default `#init` options to `{}` and use `options.launchOrConnect ?? {}` in `create`, so a missing/undefined launch-options object no longer crashes and falls back to defaults. - example/browser.ts: pass `headless` under the documented `launchOrConnect` key so the example matches the public `create` signature and runs as-is. Signed-off-by: linhongyu510 <linhongyu510@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #117.
Browser.create()is typed ascreate(options: { launchOrConnect: LaunchOptions })and forwardsoptions.launchOrConnectto the private#init. Two paths lead to#init(undefined), after which#processOptionsdereferencesoptions.defaultViewportand throws:Browser.create({ headless: false }). This is exactly what the bundledpackages/browser/example/browser.tsdid, sonpx tsx example/browser.tscrashes out of the box (the repro in [Bug]: example/browser.ts fails due to accessing a property of undefined #117).launchOrConnectentirely.Changes
src/browser/browser.ts: default#initparam to{}and useoptions.launchOrConnect ?? {}insidecreate, so a missing/undefined launch-options object falls back to defaults instead of crashing.example/browser.ts: nestheadlessunder the documentedlaunchOrConnectkey so the example matches the publiccreatesignature and runs unmodified.Verification
#processOptionsreadingoptions.defaultViewporton anundefinedargument; both entry paths (createforwardingundefined, and#initreceiving no value) are now guarded.{ launchOrConnect: LaunchOptions }signature, matching README quick-start usage (Browser.create()with no args also remains valid).Note: #119 previously targeted this issue but was closed by its author without merging and only edited the example (also flipping
headlesstotrue); this PR fixes the underlying SDK crash as well so the defect cannot recur from other callers.