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: update fs methods for options param (backport: 4-0-x) #15350

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
80 changes: 45 additions & 35 deletions lib/common/asar.js
Expand Up @@ -238,9 +238,9 @@
}

const { lstatSync } = fs
fs.lstatSync = pathArgument => {
fs.lstatSync = (pathArgument, options) => {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return lstatSync(pathArgument)
if (!isAsar) return lstatSync(pathArgument, options)

const archive = getOrCreateArchive(asarPath)
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
Expand All @@ -252,9 +252,13 @@
}

const { lstat } = fs
fs.lstat = (pathArgument, callback) => {
fs.lstat = function (pathArgument, options, callback) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return lstat(pathArgument, callback)
if (typeof options === 'function') {
callback = options
options = {}
}
if (!isAsar) return lstat(pathArgument, options, callback)

const archive = getOrCreateArchive(asarPath)
if (!archive) {
Expand All @@ -275,25 +279,29 @@
}

const { statSync } = fs
fs.statSync = pathArgument => {
fs.statSync = (pathArgument, options) => {
const { isAsar } = splitPath(pathArgument)
if (!isAsar) return statSync(pathArgument)
if (!isAsar) return statSync(pathArgument, options)

// Do not distinguish links for now.
return fs.lstatSync(pathArgument)
return fs.lstatSync(pathArgument, options)
}

const { stat } = fs
fs.stat = (pathArgument, callback) => {
fs.stat = (pathArgument, options, callback) => {
const { isAsar } = splitPath(pathArgument)
if (!isAsar) return stat(pathArgument, callback)
if (typeof options === 'function') {
callback = options
options = {}
}
if (!isAsar) return stat(pathArgument, options, callback)

// Do not distinguish links for now.
process.nextTick(() => fs.lstat(pathArgument, callback))
process.nextTick(() => fs.lstat(pathArgument, options, callback))
}

const { realpathSync } = fs
fs.realpathSync = function (pathArgument) {
fs.realpathSync = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return realpathSync.apply(this, arguments)

Expand All @@ -307,10 +315,10 @@
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
}

return path.join(realpathSync(asarPath), fileRealPath)
return path.join(realpathSync(asarPath, options), fileRealPath)
}

fs.realpathSync.native = function (pathArgument) {
fs.realpathSync.native = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return realpathSync.native.apply(this, arguments)

Expand All @@ -324,17 +332,17 @@
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
}

return path.join(realpathSync.native(asarPath), fileRealPath)
return path.join(realpathSync.native(asarPath, options), fileRealPath)
}

const { realpath } = fs
fs.realpath = function (pathArgument, cache, callback) {
fs.realpath = function (pathArgument, options, callback) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return realpath.apply(this, arguments)

if (typeof cache === 'function') {
callback = cache
cache = undefined
if (arguments.length < 3) {
callback = options
options = {}
}

const archive = getOrCreateArchive(asarPath)
Expand All @@ -351,7 +359,7 @@
return
}

realpath(asarPath, (error, archiveRealPath) => {
realpath(asarPath, options, (error, archiveRealPath) => {
if (error === null) {
const fullPath = path.join(archiveRealPath, fileRealPath)
callback(null, fullPath)
Expand All @@ -361,13 +369,13 @@
})
}

fs.realpath.native = function (pathArgument, cache, callback) {
fs.realpath.native = function (pathArgument, options, callback) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return realpath.native.apply(this, arguments)

if (typeof cache === 'function') {
callback = cache
cache = undefined
if (arguments.length < 3) {
callback = options
options = {}
}

const archive = getOrCreateArchive(asarPath)
Expand All @@ -384,7 +392,7 @@
return
}

realpath.native(asarPath, (error, archiveRealPath) => {
realpath.native(asarPath, options, (error, archiveRealPath) => {
if (error === null) {
const fullPath = path.join(archiveRealPath, fileRealPath)
callback(null, fullPath)
Expand Down Expand Up @@ -602,8 +610,12 @@
}

const { readdir } = fs
fs.readdir = function (pathArgument, callback) {
fs.readdir = function (pathArgument, options, callback) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (typeof options === 'function') {
callback = options
options = {}
}
if (!isAsar) return readdir.apply(this, arguments)

const archive = getOrCreateArchive(asarPath)
Expand All @@ -624,7 +636,7 @@
}

const { readdirSync } = fs
fs.readdirSync = function (pathArgument) {
fs.readdirSync = function (pathArgument, options) {
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
if (!isAsar) return readdirSync.apply(this, arguments)

Expand Down Expand Up @@ -684,14 +696,12 @@

// Calling mkdir for directory inside asar archive should throw ENOTDIR
// error, but on Windows it throws ENOENT.
// This is to work around the recursive looping bug of mkdirp since it is
// widely used.
if (process.platform === 'win32') {
const { mkdir } = fs
fs.mkdir = (pathArgument, mode, callback) => {
if (typeof mode === 'function') {
callback = mode
mode = undefined
fs.mkdir = (pathArgument, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

const { isAsar, filePath } = splitPath(pathArgument)
Expand All @@ -701,14 +711,14 @@
return
}

mkdir(pathArgument, mode, callback)
mkdir(pathArgument, options, callback)
}

const { mkdirSync } = fs
fs.mkdirSync = function (pathArgument, mode) {
fs.mkdirSync = function (pathArgument, options) {
const { isAsar, filePath } = splitPath(pathArgument)
if (isAsar && filePath.length) throw createError(AsarError.NOT_DIR)
return mkdirSync(pathArgument, mode)
return mkdirSync(pathArgument, options)
}
}

Expand Down
21 changes: 21 additions & 0 deletions spec/asar-spec.js
Expand Up @@ -192,6 +192,15 @@ describe('asar package', function () {
assert.strictEqual(stats.size, 0)
})

it('returns information of root with stats as bigint', function () {
const p = path.join(fixtures, 'asar', 'a.asar')
const stats = fs.lstatSync(p, { bigint: false })
assert.strictEqual(stats.isFile(), false)
assert.strictEqual(stats.isDirectory(), true)
assert.strictEqual(stats.isSymbolicLink(), false)
assert.strictEqual(stats.size, 0)
})

it('returns information of a normal file', function () {
const ref2 = ['file1', 'file2', 'file3', path.join('dir1', 'file1'), path.join('link2', 'file1')]
for (let j = 0, len = ref2.length; j < len; j++) {
Expand Down Expand Up @@ -275,6 +284,18 @@ describe('asar package', function () {
})
})

it('returns information of root with stats as bigint', function (done) {
const p = path.join(fixtures, 'asar', 'a.asar')
fs.lstat(p, { bigint: false }, function (err, stats) {
assert.strictEqual(err, null)
assert.strictEqual(stats.isFile(), false)
assert.strictEqual(stats.isDirectory(), true)
assert.strictEqual(stats.isSymbolicLink(), false)
assert.strictEqual(stats.size, 0)
done()
})
})

it('returns information of a normal file', function (done) {
const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'file1')
fs.lstat(p, function (err, stats) {
Expand Down