refactor: eliminate duplicate code in asar.js (#18146)
This commit is contained in:
Родитель
02710ef574
Коммит
d79dc056bc
|
@ -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)
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче