mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: prefer use of fs/promise to utils/promisify (#9848)
Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com>
This commit is contained in:
parent
3e7a514e55
commit
17c650b6d1
@ -17,13 +17,12 @@
|
|||||||
import {exec as execChildProcess} from 'child_process';
|
import {exec as execChildProcess} from 'child_process';
|
||||||
import {createReadStream, createWriteStream, existsSync, readdirSync} from 'fs';
|
import {createReadStream, createWriteStream, existsSync, readdirSync} from 'fs';
|
||||||
import {chmod, mkdir, readdir, unlink} from 'fs/promises';
|
import {chmod, mkdir, readdir, unlink} from 'fs/promises';
|
||||||
import * as http from 'http';
|
import http from 'http';
|
||||||
import * as https from 'https';
|
import https from 'https';
|
||||||
import * as os from 'os';
|
import os from 'os';
|
||||||
import * as path from 'path';
|
import path from 'path';
|
||||||
import * as URL from 'url';
|
import URL from 'url';
|
||||||
import * as util from 'util';
|
import {promisify, format} from 'util';
|
||||||
import {promisify} from 'util';
|
|
||||||
|
|
||||||
import extractZip from 'extract-zip';
|
import extractZip from 'extract-zip';
|
||||||
import createHttpsProxyAgent, {
|
import createHttpsProxyAgent, {
|
||||||
@ -106,7 +105,7 @@ function downloadURL(
|
|||||||
host: string,
|
host: string,
|
||||||
revision: string
|
revision: string
|
||||||
): string {
|
): string {
|
||||||
const url = util.format(
|
const url = format(
|
||||||
downloadURLs[product][platform],
|
downloadURLs[product][platform],
|
||||||
host,
|
host,
|
||||||
revision,
|
revision,
|
||||||
|
@ -14,11 +14,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as childProcess from 'child_process';
|
import childProcess from 'child_process';
|
||||||
import * as fs from 'fs';
|
import fs from 'fs';
|
||||||
import * as path from 'path';
|
import {rename, unlink} from 'fs/promises';
|
||||||
import * as readline from 'readline';
|
import path from 'path';
|
||||||
import {promisify} from 'util';
|
import readline from 'readline';
|
||||||
|
|
||||||
import rimraf from 'rimraf';
|
import rimraf from 'rimraf';
|
||||||
|
|
||||||
@ -40,9 +40,6 @@ import {isErrnoException, isErrorLike} from '../util/ErrorLike.js';
|
|||||||
import {LaunchOptions} from './LaunchOptions.js';
|
import {LaunchOptions} from './LaunchOptions.js';
|
||||||
import {PipeTransport} from './PipeTransport.js';
|
import {PipeTransport} from './PipeTransport.js';
|
||||||
|
|
||||||
const renameAsync = promisify(fs.rename);
|
|
||||||
const unlinkAsync = promisify(fs.unlink);
|
|
||||||
|
|
||||||
const debugLauncher = debug('puppeteer:launcher');
|
const debugLauncher = debug('puppeteer:launcher');
|
||||||
|
|
||||||
const PROCESS_ERROR_EXPLANATION = `Puppeteer was unable to kill the process which ran the browser binary.
|
const PROCESS_ERROR_EXPLANATION = `Puppeteer was unable to kill the process which ran the browser binary.
|
||||||
@ -136,7 +133,7 @@ export class BrowserRunner {
|
|||||||
try {
|
try {
|
||||||
// When an existing user profile has been used remove the user
|
// When an existing user profile has been used remove the user
|
||||||
// preferences file and restore possibly backuped preferences.
|
// preferences file and restore possibly backuped preferences.
|
||||||
await unlinkAsync(path.join(this.#userDataDir, 'user.js'));
|
await unlink(path.join(this.#userDataDir, 'user.js'));
|
||||||
|
|
||||||
const prefsBackupPath = path.join(
|
const prefsBackupPath = path.join(
|
||||||
this.#userDataDir,
|
this.#userDataDir,
|
||||||
@ -144,8 +141,8 @@ export class BrowserRunner {
|
|||||||
);
|
);
|
||||||
if (fs.existsSync(prefsBackupPath)) {
|
if (fs.existsSync(prefsBackupPath)) {
|
||||||
const prefsPath = path.join(this.#userDataDir, 'prefs.js');
|
const prefsPath = path.join(this.#userDataDir, 'prefs.js');
|
||||||
await unlinkAsync(prefsPath);
|
await unlink(prefsPath);
|
||||||
await renameAsync(prefsBackupPath, prefsPath);
|
await rename(prefsBackupPath, prefsPath);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
debugError(error);
|
debugError(error);
|
||||||
|
@ -14,10 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from 'fs';
|
import {mkdtemp} from 'fs/promises';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {promisify} from 'util';
|
|
||||||
|
|
||||||
import expect from 'expect';
|
import expect from 'expect';
|
||||||
import {
|
import {
|
||||||
@ -28,8 +27,6 @@ import rimraf from 'rimraf';
|
|||||||
|
|
||||||
import {getTestState} from './mocha-utils.js';
|
import {getTestState} from './mocha-utils.js';
|
||||||
|
|
||||||
const mkdtempAsync = promisify(fs.mkdtemp);
|
|
||||||
|
|
||||||
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
||||||
|
|
||||||
const extensionPath = path.join(__dirname, '../assets', 'simple-extension');
|
const extensionPath = path.join(__dirname, '../assets', 'simple-extension');
|
||||||
@ -215,7 +212,7 @@ describe('headful tests', function () {
|
|||||||
/* Needs investigation into why but this fails consistently on Windows CI. */
|
/* Needs investigation into why but this fails consistently on Windows CI. */
|
||||||
const {server, puppeteer} = getTestState();
|
const {server, puppeteer} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
// Write a cookie in headful chrome
|
// Write a cookie in headful chrome
|
||||||
const headfulBrowser = await launchBrowser(
|
const headfulBrowser = await launchBrowser(
|
||||||
puppeteer,
|
puppeteer,
|
||||||
|
@ -14,10 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import {mkdtemp, readFile, stat, writeFile} from 'fs/promises';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {TLSSocket} from 'tls';
|
import {TLSSocket} from 'tls';
|
||||||
import {promisify} from 'util';
|
|
||||||
|
|
||||||
import {Protocol} from 'devtools-protocol';
|
import {Protocol} from 'devtools-protocol';
|
||||||
import expect from 'expect';
|
import expect from 'expect';
|
||||||
@ -32,10 +32,6 @@ import utils from './utils.js';
|
|||||||
const rmAsync = (filename: string) => {
|
const rmAsync = (filename: string) => {
|
||||||
return rimraf(filename);
|
return rimraf(filename);
|
||||||
};
|
};
|
||||||
const mkdtempAsync = promisify(fs.mkdtemp);
|
|
||||||
const readFileAsync = promisify(fs.readFile);
|
|
||||||
const statAsync = promisify(fs.stat);
|
|
||||||
const writeFileAsync = promisify(fs.writeFile);
|
|
||||||
|
|
||||||
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
||||||
const FIREFOX_TIMEOUT = 30 * 1000;
|
const FIREFOX_TIMEOUT = 30 * 1000;
|
||||||
@ -50,7 +46,7 @@ describe('Launcher specs', function () {
|
|||||||
it('should download and extract chrome linux binary', async () => {
|
it('should download and extract chrome linux binary', async () => {
|
||||||
const {server} = getTestState();
|
const {server} = getTestState();
|
||||||
|
|
||||||
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
|
const downloadsFolder = await mkdtemp(TMP_FOLDER);
|
||||||
const browserFetcher = new BrowserFetcher({
|
const browserFetcher = new BrowserFetcher({
|
||||||
platform: 'linux',
|
platform: 'linux',
|
||||||
path: downloadsFolder,
|
path: downloadsFolder,
|
||||||
@ -74,13 +70,13 @@ describe('Launcher specs', function () {
|
|||||||
|
|
||||||
revisionInfo = (await browserFetcher.download(expectedRevision))!;
|
revisionInfo = (await browserFetcher.download(expectedRevision))!;
|
||||||
expect(revisionInfo.local).toBe(true);
|
expect(revisionInfo.local).toBe(true);
|
||||||
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe(
|
expect(await readFile(revisionInfo.executablePath, 'utf8')).toBe(
|
||||||
'LINUX BINARY\n'
|
'LINUX BINARY\n'
|
||||||
);
|
);
|
||||||
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
|
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
|
||||||
expect(
|
expect((await stat(revisionInfo.executablePath)).mode & 0o777).toBe(
|
||||||
(await statAsync(revisionInfo.executablePath)).mode & 0o777
|
expectedPermissions
|
||||||
).toBe(expectedPermissions);
|
);
|
||||||
expect(await browserFetcher.localRevisions()).toEqual([
|
expect(await browserFetcher.localRevisions()).toEqual([
|
||||||
expectedRevision,
|
expectedRevision,
|
||||||
]);
|
]);
|
||||||
@ -91,7 +87,7 @@ describe('Launcher specs', function () {
|
|||||||
it('should download and extract firefox linux binary', async () => {
|
it('should download and extract firefox linux binary', async () => {
|
||||||
const {server} = getTestState();
|
const {server} = getTestState();
|
||||||
|
|
||||||
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
|
const downloadsFolder = await mkdtemp(TMP_FOLDER);
|
||||||
const browserFetcher = new BrowserFetcher({
|
const browserFetcher = new BrowserFetcher({
|
||||||
platform: 'linux',
|
platform: 'linux',
|
||||||
path: downloadsFolder,
|
path: downloadsFolder,
|
||||||
@ -119,13 +115,13 @@ describe('Launcher specs', function () {
|
|||||||
|
|
||||||
revisionInfo = (await browserFetcher.download(expectedVersion))!;
|
revisionInfo = (await browserFetcher.download(expectedVersion))!;
|
||||||
expect(revisionInfo.local).toBe(true);
|
expect(revisionInfo.local).toBe(true);
|
||||||
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe(
|
expect(await readFile(revisionInfo.executablePath, 'utf8')).toBe(
|
||||||
'FIREFOX LINUX BINARY\n'
|
'FIREFOX LINUX BINARY\n'
|
||||||
);
|
);
|
||||||
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
|
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
|
||||||
expect(
|
expect((await stat(revisionInfo.executablePath)).mode & 0o777).toBe(
|
||||||
(await statAsync(revisionInfo.executablePath)).mode & 0o777
|
expectedPermissions
|
||||||
).toBe(expectedPermissions);
|
);
|
||||||
expect(await browserFetcher.localRevisions()).toEqual([
|
expect(await browserFetcher.localRevisions()).toEqual([
|
||||||
expectedVersion,
|
expectedVersion,
|
||||||
]);
|
]);
|
||||||
@ -243,7 +239,7 @@ describe('Launcher specs', function () {
|
|||||||
it('userDataDir option', async () => {
|
it('userDataDir option', async () => {
|
||||||
const {defaultBrowserOptions, puppeteer} = getTestState();
|
const {defaultBrowserOptions, puppeteer} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
||||||
const browser = await puppeteer.launch(options);
|
const browser = await puppeteer.launch(options);
|
||||||
// Open a page to make sure its functional.
|
// Open a page to make sure its functional.
|
||||||
@ -288,11 +284,11 @@ describe('Launcher specs', function () {
|
|||||||
it('userDataDir option restores preferences', async () => {
|
it('userDataDir option restores preferences', async () => {
|
||||||
const {defaultBrowserOptions, puppeteer} = getTestState();
|
const {defaultBrowserOptions, puppeteer} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
|
|
||||||
const prefsJSPath = path.join(userDataDir, 'prefs.js');
|
const prefsJSPath = path.join(userDataDir, 'prefs.js');
|
||||||
const prefsJSContent = 'user_pref("browser.warnOnQuit", true)';
|
const prefsJSContent = 'user_pref("browser.warnOnQuit", true)';
|
||||||
await writeFileAsync(prefsJSPath, prefsJSContent);
|
await writeFile(prefsJSPath, prefsJSContent);
|
||||||
|
|
||||||
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
||||||
const browser = await puppeteer.launch(options);
|
const browser = await puppeteer.launch(options);
|
||||||
@ -302,7 +298,7 @@ describe('Launcher specs', function () {
|
|||||||
await browser.close();
|
await browser.close();
|
||||||
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
||||||
|
|
||||||
expect(await readFileAsync(prefsJSPath, 'utf8')).toBe(prefsJSContent);
|
expect(await readFile(prefsJSPath, 'utf8')).toBe(prefsJSContent);
|
||||||
|
|
||||||
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
||||||
await rmAsync(userDataDir).catch(() => {});
|
await rmAsync(userDataDir).catch(() => {});
|
||||||
@ -310,7 +306,7 @@ describe('Launcher specs', function () {
|
|||||||
it('userDataDir argument', async () => {
|
it('userDataDir argument', async () => {
|
||||||
const {isChrome, puppeteer, defaultBrowserOptions} = getTestState();
|
const {isChrome, puppeteer, defaultBrowserOptions} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
const options = Object.assign({}, defaultBrowserOptions);
|
const options = Object.assign({}, defaultBrowserOptions);
|
||||||
if (isChrome) {
|
if (isChrome) {
|
||||||
options.args = [
|
options.args = [
|
||||||
@ -334,7 +330,7 @@ describe('Launcher specs', function () {
|
|||||||
it('userDataDir argument with non-existent dir', async () => {
|
it('userDataDir argument with non-existent dir', async () => {
|
||||||
const {isChrome, puppeteer, defaultBrowserOptions} = getTestState();
|
const {isChrome, puppeteer, defaultBrowserOptions} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
await rmAsync(userDataDir);
|
await rmAsync(userDataDir);
|
||||||
const options = Object.assign({}, defaultBrowserOptions);
|
const options = Object.assign({}, defaultBrowserOptions);
|
||||||
if (isChrome) {
|
if (isChrome) {
|
||||||
@ -359,7 +355,7 @@ describe('Launcher specs', function () {
|
|||||||
it('userDataDir option should restore state', async () => {
|
it('userDataDir option should restore state', async () => {
|
||||||
const {server, puppeteer, defaultBrowserOptions} = getTestState();
|
const {server, puppeteer, defaultBrowserOptions} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
||||||
const browser = await puppeteer.launch(options);
|
const browser = await puppeteer.launch(options);
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
@ -384,7 +380,7 @@ describe('Launcher specs', function () {
|
|||||||
it('userDataDir option should restore cookies', async () => {
|
it('userDataDir option should restore cookies', async () => {
|
||||||
const {server, puppeteer, defaultBrowserOptions} = getTestState();
|
const {server, puppeteer, defaultBrowserOptions} = getTestState();
|
||||||
|
|
||||||
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
||||||
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
||||||
const browser = await puppeteer.launch(options);
|
const browser = await puppeteer.launch(options);
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
Loading…
Reference in New Issue
Block a user