Skip to content

Commit 5c8b5e1

Browse files
author
dtolstyi
committed
Reverted async/await syntax to enable backward-compatibility and support of node.js 6.x (closes #12)
1 parent 6f15570 commit 5c8b5e1

File tree

5 files changed

+36
-81
lines changed

5 files changed

+36
-81
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ os:
44
- linux
55
- osx
66
node_js:
7+
- "6"
78
- "7"
89
- "8"
910
- "9"

install.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function createTempFile() {
1313
return new Promise((resolve, reject) => {
1414
tmp.file((error, path) => {
1515
if (error) {
16-
console.log('An error occured while trying to create temporary file', error);
16+
console.error('An error occured while trying to create temporary file', error);
1717
reject(error);
1818
} else {
1919
resolve(path);
@@ -22,17 +22,19 @@ function createTempFile() {
2222
});
2323
}
2424

25-
async function downloadChromiumRevision(revision) {
26-
const tmpPath = await createTempFile();
25+
function downloadChromiumRevision(revision) {
26+
return createTempFile()
27+
.then(tmpFilePath => {
28+
debug('Downloading Chromium archive from Google CDN');
29+
const url = utils.getDownloadUrl(revision);
2730

28-
debug('Downloading Chromium archive from Google CDN');
29-
const url = utils.getDownloadUrl(revision);
30-
31-
return _downloadFile(url, tmpPath);
31+
return _downloadFile(url, tmpFilePath);
32+
});
3233
}
3334

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

68-
async function install() {
69-
try {
70-
console.info('Step 1. Retrieving Chromium latest revision number');
71-
const revision = await utils.getLatestRevisionNumber();
72-
73-
console.info('Step 2. Downloading Chromium (this might take a while)');
74-
const tmpPath = await downloadChromiumRevision(revision);
75-
76-
console.info('Step 3. Setting up Chromium binaries');
77-
await unzipArchive(tmpPath, config.BIN_OUT_PATH);
78-
79-
console.info('Process is successfully finished');
80-
} catch (err) {
81-
console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', err);
82-
}
70+
function install() {
71+
return utils.getLatestRevisionNumber()
72+
.then(downloadChromiumRevision)
73+
.then(downloadPath => unzipArchive(downloadPath, config.BIN_OUT_PATH))
74+
.then(() => console.info('Process is successfully finished'))
75+
.catch(err => console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', err));
8376
}
8477

8578
module.exports = install();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
{
3434
"files": "test-install.js",
3535
"rules": {
36-
"no-await-in-loop": "off"
36+
"no-await-in-loop": "off",
37+
"ava/prefer-async-await": "off"
3738
}
3839
}
3940
]

test-install.js

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ import test from 'ava';
44

55
const fs = require('fs');
66
const rimraf = require('rimraf');
7-
const got = require('got');
87
const debug = require('debug')('node-chromium');
98

109
const utils = require('./utils');
1110
const config = require('./config');
1211

13-
const install = async () => {
14-
await require('./install');
15-
};
16-
1712
test.before(t => {
1813
// Deleting output folder
1914
const outPath = config.BIN_OUT_PATH;
@@ -34,54 +29,10 @@ test('Before Install Process', t => {
3429
t.false(fs.existsSync(binPath), `Chromium binary is found in: [${binPath}]`);
3530
});
3631

37-
test('Chromium Install', async t => {
38-
await install();
39-
40-
const binPath = utils.getOsChromiumBinPath();
41-
const isExists = fs.existsSync(binPath);
42-
t.true(isExists, `Chromium binary is not found in: [${binPath}]`);
43-
});
44-
45-
test.serial('Different OS support', async t => {
46-
const supportedPlatforms = ['darwin', 'linux', 'win32'];
47-
const notSupportedPlatforms = ['aix', 'freebsd', 'openbsd', 'sunos'];
48-
49-
const originalPlatform = process.platform;
50-
51-
for (const platform of supportedPlatforms) {
52-
mockPlatform(platform);
53-
54-
const revision = await utils.getLatestRevisionNumber();
55-
56-
const url = utils.getDownloadUrl(revision);
57-
t.true(await isUrlAccessible(url));
58-
}
59-
60-
for (const platform of notSupportedPlatforms) {
61-
mockPlatform(platform);
62-
63-
t.throws(() => {
64-
utils.getDownloadUrl();
65-
}, 'Unsupported platform');
66-
}
67-
68-
mockPlatform(originalPlatform);
69-
70-
t.pass();
71-
});
72-
73-
async function isUrlAccessible(url) {
74-
try {
75-
const response = await got(url, {method: 'HEAD'});
76-
return /4\d\d/.test(response.statusCode) === false;
77-
} catch (err) {
78-
console.warn(`An error [${err.message}] occurred while trying to check URL [${url}] accessibility`);
79-
return false;
80-
}
81-
}
82-
83-
function mockPlatform(newPlatformValue) {
84-
Object.defineProperty(process, 'platform', {
85-
value: newPlatformValue
32+
test('Chromium Install', t => {
33+
return require('./install').then(() => {
34+
const binPath = utils.getOsChromiumBinPath();
35+
const isExists = fs.existsSync(binPath);
36+
t.true(isExists, `Chromium binary is not found in: [${binPath}]`);
8637
});
87-
}
38+
});

utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,17 @@ module.exports = {
9595
*
9696
* @returns {Promise<String>}
9797
*/
98-
async getLatestRevisionNumber() {
99-
const url = this.getOsCdnUrl() + '%2FLAST_CHANGE?alt=media';
100-
return (await got(url)).body;
98+
getLatestRevisionNumber() {
99+
return new Promise((resolve, reject) => {
100+
const url = this.getOsCdnUrl() + '%2FLAST_CHANGE?alt=media';
101+
got(url)
102+
.then(response => {
103+
resolve(response.body);
104+
})
105+
.catch(err => {
106+
console.error('An error occured while trying to retrieve latest revision number', err);
107+
reject(err);
108+
});
109+
});
101110
}
102111
};

0 commit comments

Comments
 (0)