Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fs.promises does not work with ASAR paths #18115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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