vox-core
Version:
Runtime de aplicaciones multiplataforma
288 lines • 11.8 kB
JavaScript
var $mod$0 = core.VW.Ecma2015.Utils.module(require('md5'));
var $mod$1 = core.VW.Ecma2015.Utils.module(require('uniqid'));
var $mod$2 = core.VW.Ecma2015.Utils.module(require('fs'));
var $mod$3 = core.VW.Ecma2015.Utils.module(require('path'));
var $mod$4 = core.VW.Ecma2015.Utils.module(require('npm-registry'));
var $mod$5 = core.VW.Ecma2015.Utils.module(require('./Child'));
{
var Adquire = function Adquire() {
Adquire.$constructor ? Adquire.$constructor.apply(this, arguments) : Adquire.$superClass && Adquire.$superClass.apply(this, arguments);
};
Adquire.$constructor = function (App) {
this.app = App;
this.appPath = $mod$3.default.join(this.app.configurationPath, 'apps');
if (!$mod$2.default.existsSync(this.appPath))
$mod$2.default.mkdirSync(this.appPath);
};
Adquire.prototype.getDetails = function (npm, module) {
var task = new core.VW.Task();
npm.packages.details(module, function (err, data) {
task.result = data;
if (err)
task.exception = err;
task.finish();
});
return task;
};
Adquire.prototype.getNumberVersion = function (ver) {
var vers = ver.split('.');
var num = (vers[0] | 0) * 100000000;
num += (vers[1] | 0) * 10000;
num += vers[2] | 0;
return num;
};
Adquire.prototype.getModule = function callee$0$0(app, module) {
var npm, install, num, num2, result, ins, currentDir, name, home, icevwExe, modulePath, packagePath, info, lastVersion, noWeb, i;
return regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1)
switch (context$1$0.prev = context$1$0.next) {
case 0:
npm = new $mod$4.default({}), num2 = -1;
name = module.split('@')[0];
home = process.env.HOME || process.env.USERPROFILE;
modulePath = $mod$3.default.join(home, 'node_modules');
modulePath = Path.join(modulePath, name), mod;
packagePath = $mod$3.default.join(modulePath, 'package.json');
context$1$0.prev = 6;
context$1$0.next = 9;
return regeneratorRuntime.awrap(this.getDetails(npm, module));
case 9:
info = context$1$0.sent;
info = info[0];
lastVersion = info.latest ? info.latest.version : info.version;
num2 = this.getNumberVersion(lastVersion);
context$1$0.next = 18;
break;
case 15:
context$1$0.prev = 15;
context$1$0.t0 = context$1$0['catch'](6);
noWeb = true;
case 18:
try {
mod = require(packagePath);
num = this.getNumberVersion(mod.version);
if (num != num2)
install = true;
} catch (e) {
}
if (!(install && !noWeb)) {
context$1$0.next = 37;
break;
}
context$1$0.prev = 20;
currentDir = process.cwd();
process.chdir(home);
context$1$0.next = 25;
return regeneratorRuntime.awrap(core.VW.PackageManager.NpmManager.load());
case 25:
context$1$0.next = 27;
return regeneratorRuntime.awrap(core.VW.PackageManager.NpmManager.install({}, module));
case 27:
result = context$1$0.sent;
for (i = 0; i < result.installed; i++) {
ins = result.installed[i];
if (ins.module) {
if (ins.module.split('@')[0] == name) {
modulePath = ins.path;
}
}
}
context$1$0.next = 34;
break;
case 31:
context$1$0.prev = 31;
context$1$0.t1 = context$1$0['catch'](20);
throw context$1$0.t1;
case 34:
context$1$0.prev = 34;
process.chdir(currentDir);
return context$1$0.finish(34);
case 37:
icevwExe = $mod$3.default.join(modulePath, 'icevw.main.json');
if ($mod$2.default.existsSync(icevwExe)) {
modulePath = $mod$3.default.join(modulePath, require(icevwExe).main);
modulePath = $mod$3.default.normalize(modulePath);
}
return context$1$0.abrupt('return', modulePath);
case 40:
case 'end':
return context$1$0.stop();
}
}, null, this, [
[
6,
15
],
[
20,
31,
34,
37
]
]);
};
Adquire.prototype.enable = function (args) {
var req = args.request;
var data = req.method == 'GET' ? req.query : req.body;
if (!data.domain)
throw new Error('Debe especificar el dominio');
if (!data.app)
throw new Error('Debe especificar el argumento `app`');
if (!req.session[data.domain])
req.session[data.domain] = {};
if (req.session[data.domain].md5hash != data.hash)
throw new Error('El token de comprobación no es válido');
req.session[data.domain][data.app] = true;
var resultado = {
'enabled': true,
'hash': req.session[data.domain].md5hash
};
return resultado;
};
Adquire.prototype.download = function (app, url) {
var c = undefined;
var task = new core.VW.Task();
try {
var id = $mod$1.default();
var tpath = $mod$3.default.join(this.appPath, '/' + id + '');
var file = $mod$3.default.join(this.appPath, '/' + id + '.zip');
var rpath = $mod$3.default.join(this.appPath, '/' + app + '');
task.result = rpath;
c = function (er, d) {
if (er) {
try {
$mod$2.default.removeSync(tpath);
} catch (e) {
}
try {
$mod$2.default.unlinkSync(file);
} catch (e) {
}
task.exception = er;
task.finish();
} else {
try {
$mod$2.default.unlinkSync(file);
} catch (e) {
}
$mod$2.default.rename(tpath, rpath, function (er) {
if (er) {
task.exception = er;
task.finish();
return;
}
task.finish();
});
}
};
var req = new core.VW.Http.Request(url);
req.timeout = 5000;
var task1 = req.getResponseAsync();
req.originalReq.pipe($mod$2.default.createWriteStream(file));
task1.oncomplete = function () {
if (task1.exception) {
c(task1.exception);
} else {
var response = task.result;
if (response.statusCode == 404) {
c('La url de la aplicación no es válida');
return;
}
var ipc = new core.VW.IPC.Comunication();
ipc.createChildProcess();
var shm = new core.VW.IPC.ShareMethods(ipc);
var task2 = shm.callAsync('icevwUtil.decompress', [
file,
tpath
]);
task2.oncomplete = function () {
ipc.cp.kill();
if (task2.exception) {
c(task2.exception);
} else {
c();
}
};
}
};
} catch (e) {
task.exception = exception;
task.finish();
}
return task;
};
Adquire.prototype.load = function callee$0$0(data) {
var dir, child;
return regeneratorRuntime.async(function callee$0$0$(context$1$0) {
while (1)
switch (context$1$0.prev = context$1$0.next) {
case 0:
vw.info(data);
if (data.app) {
context$1$0.next = 3;
break;
}
throw new Error('Debe especificar el id de la aplicación');
case 3:
if (data.url) {
context$1$0.next = 6;
break;
}
if (data.module) {
context$1$0.next = 6;
break;
}
throw new Error('Debe especificar la url de la aplicación');
case 6:
if (data.uid) {
context$1$0.next = 8;
break;
}
throw new Error('Debe especificar el argumento `uid`');
case 8:
if (!data.module) {
context$1$0.next = 14;
break;
}
context$1$0.next = 11;
return regeneratorRuntime.awrap(this.getModule(data.app, data.module));
case 11:
dir = context$1$0.sent;
context$1$0.next = 19;
break;
case 14:
if (!data.url) {
context$1$0.next = 19;
break;
}
dir = $mod$3.default.join(this.appPath, data.app);
if ($mod$2.default.existsSync(dir)) {
context$1$0.next = 19;
break;
}
context$1$0.next = 19;
return regeneratorRuntime.awrap(this.download(data.app, data.url));
case 19:
child = $mod$5.default.get(data.uid, dir, this.app.log);
context$1$0.next = 22;
return regeneratorRuntime.awrap(child.init());
case 22:
case 'end':
return context$1$0.stop();
}
}, null, this);
};
Adquire.prototype.enabled = function (args) {
var req = args.request;
var res = args.response;
var data = req.method == 'GET' ? req.query : req.body;
if (!data.domain) {
throw new Error('Debe especificar el dominio');
}
if (!req.session[data.domain])
req.session[data.domain] = {};
hash = req.session[data.domain].hash = $mod$1.default();
req.session[data.domain].md5hash = $mod$0.default(hash);
};
}
exports.default = Adquire;