refactor: use helpers for JSON/text (#10154)

This commit is contained in:
Alex Rudenko 2023-05-11 10:21:49 +02:00 committed by GitHub
parent 317fa732f9
commit c815ba45a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 56 deletions

View File

@ -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<string> {
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`)
);
}

View File

@ -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<string> {
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<void> {

View File

@ -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';