refactor: eliminate duplicate code in asar.js (#18146)
This commit is contained in:
		
					parent
					
						
							
								02710ef574
							
						
					
				
			
			
				commit
				
					
						d79dc056bc
					
				
			
		
					 1 changed files with 52 additions and 92 deletions
				
			
		|  | @ -311,107 +311,67 @@ | ||||||
| 
 | 
 | ||||||
|     fs.promises.stat = util.promisify(fs.stat) |     fs.promises.stat = util.promisify(fs.stat) | ||||||
| 
 | 
 | ||||||
|     const { realpathSync } = fs |     const wrapRealpathSync = function (realpathSync) { | ||||||
|     fs.realpathSync = function (pathArgument, options) { |       return function (pathArgument, options) { | ||||||
|       const { isAsar, asarPath, filePath } = splitPath(pathArgument) |         const { isAsar, asarPath, filePath } = splitPath(pathArgument) | ||||||
|       if (!isAsar) return realpathSync.apply(this, arguments) |         if (!isAsar) return realpathSync.apply(this, arguments) | ||||||
| 
 | 
 | ||||||
|       const archive = getOrCreateArchive(asarPath) |         const archive = getOrCreateArchive(asarPath) | ||||||
|       if (!archive) { |         if (!archive) { | ||||||
|         throw createError(AsarError.INVALID_ARCHIVE, { asarPath }) |           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 { realpathSync } = fs | ||||||
|       const { isAsar, asarPath, filePath } = splitPath(pathArgument) |     fs.realpathSync = wrapRealpathSync(realpathSync) | ||||||
|       if (!isAsar) return realpathSync.native.apply(this, arguments) |     fs.realpathSync.native = wrapRealpathSync(realpathSync.native) | ||||||
| 
 | 
 | ||||||
|       const archive = getOrCreateArchive(asarPath) |     const wrapRealpath = function (realpath) { | ||||||
|       if (!archive) { |       return function (pathArgument, options, callback) { | ||||||
|         throw createError(AsarError.INVALID_ARCHIVE, { asarPath }) |         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 |     const { realpath } = fs | ||||||
|     fs.realpath = function (pathArgument, options, callback) { |     fs.realpath = wrapRealpath(realpath) | ||||||
|       const { isAsar, asarPath, filePath } = splitPath(pathArgument) |     fs.realpath.native = wrapRealpath(realpath.native) | ||||||
|       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.promises.realpath = util.promisify(fs.realpath.native) |     fs.promises.realpath = util.promisify(fs.realpath.native) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Milan Burda
				Milan Burda