Skip to content

Commit 75ae702

Browse files
refactor(browser): gettabbyid returns undefined when tab not found despite non-nullable return type
`Array.prototype.find()` returns `undefined` when no element matches, but the function declares its return type as `Promise<TargetInfo>` (non-nullable). The `as TargetInfo` cast silences the compiler but doesn't prevent `undefined` at runtime. Any caller accessing properties on the result (e.g., `tab.url`, `tab.webSocketDebuggerUrl`) will get a TypeError: "Cannot read properties of undefined". This is especially likely during race conditions where a tab closes between `getTabs` and the caller using the result. Affected files: utils.ts Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com>
1 parent 526d487 commit 75ae702

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • src/browser/provider/built-in/dedicated/chrome/cdp-client

src/browser/provider/built-in/dedicated/chrome/cdp-client/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ export async function getTabs (port: number): Promise<TargetInfo[]> {
1616

1717
export async function getTabById (port: number, id: string): Promise<TargetInfo> {
1818
const tabs = await getTabs(port);
19+
const tab = tabs.find(tab => tab.id === id);
1920

20-
return tabs.find(tab => tab.id === id) as TargetInfo;
21+
if (!tab)
22+
throw new Error(`Tab with id "${id}" not found`);
23+
24+
return tab;
2125
}
2226

2327
export async function getFirstTab (port: number): Promise<TargetInfo> {

0 commit comments

Comments
 (0)