Skip to content

fix(browser): guard Browser.create against undefined launch options (#117) - #121

Open
linhongyu510 wants to merge 1 commit into
web-infra-dev:mainfrom
linhongyu510:fix/117-browser-create-undefined-options
Open

fix(browser): guard Browser.create against undefined launch options (#117)#121
linhongyu510 wants to merge 1 commit into
web-infra-dev:mainfrom
linhongyu510:fix/117-browser-create-undefined-options

Conversation

@linhongyu510

Copy link
Copy Markdown

Summary

Fixes #117.

Browser.create() is typed as create(options: { launchOrConnect: LaunchOptions }) and forwards options.launchOrConnect to the private #init. Two paths lead to #init(undefined), after which #processOptions dereferences options.defaultViewport and throws:

TypeError: Cannot read properties of undefined (reading 'defaultViewport')
    at Browser.#processOptions (packages/browser/src/browser/browser.ts:149)
    at Browser.#init (.../browser.ts:75)
    at Browser.create (.../browser.ts:27)
  1. A caller passes launch options at the top level, e.g. Browser.create({ headless: false }). This is exactly what the bundled packages/browser/example/browser.ts did, so npx tsx example/browser.ts crashes out of the box (the repro in [Bug]: example/browser.ts fails due to accessing a property of undefined #117).
  2. A caller omits launchOrConnect entirely.

Changes

  • src/browser/browser.ts: default #init param to {} and use options.launchOrConnect ?? {} inside create, so a missing/undefined launch-options object falls back to defaults instead of crashing.
  • example/browser.ts: nest headless under the documented launchOrConnect key so the example matches the public create signature and runs unmodified.

Verification

  • Traced the crash to #processOptions reading options.defaultViewport on an undefined argument; both entry paths (create forwarding undefined, and #init receiving no value) are now guarded.
  • The example now conforms to the { 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 headless to true); this PR fixes the underlying SDK crash as well so the defect cannot recur from other callers.

…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>
@linhongyu510
linhongyu510 requested a review from skychx as a code owner August 28, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: example/browser.ts fails due to accessing a property of undefined

1 participant