From 9587596437e15bb43a49180aea122276de0159ee Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 13 Dec 2021 13:35:10 +0900 Subject: [PATCH] Allow specifying the offset of asar --- src/asar_archive.js | 7 +++++-- src/bootstrap.js | 2 +- test.js | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/asar_archive.js b/src/asar_archive.js index eb2e6af..05060f4 100644 --- a/src/asar_archive.js +++ b/src/asar_archive.js @@ -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) diff --git a/src/bootstrap.js b/src/bootstrap.js index 5147165..73f6c2a 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -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. diff --git a/test.js b/test.js index c1eb156..3780084 100755 --- a/test.js +++ b/test.js @@ -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)