Skip to content

Commit

Permalink
Re-organize packages
Browse files Browse the repository at this point in the history
  • Loading branch information
zabil committed May 14, 2024
1 parent b69ed52 commit f983588
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 95 deletions.
4 changes: 3 additions & 1 deletion lib/browser.js → lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const { promisify } = require('util');
const { helper, assert } = require('./helper');
const { createInterface } = require('readline');
const { parse } = require('url');
const BrowserMetadata = require('./browser/metadata');

const supportedPlatforms = ['mac-arm64', 'mac-x64', 'linux', 'win32', 'win64'];
const revisionInfo = new BrowserMetadata().revisionInfo();

const readdirAsync = promisify(readdir.bind(fs));

Expand Down Expand Up @@ -111,7 +114,6 @@ class Browser {
};
}

const revisionInfo = metadata.taiko.browser.revision;
const missingText = !revisionInfo.local
? 'Chromium revision is not downloaded. Provide TAIKO_BROWSER_PATH or Install taiko again to download bundled chromium.'
: null;
Expand Down
102 changes: 10 additions & 92 deletions lib/browserFetcher.js → lib/browser/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ const path = require('path');
const extract = require('extract-zip');
const util = require('util');
const URL = require('url');
const { helper, assert } = require('./helper');
const { helper, assert } = require('../helper');
const ProxyAgent = require('https-proxy-agent');
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
const supportedPlatforms = ['mac-arm64', 'mac-x64', 'linux', 'win32', 'win64'];

const readdirAsync = util.promisify(fs.readdir.bind(fs));
const mkdirAsync = util.promisify(fs.mkdir.bind(fs));
const unlinkAsync = util.promisify(fs.unlink.bind(fs));
const chmodAsync = util.promisify(fs.chmod.bind(fs));
const BrowserMetadata = require('./metadata');

