Skip to content

Commit

Permalink
Reverted async/await syntax to enable backward-compatibility and supp…
Browse files Browse the repository at this point in the history
…ort of node.js 6.x (closes #12)
  • Loading branch information
dtolstyi committed Nov 16, 2018
1 parent 6f15570 commit 5c8b5e1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 81 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ os:
- linux
- osx
node_js:
- "6"
- "7"
- "8"
- "9"
Expand Down
37 changes: 15 additions & 22 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function createTempFile() {
return new Promise((resolve, reject) => {
tmp.file((error, path) => {
if (error) {
console.log('An error occured while trying to create temporary file', error);
console.error('An error occured while trying to create temporary file', error);
reject(error);
} else {
resolve(path);
Expand All @@ -22,17 +22,19 @@ function createTempFile() {
});
}

async function downloadChromiumRevision(revision) {
const tmpPath = await createTempFile();
function downloadChromiumRevision(revision) {
return createTempFile()
.then(tmpFilePath => {
debug('Downloading Chromium archive from Google CDN');
const url = utils.getDownloadUrl(revision);

debug('Downloading Chromium archive from Google CDN');
const url = utils.getDownloadUrl(revision);

return _downloadFile(url, tmpPath);
return _downloadFile(url, tmpFilePath);
});
}

function _downloadFile(url, destPath) {
return new Promise((resolve, reject) => {
console.info('Downloading Chromium archive from CDN (this process might take a while)');
got.stream(url)
.on('error', error => {
console.error('An error occurred while trying to download file', error.message);
Expand Down Expand Up @@ -65,21 +67,12 @@ function unzipArchive(archivePath, outputFolder) {
});
}

async function install() {
try {
console.info('Step 1. Retrieving Chromium latest revision number');
const revision = await utils.getLatestRevisionNumber();

console.info('Step 2. Downloading Chromium (this might take a while)');
const tmpPath = await downloadChromiumRevision(revision);

console.info('Step 3. Setting up Chromium binaries');
await unzipArchive(tmpPath, config.BIN_OUT_PATH);

console.info('Process is successfully finished');
} catch (err) {
console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', err);
}
function install() {
return utils.getLatestRevisionNumber()
.then(downloadChromiumRevision)
.then(downloadPath => unzipArchive(downloadPath, config.BIN_OUT_PATH))
.then(() => console.info('Process is successfully finished'))
.catch(err => console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', err));
}

module.exports = install();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
{
"files": "test-install.js",
"rules": {
"no-await-in-loop": "off"
"no-await-in-loop": "off",
"ava/prefer-async-await": "off"
}
}
]
Expand Down
61 changes: 6 additions & 55 deletions test-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import test from 'ava';

const fs = require('fs');
const rimraf = require('rimraf');
const got = require('got');
const debug = require('debug')('node-chromium');

const utils = require('./utils');
const config = require('./config');

const install = async () => {
await require('./install');
};

test.before(t => {
// Deleting output folder
const outPath = config.BIN_OUT_PATH;
Expand All @@ -34,54 +29,10 @@ test('Before Install Process', t => {
t.false(fs.existsSync(binPath), `Chromium binary is found in: [${binPath}]`);
});

test('Chromium Install', async t => {
await install();

const binPath = utils.getOsChromiumBinPath();
const isExists = fs.existsSync(binPath);
t.true(isExists, `Chromium binary is not found in: [${binPath}]`);
});

test.serial('Different OS support', async t => {
const supportedPlatforms = ['darwin', 'linux', 'win32'];
const notSupportedPlatforms = ['aix', 'freebsd', 'openbsd', 'sunos'];

const originalPlatform = process.platform;

for (const platform of supportedPlatforms) {
mockPlatform(platform);

const revision = await utils.getLatestRevisionNumber();

const url = utils.getDownloadUrl(revision);
t.true(await isUrlAccessible(url));
}

for (const platform of notSupportedPlatforms) {
mockPlatform(platform);

t.throws(() => {
utils.getDownloadUrl();
}, 'Unsupported platform');
}

mockPlatform(originalPlatform);

t.pass();
});

async function isUrlAccessible(url) {
try {
const response = await got(url, {method: 'HEAD'});
return /4\d\d/.test(response.statusCode) === false;
} catch (err) {
console.warn(`An error [${err.message}] occurred while trying to check URL [${url}] accessibility`);
return false;
}
}

function mockPlatform(newPlatformValue) {
Object.defineProperty(process, 'platform', {
value: newPlatformValue
test('Chromium Install', t => {
return require('./install').then(() => {
const binPath = utils.getOsChromiumBinPath();
const isExists = fs.existsSync(binPath);
t.true(isExists, `Chromium binary is not found in: [${binPath}]`);
});
}
});
15 changes: 12 additions & 3 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,17 @@ module.exports = {
*
* @returns {Promise<String>}
*/
async getLatestRevisionNumber() {
const url = this.getOsCdnUrl() + '%2FLAST_CHANGE?alt=media';
return (await got(url)).body;
getLatestRevisionNumber() {
return new Promise((resolve, reject) => {
const url = this.getOsCdnUrl() + '%2FLAST_CHANGE?alt=media';
got(url)
.then(response => {
resolve(response.body);
})
.catch(err => {
console.error('An error occured while trying to retrieve latest revision number', err);
reject(err);
});
});
}
};

0 comments on commit 5c8b5e1

Please sign in to comment.