| 309 | } |
| 310 | |
| 311 | async function connectRegionPostgres( |
| 312 | page: Page, |
| 313 | password: string, |
| 314 | ): Promise<Client> { |
| 315 | await page.getByTestId("connect-menu-button").click(); |
| 316 | await page.getByRole("button", { name: "External tools" }).click(); |
| 317 | |
| 318 | const hostAddress = (await page.getByLabel("Host").innerText()).trim(); |
| 319 | const port = (await page.getByLabel("Port").innerText()).trim(); |
| 320 | const database = (await page.getByLabel("Database").innerText()).trim(); |
| 321 | |
| 322 | assert(hostAddress && port && database); |
| 323 | |
| 324 | const url = new URL( |
| 325 | hostAddress.startsWith("http") ? hostAddress : `http://${hostAddress}`, |
| 326 | ); |
| 327 | const dns = new CacheableLookup({ |
| 328 | maxTtl: 0, // always re-lookup |
| 329 | errorTtl: 0, |
| 330 | }); |
| 331 | |
| 332 | for (let i = 0; i < 60; i++) { |
| 333 | try { |
| 334 | const entry = await dns.lookupAsync(url.hostname); |
| 335 | console.info("connecting to environmentd"); |
| 336 | console.info(` USER=${EMAIL}`); |
| 337 | console.info(` HOST=${entry.address}`); |
| 338 | console.info(` PORT=${port}`); |
| 339 | console.info(` DATABASE=${database}`); |
| 340 | const pgParams = { |
| 341 | user: EMAIL, |
| 342 | host: entry.address, |
| 343 | port: parseInt(port, 10), |
| 344 | database: database, |
| 345 | password, |
| 346 | ssl: { rejectUnauthorized: false }, |
| 347 | // 5 second connection timeout, because Frontegg authentication can be slow. |
| 348 | connectionTimeoutMillis: 50000, |
| 349 | }; |
| 350 | |
| 351 | const client = new Client(pgParams); |
| 352 | await client.connect(); |
| 353 | return client; |
| 354 | } catch (error) { |
| 355 | console.error(error, { EMAIL, database, password }); |
| 356 | await page.waitForTimeout(1000); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | throw new Error("unable to connect to region"); |
| 361 | } |