Skip to content

Commit

Permalink
Fix fs methods with options
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jan 6, 2025
1 parent 178857b commit 3d9d69f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
- name: Upload Binary Files
uses: actions/upload-artifact@v4
with:
name: yode-${{ matrix.os }}-${{ matrix.arch }}
path: out/Release/*.zip
retention-days: 1

Expand Down
75 changes: 48 additions & 27 deletions src/asar_monkey_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,29 @@ function overrideAPI(module, name, arg = 0) {
// Override fs APIs.
exports.wrapFsWithAsar = function(fs) {
const {lstatSync} = fs
fs.lstatSync = function(p) {
fs.lstatSync = function(p, options) {
const [isAsar, filePath] = splitPath(p)
if (!isAsar)
return lstatSync(p)
return lstatSync(p, options)
const stats = process.asarArchive.stat(filePath)
if (!stats)
notFoundError(filePath)
if (!stats) {
if (options?.throwIfNoEntry)
notFoundError(filePath)
else
return undefined
}
return generateStats(stats)
}

const {lstat} = fs
fs.lstat = function(p, callback) {
fs.lstat = function(p, options, callback) {
if (typeof options == 'function') {
callback = options
options = {}
}
const [isAsar, filePath] = splitPath(p)
if (!isAsar)
return lstat(p, callback)
return lstat(p, options, callback)
const stats = process.asarArchive.stat(filePath)
if (!stats)
return notFoundError(filePath, callback)
Expand All @@ -175,29 +183,33 @@ exports.wrapFsWithAsar = function(fs) {
fs.promises.lstat = util.promisify(fs.lstat)

const {statSync} = fs
fs.statSync = function(p) {
fs.statSync = function(p, options) {
const [isAsar] = splitPath(p)
if (!isAsar)
return statSync(p)
return statSync(p, options)
// Do not distinguish links for now.
return fs.lstatSync(p)
return fs.lstatSync(p, options)
}

const {stat} = fs
fs.stat = function(p, callback) {
fs.stat = function(p, options, callback) {
if (typeof options == 'function') {
callback = options
options = {}
}
const [isAsar] = splitPath(p)
if (!isAsar)
return stat(p, callback)
return stat(p, options, callback)
// Do not distinguish links for now.
process.nextTick(function() {
fs.lstat(p, callback)
fs.lstat(p, options, callback)
})
}

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

const wrapRealpathSync = function(func) {
return function(p) {
return function(p, options) {
const [isAsar, filePath] = splitPath(p)
if (!isAsar)
return func.apply(this, arguments)
Expand All @@ -208,7 +220,7 @@ exports.wrapFsWithAsar = function(fs) {
if (info.unpacked)
return real
else
return path.join(func(process.execPath), 'asar', real)
return path.join(func(process.execPath, options), 'asar', real)
}
}

Expand All @@ -217,13 +229,13 @@ exports.wrapFsWithAsar = function(fs) {
fs.realpathSync.native = wrapRealpathSync(realpathSync.native);

const wrapRealpath = function(func) {
return function(p, cache, callback) {
return function(p, options, callback) {
const [isAsar, filePath] = splitPath(p)
if (!isAsar)
return func.apply(this, arguments)
if (typeof cache === 'function') {
callback = cache
cache = undefined
if (arguments.length < 3) {
callback = options
options = {}
}
const info = process.asarArchive.getFileInfo(filePath)
if (!info)
Expand All @@ -232,7 +244,7 @@ exports.wrapFsWithAsar = function(fs) {
if (info.unpacked) {
callback(null, real)
} else {
func(process.execPath, function(err, p) {
func(process.execPath, options, function(err, p) {
if (err)
return callback(err)
return callback(null, path.join(p, 'asar', real))
Expand Down Expand Up @@ -419,10 +431,16 @@ exports.wrapFsWithAsar = function(fs) {
}

const {readdir} = fs
fs.readdir = function(p, callback) {
fs.readdir = function(p, options, callback) {
const [isAsar, filePath] = splitPath(p)
if (!isAsar)
return readdir.apply(this, arguments)
if (typeof options == 'function') {
callback = options
options = {}
} else if (typeof options == 'object') {
throw new Error('fs.readdir with options is not supported for ASAR')
}
const files = process.asarArchive.readdir(filePath)
if (!files)
return notFoundError(filePath, callback)
Expand All @@ -434,10 +452,12 @@ exports.wrapFsWithAsar = function(fs) {
fs.promises.readdir = util.promisify(fs.readdir)

const {readdirSync} = fs
fs.readdirSync = function(p) {
fs.readdirSync = function(p, options) {
const [isAsar, filePath] = splitPath(p)
if (!isAsar)
return readdirSync.apply(this, arguments)
if (typeof options == 'object')
throw new Error('fs.readdir with options is not supported for ASAR')
const files = process.asarArchive.readdir(filePath)
if (!files)
notFoundError(filePath)
Expand Down Expand Up @@ -482,22 +502,23 @@ exports.wrapFsWithAsar = function(fs) {
// widely used.
if (process.platform === 'win32') {
const {mkdir} = fs
fs.mkdir = function(p, mode, callback) {
if (typeof mode === 'function') {
callback = mode
fs.mkdir = function(p, options, callback) {
if (typeof options == 'function') {
callback = options
options = {}
}
const [isAsar, filePath] = splitPath(p)
if (isAsar && filePath.length)
return notDirError(callback)
mkdir(p, mode, callback)
mkdir(p, options, callback)
}

const {mkdirSync} = fs
fs.mkdirSync = function(p, mode) {
fs.mkdirSync = function(p, options) {
const [isAsar, filePath] = splitPath(p)
if (isAsar && filePath.length)
return notDirError()
return mkdirSync(p, mode)
return mkdirSync(p, options)
}
}

Expand Down

0 comments on commit 3d9d69f

Please sign in to comment.