pubo-node 1.0.35 → 1.0.37

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,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports["pubo-node"]=n():t["pubo-node"]=n()}(this,(function(){return function(t){var n={};function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var o in t)e.d(r,o,function(n){return t[n]}.bind(null,o));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=8)}([function(t,n){t.exports=require("pubo-utils")},function(t,n){t.exports=require("fs")},function(t,n){t.exports=require("pako")},function(t,n){t.exports=require("child_process")},function(t,n){t.exports=require("crypto")},function(t,n){t.exports=require("grpc")},function(t,n){function e(t){var n=new Error("Cannot find module '"+t+"'");throw n.code="MODULE_NOT_FOUND",n}e.keys=function(){return[]},e.resolve=e,t.exports=e,e.id=6},function(t,n){function e(t){var n=new Error("Cannot find module '"+t+"'");throw n.code="MODULE_NOT_FOUND",n}e.keys=function(){return[]},e.resolve=e,t.exports=e,e.id=7},function(t,n,e){"use strict";e.r(n),e.d(n,"JsonStorage",(function(){return c})),e.d(n,"initRtspVideos",(function(){return y})),e.d(n,"FtpClient",(function(){return O})),e.d(n,"md5",(function(){return j})),e.d(n,"createGrpcService",(function(){return F})),e.d(n,"GrpcPool",(function(){return E}));var r=e(1),o=e(2),i=function(){return(i=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t}).apply(this,arguments)},c=function(){function t(t){this._state={},this.path=t,this.init()}return t.prototype.init=function(){try{var t=Object(r.readFileSync)(this.path),n=o.inflate(t,{to:"string"});this._state=JSON.parse(n)}catch(t){this.state={}}},Object.defineProperty(t.prototype,"state",{get:function(){return this._state},set:function(t){var n=o.deflate(JSON.stringify(t)),e=Buffer.from(n);Object(r.writeFile)(this.path,e,(function(t){return t&&console.log(t)})),this._state=t},enumerable:!1,configurable:!0}),t.prototype.get=function(t){return t?this.state[t]:i({},this.state)},t.prototype.set=function(t,n){var e=i({},this.state);e[t]=n,this.state=e},t.prototype.merge=function(t){this.state=i(i({},this._state),t)},t}(),u=e(3),s=e(0),a=function(){return(a=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t}).apply(this,arguments)},l=function(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,o,i=e.call(t),c=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)c.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return c},f=function(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(l(arguments[n]));return t},p=function(){function t(t,n){var e=this;this.eventEmitter=new s.Emitter,this.dog=new s.WatchDog({limit:10,onTimeout:function(){return e.connect()}}),this.url=t,this.options=a({},n),this.options.input||(this.options.input=["-rtsp_transport","tcp","-i"]),this.options.output||(this.options.output=["-f","mpegts","-codec:v","mpeg1video"]),this.connect(),this.dog.init()}return t.prototype.onMessage=function(t){this.dog.feed(),this.eventEmitter.emit("message",t.toString())},t.prototype.closeOld=function(){this.s&&(console.log("LOG Video-Server: "+this.url+" try to reconnect;"),this.s.kill(),this.s=null)},t.prototype.connect=function(){var t=this;this.closeOld();var n=f(this.options.input,[this.url],this.options.output,["-"]);this.s=Object(u.spawn)("ffmpeg",n,{detached:!1}),this.s.stderr.on("data",(function(n){return t.onMessage(n)})),this.s.stdout.on("data",(function(n){return t.eventEmitter.emit("data",n)}))},t.prototype.on=function(t,n){return this.eventEmitter.on(t,n)},t.prototype.cancel=function(t){this.eventEmitter.cancel(t)},t}(),h=function(t,n){var e={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&n.indexOf(r)<0&&(e[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)n.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(e[r[o]]=t[r[o]])}return e},y=function(t){var n=t.app,e=t.server,r=t.videos,o=t.path,i=t.expressWs,c=r.map((function(t){var n=t.url,e=h(t,["url"]);return new p(n,e)}));return n.ws||i(n,e),n.ws(o,(function(t,n){var e=n.query.channel,r=void 0===e?0:e,o=null;c[r]&&(o=c[r].on("data",(function(n){t.send(n)}))),t.on("close",(function(){o&&c[r].cancel(o)}))})),c},d=function(){return(d=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t}).apply(this,arguments)},v=function(t,n,e,r){return new(e||(e=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 n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(c,u)}s((r=r.apply(t,n||[])).next())}))},b=function(t,n){var e,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(i){return function(u){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;c;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(o=c.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){c=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){c.label=i[1];break}if(6===i[0]&&c.label<o[1]){c.label=o[1],o=i;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(i);break}o[2]&&c.ops.pop(),c.trys.pop();continue}i=n.call(t,c)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}},g=function(t,n){var e={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&n.indexOf(r)<0&&(e[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)n.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(e[r[o]]=t[r[o]])}return e},m=function(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,o,i=e.call(t),c=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)c.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return c},w=function(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(m(arguments[n]));return t},O=function(){function t(t){var n=t.driver,e=g(t,["driver"]);this.queue=new s.SyncQueue,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.driver=n,this.options=e}return t.prototype._push=function(t){var n=this;return function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return n.queue.push((function(){return t.call.apply(t,w([n],e))}))}},t.prototype._bind=function(t){var n=this;return this._push((function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return v(n,void 0,void 0,(function(){var n;return b(this,(function(r){switch(r.label){case 0:return[4,this.connect()];case 1:return n=r.sent(),[2,this._run(t,n,e)]}}))}))}))},t.prototype._run=function(t,n,e){return new Promise((function(r,o){n[t].apply(n,w(e,[function(e,i){e?(o(e),n.end()):(r(i||"operation successful."),"get"===t?i.once("close",(function(){return n.end()})):n.end())}]))}))},t.prototype.connect=function(){var t=this;return new Promise((function(n,e){var r=new t.driver;r.once("ready",(function(){n(r)})),r.once("error",(function(t){e(t),r.end()})),r.connect(d({},t.options))}))},t}(),x=e(4),j=function(t){return Object(x.createHash)("md5").update(t).digest("hex")},_=e(5),S=function(){return(S=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t}).apply(this,arguments)},P=function(){function t(t){this.options={"grpc.max_send_message_length":-1,"grpc.max_receive_message_length":-1},this.instance=null,this.config=t,this.common=e(6)(this.config.root+"/common_pb"),this.RpcClient=e(7)(this.config.root+"/service_grpc_pb").RpcClient,this.proto=global.proto,global.proto={}}return Object.defineProperty(t.prototype,"client",{get:function(){if(!this.instance){var t=S(S({},this.options),this.config.options||{});this.instance=new this.RpcClient(this.config.url,_.credentials.createInsecure(),t)}return this.instance},enumerable:!1,configurable:!0}),t}(),E=function(){function t(t){this.list=[],this.list=t.map((function(t){return{id:t.id,grpc:new P(t)}}))}return t.prototype.getGrpc=function(t){var n;return null===(n=this.list.find((function(n){return n.id===t})))||void 0===n?void 0:n.grpc},t.pool=[],t.instance=null,t.getPool=function(){return t.instance||(t.instance=new t(t.pool)),t.instance},t.setPool=function(n){t.pool=n,t.instance=new t(t.pool)},t}(),k=function(t,n,e,r){return new(e||(e=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 n;t.done?o(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(c,u)}s((r=r.apply(t,n||[])).next())}))},q=function(t,n){var e,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(i){return function(u){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;c;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(o=c.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){c=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){c.label=i[1];break}if(6===i[0]&&c.label<o[1]){c.label=o[1],o=i;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(i);break}o[2]&&c.ops.pop(),c.trys.pop();continue}i=n.call(t,c)}catch(t){i=[6,t],r=0}finally{e=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}},N=function(t,n){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var r,o,i=e.call(t),c=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)c.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return c},T=function(){for(var t=[],n=0;n<arguments.length;n++)t=t.concat(N(arguments[n]));return t},M=function(t){var n="function"==typeof Symbol&&Symbol.iterator,e=n&&t[n],r=0;if(e)return e.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(n?"Object is not iterable.":"Symbol.iterator is not defined.")};var C=function(){function t(t){if(this.config=t,this.grpc=E.getPool().getGrpc(this.config.client),!this.grpc||!this.grpc.client[this.config.serviceName])throw new Error("rpc service not found:"+this.config.serviceName);var n,e;this.promise=(n=this.grpc.client[this.config.serviceName],e=this.grpc,function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return new Promise((function(r,o){var i=e.client;n.call.apply(n,T([i],t,[function(t,n){global.proto=e.proto,t?o(t):r(n)}]))}))})}return t.prototype.serializer=function(t,n){var e,r;if(void 0===n&&(n={}),!this.grpc)throw new Error("rpc client not found:"+this.config.client);global.proto=this.grpc.proto;var o=new this.grpc.common[t];try{for(var i=M(Object.keys(o.toObject())),c=i.next();!c.done;c=i.next()){var u=c.value;void 0!==n[u]&&o["set"+u[0].toUpperCase()+u.slice(1)](n[u])}}catch(t){e={error:t}}finally{try{c&&!c.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return o},t.prototype.send=function(t){return k(this,void 0,void 0,(function(){return q(this,(function(n){switch(n.label){case 0:return[4,this.promise(this.serializer(this.config.requestType,t))];case 1:return[2,n.sent().toObject()]}}))}))},t}(),G=function(){return(G=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var o in n=arguments[e])Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o]);return t}).apply(this,arguments)},F=Object(s.superFactory)((function(t,n){return new C(G(G({},t),{serviceName:t.service||n}))}))}])}));
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,(function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=5)}([function(t,e){t.exports=require("pubo-utils")},function(t,e){t.exports=require("fs")},function(t,e){t.exports=require("pako")},function(t,e){t.exports=require("child_process")},function(t,e){t.exports=require("crypto")},function(t,e,n){"use strict";n.r(e),n.d(e,"JsonStorage",(function(){return u})),n.d(e,"initRtspVideos",(function(){return y})),n.d(e,"FtpClient",(function(){return w})),n.d(e,"md5",(function(){return x})),n.d(e,"createRpcClient",(function(){return S}));var r=n(1),o=n(2),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}).apply(this,arguments)},u=function(){function t(t){this._state={},this.path=t,this.init()}return t.prototype.init=function(){try{var t=Object(r.readFileSync)(this.path),e=o.inflate(t,{to:"string"});this._state=JSON.parse(e)}catch(t){this.state={}}},Object.defineProperty(t.prototype,"state",{get:function(){return this._state},set:function(t){var e=o.deflate(JSON.stringify(t)),n=Buffer.from(e);Object(r.writeFile)(this.path,n,(function(t){return t&&console.log(t)})),this._state=t},enumerable:!1,configurable:!0}),t.prototype.get=function(t){return t?this.state[t]:i({},this.state)},t.prototype.set=function(t,e){var n=i({},this.state);n[t]=e,this.state=n},t.prototype.merge=function(t){this.state=i(i({},this._state),t)},t}(),c=n(3),s=n(0),a=function(){return(a=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}).apply(this,arguments)},f=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)u.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 u},l=function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(f(arguments[e]));return t},p=function(){function t(t,e){var n=this;this.eventEmitter=new s.Emitter,this.dog=new s.WatchDog({limit:10,onTimeout:function(){return n.connect()}}),this.url=t,this.options=a({},e),this.options.input||(this.options.input=["-rtsp_transport","tcp","-i"]),this.options.output||(this.options.output=["-f","mpegts","-codec:v","mpeg1video"]),this.connect(),this.dog.init()}return t.prototype.onMessage=function(t){this.dog.feed(),this.eventEmitter.emit("message",t.toString())},t.prototype.closeOld=function(){this.s&&(console.log("LOG Video-Server: "+this.url+" try to reconnect;"),this.s.kill(),this.s=null)},t.prototype.connect=function(){var t=this;this.closeOld();var e=l(this.options.input,[this.url],this.options.output,["-"]);this.s=Object(c.spawn)("ffmpeg",e,{detached:!1}),this.s.stderr.on("data",(function(e){return t.onMessage(e)})),this.s.stdout.on("data",(function(e){return t.eventEmitter.emit("data",e)}))},t.prototype.on=function(t,e){return this.eventEmitter.on(t,e)},t.prototype.cancel=function(t){this.eventEmitter.cancel(t)},t}(),h=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},y=function(t){var e=t.app,n=t.server,r=t.videos,o=t.path,i=t.expressWs,u=r.map((function(t){var e=t.url,n=h(t,["url"]);return new p(e,n)}));return e.ws||i(e,n),e.ws(o,(function(t,e){var n=e.query.channel,r=void 0===n?0:n,o=null;u[r]&&(o=u[r].on("data",(function(e){t.send(e)}))),t.on("close",(function(){o&&u[r].cancel(o)}))})),u},d=function(){return(d=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}).apply(this,arguments)},v=function(t,e,n,r){return new(n||(n=Promise))((function(o,i){function u(t){try{s(r.next(t))}catch(t){i(t)}}function c(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(u,c)}s((r=r.apply(t,e||[])).next())}))},b=function(t,e){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}},g=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},m=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)u.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 u},O=function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(m(arguments[e]));return t},w=function(){function t(t){var e=t.driver,n=g(t,["driver"]);this.queue=new s.SyncQueue,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.driver=e,this.options=n}return t.prototype._push=function(t){var e=this;return function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return e.queue.push((function(){return t.call.apply(t,O([e],n))}))}},t.prototype._bind=function(t){var e=this;return this._push((function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return v(e,void 0,void 0,(function(){var e;return b(this,(function(r){switch(r.label){case 0:return[4,this.connect()];case 1:return e=r.sent(),[2,this._run(t,e,n)]}}))}))}))},t.prototype._run=function(t,e,n){return new Promise((function(r,o){e[t].apply(e,O(n,[function(n,i){n?(o(n),e.end()):(r(i||"operation successful."),"get"===t?i.once("close",(function(){return e.end()})):e.end())}]))}))},t.prototype.connect=function(){var t=this;return new Promise((function(e,n){var r=new t.driver;r.once("ready",(function(){e(r)})),r.once("error",(function(t){n(t),r.end()})),r.connect(d({},t.options))}))},t}(),j=n(4),x=function(t){return Object(j.createHash)("md5").update(t).digest("hex")},_=function(){return(_=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}).apply(this,arguments)};function S(t){var e=t.url,n=t.options,r=void 0===n?{}:n,o=t.ServiceImp,i=t.Grpc,u=_({"grpc.max_send_message_length":-1,"grpc.max_receive_message_length":-1},r),c=new i.Client(e,i.credentials.createInsecure(),u);return new o({request:function(t,e,n){var r="/"+t+"/"+e;return new Promise((function(t,e){var o=function(t){return t};c.makeUnaryRequest(r,o,o,n,(function(n,r){n?e(n):t(r)}))}))}})}}])}));
@@ -1,8 +1,8 @@
1
- import { GrpcService } from './GrpcService';
2
- interface GrpcServiceOptions {
3
- client: string;
4
- requestType: string;
5
- service?: string;
1
+ interface CreateClientProps {
2
+ url: string;
3
+ options?: any;
4
+ ServiceImp: any;
5
+ Grpc: any;
6
6
  }
7
- export declare const createGrpcService: import("pubo-utils/lib/factory").CreateFactory<GrpcServiceOptions, GrpcService>;
7
+ export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc }: CreateClientProps): T;
8
8
  export {};
