From f6aefdf5f0497a8bd3df8fcc5b01cd53d905f683 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Wed, 16 Oct 2024 09:37:07 -0700 Subject: [PATCH] Remove use of legacy `denodeify` Summary: Just some idle cleanup - remove use of a very old npm library to convert callback APIs to promise APIs, no longer needed. Changelog: Internal Reviewed By: huntie Differential Revision: D64464434 fbshipit-source-id: 88f13b59e9b5cfb965c95374d1669824e7bf1a2a --- packages/metro/package.json | 1 - packages/metro/src/Assets.js | 37 +++++-------------- .../output/RamBundle/write-sourcemap.js | 2 +- .../metro/src/shared/output/bundle.flow.js | 2 +- packages/metro/src/shared/output/writeFile.js | 13 +++---- yarn.lock | 5 --- 6 files changed, 16 insertions(+), 44 deletions(-) diff --git a/packages/metro/package.json b/packages/metro/package.json index 1f58fdecbd..707cf136f7 100644 --- a/packages/metro/package.json +++ b/packages/metro/package.json @@ -25,7 +25,6 @@ "ci-info": "^2.0.0", "connect": "^3.6.5", "debug": "^2.2.0", - "denodeify": "^1.2.1", "error-stack-parser": "^2.0.6", "flow-enums-runtime": "^0.0.6", "graceful-fs": "^4.2.4", diff --git a/packages/metro/src/Assets.js b/packages/metro/src/Assets.js index 10f47d932c..bef30fd5c4 100644 --- a/packages/metro/src/Assets.js +++ b/packages/metro/src/Assets.js @@ -15,14 +15,10 @@ import type {AssetPath} from './node-haste/lib/AssetPaths'; const AssetPaths = require('./node-haste/lib/AssetPaths'); const crypto = require('crypto'); -const denodeify = require('denodeify'); const fs = require('fs'); const getImageSize = require('image-size'); const path = require('path'); -const readDir = denodeify(fs.readdir); -const readFile = denodeify(fs.readFile); - export type AssetInfo = { +files: Array, +hash: string, @@ -95,25 +91,6 @@ export type AssetDataPlugin = ( assetData: AssetData, ) => AssetData | Promise; -const hashFiles = denodeify(function hashFilesCb(files, hash, callback): void { - if (!files.length) { - callback(null); - return; - } - - const file = files.shift(); - - fs.readFile(file, (err, data: Buffer) => { - if (err) { - callback(err); - return; - } else { - hash.update(data); - hashFilesCb(files, hash, callback); - } - }); -}); - function buildAssetMap( dir: string, files: $ReadOnlyArray, @@ -168,7 +145,7 @@ async function getAbsoluteAssetRecord( ): Promise<{files: Array, scales: Array}> { const filename = path.basename(assetPath); const dir = path.dirname(assetPath); - const files = await readDir(dir); + const files = await fs.promises.readdir(dir); const assetData = AssetPaths.parse( filename, @@ -210,8 +187,12 @@ async function getAbsoluteAssetInfo( const {scales, files} = await getAbsoluteAssetRecord(assetPath, platform); const hasher = crypto.createHash('md5'); - if (files.length > 0) { - await hashFiles(Array.from(files), hasher); + const fileData = await Promise.all( + files.map(file => fs.promises.readFile(file)), + ); + + for (const data of fileData) { + hasher.update(data); } return {files, hash: hasher.digest('hex'), name, scales, type}; @@ -328,11 +309,11 @@ async function getAsset( for (let i = 0; i < record.scales.length; i++) { if (record.scales[i] >= assetData.resolution) { - return readFile(record.files[i]); + return fs.promises.readFile(record.files[i]); } } - return readFile(record.files[record.files.length - 1]); + return fs.promises.readFile(record.files[record.files.length - 1]); } function pathBelongsToRoots( diff --git a/packages/metro/src/shared/output/RamBundle/write-sourcemap.js b/packages/metro/src/shared/output/RamBundle/write-sourcemap.js index 06c0efc24b..2d6604f9bd 100644 --- a/packages/metro/src/shared/output/RamBundle/write-sourcemap.js +++ b/packages/metro/src/shared/output/RamBundle/write-sourcemap.js @@ -22,7 +22,7 @@ function writeSourcemap( return Promise.resolve(); } log('Writing sourcemap output to:', fileName); - const writeMap = writeFile(fileName, contents, null); + const writeMap = writeFile(fileName, contents); // $FlowFixMe[unused-promise] writeMap.then(() => log('Done writing sourcemap output')); return writeMap; diff --git a/packages/metro/src/shared/output/bundle.flow.js b/packages/metro/src/shared/output/bundle.flow.js index fae2284ded..9f0038e1b7 100644 --- a/packages/metro/src/shared/output/bundle.flow.js +++ b/packages/metro/src/shared/output/bundle.flow.js @@ -76,7 +76,7 @@ async function saveBundleAndMap( writeFns.push(async () => { log(`Writing sourcemap output to: ${sourcemapOutput}`); - await writeFile(sourcemapOutput, map, null); + await writeFile(sourcemapOutput, map); log('Done writing sourcemap output'); }); } diff --git a/packages/metro/src/shared/output/writeFile.js b/packages/metro/src/shared/output/writeFile.js index 7b2b6fc052..7b5ca7343c 100644 --- a/packages/metro/src/shared/output/writeFile.js +++ b/packages/metro/src/shared/output/writeFile.js @@ -4,22 +4,19 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict-local * @format * @oncall react_native */ 'use strict'; -const denodeify = require('denodeify'); const fs = require('fs'); const throat = require('throat'); -type WriteFn = ( - file: string, - data: string | Buffer, - encoding?: ?string, -) => Promise; -const writeFile: WriteFn = throat(128, denodeify(fs.writeFile)); +const writeFile: typeof fs.promises.writeFile = throat( + 128, + fs.promises.writeFile, +); module.exports = writeFile; diff --git a/yarn.lock b/yarn.lock index 2d90043937..a063584afd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2336,11 +2336,6 @@ define-properties@^1.1.4: has-property-descriptors "^1.0.0" object-keys "^1.1.1" -denodeify@^1.2.1: - version "1.2.1" - resolved "https://registry.yarnpkg.com/denodeify/-/denodeify-1.2.1.tgz#3a36287f5034e699e7577901052c2e6c94251631" - integrity sha1-OjYof1A05pnnV3kBBSwubJQlFjE= - detect-newline@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"