From c815ba45a38a8f22ee0ca38cea92f5e13b93cf25 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 11 May 2023 10:21:49 +0200 Subject: [PATCH] refactor: use helpers for JSON/text (#10154) --- .../browsers/src/browser-data/chromedriver.ts | 31 ++------------- packages/browsers/src/browser-data/firefox.ts | 39 ++++++------------- packages/browsers/test/src/versions.ts | 2 +- 3 files changed, 16 insertions(+), 56 deletions(-) diff --git a/packages/browsers/src/browser-data/chromedriver.ts b/packages/browsers/src/browser-data/chromedriver.ts index 39894d2e866..6631e870a36 100644 --- a/packages/browsers/src/browser-data/chromedriver.ts +++ b/packages/browsers/src/browser-data/chromedriver.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {httpRequest} from '../httpUtil.js'; +import {getText} from '../httpUtil.js'; import {BrowserPlatform} from './types.js'; @@ -64,30 +64,7 @@ export function relativeExecutablePath( export async function resolveBuildId( _channel: 'latest' = 'latest' ): Promise { - return new Promise((resolve, reject) => { - const request = httpRequest( - new URL(`https://chromedriver.storage.googleapis.com/LATEST_RELEASE`), - 'GET', - response => { - let data = ''; - if (response.statusCode && response.statusCode >= 400) { - return reject(new Error(`Got status code ${response.statusCode}`)); - } - response.on('data', chunk => { - data += chunk; - }); - response.on('end', () => { - try { - return resolve(String(data)); - } catch { - return reject(new Error('Chrome version not found')); - } - }); - }, - false - ); - request.on('error', err => { - reject(err); - }); - }); + return await getText( + new URL(`https://chromedriver.storage.googleapis.com/LATEST_RELEASE`) + ); } diff --git a/packages/browsers/src/browser-data/firefox.ts b/packages/browsers/src/browser-data/firefox.ts index 6b9fb080f57..ea0ffe234a9 100644 --- a/packages/browsers/src/browser-data/firefox.ts +++ b/packages/browsers/src/browser-data/firefox.ts @@ -17,7 +17,7 @@ import fs from 'fs'; import path from 'path'; -import {httpRequest} from '../httpUtil.js'; +import {getJSON} from '../httpUtil.js'; import {BrowserPlatform, ProfileOptions} from './types.js'; @@ -68,33 +68,16 @@ export function relativeExecutablePath( export async function resolveBuildId( channel: 'FIREFOX_NIGHTLY' = 'FIREFOX_NIGHTLY' ): Promise { - return new Promise((resolve, reject) => { - const request = httpRequest( - new URL('https://product-details.mozilla.org/1.0/firefox_versions.json'), - 'GET', - response => { - let data = ''; - if (response.statusCode && response.statusCode >= 400) { - return reject(new Error(`Got status code ${response.statusCode}`)); - } - response.on('data', chunk => { - data += chunk; - }); - response.on('end', () => { - try { - const versions = JSON.parse(data); - return resolve(versions[channel]); - } catch { - return reject(new Error('Firefox version not found')); - } - }); - }, - false - ); - request.on('error', err => { - reject(err); - }); - }); + const versions = (await getJSON( + new URL('https://product-details.mozilla.org/1.0/firefox_versions.json') + )) as { + [channel: string]: string; + }; + const version = versions[channel]; + if (!version) { + throw new Error(`Channel ${channel} is not found.`); + } + return version; } export async function createProfile(options: ProfileOptions): Promise { diff --git a/packages/browsers/test/src/versions.ts b/packages/browsers/test/src/versions.ts index 606827fe3c3..2707569dda5 100644 --- a/packages/browsers/test/src/versions.ts +++ b/packages/browsers/test/src/versions.ts @@ -18,5 +18,5 @@ export const testChromeBuildId = '113.0.5672.0'; export const testChromiumBuildId = '1083080'; // TODO: We can add a Cron job to auto-update on change. // Firefox keeps only `latest` version of Nightly builds. -export const testFirefoxBuildId = '114.0a1'; +export const testFirefoxBuildId = '115.0a1'; export const testChromeDriverBuildId = '112.0.5615.49';