fix: Remove synchronous operation with file system. (#879)

This patch:
- introduces `helper.promisify` - a simple polyfill for the `util.promisify`. The 
  `util.promisify` could not be used due to Node6 compatibility issues.
- migrates all sync filesystem operations to the async replicas

Fixes #884.
This commit is contained in:
Gleb Azarov
2017-10-11 10:55:48 +03:00
committed by Andrey Lushnikov
parent 7a8aa73466
commit ff08e45785
4 changed files with 42 additions and 13 deletions

View File

@@ -15,7 +15,7 @@
*/
const os = require('os');
const path = require('path');
const removeSync = require('rimraf').sync;
const removeFolder = require('rimraf');
const childProcess = require('child_process');
const Downloader = require('../utils/ChromiumDownloader');
const {Connection} = require('./Connection');
@@ -26,6 +26,9 @@ const {helper} = require('./helper');
// @ts-ignore
const ChromiumRevision = require('../package.json').puppeteer.chromium_revision;
const mkdtempAsync = helper.promisify(fs.mkdtemp);
const removeFolderAsync = helper.promisify(removeFolder);
const CHROME_PROFILE_PATH = path.join(os.tmpdir(), 'puppeteer_dev_profile-');
const DEFAULT_ARGS = [
@@ -69,7 +72,7 @@ class Launcher {
if (!options.args || !options.args.some(arg => arg.startsWith('--user-data-dir'))) {
if (!options.userDataDir)
temporaryUserDataDir = fs.mkdtempSync(CHROME_PROFILE_PATH);
temporaryUserDataDir = await mkdtempAsync(CHROME_PROFILE_PATH);
chromeArguments.push(`--user-data-dir=${options.userDataDir || temporaryUserDataDir}`);
}
@@ -107,12 +110,16 @@ class Launcher {
chromeProcess.stderr.pipe(process.stderr);
}
const waitForChromeToClose = new Promise(fulfill => {
const waitForChromeToClose = new Promise((fulfill, reject) => {
chromeProcess.once('close', () => {
// Cleanup as processes exit.
if (temporaryUserDataDir)
removeSync(temporaryUserDataDir);
fulfill();
if (temporaryUserDataDir) {
removeFolderAsync(temporaryUserDataDir)
.then(() => fulfill())
.catch(err => console.error(err));
} else {
fulfill();
}
});
});
@@ -153,7 +160,7 @@ class Launcher {
if (temporaryUserDataDir) {
// Attempt to remove temporary profile directory to avoid littering.
try {
removeSync(temporaryUserDataDir);
removeFolder.sync(temporaryUserDataDir);
} catch (e) { }
}
return waitForChromeToClose;