From 1f76cdda12ec2a9f22be02f9a60ad4265649565a Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 21 Mar 2023 13:41:56 +0100 Subject: [PATCH] chore: replace rimraf with a helper in BrowserFetcher/Connector (#9890) --- package-lock.json | 4 +- packages/puppeteer-core/package.json | 1 - .../puppeteer-core/src/node/BrowserFetcher.ts | 4 +- .../puppeteer-core/src/node/BrowserRunner.ts | 7 ++-- packages/puppeteer-core/src/util/fs.ts | 37 +++++++++++++++++++ packages/puppeteer-core/src/util/util.ts | 1 + test/src/headful.spec.ts | 3 +- test/src/launcher.spec.ts | 3 +- test/src/mocha-utils.ts | 9 +---- 9 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 packages/puppeteer-core/src/util/fs.ts diff --git a/package-lock.json b/package-lock.json index c07b869c68b..52c78cd0a65 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7750,6 +7750,7 @@ "version": "4.4.0", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-4.4.0.tgz", "integrity": "sha512-X36S+qpCUR0HjXlkDe4NAOhS//aHH0Z+h8Ckf2auGJk3PTnx5rLmrHkwNdbVQuCSUhOyFrlRvFEllZOYE+yZGQ==", + "dev": true, "dependencies": { "glob": "^9.2.0" }, @@ -9303,7 +9304,6 @@ "extract-zip": "2.0.1", "https-proxy-agent": "5.0.1", "proxy-from-env": "1.1.0", - "rimraf": "4.4.0", "tar-fs": "2.1.1", "unbzip2-stream": "1.4.3", "ws": "8.13.0" @@ -14108,7 +14108,6 @@ "mitt": "3.0.0", "parsel-js": "1.1.0", "proxy-from-env": "1.1.0", - "rimraf": "4.4.0", "tar-fs": "2.1.1", "unbzip2-stream": "1.4.3", "ws": "8.13.0" @@ -14410,6 +14409,7 @@ "version": "4.4.0", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-4.4.0.tgz", "integrity": "sha512-X36S+qpCUR0HjXlkDe4NAOhS//aHH0Z+h8Ckf2auGJk3PTnx5rLmrHkwNdbVQuCSUhOyFrlRvFEllZOYE+yZGQ==", + "dev": true, "requires": { "glob": "^9.2.0" } diff --git a/packages/puppeteer-core/package.json b/packages/puppeteer-core/package.json index ffe4a3ea743..a1be90ed5f3 100644 --- a/packages/puppeteer-core/package.json +++ b/packages/puppeteer-core/package.json @@ -138,7 +138,6 @@ "extract-zip": "2.0.1", "https-proxy-agent": "5.0.1", "proxy-from-env": "1.1.0", - "rimraf": "4.4.0", "tar-fs": "2.1.1", "unbzip2-stream": "1.4.3", "ws": "8.13.0" diff --git a/packages/puppeteer-core/src/node/BrowserFetcher.ts b/packages/puppeteer-core/src/node/BrowserFetcher.ts index 99815509906..8ac4edebd89 100644 --- a/packages/puppeteer-core/src/node/BrowserFetcher.ts +++ b/packages/puppeteer-core/src/node/BrowserFetcher.ts @@ -30,13 +30,13 @@ import createHttpsProxyAgent, { HttpsProxyAgentOptions, } from 'https-proxy-agent'; import {getProxyForUrl} from 'proxy-from-env'; -import rimraf from 'rimraf'; import tar from 'tar-fs'; import bzip from 'unbzip2-stream'; import {debug} from '../common/Debug.js'; import {Product} from '../common/Product.js'; import {assert} from '../util/assert.js'; +import {rm} from '../util/fs.js'; const debugFetcher = debug('puppeteer:fetcher'); @@ -412,7 +412,7 @@ export class BrowserFetcher { existsSync(folderPath), `Failed to remove: revision ${revision} is not downloaded` ); - await rimraf(folderPath); + await rm(folderPath); } /** diff --git a/packages/puppeteer-core/src/node/BrowserRunner.ts b/packages/puppeteer-core/src/node/BrowserRunner.ts index 9e2b9cd6dd6..cb8e2474293 100644 --- a/packages/puppeteer-core/src/node/BrowserRunner.ts +++ b/packages/puppeteer-core/src/node/BrowserRunner.ts @@ -20,8 +20,6 @@ import {rename, unlink} from 'fs/promises'; import path from 'path'; import readline from 'readline'; -import rimraf from 'rimraf'; - import type {Connection as BiDiConnection} from '../common/bidi/bidi.js'; import {Connection} from '../common/Connection.js'; import {debug} from '../common/Debug.js'; @@ -36,6 +34,7 @@ import { } from '../common/util.js'; import {assert} from '../util/assert.js'; import {isErrnoException, isErrorLike} from '../util/ErrorLike.js'; +import {rm, rmSync} from '../util/fs.js'; import {LaunchOptions} from './LaunchOptions.js'; import {PipeTransport} from './PipeTransport.js'; @@ -122,7 +121,7 @@ export class BrowserRunner { // Cleanup as processes exit. if (this.#isTempUserDataDir) { try { - await rimraf(this.#userDataDir); + await rm(this.#userDataDir); fulfill(); } catch (error) { debugError(error); @@ -236,7 +235,7 @@ export class BrowserRunner { // Attempt to remove temporary profile directory to avoid littering. try { if (this.#isTempUserDataDir) { - rimraf.sync(this.#userDataDir); + rmSync(this.#userDataDir); } } catch (error) {} diff --git a/packages/puppeteer-core/src/util/fs.ts b/packages/puppeteer-core/src/util/fs.ts new file mode 100644 index 00000000000..ae0419a91d4 --- /dev/null +++ b/packages/puppeteer-core/src/util/fs.ts @@ -0,0 +1,37 @@ +/** + * Copyright 2023 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs'; + +const rmOptions = { + force: true, + recursive: true, + maxRetries: 5, +}; + +/** + * @internal + */ +export async function rm(path: string): Promise { + await fs.promises.rm(path, rmOptions); +} + +/** + * @internal + */ +export function rmSync(path: string): void { + fs.rmSync(path, rmOptions); +} diff --git a/packages/puppeteer-core/src/util/util.ts b/packages/puppeteer-core/src/util/util.ts index d3160757948..cfa024ae056 100644 --- a/packages/puppeteer-core/src/util/util.ts +++ b/packages/puppeteer-core/src/util/util.ts @@ -15,6 +15,7 @@ */ export * from './assert.js'; +export * from './fs.js'; export * from './DebuggableDeferredPromise.js'; export * from './DeferredPromise.js'; export * from './ErrorLike.js'; diff --git a/test/src/headful.spec.ts b/test/src/headful.spec.ts index a27f5527494..aad99d28bed 100644 --- a/test/src/headful.spec.ts +++ b/test/src/headful.spec.ts @@ -23,8 +23,9 @@ import { PuppeteerLaunchOptions, PuppeteerNode, } from 'puppeteer-core/internal/node/PuppeteerNode.js'; +import {rmSync} from 'puppeteer-core/internal/util/fs.js'; -import {getTestState, rmSync} from './mocha-utils.js'; +import {getTestState} from './mocha-utils.js'; const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-'); diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index b5446f10dff..dfcc734b2e7 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -23,9 +23,10 @@ import {Protocol} from 'devtools-protocol'; import expect from 'expect'; import {BrowserFetcher, TimeoutError} from 'puppeteer'; import {Page} from 'puppeteer-core/internal/api/Page.js'; +import {rmSync} from 'puppeteer-core/internal/util/fs.js'; import sinon from 'sinon'; -import {getTestState, itOnlyRegularInstall, rmSync} from './mocha-utils.js'; +import {getTestState, itOnlyRegularInstall} from './mocha-utils.js'; import utils from './utils.js'; const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-'); diff --git a/test/src/mocha-utils.ts b/test/src/mocha-utils.ts index d8a4b6f40c0..2a99671011b 100644 --- a/test/src/mocha-utils.ts +++ b/test/src/mocha-utils.ts @@ -34,6 +34,7 @@ import { PuppeteerNode, } from 'puppeteer-core/internal/node/PuppeteerNode.js'; import {isErrorLike} from 'puppeteer-core/internal/util/ErrorLike.js'; +import {rmSync} from 'puppeteer-core/internal/util/fs.js'; import sinon from 'sinon'; import {extendExpectWithToBeGolden} from './utils.js'; @@ -331,11 +332,3 @@ export const createTimeout = ( }, n); }); }; - -export function rmSync(target: string): void { - fs.rmSync(target, { - force: true, - recursive: true, - maxRetries: 5, - }); -}