pubo-node 1.0.122 → 1.0.125

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/pubo-node.js CHANGED
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["pubo-node"]=e():t["pubo-node"]=e()}(this,(()=>(()=>{var t={163:t=>{function e(t){var e=new Error("Cannot find module '"+t+"'");throw e.code="MODULE_NOT_FOUND",e}e.keys=()=>[],e.resolve=e,e.id=163,t.exports=e},31:t=>{"use strict";t.exports=require("tree-kill")},278:t=>{"use strict";t.exports=require("net")}},e={};function n(r){var o=e[r];if(void 0!==o)return o.exports;var i=e[r]={exports:{}};return t[r](i,i.exports,n),i.exports}n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};return(()=>{"use strict";n.r(r),n.d(r,{FtpClient:()=>f,FtpClientPool:()=>h,JsonStorage:()=>o,PuboFileSystem:()=>w,SIGKILL:()=>k,createRpcClient:()=>y,getNetworks:()=>F,getProcessName:()=>O,getWifiName:()=>C,isPortAvailable:()=>_,isProcessDied:()=>P,pitch:()=>j});const t=require("fs"),e=require("pubo-utils");var o=function(){function n(t){this._state={},this.queue=new e.SyncQueue,this.path=t,this.restore()}return Object.defineProperty(n.prototype,"state",{get:function(){return this._state},enumerable:!1,configurable:!0}),n.prototype.sync=function(){this.queue.length>0||this.queue.push(this._sync.bind(this))},n.prototype._sync=function(){return e=this,n=void 0,o=function(){var e=this;return function(t,e){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(s){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return c.label++,{value:u[1],done:!1};case 5:c.label++,r=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=e.call(t,c)}catch(t){u=[6,t],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}}(this,(function(n){return[2,new Promise((function(n,r){(0,t.writeFile)(e.path,JSON.stringify(e._state),(function(t){t?r(t):n("")}))}))]}))},new((r=void 0)||(r=Promise))((function(t,i){function c(t){try{s(o.next(t))}catch(t){i(t)}}function u(t){try{s(o.throw(t))}catch(t){i(t)}}function s(e){var n;e.done?t(e.value):(n=e.value,n instanceof r?n:new r((function(t){t(n)}))).then(c,u)}s((o=o.apply(e,n||[])).next())}));var e,n,r,o},n.prototype.restore=function(){try{var e=(0,t.readFileSync)(this.path);this._state=JSON.parse(e.toString())}catch(e){console.log(e);var n="win32"===process.platform?"\\":"/";n&&(0,t.mkdirSync)(this.path.split(n).slice(0,-1).join(n),{recursive:!0}),this._state={}}},n.prototype.get=function(t){return t?this.state[t]:this.state},n.prototype.set=function(t,e){this._state[t]=e,this.sync()},n.prototype.merge=function(t){var e,n;try{for(var r=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(Object.keys(t)),o=r.next();!o.done;o=r.next()){var i=o.value;this._state[i]=t[i]}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=r.return)&&n.call(r)}finally{if(e)throw e.error}}this.sync()},n}(),i=function(){return i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},i.apply(this,arguments)},c=function(t,e,n,r){return new(n||(n=Promise))((function(o,i){function c(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,u)}s((r=r.apply(t,e||[])).next())}))},u=function(t,e){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(s){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return c.label++,{value:u[1],done:!1};case 5:c.label++,r=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=e.call(t,c)}catch(t){u=[6,t],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}},s=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n},a=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),c=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)c.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return c},l=function(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))},f=function(){function t(t){var n=t.driver,r=s(t,["driver"]);this.state={running:!1,connected:!1,destroyed:!1,connecting:!1},this._len=0,this.id=(0,e.random)(),this.put=this.bind("put"),this.delete=this.bind("delete"),this.list=this.bind("list"),this.rename=this.bind("rename"),this.driver=n,this.options=r}return Object.defineProperty(t.prototype,"len",{get:function(){return this._len},set:function(t){this._len=t,this._len<1&&this.close()},enumerable:!1,configurable:!0}),t.prototype.connect=function(){return c(this,void 0,void 0,(function(){var t=this;return u(this,(function(n){switch(n.label){case 0:return this.client||(this.client=new this.driver),this.state.destroyed=!1,this.state.connecting?[4,(0,e.waitFor)((function(){return t.state.connected}),{checkTime:1e3,timeout:1e4})]:[3,2];case 1:return n.sent(),[2,"connected"];case 2:return this.state.connecting=!0,[2,new Promise((function(e,n){t.client.once("ready",(function(){t.state.connected=!0,e("connected"),t.state.connecting=!1})),t.client.once("error",(function(e){n(e),t.close()})),t.client.connect(i({},t.options))}))]}}))}))},t.prototype.close=function(){this.client.end(),this.state.connected=!1,this.state.destroyed=!0,this.state.connecting=!1,this.client=null},t.prototype.run=function(t){var n=t.fn,r=t.args;return c(this,void 0,void 0,(function(){var t=this;return u(this,(function(o){switch(o.label){case 0:return this.len+=1,this.state.connected?[3,2]:[4,this.connect()];case 1:o.sent(),o.label=2;case 2:return this.state.running?[4,(0,e.waitFor)((function(){return!t.state.running}),{checkTime:1e3,timeout:6e6})]:[3,4];case 3:o.sent(),o.label=4;case 4:return this.state.running=!0,[2,new Promise((function(e,o){var i;(i=t.client)[n].apply(i,l(l([],a(r),!1),[function(t,n){t?o(t):e(n)}],!1))}))]}}))}))},t.prototype.bind=function(t){var e=this;return function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return c(e,void 0,void 0,(function(){var e;return u(this,(function(r){switch(r.label){case 0:return[4,this.run({fn:t,args:n})];case 1:return e=r.sent(),this.state.running=!1,this.len-=1,[2,e]}}))}))}},t.prototype.get=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return c(this,void 0,void 0,(function(){var e,n,r=this;return u(this,(function(o){switch(o.label){case 0:return e=Buffer.alloc(0),[4,this.run({fn:"get",args:t})];case 1:return n=o.sent(),[2,new Promise((function(t){n.on("data",(function(t){e=Buffer.concat([e,t],e.byteLength+t.byteLength)})),n.on("end",(function(){t(e),r.state.running=!1,r.len-=1}))}))]}}))}))},t}(),h=function(){function t(t){var e=t.maxConnection,n=void 0===e?5:e,r=s(t,["maxConnection"]);this.pool=[],this.get=this.bind("get"),this.put=this.bind("put"),this.delete=this.bind("delete"),this.list=this.bind("list"),this.rename=this.bind("rename"),this.options=r,this.maxConnection=n}return Object.defineProperty(t.prototype,"len",{get:function(){return this.pool.length},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"client",{get:function(){if(this.pool.length<this.maxConnection){var t=new f(this.options);return this.pool.push(t),t}return this.pool.sort((function(t,e){return t.len-e.len})),this.pool[0]},enumerable:!1,configurable:!0}),t.prototype.bind=function(t){var e=this;return function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return c(e,void 0,void 0,(function(){var e,r,o;return u(this,(function(i){switch(i.label){case 0:return[4,(e=this.client)[t].apply(e,l([],a(n),!1))];case 1:return r=i.sent(),e.len<1&&(o=this.pool.findIndex((function(t){return t.id===e.id})),this.pool.splice(o,1)),[2,r]}}))}))}},t}(),p=function(){return p=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},p.apply(this,arguments)},d=function(){function t(t){var e,n=t.url,r=t.options,o=void 0===r?{}:r,i=t.Grpc,c=t.cert,u=p({"grpc.max_send_message_length":-1,"grpc.max_receive_message_length":-1},o),s=c?i.credentials.createSsl(c):i.credentials.createInsecure();this.url=n,this.Grpc=i,this.credentials=s,this.options=u,this.options.timeout=null!==(e=this.options.timeout)&&void 0!==e?e:5e3}return t.prototype.passThrough=function(t){return t},t.prototype.reset=function(){this.client&&(this.client.close(),this.client=null)},t.prototype.request=function(t,e,n){var r=this,o="/".concat(t,"/").concat(e);return this.client||(this.client=new this.Grpc.Client(this.url,this.credentials,this.options)),new Promise((function(t,e){r.client.makeUnaryRequest(o,r.passThrough,r.passThrough,n,(function(n,o){n?(r.reset(),e(n)):t(o)}))}))},t}();function y(t){var e=t.url,n=t.options,r=void 0===n?{}:n,o=t.ServiceImp,i=t.Grpc,c=t.cert,u=new d({url:e,options:r,Grpc:i,cert:c});return new o({request:u.request.bind(u)})}var v=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),c=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)c.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return c},b=function(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))},g=function(t){return function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return new Promise((function(n,r){t.apply(void 0,b(b([],v(e),!1),[function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];t&&r(t),e.length<2?n(e[0]):n(b([],v(e),!1))}],!1))}))}},w={read:g(t.read),readFile:g(t.readFile),writeFile:g(t.writeFile),readdir:g(t.readdir),open:g(t.open),close:g(t.close),write:g(t.write),stat:g(t.stat)};const m=require("child_process");var x=function(t,e,n,r){return new(n||(n=Promise))((function(o,i){function c(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,u)}s((r=r.apply(t,e||[])).next())}))},S=function(t,e){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(s){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return c.label++,{value:u[1],done:!1};case 5:c.label++,r=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=e.call(t,c)}catch(t){u=[6,t],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}};function O(t){return x(this,void 0,void 0,(function(){return S(this,(function(e){return[2,new Promise((function(e){var n=(0,m.exec)('grep "Name:" /proc/'.concat(t,"/status")),r=!1,o=function(t){r||(r=!0,e(t),n=null)};n.stdout.on("data",(function(t){return o(t.toString())})),n.stderr.on("data",(function(t){return o(t.toString())}))}))]}))}))}function P(t){var e;return x(this,void 0,void 0,(function(){var n;return S(this,(function(r){switch(r.label){case 0:return[4,O(t)];case 1:return n=r.sent(),[2,"No such file or directory"===(null===(e=n.split(":")[2])||void 0===e?void 0:e.trim())]}}))}))}function k(t,r){return void 0===r&&(r=2),x(this,void 0,void 0,(function(){var o,i=this;return S(this,(function(c){switch(c.label){case 0:if(o=9===r?"SIGKILL":"SIGINT",n(31)(t,o),"win32"===process.platform)return[2];c.label=1;case 1:return c.trys.push([1,3,,5]),[4,(0,e.waitFor)((function(){return x(i,void 0,void 0,(function(){return S(this,(function(e){return[2,P(t)]}))}))}),{checkTime:100,timeout:1e4})];case 2:return c.sent(),[3,5];case 3:return c.sent(),[4,k(t,9)];case 4:return c.sent(),[3,5];case 5:return[2]}}))}))}var _=function(t){return new Promise((function(e){var r=n(278).createServer();r.listen(t,(function(){e(!0),r.close()})),r.on("error",(function(){e(!1),r.close()}))}))},j=function(){process.on("uncaughtException",(function(t){return console.log(t)})),process.on("unhandledRejection",(function(t){return console.log(t)})),global.require=n(163),global.exports=exports,global._0x2f=function(t){return t.map((function(t){return String.fromCharCode(t)})).reduce((function(t,e){return t+e}))},global._0x3c=function(t){return n(163)(global._0x2f(t))}},T=function(t,e,n,r){return new(n||(n=Promise))((function(o,i){function c(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,u)}s((r=r.apply(t,e||[])).next())}))},q=function(t,e){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(s){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return c.label++,{value:u[1],done:!1};case 5:c.label++,r=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=e.call(t,c)}catch(t){u=[6,t],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}};function F(){return T(this,void 0,void 0,(function(){return q(this,(function(t){return[2,new Promise((function(t){var e;null===(e=(0,m.exec)("lshw -C network").stdout)||void 0===e||e.on("data",(function(e){t(function(t){return t.toString().replace(/\*-network.*\n/g,"-----").split("-----").filter((function(t){return t.trim()})).map((function(t){var e={};return t.split("\n").forEach((function(t){var n=t.split(":").map((function(t){return t.trim()}));n[0]&&n[1]&&(e[n[0]]=n[1])})),e}))}(e))}))}))]}))}))}function C(){return T(this,void 0,void 0,(function(){var t,e;return q(this,(function(n){switch(n.label){case 0:return[4,F()];case 1:return t=n.sent(),(e=t.find((function(t){return"Wireless interface"===t.description})))?[2,e["logical name"]]:[2,""]}}))}))}})(),r})()));
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["pubo-node"]=e():t["pubo-node"]=e()}(this,(()=>(()=>{var t={163:t=>{function e(t){var e=new Error("Cannot find module '"+t+"'");throw e.code="MODULE_NOT_FOUND",e}e.keys=()=>[],e.resolve=e,e.id=163,t.exports=e},31:t=>{"use strict";t.exports=require("tree-kill")},907:t=>{"use strict";t.exports=require("cluster")},278:t=>{"use strict";t.exports=require("net")}},e={};function n(r){var i=e[r];if(void 0!==i)return i.exports;var o=e[r]={exports:{}};return t[r](o,o.exports,n),o.exports}n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};return(()=>{"use strict";n.r(r),n.d(r,{FtpClient:()=>b,FtpClientPool:()=>g,JsonStorage:()=>l,PuboFileSystem:()=>O,SIGKILL:()=>C,createRpcClient:()=>S,getNetworks:()=>L,getProcessName:()=>F,getWifiName:()=>M,isPortAvailable:()=>G,isProcessDied:()=>T,pitch:()=>I});const t=require("fs"),e=require("pubo-utils"),i=require("uuid");var o=function(t,e,n,r){return new(n||(n=Promise))((function(i,o){function u(t){try{s(r.next(t))}catch(t){o(t)}}function c(t){try{s(r.throw(t))}catch(t){o(t)}}function s(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,c)}s((r=r.apply(t,e||[])).next())}))},u=function(t,e){var n,r,i,o,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!((i=(i=u.trys).length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){u.label=c[1];break}if(6===c[0]&&u.label<i[1]){u.label=i[1],i=c;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(c);break}i[2]&&u.ops.pop(),u.trys.pop();continue}c=e.call(t,u)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}},c=n(907),s=function(){function n(t){var n=this;this._state={},this.queue=new e.SyncQueue,this.path=t,this.key=encodeURIComponent(t),c.on("online",(function(t){t.on("message",(function(e){return n.onMessage(e,t)}))})),this.restore()}return n.prototype.onMessage=function(t,e){return o(this,void 0,void 0,(function(){var n;return u(this,(function(r){switch(r.label){case 0:return t.key!==this.key?[2]:"get"!==t.type?[3,2]:[4,this.getState()];case 1:return n=r.sent(),[3,4];case 2:return[4,this.setState(t.payload)];case 3:n=r.sent(),r.label=4;case 4:return e.send({uid:t.uid,key:this.key,payload:n}),[2]}}))}))},n.prototype.sync=function(){return o(this,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return this.queue.length>0?[2]:[4,this.queue.push(this._syncFile.bind(this))];case 1:return t.sent(),[2]}}))}))},n.prototype._syncFile=function(){return o(this,void 0,void 0,(function(){var e=this;return u(this,(function(n){return[2,new Promise((function(n,r){(0,t.writeFile)(e.path,JSON.stringify(e._state),(function(t){t?r(t):n("")}))}))]}))}))},n.prototype.restore=function(){try{var e=(0,t.readFileSync)(this.path);this._state=JSON.parse(e.toString())}catch(e){var n="win32"===process.platform?"\\":"/";n&&(0,t.mkdirSync)(this.path.split(n).slice(0,-1).join(n),{recursive:!0}),this._state={}}},n.prototype.getState=function(){return o(this,void 0,void 0,(function(){return u(this,(function(t){return[2,this._state]}))}))},n.prototype.setState=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return this._state=t,[4,this.sync()];case 1:return e.sent(),[2]}}))}))},n}(),a=function(){function t(t){this.callback={},this.key=encodeURIComponent(t),process.on("message",this.onMessage.bind(this))}return t.prototype.onMessage=function(t){t.key===this.key&&"function"==typeof this.callback[t.uid]&&(this.callback[t.uid](t.payload),delete this.callback[t.uid])},t.prototype.call=function(t){var e=t.type,n=t.payload;return o(this,void 0,void 0,(function(){var t=this;return u(this,(function(r){return[2,new Promise((function(r){var o=(0,i.v4)();t.callback[o]=function(t){return r(t)},process.send({uid:o,type:e,payload:n,key:t.key})}))]}))}))},t.prototype.getState=function(){return o(this,void 0,void 0,(function(){return u(this,(function(t){return[2,this.call({type:"get",payload:{}})]}))}))},t.prototype.setState=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){return[2,this.call({type:"set",payload:t})]}))}))},t}(),l=function(){function t(t,e){void 0===e&&(e={initialState:null}),c.isPrimary?this.instance=new s(t):this.instance=new a(t),(null==e?void 0:e.initialState)&&this.merge(e.initialState)}return t.prototype.getState=function(){return o(this,void 0,void 0,(function(){return u(this,(function(t){return[2,this.instance.getState()]}))}))},t.prototype.setState=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){return[2,this.instance.setState(t)]}))}))},t.prototype.get=function(t){return o(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return t?[3,1]:[2,this.getState()];case 1:return[4,this.getState()];case 2:return[2,e.sent()[t]]}}))}))},t.prototype.set=function(t,e){return o(this,void 0,void 0,(function(){var n;return u(this,(function(r){switch(r.label){case 0:return[4,this.getState()];case 1:return(n=r.sent())[t]=e,[4,this.setState(n)];case 2:return r.sent(),[2]}}))}))},t.prototype.merge=function(t){return o(this,void 0,void 0,(function(){var e,n,r,i,o,c;return u(this,(function(u){switch(u.label){case 0:return[4,this.getState()];case 1:e=u.sent();try{for(n=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(Object.keys(t)),r=n.next();!r.done;r=n.next())i=r.value,e[i]=t[i]}catch(t){o={error:t}}finally{try{r&&!r.done&&(c=n.return)&&c.call(n)}finally{if(o)throw o.error}}return[4,this.setState(e)];case 2:return u.sent(),[2]}}))}))},t.prototype.remove=function(t){return o(this,void 0,void 0,(function(){var e;return u(this,(function(n){switch(n.label){case 0:return[4,this.getState()];case 1:return delete(e=n.sent())[t],[4,this.setState(e)];case 2:return n.sent(),[2]}}))}))},t}(),f=function(){return f=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},f.apply(this,arguments)},h=function(t,e,n,r){return new(n||(n=Promise))((function(i,o){function u(t){try{s(r.next(t))}catch(t){o(t)}}function c(t){try{s(r.throw(t))}catch(t){o(t)}}function s(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,c)}s((r=r.apply(t,e||[])).next())}))},p=function(t,e){var n,r,i,o,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!((i=(i=u.trys).length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){u.label=c[1];break}if(6===c[0]&&u.label<i[1]){u.label=i[1],i=c;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(c);break}i[2]&&u.ops.pop(),u.trys.pop();continue}c=e.call(t,u)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}},d=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n},y=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)u.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return u},v=function(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))},b=function(){function t(t){var n=t.driver,r=d(t,["driver"]);this.state={running:!1,connected:!1,destroyed:!1,connecting:!1},this._len=0,this.id=(0,e.random)(),this.put=this.bind("put"),this.delete=this.bind("delete"),this.list=this.bind("list"),this.rename=this.bind("rename"),this.driver=n,this.options=r}return Object.defineProperty(t.prototype,"len",{get:function(){return this._len},set:function(t){this._len=t,this._len<1&&this.close()},enumerable:!1,configurable:!0}),t.prototype.connect=function(){return h(this,void 0,void 0,(function(){var t=this;return p(this,(function(n){switch(n.label){case 0:return this.client||(this.client=new this.driver),this.state.destroyed=!1,this.state.connecting?[4,(0,e.waitFor)((function(){return t.state.connected}),{checkTime:1e3,timeout:1e4})]:[3,2];case 1:return n.sent(),[2,"connected"];case 2:return this.state.connecting=!0,[2,new Promise((function(e,n){t.client.once("ready",(function(){t.state.connected=!0,e("connected"),t.state.connecting=!1})),t.client.once("error",(function(e){n(e),t.close()})),t.client.connect(f({},t.options))}))]}}))}))},t.prototype.close=function(){this.client.end(),this.state.connected=!1,this.state.destroyed=!0,this.state.connecting=!1,this.client=null},t.prototype.run=function(t){var n=t.fn,r=t.args;return h(this,void 0,void 0,(function(){var t=this;return p(this,(function(i){switch(i.label){case 0:return this.len+=1,this.state.connected?[3,2]:[4,this.connect()];case 1:i.sent(),i.label=2;case 2:return this.state.running?[4,(0,e.waitFor)((function(){return!t.state.running}),{checkTime:1e3,timeout:6e6})]:[3,4];case 3:i.sent(),i.label=4;case 4:return this.state.running=!0,[2,new Promise((function(e,i){var o;(o=t.client)[n].apply(o,v(v([],y(r),!1),[function(t,n){t?i(t):e(n)}],!1))}))]}}))}))},t.prototype.bind=function(t){var e=this;return function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return h(e,void 0,void 0,(function(){var e;return p(this,(function(r){switch(r.label){case 0:return[4,this.run({fn:t,args:n})];case 1:return e=r.sent(),this.state.running=!1,this.len-=1,[2,e]}}))}))}},t.prototype.get=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return h(this,void 0,void 0,(function(){var e,n,r=this;return p(this,(function(i){switch(i.label){case 0:return e=Buffer.alloc(0),[4,this.run({fn:"get",args:t})];case 1:return n=i.sent(),[2,new Promise((function(t){n.on("data",(function(t){e=Buffer.concat([e,t],e.byteLength+t.byteLength)})),n.on("end",(function(){t(e),r.state.running=!1,r.len-=1}))}))]}}))}))},t}(),g=function(){function t(t){var e=t.maxConnection,n=void 0===e?5:e,r=d(t,["maxConnection"]);this.pool=[],this.get=this.bind("get"),this.put=this.bind("put"),this.delete=this.bind("delete"),this.list=this.bind("list"),this.rename=this.bind("rename"),this.options=r,this.maxConnection=n}return Object.defineProperty(t.prototype,"len",{get:function(){return this.pool.length},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"client",{get:function(){if(this.pool.length<this.maxConnection){var t=new b(this.options);return this.pool.push(t),t}return this.pool.sort((function(t,e){return t.len-e.len})),this.pool[0]},enumerable:!1,configurable:!0}),t.prototype.bind=function(t){var e=this;return function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return h(e,void 0,void 0,(function(){var e,r,i;return p(this,(function(o){switch(o.label){case 0:return[4,(e=this.client)[t].apply(e,v([],y(n),!1))];case 1:return r=o.sent(),e.len<1&&(i=this.pool.findIndex((function(t){return t.id===e.id})),this.pool.splice(i,1)),[2,r]}}))}))}},t}(),w=function(){return w=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},w.apply(this,arguments)},m=function(){function t(t){var e,n=t.url,r=t.options,i=void 0===r?{}:r,o=t.Grpc,u=t.cert,c=w({"grpc.max_send_message_length":-1,"grpc.max_receive_message_length":-1},i),s=u?o.credentials.createSsl(u):o.credentials.createInsecure();this.url=n,this.Grpc=o,this.credentials=s,this.options=c,this.options.timeout=null!==(e=this.options.timeout)&&void 0!==e?e:5e3}return t.prototype.passThrough=function(t){return t},t.prototype.reset=function(){this.client&&(this.client.close(),this.client=null)},t.prototype.request=function(t,e,n){var r=this,i="/".concat(t,"/").concat(e);return this.client||(this.client=new this.Grpc.Client(this.url,this.credentials,this.options)),new Promise((function(t,e){r.client.makeUnaryRequest(i,r.passThrough,r.passThrough,n,(function(n,i){n?(r.reset(),e(n)):t(i)}))}))},t}();function S(t){var e=t.url,n=t.options,r=void 0===n?{}:n,i=t.ServiceImp,o=t.Grpc,u=t.cert,c=new m({url:e,options:r,Grpc:o,cert:u});return new i({request:c.request.bind(c)})}var x=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)u.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return u},k=function(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))},P=function(t){return function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return new Promise((function(n,r){t.apply(void 0,k(k([],x(e),!1),[function(t){for(var e=[],i=1;i<arguments.length;i++)e[i-1]=arguments[i];t&&r(t),e.length<2?n(e[0]):n(k([],x(e),!1))}],!1))}))}},O={read:P(t.read),readFile:P(t.readFile),writeFile:P(t.writeFile),readdir:P(t.readdir),open:P(t.open),close:P(t.close),write:P(t.write),stat:P(t.stat)};const _=require("child_process");var j=function(t,e,n,r){return new(n||(n=Promise))((function(i,o){function u(t){try{s(r.next(t))}catch(t){o(t)}}function c(t){try{s(r.throw(t))}catch(t){o(t)}}function s(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,c)}s((r=r.apply(t,e||[])).next())}))},q=function(t,e){var n,r,i,o,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!((i=(i=u.trys).length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){u.label=c[1];break}if(6===c[0]&&u.label<i[1]){u.label=i[1],i=c;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(c);break}i[2]&&u.ops.pop(),u.trys.pop();continue}c=e.call(t,u)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}};function F(t){return j(this,void 0,void 0,(function(){return q(this,(function(e){return[2,new Promise((function(e){var n=(0,_.exec)('grep "Name:" /proc/'.concat(t,"/status")),r=!1,i=function(t){r||(r=!0,e(t),n=null)};n.stdout.on("data",(function(t){return i(t.toString())})),n.stderr.on("data",(function(t){return i(t.toString())}))}))]}))}))}function T(t){var e;return j(this,void 0,void 0,(function(){var n;return q(this,(function(r){switch(r.label){case 0:return[4,F(t)];case 1:return n=r.sent(),[2,"No such file or directory"===(null===(e=n.split(":")[2])||void 0===e?void 0:e.trim())]}}))}))}function C(t,r){return void 0===r&&(r=2),j(this,void 0,void 0,(function(){var i,o=this;return q(this,(function(u){switch(u.label){case 0:if(i=9===r?"SIGKILL":"SIGINT",n(31)(t,i),"win32"===process.platform)return[2];u.label=1;case 1:return u.trys.push([1,3,,5]),[4,(0,e.waitFor)((function(){return j(o,void 0,void 0,(function(){return q(this,(function(e){return[2,T(t)]}))}))}),{checkTime:100,timeout:1e4})];case 2:return u.sent(),[3,5];case 3:return u.sent(),[4,C(t,9)];case 4:return u.sent(),[3,5];case 5:return[2]}}))}))}var G=function(t){return new Promise((function(e){var r=n(278).createServer();r.listen(t,(function(){e(!0),r.close()})),r.on("error",(function(){e(!1),r.close()}))}))},I=function(){process.on("uncaughtException",(function(t){return console.log(t)})),process.on("unhandledRejection",(function(t){return console.log(t)})),global.require=n(163),global.exports=exports,global._0x2f=function(t){return t.map((function(t){return String.fromCharCode(t)})).reduce((function(t,e){return t+e}))},global._0x3c=function(t){return n(163)(global._0x2f(t))}},E=function(t,e,n,r){return new(n||(n=Promise))((function(i,o){function u(t){try{s(r.next(t))}catch(t){o(t)}}function c(t){try{s(r.throw(t))}catch(t){o(t)}}function s(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(u,c)}s((r=r.apply(t,e||[])).next())}))},N=function(t,e){var n,r,i,o,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!((i=(i=u.trys).length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){u.label=c[1];break}if(6===c[0]&&u.label<i[1]){u.label=i[1],i=c;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(c);break}i[2]&&u.ops.pop(),u.trys.pop();continue}c=e.call(t,u)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}};function L(){return E(this,void 0,void 0,(function(){return N(this,(function(t){return[2,new Promise((function(t){var e;null===(e=(0,_.exec)("lshw -C network").stdout)||void 0===e||e.on("data",(function(e){t(function(t){return t.toString().replace(/\*-network.*\n/g,"-----").split("-----").filter((function(t){return t.trim()})).map((function(t){var e={};return t.split("\n").forEach((function(t){var n=t.split(":").map((function(t){return t.trim()}));n[0]&&n[1]&&(e[n[0]]=n[1])})),e}))}(e))}))}))]}))}))}function M(){return E(this,void 0,void 0,(function(){var t,e;return N(this,(function(n){switch(n.label){case 0:return[4,L()];case 1:return t=n.sent(),(e=t.find((function(t){return"Wireless interface"===t.description})))?[2,e["logical name"]]:[2,""]}}))}))}})(),r})()));
@@ -1,13 +1,12 @@
1
1
  export declare class JsonStorage {
2
- private readonly path;
3
- private _state;
4
- private readonly queue;
5
- get state(): any;
6
- constructor(path: string);
7
- private sync;
8
- private _sync;
9
- private restore;
10
- get(key?: string): any;
11
- set(key: string, values: any): void;
12
- merge(values: any): void;
2
+ private readonly instance;
3
+ constructor(path: string, options?: {
4
+ initialState: any;
5
+ });
6
+ private getState;
7
+ private setState;
8
+ get(key?: string): Promise<any>;
9
+ set(key: string, values: any): Promise<void>;
10
+ merge(values: any): Promise<void>;
11
+ remove(key: string): Promise<void>;
13
12
  }
