From fe2f9553fe291752d9cd5368eda4ab6467f73172 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Mon, 12 Feb 2024 17:11:51 +0100 Subject: [PATCH] feat: use async resolve --- lib/common.ts | 12 ++++++++++++ lib/follow.ts | 37 +++++++++++++++++++++++-------------- package.json | 4 ++-- yarn.lock | 19 ++++++++++++++----- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/lib/common.ts b/lib/common.ts index 4bef2c75c..6eefe580b 100644 --- a/lib/common.ts +++ b/lib/common.ts @@ -1,5 +1,7 @@ import assert from 'assert'; import fs from 'fs'; +import { pathExists } from 'fs-extra'; +import { realpath } from 'fs/promises'; import path from 'path'; export const STORE_BLOB = 0; @@ -264,3 +266,13 @@ export function toNormalizedRealPath(requestPath: string) { return file; } + +export async function toNormalizedRealPathAsync(requestPath: string) { + const file = normalizePath(requestPath); + + if (await pathExists(requestPath)) { + return realpath(file); + } + + return file; +} diff --git a/lib/follow.ts b/lib/follow.ts index 03fba2505..3aadbc286 100644 --- a/lib/follow.ts +++ b/lib/follow.ts @@ -1,7 +1,7 @@ -import { sync, SyncOpts } from 'resolve'; +import async, { AsyncOpts } from 'resolve'; import fs from 'fs'; import path from 'path'; -import { toNormalizedRealPath } from './common'; +import { toNormalizedRealPathAsync } from './common'; import type { PackageJson } from './types'; @@ -25,20 +25,20 @@ function parentDirectoriesContain(parent: string, directory: string) { } } -interface FollowOptions extends Pick { +interface FollowOptions extends Pick { ignoreFile?: string; catchReadFile?: (file: string) => void; catchPackageFilter?: (config: PackageJson, base: string, dir: string) => void; } export function follow(x: string, opts: FollowOptions) { - // TODO async version - return new Promise((resolve) => { - resolve( - sync(x, { + return new Promise((resolve, reject) => { + async( + x, + { basedir: opts.basedir, extensions: opts.extensions, - isFile: (file) => { + isFile: (file: string) => { if ( opts.ignoreFile && path.join(path.dirname(opts.ignoreFile), PROOF) === file @@ -85,16 +85,16 @@ export function follow(x: string, opts: FollowOptions) { return stat.isDirectory(); }, - readFileSync: (file) => { + readFile: (file) => { if (opts.ignoreFile && opts.ignoreFile === file) { - return Buffer.from(`{"main":"${PROOF}"}`); + return Promise.resolve(Buffer.from(`{"main":"${PROOF}"}`)); } if (opts.catchReadFile) { opts.catchReadFile(file); } - return fs.readFileSync(file); + return fs.promises.readFile(file); }, packageFilter: (config, base, dir) => { if (opts.catchPackageFilter) { @@ -106,11 +106,20 @@ export function follow(x: string, opts: FollowOptions) { /** function to synchronously resolve a potential symlink to its real path */ // realpathSync?: (file: string) => string; - realpathSync: (file) => { - const file2 = toNormalizedRealPath(file); + realpath: async (file) => { + const file2 = await toNormalizedRealPathAsync(file); return file2; }, - }), + }, + (err, res) => { + if (err) { + reject(err); + } else if (res === undefined) { + reject(new Error(`Cannot find module '${x}'`)); + } else { + resolve(res); + } + }, ); }); } diff --git a/package.json b/package.json index 5704916de..7790505be 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "minimist": "^1.2.6", "multistream": "^4.1.0", "prebuild-install": "7.1.1", - "resolve": "^1.22.0", + "resolve": "2.0.0-next.5", "stream-meter": "^1.0.4" }, "devDependencies": { @@ -46,7 +46,7 @@ "@types/minimist": "1.2.2", "@types/multistream": "4.1.0", "@types/node": "14.18.20", - "@types/resolve": "1.20.2", + "@types/resolve": "1.20.6", "@types/stream-meter": "0.0.22", "@typescript-eslint/eslint-plugin": "6.7.4", "@typescript-eslint/parser": "6.7.4", diff --git a/yarn.lock b/yarn.lock index 61694bff4..5aea7dd2e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -538,10 +538,10 @@ resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz" integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== -"@types/resolve@1.20.2": - version "1.20.2" - resolved "https://registry.npmjs.org/@types/resolve/-/resolve-1.20.2.tgz" - integrity sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q== +"@types/resolve@1.20.6": + version "1.20.6" + resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.6.tgz#e6e60dad29c2c8c206c026e6dd8d6d1bdda850b8" + integrity sha512-A4STmOXPhMUtHH+S6ymgE2GiBSMqf4oTvcQZMcHzokuTLVYzXTB8ttjcgxOVaAp2lGwEdzZ0J+cRbbeevQj1UQ== "@types/semver@^7.5.0": version "7.5.3" @@ -3891,7 +3891,16 @@ resolve-from@^4.0.0: resolved "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz" integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g== -resolve@^1.1.6, resolve@^1.22.0, resolve@^1.22.4: +resolve@2.0.0-next.5: + version "2.0.0-next.5" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-2.0.0-next.5.tgz#6b0ec3107e671e52b68cd068ef327173b90dc03c" + integrity sha512-U7WjGVG9sH8tvjW5SmGbQuui75FiyjAX72HX15DwBBwF9dNiQZRQAg9nnPhYy+TUnE0+VcrttuvNI8oSxZcocA== + dependencies: + is-core-module "^2.13.0" + path-parse "^1.0.7" + supports-preserve-symlinks-flag "^1.0.0" + +resolve@^1.1.6, resolve@^1.22.4: version "1.22.6" resolved "https://registry.npmjs.org/resolve/-/resolve-1.22.6.tgz" integrity sha512-njhxM7mV12JfufShqGy3Rz8j11RPdLy4xi15UurGJeoHLfJpVXKdh3ueuOqbYUcDZnffr6X739JBo5LzyahEsw==