Skip to content

chore: browserserver #643

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class BackendClient extends EventEmitter {
pair.fulfill(message.result);
}
};
this._transport.onclose = () => this._onCloseEvent.fire();
await this.initialize();
}

Expand Down
64 changes: 43 additions & 21 deletions src/reusedBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,32 @@ export class ReusedBrowser implements vscodeTypes.Disposable {
return {};
}

const args = [
config.cli,
'run-server',
`--path=/${createGuid()}`
];
const cwd = config.workspaceFolder;
const envProvider = () => ({
...this._envProvider(),
PW_CODEGEN_NO_INSPECTOR: '1',
PW_EXTENSION_MODE: '1',
});
let backend = await this._connectToBrowserServer();
if (!backend) {
const args = [
config.cli,
'run-server',
`--path=/${createGuid()}`
];
const cwd = config.workspaceFolder;
const envProvider = () => ({
...this._envProvider(),
PW_CODEGEN_NO_INSPECTOR: '1',
PW_EXTENSION_MODE: '1',
});

const errors: string[] = [];
const backendServer = new BackendServer(this._vscode, () => new Backend(this._vscode), {
args,
cwd,
envProvider,
errors,
});
backend = await backendServer.startAndConnect();
if (!backend)
return { errors };
}

const errors: string[] = [];
const backendServer = new BackendServer(this._vscode, () => new Backend(this._vscode), {
args,
cwd,
envProvider,
errors,
});
const backend = await backendServer.startAndConnect();
if (!backend)
return { errors };
backend.onClose(() => {
if (backend === this._backend) {
this._backend = undefined;
Expand Down Expand Up @@ -191,6 +195,24 @@ export class ReusedBrowser implements vscodeTypes.Disposable {
return {};
}

private async _connectToBrowserServer(): Promise<Backend | null> {
try {
const baseURL = new URL('http://localhost:14518');
Copy link
Preview

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Hardcoding the browser server URL and port limits flexibility; consider extracting the host/port into a config or environment variable.

Suggested change
const baseURL = new URL('http://localhost:14518');
const host = process.env.BROWSER_SERVER_HOST || 'localhost';
const port = process.env.BROWSER_SERVER_PORT || '14518';
const baseURL = new URL(`http://${host}:${port}`);

Copilot uses AI. Check for mistakes.

const response = await fetch(new URL('/json', baseURL));
if (!response.ok)
return null;
const json = await response.json();
if (typeof json.wsEndpointPath !== 'string')
return null;
const client = new Backend(this._vscode);
await client._connect(baseURL.toString());
Copy link
Preview

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned wsEndpointPath from the /json endpoint is never used; instead of connecting to baseURL directly, construct the WebSocket URL with json.wsEndpointPath (e.g. await client._connect(new URL(json.wsEndpointPath, baseURL).toString())).

Suggested change
await client._connect(baseURL.toString());
await client._connect(new URL(json.wsEndpointPath, baseURL).toString());

Copilot uses AI. Check for mistakes.

return client;
} catch (e) {
console.error('Failed to connect to browser server:', e);
Copy link
Preview

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using console.error directly may not integrate with the project's logging framework; consider using a centralized logger for consistency.

Copilot uses AI. Check for mistakes.

return null;
}
}

private _scheduleEdit(callback: () => Promise<void>) {
this._editOperations = this._editOperations.then(callback).catch(e => console.log(e));
}
Expand Down
Loading