function existsAsync(filePath) {
let fulfill = null;
Expand All @@ -45,8 +45,9 @@ class BrowserFetcher {
/**
* @param {!BrowserFetcher.Options=} options
*/
constructor(browser) {
constructor() {
this._downloadsFolder = path.join(helper.projectRoot(), '.local-chromium');
this.browserMetadata = new BrowserMetadata();
const platform = os.platform();
if (platform === 'darwin') {
this._platform = os.arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
Expand All @@ -57,25 +58,6 @@ class BrowserFetcher {
}
assert(this._platform, 'Unsupported platform: ' + os.platform());
assert(supportedPlatforms.includes(this._platform), 'Unsupported platform: ' + this._platform);
const download = browser.downloads.chrome.find(
(download) => download.platform === this._platform,
);
this.downloadEndpoint = download.url;
this.revision = browser.revision;
}

archiveName() {
if (this._platform === 'linux') {
return 'chrome-linux';
}
if (this._platform.includes('mac')) {
return 'chrome-mac';
}
if (this._platform === 'win32' || this.platform === 'win64') {
// Windows archive name changed at r591479.
return parseInt(this.revision, 10) > 591479 ? 'chrome-win' : 'chrome-win32';
}
return null;
}

/**
Expand All @@ -92,7 +74,7 @@ class BrowserFetcher {
canDownload() {
let resolve;
const promise = new Promise((x) => (resolve = x));
const request = httpRequest(this.downloadEndpoint, 'HEAD', (response) => {
const request = httpRequest(this.browserMetadata.downloadURL(), 'HEAD', (response) => {
resolve(response.statusCode === 200);
});
request.on('error', (error) => {
Expand All @@ -111,40 +93,26 @@ class BrowserFetcher {
const zipPath = path.join(this._downloadsFolder, `download-${this._platform}-${revision}.zip`);
const folderPath = this._getFolderPath(revision);
if (await existsAsync(folderPath)) {
return this.revisionInfo(revision);
return this.browserMetadata.revisionInfo();
}
if (!(await existsAsync(this._downloadsFolder))) {
await mkdirAsync(this._downloadsFolder);
}
try {
await downloadFile(this.downloadEndpoint, zipPath, progressCallback);
await downloadFile(this.browserMetadata.downloadURL(), zipPath, progressCallback);
await extractZip(zipPath, folderPath);
} finally {
if (await existsAsync(zipPath)) {
await unlinkAsync(zipPath);
}
}
const revisionInfo = this.revisionInfo(revision);
const revisionInfo = this.browserMetadata.revisionInfo();
if (revisionInfo) {
await chmodAsync(revisionInfo.executablePath, 0o755);
}
return revisionInfo;
}

/**
* @return {!Promise<!Array<string>>}
*/
async localRevisions() {
if (!(await existsAsync(this._downloadsFolder))) {
return [];
}
const fileNames = await readdirAsync(this._downloadsFolder);
return fileNames
.map((fileName) => parseFolderPath(fileName))
.filter((entry) => entry && entry.platform === this._platform)
.map((entry) => entry.revision);
}

/**
* @param {string} revision
* @return {!Promise}
Expand All @@ -158,67 +126,17 @@ class BrowserFetcher {
fs.removeSync(folderPath);
}

/**
* @param {string} revision
* @return {!BrowserFetcher.RevisionInfo}
*/
revisionInfo() {
const folderPath = this._getFolderPath(this.revision);
let executablePath = '';
if (this._platform.includes('mac')) {
executablePath = path.join(
folderPath,
this.archiveName(),
'Chromium.app',
'Contents',
'MacOS',
'Chromium',
);
} else if (this._platform === 'linux') {
executablePath = path.join(folderPath, this.archiveName(), 'chrome');
} else if (this._platform === 'win32' || this._platform === 'win64') {
executablePath = path.join(folderPath, this.archiveName(), 'chrome.exe');
} else {
throw 'Unsupported platform: ' + this._platform;
}
const local = fs.existsSync(folderPath);
return {
revision: this.revision,
executablePath,
folderPath,
local,
url: this.downloadEndpoint,
};
}

/**
* @param {string} revision
* @return {string}
*/
_getFolderPath() {
return path.join(this._downloadsFolder, this._platform + '-' + this.revision);
_getFolderPath(revision) {
return path.join(this._downloadsFolder, this._platform + '-' + revision);
}
}

module.exports = BrowserFetcher;

/**
* @param {string} folderPath
* @return {?{platform: string, revision: string}}
*/
function parseFolderPath(folderPath) {
const name = path.basename(folderPath);
const splits = name.split('-');
if (splits.length !== 2) {
return null;
}
const [platform, revision] = splits;
if (!supportedPlatforms.includes(platform)) {
return null;
}
return { platform, revision };
}

/**
* @param {string} url
* @param {string} destinationPath
Expand Down
File renamed without changes.
156 changes: 156 additions & 0 deletions lib/browser/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Copyright 2018 Thoughtworks Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This module is imported from Puppeteer(https://github.com/GoogleChrome/puppeteer)
* Few modifications are done on the file.
*/

const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const util = require('util');
const { helper, assert } = require('../helper');
const supportedPlatforms = ['mac-arm64', 'mac-x64', 'linux', 'win32', 'win64'];

const readdirAsync = util.promisify(fs.readdir.bind(fs));
const browser = require('../../package.json').taiko.browser;

function existsAsync(filePath) {
let fulfill = null;
const promise = new Promise((x) => (fulfill = x));
fs.access(filePath, (err) => fulfill(!err));
return promise;
}

class BrowserMetadata {
/**
* @param {!BrowserFetcher.Options=} options
*/
constructor() {
this._downloadsFolder = path.join(helper.projectRoot(), '.local-chromium');
const platform = os.platform();
if (platform === 'darwin') {
this._platform = os.arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
} else if (platform === 'linux') {
this._platform = 'linux';
} else if (platform === 'win32') {
this._platform = os.arch() === 'x64' ? 'win64' : 'win32';
}
assert(this._platform, 'Unsupported platform: ' + os.platform());
assert(supportedPlatforms.includes(this._platform), 'Unsupported platform: ' + this._platform);
const download = browser.downloads.chrome.find(
(download) => download.platform === this._platform,
);
this.downloadEndpoint = download.url;
this.revision = browser.revision;
}

downloadURL() {
return this.downloadEndpoint;
}

revision() {
return this.revision;
}

archiveName() {
if (this._platform === 'linux') {
return 'chrome-linux';
}
if (this._platform.includes('mac')) {
return 'chrome-mac';
}
if (this._platform === 'win32' || this.platform === 'win64') {
// Windows archive name changed at r591479.
return parseInt(this.revision, 10) > 591479 ? 'chrome-win' : 'chrome-win32';
}
return null;
}

/**
* @return {!Promise<!Array<string>>}
*/
async localRevisions() {
if (!(await existsAsync(this._downloadsFolder))) {
return [];
}
const fileNames = await readdirAsync(this._downloadsFolder);
return fileNames
.map((fileName) => parseFolderPath(fileName))
.filter((entry) => entry && entry.platform === this._platform)
.map((entry) => entry.revision);
}

/**
* @param {string} revision
* @return {!BrowserFetcher.RevisionInfo}
*/
revisionInfo() {
const folderPath = this._getFolderPath(this.revision);
let executablePath = '';
if (this._platform.includes('mac')) {
executablePath = path.join(
folderPath,
this.archiveName(),
'Chromium.app',
'Contents',
'MacOS',
'Chromium',
);
} else if (this._platform === 'linux') {
executablePath = path.join(folderPath, this.archiveName(), 'chrome');
} else if (this._platform === 'win32' || this._platform === 'win64') {
executablePath = path.join(folderPath, this.archiveName(), 'chrome.exe');
} else {
throw 'Unsupported platform: ' + this._platform;
}
const local = fs.existsSync(folderPath);
return {
revision: this.revision,
executablePath,
folderPath,
local,
url: this.downloadEndpoint,
};
}

/**
* @param {string} revision
* @return {string}
*/
_getFolderPath() {
return path.join(this._downloadsFolder, this._platform + '-' + this.revision);
}
}

module.exports = BrowserMetadata;

/**
* @param {string} folderPath
* @return {?{platform: string, revision: string}}
*/
function parseFolderPath(folderPath) {
const name = path.basename(folderPath);
const splits = name.split('-');
if (splits.length !== 2) {
return null;
}
const [platform, revision] = splits;
if (!supportedPlatforms.includes(platform)) {
return null;
}
return { platform, revision };
}
5 changes: 3 additions & 2 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
* This module is imported from Puppeteer(https://github.com/GoogleChrome/puppeteer)
* Few modifications are done on the file.
*/
const BrowserFetcher = require('./browserFetcher');
const BrowserFetcher = require('./browser/fetcher');
const BrowserMetadata = require('./browser/metadata');
const browser = require('../package.json').taiko.browser;
const browserFetcher = new BrowserFetcher(browser);
const revisionInfo = browserFetcher.revisionInfo();
const revisionInfo = new BrowserMetadata().revisionInfo();

let progressBar = null;
let lastDownloadedBytes = 0;
Expand Down

0 comments on commit f983588

Please sign in to comment.