package/es/grpc/index.js CHANGED
@@ -14,10 +14,40 @@ var __assign = this && this.__assign || function () {
14
14
  return __assign.apply(this, arguments);
15
15
  };
16
16
 
17
- import { superFactory } from 'pubo-utils';
18
- import { GrpcService } from './GrpcService';
19
- export var createGrpcService = superFactory(function (config, key) {
20
- return new GrpcService(__assign(__assign({}, config), {
21
- serviceName: config.service || key
22
- }));
23
- });
17
+ export function createRpcClient(_a) {
18
+ var url = _a.url,
19
+ _b = _a.options,
20
+ options = _b === void 0 ? {} : _b,
21
+ ServiceImp = _a.ServiceImp,
22
+ Grpc = _a.Grpc;
23
+
24
+ var opt = __assign({
25
+ 'grpc.max_send_message_length': -1,
26
+ 'grpc.max_receive_message_length': -1
27
+ }, options);
28
+
29
+ var connection = new Grpc.Client(url, Grpc.credentials.createInsecure(), opt);
30
+
31
+ var request = function request(service, method, data) {
32
+ var path = "/" + service + "/" + method;
33
+ return new Promise(function (resolve, reject) {
34
+ var resultCallback = function resultCallback(err, res) {
35
+ if (err) {
36
+ reject(err);
37
+ } else {
38
+ resolve(res);
39
+ }
40
+ };
41
+
42
+ var passThrough = function passThrough(argument) {
43
+ return argument;
44
+ };
45
+
46
+ connection.makeUnaryRequest(path, passThrough, passThrough, data, resultCallback);
47
+ });
48
+ };
49
+
50
+ return new ServiceImp({
51
+ request: request
52
+ });
53
+ }
package/es/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export { JsonStorage } from './storage/json';
2
2
  export { initRtspVideos } from './rtsp-video';
