[Updater > Module] Rewrite to use Mu api
This commit is contained in:
parent
3935af3bdc
commit
dfba43512a
2 changed files with 27 additions and 42 deletions
|
@ -3,6 +3,8 @@ const fs = require('fs');
|
||||||
const mkdirp = require('mkdirp');
|
const mkdirp = require('mkdirp');
|
||||||
const Module = require('module');
|
const Module = require('module');
|
||||||
const { execFile } = require('child_process');
|
const { execFile } = require('child_process');
|
||||||
|
const { Readable, Writable } = require('stream');
|
||||||
|
const zlib = require('zlib');
|
||||||
|
|
||||||
const paths = require('../paths');
|
const paths = require('../paths');
|
||||||
const request = require('./request');
|
const request = require('./request');
|
||||||
|
@ -16,7 +18,7 @@ let skipHost, skipModule,
|
||||||
downloading, installing,
|
downloading, installing,
|
||||||
basePath, manifestPath, downloadPath,
|
basePath, manifestPath, downloadPath,
|
||||||
hostUpdater,
|
hostUpdater,
|
||||||
baseUrl, baseQuery,
|
baseUrl,
|
||||||
checking, hostAvail, last;
|
checking, hostAvail, last;
|
||||||
|
|
||||||
const resetTracking = () => {
|
const resetTracking = () => {
|
||||||
|
@ -112,11 +114,10 @@ exports.init = (endpoint, { releaseChannel, version }) => {
|
||||||
const platform = process.platform === 'darwin' ? 'osx' : 'linux';
|
const platform = process.platform === 'darwin' ? 'osx' : 'linux';
|
||||||
hostUpdater.setFeedURL(`${endpoint}/updates/${releaseChannel}?platform=${platform}&version=${version}`);
|
hostUpdater.setFeedURL(`${endpoint}/updates/${releaseChannel}?platform=${platform}&version=${version}`);
|
||||||
|
|
||||||
baseUrl = `${endpoint}/modules/${releaseChannel}`;
|
// endpoint = 'https://cdn.jsdelivr.net/gh/openasar/mu@gh-pages';
|
||||||
baseQuery = {
|
// endpoint = 'https://openasar.dev/Mu';
|
||||||
host_version: version,
|
endpoint = 'https://mu.openasar.dev'
|
||||||
platform
|
baseUrl = `${endpoint}/${platform}/${releaseChannel}`;
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const hostPassed = (skip = skipModule) => {
|
const hostPassed = (skip = skipModule) => {
|
||||||
|
@ -134,13 +135,7 @@ const checkModules = async () => {
|
||||||
hostAvail = false;
|
hostAvail = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { body } = await request.get({
|
const { body } = await request.get(baseUrl + '/modules.json');
|
||||||
url: baseUrl + '/versions.json',
|
|
||||||
qs: {
|
|
||||||
...baseQuery,
|
|
||||||
_: Math.floor(Date.now() / 300000) // 5 min intervals
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
checking = false;
|
checking = false;
|
||||||
|
|
||||||
|
@ -179,10 +174,13 @@ const downloadModule = async (name, ver) => {
|
||||||
name
|
name
|
||||||
});
|
});
|
||||||
|
|
||||||
const url = baseUrl + '/' + name + '/' + ver;
|
const url = baseUrl + '/' + name;
|
||||||
|
const path = join(downloadPath, name + '-' + ver + '.tar');
|
||||||
|
|
||||||
const path = join(downloadPath, name + '-' + ver + '.zip');
|
const stream = zlib.createBrotliDecompress();
|
||||||
const stream = fs.createWriteStream(path);
|
stream.pipe(fs.createWriteStream(path));
|
||||||
|
// const path = join(downloadPath, name + '-' + ver + '.tar.br');
|
||||||
|
// const stream = fs.createWriteStream(path);
|
||||||
|
|
||||||
stream.on('progress', ([ cur, total ]) => events.emit('downloading-module-progress', {
|
stream.on('progress', ([ cur, total ]) => events.emit('downloading-module-progress', {
|
||||||
name,
|
name,
|
||||||
|
@ -196,7 +194,6 @@ const downloadModule = async (name, ver) => {
|
||||||
try {
|
try {
|
||||||
const resp = await request.get({
|
const resp = await request.get({
|
||||||
url,
|
url,
|
||||||
qs: baseQuery,
|
|
||||||
stream
|
stream
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -250,43 +247,31 @@ const installModule = async (name, ver, path) => {
|
||||||
// Extract zip via unzip cmd line - replaces yauzl dep (speed++, size--, jank++)
|
// Extract zip via unzip cmd line - replaces yauzl dep (speed++, size--, jank++)
|
||||||
const ePath = join(basePath, name);
|
const ePath = join(basePath, name);
|
||||||
|
|
||||||
const total = await new Promise((res) => {
|
let total = 0;
|
||||||
const p = execFile('unzip', ['-l', path]);
|
await new Promise((res) => {
|
||||||
|
const p = execFile('tar', ['-tf', path]);
|
||||||
|
|
||||||
p.stdout.on('data', x => {
|
p.stdout.on('data', x => total += x.toString().split('\n').length);
|
||||||
const m = x.toString().match(/([0-9]+) files/);
|
p.on('close', res);
|
||||||
if (m) res(parseInt(m[1]));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
p.stderr.on('data', res); // On error resolve undefined (??'d to 0)
|
|
||||||
}) ?? 0;
|
|
||||||
|
|
||||||
mkdirp.sync(ePath);
|
mkdirp.sync(ePath);
|
||||||
|
|
||||||
const proc = execFile('unzip', ['-o', path, '-d', ePath]);
|
const proc = execFile('tar', ['-xvf', path, '-C', 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.on('error', handleErr);
|
||||||
proc.stderr.on('data', handleErr);
|
proc.stderr.on('data', handleErr);
|
||||||
|
|
||||||
let cur = 0;
|
let cur = 0;
|
||||||
proc.stdout.on('data', x => x.toString().split('\n').forEach(y => {
|
proc.stdout.on('data', x => {
|
||||||
if (!y.includes('inflating')) return;
|
cur += x.toString().split('\n').length;
|
||||||
|
|
||||||
cur++;
|
|
||||||
events.emit('installing-module-progress', {
|
events.emit('installing-module-progress', {
|
||||||
name,
|
name,
|
||||||
cur,
|
cur,
|
||||||
total
|
total
|
||||||
});
|
});
|
||||||
}));
|
});
|
||||||
|
|
||||||
proc.on('close', () => {
|
proc.on('close', () => {
|
||||||
if (hasError) return;
|
if (hasError) return;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const request = require('request');
|
const request = require('request');
|
||||||
|
|
||||||
const nodeRequest = (opts) => new Promise((resolve, reject) => {
|
const nodeRequest = (opts) => new Promise((resolve, reject) => {
|
||||||
const { stream, timeout } = opts;
|
let { stream, timeout } = opts;
|
||||||
|
|
||||||
const req = request({ ...opts, timeout: timeout ?? 15000 });
|
const req = request({ ...opts, timeout: timeout ?? 15000 });
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue