Skip to content

Commit

Permalink
Remove use of legacy denodeify
Browse files Browse the repository at this point in the history
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
  • Loading branch information
robhogan authored and facebook-github-bot committed Oct 16, 2024
1 parent 0f8cabd commit f6aefdf
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 44 deletions.
1 change: 0 additions & 1 deletion packages/metro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 9 additions & 28 deletions packages/metro/src/Assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
+hash: string,
Expand Down Expand Up @@ -95,25 +91,6 @@ export type AssetDataPlugin = (
assetData: AssetData,
) => AssetData | Promise<AssetData>;

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<string>,
Expand Down Expand Up @@ -168,7 +145,7 @@ async function getAbsoluteAssetRecord(
): Promise<{files: Array<string>, scales: Array<number>}> {
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,
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/shared/output/bundle.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
Expand Down
13 changes: 5 additions & 8 deletions packages/metro/src/shared/output/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>;
const writeFile: WriteFn = throat(128, denodeify(fs.writeFile));
const writeFile: typeof fs.promises.writeFile = throat(
128,
fs.promises.writeFile,
);

module.exports = writeFile;
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f6aefdf

Please sign in to comment.