Add asar-supported fs.access implementation
This commit is contained in:
parent
a518c47f4c
commit
3ad5504194
2 changed files with 73 additions and 0 deletions
|
@ -126,6 +126,19 @@
|
|||
})
|
||||
}
|
||||
|
||||
// Create a EACCES error.
|
||||
const accessError = function (asarPath, filePath, callback) {
|
||||
const error = new Error(`EACCES: permission denied, access '${filePath}'`)
|
||||
error.code = 'EACCES'
|
||||
error.errno = -13
|
||||
if (typeof callback !== 'function') {
|
||||
throw error
|
||||
}
|
||||
process.nextTick(function () {
|
||||
callback(error)
|
||||
})
|
||||
}
|
||||
|
||||
// Create invalid archive error.
|
||||
const invalidArchiveError = function (asarPath, callback) {
|
||||
const error = new Error(`Invalid package ${asarPath}`)
|
||||
|
@ -362,6 +375,40 @@
|
|||
return archive.stat(filePath) !== false
|
||||
}
|
||||
|
||||
const {access} = fs
|
||||
fs.access = function (p, mode, callback) {
|
||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||
if (!isAsar) {
|
||||
return access.apply(this, arguments)
|
||||
}
|
||||
if (typeof mode === 'function') {
|
||||
callback = mode
|
||||
mode = fs.constants.F_OK
|
||||
}
|
||||
const archive = getOrCreateArchive(asarPath)
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback)
|
||||
}
|
||||
const info = archive.getFileInfo(filePath)
|
||||
if (!info) {
|
||||
return notFoundError(asarPath, filePath, callback)
|
||||
}
|
||||
if (info.unpacked) {
|
||||
const realPath = archive.copyFileOut(filePath)
|
||||
return fs.access(realPath, mode, callback)
|
||||
}
|
||||
const stats = getOrCreateArchive(asarPath).stat(filePath)
|
||||
if (!stats) {
|
||||
return notFoundError(asarPath, filePath, callback)
|
||||
}
|
||||
if (mode & fs.constants.W_OK) {
|
||||
return accessError(asarPath, filePath, callback)
|
||||
}
|
||||
process.nextTick(function () {
|
||||
callback()
|
||||
})
|
||||
}
|
||||
|
||||
const {readFile} = fs
|
||||
fs.readFile = function (p, options, callback) {
|
||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue