Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/asarUpdate.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
const { get } = require('https');
const get = require('./utils/get');
const fs = require('original-fs'); // Use original-fs, not Electron's modified fs
const { join } = require('path');

const asarPath = join(__filename, '..');

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

// todo: have these https utils centralised?
const redirs = url => new Promise(res => get(url, r => { // Minimal wrapper around https.get to follow redirects
const loc = r.headers.location;
if (loc) return redirs(loc).then(res);

res(r);
}));

module.exports = async () => { // (Try) update asar
if (!oaVersion.includes('-')) return;
log('AsarUpdate', 'Updating...');

const res = (await redirs(asarUrl));

let data = [];
res.on('data', d => {
data.push(d);
});
const buf = (await get(asarUrl))[1];

res.on('end', () => {
const buf = Buffer.concat(data);
if (!buf.toString('hex').startsWith('04000000')) return log('AsarUpdate', 'Download error'); // Not like ASAR header
if (!buf || !buf.toString('hex').startsWith('04000000')) return log('AsarUpdate', 'Download error'); // Request failed or ASAR header not present

fs.writeFile(asarPath, buf, e => log('AsarUpdate', 'Downloaded', e ?? ''));
});
fs.writeFile(asarPath, buf, e => log('AsarUpdate', 'Downloaded', e ?? ''));
};
58 changes: 22 additions & 36 deletions src/updater/moduleUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const Module = require('module');
const { execFile } = require('child_process');
const { app, autoUpdater } = require('electron');
const { get } = require('https');
const get = require('../utils/get');

const paths = require('../paths');

Expand Down Expand Up @@ -32,20 +32,6 @@ const resetTracking = () => {
installing = Object.assign({}, base);
};

const req = url => new Promise(res => get(url, r => { // Minimal wrapper around https.get to include body
let dat = '';
r.on('data', b => dat += b.toString());

r.on('end', () => res([ r, dat ]));
}));

const redirs = url => new Promise(res => get(url, r => { // Minimal wrapper around https.get to follow redirects
const loc = r.headers.location;
if (loc) return redirs(loc).then(res);

res(r);
}));

exports.init = (endpoint, { releaseChannel, version }) => {
skipHost = settings.get('SKIP_HOST_UPDATE');
skipModule = settings.get('SKIP_MODULE_UPDATE');
Expand Down Expand Up @@ -77,10 +63,10 @@ exports.init = (endpoint, { releaseChannel, version }) => {
}

checkForUpdates() {
req(this.url).then(([ r, b ]) => {
if (r.statusCode === 204) return this.emit('update-not-available');
get(this.url).then(([r, b, _headers]) => {
if (!b || r === 204) return this.emit('update-not-available');

this.emit('update-manually', b);
this.emit('update-manually', b.toString());
});
}

Expand Down Expand Up @@ -111,7 +97,12 @@ exports.init = (endpoint, { releaseChannel, version }) => {
};

const checkModules = async () => {
remote = JSON.parse((await req(baseUrl + '/versions.json' + qs))[1]);
const buf = (await get(baseUrl + '/versions.json' + qs))[1];
if (!buf) {
log('Modules', 'versions.json retrieval failure.');
return;
}
remote = JSON.parse(buf.toString());

for (const name in installed) {
const inst = installed[name].installedVersion;
Expand All @@ -131,27 +122,22 @@ const downloadModule = async (name, ver) => {
downloading.total++;

const path = join(downloadPath, name + '-' + ver + '.zip');
const file = fs.createWriteStream(path);

// log('Modules', 'Downloading', `${name}@${ver}`);

let success, total, cur = 0;
const res = await redirs(baseUrl + '/' + name + '/' + ver + qs);
success = res.statusCode === 200;
total = parseInt(res.headers['content-length'] ?? 1, 10);

res.pipe(file);
let success, total, cur = 0;
const res = await get(baseUrl + '/' + name + '/' + ver + qs);
success = res[0] === 200;

res.on('data', c => {
cur += c.length;

events.emit('downloading-module', { name, cur, total });
});

await new Promise((res) => file.on('close', res));

if (success) commitManifest();
else downloading.fail++;
// todo: if a progress-bar-like interface and stream-like file writing are still wanted, implement
if (success) {
total = parseInt(res[2].get('content-length') ?? 1, 10);
events.emit('downloading-module', { name, total, total });
fs.writeFile(path, res[1], e => log('ModuleUpdate', 'Writing to file failed:', e));
commitManifest();
} else {
downloading.fail++;
}

events.emit('downloaded-module', {
name
Expand Down
17 changes: 17 additions & 0 deletions src/utils/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { net } = require('electron');

// returns a promise that resolves to [statusCode, Buffer, headers]
// [code, null, null] if request failed
module.exports = async (url) => {
const request = new Request(url, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this use net.request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can review the current design choice I made. A get utility returning Promises that resolves to response data, and a more sophisticated request utility if more fine-grained control is needed.

method: 'GET',
redirect: 'follow'
});
const response = await net.fetch(request);

if (response.ok) {
return [response.status, Buffer.from(await response.arrayBuffer()), response.headers];
} else {
return [response.status, null, null];
}
};