Skip to content

Commit

Permalink
Allow specifying the offset of asar
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Dec 13, 2021
1 parent a6e1dc0 commit 9587596
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/asar_archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ const os = require('os')
const Pickle = require('pickle')

class AsarArchive {
constructor(asarPath) {
constructor(asarPath, offset = null) {
const fd = fs.openSync(asarPath, 'r')
try {
this.readExtendedMeta(fd, fs.statSync(asarPath))
if (offset)
this.contentOffset = offset
else
this.readExtendedMeta(fd, fs.statSync(asarPath))

// Read size.
let buffer = Buffer.alloc(8)
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function wrapWithActivateUvLoop(func) {
try {
// Is the executable concatenated with ASAR archive?
const AsarArchive = require('asar_archive')
process.asarArchive = new AsarArchive(execPath)
process.asarArchive = new AsarArchive(execPath/* REPLACE_WITH_OFFSET */)

// If it is (i.e. no exception), then patch the fs module after bootstrap
// is over.
Expand Down
25 changes: 23 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,44 @@ describe('node', function() {
assert.ok(result.stdout.toString().includes('fs.readFile(__filename'))
})

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

it('Promise can resolve', async () => {
await new Promise(resolve => setTimeout(resolve, 100))
})
})

function packageAndRun(asar) {
function packageAndRun(asar, modifyBinary = null) {
const a = path.join(__dirname, 'fixtures', asar + '.asar')
const p = path.join(__dirname, `${path.basename(asar, '.asar')}_${path.basename(process.execPath)}`)
fs.writeFileSync(p, fs.readFileSync(process.execPath))
if (modifyBinary)
modifyBinary(p)
fs.appendFileSync(p, fs.readFileSync(a))
appendMeta(p, a)
fs.chmodSync(p, 0o755)
const result = require('child_process').spawnSync(p)
fs.unlinkSync(p) // will be left for debugging if failed to run
if (result.status !== null)
fs.unlinkSync(p) // will be left for debugging if failed to run
return result
}

function changeOffset(target) {
const mark = '/* REPLACE_WITH_OFFSET */'
const data = fs.readFileSync(target)
const pos = data.indexOf(Buffer.from(mark))
if (pos <= 0)
throw new Error('Unable to find offset mark')
const stat = fs.statSync(target)
const replace = `, ${stat.size}`.padEnd(mark.length, ' ')
data.write(replace, pos)
fs.writeFileSync(target, data)
}

// Append ASAR meta information at end of target.
function appendMeta(target, asar) {
const stat = fs.statSync(asar)
Expand Down

0 comments on commit 9587596

Please sign in to comment.