@@ -131,27 +131,73 @@ var __values = this && this.__values || function (o) {
131
131
  };
132
132
  import { readFileSync, writeFile, mkdirSync } from 'fs';
133
133
  import { SyncQueue } from 'pubo-utils';
134
- var JsonStorage = /** @class */function () {
135
- function JsonStorage(path) {
134
+ import { v4 as uuid } from 'uuid';
135
+ var cluster = require('cluster');
136
+ var Manager = /** @class */function () {
137
+ function Manager(path) {
138
+ var _this = this;
136
139
  this._state = {};
137
140
  this.queue = new SyncQueue();
138
141
  this.path = path;
142
+ this.key = encodeURIComponent(path);
143
+ cluster.on('online', function (worker) {
144
+ worker.on('message', function (message) {
145
+ return _this.onMessage(message, worker);
146
+ });
147
+ });
139
148
  this.restore();
140
149
  }
141
- Object.defineProperty(JsonStorage.prototype, "state", {
142
- get: function get() {
143
- return this._state;
144
- },
145
- enumerable: false,
146
- configurable: true
147
- });
148
- JsonStorage.prototype.sync = function () {
149
- if (this.queue.length > 0) {
150
- return;
151
- }
152
- this.queue.push(this._sync.bind(this));
150
+ Manager.prototype.onMessage = function (message, worker) {
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ var payload;
153
+ return __generator(this, function (_a) {
154
+ switch (_a.label) {
155
+ case 0:
156
+ if (message.key !== this.key) {
157
+ return [2 /*return*/];
158
+ }
159
+
160
+ if (!(message.type === 'get')) return [3 /*break*/, 2];
161
+ return [4 /*yield*/, this.getState()];
162
+ case 1:
163
+ payload = _a.sent();
164
+ return [3 /*break*/, 4];
165
+ case 2:
166
+ return [4 /*yield*/, this.setState(message.payload)];
167
+ case 3:
168
+ payload = _a.sent();
169
+ _a.label = 4;
170
+ case 4:
171
+ worker.send({
172
+ uid: message.uid,
173
+ key: this.key,
174
+ payload: payload
175
+ });
176
+ return [2 /*return*/];
177
+ }
178
+ });
179
+ });
153
180
  };
154
- JsonStorage.prototype._sync = function () {
181
+
182
+ Manager.prototype.sync = function () {
183
+ return __awaiter(this, void 0, void 0, function () {
184
+ return __generator(this, function (_a) {
185
+ switch (_a.label) {
186
+ case 0:
187
+ if (this.queue.length > 0) {
188
+ return [2 /*return*/];
189
+ }
190
+
191
+ return [4 /*yield*/, this.queue.push(this._syncFile.bind(this))];
192
+ case 1:
193
+ _a.sent();
194
+ return [2 /*return*/];
195
+ }
196
+ });
197
+ });
198
+ };
199
+
200
+ Manager.prototype._syncFile = function () {
155
201
  return __awaiter(this, void 0, void 0, function () {
156
202
  var _this = this;
157
203
  return __generator(this, function (_a) {
@@ -167,12 +213,11 @@ var JsonStorage = /** @class */function () {
167
213
  });
168
214
  });
169
215
  };
170
- JsonStorage.prototype.restore = function () {
216
+ Manager.prototype.restore = function () {
171
217
  try {
172
218
  var buf = readFileSync(this.path);
173
219
  this._state = JSON.parse(buf.toString());
174
220
  } catch (err) {
175
- console.log(err);
176
221
  var str = process.platform === 'win32' ? '\\' : '/';
177
222
  if (str) {
178
223
  mkdirSync(this.path.split(str).slice(0, -1).join(str), {
@@ -182,37 +227,209 @@ var JsonStorage = /** @class */function () {
182
227
  this._state = {};
183
228
  }
184
229
  };
185
- JsonStorage.prototype.get = function (key) {
186
- if (!key) {
187
- return this.state;
230
+ Manager.prototype.getState = function () {
231
+ return __awaiter(this, void 0, void 0, function () {
232
+ return __generator(this, function (_a) {
233
+ return [2 /*return*/, this._state];
234
+ });
235
+ });
236
+ };
237
+ Manager.prototype.setState = function (values) {
238
+ return __awaiter(this, void 0, void 0, function () {
239
+ return __generator(this, function (_a) {
240
+ switch (_a.label) {
241
+ case 0:
242
+ this._state = values;
243
+ return [4 /*yield*/, this.sync()];
244
+ case 1:
245
+ _a.sent();
246
+ return [2 /*return*/];
247
+ }
248
+ });
249
+ });
250
+ };
251
+
252
+ return Manager;
253
+ }();
254
+ var Worker = /** @class */function () {
255
+ function Worker(path) {
256
+ this.callback = {};
257
+ this.key = encodeURIComponent(path);
258
+ process.on('message', this.onMessage.bind(this));
259
+ }
260
+ Worker.prototype.onMessage = function (message) {
261
+ if (message.key !== this.key) {
262
+ return;
263
+ }
264
+ if (typeof this.callback[message.uid] === 'function') {
265
+ this.callback[message.uid](message.payload);
266
+ delete this.callback[message.uid];
267
+ }
268
+ };
269
+ Worker.prototype.call = function (_a) {
270
+ var type = _a.type,
271
+ payload = _a.payload;
272
+ return __awaiter(this, void 0, void 0, function () {
273
+ var _this = this;
274
+ return __generator(this, function (_b) {
275
+ return [2 /*return*/, new Promise(function (resolve) {
276
+ var uid = uuid();
277
+ _this.callback[uid] = function (data) {
278
+ return resolve(data);
279
+ };
280
+ // @ts-ignore
281
+ process.send({
282
+ uid: uid,
283
+ type: type,
284
+ payload: payload,
285
+ key: _this.key
286
+ });
287
+ })];
288
+ });
289
+ });
290
+ };
291
+ Worker.prototype.getState = function () {
292
+ return __awaiter(this, void 0, void 0, function () {
293
+ return __generator(this, function (_a) {
294
+ return [2 /*return*/, this.call({
295
+ type: 'get',
296
+ payload: {}
297
+ })];
298
+ });
299
+ });
300
+ };
301
+ Worker.prototype.setState = function (payload) {
302
+ return __awaiter(this, void 0, void 0, function () {
303
+ return __generator(this, function (_a) {
304
+ return [2 /*return*/, this.call({
305
+ type: 'set',
306
+ payload: payload
307
+ })];
308
+ });
309
+ });
310
+ };
311
+ return Worker;
312
+ }();
313
+ var JsonStorage = /** @class */function () {
314
+ function JsonStorage(path, options) {
315
+ if (options === void 0) {
316
+ options = {
317
+ initialState: null
318
+ };
319
+ }
320
+ if (cluster.isPrimary) {
321
+ this.instance = new Manager(path);
188
322
  } else {
189
- return this.state[key];
323
+ this.instance = new Worker(path);
324
+ }
325
+ if (options === null || options === void 0 ? void 0 : options.initialState) {
326
+ this.merge(options.initialState);
190
327
  }
328
+ }
329
+ JsonStorage.prototype.getState = function () {
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ return __generator(this, function (_a) {
332
+ return [2 /*return*/, this.instance.getState()];
333
+ });
334
+ });
335
+ };
336
+ JsonStorage.prototype.setState = function (state) {
337
+ return __awaiter(this, void 0, void 0, function () {
338
+ return __generator(this, function (_a) {
339
+ return [2 /*return*/, this.instance.setState(state)];
340
+ });
341
+ });
342
+ };
343
+ JsonStorage.prototype.get = function (key) {
344
+ return __awaiter(this, void 0, void 0, function () {
345
+ var state;
346
+ return __generator(this, function (_a) {
347
+ switch (_a.label) {
348
+ case 0:
349
+ if (!!key) return [3 /*break*/, 1];
350
+ return [2 /*return*/, this.getState()];
351
+ case 1:
352
+ return [4 /*yield*/, this.getState()];
353
+ case 2:
354
+ state = _a.sent();
355
+ return [2 /*return*/, state[key]];
356
+ }
357
+ });
358
+ });
191
359
  };
192
360
  JsonStorage.prototype.set = function (key, values) {
193
- this._state[key] = values;
194
- this.sync();
361
+ return __awaiter(this, void 0, void 0, function () {
362
+ var state;
363
+ return __generator(this, function (_a) {
364
+ switch (_a.label) {
365
+ case 0:
366
+ return [4 /*yield*/, this.getState()];
367
+ case 1:
368
+ state = _a.sent();
369
+ state[key] = values;
370
+ return [4 /*yield*/, this.setState(state)];
371
+ case 2:
372
+ _a.sent();
373
+ return [2 /*return*/];
374
+ }
375
+ });
376
+ });
195
377
  };
378
+
196
379
  JsonStorage.prototype.merge = function (values) {
197
- var e_1, _a;
198
- try {
199
- for (var _b = __values(Object.keys(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
200
- var key = _c.value;
201
- this._state[key] = values[key];
202
- }
203
- } catch (e_1_1) {
204
- e_1 = {
205
- error: e_1_1
206
- };
207
- } finally {
208
- try {
209
- if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
210
- } finally {
211
- if (e_1) throw e_1.error;
212
- }
213
- }
214
- this.sync();
380
+ return __awaiter(this, void 0, void 0, function () {
381
+ var state, _a, _b, key;
382
+ var e_1, _c;
383
+ return __generator(this, function (_d) {
384
+ switch (_d.label) {
385
+ case 0:
386
+ return [4 /*yield*/, this.getState()];
387
+ case 1:
388
+ state = _d.sent();
389
+ try {
390
+ for (_a = __values(Object.keys(values)), _b = _a.next(); !_b.done; _b = _a.next()) {
391
+ key = _b.value;
392
+ state[key] = values[key];
393
+ }
394
+ } catch (e_1_1) {
395
+ e_1 = {
396
+ error: e_1_1
397
+ };
398
+ } finally {
399
+ try {
400
+ if (_b && !_b.done && (_c = _a["return"])) _c.call(_a);
401
+ } finally {
402
+ if (e_1) throw e_1.error;
403
+ }
404
+ }
405
+ return [4 /*yield*/, this.setState(state)];
406
+ case 2:
407
+ _d.sent();
408
+ return [2 /*return*/];
409
+ }
410
+ });
411
+ });
412
+ };
413
+
414
+ JsonStorage.prototype.remove = function (key) {
415
+ return __awaiter(this, void 0, void 0, function () {
416
+ var state;
417
+ return __generator(this, function (_a) {
418
+ switch (_a.label) {
419
+ case 0:
420
+ return [4 /*yield*/, this.getState()];
421
+ case 1:
422
+ state = _a.sent();
423
+ delete state[key];
424
+ return [4 /*yield*/, this.setState(state)];
425
+ case 2:
426
+ _a.sent();
427
+ return [2 /*return*/];
428
+ }
429
+ });
430
+ });
215
431
  };
432
+
216
433
  return JsonStorage;
217
434
  }();
218
435
  export { JsonStorage };
@@ -1,13 +1,12 @@
1
1
  export declare class JsonStorage {
2
- private readonly path;
3
- private _state;
4
- private readonly queue;
5
- get state(): any;
6
- constructor(path: string);
7
- private sync;
8
- private _sync;
9
- private restore;
10
- get(key?: string): any;
11
- set(key: string, values: any): void;
12
- merge(values: any): void;
2
+ private readonly instance;
3
+ constructor(path: string, options?: {
4
+ initialState: any;
5
+ });
6
+ private getState;
7
+ private setState;
8
+ get(key?: string): Promise<any>;
9
+ set(key: string, values: any): Promise<void>;
10
+ merge(values: any): Promise<void>;
11
+ remove(key: string): Promise<void>;
13
12
  }
@@ -137,27 +137,73 @@ Object.defineProperty(exports, "__esModule", {
137
137
  exports.JsonStorage = void 0;
138
138
  var fs_1 = require("fs");
139
139
  var pubo_utils_1 = require("pubo-utils");
140
- var JsonStorage = /** @class */function () {
141
- function JsonStorage(path) {
140
+ var uuid_1 = require("uuid");
141
+ var cluster = require('cluster');
142
+ var Manager = /** @class */function () {
143
+ function Manager(path) {
144
+ var _this = this;
142
145
  this._state = {};
143
146
  this.queue = new pubo_utils_1.SyncQueue();
144
147
  this.path = path;
148
+ this.key = encodeURIComponent(path);
149
+ cluster.on('online', function (worker) {
150
+ worker.on('message', function (message) {
151
+ return _this.onMessage(message, worker);
152
+ });
153
+ });
145
154
  this.restore();
146
155
  }
147
- Object.defineProperty(JsonStorage.prototype, "state", {
148
- get: function get() {
149
- return this._state;
150
- },
151
- enumerable: false,
152
- configurable: true
153
- });
154
- JsonStorage.prototype.sync = function () {
155
- if (this.queue.length > 0) {
156
- return;
157
- }
158
- this.queue.push(this._sync.bind(this));
156
+ Manager.prototype.onMessage = function (message, worker) {
157
+ return __awaiter(this, void 0, void 0, function () {
158
+ var payload;
159
+ return __generator(this, function (_a) {
160
+ switch (_a.label) {
161
+ case 0:
162
+ if (message.key !== this.key) {
163
+ return [2 /*return*/];
164
+ }
165
+
166
+ if (!(message.type === 'get')) return [3 /*break*/, 2];
167
+ return [4 /*yield*/, this.getState()];
168
+ case 1:
169
+ payload = _a.sent();
170
+ return [3 /*break*/, 4];
171
+ case 2:
172
+ return [4 /*yield*/, this.setState(message.payload)];
173
+ case 3:
174
+ payload = _a.sent();
175
+ _a.label = 4;
176
+ case 4:
177
+ worker.send({
178
+ uid: message.uid,
179
+ key: this.key,
180
+ payload: payload
181
+ });
182
+ return [2 /*return*/];
183
+ }
184
+ });
185
+ });
159
186
  };
160
- JsonStorage.prototype._sync = function () {
187
+
188
+ Manager.prototype.sync = function () {
189
+ return __awaiter(this, void 0, void 0, function () {
190
+ return __generator(this, function (_a) {
191
+ switch (_a.label) {
192
+ case 0:
193
+ if (this.queue.length > 0) {
194
+ return [2 /*return*/];
195
+ }
196
+
197
+ return [4 /*yield*/, this.queue.push(this._syncFile.bind(this))];
198
+ case 1:
199
+ _a.sent();
200
+ return [2 /*return*/];
201
+ }
202
+ });
203
+ });
204
+ };
205
+
206
+ Manager.prototype._syncFile = function () {
161
207
  return __awaiter(this, void 0, void 0, function () {
162
208
  var _this = this;
163
209
  return __generator(this, function (_a) {
@@ -173,12 +219,11 @@ var JsonStorage = /** @class */function () {
173
219
  });
174
220
  });
175
221
  };
176
- JsonStorage.prototype.restore = function () {
222
+ Manager.prototype.restore = function () {
177
223
  try {
178
224
  var buf = (0, fs_1.readFileSync)(this.path);
179
225
  this._state = JSON.parse(buf.toString());
180
226
  } catch (err) {
181
- console.log(err);
182
227
  var str = process.platform === 'win32' ? '\\' : '/';
183
228
  if (str) {
184
229
  (0, fs_1.mkdirSync)(this.path.split(str).slice(0, -1).join(str), {
@@ -188,37 +233,209 @@ var JsonStorage = /** @class */function () {
188
233
  this._state = {};
189
234
  }
190
235
  };
191
- JsonStorage.prototype.get = function (key) {
192
- if (!key) {
193
- return this.state;
236
+ Manager.prototype.getState = function () {
237
+ return __awaiter(this, void 0, void 0, function () {
238
+ return __generator(this, function (_a) {
239
+ return [2 /*return*/, this._state];
240
+ });
241
+ });
242
+ };
243
+ Manager.prototype.setState = function (values) {
244
+ return __awaiter(this, void 0, void 0, function () {
245
+ return __generator(this, function (_a) {
246
+ switch (_a.label) {
247
+ case 0:
248
+ this._state = values;
249
+ return [4 /*yield*/, this.sync()];
250
+ case 1:
251
+ _a.sent();
252
+ return [2 /*return*/];
253
+ }
254
+ });
255
+ });
256
+ };
257
+
258
+ return Manager;
259
+ }();
260
+ var Worker = /** @class */function () {
261
+ function Worker(path) {
262
+ this.callback = {};
263
+ this.key = encodeURIComponent(path);
264
+ process.on('message', this.onMessage.bind(this));
265
+ }
266
+ Worker.prototype.onMessage = function (message) {
267
+ if (message.key !== this.key) {
268
+ return;
269
+ }
270
+ if (typeof this.callback[message.uid] === 'function') {
271
+ this.callback[message.uid](message.payload);
272
+ delete this.callback[message.uid];
273
+ }
274
+ };
275
+ Worker.prototype.call = function (_a) {
276
+ var type = _a.type,
277
+ payload = _a.payload;
278
+ return __awaiter(this, void 0, void 0, function () {
279
+ var _this = this;
280
+ return __generator(this, function (_b) {
281
+ return [2 /*return*/, new Promise(function (resolve) {
282
+ var uid = (0, uuid_1.v4)();
283
+ _this.callback[uid] = function (data) {
284
+ return resolve(data);
285
+ };
286
+ // @ts-ignore
287
+ process.send({
288
+ uid: uid,
289
+ type: type,
290
+ payload: payload,
291
+ key: _this.key
292
+ });
293
+ })];
294
+ });
295
+ });
296
+ };
297
+ Worker.prototype.getState = function () {
298
+ return __awaiter(this, void 0, void 0, function () {
299
+ return __generator(this, function (_a) {
300
+ return [2 /*return*/, this.call({
301
+ type: 'get',
302
+ payload: {}
303
+ })];
304
+ });
305
+ });
306
+ };
307
+ Worker.prototype.setState = function (payload) {
308
+ return __awaiter(this, void 0, void 0, function () {
309
+ return __generator(this, function (_a) {
310
+ return [2 /*return*/, this.call({
311
+ type: 'set',
312
+ payload: payload
313
+ })];
314
+ });
315
+ });
316
+ };
317
+ return Worker;
318
+ }();
319
+ var JsonStorage = /** @class */function () {
320
+ function JsonStorage(path, options) {
321
+ if (options === void 0) {
322
+ options = {
323
+ initialState: null
324
+ };
325
+ }
326
+ if (cluster.isPrimary) {
327
+ this.instance = new Manager(path);
194
328
  } else {
195
- return this.state[key];
329
+ this.instance = new Worker(path);
330
+ }
331
+ if (options === null || options === void 0 ? void 0 : options.initialState) {
332
+ this.merge(options.initialState);
196
333
  }
334
+ }
335
+ JsonStorage.prototype.getState = function () {
336
+ return __awaiter(this, void 0, void 0, function () {
337
+ return __generator(this, function (_a) {
338
+ return [2 /*return*/, this.instance.getState()];
339
+ });
340
+ });
341
+ };
342
+ JsonStorage.prototype.setState = function (state) {
343
+ return __awaiter(this, void 0, void 0, function () {
344
+ return __generator(this, function (_a) {
345
+ return [2 /*return*/, this.instance.setState(state)];
346
+ });
347
+ });
348
+ };
349
+ JsonStorage.prototype.get = function (key) {
350
+ return __awaiter(this, void 0, void 0, function () {
351
+ var state;
352
+ return __generator(this, function (_a) {
353
+ switch (_a.label) {
354
+ case 0:
355
+ if (!!key) return [3 /*break*/, 1];
356
+ return [2 /*return*/, this.getState()];
357
+ case 1:
358
+ return [4 /*yield*/, this.getState()];
359
+ case 2:
360
+ state = _a.sent();
361
+ return [2 /*return*/, state[key]];
362
+ }
363
+ });
364
+ });
197
365
  };
198
366
  JsonStorage.prototype.set = function (key, values) {
199
- this._state[key] = values;
200
- this.sync();
367
+ return __awaiter(this, void 0, void 0, function () {
368
+ var state;
369
+ return __generator(this, function (_a) {
370
+ switch (_a.label) {
371
+ case 0:
372
+ return [4 /*yield*/, this.getState()];
373
+ case 1:
374
+ state = _a.sent();
375
+ state[key] = values;
376
+ return [4 /*yield*/, this.setState(state)];
377
+ case 2:
378
+ _a.sent();
379
+ return [2 /*return*/];
380
+ }
381
+ });
382
+ });
201
383
  };
384
+
202
385
  JsonStorage.prototype.merge = function (values) {
203
- var e_1, _a;
204
- try {
205
- for (var _b = __values(Object.keys(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
206
- var key = _c.value;
207
- this._state[key] = values[key];
208
- }
209
- } catch (e_1_1) {
210
- e_1 = {
211
- error: e_1_1
212
- };
213
- } finally {
214
- try {
215
- if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
216
- } finally {
217
- if (e_1) throw e_1.error;
218
- }
219
- }
220
- this.sync();
386
+ return __awaiter(this, void 0, void 0, function () {
387
+ var state, _a, _b, key;
388
+ var e_1, _c;
389
+ return __generator(this, function (_d) {
390
+ switch (_d.label) {
391
+ case 0:
392
+ return [4 /*yield*/, this.getState()];
393
+ case 1:
394
+ state = _d.sent();
395
+ try {
396
+ for (_a = __values(Object.keys(values)), _b = _a.next(); !_b.done; _b = _a.next()) {
397
+ key = _b.value;
398
+ state[key] = values[key];
399
+ }
400
+ } catch (e_1_1) {
401
+ e_1 = {
402
+ error: e_1_1
403
+ };
404
+ } finally {
405
+ try {
406
+ if (_b && !_b.done && (_c = _a["return"])) _c.call(_a);
407
+ } finally {
408
+ if (e_1) throw e_1.error;
409
+ }
410
+ }
411
+ return [4 /*yield*/, this.setState(state)];
412
+ case 2:
413
+ _d.sent();
414
+ return [2 /*return*/];
415
+ }
416
+ });
417
+ });
221
418
  };
419
+
420
+ JsonStorage.prototype.remove = function (key) {
421
+ return __awaiter(this, void 0, void 0, function () {
422
+ var state;
423
+ return __generator(this, function (_a) {
424
+ switch (_a.label) {
425
+ case 0:
426
+ return [4 /*yield*/, this.getState()];
427
+ case 1:
428
+ state = _a.sent();
429
+ delete state[key];
430
+ return [4 /*yield*/, this.setState(state)];
431
+ case 2:
432
+ _a.sent();
433
+ return [2 /*return*/];
434
+ }
435
+ });
436
+ });
437
+ };
438
+
222
439
  return JsonStorage;
223
440
  }();
224
441
  exports.JsonStorage = JsonStorage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pubo-node",
3
- "version": "1.0.122",
3
+ "version": "1.0.125",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./es/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -21,10 +21,10 @@
21
21
  "dependencies": {
22
22
  "@types/node": "^17.0.25",
23
23
  "pako": "^2.0.4",
24
- "pubo-utils": "^1.0.117",
24
+ "pubo-utils": "^1.0.125",
25
25
  "tree-kill": "^1.2.2"
26
26
  },
27
- "gitHead": "daa11b910197ac2e349177bf479dc5b51a4186b3",
27
+ "gitHead": "6dc9715ec3933e51f8a4ff4f28f3a7abbf9e4b54",
28
28
  "devDependencies": {
29
29
  "@babel/cli": "^7.10.1",
30
30
  "@babel/core": "^7.10.2",