Skip to content

Commit 3d9d69f

Browse files
committed
Fix fs methods with options
1 parent 178857b commit 3d9d69f

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
- name: Upload Binary Files
5757
uses: actions/upload-artifact@v4
5858
with:
59+
name: yode-${{ matrix.os }}-${{ matrix.arch }}
5960
path: out/Release/*.zip
6061
retention-days: 1
6162

src/asar_monkey_patch.js

+48-27
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,29 @@ function overrideAPI(module, name, arg = 0) {
149149
// Override fs APIs.
150150
exports.wrapFsWithAsar = function(fs) {
151151
const {lstatSync} = fs
152-
fs.lstatSync = function(p) {
152+
fs.lstatSync = function(p, options) {
153153
const [isAsar, filePath] = splitPath(p)
154154
if (!isAsar)
155-
return lstatSync(p)
155+
return lstatSync(p, options)
156156
const stats = process.asarArchive.stat(filePath)
157-
if (!stats)
158-
notFoundError(filePath)
157+
if (!stats) {
158+
if (options?.throwIfNoEntry)
159+
notFoundError(filePath)
160+
else
161+
return undefined
162+
}
159163
return generateStats(stats)
160164
}
161165

162166
const {lstat} = fs
163-
fs.lstat = function(p, callback) {
167+
fs.lstat = function(p, options, callback) {
168+
if (typeof options == 'function') {
169+
callback = options
170+
options = {}
171+
}
164172
const [isAsar, filePath] = splitPath(p)
165173
if (!isAsar)
166-
return lstat(p, callback)
174+
return lstat(p, options, callback)
167175
const stats = process.asarArchive.stat(filePath)
168176
if (!stats)
169177
return notFoundError(filePath, callback)
@@ -175,29 +183,33 @@ exports.wrapFsWithAsar = function(fs) {
175183
fs.promises.lstat = util.promisify(fs.lstat)
176184

177185
const {statSync} = fs
178-
fs.statSync = function(p) {
186+
fs.statSync = function(p, options) {
179187
const [isAsar] = splitPath(p)
180188
if (!isAsar)
181-
return statSync(p)
189+
return statSync(p, options)
182190
// Do not distinguish links for now.
183-
return fs.lstatSync(p)
191+
return fs.lstatSync(p, options)
184192
}
185193

186194
const {stat} = fs
187-
fs.stat = function(p, callback) {
195+
fs.stat = function(p, options, callback) {
196+
if (typeof options == 'function') {
197+
callback = options
198+
options = {}
199+
}
188200
const [isAsar] = splitPath(p)
189201
if (!isAsar)
190-
return stat(p, callback)
202+
return stat(p, options, callback)
191203
// Do not distinguish links for now.
192204
process.nextTick(function() {
193-
fs.lstat(p, callback)
205+
fs.lstat(p, options, callback)
194206
})
195207
}
196208

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

199211
const wrapRealpathSync = function(func) {
200-
return function(p) {
212+
return function(p, options) {
201213
const [isAsar, filePath] = splitPath(p)
202214
if (!isAsar)
203215
return func.apply(this, arguments)
@@ -208,7 +220,7 @@ exports.wrapFsWithAsar = function(fs) {
208220
if (info.unpacked)
209221
return real
210222
else
211-
return path.join(func(process.execPath), 'asar', real)
223+
return path.join(func(process.execPath, options), 'asar', real)
212224
}
213225
}
214226

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

219231
const wrapRealpath = function(func) {
220-
return function(p, cache, callback) {
232+
return function(p, options, callback) {
221233
const [isAsar, filePath] = splitPath(p)
222234
if (!isAsar)
223235
return func.apply(this, arguments)
224-
if (typeof cache === 'function') {
225-
callback = cache
226-
cache = undefined
236+
if (arguments.length < 3) {
237+
callback = options
238+
options = {}
227239
}
228240
const info = process.asarArchive.getFileInfo(filePath)
229241
if (!info)
@@ -232,7 +244,7 @@ exports.wrapFsWithAsar = function(fs) {
232244
if (info.unpacked) {
233245
callback(null, real)
234246
} else {
235-
func(process.execPath, function(err, p) {
247+
func(process.execPath, options, function(err, p) {
236248
if (err)
237249
return callback(err)
238250
return callback(null, path.join(p, 'asar', real))
@@ -419,10 +431,16 @@ exports.wrapFsWithAsar = function(fs) {
419431
}
420432

421433
const {readdir} = fs
422-
fs.readdir = function(p, callback) {
434+
fs.readdir = function(p, options, callback) {
423435
const [isAsar, filePath] = splitPath(p)
424436
if (!isAsar)
425437
return readdir.apply(this, arguments)
438+
if (typeof options == 'function') {
439+
callback = options
440+
options = {}
441+
} else if (typeof options == 'object') {
442+
throw new Error('fs.readdir with options is not supported for ASAR')
443+
}
426444
const files = process.asarArchive.readdir(filePath)
427445
if (!files)
428446
return notFoundError(filePath, callback)
@@ -434,10 +452,12 @@ exports.wrapFsWithAsar = function(fs) {
434452
fs.promises.readdir = util.promisify(fs.readdir)
435453

436454
const {readdirSync} = fs
437-
fs.readdirSync = function(p) {
455+
fs.readdirSync = function(p, options) {
438456
const [isAsar, filePath] = splitPath(p)
439457
if (!isAsar)
440458
return readdirSync.apply(this, arguments)
459+
if (typeof options == 'object')
460+
throw new Error('fs.readdir with options is not supported for ASAR')
441461
const files = process.asarArchive.readdir(filePath)
442462
if (!files)
443463
notFoundError(filePath)
@@ -482,22 +502,23 @@ exports.wrapFsWithAsar = function(fs) {
482502
// widely used.
483503
if (process.platform === 'win32') {
484504
const {mkdir} = fs
485-
fs.mkdir = function(p, mode, callback) {
486-
if (typeof mode === 'function') {
487-
callback = mode
505+
fs.mkdir = function(p, options, callback) {
506+
if (typeof options == 'function') {
507+
callback = options
508+
options = {}
488509
}
489510
const [isAsar, filePath] = splitPath(p)
490511
if (isAsar && filePath.length)
491512
return notDirError(callback)
492-
mkdir(p, mode, callback)
513+
mkdir(p, options, callback)
493514
}
494515

495516
const {mkdirSync} = fs
496-
fs.mkdirSync = function(p, mode) {
517+
fs.mkdirSync = function(p, options) {
497518
const [isAsar, filePath] = splitPath(p)
498519
if (isAsar && filePath.length)
499520
return notDirError()
500-
return mkdirSync(p, mode)
521+
return mkdirSync(p, options)
501522
}
502523
}
503524

0 commit comments

Comments
 (0)