Commit 86488871 authored by Cédric Moreau's avatar Cédric Moreau

[fix] #1037 Migrate modules "peersignal" "plugin"

parent fe918312
......@@ -18,5 +18,7 @@ app/modules/router.js
app/modules/revert.js
app/modules/reset.js
app/modules/reapply.js
app/modules/peersignal.js
app/modules/plugin.js
test/*.js
test/**/*.js
\ No newline at end of file
......@@ -56,4 +56,6 @@ app/modules/router*.js*
app/modules/wizard.js*
app/modules/revert.js*
app/modules/reapply.js*
app/modules/reset.js*
\ No newline at end of file
app/modules/reset.js*
app/modules/peersignal.js*
app/modules/plugin.js*
\ No newline at end of file
"use strict";
const co = require('co');
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const async = require('async');
const constants = require('../lib/constants');
module.exports = {
duniter: {
service: {
neutral: (server, conf, logger) => new PeerSignalEmitter(server, conf, logger)
duniter: {
service: {
neutral: (server, conf) => new PeerSignalEmitter(server, conf)
}
}
}
}
};
/**
* Service which triggers the server's peering generation (actualization of the Peer document).
* @constructor
*/
function PeerSignalEmitter(server, conf) {
let INTERVAL = null;
const peerFifo = async.queue(function (task, callback) {
task(callback);
}, 1);
this.startService = () => co(function*() {
// The interval duration
const SIGNAL_INTERVAL = 1000 * conf.avgGenTime * constants.NETWORK.STATUS_INTERVAL.UPDATE;
// We eventually clean an existing interval
if (INTERVAL)
clearInterval(INTERVAL);
// Create the new regular algorithm
INTERVAL = setInterval(function () {
peerFifo.push((done) => co(function*(){
try {
yield server.PeeringService.generateSelfPeer(conf, SIGNAL_INTERVAL);
done();
} catch (e) {
done(e);
class PeerSignalEmitter {
constructor(server, conf) {
this.server = server;
this.conf = conf;
this.INTERVAL = null;
this.peerFifo = async.queue(function (task, callback) {
task(callback);
}, 1);
}
startService() {
return __awaiter(this, void 0, void 0, function* () {
// The interval duration
const SIGNAL_INTERVAL = 1000 * this.conf.avgGenTime * constants.NETWORK.STATUS_INTERVAL.UPDATE;
// We eventually clean an existing interval
if (this.INTERVAL)
clearInterval(this.INTERVAL);
// Create the new regular algorithm
this.INTERVAL = setInterval(() => {
this.peerFifo.push((done) => __awaiter(this, void 0, void 0, function* () {
try {
yield this.server.PeeringService.generateSelfPeer(this.conf, SIGNAL_INTERVAL);
done();
}
catch (e) {
done(e);
}
}));
}, SIGNAL_INTERVAL);
// Launches it a first time, immediately
yield this.server.PeeringService.generateSelfPeer(this.conf, SIGNAL_INTERVAL);
});
}
stopService() {
// Stop the interval
if (this.INTERVAL) {
clearInterval(this.INTERVAL);
}
}))
}, SIGNAL_INTERVAL);
// Launches it a first time, immediately
yield server.PeeringService.generateSelfPeer(conf, SIGNAL_INTERVAL);
});
this.stopService = () => co(function*() {
// Stop the interval
clearInterval(INTERVAL);
// Empty the fifo
peerFifo.kill();
});
// Empty the fifo
this.peerFifo.kill();
}
}
//# sourceMappingURL=peersignal.js.map
\ No newline at end of file
"use strict";
import {ConfDTO} from "../lib/dto/ConfDTO"
const async = require('async');
const constants = require('../lib/constants');
module.exports = {
duniter: {
service: {
neutral: (server:any, conf:ConfDTO) => new PeerSignalEmitter(server, conf)
}
}
}
/**
* Service which triggers the server's peering generation (actualization of the Peer document).
* @constructor
*/
class PeerSignalEmitter {
INTERVAL:NodeJS.Timer|null = null
peerFifo = async.queue(function (task:any, callback:any) {
task(callback);
}, 1)
constructor(private server:any, private conf:ConfDTO) {
}
async startService() {
// The interval duration
const SIGNAL_INTERVAL = 1000 * this.conf.avgGenTime * constants.NETWORK.STATUS_INTERVAL.UPDATE;
// We eventually clean an existing interval
if (this.INTERVAL)
clearInterval(this.INTERVAL);
// Create the new regular algorithm
this.INTERVAL = setInterval(() => {
this.peerFifo.push(async (done:any) => {
try {
await this.server.PeeringService.generateSelfPeer(this.conf, SIGNAL_INTERVAL)
done();
} catch (e) {
done(e);
}
})
}, SIGNAL_INTERVAL)
// Launches it a first time, immediately
await this.server.PeeringService.generateSelfPeer(this.conf, SIGNAL_INTERVAL)
}
stopService() {
// Stop the interval
if (this.INTERVAL) {
clearInterval(this.INTERVAL)
}
// Empty the fifo
this.peerFifo.kill();
}
}
"use strict";
const co = require('co');
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
module.exports = {
duniter: {
methods: {
canWrite: getNPMAccess,
npmInstall,
npmRemove
},
cli: [{
name: 'plug [what]',
desc: 'Plugs in a duniter module to this Duniter codebase, making it available for the node.',
logs: false,
onDatabaseExecute: (server, conf, program, params) => co(function*() {
const what = params[0];
try {
console.log('Trying to install module "%s"...', what)
yield checkNPMAccess()
yield npmInstall(what)
console.log('Module successfully installed.')
} catch (err) {
console.error('Error during installation of the plugin:', err);
}
// Close the DB connection properly
return server && server.disconnect()
})
}, {
name: 'unplug [what]',
desc: 'Plugs in a duniter module to this Duniter codebase, making it available for the node.',
logs: false,
onDatabaseExecute: (server, conf, program, params) => co(function*() {
const what = params[0];
try {
console.log('Trying to remove module "%s"...', what)
yield checkNPMAccess()
yield npmRemove(what)
console.log('Module successfully uninstalled.')
} catch (err) {
console.error('Error during installation of the plugin:', err);
}
// Close the DB connection properly
return server && server.disconnect()
})
}]
}
}
function npmInstall(what, npm, cwd) {
return new Promise((res, rej) => {
const node = getNode()
npm = npm || getNPM()
cwd = cwd || getCWD()
const install = spawn(node, [npm, 'i', '--save', what], { cwd })
install.stdout.pipe(process.stdout)
install.stderr.pipe(process.stderr)
install.stderr.on('data', (data) => {
if (data.toString().match(/ERR!/)) {
setTimeout(() => {
install.kill('SIGINT')
}, 100)
}
});
install.on('close', (code) => {
if (code === null || code > 0) {
return rej('could not retrieve or install the plugin')
}
res()
duniter: {
methods: {
canWrite: getNPMAccess,
npmInstall,
npmRemove
},
cli: [{
name: 'plug [what]',
desc: 'Plugs in a duniter module to this Duniter codebase, making it available for the node.',
logs: false,
onDatabaseExecute: (server, conf, program, params) => __awaiter(this, void 0, void 0, function* () {
const what = params[0];
try {
console.log('Trying to install module "%s"...', what);
yield checkNPMAccess();
yield npmInstall(what);
console.log('Module successfully installed.');
}
catch (err) {
console.error('Error during installation of the plugin:', err);
}
// Close the DB connection properly
return server && server.disconnect();
})
}, {
name: 'unplug [what]',
desc: 'Plugs in a duniter module to this Duniter codebase, making it available for the node.',
logs: false,
onDatabaseExecute: (server, conf, program, params) => __awaiter(this, void 0, void 0, function* () {
const what = params[0];
try {
console.log('Trying to remove module "%s"...', what);
yield checkNPMAccess();
yield npmRemove(what);
console.log('Module successfully uninstalled.');
}
catch (err) {
console.error('Error during installation of the plugin:', err);
}
// Close the DB connection properly
return server && server.disconnect();
})
}]
}
};
function npmInstall(what, npm = null, cwd = null) {
return new Promise((res, rej) => {
const node = getNode();
npm = npm || getNPM();
cwd = cwd || getCWD();
const install = spawn(node, [npm, 'i', '--save', what], { cwd });
install.stdout.pipe(process.stdout);
install.stderr.pipe(process.stderr);
install.stderr.on('data', (data) => {
if (data.toString().match(/ERR!/)) {
setTimeout(() => {
install.kill('SIGINT');
}, 100);
}
});
install.on('close', (code) => {
if (code === null || code > 0) {
return rej('could not retrieve or install the plugin');
}
res();
});
});
})
}
function npmRemove(what, npm, cwd) {
return new Promise((res, rej) => {
const node = getNode()
npm = npm || getNPM()
cwd = cwd || getCWD()
const uninstall = spawn(node, [npm, 'remove', '--save', what], { cwd })
uninstall.stdout.pipe(process.stdout)
uninstall.stderr.pipe(process.stderr)
uninstall.stderr.on('data', (data) => {
if (data.toString().match(/ERR!/)) {
setTimeout(() => {
uninstall.kill('SIGINT')
}, 100)
}
function npmRemove(what, npm = null, cwd = null) {
return new Promise((res, rej) => {
const node = getNode();
npm = npm || getNPM();
cwd = cwd || getCWD();
const uninstall = spawn(node, [npm, 'remove', '--save', what], { cwd });
uninstall.stdout.pipe(process.stdout);
uninstall.stderr.pipe(process.stderr);
uninstall.stderr.on('data', (data) => {
if (data.toString().match(/ERR!/)) {
setTimeout(() => {
uninstall.kill('SIGINT');
}, 100);
}
});
uninstall.on('close', (code) => {
if (code === null || code > 0) {
return rej('error during the uninstallation of the plugin');
}
res();
});
});
uninstall.on('close', (code) => {
if (code === null || code > 0) {
return rej('error during the uninstallation of the plugin')
}
res()
});
})
}
function getNode() {
return process.argv[0]
.replace(/(node|nw)$/, 'node')
.replace(/(node|nw)\.exe$/, 'nodejs\\node.exe')
return process.argv[0]
.replace(/(node|nw)$/, 'node')
.replace(/(node|nw)\.exe$/, 'nodejs\\node.exe');
}
function getNPM() {
return process.argv[0]
.replace(/(node|nw)$/, 'npm')
.replace(/(node|nw)\.exe$/, 'nodejs\\node_modules\\npm\\bin\\npm-cli.js')
return process.argv[0]
.replace(/(node|nw)$/, 'npm')
.replace(/(node|nw)\.exe$/, 'nodejs\\node_modules\\npm\\bin\\npm-cli.js');
}
function getCWD() {
return process.argv[1].replace(/bin\/duniter$/, '')
return process.argv[1].replace(/bin\/duniter$/, '');
}
function checkNPMAccess() {
return co(function*() {
const hasReadWriteAccess = yield getNPMAccess()
if (!hasReadWriteAccess) {
throw 'no write access on disk'
}
})
return __awaiter(this, void 0, void 0, function* () {
const hasReadWriteAccess = yield getNPMAccess();
if (!hasReadWriteAccess) {
throw 'no write access on disk';
}
});
}
function getNPMAccess() {
return co(function*() {
const hasAccessToPackageJSON = yield new Promise((res) => {
fs.access(path.join(__dirname, '/../../package.json'), fs.constants.R_OK | fs.constants.W_OK, (err) => {
res(!err)
})
})
const hasAccessToNodeModules = yield new Promise((res) => {
fs.access(path.join(__dirname, '/../../node_modules'), fs.constants.R_OK | fs.constants.W_OK, (err) => {
res(!err)
})
})
console.log(hasAccessToPackageJSON, hasAccessToNodeModules)
return hasAccessToPackageJSON && hasAccessToNodeModules
})
return __awaiter(this, void 0, void 0, function* () {
const hasAccessToPackageJSON = yield new Promise((res) => {
fs.access(path.join(__dirname, '/../../package.json'), fs.constants.R_OK | fs.constants.W_OK, (err) => {
res(!err);
});
});
const hasAccessToNodeModules = yield new Promise((res) => {
fs.access(path.join(__dirname, '/../../node_modules'), fs.constants.R_OK | fs.constants.W_OK, (err) => {
res(!err);
});
});
console.log(hasAccessToPackageJSON, hasAccessToNodeModules);
return hasAccessToPackageJSON && hasAccessToNodeModules;
});
}
//# sourceMappingURL=plugin.js.map
\ No newline at end of file
import {ConfDTO} from "../lib/dto/ConfDTO"
"use strict";
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
module.exports = {
duniter: {
methods: {
canWrite: getNPMAccess,
npmInstall,
npmRemove
},
cli: [{
name: 'plug [what]',
desc: 'Plugs in a duniter module to this Duniter codebase, making it available for the node.',
logs: false,
onDatabaseExecute: async (server:any, conf:ConfDTO, program:any, params:any) => {
const what = params[0];
try {
console.log('Trying to install module "%s"...', what)
await checkNPMAccess()
await npmInstall(what)
console.log('Module successfully installed.')
} catch (err) {
console.error('Error during installation of the plugin:', err);
}
// Close the DB connection properly
return server && server.disconnect()
}
}, {
name: 'unplug [what]',
desc: 'Plugs in a duniter module to this Duniter codebase, making it available for the node.',
logs: false,
onDatabaseExecute: async (server:any, conf:ConfDTO, program:any, params:any) => {
const what = params[0];
try {
console.log('Trying to remove module "%s"...', what)
await checkNPMAccess()
await npmRemove(what)
console.log('Module successfully uninstalled.')
} catch (err) {
console.error('Error during installation of the plugin:', err);
}
// Close the DB connection properly
return server && server.disconnect()
}
}]
}
}
function npmInstall(what:string, npm:string|null = null, cwd:string|null = null) {
return new Promise((res, rej) => {
const node = getNode()
npm = npm || getNPM()
cwd = cwd || getCWD()
const install = spawn(node, [npm, 'i', '--save', what], { cwd })
install.stdout.pipe(process.stdout)
install.stderr.pipe(process.stderr)
install.stderr.on('data', (data:any) => {
if (data.toString().match(/ERR!/)) {
setTimeout(() => {
install.kill('SIGINT')
}, 100)
}
});
install.on('close', (code:number|null) => {
if (code === null || code > 0) {
return rej('could not retrieve or install the plugin')
}
res()
});
})
}
function npmRemove(what:string, npm:string|null = null, cwd:string|null = null) {
return new Promise((res, rej) => {
const node = getNode()
npm = npm || getNPM()
cwd = cwd || getCWD()
const uninstall = spawn(node, [npm, 'remove', '--save', what], { cwd })
uninstall.stdout.pipe(process.stdout)
uninstall.stderr.pipe(process.stderr)
uninstall.stderr.on('data', (data:any) => {
if (data.toString().match(/ERR!/)) {
setTimeout(() => {
uninstall.kill('SIGINT')
}, 100)
}
});
uninstall.on('close', (code:number|null) => {
if (code === null || code > 0) {
return rej('error during the uninstallation of the plugin')
}
res()
});
})
}
function getNode() {
return process.argv[0]
.replace(/(node|nw)$/, 'node')
.replace(/(node|nw)\.exe$/, 'nodejs\\node.exe')
}
function getNPM() {
return process.argv[0]
.replace(/(node|nw)$/, 'npm')
.replace(/(node|nw)\.exe$/, 'nodejs\\node_modules\\npm\\bin\\npm-cli.js')
}
function getCWD() {
return process.argv[1].replace(/bin\/duniter$/, '')
}
async function checkNPMAccess() {
const hasReadWriteAccess = await getNPMAccess()
if (!hasReadWriteAccess) {
throw 'no write access on disk'
}
}
async function getNPMAccess() {
const hasAccessToPackageJSON = await new Promise((res) => {
fs.access(path.join(__dirname, '/../../package.json'), fs.constants.R_OK | fs.constants.W_OK, (err:any) => {
res(!err)
})
})
const hasAccessToNodeModules = await new Promise((res) => {
fs.access(path.join(__dirname, '/../../node_modules'), fs.constants.R_OK | fs.constants.W_OK, (err:any) => {
res(!err)
})
})
console.log(hasAccessToPackageJSON, hasAccessToNodeModules)
return hasAccessToPackageJSON && hasAccessToNodeModules
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment