From d79dc056bc5e8ff83c3138154b8eee6cea424d03 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Tue, 7 May 2019 15:54:35 +0200 Subject: [PATCH] refactor: eliminate duplicate code in asar.js (#18146) --- lib/common/asar.js | 144 ++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 92 deletions(-) diff --git a/lib/common/asar.js b/lib/common/asar.js index f0977753d07..5a9ef0a5965 100644 --- a/lib/common/asar.js +++ b/lib/common/asar.js @@ -311,107 +311,67 @@ fs.promises.stat = util.promisify(fs.stat) - const { realpathSync } = fs - fs.realpathSync = function (pathArgument, options) { - const { isAsar, asarPath, filePath } = splitPath(pathArgument) - if (!isAsar) return realpathSync.apply(this, arguments) + const wrapRealpathSync = function (realpathSync) { + return function (pathArgument, options) { + const { isAsar, asarPath, filePath } = splitPath(pathArgument) + if (!isAsar) return realpathSync.apply(this, arguments) - const archive = getOrCreateArchive(asarPath) - if (!archive) { - throw createError(AsarError.INVALID_ARCHIVE, { asarPath }) + const archive = getOrCreateArchive(asarPath) + if (!archive) { + throw createError(AsarError.INVALID_ARCHIVE, { asarPath }) + } + + const fileRealPath = archive.realpath(filePath) + if (fileRealPath === false) { + throw createError(AsarError.NOT_FOUND, { asarPath, filePath }) + } + + return path.join(realpathSync(asarPath, options), fileRealPath) } - - const fileRealPath = archive.realpath(filePath) - if (fileRealPath === false) { - throw createError(AsarError.NOT_FOUND, { asarPath, filePath }) - } - - return path.join(realpathSync(asarPath, options), fileRealPath) } - fs.realpathSync.native = function (pathArgument, options) { - const { isAsar, asarPath, filePath } = splitPath(pathArgument) - if (!isAsar) return realpathSync.native.apply(this, arguments) + const { realpathSync } = fs + fs.realpathSync = wrapRealpathSync(realpathSync) + fs.realpathSync.native = wrapRealpathSync(realpathSync.native) - const archive = getOrCreateArchive(asarPath) - if (!archive) { - throw createError(AsarError.INVALID_ARCHIVE, { asarPath }) + const wrapRealpath = function (realpath) { + return function (pathArgument, options, callback) { + const { isAsar, asarPath, filePath } = splitPath(pathArgument) + if (!isAsar) return realpath.apply(this, arguments) + + if (arguments.length < 3) { + callback = options + options = {} + } + + const archive = getOrCreateArchive(asarPath) + if (!archive) { + const error = createError(AsarError.INVALID_ARCHIVE, { asarPath }) + nextTick(callback, [error]) + return + } + + const fileRealPath = archive.realpath(filePath) + if (fileRealPath === false) { + const error = createError(AsarError.NOT_FOUND, { asarPath, filePath }) + nextTick(callback, [error]) + return + } + + realpath(asarPath, options, (error, archiveRealPath) => { + if (error === null) { + const fullPath = path.join(archiveRealPath, fileRealPath) + callback(null, fullPath) + } else { + callback(error) + } + }) } - - const fileRealPath = archive.realpath(filePath) - if (fileRealPath === false) { - throw createError(AsarError.NOT_FOUND, { asarPath, filePath }) - } - - return path.join(realpathSync.native(asarPath, options), fileRealPath) } const { realpath } = fs - fs.realpath = function (pathArgument, options, callback) { - const { isAsar, asarPath, filePath } = splitPath(pathArgument) - if (!isAsar) return realpath.apply(this, arguments) - - if (arguments.length < 3) { - callback = options - options = {} - } - - const archive = getOrCreateArchive(asarPath) - if (!archive) { - const error = createError(AsarError.INVALID_ARCHIVE, { asarPath }) - nextTick(callback, [error]) - return - } - - const fileRealPath = archive.realpath(filePath) - if (fileRealPath === false) { - const error = createError(AsarError.NOT_FOUND, { asarPath, filePath }) - nextTick(callback, [error]) - return - } - - realpath(asarPath, options, (error, archiveRealPath) => { - if (error === null) { - const fullPath = path.join(archiveRealPath, fileRealPath) - callback(null, fullPath) - } else { - callback(error) - } - }) - } - - fs.realpath.native = function (pathArgument, options, callback) { - const { isAsar, asarPath, filePath } = splitPath(pathArgument) - if (!isAsar) return realpath.native.apply(this, arguments) - - if (arguments.length < 3) { - callback = options - options = {} - } - - const archive = getOrCreateArchive(asarPath) - if (!archive) { - const error = createError(AsarError.INVALID_ARCHIVE, { asarPath }) - nextTick(callback, [error]) - return - } - - const fileRealPath = archive.realpath(filePath) - if (fileRealPath === false) { - const error = createError(AsarError.NOT_FOUND, { asarPath, filePath }) - nextTick(callback, [error]) - return - } - - realpath.native(asarPath, options, (error, archiveRealPath) => { - if (error === null) { - const fullPath = path.join(archiveRealPath, fileRealPath) - callback(null, fullPath) - } else { - callback(error) - } - }) - } + fs.realpath = wrapRealpath(realpath) + fs.realpath.native = wrapRealpath(realpath.native) fs.promises.realpath = util.promisify(fs.realpath.native)