Skip to content

Commit

Permalink
fix: fs.promises does not work with asar paths
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed May 1, 2019
1 parent 6f5c850 commit 7d21b45
Show file tree
Hide file tree
Showing 2 changed files with 288 additions and 15 deletions.
52 changes: 37 additions & 15 deletions lib/common/asar.js
Expand Up @@ -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)

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

const makePromiseFunction = function (orig, pathArgumentIndex) {
return function () {
const pathArgument = arguments[pathArgumentIndex]
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return orig.apply(this, arguments)

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

arguments[pathArgumentIndex] = newPath
return orig.apply(this, arguments)
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -365,6 +377,8 @@
})
}

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

fs.realpath.native = function (pathArgument, options, callback) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return realpath.native.apply(this, arguments)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7d21b45

Please sign in to comment.