Skip to content

Commit

Permalink
fix: fs.promises does not work with asar paths (#18115)
Browse files Browse the repository at this point in the history
  • Loading branch information
trop[bot] authored and codebytere committed May 3, 2019
1 parent 54199d2 commit 7eac676
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 15 deletions.
52 changes: 37 additions & 15 deletions lib/common/asar.js
Expand Up @@ -201,24 +201,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)

const archive = getOrCreateArchive(asarPath)
if (!archive) {
return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }))
}
module[name][util.promisify.custom] = makePromiseFunction(old[util.promisify.custom], pathArgumentIndex)
}

const newPath = archive.copyFileOut(filePath)
if (!newPath) {
return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }))
}
if (module.promises && module.promises[name]) {
module.promises[name] = makePromiseFunction(module.promises[name], pathArgumentIndex)
}
}

arguments[pathArgumentIndex] = newPath
return old[util.promisify.custom].apply(this, arguments)
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)

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)
}
}

Expand Down Expand Up @@ -277,6 +285,8 @@
nextTick(callback, [null, fsStats])
}

fs.promises.lstat = util.promisify(fs.lstat)

const { statSync } = fs
fs.statSync = (pathArgument, options) => {
const { isAsar } = splitPath(pathArgument)
Expand All @@ -299,6 +309,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)
Expand Down Expand Up @@ -401,6 +413,8 @@
})
}

fs.promises.realpath = util.promisify(fs.realpath.native)

const { exists } = fs
fs.exists = (pathArgument, callback) => {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
Expand Down Expand Up @@ -486,6 +500,8 @@
nextTick(callback)
}

fs.promises.access = util.promisify(fs.access)

const { accessSync } = fs
fs.accessSync = function (pathArgument, mode) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
Expand Down Expand Up @@ -573,6 +589,8 @@
})
}

fs.promises.readFile = util.promisify(fs.readFile)

const { readFileSync } = fs
fs.readFileSync = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
Expand Down Expand Up @@ -634,6 +652,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)
Expand Down Expand Up @@ -713,6 +733,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)
Expand Down

0 comments on commit 7eac676

Please sign in to comment.