fix: override fs.realpathSync.native and fs.realpath.native (backport: 3-0-x) (#14051)

* fix: override fs.realpathSync.native and fs.realpath.native

* spec: test new native functions
This commit is contained in:
trop[bot] 2018-08-13 23:53:34 -07:00 committed by Shelley Vohr
parent 8eec8e150d
commit daf23288bc
2 changed files with 228 additions and 82 deletions

View file

@ -332,43 +332,68 @@
const {realpathSync} = fs const {realpathSync} = fs
fs.realpathSync = function (p) { fs.realpathSync = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p) const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) return realpathSync.apply(this, arguments)
return realpathSync.apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return invalidArchiveError(asarPath)
invalidArchiveError(asarPath)
}
const real = archive.realpath(filePath) const real = archive.realpath(filePath)
if (real === false) { if (real === false) notFoundError(asarPath, filePath)
notFoundError(asarPath, filePath)
}
return path.join(realpathSync(asarPath), real) return path.join(realpathSync(asarPath), real)
} }
fs.realpathSync.native = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) return realpathSync.native.apply(this, arguments)
const archive = getOrCreateArchive(asarPath)
if (!archive) return invalidArchiveError(asarPath)
const real = archive.realpath(filePath)
if (real === false) notFoundError(asarPath, filePath)
return path.join(realpathSync.native(asarPath), real)
}
const {realpath} = fs const {realpath} = fs
fs.realpath = function (p, cache, callback) { fs.realpath = function (p, cache, callback) {
const [isAsar, asarPath, filePath] = splitPath(p) const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) return realpath.apply(this, arguments)
return realpath.apply(this, arguments)
}
if (typeof cache === 'function') { if (typeof cache === 'function') {
callback = cache callback = cache
cache = void 0 cache = void 0
} }
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return invalidArchiveError(asarPath, callback)
return invalidArchiveError(asarPath, callback)
}
const real = archive.realpath(filePath) const real = archive.realpath(filePath)
if (real === false) { if (real === false) return notFoundError(asarPath, filePath, callback)
return notFoundError(asarPath, filePath, callback)
return realpath(asarPath, (err, p) => {
return (err) ? callback(err) : callback(null, path.join(p, real))
})
}
fs.realpath.native = function (p, cache, callback) {
const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) return realpath.native.apply(this, arguments)
if (typeof cache === 'function') {
callback = cache
cache = void 0
} }
return realpath(asarPath, function (err, p) {
if (err) { const archive = getOrCreateArchive(asarPath)
return callback(err) if (!archive) return invalidArchiveError(asarPath, callback)
}
return callback(null, path.join(p, real)) const real = archive.realpath(filePath)
if (real === false) return notFoundError(asarPath, filePath, callback)
return realpath.native(asarPath, (err, p) => {
return (err) ? callback(err) : callback(null, path.join(p, real))
}) })
} }

View file