3
3
  export { FtpClient } from './ftp-client';
4
4
  export { md5 } from './md5';
5
- export { createGrpcService } from './grpc';
6
- export { GrpcPool } from './grpc/GrpcPool';
5
+ export { createRpcClient } from './grpc';
package/es/index.js CHANGED
@@ -2,5 +2,4 @@ export { JsonStorage } from './storage/json';
2
2
  export { initRtspVideos } from './rtsp-video';
3
3
  export { FtpClient } from './ftp-client';
4
4
  export { md5 } from './md5';
5
- export { createGrpcService } from './grpc';
6
- export { GrpcPool } from './grpc/GrpcPool';
5
+ export { createRpcClient } from './grpc';
@@ -1,8 +1,8 @@
1
- import { GrpcService } from './GrpcService';
2
- interface GrpcServiceOptions {
3
- client: string;
4
- requestType: string;
5
- service?: string;
1
+ interface CreateClientProps {
2
+ url: string;
3
+ options?: any;
4
+ ServiceImp: any;
5
+ Grpc: any;
6
6
  }
7
- export declare const createGrpcService: import("pubo-utils/lib/factory").CreateFactory<GrpcServiceOptions, GrpcService>;
7
+ export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc }: CreateClientProps): T;
8
8
  export {};
package/lib/grpc/index.js CHANGED
@@ -19,14 +19,44 @@ var __assign = this && this.__assign || function () {
19
19
  Object.defineProperty(exports, "__esModule", {
20
20
  value: true
21
21
  });
22
- exports.createGrpcService = void 0;
23
-
24
- var pubo_utils_1 = require("pubo-utils");
22
+ exports.createRpcClient = void 0;
23
+
24
+ function createRpcClient(_a) {
25
+ var url = _a.url,
26
+ _b = _a.options,
27
+ options = _b === void 0 ? {} : _b,
28
+ ServiceImp = _a.ServiceImp,
29
+ Grpc = _a.Grpc;
30
+
31
+ var opt = __assign({
32
+ 'grpc.max_send_message_length': -1,
33
+ 'grpc.max_receive_message_length': -1
34
+ }, options);
35
+
36
+ var connection = new Grpc.Client(url, Grpc.credentials.createInsecure(), opt);
37
+
38
+ var request = function request(service, method, data) {
39
+ var path = "/" + service + "/" + method;
40
+ return new Promise(function (resolve, reject) {
41
+ var resultCallback = function resultCallback(err, res) {
42
+ if (err) {
43
+ reject(err);
44
+ } else {
45
+ resolve(res);
46
+ }
47
+ };
48
+
49
+ var passThrough = function passThrough(argument) {
50
+ return argument;
51
+ };
52
+
53
+ connection.makeUnaryRequest(path, passThrough, passThrough, data, resultCallback);
54
+ });
55
+ };
25
56
 
26
- var GrpcService_1 = require("./GrpcService");
57
+ return new ServiceImp({
58
+ request: request
59
+ });
60
+ }
27
61
 
28
- exports.createGrpcService = pubo_utils_1.superFactory(function (config, key) {
29
- return new GrpcService_1.GrpcService(__assign(__assign({}, config), {
30
- serviceName: config.service || key
31
- }));
32
- });
62
+ exports.createRpcClient = createRpcClient;
package/lib/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export { JsonStorage } from './storage/json';
2
2
  export { initRtspVideos } from './rtsp-video';
3
3
  export { FtpClient } from './ftp-client';
4
4
  export { md5 } from './md5';
5
- export { createGrpcService } from './grpc';
6
- export { GrpcPool } from './grpc/GrpcPool';
5
+ export { createRpcClient } from './grpc';
package/lib/index.js CHANGED
@@ -42,18 +42,9 @@ Object.defineProperty(exports, "md5", {
42
42
 
43
43
  var grpc_1 = require("./grpc");
44
44
 
45
- Object.defineProperty(exports, "createGrpcService", {
45
+ Object.defineProperty(exports, "createRpcClient", {
46
46
  enumerable: true,
47
47
  get: function get() {
48
- return grpc_1.createGrpcService;
49
- }
50
- });
51
-
52
- var GrpcPool_1 = require("./grpc/GrpcPool");
53
-
54
- Object.defineProperty(exports, "GrpcPool", {
55
- enumerable: true,
56
- get: function get() {
57
- return GrpcPool_1.GrpcPool;
48
+ return grpc_1.createRpcClient;
58
49
  }
59
50
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pubo-node",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./es/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "@types/node": "^17.0.25",
23
23
  "grpc": "^1.24.11",
24
24
  "pako": "^2.0.4",
25
- "pubo-utils": "^1.0.35-alpha.0"
25
+ "pubo-utils": "^1.0.37"
26
26
  },
27
- "gitHead": "b8ce14e2272518a06835d63a07087d347823b19f"
27
+ "gitHead": "78eee2cbf3b652f8b4f330c9f21b7aced51c38f1"
28
28
  }
package/es/grpc/Grpc.d.ts DELETED
@@ -1,15 +0,0 @@
1
- export interface GrpcConfig {
2
- root: string;
3
- url: string;
4
- options?: any;
5
- }
6
- export declare class Grpc {
7
- private readonly config;
8
- private readonly options;
9
- private instance;
10
- private readonly RpcClient;
11
- common: any;
12
- proto: any;
13
- constructor(conf: GrpcConfig);
14
- get client(): any;
15
- }
package/es/grpc/Grpc.js DELETED
@@ -1,52 +0,0 @@
1
- var __assign = this && this.__assign || function () {
2
- __assign = Object.assign || function (t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
-
6
- for (var p in s) {
7
- if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
8
- }
9
- }
10
-
11
- return t;
12
- };
13
-
14
- return __assign.apply(this, arguments);
15
- };
16
-
17
- import * as grpc from 'grpc';
18
-
19
- var Grpc =
20
- /** @class */
21
- function () {
22
- function Grpc(conf) {
23
- this.options = {
24
- 'grpc.max_send_message_length': -1,
25
- 'grpc.max_receive_message_length': -1
26
- };
27
- this.instance = null;
28
- this.config = conf;
29
- this.common = require(this.config.root + "/common_pb"); // eslint-disable-next-line @typescript-eslint/no-var-requires
30
-
31
- this.RpcClient = require(this.config.root + "/service_grpc_pb").RpcClient;
32
- this.proto = global.proto;
33
- global.proto = {};
34
- }
35
-
36
- Object.defineProperty(Grpc.prototype, "client", {
37
- get: function get() {
38
- if (!this.instance) {
39
- var options = __assign(__assign({}, this.options), this.config.options || {});
40
-
41
- this.instance = new this.RpcClient(this.config.url, grpc.credentials.createInsecure(), options);
42
- }
43
-
44
- return this.instance;
45
- },
46
- enumerable: false,
47
- configurable: true
48
- });
49
- return Grpc;
50
- }();
51
-
52
- export { Grpc };
@@ -1,14 +0,0 @@
1
- import type { GrpcConfig } from './Grpc';
2
- import { Grpc } from './Grpc';
3
- export declare type GrpcPoolType = (GrpcConfig & {
4
- id: any;
5
- })[];
6
- export declare class GrpcPool {
7
- static pool: GrpcPoolType;
8
- private static instance;
9
- static getPool: () => GrpcPool;
10
- static setPool: (pool: GrpcPoolType) => void;
11
- private readonly list;
12
- constructor(list: GrpcPoolType);
13
- getGrpc(id: string): Grpc | undefined;
14
- }
@@ -1,43 +0,0 @@
1
- import { Grpc } from './Grpc';
2
-
3
- var GrpcPool =
4
- /** @class */
5
- function () {
6
- function GrpcPool(list) {
7
- this.list = [];
8
- this.list = list.map(function (conf) {
9
- return {
10
- id: conf.id,
11
- grpc: new Grpc(conf)
12
- };
13
- });
14
- }
15
-
16
- GrpcPool.prototype.getGrpc = function (id) {
17
- var _a;
18
-
19
- return (_a = this.list.find(function (item) {
20
- return item.id === id;
21
- })) === null || _a === void 0 ? void 0 : _a.grpc;
22
- };
23
-
24
- GrpcPool.pool = [];
25
- GrpcPool.instance = null;
26
-
27
- GrpcPool.getPool = function () {
28
- if (!GrpcPool.instance) {
29
- GrpcPool.instance = new GrpcPool(GrpcPool.pool);
30
- }
31
-
32
- return GrpcPool.instance;
33
- };
34
-
35
- GrpcPool.setPool = function (pool) {
36
- GrpcPool.pool = pool;
37
- GrpcPool.instance = new GrpcPool(GrpcPool.pool);
38
- };
39
-
40
- return GrpcPool;
41
- }();
42
-
43
- export { GrpcPool };
@@ -1,13 +0,0 @@
1
- export interface GrpcServiceConfig {
2
- client: string;
3
- serviceName: string;
4
- requestType: string;
5
- }
6
- export declare class GrpcService {
7
- private readonly config;
8
- private readonly grpc?;
9
- private readonly promise;
10
- constructor(conf: GrpcServiceConfig);
11
- serializer(requestType: string, payload?: any): any;
12
- send(request?: any): Promise<any>;
13
- }
@@ -1,295 +0,0 @@
1
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) {
3
- return value instanceof P ? value : new P(function (resolve) {
4
- resolve(value);
5
- });
6
- }
7
-
8
- return new (P || (P = Promise))(function (resolve, reject) {
9
- function fulfilled(value) {
10
- try {
11
- step(generator.next(value));
12
- } catch (e) {
13
- reject(e);
14
- }
15
- }
16
-
17
- function rejected(value) {
18
- try {
19
- step(generator["throw"](value));
20
- } catch (e) {
21
- reject(e);
22
- }
23
- }
24
-
25
- function step(result) {
26
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
27
- }
28
-
29
- step((generator = generator.apply(thisArg, _arguments || [])).next());
30
- });
31
- };
32
-
33
- var __generator = this && this.__generator || function (thisArg, body) {
34
- var _ = {
35
- label: 0,
36
- sent: function sent() {
37
- if (t[0] & 1) throw t[1];
38
- return t[1];
39
- },
40
- trys: [],
41
- ops: []
42
- },
43
- f,
44
- y,
45
- t,
46
- g;
47
- return g = {
48
- next: verb(0),
49
- "throw": verb(1),
50
- "return": verb(2)
51
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
52
- return this;
53
- }), g;
54
-
55
- function verb(n) {
56
- return function (v) {
57
- return step([n, v]);
58
- };
59
- }
60
-
61
- function step(op) {
62
- if (f) throw new TypeError("Generator is already executing.");
63
-
64
- while (_) {
65
- try {
66
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
67
- if (y = 0, t) op = [op[0] & 2, t.value];
68
-
69
- switch (op[0]) {
70
- case 0:
71
- case 1:
72
- t = op;
73
- break;
74
-
75
- case 4:
76
- _.label++;
77
- return {
78
- value: op[1],
79
- done: false
80
- };
81
-
82
- case 5:
83
- _.label++;
84
- y = op[1];
85
- op = [0];
86
- continue;
87
-
88
- case 7:
89
- op = _.ops.pop();
90
-
91
- _.trys.pop();
92
-
93
- continue;
94
-
95
- default:
96
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
97
- _ = 0;
98
- continue;
99
- }
100
-
101
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
102
- _.label = op[1];
103
- break;
104
- }
105
-
106
- if (op[0] === 6 && _.label < t[1]) {
107
- _.label = t[1];
108
- t = op;
109
- break;
110
- }
111
-
112
- if (t && _.label < t[2]) {
113
- _.label = t[2];
114
-
115
- _.ops.push(op);
116
-
117
- break;
118
- }
119
-
120
- if (t[2]) _.ops.pop();
121
-
122
- _.trys.pop();
123
-
124
- continue;
125
- }
126
-
127
- op = body.call(thisArg, _);
128
- } catch (e) {
129
- op = [6, e];
130
- y = 0;
131
- } finally {
132
- f = t = 0;
133
- }
134
- }
135
-
136
- if (op[0] & 5) throw op[1];
137
- return {
138
- value: op[0] ? op[1] : void 0,
139
- done: true
140
- };
141
- }
142
- };
143
-
144
- var __read = this && this.__read || function (o, n) {
145
- var m = typeof Symbol === "function" && o[Symbol.iterator];
146
- if (!m) return o;
147
- var i = m.call(o),
148
- r,
149
- ar = [],
150
- e;
151
-
152
- try {
153
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
154
- ar.push(r.value);
155
- }
156
- } catch (error) {
157
- e = {
158
- error: error
159
- };
160
- } finally {
161
- try {
162
- if (r && !r.done && (m = i["return"])) m.call(i);
163
- } finally {
164
- if (e) throw e.error;
165
- }
166
- }
167
-
168
- return ar;
169
- };
170
-
171
- var __spread = this && this.__spread || function () {
172
- for (var ar = [], i = 0; i < arguments.length; i++) {
173
- ar = ar.concat(__read(arguments[i]));
174
- }
175
-
176
- return ar;
177
- };
178
-
179
- var __values = this && this.__values || function (o) {
180
- var s = typeof Symbol === "function" && Symbol.iterator,
181
- m = s && o[s],
182
- i = 0;
183
- if (m) return m.call(o);
184
- if (o && typeof o.length === "number") return {
185
- next: function next() {
186
- if (o && i >= o.length) o = void 0;
187
- return {
188
- value: o && o[i++],
189
- done: !o
190
- };
191
- }
192
- };
193
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
194
- };
195
-
196
- import { GrpcPool } from './GrpcPool';
197
-
198
- function createRpcPromise(fn, grpc) {
199
- return function () {
200
- var args = [];
201
-
202
- for (var _i = 0; _i < arguments.length; _i++) {
203
- args[_i] = arguments[_i];
204
- }
205
-
206
- return new Promise(function (resolve, reject) {
207
- var client = grpc.client;
208
- fn.call.apply(fn, __spread([client], args, [function (err, response) {
209
- global.proto = grpc.proto;
210
-
211
- if (err) {
212
- reject(err);
213
- } else {
214
- resolve(response);
215
- }
216
- }]));
217
- });
218
- };
219
- }
220
-
221
- var GrpcService =
222
- /** @class */
223
- function () {
224
- function GrpcService(conf) {
225
- this.config = conf;
226
- this.grpc = GrpcPool.getPool().getGrpc(this.config.client);
227
-
228
- if (!this.grpc || !this.grpc.client[this.config.serviceName]) {
229
- throw new Error("rpc service not found:" + this.config.serviceName);
230
- }
231
-
232
- this.promise = createRpcPromise(this.grpc.client[this.config.serviceName], this.grpc);
233
- }
234
-
235
- GrpcService.prototype.serializer = function (requestType, payload) {
236
- var e_1, _a;
237
-
238
- if (payload === void 0) {
239
- payload = {};
240
- }
241
-
242
- if (!this.grpc) {
243
- throw new Error("rpc client not found:" + this.config.client);
244
- }
245
-
246
- global.proto = this.grpc.proto;
247
- var request = new this.grpc.common[requestType]();
248
-
249
- try {
250
- for (var _b = __values(Object.keys(request.toObject())), _c = _b.next(); !_c.done; _c = _b.next()) {
251
- var key = _c.value;
252
-
253
- if (payload[key] !== undefined) {
254
- request["set" + key[0].toUpperCase() + key.slice(1)](payload[key]);
255
- }
256
- }
257
- } catch (e_1_1) {
258
- e_1 = {
259
- error: e_1_1
260
- };
261
- } finally {
262
- try {
263
- if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
264
- } finally {
265
- if (e_1) throw e_1.error;
266
- }
267
- }
268
-
269
- return request;
270
- };
271
-
272
- GrpcService.prototype.send = function (request) {
273
- return __awaiter(this, void 0, void 0, function () {
274
- var res;
275
- return __generator(this, function (_a) {
276
- switch (_a.label) {
277
- case 0:
278
- return [4
279
- /*yield*/
280
- , this.promise(this.serializer(this.config.requestType, request))];
281
-
282
- case 1:
283
- res = _a.sent();
284
- return [2
285
- /*return*/
286
- , res.toObject()];
287
- }
288
- });
289
- });
290
- };
291
-
292
- return GrpcService;
293
- }();
294
-
295
- export { GrpcService };
@@ -1,15 +0,0 @@
1
- export interface GrpcConfig {
2
- root: string;
3
- url: string;
4
- options?: any;
5
- }
6
- export declare class Grpc {
7
- private readonly config;
8
- private readonly options;
9
- private instance;
10
- private readonly RpcClient;
11
- common: any;
12
- proto: any;
13
- constructor(conf: GrpcConfig);
14
- get client(): any;
15
- }
package/lib/grpc/Grpc.js DELETED
@@ -1,93 +0,0 @@
1
- "use strict";
2
-
3
- var __assign = this && this.__assign || function () {
4
- __assign = Object.assign || function (t) {
5
- for (var s, i = 1, n = arguments.length; i < n; i++) {
6
- s = arguments[i];
7
-
8
- for (var p in s) {
9
- if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
10
- }
11
- }
12
-
13
- return t;
14
- };
15
-
16
- return __assign.apply(this, arguments);
17
- };
18
-
19
- var __createBinding = this && this.__createBinding || (Object.create ? function (o, m, k, k2) {
20
- if (k2 === undefined) k2 = k;
21
- Object.defineProperty(o, k2, {
22
- enumerable: true,
23
- get: function get() {
24
- return m[k];
25
- }
26
- });
27
- } : function (o, m, k, k2) {
28
- if (k2 === undefined) k2 = k;
29
- o[k2] = m[k];
30
- });
31
-
32
- var __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function (o, v) {
33
- Object.defineProperty(o, "default", {
34
- enumerable: true,
35
- value: v
36
- });
37
- } : function (o, v) {
38
- o["default"] = v;
39
- });
40
-
41
- var __importStar = this && this.__importStar || function (mod) {
42
- if (mod && mod.__esModule) return mod;
43
- var result = {};
44
- if (mod != null) for (var k in mod) {
45
- if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
46
- }
47
-
48
- __setModuleDefault(result, mod);
49
-
50
- return result;
51
- };
52
-
53
- Object.defineProperty(exports, "__esModule", {
54
- value: true
55
- });
56
- exports.Grpc = void 0;
57
-
58
- var grpc = __importStar(require("grpc"));
59
-
60
- var Grpc =
61
- /** @class */
62
- function () {
63
- function Grpc(conf) {
64
- this.options = {
65
- 'grpc.max_send_message_length': -1,
66
- 'grpc.max_receive_message_length': -1
67
- };
68
- this.instance = null;
69
- this.config = conf;
70
- this.common = require(this.config.root + "/common_pb"); // eslint-disable-next-line @typescript-eslint/no-var-requires
71
-
72
- this.RpcClient = require(this.config.root + "/service_grpc_pb").RpcClient;
73
- this.proto = global.proto;
74
- global.proto = {};
75
- }
76
-
77
- Object.defineProperty(Grpc.prototype, "client", {
78
- get: function get() {
79
- if (!this.instance) {
80
- var options = __assign(__assign({}, this.options), this.config.options || {});
81
-
82
- this.instance = new this.RpcClient(this.config.url, grpc.credentials.createInsecure(), options);
83
- }
84
-
85
- return this.instance;
86
- },
87
- enumerable: false,
88
- configurable: true
89
- });
90
- return Grpc;
91
- }();
92
-
93
- exports.Grpc = Grpc;
@@ -1,14 +0,0 @@
1
- import type { GrpcConfig } from './Grpc';
2
- import { Grpc } from './Grpc';
3
- export declare type GrpcPoolType = (GrpcConfig & {
4
- id: any;
5
- })[];
6
- export declare class GrpcPool {
7
- static pool: GrpcPoolType;
8
- private static instance;
9
- static getPool: () => GrpcPool;
10
- static setPool: (pool: GrpcPoolType) => void;
11
- private readonly list;
12
- constructor(list: GrpcPoolType);
13
- getGrpc(id: string): Grpc | undefined;
14
- }
@@ -1,50 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.GrpcPool = void 0;
7
-
8
- var Grpc_1 = require("./Grpc");
9
-
10
- var GrpcPool =
11
- /** @class */
12
- function () {
13
- function GrpcPool(list) {
14
- this.list = [];
15
- this.list = list.map(function (conf) {
16
- return {
17
- id: conf.id,
18
- grpc: new Grpc_1.Grpc(conf)
19
- };
20
- });
21
- }
22
-
23
- GrpcPool.prototype.getGrpc = function (id) {
24
- var _a;
25
-
26
- return (_a = this.list.find(function (item) {
27
- return item.id === id;
28
- })) === null || _a === void 0 ? void 0 : _a.grpc;
29
- };
30
-
31
- GrpcPool.pool = [];
32
- GrpcPool.instance = null;
33
-
34
- GrpcPool.getPool = function () {
35
- if (!GrpcPool.instance) {
36
- GrpcPool.instance = new GrpcPool(GrpcPool.pool);
37
- }
38
-
39
- return GrpcPool.instance;
40
- };
41
-
42
- GrpcPool.setPool = function (pool) {
43
- GrpcPool.pool = pool;
44
- GrpcPool.instance = new GrpcPool(GrpcPool.pool);
45
- };
46
-
47
- return GrpcPool;
48
- }();
49
-
50
- exports.GrpcPool = GrpcPool;
@@ -1,13 +0,0 @@
1
- export interface GrpcServiceConfig {
2
- client: string;
3
- serviceName: string;
4
- requestType: string;
5
- }
6
- export declare class GrpcService {
7
- private readonly config;
8
- private readonly grpc?;
9
- private readonly promise;
10
- constructor(conf: GrpcServiceConfig);
11
- serializer(requestType: string, payload?: any): any;
12
- send(request?: any): Promise<any>;
13
- }
@@ -1,302 +0,0 @@
1
- "use strict";
2
-
3
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
4
- function adopt(value) {
5
- return value instanceof P ? value : new P(function (resolve) {
6
- resolve(value);
7
- });
8
- }
9
-
10
- return new (P || (P = Promise))(function (resolve, reject) {
11
- function fulfilled(value) {
12
- try {
13
- step(generator.next(value));
14
- } catch (e) {
15
- reject(e);
16
- }
17
- }
18
-
19
- function rejected(value) {
20
- try {
21
- step(generator["throw"](value));
22
- } catch (e) {
23
- reject(e);
24
- }
25
- }
26
-
27
- function step(result) {
28
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
29
- }
30
-
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
-
35
- var __generator = this && this.__generator || function (thisArg, body) {
36
- var _ = {
37
- label: 0,
38
- sent: function sent() {
39
- if (t[0] & 1) throw t[1];
40
- return t[1];
41
- },
42
- trys: [],
43
- ops: []
44
- },
45
- f,
46
- y,
47
- t,
48
- g;
49
- return g = {
50
- next: verb(0),
51
- "throw": verb(1),
52
- "return": verb(2)
53
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
54
- return this;
55
- }), g;
56
-
57
- function verb(n) {
58
- return function (v) {
59
- return step([n, v]);
60
- };
61
- }
62
-
63
- function step(op) {
64
- if (f) throw new TypeError("Generator is already executing.");
65
-
66
- while (_) {
67
- try {
68
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
69
- if (y = 0, t) op = [op[0] & 2, t.value];
70
-
71
- switch (op[0]) {
72
- case 0:
73
- case 1:
74
- t = op;
75
- break;
76
-
77
- case 4:
78
- _.label++;
79
- return {
80
- value: op[1],
81
- done: false
82
- };
83
-
84
- case 5:
85
- _.label++;
86
- y = op[1];
87
- op = [0];
88
- continue;
89
-
90
- case 7:
91
- op = _.ops.pop();
92
-
93
- _.trys.pop();
94
-
95
- continue;
96
-
97
- default:
98
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
99
- _ = 0;
100
- continue;
101
- }
102
-
103
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
104
- _.label = op[1];
105
- break;
106
- }
107
-
108
- if (op[0] === 6 && _.label < t[1]) {
109
- _.label = t[1];
110
- t = op;
111
- break;
112
- }
113
-
114
- if (t && _.label < t[2]) {
115
- _.label = t[2];
116
-
117
- _.ops.push(op);
118
-
119
- break;
120
- }
121
-
122
- if (t[2]) _.ops.pop();
123
-
124
- _.trys.pop();
125
-
126
- continue;
127
- }
128
-
129
- op = body.call(thisArg, _);
130
- } catch (e) {
131
- op = [6, e];
132
- y = 0;
133
- } finally {
134
- f = t = 0;
135
- }
136
- }
137
-
138
- if (op[0] & 5) throw op[1];
139
- return {
140
- value: op[0] ? op[1] : void 0,
141
- done: true
142
- };
143
- }
144
- };
145
-
146
- var __read = this && this.__read || function (o, n) {
147
- var m = typeof Symbol === "function" && o[Symbol.iterator];
148
- if (!m) return o;
149
- var i = m.call(o),
150
- r,
151
- ar = [],
152
- e;
153
-
154
- try {
155
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
156
- ar.push(r.value);
157
- }
158
- } catch (error) {
159
- e = {
160
- error: error
161
- };
162
- } finally {
163
- try {
164
- if (r && !r.done && (m = i["return"])) m.call(i);
165
- } finally {
166
- if (e) throw e.error;
167
- }
168
- }
169
-
170
- return ar;
171
- };
172
-
173
- var __spread = this && this.__spread || function () {
174
- for (var ar = [], i = 0; i < arguments.length; i++) {
175
- ar = ar.concat(__read(arguments[i]));
176
- }
177
-
178
- return ar;
179
- };
180
-
181
- var __values = this && this.__values || function (o) {
182
- var s = typeof Symbol === "function" && Symbol.iterator,
183
- m = s && o[s],
184
- i = 0;
185
- if (m) return m.call(o);
186
- if (o && typeof o.length === "number") return {
187
- next: function next() {
188
- if (o && i >= o.length) o = void 0;
189
- return {
190
- value: o && o[i++],
191
- done: !o
192
- };
193
- }
194
- };
195
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
196
- };
197
-
198
- Object.defineProperty(exports, "__esModule", {
199
- value: true
200
- });
201
- exports.GrpcService = void 0;
202
-
203
- var GrpcPool_1 = require("./GrpcPool");
204
-
205
- function createRpcPromise(fn, grpc) {
206
- return function () {
207
- var args = [];
208
-
209
- for (var _i = 0; _i < arguments.length; _i++) {
210
- args[_i] = arguments[_i];
211
- }
212
-
213
- return new Promise(function (resolve, reject) {
214
- var client = grpc.client;
215
- fn.call.apply(fn, __spread([client], args, [function (err, response) {
216
- global.proto = grpc.proto;
217
-
218
- if (err) {
219
- reject(err);
220
- } else {
221
- resolve(response);
222
- }
223
- }]));
224
- });
225
- };
226
- }
227
-
228
- var GrpcService =
229
- /** @class */
230
- function () {
231
- function GrpcService(conf) {
232
- this.config = conf;
233
- this.grpc = GrpcPool_1.GrpcPool.getPool().getGrpc(this.config.client);
234
-
235
- if (!this.grpc || !this.grpc.client[this.config.serviceName]) {
236
- throw new Error("rpc service not found:" + this.config.serviceName);
237
- }
238
-
239
- this.promise = createRpcPromise(this.grpc.client[this.config.serviceName], this.grpc);
240
- }
241
-
242
- GrpcService.prototype.serializer = function (requestType, payload) {
243
- var e_1, _a;
244
-
245
- if (payload === void 0) {
246
- payload = {};
247
- }
248
-
249
- if (!this.grpc) {
250
- throw new Error("rpc client not found:" + this.config.client);
251
- }
252
-
253
- global.proto = this.grpc.proto;
254
- var request = new this.grpc.common[requestType]();
255
-
256
- try {
257
- for (var _b = __values(Object.keys(request.toObject())), _c = _b.next(); !_c.done; _c = _b.next()) {
258
- var key = _c.value;
259
-
260
- if (payload[key] !== undefined) {
261
- request["set" + key[0].toUpperCase() + key.slice(1)](payload[key]);
262
- }
263
- }
264
- } catch (e_1_1) {
265
- e_1 = {
266
- error: e_1_1
267
- };
268
- } finally {
269
- try {
270
- if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
271
- } finally {
272
- if (e_1) throw e_1.error;
273
- }
274
- }
275
-
276
- return request;
277
- };
278
-
279
- GrpcService.prototype.send = function (request) {
280
- return __awaiter(this, void 0, void 0, function () {
281
- var res;
282
- return __generator(this, function (_a) {
283
- switch (_a.label) {
284
- case 0:
285
- return [4
286
- /*yield*/
287
- , this.promise(this.serializer(this.config.requestType, request))];
288
-
289
- case 1:
290
- res = _a.sent();
291
- return [2
292
- /*return*/
293
- , res.toObject()];
294
- }
295
- });
296
- });
297
- };
298
-
299
- return GrpcService;
300
- }();
301
-
302
- exports.GrpcService = GrpcService;