export const 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 } const primaryRediscnx = this.allRediscnx.find(cnx => ((payload.key.startsWith(cnx.redisConfig.chansNamespace)) &&(cnx.redisConfig.role=='primary')) ) if(!primaryRediscnx){ this.sendErr(action, 'No primary redis for this key prefix !', reqid); if(this.debug) console.log('ACTION_SET: No primary redis for this key ', payload.key) 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 > primaryRediscnx.redisConfig.storeMaxSize){ this.sendErr(action, 'value too large !', reqid); return } let exp = ((payload.expire>0) && (payload.expire<63072000)) ? payload.expire : 63072000 await primaryRediscnx.redisSet(payload.key, val, exp, primaryRediscnx.redisConfig.storePrefix) } else { await primaryRediscnx.redisDel(payload.key, primaryRediscnx.redisConfig.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)) { console.log('Unauth GET key:',this.userId, this.roles, payload.key) this.sendErr(action, 'Unauthorized key !', reqid); return } const primaryRediscnx = this.allRediscnx.find(cnx => ((payload.key.startsWith(cnx.redisConfig.chansNamespace)) &&(cnx.redisConfig.role=='primary')) ) if(!primaryRediscnx){ this.sendErr(action, 'No primary redis for this key prefix !', reqid); if(this.debug) console.log('ACTION_GET: No primary redis for this key ', payload.key) return } let rawVal = await primaryRediscnx.redisGet(payload.key, primaryRediscnx.redisConfig.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)); } }