@ -297,129 +297,250 @@ describe('asar package', function () {
}) })
}) })
describe('fs.realpathSync', function () { describe('fs.realpathSync', () => {
it('returns real path root', function () { it('returns real path root', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = 'a.asar' const p = 'a.asar'
var r = fs.realpathSync(path.join(parent, p)) const r = fs.realpathSync(path.join(parent, p))
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
}) })
it('returns real path of a normal file', function () { it('returns real path of a normal file', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'file1') const p = path.join('a.asar', 'file1')
var r = fs.realpathSync(path.join(parent, p)) const r = fs.realpathSync(path.join(parent, p))
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
}) })
it('returns real path of a normal directory', function () { it('returns real path of a normal directory', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'dir1') const p = path.join('a.asar', 'dir1')
var r = fs.realpathSync(path.join(parent, p)) const r = fs.realpathSync(path.join(parent, p))
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
}) })
it('returns real path of a linked file', function () { it('returns real path of a linked file', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'link2', 'link1') const p = path.join('a.asar', 'link2', 'link1')
var r = fs.realpathSync(path.join(parent, p)) const r = fs.realpathSync(path.join(parent, p))
assert.equal(r, path.join(parent, 'a.asar', 'file1')) assert.equal(r, path.join(parent, 'a.asar', 'file1'))
}) })
it('returns real path of a linked directory', function () { it('returns real path of a linked directory', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'link2', 'link2') const p = path.join('a.asar', 'link2', 'link2')
var r = fs.realpathSync(path.join(parent, p)) const r = fs.realpathSync(path.join(parent, p))
assert.equal(r, path.join(parent, 'a.asar', 'dir1')) assert.equal(r, path.join(parent, 'a.asar', 'dir1'))
}) })
it('returns real path of an unpacked file', function () { it('returns real path of an unpacked file', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('unpack.asar', 'a.txt') const p = path.join('unpack.asar', 'a.txt')
var r = fs.realpathSync(path.join(parent, p)) const r = fs.realpathSync(path.join(parent, p))
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
}) })
it('throws ENOENT error when can not find file', function () { it('throws ENOENT error when can not find file', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'not-exist') const p = path.join('a.asar', 'not-exist')
var throws = function () { const throws = () => fs.realpathSync(path.join(parent, p))
fs.realpathSync(path.join(parent, p))
}
assert.throws(throws, /ENOENT/) assert.throws(throws, /ENOENT/)
}) })
}) })
describe('fs.realpath', function () { describe('fs.realpathSync.native', () => {
it('returns real path root', function (done) { it('returns real path root', () => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
var p = 'a.asar' const p = 'a.asar'
fs.realpath(path.join(parent, p), function (err, r) { const r = fs.realpathSync.native(path.join(parent, p))
assert.equal(r, path.join(parent, p))
})
it('returns real path of a normal file', () => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'file1')
const r = fs.realpathSync.native(path.join(parent, p))
assert.equal(r, path.join(parent, p))
})
it('returns real path of a normal directory', () => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'dir1')
const r = fs.realpathSync.native(path.join(parent, p))
assert.equal(r, path.join(parent, p))
})
it('returns real path of a linked file', () => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'link2', 'link1')
const r = fs.realpathSync.native(path.join(parent, p))
assert.equal(r, path.join(parent, 'a.asar', 'file1'))
})
it('returns real path of a linked directory', () => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'link2', 'link2')
const r = fs.realpathSync.native(path.join(parent, p))
assert.equal(r, path.join(parent, 'a.asar', 'dir1'))
})
it('returns real path of an unpacked file', () => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('unpack.asar', 'a.txt')
const r = fs.realpathSync.native(path.join(parent, p))
assert.equal(r, path.join(parent, p))
})
it('throws ENOENT error when can not find file', () => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'not-exist')
const throws = () => fs.realpathSync.native(path.join(parent, p))
assert.throws(throws, /ENOENT/)
})
})
describe('fs.realpath', () => {
it('returns real path root', done => {
const parent = fs.realpathSync(path.join(fixtures, 'asar'))
const p = 'a.asar'
fs.realpath(path.join(parent, p), (err, r) => {
assert.equal(err, null) assert.equal(err, null)
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
done() done()
}) })
}) })
it('returns real path of a normal file', function (done) { it('returns real path of a normal file', done => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'file1') const p = path.join('a.asar', 'file1')
fs.realpath(path.join(parent, p), function (err, r) { fs.realpath(path.join(parent, p), (err, r) => {
assert.equal(err, null) assert.equal(err, null)
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
done() done()
}) })
}) })
it('returns real path of a normal directory', function (done) { it('returns real path of a normal directory', done => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'dir1') const p = path.join('a.asar', 'dir1')
fs.realpath(path.join(parent, p), function (err, r) { fs.realpath(path.join(parent, p), (err, r) => {
assert.equal(err, null) assert.equal(err, null)
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
done() done()
}) })
}) })
it('returns real path of a linked file', function (done) { it('returns real path of a linked file', done => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'link2', 'link1') const p = path.join('a.asar', 'link2', 'link1')
fs.realpath(path.join(parent, p), function (err, r) { fs.realpath(path.join(parent, p), (err, r) => {
assert.equal(err, null) assert.equal(err, null)
assert.equal(r, path.join(parent, 'a.asar', 'file1')) assert.equal(r, path.join(parent, 'a.asar', 'file1'))
done() done()
}) })
}) })
it('returns real path of a linked directory', function (done) { it('returns real path of a linked directory', done => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'link2', 'link2') const p = path.join('a.asar', 'link2', 'link2')
fs.realpath(path.join(parent, p), function (err, r) { fs.realpath(path.join(parent, p), (err, r) => {
assert.equal(err, null) assert.equal(err, null)
assert.equal(r, path.join(parent, 'a.asar', 'dir1')) assert.equal(r, path.join(parent, 'a.asar', 'dir1'))
done() done()
}) })
}) })
it('returns real path of an unpacked file', function (done) { it('returns real path of an unpacked file', done => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('unpack.asar', 'a.txt') const p = path.join('unpack.asar', 'a.txt')
fs.realpath(path.join(parent, p), function (err, r) { fs.realpath(path.join(parent, p), (err, r) => {
assert.equal(err, null) assert.equal(err, null)
assert.equal(r, path.join(parent, p)) assert.equal(r, path.join(parent, p))
done() done()
}) })
}) })
it('throws ENOENT error when can not find file', function (done) { it('throws ENOENT error when can not find file', done => {
var parent = fs.realpathSync(path.join(fixtures, 'asar')) const parent = fs.realpathSync(path.join(fixtures, 'asar'))
var p = path.join('a.asar', 'not-exist') const p = path.join('a.asar', 'not-exist')
fs.realpath(path.join(parent, p), function (err) { fs.realpath(path.join(parent, p), err => {
assert.equal(err.code, 'ENOENT') assert.equal(err.code, 'ENOENT')
done() done()
}) })
}) })
}) })
describe('fs.realpath.native', () => {
it('returns real path root', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = 'a.asar'
fs.realpath.native(path.join(parent, p), (err, r) => {
assert.equal(err, null)
assert.equal(r, path.join(parent, p))
done()
})
})
it('returns real path of a normal file', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'file1')
fs.realpath.native(path.join(parent, p), (err, r) => {
assert.equal(err, null)
assert.equal(r, path.join(parent, p))
done()
})
})
it('returns real path of a normal directory', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'dir1')
fs.realpath.native(path.join(parent, p), (err, r) => {
assert.equal(err, null)
assert.equal(r, path.join(parent, p))
done()
})
})
it('returns real path of a linked file', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'link2', 'link1')
fs.realpath.native(path.join(parent, p), (err, r) => {
assert.equal(err, null)
assert.equal(r, path.join(parent, 'a.asar', 'file1'))
done()
})
})
it('returns real path of a linked directory', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'link2', 'link2')
fs.realpath.native(path.join(parent, p), (err, r) => {
assert.equal(err, null)
assert.equal(r, path.join(parent, 'a.asar', 'dir1'))
done()
})
})
it('returns real path of an unpacked file', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('unpack.asar', 'a.txt')
fs.realpath.native(path.join(parent, p), (err, r) => {
assert.equal(err, null)
assert.equal(r, path.join(parent, p))
done()
})
})
it('throws ENOENT error when can not find file', done => {
const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
const p = path.join('a.asar', 'not-exist')
fs.realpath.native(path.join(parent, p), err => {
assert.equal(err.code, 'ENOENT')
done()
})
})
})
describe('fs.readdirSync', function () { describe('fs.readdirSync', function () {
it('reads dirs from root', function () { it('reads dirs from root', function () {
var p = path.join(fixtures, 'asar', 'a.asar') var p = path.join(fixtures, 'asar', 'a.asar')