From 5c8b5e154677c62cc130f9c10e1234895934e0f4 Mon Sep 17 00:00:00 2001 From: dtolstyi Date: Fri, 16 Nov 2018 14:29:23 +0100 Subject: [PATCH] Reverted async/await syntax to enable backward-compatibility and support of node.js 6.x (closes #12) --- .travis.yml | 1 + install.js | 37 ++++++++++++------------------ package.json | 3 ++- test-install.js | 61 +++++-------------------------------------------- utils.js | 15 +++++++++--- 5 files changed, 36 insertions(+), 81 deletions(-) diff --git a/.travis.yml b/.travis.yml index d2e0d57..030d901 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ os: - linux - osx node_js: + - "6" - "7" - "8" - "9" diff --git a/install.js b/install.js index c91d76e..3de9c85 100644 --- a/install.js +++ b/install.js @@ -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); @@ -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); @@ -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(); diff --git a/package.json b/package.json index 1f7be8d..62af1ad 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ { "files": "test-install.js", "rules": { - "no-await-in-loop": "off" + "no-await-in-loop": "off", + "ava/prefer-async-await": "off" } } ] diff --git a/test-install.js b/test-install.js index ce9fca0..973cd2c 100644 --- a/test-install.js +++ b/test-install.js @@ -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; @@ -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}]`); }); -} +}); diff --git a/utils.js b/utils.js index ed034c7..f810ac1 100644 --- a/utils.js +++ b/utils.js @@ -95,8 +95,17 @@ module.exports = { * * @returns {Promise} */ - 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); + }); + }); } };