Browse Source

refactor: eliminate duplicate code in asar.js (#18146)

Milan Burda 6 years ago
parent
commit
d79dc056bc
1 changed files with 48 additions and 88 deletions
  1. 48 88
      lib/common/asar.js

+ 48 - 88
lib/common/asar.js

@@ -311,108 +311,68 @@
 
     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 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)
-    }
-
-    fs.realpathSync.native = function (pathArgument, options) {
-      const { isAsar, asarPath, filePath } = splitPath(pathArgument)
-      if (!isAsar) return realpathSync.native.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 })
+        }
 
-      const fileRealPath = archive.realpath(filePath)
-      if (fileRealPath === false) {
-        throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
+        return path.join(realpathSync(asarPath, options), fileRealPath)
       }
-
-      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 { realpathSync } = fs
+    fs.realpathSync = wrapRealpathSync(realpathSync)
+    fs.realpathSync.native = wrapRealpathSync(realpathSync.native)
 
-      const fileRealPath = archive.realpath(filePath)
-      if (fileRealPath === false) {
-        const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
-        nextTick(callback, [error])
-        return
-      }
+    const wrapRealpath = function (realpath) {
+      return function (pathArgument, options, callback) {
+        const { isAsar, asarPath, filePath } = splitPath(pathArgument)
+        if (!isAsar) return realpath.apply(this, arguments)
 
-      realpath(asarPath, options, (error, archiveRealPath) => {
-        if (error === null) {
-          const fullPath = path.join(archiveRealPath, fileRealPath)
-          callback(null, fullPath)
-        } else {
-          callback(error)
+        if (arguments.length < 3) {
+          callback = options
+          options = {}
         }
-      })
-    }
 
-    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 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
+        }
 
-      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)
+          }
+        })
       }
-
-      realpath.native(asarPath, options, (error, archiveRealPath) => {
-        if (error === null) {
-          const fullPath = path.join(archiveRealPath, fileRealPath)
-          callback(null, fullPath)
-        } else {
-          callback(error)
-        }
-      })
     }
 
+    const { realpath } = fs
+    fs.realpath = wrapRealpath(realpath)
+    fs.realpath.native = wrapRealpath(realpath.native)
+
     fs.promises.realpath = util.promisify(fs.realpath.native)
 
     const { exists } = fs