Skip to content

Commit

Permalink
Fix fs.realpath not working on dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Mar 25, 2023
1 parent 6a79992 commit 4dd7ded
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/asar_archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class AsarArchive {
const node = this.getNode(filePath)
if (!node)
return null
if (node.files)
return null
if (node.files) // dir
return { path: filePath, unpacked: node.unpacked }
if (node.link)
return this.getFileInfo(node.link)
const info = { path: filePath, size: node.size }
Expand All @@ -96,6 +96,8 @@ class AsarArchive {
readFile(info) {
if (info.unpacked)
throw new Error('Should not use readFile for unpacked path')
if (typeof info.size != 'number')
throw new Error('readFile only works on file')
const buffer = Buffer.alloc(info.size)
const fd = fs.openSync(process.execPath, 'r')
try {
Expand All @@ -109,6 +111,8 @@ class AsarArchive {
}

copyFileOut(info) {
if (typeof info.size != 'number')
throw new Error('copyFileOut only works on file')
if (this.tmpFiles[info.path])
return this.tmpFiles[info.path]
if (info.unpacked)
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions test/asar_fs_realpath_dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('node:fs')
const path = require('node:path')

process.stdout.write(fs.realpathSync(path.join(__dirname, 'dir')))
process.exit(0)
17 changes: 12 additions & 5 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,29 @@ describe('node', function() {
assert.equal(result.stdout.toString().trim(), p)
})

it('start with asar with async fs', async () => {
it('start with asar with offset', async () => {
const result = await packageAndRun('fs_async', changeOffset)
assert.equal(result.status, 0)
assert.ok(result.stdout.toString().includes('fs.readFile(__filename'))
})

it('async fs works on asar', async () => {
const result = await packageAndRun('fs_async')
assert.equal(result.status, 0)
assert.ok(result.stdout.toString().includes('fs.readFile(__filename'))
})

it('start with asar with promise fs', async () => {
it('promise fs works on asar', async () => {
const result = await packageAndRun('fs_promise')
assert.equal(result.status, 0)
assert.ok(result.stdout.toString().includes('fs.readFile(__filename'))
})

it('start with asar with offset', async () => {
const result = await packageAndRun('fs_async', changeOffset)
it('fs.realpathSync works on dir in asar', async () => {
const result = await packageAndRun('fs_realpath_dir')
console.log(result.stderr.toString())
assert.equal(result.status, 0)
assert.ok(result.stdout.toString().includes('fs.readFile(__filename'))
assert.ok(result.stdout.toString().endsWith(path.join('asar', 'dir')));
})

it('Promise can resolve', async () => {
Expand Down

0 comments on commit 4dd7ded

Please sign in to comment.