UNPKG

rpdkey

Version:

RpdKey client

413 lines (404 loc) 17 kB
let http=require('http') const { promisify } = require('util'); class rpdkey { static host="127.0.0.1"; static port=6888; static protocol=http; static token='' static initlist=[] static user=""; static password=""; static todisconnect=0; static maxBatchCount=1800; constructor(){ } static connection(cfg){ class connection{ constructor() { if(typeof cfg=='undefined') this.cfg={} else this.cfg=cfg this.token="" this.protocol=http this.cmdlist=[] this.reqing=false; this.reqlength=0; this.bufftable=null; this.seq=0; } pushcmd(url,data,cb){ let _this=this; if(_this.token==""){ _this.cmdlist.push({u:url,d:data,cb:cb,req:0}) return; } if(_this.cmdlist.length==0 && !_this.reqing){ _this.reqing=true; _this.postdata(url,data,function(err,res){ _this.reqing=false; if(res.result==21100){ _this.token=""; _this.cmdlist.push({u:url,d:data,cb:cb,req:0}) _this.postlogindata(cb); }else{ if(typeof cb!='undefined'){ cb(res.result==0?null:res,res); } } _this.checkcmd(); }) return false; } _this.cmdlist.push({u:url,d:data,cb:cb,req:0}) _this.checkcmd(); return true; } checkcmd(){ let _this=this; if(_this.reqing ) return; if(_this.cmdlist.length==0) { if(_this.todisconnect){ _this.disconnect(); } return; } let batch=[] let cobj=null; let lasturl=null; let lastsql=null; let nonbreaknum=0; for(let k=0;k<_this.cmdlist.length;k++){ if(_this.cmdlist[k].req==0 ){ _this.cmdlist[k].req=1; let paraobj={d:_this.cmdlist[k].d} let url=_this.cmdlist[k].u.substring(4); if(lasturl!=null && url==lasturl){ }else paraobj.u=url; lasturl=url; if(url=="/execsql"){ if(lastsql!=null && paraobj.d.sql==lastsql){ delete paraobj.d.sql; }else{ lastsql=paraobj.d.sql; } } batch.push(paraobj); if(url=="/begin"){ nonbreaknum=paraobj.d.len; }else{ if(nonbreaknum>0) nonbreaknum--; } if(nonbreaknum==0) if(batch.length>rpdkey.maxBatchCount) break; } } if(batch.length==0) return; _this.reqing=true; _this.reqlength=batch.length; this.postdata("/sys/record/batch",{batch:batch},function(err,res){ _this.reqing=false; let listoff=0; if(res.result==2100){ for(let k=0;k<_this.cmdlist.length;k++){ if(_this.cmdlist[k].req==1 && listoff<_this.reqlength){ _this.cmdlist[k].req=2; if(typeof _this.cmdlist[k].cb!='undefined') _this.cmdlist[k].cb(res,null) } } } else{ for(let k=0;k<_this.cmdlist.length;k++){ if(_this.cmdlist[k].req==1 && listoff<res.list.length){ _this.cmdlist[k].req=2; let obj=res.list[listoff++]; if(typeof _this.cmdlist[k].cb!='undefined') _this.cmdlist[k].cb(obj.result==0?null:obj,obj) } } } for(let k=0;k<_this.cmdlist.length;k++){ if(_this.cmdlist[k].req!=2){ if(k) _this.cmdlist.splice(0,k) break; } } _this.checkcmd(); }) } postdata(purl,post_data,cb){ let _this=this; var content = JSON.stringify(post_data); let cseq=this.seq.toString(); this.seq++; //console.log("add seq",this.cfg) let headers={ 'Seq': cseq, 'Content-Type': 'application/json; charset=UTF-8' }; if(this.token!="") headers["X-Token"]=this.token; var options = { hostname: this.cfg.host, port: this.cfg.port, path: purl, method: 'POST', headers: headers }; //console.log("send:",post_data) function getdata(level){ level++; var req = _this.protocol.request(options, function (res) { res.setEncoding('utf8'); let data="" res.on('data', function (chunk) { data+=chunk.toString() }); res.on('end',function(){ //console.log("g:",data) if(typeof cb!='undefined'){ let jdata=JSON.parse(data); cb(jdata.result==0?null:jdata,jdata); } }) }); req.on('error', function (e) { if(level<3){ getdata(level) }else{ cb({result:6028},null) } }); req.write(content); req.end(); } getdata(0) } postlogindata(cb){ let _this=this; this.postdata("/admin/user/login",{user:this.cfg.user,password:this.cfg.password},function(err,res){ if(!err){ _this.token=res.token; if(_this.cmdlist.length){ }else{ if(_this.todisconnect){ _this.disconnect(); } } }else{ _this.todisconnect=0; } if(typeof cb!='undefined') cb(err,res) _this.checkcmd(); }) } connect(cb){ if(typeof this.cfg.protocol!='undefined' && this.cfg.protocol=='https') this.protocol=require('http'); this.todisconnect=0; this.postlogindata(cb); } disconnect(cb){ if(this.token!='') this.postdata("/admin/user/logout",{},function(err,res){ if(!err){ console.log("disconnect ok") this.token=""; } cb(err,res) }); else{ this.todisconnect=1; } } execsql(sql,parameter,cb){ let _this=this; if(typeof parameter=='function'){ cb=parameter; _this.pushcmd("/sys/execsql",{sql:sql},cb) }else{ _this.pushcmd("/sys/execsql",{sql:sql,p:parameter},cb) } } execSql(sql,parameter,cb){ let _this=this; function sqlcb(err,res){ if(typeof cb!='function') return; if(typeof res.list!='undefined'){ res.rows=res.list;//rpdkey.cursor(res.list,-1) }else res.rows=null; cb(err,res) } if(typeof sql=='object'){ let sv={sql:sql.sql}; if(typeof sql.name!='undefined') sv.name=sql.name; if(typeof parameter=='function'){ cb=parameter; _this.pushcmd("/sys/execsql",sv,sqlcb) }else{ sv.p=parameter; _this.pushcmd("/sys/execsql",sv,sqlcb) } return; } if(typeof parameter=='function'){ cb=parameter; _this.pushcmd("/sys/execsql",{sql:sql},sqlcb) }else{ _this.pushcmd("/sys/execsql",{sql:sql,p:parameter},sqlcb) } } table(tablename){ let _this=this; class table { constructor() { this.conn=_this this.name=tablename } pushcmd(url,data,cb){ return this.conn.pushcmd(url,data,cb) } insert(rowobject,cb){ this.pushcmd("/sys/record/insert",{table:this.name,value:rowobject},cb); } update(rowobject,cb){ this.pushcmd("/sys/record/update" ,{table:this.name,value:rowobject},cb); } calUpdate(rowobject,op,cb){ this.pushcmd("/sys/record/update" ,{table:this.name,op:op,value:rowobject},cb); } check(rowobject,op,cb){ this.pushcmd("/sys/record/check" ,{table:this.name,op:op,value:rowobject},cb); } delete(key,cb){ this.pushcmd("/sys/record/delete",{table:this.name,key:key},cb); } find(query,cb){ if(typeof query.key!='undefined'){ this.pushcmd("/sys/record/get",{table:this.name,key:query.key},function(err,data){ cb(err,typeof data.data!='undefined'?rpdkey.cursor([data.data],1):null) }); } else{ this.pushcmd("/sys/record/list",{table:this.name,query:query},function(err,res){ cb(err,typeof res.list!='undefined'?rpdkey.cursor(res.list ,typeof res.total!='undefined'?res.total:-1):null) }); } } } if(this.bufftable!=null && this.bufftable.name==tablename) return this.bufftable; this.bufftable=new table(); if(_this.cfg.promisify){ this.bufftable.insertSync=this.bufftable.insert; this.bufftable.insert=promisify(this.bufftable.insertSync); this.bufftable.deleteSync=this.bufftable.delete; this.bufftable.delete=promisify(this.bufftable.deleteSync); this.bufftable.updateSync=this.bufftable.update; this.bufftable.update=promisify(this.bufftable.updateSync); this.bufftable.calUpdateSync=this.bufftable.calUpdate; this.bufftable.calUpdate=promisify(this.bufftable.calUpdateSync); this.bufftable.checkSync=this.bufftable.check; this.bufftable.check=promisify(this.bufftable.checkSync); this.bufftable.findSync=this.bufftable.find; this.bufftable.find=promisify(this.bufftable.findSync); } return this.bufftable; } batch(){ let _this=this; class batch{ constructor(){ this.conn=_this this.cmdlist=[] } pushcmd(url,data,cb){ this.cmdlist.push({u:"/sys/record/"+url,d:data,cb:cb,req:0}) return this.cmdlist.length-1; } insert(table,rowobject,cb){ this.pushcmd("insert",{table:typeof table.name!='undefined'?table.name:table,value:rowobject},cb); } update(table,rowobject,cb){ return this.pushcmd("update",{table:typeof table.name!='undefined'?table.name:table,value:rowobject},cb); } calUpdate(table,rowobject,op,cb){ return this.pushcmd("update",{table:typeof table.name!='undefined'?table.name:table,value:rowobject,op:op},cb); } check(table,rowobject,op,cb){ return this.pushcmd("check",{table:typeof table.name!='undefined'?table.name:table,value:rowobject,op:op},cb); } delete(table,rowobject,cb){ return this.pushcmd("delete",{table:typeof table.name!='undefined'?table.name:table,key:rowobject},cb); } exec(cb){ this.conn.cmdlist.push({u:"/sys/begin",d:{len:this.cmdlist.length},cb:cb,req:0}) this.conn.cmdlist.push(...this.cmdlist) this.conn.checkcmd(); } } let batchobj=new batch(); if(_this.cfg.promisify){ batchobj.execSync=batchobj.exec; batchobj.exec=promisify(conn.execSync); } return batchobj; } } let conn=new connection() if(conn.cfg.promisify){ conn.connectSync=conn.connect; conn.disconnectSync=conn.disconnect; conn.execSqlSync=conn.execSql; conn.connect=promisify(conn.connectSync); conn.disconnect=promisify(conn.disconnectSync); conn.execSql=promisify(conn.execSqlSync); } return conn; } static cursor(list,cnt){ class cursor { constructor(list,count) { this.rows=list; this.resultptr=0; if(typeof count=='undefined') this.count=list.length; else this.count=count; } toArray(){ return this.rows } fetchone(){ if(this.rows==null || this.resultptr>=this.rows.length) return null return this.rows[this.resultptr++] } fetchmany(lines){ if(this.rows==null) return null; let list=[] for(let k=this.resultptr;k<this.rows+lines;k++){ let v=this.fetchone() if(v==null) break; list.push(v) } this.resultptr+=lines; return list; } fetchall(){ return this.rows } total(){ return this.count } } return new cursor(list,cnt); } } module.exports=rpdkey;