fix: fs.promises does not work with asar paths (#18092)

This commit is contained in:
Milan Burda 2019-05-02 14:06:01 +02:00 committed by Cheng Zhao
parent 9585818a90
commit 2dd108e9c9
2 changed files with 292 additions and 14 deletions

View file

@ -198,24 +198,32 @@
}
if (old[util.promisify.custom]) {
module[name][util.promisify.custom] = function () {
const pathArgument = arguments[pathArgumentIndex]
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return old[util.promisify.custom].apply(this, arguments)
module[name][util.promisify.custom] = makePromiseFunction(old[util.promisify.custom], pathArgumentIndex)
}
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }))
}
if (module.promises && module.promises[name]) {
module.promises[name] = makePromiseFunction(module.promises[name], pathArgumentIndex)
}
}
const newPath = archive.copyFileOut(filePath)
if (!newPath) {
return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }))
}
const makePromiseFunction = function (orig, pathArgumentIndex) {
return function (...args) {
const pathArgument = args[pathArgumentIndex]
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return orig.apply(this, args)
arguments[pathArgumentIndex] = newPath
return old[util.promisify.custom].apply(this, arguments)
const archive = getOrCreateArchive(asarPath)
if (!archive) {
return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }))
}
const newPath = archive.copyFileOut(filePath)
if (!newPath) {
return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }))
}
args[pathArgumentIndex] = newPath
return orig.apply(this, args)
}
}
@ -274,6 +282,8 @@
nextTick(callback, [null, fsStats])
}
fs.promises.lstat = util.promisify(fs.lstat)
const { statSync } = fs
fs.statSync = (pathArgument, options) => {
const { isAsar } = splitPath(pathArgument)
@ -296,6 +306,8 @@
process.nextTick(() => fs.lstat(pathArgument, options, callback))
}
fs.promises.stat = util.promisify(fs.stat)
const { realpathSync } = fs
fs.realpathSync = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
@ -398,6 +410,8 @@
})
}
fs.promises.realpath = util.promisify(fs.realpath.native)
const { exists } = fs
fs.exists = (pathArgument, callback) => {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
@ -483,6 +497,8 @@
nextTick(callback)
}
fs.promises.access = util.promisify(fs.access)
const { accessSync } = fs
fs.accessSync = function (pathArgument, mode) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
@ -570,6 +586,8 @@
})
}
fs.promises.readFile = util.promisify(fs.readFile)
const { readFileSync } = fs
fs.readFileSync = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
@ -631,6 +649,8 @@
nextTick(callback, [null, files])
}
fs.promises.readdir = util.promisify(fs.readdir)
const { readdirSync } = fs
fs.readdirSync = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
@ -710,6 +730,8 @@
mkdir(pathArgument, options, callback)
}
fs.promises.mkdir = util.promisify(fs.mkdir)
const { mkdirSync } = fs
fs.mkdirSync = function (pathArgument, options) {
const { isAsar, filePath } = splitPath(pathArgument)