refactor(config) patch: promisify

This commit is contained in:
coderaiser 2017-12-01 18:00:08 +02:00
parent d3b6a7505f
commit 53d79bfda8
3 changed files with 33 additions and 30 deletions

View file

@ -120,6 +120,7 @@
"deepword": "^2.0.0",
"dword": "^6.0.0",
"edward": "^6.0.0",
"es6-promisify": "^5.0.0",
"execon": "^1.2.0",
"express": "^4.13.0",
"files-io": "^1.2.0",
@ -154,6 +155,7 @@
"table": "^4.0.1",
"try-catch": "^1.0.0",
"tryrequire": "^1.1.5",
"wraptile": "^1.0.0",
"writejson": "^1.1.0"
},
"devDependencies": {
@ -166,7 +168,6 @@
"css-loader": "^0.28.4",
"emitify": "^3.0.2",
"es6-promise": "^4.0.5",
"es6-promisify": "^5.0.0",
"eslint": "^4.0.0",
"eslint-plugin-node": "^5.1.0",
"extract-text-webpack-plugin": "^3.0.0",
@ -204,7 +205,6 @@
"url-loader": "^0.6.1",
"version-io": "^2.0.1",
"webpack": "^3.0.0",
"wraptile": "^1.0.0",
"yaspeller": "^4.0.0"
},
"engines": {

View file

@ -10,8 +10,12 @@ const fs = require('fs');
const exit = require(DIR_SERVER + 'exit');
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
const fullstore = require('fullstore/legacy');
const currify = require('currify/legacy');
const wraptile = require('wraptile/legacy');
const squad = require('squad');
const pullout = require('pullout/legacy');
const promisify = require('es6-promisify');
const pullout = promisify(require('pullout/legacy'));
const ponse = require('ponse');
const jonny = require('jonny');
const jju = require('jju');
@ -22,6 +26,12 @@ const criton = require('criton');
const HOME = require('os-homedir')();
const manageConfig = squad(traverse, cryptoPass);
const save = promisify(_save);
const swap = currify((f, a, b) => f(b, a));
const sendError = swap(ponse.sendError);
const send = swap(ponse.send);
const formatMsg = currify(CloudFunc.formatMsg);
const apiURL = CloudFunc.apiURL;
@ -48,7 +58,7 @@ if (error && error.code !== 'ENOENT')
const config = Object.assign({}, rootConfig, configHome);
module.exports = manage;
module.exports.save = save;
module.exports.save = _save;
module.exports.middle = middle;
module.exports.listen = (socket, authCheck) => {
check(socket, authCheck);
@ -74,7 +84,7 @@ function manage(key, value) {
config[key] = value;
}
function save(callback) {
function _save(callback) {
writejson(ConfigHome, config, callback);
}
@ -100,15 +110,13 @@ function connection(socket) {
manageConfig(json);
save((error) => {
if (error)
return socket.emit('err', error.message);
save().then(() => {
const data = CloudFunc.formatMsg('config', key(json));
socket.broadcast.send(json);
socket.send(json);
socket.emit('log', data);
}).catch((e) => {
socket.emit('err', e.message);
});
});
}
@ -130,7 +138,7 @@ function middle(req, res, next) {
.status(404)
.send('Config is disabled');
patch(req, res, next);
patch(req, res);
break;
default:
@ -149,7 +157,8 @@ function get(req, res) {
});
}
function patch(req, res, callback) {
function patch(req, res) {
const jsonStore = fullstore();
const options = {
name : 'config.json',
request : req,
@ -157,23 +166,18 @@ function patch(req, res, callback) {
cache : false
};
pullout(req, 'string', (error, body) => {
const json = jonny.parse(body) || {};
if (error)
return callback(error);
manageConfig(json);
save((error) => {
if (error)
return ponse.sendError(error, options);
const data = CloudFunc.formatMsg('config', key(json));
ponse.send(data, options);
});
});
const saveData = wraptile(save);
pullout(req, 'string')
.then(jonny.parse)
.then(jsonStore)
.then(manageConfig)
.then(saveData)
.then(jsonStore)
.then(key)
.then(formatMsg('config'))
.then(send(options))
.catch(sendError(options));
}
function traverse(json) {

View file

@ -32,7 +32,6 @@ test('rest: getWin32RootMsg', (t) => {
});
test('rest: isRootWin32', (t) => {
const path = '/';
const result = _isRootWin32('/');
t.notOk(result, 'should equal');