2019-01-17 18:00:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.events = exports.NO_PENDING_UPDATES = exports.INSTALLING_MODULE_PROGRESS = exports.INSTALLING_MODULE = exports.INSTALLING_MODULES_FINISHED = exports.DOWNLOADED_MODULE = exports.UPDATE_MANUALLY = exports.DOWNLOADING_MODULES_FINISHED = exports.DOWNLOADING_MODULE_PROGRESS = exports.DOWNLOADING_MODULE = exports.UPDATE_CHECK_FINISHED = exports.INSTALLED_MODULE = exports.CHECKING_FOR_UPDATES = undefined;
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; // Manages additional module installation and management.
|
|
|
|
// We add the module folder path to require() lookup paths here.
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
// undocumented node API
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
exports.initPathsOnly = initPathsOnly;
|
|
|
|
exports.init = init;
|
|
|
|
exports.checkForUpdates = checkForUpdates;
|
|
|
|
exports.quitAndInstallUpdates = quitAndInstallUpdates;
|
|
|
|
exports.isInstalled = isInstalled;
|
|
|
|
exports.getInstalled = getInstalled;
|
|
|
|
exports.install = install;
|
|
|
|
exports.installPendingUpdates = installPendingUpdates;
|
|
|
|
|
|
|
|
var _fs = require('fs');
|
|
|
|
|
|
|
|
var _fs2 = _interopRequireDefault(_fs);
|
|
|
|
|
|
|
|
var _path = require('path');
|
|
|
|
|
|
|
|
var _path2 = _interopRequireDefault(_path);
|
|
|
|
|
|
|
|
var _module = require('module');
|
|
|
|
|
|
|
|
var _module2 = _interopRequireDefault(_module);
|
|
|
|
|
|
|
|
var _events = require('events');
|
|
|
|
|
|
|
|
var _mkdirp = require('mkdirp');
|
|
|
|
|
|
|
|
var _mkdirp2 = _interopRequireDefault(_mkdirp);
|
|
|
|
|
|
|
|
var _yauzl = require('yauzl');
|
|
|
|
|
|
|
|
var _yauzl2 = _interopRequireDefault(_yauzl);
|
|
|
|
|
|
|
|
var _paths = require('./paths');
|
|
|
|
|
|
|
|
var paths = _interopRequireWildcard(_paths);
|
|
|
|
|
|
|
|
var _Backoff = require('./Backoff');
|
|
|
|
|
|
|
|
var _Backoff2 = _interopRequireDefault(_Backoff);
|
|
|
|
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const originalFs = require('original-fs');
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
class Events extends _events.EventEmitter {
|
|
|
|
emit(...args) {
|
|
|
|
process.nextTick(() => super.emit.apply(this, args));
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
2019-01-18 17:51:44 +00:00
|
|
|
}
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
class LogStream {
|
|
|
|
constructor(logPath) {
|
2019-01-17 18:00:12 +00:00
|
|
|
try {
|
|
|
|
this.logStream = _fs2.default.createWriteStream(logPath, { flags: 'a' });
|
|
|
|
} catch (e) {
|
2019-01-18 17:51:44 +00:00
|
|
|
console.error(`Failed to create ${logPath}: ${String(e)}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
log(message) {
|
|
|
|
message = `[Modules] ${message}`;
|
|
|
|
console.log(message);
|
|
|
|
if (this.logStream) {
|
|
|
|
this.logStream.write(message);
|
|
|
|
this.logStream.write('\r\n');
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
2019-01-18 17:51:44 +00:00
|
|
|
}
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
end() {
|
|
|
|
if (this.logStream) {
|
|
|
|
this.logStream.end();
|
|
|
|
this.logStream = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
// events
|
2019-01-18 17:51:44 +00:00
|
|
|
const CHECKING_FOR_UPDATES = exports.CHECKING_FOR_UPDATES = 'checking-for-updates';
|
|
|
|
const INSTALLED_MODULE = exports.INSTALLED_MODULE = 'installed-module';
|
|
|
|
const UPDATE_CHECK_FINISHED = exports.UPDATE_CHECK_FINISHED = 'update-check-finished';
|
|
|
|
const DOWNLOADING_MODULE = exports.DOWNLOADING_MODULE = 'downloading-module';
|
|
|
|
const DOWNLOADING_MODULE_PROGRESS = exports.DOWNLOADING_MODULE_PROGRESS = 'downloading-module-progress';
|
|
|
|
const DOWNLOADING_MODULES_FINISHED = exports.DOWNLOADING_MODULES_FINISHED = 'downloading-modules-finished';
|
|
|
|
const UPDATE_MANUALLY = exports.UPDATE_MANUALLY = 'update-manually';
|
|
|
|
const DOWNLOADED_MODULE = exports.DOWNLOADED_MODULE = 'downloaded-module';
|
|
|
|
const INSTALLING_MODULES_FINISHED = exports.INSTALLING_MODULES_FINISHED = 'installing-modules-finished';
|
|
|
|
const INSTALLING_MODULE = exports.INSTALLING_MODULE = 'installing-module';
|
|
|
|
const INSTALLING_MODULE_PROGRESS = exports.INSTALLING_MODULE_PROGRESS = 'installing-module-progress';
|
|
|
|
const NO_PENDING_UPDATES = exports.NO_PENDING_UPDATES = 'no-pending-updates';
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
// settings
|
2019-01-18 17:51:44 +00:00
|
|
|
const ALWAYS_ALLOW_UPDATES = 'ALWAYS_ALLOW_UPDATES';
|
|
|
|
const SKIP_HOST_UPDATE = 'SKIP_HOST_UPDATE';
|
|
|
|
const SKIP_MODULE_UPDATE = 'SKIP_MODULE_UPDATE';
|
|
|
|
const ALWAYS_BOOTSTRAP_MODULES = 'ALWAYS_BOOTSTRAP_MODULES';
|
|
|
|
const USE_LOCAL_MODULE_VERSIONS = 'USE_LOCAL_MODULE_VERSIONS';
|
|
|
|
|
|
|
|
const request = require('../app_bootstrap/request');
|
|
|
|
const REQUEST_TIMEOUT = 15000;
|
|
|
|
const backoff = new _Backoff2.default(1000, 20000);
|
|
|
|
const events = exports.events = new Events();
|
|
|
|
|
|
|
|
let logger;
|
|
|
|
let locallyInstalledModules;
|
|
|
|
let moduleInstallPath;
|
|
|
|
let installedModulesFilePath;
|
|
|
|
let moduleDownloadPath;
|
|
|
|
let bootstrapping;
|
|
|
|
let hostUpdater;
|
|
|
|
let hostUpdateAvailable;
|
|
|
|
let skipHostUpdate;
|
|
|
|
let skipModuleUpdate;
|
|
|
|
let checkingForUpdates;
|
|
|
|
let remoteBaseURL;
|
|
|
|
let remoteQuery;
|
|
|
|
let settings;
|
|
|
|
let remoteModuleVersions;
|
|
|
|
let installedModules;
|
|
|
|
let download;
|
|
|
|
let unzip;
|
|
|
|
let newInstallInProgress;
|
|
|
|
let localModuleVersionsFilePath;
|
|
|
|
let updatable;
|
|
|
|
let bootstrapManifestFilePath;
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
function initPathsOnly(_buildInfo) {
|
|
|
|
if (locallyInstalledModules || moduleInstallPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have `localModulesRoot` in our buildInfo file, we do not fetch modules
|
|
|
|
// from remote, and rely on our locally bundled ones.
|
|
|
|
// Typically used for development mode, or private builds.
|
|
|
|
locallyInstalledModules = _buildInfo.localModulesRoot != null;
|
|
|
|
|
|
|
|
if (locallyInstalledModules) {
|
|
|
|
if (_module2.default.globalPaths.indexOf(_buildInfo.localModulesRoot) === -1) {
|
|
|
|
_module2.default.globalPaths.push(_buildInfo.localModulesRoot);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
moduleInstallPath = _path2.default.join(paths.getUserDataVersioned(), 'modules');
|
|
|
|
if (_module2.default.globalPaths.indexOf(moduleInstallPath) === -1) {
|
|
|
|
_module2.default.globalPaths.push(moduleInstallPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(_endpoint, _settings, _buildInfo) {
|
2019-01-18 17:51:44 +00:00
|
|
|
const endpoint = _endpoint;
|
2019-01-17 18:00:12 +00:00
|
|
|
settings = _settings;
|
2019-01-18 17:51:44 +00:00
|
|
|
const buildInfo = _buildInfo;
|
2019-01-17 18:00:12 +00:00
|
|
|
updatable = buildInfo.version != '0.0.0' && !buildInfo.debug || settings.get(ALWAYS_ALLOW_UPDATES);
|
|
|
|
|
|
|
|
initPathsOnly(buildInfo);
|
|
|
|
|
|
|
|
logger = new LogStream(_path2.default.join(paths.getUserData(), 'modules.log'));
|
|
|
|
bootstrapping = false;
|
|
|
|
hostUpdateAvailable = false;
|
|
|
|
checkingForUpdates = false;
|
|
|
|
skipHostUpdate = settings.get(SKIP_HOST_UPDATE) || !updatable;
|
|
|
|
skipModuleUpdate = settings.get(SKIP_MODULE_UPDATE) || locallyInstalledModules || !updatable;
|
|
|
|
localModuleVersionsFilePath = _path2.default.join(paths.getUserData(), 'local_module_versions.json');
|
|
|
|
bootstrapManifestFilePath = _path2.default.join(paths.getResources(), 'bootstrap', 'manifest.json');
|
|
|
|
installedModules = {};
|
|
|
|
remoteModuleVersions = {};
|
|
|
|
newInstallInProgress = {};
|
|
|
|
|
|
|
|
download = {
|
|
|
|
// currently downloading
|
|
|
|
active: false,
|
|
|
|
// {name, version}
|
|
|
|
queue: [],
|
|
|
|
// current queue index being downloaded
|
|
|
|
next: 0,
|
|
|
|
// download failure count
|
|
|
|
failures: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
unzip = {
|
|
|
|
// currently unzipping
|
|
|
|
active: false,
|
|
|
|
// {name, version, zipfile}
|
|
|
|
queue: [],
|
|
|
|
// current queue index being unzipped
|
|
|
|
next: 0,
|
|
|
|
// unzip failure count
|
|
|
|
failures: 0
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Modules initializing`);
|
|
|
|
logger.log(`Distribution: ${locallyInstalledModules ? 'local' : 'remote'}`);
|
|
|
|
logger.log(`Host updates: ${skipHostUpdate ? 'disabled' : 'enabled'}`);
|
|
|
|
logger.log(`Module updates: ${skipModuleUpdate ? 'disabled' : 'enabled'}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
if (!locallyInstalledModules) {
|
|
|
|
installedModulesFilePath = _path2.default.join(moduleInstallPath, 'installed.json');
|
|
|
|
moduleDownloadPath = _path2.default.join(moduleInstallPath, 'pending');
|
|
|
|
_mkdirp2.default.sync(moduleDownloadPath);
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Module install path: ${moduleInstallPath}`);
|
|
|
|
logger.log(`Module installed file path: ${installedModulesFilePath}`);
|
|
|
|
logger.log(`Module download path: ${moduleDownloadPath}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
let failedLoadingInstalledModules = false;
|
2019-01-17 18:00:12 +00:00
|
|
|
try {
|
|
|
|
installedModules = JSON.parse(_fs2.default.readFileSync(installedModulesFilePath));
|
|
|
|
} catch (err) {
|
|
|
|
failedLoadingInstalledModules = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanDownloadedModules(installedModules);
|
|
|
|
|
|
|
|
bootstrapping = failedLoadingInstalledModules || settings.get(ALWAYS_BOOTSTRAP_MODULES);
|
|
|
|
}
|
|
|
|
|
|
|
|
hostUpdater = require('../app_bootstrap/hostUpdater');
|
|
|
|
// TODO: hostUpdater constants
|
2019-01-18 17:51:44 +00:00
|
|
|
hostUpdater.on('checking-for-update', () => events.emit(CHECKING_FOR_UPDATES));
|
|
|
|
hostUpdater.on('update-available', () => hostOnUpdateAvailable());
|
|
|
|
hostUpdater.on('update-progress', progress => hostOnUpdateProgress(progress));
|
|
|
|
hostUpdater.on('update-not-available', () => hostOnUpdateNotAvailable());
|
|
|
|
hostUpdater.on('update-manually', newVersion => hostOnUpdateManually(newVersion));
|
|
|
|
hostUpdater.on('update-downloaded', () => hostOnUpdateDownloaded());
|
|
|
|
hostUpdater.on('error', err => hostOnError(err));
|
|
|
|
let setFeedURL = hostUpdater.setFeedURL.bind(hostUpdater);
|
|
|
|
|
|
|
|
remoteBaseURL = `${endpoint}/modules/${buildInfo.releaseChannel}`;
|
2019-01-17 18:00:12 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
remoteQuery = { host_version: buildInfo.version };
|
|
|
|
|
|
|
|
switch (process.platform) {
|
|
|
|
case 'darwin':
|
2019-01-18 17:51:44 +00:00
|
|
|
setFeedURL(`${endpoint}/updates/${buildInfo.releaseChannel}?platform=osx&version=${buildInfo.version}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
remoteQuery.platform = 'osx';
|
|
|
|
break;
|
|
|
|
case 'win32':
|
|
|
|
// Squirrel for Windows can't handle query params
|
|
|
|
// https://github.com/Squirrel/Squirrel.Windows/issues/132
|
2019-01-18 17:51:44 +00:00
|
|
|
setFeedURL(`${endpoint}/updates/${buildInfo.releaseChannel}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
remoteQuery.platform = 'win';
|
|
|
|
break;
|
|
|
|
case 'linux':
|
2019-01-18 17:51:44 +00:00
|
|
|
setFeedURL(`${endpoint}/updates/${buildInfo.releaseChannel}?platform=linux&version=${buildInfo.version}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
remoteQuery.platform = 'linux';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanDownloadedModules(installedModules) {
|
|
|
|
try {
|
2019-01-18 17:51:44 +00:00
|
|
|
const entries = _fs2.default.readdirSync(moduleDownloadPath) || [];
|
|
|
|
entries.forEach(entry => {
|
|
|
|
const entryPath = _path2.default.join(moduleDownloadPath, entry);
|
|
|
|
let isStale = true;
|
|
|
|
for (const moduleName of Object.keys(installedModules)) {
|
|
|
|
if (entryPath === installedModules[moduleName].updateZipfile) {
|
|
|
|
isStale = false;
|
|
|
|
break;
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isStale) {
|
|
|
|
_fs2.default.unlinkSync(_path2.default.join(moduleDownloadPath, entry));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
logger.log('Could not clean downloaded modules');
|
|
|
|
logger.log(err.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostOnUpdateAvailable() {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Host update is available.`);
|
2019-01-17 18:00:12 +00:00
|
|
|
hostUpdateAvailable = true;
|
|
|
|
events.emit(UPDATE_CHECK_FINISHED, true, 1, false);
|
|
|
|
events.emit(DOWNLOADING_MODULE, 'host', 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostOnUpdateProgress(progress) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Host update progress: ${progress}%`);
|
2019-01-17 18:00:12 +00:00
|
|
|
events.emit(DOWNLOADING_MODULE_PROGRESS, 'host', progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostOnUpdateNotAvailable() {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Host is up to date.`);
|
2019-01-17 18:00:12 +00:00
|
|
|
if (!skipModuleUpdate) {
|
|
|
|
checkForModuleUpdates();
|
|
|
|
} else {
|
|
|
|
events.emit(UPDATE_CHECK_FINISHED, true, 0, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostOnUpdateManually(newVersion) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Host update is available. Manual update required!`);
|
2019-01-17 18:00:12 +00:00
|
|
|
hostUpdateAvailable = true;
|
|
|
|
checkingForUpdates = false;
|
|
|
|
events.emit(UPDATE_MANUALLY, newVersion);
|
|
|
|
events.emit(UPDATE_CHECK_FINISHED, true, 1, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostOnUpdateDownloaded() {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Host update downloaded.`);
|
2019-01-17 18:00:12 +00:00
|
|
|
checkingForUpdates = false;
|
|
|
|
events.emit(DOWNLOADED_MODULE, 'host', 1, 1, true);
|
|
|
|
events.emit(DOWNLOADING_MODULES_FINISHED, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostOnError(err) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Host update failed: ${err}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
// [adill] osx unsigned builds will fire this code signing error inside setFeedURL and
|
|
|
|
// if we don't do anything about it hostUpdater.checkForUpdates() will never respond.
|
|
|
|
if (err && String(err).indexOf('Could not get code signature for running application') !== -1) {
|
|
|
|
console.warn('Skipping host updates due to code signing failure.');
|
|
|
|
skipHostUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkingForUpdates = false;
|
|
|
|
if (!hostUpdateAvailable) {
|
|
|
|
events.emit(UPDATE_CHECK_FINISHED, false, 0, false);
|
|
|
|
} else {
|
|
|
|
events.emit(DOWNLOADED_MODULE, 'host', 1, 1, false);
|
|
|
|
events.emit(DOWNLOADING_MODULES_FINISHED, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkForUpdates() {
|
|
|
|
if (checkingForUpdates) return;
|
|
|
|
|
|
|
|
checkingForUpdates = true;
|
|
|
|
hostUpdateAvailable = false;
|
|
|
|
if (skipHostUpdate) {
|
|
|
|
events.emit(CHECKING_FOR_UPDATES);
|
|
|
|
hostOnUpdateNotAvailable();
|
|
|
|
} else {
|
|
|
|
logger.log('Checking for host updates.');
|
|
|
|
hostUpdater.checkForUpdates();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRemoteModuleName(name) {
|
|
|
|
if (process.platform === 'win32' && process.arch === 'x64') {
|
2019-01-18 17:51:44 +00:00
|
|
|
return `${name}.x64`;
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkForModuleUpdates() {
|
2019-01-18 17:51:44 +00:00
|
|
|
const query = _extends({}, remoteQuery, { _: Math.floor(Date.now() / 1000 / 60 / 5) });
|
|
|
|
const url = `${remoteBaseURL}/versions.json`;
|
|
|
|
logger.log(`Checking for module updates at ${url}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
request.get({
|
2019-01-18 17:51:44 +00:00
|
|
|
url,
|
2019-01-17 18:00:12 +00:00
|
|
|
agent: false,
|
|
|
|
encoding: null,
|
|
|
|
qs: query,
|
|
|
|
timeout: REQUEST_TIMEOUT,
|
|
|
|
strictSSL: false
|
2019-01-18 17:51:44 +00:00
|
|
|
}, (err, response, body) => {
|
2019-01-17 18:00:12 +00:00
|
|
|
checkingForUpdates = false;
|
|
|
|
|
|
|
|
if (!err && response.statusCode !== 200) {
|
2019-01-18 17:51:44 +00:00
|
|
|
err = new Error(`Non-200 response code: ${response.statusCode}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Failed fetching module versions: ${String(err)}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
events.emit(UPDATE_CHECK_FINISHED, false, 0, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteModuleVersions = JSON.parse(body);
|
|
|
|
if (settings.get(USE_LOCAL_MODULE_VERSIONS)) {
|
|
|
|
try {
|
|
|
|
remoteModuleVersions = JSON.parse(_fs2.default.readFileSync(localModuleVersionsFilePath));
|
|
|
|
console.log('Using local module versions: ', remoteModuleVersions);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Failed to parse local module versions: ', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const updatesToDownload = [];
|
|
|
|
for (const moduleName of Object.keys(installedModules)) {
|
|
|
|
const installedModule = installedModules[moduleName];
|
|
|
|
const installed = installedModule.installedVersion;
|
|
|
|
if (installed === null) {
|
|
|
|
continue;
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
2019-01-18 17:51:44 +00:00
|
|
|
|
|
|
|
const update = installedModule.updateVersion || 0;
|
|
|
|
const remote = remoteModuleVersions[getRemoteModuleName(moduleName)] || 0;
|
|
|
|
// TODO: strict equality?
|
|
|
|
if (installed != remote && update != remote) {
|
|
|
|
logger.log(`Module update available: ${moduleName}@${remote} [installed: ${installed}]`);
|
|
|
|
updatesToDownload.push({ name: moduleName, version: remote });
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
events.emit(UPDATE_CHECK_FINISHED, true, updatesToDownload.length, false);
|
|
|
|
if (updatesToDownload.length === 0) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`No module updates available.`);
|
2019-01-17 18:00:12 +00:00
|
|
|
} else {
|
2019-01-18 17:51:44 +00:00
|
|
|
updatesToDownload.forEach(e => addModuleToDownloadQueue(e.name, e.version));
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addModuleToDownloadQueue(name, version, authToken) {
|
2019-01-18 17:51:44 +00:00
|
|
|
download.queue.push({ name, version, authToken });
|
|
|
|
process.nextTick(() => processDownloadQueue());
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function processDownloadQueue() {
|
|
|
|
if (download.active) return;
|
|
|
|
if (download.queue.length === 0) return;
|
|
|
|
|
|
|
|
download.active = true;
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const queuedModule = download.queue[download.next];
|
2019-01-17 18:00:12 +00:00
|
|
|
download.next += 1;
|
|
|
|
|
|
|
|
events.emit(DOWNLOADING_MODULE, queuedModule.name, download.next, download.queue.length);
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
let totalBytes = 1;
|
|
|
|
let receivedBytes = 0;
|
|
|
|
let progress = 0;
|
|
|
|
let hasErrored = false;
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const url = `${remoteBaseURL}/${getRemoteModuleName(queuedModule.name)}/${queuedModule.version}`;
|
|
|
|
logger.log(`Fetching ${queuedModule.name}@${queuedModule.version} from ${url}`);
|
|
|
|
const headers = {};
|
2019-01-17 18:00:12 +00:00
|
|
|
if (queuedModule.authToken) {
|
|
|
|
headers['Authorization'] = queuedModule.authToken;
|
|
|
|
}
|
|
|
|
request.get({
|
2019-01-18 17:51:44 +00:00
|
|
|
url,
|
2019-01-17 18:00:12 +00:00
|
|
|
agent: false,
|
|
|
|
encoding: null,
|
|
|
|
followAllRedirects: true,
|
|
|
|
qs: remoteQuery,
|
|
|
|
timeout: REQUEST_TIMEOUT,
|
|
|
|
strictSSL: false,
|
2019-01-18 17:51:44 +00:00
|
|
|
headers
|
|
|
|
}).on('error', err => {
|
2019-01-17 18:00:12 +00:00
|
|
|
if (hasErrored) return;
|
|
|
|
hasErrored = true;
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Failed fetching ${queuedModule.name}@${queuedModule.version}: ${String(err)}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
finishModuleDownload(queuedModule.name, queuedModule.version, null, false);
|
2019-01-18 17:51:44 +00:00
|
|
|
}).on('response', response => {
|
2019-01-17 18:00:12 +00:00
|
|
|
totalBytes = response.headers['content-length'] || 1;
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const moduleZipPath = _path2.default.join(moduleDownloadPath, `${queuedModule.name}-${queuedModule.version}.zip`);
|
|
|
|
logger.log(`Streaming ${queuedModule.name}@${queuedModule.version} [${totalBytes} bytes] to ${moduleZipPath}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const stream = _fs2.default.createWriteStream(moduleZipPath);
|
|
|
|
stream.on('finish', () => finishModuleDownload(queuedModule.name, queuedModule.version, moduleZipPath, response.statusCode === 200));
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
response.on('data', chunk => {
|
2019-01-17 18:00:12 +00:00
|
|
|
receivedBytes += chunk.length;
|
|
|
|
stream.write(chunk);
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const fraction = receivedBytes / totalBytes;
|
|
|
|
const newProgress = Math.min(Math.floor(100 * fraction), 100);
|
2019-01-17 18:00:12 +00:00
|
|
|
if (progress != newProgress) {
|
|
|
|
progress = newProgress;
|
|
|
|
events.emit(DOWNLOADING_MODULE_PROGRESS, queuedModule.name, progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: on response error
|
|
|
|
// TODO: on stream error
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
response.on('end', () => stream.end());
|
2019-01-17 18:00:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function commitInstalledModules() {
|
2019-01-18 17:51:44 +00:00
|
|
|
const data = JSON.stringify(installedModules, null, 2);
|
2019-01-17 18:00:12 +00:00
|
|
|
_fs2.default.writeFileSync(installedModulesFilePath, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function finishModuleDownload(name, version, zipfile, succeeded) {
|
|
|
|
if (!installedModules[name]) {
|
|
|
|
installedModules[name] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (succeeded) {
|
|
|
|
installedModules[name].updateVersion = version;
|
|
|
|
installedModules[name].updateZipfile = zipfile;
|
|
|
|
commitInstalledModules();
|
|
|
|
} else {
|
|
|
|
download.failures += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
events.emit(DOWNLOADED_MODULE, name, download.next, download.queue.length, succeeded);
|
|
|
|
|
|
|
|
if (download.next >= download.queue.length) {
|
2019-01-18 17:51:44 +00:00
|
|
|
const successes = download.queue.length - download.failures;
|
|
|
|
logger.log(`Finished module downloads. [success: ${successes}] [failure: ${download.failures}]`);
|
2019-01-17 18:00:12 +00:00
|
|
|
events.emit(DOWNLOADING_MODULES_FINISHED, successes, download.failures);
|
|
|
|
download.queue = [];
|
|
|
|
download.next = 0;
|
|
|
|
download.failures = 0;
|
|
|
|
download.active = false;
|
|
|
|
} else {
|
2019-01-18 17:51:44 +00:00
|
|
|
const continueDownloads = () => {
|
2019-01-17 18:00:12 +00:00
|
|
|
download.active = false;
|
|
|
|
processDownloadQueue();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (succeeded) {
|
|
|
|
backoff.succeed();
|
|
|
|
process.nextTick(continueDownloads);
|
|
|
|
} else {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Waiting ${Math.floor(backoff.current)}ms before next download.`);
|
2019-01-17 18:00:12 +00:00
|
|
|
backoff.fail(continueDownloads);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newInstallInProgress[name]) {
|
|
|
|
addModuleToUnzipQueue(name, version, zipfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addModuleToUnzipQueue(name, version, zipfile) {
|
2019-01-18 17:51:44 +00:00
|
|
|
unzip.queue.push({ name, version, zipfile });
|
|
|
|
process.nextTick(() => processUnzipQueue());
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function processUnzipQueue() {
|
|
|
|
if (unzip.active) return;
|
|
|
|
if (unzip.queue.length === 0) return;
|
|
|
|
|
|
|
|
unzip.active = true;
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const queuedModule = unzip.queue[unzip.next];
|
2019-01-17 18:00:12 +00:00
|
|
|
unzip.next += 1;
|
|
|
|
|
|
|
|
events.emit(INSTALLING_MODULE, queuedModule.name, unzip.next, unzip.queue.length);
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
let hasErrored = false;
|
|
|
|
const onError = (error, zipfile) => {
|
2019-01-17 18:00:12 +00:00
|
|
|
if (hasErrored) return;
|
|
|
|
hasErrored = true;
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Failed installing ${queuedModule.name}@${queuedModule.version}: ${String(error)}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
succeeded = false;
|
|
|
|
if (zipfile) {
|
|
|
|
zipfile.close();
|
|
|
|
}
|
|
|
|
finishModuleUnzip(queuedModule, succeeded);
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
let succeeded = true;
|
|
|
|
const extractRoot = _path2.default.join(moduleInstallPath, queuedModule.name);
|
|
|
|
logger.log(`Installing ${queuedModule.name}@${queuedModule.version} from ${queuedModule.zipfile}`);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const processZipfile = (err, zipfile) => {
|
2019-01-17 18:00:12 +00:00
|
|
|
if (err) {
|
|
|
|
onError(err, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
const totalEntries = zipfile.entryCount;
|
|
|
|
let processedEntries = 0;
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
zipfile.on('entry', entry => {
|
2019-01-17 18:00:12 +00:00
|
|
|
processedEntries += 1;
|
2019-01-18 17:51:44 +00:00
|
|
|
const percent = Math.min(Math.floor(processedEntries / totalEntries * 100), 100);
|
2019-01-17 18:00:12 +00:00
|
|
|
events.emit(INSTALLING_MODULE_PROGRESS, queuedModule.name, percent);
|
|
|
|
|
|
|
|
// skip directories
|
|
|
|
if (/\/$/.test(entry.fileName)) {
|
|
|
|
zipfile.readEntry();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
zipfile.openReadStream(entry, (err, stream) => {
|
2019-01-17 18:00:12 +00:00
|
|
|
if (err) {
|
|
|
|
onError(err, zipfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
stream.on('error', e => onError(e, zipfile));
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
(0, _mkdirp2.default)(_path2.default.join(extractRoot, _path2.default.dirname(entry.fileName)), err => {
|
2019-01-17 18:00:12 +00:00
|
|
|
if (err) {
|
|
|
|
onError(err, zipfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [adill] createWriteStream via original-fs is broken in Electron 4.0.0-beta.6 with .asar files
|
|
|
|
// so we unzip to a temporary filename and rename it afterwards
|
2019-01-18 17:51:44 +00:00
|
|
|
const tempFileName = _path2.default.join(extractRoot, entry.fileName + '.tmp');
|
|
|
|
const finalFileName = _path2.default.join(extractRoot, entry.fileName);
|
|
|
|
const writeStream = originalFs.createWriteStream(tempFileName);
|
2019-01-17 18:00:12 +00:00
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
writeStream.on('error', e => {
|
2019-01-17 18:00:12 +00:00
|
|
|
stream.destroy();
|
|
|
|
try {
|
|
|
|
originalFs.unlinkSync(tempFileName);
|
|
|
|
} catch (err) {}
|
|
|
|
onError(e, zipfile);
|
|
|
|
});
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
writeStream.on('finish', () => {
|
2019-01-17 18:00:12 +00:00
|
|
|
try {
|
|
|
|
originalFs.unlinkSync(finalFileName);
|
|
|
|
} catch (err) {}
|
|
|
|
try {
|
|
|
|
originalFs.renameSync(tempFileName, finalFileName);
|
|
|
|
} catch (err) {
|
|
|
|
onError(err, zipfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
zipfile.readEntry();
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.pipe(writeStream);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
zipfile.on('error', err => {
|
2019-01-17 18:00:12 +00:00
|
|
|
onError(err, zipfile);
|
|
|
|
});
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
zipfile.on('end', () => {
|
2019-01-17 18:00:12 +00:00
|
|
|
if (!succeeded) return;
|
|
|
|
|
|
|
|
installedModules[queuedModule.name].installedVersion = queuedModule.version;
|
|
|
|
finishModuleUnzip(queuedModule, succeeded);
|
|
|
|
});
|
|
|
|
|
|
|
|
zipfile.readEntry();
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
_yauzl2.default.open(queuedModule.zipfile, { lazyEntries: true, autoClose: true }, processZipfile);
|
|
|
|
} catch (err) {
|
|
|
|
onError(err, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function finishModuleUnzip(unzippedModule, succeeded) {
|
|
|
|
delete newInstallInProgress[unzippedModule.name];
|
|
|
|
delete installedModules[unzippedModule.name].updateZipfile;
|
|
|
|
delete installedModules[unzippedModule.name].updateVersion;
|
|
|
|
commitInstalledModules();
|
|
|
|
|
|
|
|
if (!succeeded) {
|
|
|
|
unzip.failures += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
events.emit(INSTALLED_MODULE, unzippedModule.name, unzip.next, unzip.queue.length, succeeded);
|
|
|
|
|
|
|
|
if (unzip.next >= unzip.queue.length) {
|
2019-01-18 17:51:44 +00:00
|
|
|
const successes = unzip.queue.length - unzip.failures;
|
2019-01-17 18:00:12 +00:00
|
|
|
bootstrapping = false;
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Finished module installations. [success: ${successes}] [failure: ${unzip.failures}]`);
|
2019-01-17 18:00:12 +00:00
|
|
|
unzip.queue = [];
|
|
|
|
unzip.next = 0;
|
|
|
|
unzip.failures = 0;
|
|
|
|
unzip.active = false;
|
|
|
|
events.emit(INSTALLING_MODULES_FINISHED, successes, unzip.failures);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
process.nextTick(() => {
|
2019-01-17 18:00:12 +00:00
|
|
|
unzip.active = false;
|
|
|
|
processUnzipQueue();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function quitAndInstallUpdates() {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Relaunching to install ${hostUpdateAvailable ? 'host' : 'module'} updates...`);
|
2019-01-17 18:00:12 +00:00
|
|
|
if (hostUpdateAvailable) {
|
|
|
|
hostUpdater.quitAndInstall();
|
|
|
|
} else {
|
|
|
|
relaunch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function relaunch() {
|
|
|
|
logger.end();
|
2019-01-18 17:51:44 +00:00
|
|
|
const { app } = require('electron');
|
2019-01-17 18:00:12 +00:00
|
|
|
app.relaunch();
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
function isInstalled(name, version) {
|
2019-01-18 17:51:44 +00:00
|
|
|
const metadata = installedModules[name];
|
2019-01-17 18:00:12 +00:00
|
|
|
if (locallyInstalledModules) return true;
|
|
|
|
if (metadata && metadata.installedVersion > 0) {
|
|
|
|
if (!version) return true;
|
|
|
|
if (metadata.installedVersion === version) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInstalled() {
|
|
|
|
return _extends({}, installedModules);
|
|
|
|
}
|
|
|
|
|
|
|
|
function install(name, defer, options) {
|
2019-01-18 17:51:44 +00:00
|
|
|
let { version, authToken } = options || {};
|
2019-01-17 18:00:12 +00:00
|
|
|
if (isInstalled(name, version)) {
|
|
|
|
if (!defer) {
|
|
|
|
events.emit(INSTALLED_MODULE, name, 1, 1, true);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newInstallInProgress[name]) return;
|
|
|
|
|
|
|
|
if (!updatable) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Not updatable; ignoring request to install ${name}...`);
|
2019-01-17 18:00:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defer) {
|
|
|
|
if (version) {
|
2019-01-18 17:51:44 +00:00
|
|
|
throw new Error(`Cannot defer install for a specific version module (${name}, ${version})`);
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Deferred install for ${name}...`);
|
2019-01-17 18:00:12 +00:00
|
|
|
installedModules[name] = { installedVersion: 0 };
|
|
|
|
commitInstalledModules();
|
|
|
|
} else {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`Starting to install ${name}...`);
|
2019-01-17 18:00:12 +00:00
|
|
|
if (!version) {
|
|
|
|
version = remoteModuleVersions[name] || 0;
|
|
|
|
}
|
|
|
|
newInstallInProgress[name] = version;
|
|
|
|
addModuleToDownloadQueue(name, version, authToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function installPendingUpdates() {
|
2019-01-18 17:51:44 +00:00
|
|
|
const updatesToInstall = [];
|
2019-01-17 18:00:12 +00:00
|
|
|
|
|
|
|
if (bootstrapping) {
|
2019-01-18 17:51:44 +00:00
|
|
|
let modules = {};
|
2019-01-17 18:00:12 +00:00
|
|
|
try {
|
|
|
|
modules = JSON.parse(_fs2.default.readFileSync(bootstrapManifestFilePath));
|
|
|
|
} catch (err) {}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
for (const moduleName of Object.keys(modules)) {
|
|
|
|
installedModules[moduleName] = { installedVersion: 0 };
|
|
|
|
const zipfile = _path2.default.join(paths.getResources(), 'bootstrap', `${moduleName}.zip`);
|
|
|
|
updatesToInstall.push({ moduleName, update: modules[moduleName], zipfile });
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:51:44 +00:00
|
|
|
for (const moduleName of Object.keys(installedModules)) {
|
|
|
|
const update = installedModules[moduleName].updateVersion || 0;
|
|
|
|
const zipfile = installedModules[moduleName].updateZipfile;
|
|
|
|
if (update > 0 && zipfile != null) {
|
|
|
|
updatesToInstall.push({ moduleName, update, zipfile });
|
2019-01-17 18:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatesToInstall.length > 0) {
|
2019-01-18 17:51:44 +00:00
|
|
|
logger.log(`${bootstrapping ? 'Bootstrapping' : 'Installing updates'}...`);
|
|
|
|
updatesToInstall.forEach(e => addModuleToUnzipQueue(e.moduleName, e.update, e.zipfile));
|
2019-01-17 18:00:12 +00:00
|
|
|
} else {
|
|
|
|
logger.log('No updates to install');
|
|
|
|
events.emit(NO_PENDING_UPDATES);
|
|
|
|
}
|
|
|
|
}
|