2nd
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
module.exports.methods = {
|
||||
|
||||
/* Request:
|
||||
{
|
||||
"action": "SET"
|
||||
"payload": {
|
||||
"key": "keyname",
|
||||
"value": "stringifiedvalue", // If null : deletes the key
|
||||
"expire": 60 // if present & non zero, expires the key in x seconds.
|
||||
// if absent or zero, we force a TTL of 2 years = 63072000 sec.
|
||||
"observe": true // if true: make it an observable key & subscribe to it
|
||||
}
|
||||
"reqid": "6az5e4r6a"
|
||||
}
|
||||
Reply:
|
||||
{
|
||||
"action":"SET",
|
||||
"success":true,
|
||||
"reqid": reqid
|
||||
}
|
||||
*/
|
||||
async action_SET(action, payload, reqid){
|
||||
//TODO : observable...
|
||||
if((typeof(payload)!='object') || (typeof(payload.key)!='string') ||
|
||||
((typeof(payload.value)!='object') && (payload.value!==null)) ||
|
||||
(payload.expire && (typeof(payload.expire)!='number')) ||
|
||||
(payload.observe && (typeof(payload.observe)!='boolean'))
|
||||
){
|
||||
this.sendErr(action, 'Invalid payload', reqid)
|
||||
return
|
||||
}
|
||||
|
||||
if(!this.accessRights.canSet(this.userId, this.roles, payload.key)){
|
||||
this.sendErr(action, 'Unauthorized key !', reqid);
|
||||
return
|
||||
}
|
||||
|
||||
if(payload.value) {
|
||||
let val = null
|
||||
try { val = JSON.stringify(payload.value)}
|
||||
catch(err) {
|
||||
this.sendErr(action, 'Cannot stringify value object !', reqid);
|
||||
return
|
||||
}
|
||||
if(val.length > this.config.redis.storeMaxSize){
|
||||
this.sendErr(action, 'value too large !', reqid);
|
||||
return
|
||||
}
|
||||
let exp = ((payload.expire>0) && (payload.expire<63072000)) ? payload.expire : 63072000
|
||||
await this.rediscnx.redisSet(payload.key, val, exp, this.config.redis.storePrefix)
|
||||
} else {
|
||||
await this.rediscnx.redisDel(payload.key, this.config.redis.storePrefix)
|
||||
}
|
||||
var reply = {
|
||||
'action': action,
|
||||
'success': true,
|
||||
};
|
||||
if(reqid) reply.reqid = reqid;
|
||||
this.send(JSON.stringify(reply));
|
||||
},
|
||||
|
||||
/* Request:
|
||||
{
|
||||
"action": "GET"
|
||||
"payload": {
|
||||
"key": "keyname"
|
||||
}
|
||||
"reqid": "6az5e4r6a"
|
||||
}
|
||||
Reply:
|
||||
{
|
||||
"action":"GET",
|
||||
"success":true,
|
||||
"payload": {
|
||||
"key" : key,
|
||||
"value": value
|
||||
}
|
||||
"reqid": reqid
|
||||
}
|
||||
*/
|
||||
async action_GET(action, payload, reqid){
|
||||
//TODO : observable...
|
||||
if((typeof(payload)!='object') || (typeof(payload.key)!='string')){
|
||||
this.sendErr(action, 'Invalid payload', reqid);
|
||||
return;
|
||||
};
|
||||
|
||||
if(!this.accessRights.canGet(this.userId, this.roles, payload.key)) {
|
||||
this.sendErr(action, 'Unauthorized key !', reqid);
|
||||
return
|
||||
}
|
||||
|
||||
let rawVal = await this.rediscnx.redisGet(payload.key, this.config.redis.storePrefix)
|
||||
let val = null
|
||||
try { val = JSON.parse(rawVal)}
|
||||
catch(err) { console.error('Action GET: Not a json !? ', rawVal) }
|
||||
var reply = {
|
||||
'action': action,
|
||||
'payload': {
|
||||
'key': payload.key,
|
||||
'value': val
|
||||
},
|
||||
'success': true,
|
||||
};
|
||||
if(reqid) reply.reqid = reqid;
|
||||
this.send(JSON.stringify(reply));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user