2022-03-15 19:11:54 +00:00
|
|
|
const { join } = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const Module = require('module');
|
2022-04-03 21:27:05 +00:00
|
|
|
const { execFile } = require('child_process');
|
2022-04-22 17:41:15 +00:00
|
|
|
const { autoUpdater } = require('electron');
|
|
|
|
const request = require('request');
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const paths = require('../paths');
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const events = exports.events = new (require('events').EventEmitter)();
|
2022-03-20 12:33:24 +00:00
|
|
|
exports.INSTALLED_MODULE = 'installed-module'; // Fixes DiscordNative ensureModule as it uses export
|
2022-03-15 19:11:54 +00:00
|
|
|
|
2022-04-17 17:15:40 +00:00
|
|
|
let skipHost, skipModule,
|
2022-03-15 19:11:54 +00:00
|
|
|
remote = {},
|
|
|
|
installed = {},
|
|
|
|
downloading, installing,
|
2022-04-04 17:41:32 +00:00
|
|
|
basePath, manifestPath, downloadPath,
|
2022-03-15 19:11:54 +00:00
|
|
|
hostUpdater,
|
2022-04-21 11:25:17 +00:00
|
|
|
baseUrl, qs,
|
2022-04-22 20:31:42 +00:00
|
|
|
last;
|
2022-03-15 19:11:54 +00:00
|
|
|
|
|
|
|
const resetTracking = () => {
|
|
|
|
const base = {
|
|
|
|
done: 0,
|
|
|
|
total: 0,
|
|
|
|
fail: 0
|
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
downloading = Object.assign({}, base);
|
|
|
|
installing = Object.assign({}, base);
|
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-05 12:13:55 +00:00
|
|
|
exports.init = (endpoint, { releaseChannel, version }) => {
|
2022-03-15 19:11:54 +00:00
|
|
|
skipHost = settings.get('SKIP_HOST_UPDATE');
|
|
|
|
skipModule = settings.get('SKIP_MODULE_UPDATE');
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
basePath = join(paths.getUserDataVersioned(), 'modules');
|
|
|
|
manifestPath = join(basePath, 'installed.json');
|
|
|
|
downloadPath = join(basePath, 'pending');
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
resetTracking();
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
Module.globalPaths.push(basePath);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
// Purge pending
|
|
|
|
fs.rmSync(downloadPath, { recursive: true, force: true });
|
|
|
|
mkdirp.sync(downloadPath);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
|
|
|
try {
|
2022-03-15 19:11:54 +00:00
|
|
|
installed = JSON.parse(fs.readFileSync(manifestPath));
|
2022-04-17 17:15:40 +00:00
|
|
|
} catch {
|
2022-04-21 10:50:05 +00:00
|
|
|
for (const m of [ 'desktop_core', 'utils' ]) { // Ignore actual bootstrap manifest and choose our own core 2, others are deferred
|
2022-04-20 16:14:34 +00:00
|
|
|
installed['discord_' + m] = { installedVersion: 0 }; // Set initial version as 0
|
2022-04-17 17:15:40 +00:00
|
|
|
}
|
2021-12-09 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 11:23:59 +00:00
|
|
|
|
2022-04-22 17:41:15 +00:00
|
|
|
hostUpdater = process.platform === 'linux' ? new (class HostLinux extends require('events').EventEmitter {
|
|
|
|
setFeedURL(url) {
|
|
|
|
this.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkForUpdates() {
|
|
|
|
request(this.url, (e, r, b) => {
|
|
|
|
if (e) return this.emit('error');
|
|
|
|
|
|
|
|
if (r.statusCode === 204) return this.emit('update-not-available');
|
|
|
|
|
|
|
|
this.emit('update-manually', b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})() : autoUpdater;
|
|
|
|
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-22 20:19:53 +00:00
|
|
|
hostUpdater.on('update-progress', progress => events.emit('downloading-module', { name: 'host', progress }));
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
hostUpdater.on('update-not-available', hostPassed);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-22 09:11:57 +00:00
|
|
|
hostUpdater.on('update-manually', d => {
|
2022-03-15 19:11:54 +00:00
|
|
|
log('Modules', 'Host manual');
|
|
|
|
|
2022-04-22 20:25:15 +00:00
|
|
|
events.emit('manual', {
|
2022-04-22 09:11:57 +00:00
|
|
|
details: d
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
2022-03-15 19:11:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
hostUpdater.on('update-downloaded', () => {
|
|
|
|
events.emit('downloaded-module', {
|
2022-04-19 22:33:54 +00:00
|
|
|
name: 'host'
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
2022-03-15 19:11:54 +00:00
|
|
|
});
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
hostUpdater.on('error', () => {
|
|
|
|
log('Modules', 'Host error');
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-22 20:51:35 +00:00
|
|
|
events.emit('checked', { failed: true });
|
2022-03-15 19:11:54 +00:00
|
|
|
});
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 22:00:20 +00:00
|
|
|
const platform = process.platform === 'darwin' ? 'osx' : 'linux';
|
2022-04-04 07:50:32 +00:00
|
|
|
hostUpdater.setFeedURL(`${endpoint}/updates/${releaseChannel}?platform=${platform}&version=${version}`);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-04 07:50:32 +00:00
|
|
|
baseUrl = `${endpoint}/modules/${releaseChannel}`;
|
2022-04-21 11:25:17 +00:00
|
|
|
qs = {
|
2022-04-04 07:50:32 +00:00
|
|
|
host_version: version,
|
2022-03-15 22:00:20 +00:00
|
|
|
platform
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-23 09:54:57 +00:00
|
|
|
const hostPassed = (skip = skipModule) => {
|
2022-04-22 20:51:35 +00:00
|
|
|
if (skip) return events.emit('checked', {
|
|
|
|
count: 0
|
2022-03-15 19:11:54 +00:00
|
|
|
});
|
|
|
|
|
2022-03-23 09:54:57 +00:00
|
|
|
log('Modules', 'Host good');
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
checkModules();
|
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const checkModules = async () => {
|
2021-12-09 16:25:14 +00:00
|
|
|
try {
|
2022-04-21 16:06:34 +00:00
|
|
|
remote = await new Promise((res) => request({
|
2022-03-23 10:56:26 +00:00
|
|
|
url: baseUrl + '/versions.json',
|
2022-04-21 11:25:17 +00:00
|
|
|
qs
|
2022-04-21 16:06:34 +00:00
|
|
|
}, (e, r, b) => res(JSON.parse(b))));
|
2022-03-15 19:11:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('Modules', 'Check failed', e);
|
|
|
|
|
2022-04-22 20:51:35 +00:00
|
|
|
return events.emit('checked', {
|
|
|
|
failed: true
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
for (const name in installed) {
|
|
|
|
const inst = installed[name].installedVersion;
|
|
|
|
const rem = remote[name];
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
if (inst !== rem) {
|
2022-04-20 10:45:42 +00:00
|
|
|
log('Modules', 'Update:', name, inst, '->', rem);
|
2022-03-23 09:54:57 +00:00
|
|
|
|
|
|
|
downloadModule(name, rem);
|
2021-12-09 16:25:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 10:02:30 +00:00
|
|
|
events.emit('checked', {
|
2022-04-23 13:29:47 +00:00
|
|
|
count: downloading.total
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const downloadModule = async (name, ver) => {
|
|
|
|
downloading.total++;
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const path = join(downloadPath, name + '-' + ver + '.zip');
|
2022-04-21 17:37:31 +00:00
|
|
|
const file = fs.createWriteStream(path);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-21 16:06:34 +00:00
|
|
|
// log('Modules', 'Downloading', `${name}@${ver}`);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-21 16:06:34 +00:00
|
|
|
let success, total, cur = 0;
|
2022-04-21 17:37:31 +00:00
|
|
|
request({
|
|
|
|
url: baseUrl + '/' + name + '/' + ver,
|
|
|
|
qs
|
|
|
|
}).on('response', (res) => {
|
2022-04-21 16:06:34 +00:00
|
|
|
success = res.statusCode === 200;
|
|
|
|
total = parseInt(res.headers['content-length'] ?? 1, 10);
|
2022-03-15 19:11:54 +00:00
|
|
|
|
2022-04-21 17:37:31 +00:00
|
|
|
res.pipe(file);
|
2022-04-21 16:06:34 +00:00
|
|
|
|
|
|
|
res.on('data', c => {
|
|
|
|
cur += c.length;
|
|
|
|
|
2022-04-21 17:37:31 +00:00
|
|
|
events.emit('downloading-module', { name, cur, total });
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
2022-04-21 16:06:34 +00:00
|
|
|
});
|
|
|
|
|
2022-04-21 17:37:31 +00:00
|
|
|
await new Promise((res) => file.on('close', res));
|
2021-12-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
if (!installed[name]) installed[name] = {};
|
|
|
|
|
|
|
|
if (success) commitManifest();
|
|
|
|
else downloading.fail++;
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
events.emit('downloaded-module', {
|
2022-04-19 22:33:54 +00:00
|
|
|
name
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
|
|
|
|
downloading.done++;
|
|
|
|
|
|
|
|
if (downloading.done === downloading.total) {
|
2022-04-22 20:51:35 +00:00
|
|
|
events.emit('downloaded', {
|
2022-03-15 19:11:54 +00:00
|
|
|
failed: downloading.fail
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
installModule(name, ver, path);
|
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-03 21:27:05 +00:00
|
|
|
const installModule = async (name, ver, path) => {
|
2022-03-15 19:11:54 +00:00
|
|
|
installing.total++;
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-20 10:45:42 +00:00
|
|
|
// log('Modules', 'Installing', `${name}@${ver}`);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-23 11:01:44 +00:00
|
|
|
let hasError;
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-04 14:41:49 +00:00
|
|
|
const handleErr = e => {
|
2022-03-15 19:11:54 +00:00
|
|
|
if (hasError) return;
|
|
|
|
hasError = true;
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
log('Modules', 'Failed install', `${name}@${ver}`, e);
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-23 11:01:44 +00:00
|
|
|
finishInstall(name, ver, false);
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
2022-04-03 21:27:05 +00:00
|
|
|
// Extract zip via unzip cmd line - replaces yauzl dep (speed++, size--, jank++)
|
|
|
|
const ePath = join(basePath, name);
|
|
|
|
|
|
|
|
const total = await new Promise((res) => {
|
|
|
|
const p = execFile('unzip', ['-l', path]);
|
|
|
|
|
2022-04-04 14:41:49 +00:00
|
|
|
p.stdout.on('data', x => {
|
2022-04-03 21:27:05 +00:00
|
|
|
const m = x.toString().match(/([0-9]+) files/);
|
|
|
|
if (m) res(parseInt(m[1]));
|
|
|
|
});
|
|
|
|
|
|
|
|
p.stderr.on('data', res); // On error resolve undefined (??'d to 0)
|
|
|
|
}) ?? 0;
|
|
|
|
|
|
|
|
mkdirp.sync(ePath);
|
|
|
|
|
|
|
|
const proc = execFile('unzip', ['-o', path, '-d', ePath]);
|
|
|
|
|
|
|
|
proc.on('error', (err) => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
require('electron').dialog.showErrorBox('Failed Dependency', 'Please install "unzip"');
|
|
|
|
process.exit(1); // Close now
|
|
|
|
}
|
|
|
|
|
|
|
|
handleErr(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.stderr.on('data', handleErr);
|
|
|
|
|
2022-04-04 14:19:27 +00:00
|
|
|
let cur = 0;
|
2022-04-04 14:41:49 +00:00
|
|
|
proc.stdout.on('data', x => x.toString().split('\n').forEach(y => {
|
|
|
|
if (!y.includes('inflating')) return;
|
2022-04-03 21:27:05 +00:00
|
|
|
|
2022-04-04 14:19:27 +00:00
|
|
|
cur++;
|
2022-04-21 17:37:31 +00:00
|
|
|
events.emit('installing-module', { name, cur, total });
|
2022-04-03 21:27:05 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
proc.on('close', () => {
|
|
|
|
if (hasError) return;
|
2022-03-19 12:31:39 +00:00
|
|
|
|
2022-04-03 21:27:05 +00:00
|
|
|
installed[name].installedVersion = ver;
|
|
|
|
commitManifest();
|
2022-03-19 12:31:39 +00:00
|
|
|
|
2022-04-03 21:27:05 +00:00
|
|
|
finishInstall(name, ver, true);
|
|
|
|
});
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const finishInstall = (name, ver, success) => {
|
|
|
|
if (!success) installing.fail++;
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
events.emit('installed-module', {
|
2022-04-20 10:45:42 +00:00
|
|
|
name,
|
|
|
|
succeeded: true
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
installing.done++;
|
2022-03-16 10:40:58 +00:00
|
|
|
log('Modules', 'Finished', `${name}@${ver}`);
|
2022-03-15 19:11:54 +00:00
|
|
|
|
2022-04-20 10:45:42 +00:00
|
|
|
if (installing.done === downloading.total) {
|
2022-04-20 18:44:58 +00:00
|
|
|
if (!installing.fail) last = Date.now();
|
2022-03-23 09:54:57 +00:00
|
|
|
|
2022-04-22 20:51:35 +00:00
|
|
|
events.emit('installed', {
|
2022-03-15 19:11:54 +00:00
|
|
|
failed: installing.fail
|
2021-12-09 16:25:14 +00:00
|
|
|
});
|
2022-03-15 19:11:54 +00:00
|
|
|
|
|
|
|
resetTracking();
|
2021-12-09 16:25:14 +00:00
|
|
|
}
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
exports.checkForUpdates = () => {
|
|
|
|
log('Modules', 'Checking');
|
|
|
|
|
2022-04-04 07:55:48 +00:00
|
|
|
events.emit('checking-for-updates');
|
2022-03-23 09:54:57 +00:00
|
|
|
|
2022-04-20 21:45:17 +00:00
|
|
|
if (skipHost) hostPassed();
|
|
|
|
else if (last > Date.now() - 10000) hostPassed(true);
|
2022-04-04 07:55:48 +00:00
|
|
|
else hostUpdater.checkForUpdates();
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-22 17:10:07 +00:00
|
|
|
exports.quitAndInstallUpdates = () => hostUpdater.quitAndInstall();
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-04-22 20:51:35 +00:00
|
|
|
exports.isInstalled = (n, v) => installed[n] && !(v && installed[n].installedVersion !== v);
|
2022-03-15 19:11:54 +00:00
|
|
|
exports.getInstalled = () => ({ ...installed });
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
const commitManifest = () => fs.writeFileSync(manifestPath, JSON.stringify(installed, null, 2));
|
2021-12-09 16:25:14 +00:00
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
exports.install = (name, def, { version } = {}) => {
|
2022-04-22 20:51:35 +00:00
|
|
|
if (exports.isInstalled(name, version)) {
|
2022-03-15 19:11:54 +00:00
|
|
|
if (!def) events.emit('installed-module', {
|
|
|
|
name,
|
|
|
|
succeeded: true
|
|
|
|
});
|
2021-12-09 16:25:14 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-15 19:11:54 +00:00
|
|
|
if (def) {
|
2022-03-18 22:49:04 +00:00
|
|
|
installed[name] = { installedVersion: 0 };
|
2022-03-15 19:11:54 +00:00
|
|
|
return commitManifest();
|
2021-12-09 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 22:52:55 +00:00
|
|
|
downloadModule(name, version ?? remote[name] ?? 0);
|
2022-03-15 19:11:54 +00:00
|
|
|
};
|