init()

in src/core/browser-service.ts [38:64]


  init() {
    const proxy = createProxyServer();

    this.proxyServer.on('upgrade', async (req, socket, head) => {
      const browserServer = await chromium.launchServer({ headless: true });
      const wsEndpoint = browserServer.wsEndpoint();
      const { pathname, origin } = new URL(wsEndpoint);
      req.url = pathname;
      log(`New browser: ${wsEndpoint}`);
      proxy.ws(req, socket, head, { target: origin });
      const closeBrowser = async () => {
        const index = this.closeCallbacks.indexOf(closeBrowser);
        if (index >= 0) {
          this.closeCallbacks.splice(index, 1);
        }
        await browserServer.close();
        log(`Socket closed: ${wsEndpoint}`);
      };
      this.closeCallbacks.push(closeBrowser);
      socket.on('close', closeBrowser);
      socket.on('error', closeBrowser);
    });

    this.proxyServer.listen(this.options.port, () => {
      log(`Listening on port: ${this.options.port}`);
    });
  }