Repository: puppeteer/puppeteer
File: packages/puppeteer-core/src/common/BrowserConnector.ts (TypeScript)

Code before:
      await import('@puppeteer/browsers');
    const platform = detectBrowserPlatform();
    if (!platform) {
      throw new Error('Could not detect required browser platform');
    }
    const {convertPuppeteerChannelToBrowsersChannel} = await import(
      '../node/LaunchOptions.js'
    );
    const {join} = await import('node:path');
    const userDataDir = resolveDefaultUserDataDir(
      Browser.CHROME,
      platform,
      convertPuppeteerChannelToBrowsersChannel(options.channel),
    );
    const portPath = join(userDataDir, 'DevToolsActivePort');
    try {
      const port = parseInt(
        await environment.value.fs.promises.readFile(portPath, 'ascii'),
        10,
      );
      if (port <= 0 || port > 65535) {
        throw new Error(`Invalid port ${port} found`);
      }
      const browserWSEndpoint = `ws://localhost:${port}`;
      const WebSocketClass = await getWebSocketTransportClass();
      const connectionTransport: ConnectionTransport =
        await WebSocketClass.create(browserWSEndpoint, headers);
      return {
        connectionTransport: connectionTransport,
        endpointUrl: browserWSEndpoint,
      };
    } catch (error) {
      throw new Error(
        `Could not find DevToolsActivePort for ${options.channel} at ${portPath}`,
        {
          cause: error,
        },
      );
    }
  }
  throw new Error('Invalid connection options');
}

async function getWSEndpoint(browserURL: string): Promise<string> {
  const endpointURL = new URL('/json/version', browserURL);

  try {
    const result = await globalThis.fetch(endpointURL.toString(), {
      method: 'GET',
    });
    if (!result.ok) {

Review comment (suggestion):
```suggestion
      const connectionTransport =
```

Code after:
      await import('@puppeteer/browsers');
    const platform = detectBrowserPlatform();
    if (!platform) {
      throw new Error('Could not detect required browser platform');
    }
    const {convertPuppeteerChannelToBrowsersChannel} = await import(
      '../node/LaunchOptions.js'
    );
    const {join} = await import('node:path');
    const userDataDir = resolveDefaultUserDataDir(
      Browser.CHROME,
      platform,
      convertPuppeteerChannelToBrowsersChannel(options.channel),
    );
    const portPath = join(userDataDir, 'DevToolsActivePort');
    try {
      const portRawValue = await environment.value.fs.promises.readFile(
        portPath,
        'ascii',
      );
      const port = parseInt(portRawValue, 10);
      if (isNaN(port) || port <= 0 || port > 65535) {
        throw new Error(`Invalid port '${portRawValue}' found`);
      }
      const browserWSEndpoint = `ws://localhost:${port}`;
      const WebSocketClass = await getWebSocketTransportClass();
      const connectionTransport = await WebSocketClass.create(
        browserWSEndpoint,
        headers,
      );
      return {
        connectionTransport: connectionTransport,
        endpointUrl: browserWSEndpoint,
      };
    } catch (error) {
      throw new Error(
        `Could not find DevToolsActivePort for ${options.channel} at ${portPath}`,
        {
          cause: error,
        },
      );
    }
  }
  throw new Error('Invalid connection options');
}

async function getWSEndpoint(browserURL: string): Promise<string> {
  const endpointURL = new URL('/json/version', browserURL);

  try {
    const result = await globalThis.fetch(endpointURL.toString(), {