Skip to content

Commit

Permalink
asarUpdate: rewrite to use async and no temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
CanadaHonk committed Dec 11, 2022
1 parent edb6a5b commit 0b1d468
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/asarUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const fs = require('original-fs'); // Use original-fs, not Electron's modified f
const { join } = require('path');

const asarPath = join(require.main.filename, '..');
const downloadPath = join(asarPath, '..', 'app.asar.download');

const asarUrl = `https://github.com/GooseMod/OpenAsar/releases/download/${oaVersion.split('-')[0]}/app.asar`;

Expand All @@ -16,17 +15,20 @@ const redirs = url => new Promise(res => get(url, r => { // Minimal wrapper arou
}));

module.exports = async () => { // (Try) update asar
log('AsarUpdate', 'Updating...');

if (!oaVersion.includes('-')) return;
log('AsarUpdate', 'Updating...');

const file = fs.createWriteStream(downloadPath);
(await redirs(asarUrl)).pipe(file);
const res = (await redirs(asarUrl));

await new Promise(res => file.on('finish', res));
let data = [];
res.on('data', d => {
data.push(d);
});

if (fs.readFileSync(downloadPath, 'utf8').startsWith('<')) return log('AsarUpdate', 'Download error');
res.on('end', () => {
const buf = Buffer.concat(data);
if (!buf.toString('hex').startsWith('04000000')) return log('AsarUpdate', 'Download error'); // Not like ASAR header

fs.copyFileSync(downloadPath, asarPath); // Overwrite actual app.asar
fs.unlinkSync(downloadPath); // Delete downloaded temp file
fs.writeFile(asarPath, buf, e => log('AsarUpdate', 'Downloaded', e ?? ''));
});
};

0 comments on commit 0b1d468

Please sign in to comment.