pubo-node 1.0.69 → 1.0.73

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,(()=>(()=>{"use strict";var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{FtpClient:()=>x,JsonStorage:()=>i,PuboFileSystem:()=>I,RtspVideosManager:()=>g,createRpcClient:()=>k,initRtspVideos:()=>b,md5:()=>q});const r=require("fs"),n=require("pako");var o=function(){return o=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},o.apply(this,arguments)},i=function(){function t(t){this._state={},this.path=t,this.init()}return t.prototype.init=function(){try{var t=(0,r.readFileSync)(this.path),e=n.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=n.deflate(JSON.stringify(t)),o=Buffer.from(e);(0,r.writeFile)(this.path,o,(function(t){return t&&console.log(t)})),this._state=t},enumerable:!1,configurable:!0}),t.prototype.get=function(t){return t?this.state[t]:o({},this.state)},t.prototype.set=function(t,e){var r=o({},this.state);r[t]=e,this.state=r},t.prototype.merge=function(t){this.state=o(o({},this._state),t)},t}();const s=require("path"),c=require("child_process"),u=require("pubo-utils");var a=function(){return a=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},a.apply(this,arguments)},l=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)s.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return s},p=function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))},f=function(){function t(t,e){var r=this;this.eventEmitter=new u.Emitter,this.dog=new u.WatchDog({limit:10,onTimeout:function(){return r.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: ".concat(this.url," try to reconnect;")),this.s.kill(),this.s=null)},t.prototype.connect=function(){var t=this;this.closeOld();var e=p(p(p(p([],l(this.options.input),!1),[this.url],!1),l(this.options.output),!1),["-"],!1);this.s=(0,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.prototype.destroy=function(){this.eventEmitter.clear(),this.dog.stop(),this.s&&(this.s.kill(),this.s=null)},t}(),h=function(){return h=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},h.apply(this,arguments)},y=function(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r},d=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)s.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return s},v=function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))},m=function(){function t(){this.path=(0,s.resolve)("resources/temp/videos.json")}return t.prototype.async=function(t){try{(0,r.writeFileSync)(this.path,JSON.stringify(t))}catch(t){console.log(t)}},Object.defineProperty(t.prototype,"data",{get:function(){try{var t=(0,r.readFileSync)(this.path);return console.log(t.toString()),JSON.parse(t.toString())}catch(t){return console.log(t),null}},enumerable:!1,configurable:!0}),t}(),g=function(){function t(t){this._system=[],this._customer=[],this.system=[],this.customer=[],this.storage=new m,this._system=t.map((function(t){return h(h({},t),{system:!0})})),this._customer=this.storage.data||[],this.system=t.map((function(t){var e=t.url,r=y(t,["url"]);return new f(e,r)})),this.customer=this._customer.map((function(t){var e=t.url,r=y(t,["url"]);return new f(e,r)}))}return Object.defineProperty(t.prototype,"list",{get:function(){return v(v([],d(this.system),!1),d(this.customer),!1)},enumerable:!1,configurable:!0}),t.prototype.getVideos=function(){return v(v([],d(this._system),!1),d(this._customer),!1)},t.prototype.create=function(t){if(this._customer.some((function(e){return e.url===t.url})))throw new Error("该地址已存在");var e=new f(t.url,t);this._customer.push(t),this.customer.push(e),this.storage.async(this._customer)},t.prototype.remove=function(t){if(t<this.system.length)throw new Error("系统配置不可删除");if(t>this.system.length+this.customer.length-1)throw new Error("超限");this._customer.splice(t-this.system.length-1,1),this.customer.splice(t-this.system.length-1,1)[0].destroy(),this.storage.async(this._customer)},t}(),b=function(t){var e=t.app,r=t.server,n=t.videos,o=t.path,i=t.expressWs,s=new g(n);return e.ws||i(e,r),e.ws(o,(function(t,e){var r=e.query.channel,n=void 0===r?0:r,o=null;s.list[n]&&(o=s.list[n].on("data",(function(e){t.send(e)}))),t.on("close",(function(){o&&s.list[n]&&s.list[n].cancel(o)}))})),s},w=function(){return w=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},w.apply(this,arguments)},O=function(t,e,r,n){return new(r||(r=Promise))((function(o,i){function s(t){try{u(n.next(t))}catch(t){i(t)}}function c(t){try{u(n.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,c)}u((n=n.apply(t,e||[])).next())}))},_=function(t,e){var r,n,o,i,s={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(c){return function(u){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,n=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!((o=(o=s.trys).length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=e.call(t,s)}catch(t){c=[6,t],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}},S=function(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r},j=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)s.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return s},P=function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))},x=function(){function t(t){var e=t.driver,r=S(t,["driver"]);this.queue=new u.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=r}return t.prototype._push=function(t){var e=this;return function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return e.queue.push((function(){return t.call.apply(t,P([e],j(r),!1))}))}},t.prototype._bind=function(t){var e=this;return this._push((function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return O(e,void 0,void 0,(function(){var e;return _(this,(function(n){switch(n.label){case 0:return[4,this.connect()];case 1:return e=n.sent(),[2,this._run(t,e,r)]}}))}))}))},t.prototype._run=function(t,e,r){return new Promise((function(n,o){e[t].apply(e,P(P([],j(r),!1),[function(r,i){r?(o(r),e.end()):(n(i||"operation successful."),"get"===t?i.once("close",(function(){return e.end()})):e.end())}],!1))}))},t.prototype.connect=function(){var t=this;return new Promise((function(e,r){var n=new t.driver;n.once("ready",(function(){e(n)})),n.once("error",(function(t){r(t),n.end()})),n.connect(w({},t.options))}))},t}();const E=require("crypto");var q=function(t){return(0,E.createHash)("md5").update(t).digest("hex")},F=function(){return F=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},F.apply(this,arguments)};function k(t){var e=t.url,r=t.options,n=void 0===r?{}:r,o=t.ServiceImp,i=t.Grpc,s=F({"grpc.max_send_message_length":-1,"grpc.max_receive_message_length":-1},n),c=new i.Client(e,i.credentials.createInsecure(),s);return new o({request:function(t,e,r){var n="/".concat(t,"/").concat(e);return new Promise((function(t,e){var o=function(t){return t};c.makeUnaryRequest(n,o,o,r,(function(r,n){r?e(r):t(n)}))}))}})}var A=function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var n,o,i=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=i.next()).done;)s.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return s},J=function(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))},M=function(t){return function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return new Promise((function(r,n){t.apply(void 0,J(J([],A(e),!1),[function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];t&&n(t),e.length<2?r(e[0]):r(J([],A(e),!1))}],!1))}))}},I={read:M(r.read),readFile:M(r.readFile),writeFile:M(r.writeFile),readdir:M(r.readdir),open:M(r.open),close:M(r.close),write:M(r.write)};return e})()));
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,(()=>(()=>{"use strict";var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{FtpClient:()=>x,FtpClientPool:()=>E,JsonStorage:()=>i,PuboFileSystem:()=>J,RtspVideosManager:()=>m,createRpcClient:()=>A,initRtspVideos:()=>b,md5:()=>k});const n=require("fs"),r=require("pako");var o=function(){return o=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},o.apply(this,arguments)},i=function(){function t(t){this._state={},this.path=t,this.init()}return t.prototype.init=function(){try{var t=(0,n.readFileSync)(this.path),e=r.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=r.deflate(JSON.stringify(t)),o=Buffer.from(e);(0,n.writeFile)(this.path,o,(function(t){return t&&console.log(t)})),this._state=t},enumerable:!1,configurable:!0}),t.prototype.get=function(t){return t?this.state[t]:o({},this.state)},t.prototype.set=function(t,e){var n=o({},this.state);n[t]=e,this.state=n},t.prototype.merge=function(t){this.state=o(o({},this._state),t)},t}();const s=require("path"),c=require("child_process"),u=require("pubo-utils");var 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},a.apply(this,arguments)},l=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)s.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 s},h=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,e){var n=this;this.eventEmitter=new u.Emitter,this.dog=new u.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: ".concat(this.url," try to reconnect;")),this.s.kill(),this.s=null)},t.prototype.connect=function(){var t=this;this.closeOld();var e=h(h(h(h([],l(this.options.input),!1),[this.url],!1),l(this.options.output),!1),["-"],!1);this.s=(0,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.prototype.destroy=function(){this.eventEmitter.clear(),this.dog.stop(),this.s&&(this.s.kill(),this.s=null)},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)},y=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},d=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)s.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 s},v=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(){function t(){this.path=(0,s.resolve)("resources/temp/videos.json")}return t.prototype.async=function(t){try{(0,n.writeFileSync)(this.path,JSON.stringify(t))}catch(t){console.log(t)}},Object.defineProperty(t.prototype,"data",{get:function(){try{var t=(0,n.readFileSync)(this.path);return console.log(t.toString()),JSON.parse(t.toString())}catch(t){return console.log(t),null}},enumerable:!1,configurable:!0}),t}(),m=function(){function t(t){this._system=[],this._customer=[],this.system=[],this.customer=[],this.storage=new g,this._system=t.map((function(t){return p(p({},t),{system:!0})})),this._customer=this.storage.data||[],this.system=t.map((function(t){var e=t.url,n=y(t,["url"]);return new f(e,n)})),this.customer=this._customer.map((function(t){var e=t.url,n=y(t,["url"]);return new f(e,n)}))}return Object.defineProperty(t.prototype,"list",{get:function(){return v(v([],d(this.system),!1),d(this.customer),!1)},enumerable:!1,configurable:!0}),t.prototype.getVideos=function(){return v(v([],d(this._system),!1),d(this._customer),!1)},t.prototype.create=function(t){if(this._customer.some((function(e){return e.url===t.url})))throw new Error("该地址已存在");var e=new f(t.url,t);this._customer.push(t),this.customer.push(e),this.storage.async(this._customer)},t.prototype.remove=function(t){if(t<this.system.length)throw new Error("系统配置不可删除");if(t>this.system.length+this.customer.length-1)throw new Error("超限");this._customer.splice(t-this.system.length-1,1),this.customer.splice(t-this.system.length-1,1)[0].destroy(),this.storage.async(this._customer)},t}(),b=function(t){var e=t.app,n=t.server,r=t.videos,o=t.path,i=t.expressWs,s=new m(r);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;s.list[r]&&(o=s.list[r].on("data",(function(e){t.send(e)}))),t.on("close",(function(){o&&s.list[r]&&s.list[r].cancel(o)}))})),s},w=function(){return w=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},w.apply(this,arguments)},O=function(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{u(r.next(t))}catch(t){i(t)}}function c(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,c)}u((r=r.apply(t,e||[])).next())}))},S=function(t,e){var n,r,o,i,s={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(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!((o=(o=s.trys).length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=e.call(t,s)}catch(t){c=[6,t],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}},j=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},_=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)s.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 s},P=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))},x=function(){function t(t){var e=t.driver,n=j(t,["driver"]);this.state={running:!1,connected:!1,destroyed:!1,connecting:!1},this._len=0,this.id=(0,u.random)(),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 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 O(this,void 0,void 0,(function(){var t=this;return S(this,(function(e){switch(e.label){case 0:return this.client||(this.client=new this.driver),this.state.destroyed=!1,this.state.connecting?[4,(0,u.waitFor)((function(){return t.state.connected}),{checkTime:1e3,timeout:1e4})]:[3,2];case 1:return e.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(w({},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 e=t.fn,n=t.args;return O(this,void 0,void 0,(function(){var t=this;return S(this,(function(r){switch(r.label){case 0:return this.len+=1,this.state.connected?[3,2]:[4,this.connect()];case 1:r.sent(),r.label=2;case 2:return this.state.running?[4,(0,u.waitFor)((function(){return!t.state.running}),{checkTime:1e3,timeout:6e6})]:[3,4];case 3:r.sent(),r.label=4;case 4:return this.state.running=!0,[2,new Promise((function(r,o){var i;(i=t.client)[e].apply(i,P(P([],_(n),!1),[function(t,e){t?o(t):r(e)}],!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 O(e,void 0,void 0,(function(){var e;return S(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 O(this,void 0,void 0,(function(){var e,n,r=this;return S(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}(),E=function(){function t(t){var e=t.maxConnection,n=void 0===e?5:e,r=j(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 x(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 O(e,void 0,void 0,(function(){var e,r,o;return S(this,(function(i){switch(i.label){case 0:return[4,(e=this.client)[t].apply(e,P([],_(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}();const F=require("crypto");var k=function(t){return(0,F.createHash)("md5").update(t).digest("hex")},q=function(){return q=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},q.apply(this,arguments)};function A(t){var e=t.url,n=t.options,r=void 0===n?{}:n,o=t.ServiceImp,i=t.Grpc,s=t.cert,c=q({"grpc.max_send_message_length":-1,"grpc.max_receive_message_length":-1},r),u=s?i.credentials.createSsl(s):i.credentials.createInsecure(),a=new i.Client(e,u,c);return new o({request:function(t,e,n){var r="/".concat(t,"/").concat(e);return new Promise((function(t,e){var o=function(t){return t};a.makeUnaryRequest(r,o,o,n,(function(n,r){n?e(n):t(r)}))}))}})}var C=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)s.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 s},T=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))},I=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,T(T([],C(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(T([],C(e),!1))}],!1))}))}},J={read:I(n.read),readFile:I(n.readFile),writeFile:I(n.writeFile),readdir:I(n.readdir),open:I(n.open),close:I(n.close),write:I(n.write)};return e})()));
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import type { Stream } from 'stream';
4
- interface FtpFile {
4
+ export interface FtpFile {
5
5
  name: string;
6
6
  owner: string;
7
7
  group: string;
@@ -9,31 +9,58 @@ interface FtpFile {
9
9
  date: Date;
10
10
  type: string;
11
11
  }
12
- interface FtpConnectOptions {
12
+ export interface FtpConnectOptions {
13
13
  user: string;
14
14
  host: string;
15
15
  password: string;
16
+ driver: any;
16
17
  }
17
- type GetFile = (path: string) => Promise<Stream>;
18
+ type GetFile = (path: string) => Promise<Buffer>;
18
19
  type PutFile = (input: string | Buffer | Stream, path: string) => Promise<string>;
19
20
  type DeleteFile = (path: string) => Promise<string>;
20
21
  type ListFiles = (path: string) => Promise<FtpFile[]>;
21
22
  type RenameFile = (path: string, old: string) => Promise<string>;
22
- export declare class FtpClient {
23
- private readonly driver;
24
- private readonly options;
25
- private readonly queue;
26
- constructor({ driver, ...options }: {
27
- driver: any;
28
- } & FtpConnectOptions);
23
+ export interface FtpClientInterface {
29
24
  get: GetFile;
30
25
  put: PutFile;
31
26
  delete: DeleteFile;
32
27
  list: ListFiles;
33
28
  rename: RenameFile;
34
- private _push;
35
- private _bind;
36
- private _run;
29
+ }
30
+ export declare class FtpClient implements FtpClientInterface {
31
+ private readonly driver;
32
+ private readonly options;
33
+ private readonly state;
34
+ private client;
35
+ private _len;
36
+ id: string;
37
+ constructor({ driver, ...options }: FtpConnectOptions);
38
+ get len(): number;
39
+ set len(n: number);
40
+ put: (...args: any[]) => Promise<any>;
41
+ delete: (...args: any[]) => Promise<any>;
42
+ list: (...args: any[]) => Promise<any>;
43
+ rename: (...args: any[]) => Promise<any>;
37
44
  private connect;
45
+ private close;
46
+ private run;
47
+ private bind;
48
+ get(...args: any[]): Promise<Buffer>;
49
+ }
50
+ export declare class FtpClientPool implements FtpClientInterface {
51
+ private readonly options;
52
+ private readonly maxConnection;
53
+ private readonly pool;
54
+ constructor({ maxConnection, ...options }: {
55
+ maxConnection?: number;
56
+ } & FtpConnectOptions);
57
+ get: (...args: any[]) => Promise<any>;
58
+ put: (...args: any[]) => Promise<any>;
59
+ delete: (...args: any[]) => Promise<any>;
60
+ list: (...args: any[]) => Promise<any>;
61
+ rename: (...args: any[]) => Promise<any>;
62
+ get len(): number;
63
+ private get client();
64
+ private bind;
38
65
  }
39
66
  export {};
@@ -162,86 +162,250 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
162
162
  }
163
163
  return to.concat(ar || Array.prototype.slice.call(from));
164
164
  };
165
- import { SyncQueue } from 'pubo-utils';
165
+ import { random, waitFor } from 'pubo-utils';
166
166
  var FtpClient = /** @class */function () {
167
167
  function FtpClient(_a) {
168
168
  var driver = _a.driver,
169
169
  options = __rest(_a, ["driver"]);
170
- this.queue = new SyncQueue();
171
- this.get = this._bind('get');
172
- this.put = this._bind('put');
173
- this["delete"] = this._bind('delete');
174
- this.list = this._bind('list');
175
- this.rename = this._bind('rename');
170
+ this.state = {
171
+ running: false,
172
+ connected: false,
173
+ destroyed: false,
174
+ connecting: false
175
+ };
176
+ this._len = 0;
177
+ this.id = random();
178
+ this.put = this.bind('put');
179
+ this["delete"] = this.bind('delete');
180
+ this.list = this.bind('list');
181
+ this.rename = this.bind('rename');
176
182
  this.driver = driver;
177
183
  this.options = options;
178
184
  }
179
- FtpClient.prototype._push = function (cb) {
180
- var _this = this;
181
- return function () {
182
- var args = [];
183
- for (var _i = 0; _i < arguments.length; _i++) {
184
- args[_i] = arguments[_i];
185
+ Object.defineProperty(FtpClient.prototype, "len", {
186
+ get: function get() {
187
+ return this._len;
188
+ },
189
+ set: function set(n) {
190
+ this._len = n;
191
+ if (this._len < 1) {
192
+ this.close();
185
193
  }
186
- return _this.queue.push(function () {
187
- return cb.call.apply(cb, __spreadArray([_this], __read(args), false));
194
+ },
195
+ enumerable: false,
196
+ configurable: true
197
+ });
198
+ FtpClient.prototype.connect = function () {
199
+ return __awaiter(this, void 0, void 0, function () {
200
+ var _this = this;
201
+ return __generator(this, function (_a) {
202
+ switch (_a.label) {
203
+ case 0:
204
+ if (!this.client) {
205
+ this.client = new this.driver();
206
+ }
207
+ this.state.destroyed = false;
208
+ if (!this.state.connecting) return [3 /*break*/, 2];
209
+ return [4 /*yield*/, waitFor(function () {
210
+ return _this.state.connected;
211
+ }, {
212
+ checkTime: 1000,
213
+ timeout: 10000
214
+ })];
215
+ case 1:
216
+ _a.sent();
217
+ return [2 /*return*/, 'connected'];
218
+ case 2:
219
+ this.state.connecting = true;
220
+ return [2 /*return*/, new Promise(function (resolve, reject) {
221
+ _this.client.once('ready', function () {
222
+ _this.state.connected = true;
223
+ resolve('connected');
224
+ _this.state.connecting = false;
225
+ });
226
+ _this.client.once('error', function (err) {
227
+ reject(err);
228
+ _this.close();
229
+ });
230
+ _this.client.connect(__assign({}, _this.options));
231
+ })];
232
+ }
188
233
  });
189
- };
234
+ });
190
235
  };
191
- FtpClient.prototype._bind = function (fn) {
236
+ FtpClient.prototype.close = function () {
237
+ this.client.end();
238
+ this.state.connected = false;
239
+ this.state.destroyed = true;
240
+ this.state.connecting = false;
241
+ this.client = null;
242
+ };
243
+ FtpClient.prototype.run = function (_a) {
244
+ var fn = _a.fn,
245
+ args = _a.args;
246
+ return __awaiter(this, void 0, void 0, function () {
247
+ var _this = this;
248
+ return __generator(this, function (_b) {
249
+ switch (_b.label) {
250
+ case 0:
251
+ this.len += 1;
252
+ if (!!this.state.connected) return [3 /*break*/, 2];
253
+ return [4 /*yield*/, this.connect()];
254
+ case 1:
255
+ _b.sent();
256
+ _b.label = 2;
257
+ case 2:
258
+ if (!this.state.running) return [3 /*break*/, 4];
259
+ return [4 /*yield*/, waitFor(function () {
260
+ return !_this.state.running;
261
+ }, {
262
+ checkTime: 1000,
263
+ timeout: 6000000
264
+ })];
265
+ case 3:
266
+ _b.sent();
267
+ _b.label = 4;
268
+ case 4:
269
+ this.state.running = true;
270
+ return [2 /*return*/, new Promise(function (resolve, reject) {
271
+ var _a;
272
+ (_a = _this.client)[fn].apply(_a, __spreadArray(__spreadArray([], __read(args), false), [function (err, res) {
273
+ if (err) {
274
+ reject(err);
275
+ } else {
276
+ resolve(res);
277
+ }
278
+ }], false));
279
+ })];
280
+ }
281
+ });
282
+ });
283
+ };
284
+ FtpClient.prototype.bind = function (fn) {
192
285
  var _this = this;
193
- return this._push(function () {
286
+ return function () {
194
287
  var args = [];
195
288
  for (var _i = 0; _i < arguments.length; _i++) {
196
289
  args[_i] = arguments[_i];
197
290
  }
198
291
  return __awaiter(_this, void 0, void 0, function () {
199
- var c;
292
+ var res;
200
293
  return __generator(this, function (_a) {
201
294
  switch (_a.label) {
202
295
  case 0:
203
- return [4 /*yield*/, this.connect()];
296
+ return [4 /*yield*/, this.run({
297
+ fn: fn,
298
+ args: args
299
+ })];
204
300
  case 1:
205
- c = _a.sent();
206
- return [2 /*return*/, this._run(fn, c, args)];
301
+ res = _a.sent();
302
+ this.state.running = false;
303
+ this.len -= 1;
304
+ return [2 /*return*/, res];
207
305
  }
208
306
  });
209
307
  });
210
- });
308
+ };
211
309
  };
212
- FtpClient.prototype._run = function (fn, c, args) {
213
- return new Promise(function (resolve, reject) {
214
- c[fn].apply(c, __spreadArray(__spreadArray([], __read(args), false), [function (err, res) {
215
- if (err) {
216
- reject(err);
217
- c.end();
218
- } else {
219
- resolve(res || 'operation successful.');
220
- if (fn === 'get') {
221
- res.once('close', function () {
222
- return c.end();
223
- });
224
- } else {
225
- c.end();
226
- }
310
+ FtpClient.prototype.get = function () {
311
+ var args = [];
312
+ for (var _i = 0; _i < arguments.length; _i++) {
313
+ args[_i] = arguments[_i];
314
+ }
315
+ return __awaiter(this, void 0, void 0, function () {
316
+ var res, stream;
317
+ var _this = this;
318
+ return __generator(this, function (_a) {
319
+ switch (_a.label) {
320
+ case 0:
321
+ res = Buffer.alloc(0);
322
+ return [4 /*yield*/, this.run({
323
+ fn: 'get',
324
+ args: args
325
+ })];
326
+ case 1:
327
+ stream = _a.sent();
328
+ return [2 /*return*/, new Promise(function (resolve) {
329
+ stream.on('data', function (chunk) {
330
+ res = Buffer.concat([res, chunk], res.byteLength + chunk.byteLength);
331
+ });
332
+ stream.on('end', function () {
333
+ resolve(res);
334
+ _this.state.running = false;
335
+ _this.len -= 1;
336
+ });
337
+ })];
227
338
  }
228
- }], false));
339
+ });
229
340
  });
230
341
  };
231
- FtpClient.prototype.connect = function () {
232
- var _this = this;
233
- return new Promise(function (resolve, reject) {
234
- var c = new _this.driver();
235
- c.once('ready', function () {
236
- resolve(c);
342
+ return FtpClient;
343
+ }();
344
+ export { FtpClient };
345
+ var FtpClientPool = /** @class */function () {
346
+ function FtpClientPool(_a) {
347
+ var _b = _a.maxConnection,
348
+ maxConnection = _b === void 0 ? 5 : _b,
349
+ options = __rest(_a, ["maxConnection"]);
350
+ this.pool = [];
351
+ this.get = this.bind('get');
352
+ this.put = this.bind('put');
353
+ this["delete"] = this.bind('delete');
354
+ this.list = this.bind('list');
355
+ this.rename = this.bind('rename');
356
+ this.options = options;
357
+ this.maxConnection = maxConnection;
358
+ }
359
+ Object.defineProperty(FtpClientPool.prototype, "len", {
360
+ get: function get() {
361
+ return this.pool.length;
362
+ },
363
+ enumerable: false,
364
+ configurable: true
365
+ });
366
+ Object.defineProperty(FtpClientPool.prototype, "client", {
367
+ get: function get() {
368
+ if (this.pool.length < this.maxConnection) {
369
+ var client = new FtpClient(this.options);
370
+ this.pool.push(client);
371
+ return client;
372
+ }
373
+ this.pool.sort(function (a, b) {
374
+ return a.len - b.len;
237
375
  });
238
- c.once('error', function (err) {
239
- reject(err);
240
- c.end();
376
+ return this.pool[0];
377
+ },
378
+ enumerable: false,
379
+ configurable: true
380
+ });
381
+ FtpClientPool.prototype.bind = function (fn) {
382
+ var _this = this;
383
+ return function () {
384
+ var args = [];
385
+ for (var _i = 0; _i < arguments.length; _i++) {
386
+ args[_i] = arguments[_i];
387
+ }
388
+ return __awaiter(_this, void 0, void 0, function () {
389
+ var client, res, index;
390
+ return __generator(this, function (_a) {
391
+ switch (_a.label) {
392
+ case 0:
393
+ client = this.client;
394
+ return [4 /*yield*/, client[fn].apply(client, __spreadArray([], __read(args), false))];
395
+ case 1:
396
+ res = _a.sent();
397
+ if (client.len < 1) {
398
+ index = this.pool.findIndex(function (item) {
399
+ return item.id === client.id;
400
+ });
401
+ this.pool.splice(index, 1);
402
+ }
403
+ return [2 /*return*/, res];
404
+ }
405
+ });
241
406
  });
242
- c.connect(__assign({}, _this.options));
243
- });
407
+ };
244
408
  };
245
- return FtpClient;
409
+ return FtpClientPool;
246
410
  }();
247
- export { FtpClient };
411
+ export { FtpClientPool };
@@ -1,8 +1,10 @@
1
+ /// <reference types="node" />
1
2
  interface CreateClientProps {
2
3
  url: string;
3
4
  options?: any;
4
5
  ServiceImp: any;
5
6
  Grpc: any;
7
+ cert?: Buffer;
6
8
  }
7
- export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc }: CreateClientProps): T;
9
+ export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc, cert }: CreateClientProps): T;
8
10
  export {};
package/es/grpc/index.js CHANGED
@@ -13,12 +13,14 @@ export function createRpcClient(_a) {
13
13
  _b = _a.options,
14
14
  options = _b === void 0 ? {} : _b,
15
15
  ServiceImp = _a.ServiceImp,
16
- Grpc = _a.Grpc;
16
+ Grpc = _a.Grpc,
17
+ cert = _a.cert;
17
18
  var opt = __assign({
18
19
  'grpc.max_send_message_length': -1,
19
20
  'grpc.max_receive_message_length': -1
20
21
  }, options);
21
- var connection = new Grpc.Client(url, Grpc.credentials.createInsecure(), opt);
22
+ var credentials = cert ? Grpc.credentials.createSsl(cert) : Grpc.credentials.createInsecure();
23
+ var connection = new Grpc.Client(url, credentials, opt);
22
24
  var request = function request(service, method, data) {
23
25
  var path = "/".concat(service, "/").concat(method);
24
26
  return new Promise(function (resolve, reject) {
package/es/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { initRtspVideos, RtspVideosManager } from './rtsp-video';
3
- export { FtpClient } from './ftp-client';
3
+ export { FtpClient, FtpClientPool, FtpConnectOptions, FtpFile } from './ftp-client';
4
4
  export { md5 } from './md5';
5
5
  export { createRpcClient } from './grpc';
6
6
  export { PuboFileSystem } from './file-system';
package/es/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { initRtspVideos, RtspVideosManager } from './rtsp-video';
3
- export { FtpClient } from './ftp-client';
3
+ export { FtpClient, FtpClientPool } from './ftp-client';
4
4
  export { md5 } from './md5';
5
5
  export { createRpcClient } from './grpc';
6
6
  export { PuboFileSystem } from './file-system';
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import type { Stream } from 'stream';
4
- interface FtpFile {
4
+ export interface FtpFile {
5
5
  name: string;
6
6
  owner: string;
7
7
  group: string;
@@ -9,31 +9,58 @@ interface FtpFile {
9
9
  date: Date;
10
10
  type: string;
11
11
  }
12
- interface FtpConnectOptions {
12
+ export interface FtpConnectOptions {
13
13
  user: string;
14
14
  host: string;
15
15
  password: string;
16
+ driver: any;
16
17
  }
17
- type GetFile = (path: string) => Promise<Stream>;
18
+ type GetFile = (path: string) => Promise<Buffer>;
18
19
  type PutFile = (input: string | Buffer | Stream, path: string) => Promise<string>;
19
20
  type DeleteFile = (path: string) => Promise<string>;
20
21
  type ListFiles = (path: string) => Promise<FtpFile[]>;
21
22
  type RenameFile = (path: string, old: string) => Promise<string>;
22
- export declare class FtpClient {
23
- private readonly driver;
24
- private readonly options;
25
- private readonly queue;
26
- constructor({ driver, ...options }: {
27
- driver: any;
28
- } & FtpConnectOptions);
23
+ export interface FtpClientInterface {
29
24
  get: GetFile;
30
25
  put: PutFile;
31
26
  delete: DeleteFile;
32
27
  list: ListFiles;
33
28
  rename: RenameFile;
34
- private _push;
35
- private _bind;
36
- private _run;
29
+ }
30
+ export declare class FtpClient implements FtpClientInterface {
31
+ private readonly driver;
32
+ private readonly options;
33
+ private readonly state;
34
+ private client;
35
+ private _len;
36
+ id: string;
37
+ constructor({ driver, ...options }: FtpConnectOptions);
38
+ get len(): number;
39
+ set len(n: number);
40
+ put: (...args: any[]) => Promise<any>;
41
+ delete: (...args: any[]) => Promise<any>;
42
+ list: (...args: any[]) => Promise<any>;
43
+ rename: (...args: any[]) => Promise<any>;
37
44
  private connect;
45
+ private close;
46
+ private run;
47
+ private bind;
48
+ get(...args: any[]): Promise<Buffer>;
49
+ }
50
+ export declare class FtpClientPool implements FtpClientInterface {
51
+ private readonly options;
52
+ private readonly maxConnection;
53
+ private readonly pool;
54
+ constructor({ maxConnection, ...options }: {
55
+ maxConnection?: number;
56
+ } & FtpConnectOptions);
57
+ get: (...args: any[]) => Promise<any>;
58
+ put: (...args: any[]) => Promise<any>;
59
+ delete: (...args: any[]) => Promise<any>;
60
+ list: (...args: any[]) => Promise<any>;
61
+ rename: (...args: any[]) => Promise<any>;
62
+ get len(): number;
63
+ private get client();
64
+ private bind;
38
65
  }
39
66
  export {};
@@ -167,87 +167,251 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
167
167
  Object.defineProperty(exports, "__esModule", {
168
168
  value: true
169
169
  });
170
- exports.FtpClient = void 0;
170
+ exports.FtpClientPool = exports.FtpClient = void 0;
171
171
  var pubo_utils_1 = require("pubo-utils");
172
172
  var FtpClient = /** @class */function () {
173
173
  function FtpClient(_a) {
174
174
  var driver = _a.driver,
175
175
  options = __rest(_a, ["driver"]);
176
- this.queue = new pubo_utils_1.SyncQueue();
177
- this.get = this._bind('get');
178
- this.put = this._bind('put');
179
- this["delete"] = this._bind('delete');
180
- this.list = this._bind('list');
181
- this.rename = this._bind('rename');
176
+ this.state = {
177
+ running: false,
178
+ connected: false,
179
+ destroyed: false,
180
+ connecting: false
181
+ };
182
+ this._len = 0;
183
+ this.id = (0, pubo_utils_1.random)();
184
+ this.put = this.bind('put');
185
+ this["delete"] = this.bind('delete');
186
+ this.list = this.bind('list');
187
+ this.rename = this.bind('rename');
182
188
  this.driver = driver;
183
189
  this.options = options;
184
190
  }
185
- FtpClient.prototype._push = function (cb) {
186
- var _this = this;
187
- return function () {
188
- var args = [];
189
- for (var _i = 0; _i < arguments.length; _i++) {
190
- args[_i] = arguments[_i];
191
+ Object.defineProperty(FtpClient.prototype, "len", {
192
+ get: function get() {
193
+ return this._len;
194
+ },
195
+ set: function set(n) {
196
+ this._len = n;
197
+ if (this._len < 1) {
198
+ this.close();
191
199
  }
192
- return _this.queue.push(function () {
193
- return cb.call.apply(cb, __spreadArray([_this], __read(args), false));
200
+ },
201
+ enumerable: false,
202
+ configurable: true
203
+ });
204
+ FtpClient.prototype.connect = function () {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ var _this = this;
207
+ return __generator(this, function (_a) {
208
+ switch (_a.label) {
209
+ case 0:
210
+ if (!this.client) {
211
+ this.client = new this.driver();
212
+ }
213
+ this.state.destroyed = false;
214
+ if (!this.state.connecting) return [3 /*break*/, 2];
215
+ return [4 /*yield*/, (0, pubo_utils_1.waitFor)(function () {
216
+ return _this.state.connected;
217
+ }, {
218
+ checkTime: 1000,
219
+ timeout: 10000
220
+ })];
221
+ case 1:
222
+ _a.sent();
223
+ return [2 /*return*/, 'connected'];
224
+ case 2:
225
+ this.state.connecting = true;
226
+ return [2 /*return*/, new Promise(function (resolve, reject) {
227
+ _this.client.once('ready', function () {
228
+ _this.state.connected = true;
229
+ resolve('connected');
230
+ _this.state.connecting = false;
231
+ });
232
+ _this.client.once('error', function (err) {
233
+ reject(err);
234
+ _this.close();
235
+ });
236
+ _this.client.connect(__assign({}, _this.options));
237
+ })];
238
+ }
194
239
  });
195
- };
240
+ });
196
241
  };
197
- FtpClient.prototype._bind = function (fn) {
242
+ FtpClient.prototype.close = function () {
243
+ this.client.end();
244
+ this.state.connected = false;
245
+ this.state.destroyed = true;
246
+ this.state.connecting = false;
247
+ this.client = null;
248
+ };
249
+ FtpClient.prototype.run = function (_a) {
250
+ var fn = _a.fn,
251
+ args = _a.args;
252
+ return __awaiter(this, void 0, void 0, function () {
253
+ var _this = this;
254
+ return __generator(this, function (_b) {
255
+ switch (_b.label) {
256
+ case 0:
257
+ this.len += 1;
258
+ if (!!this.state.connected) return [3 /*break*/, 2];
259
+ return [4 /*yield*/, this.connect()];
260
+ case 1:
261
+ _b.sent();
262
+ _b.label = 2;
263
+ case 2:
264
+ if (!this.state.running) return [3 /*break*/, 4];
265
+ return [4 /*yield*/, (0, pubo_utils_1.waitFor)(function () {
266
+ return !_this.state.running;
267
+ }, {
268
+ checkTime: 1000,
269
+ timeout: 6000000
270
+ })];
271
+ case 3:
272
+ _b.sent();
273
+ _b.label = 4;
274
+ case 4:
275
+ this.state.running = true;
276
+ return [2 /*return*/, new Promise(function (resolve, reject) {
277
+ var _a;
278
+ (_a = _this.client)[fn].apply(_a, __spreadArray(__spreadArray([], __read(args), false), [function (err, res) {
279
+ if (err) {
280
+ reject(err);
281
+ } else {
282
+ resolve(res);
283
+ }
284
+ }], false));
285
+ })];
286
+ }
287
+ });
288
+ });
289
+ };
290
+ FtpClient.prototype.bind = function (fn) {
198
291
  var _this = this;
199
- return this._push(function () {
292
+ return function () {
200
293
  var args = [];
201
294
  for (var _i = 0; _i < arguments.length; _i++) {
202
295
  args[_i] = arguments[_i];
203
296
  }
204
297
  return __awaiter(_this, void 0, void 0, function () {
205
- var c;
298
+ var res;
206
299
  return __generator(this, function (_a) {
207
300
  switch (_a.label) {
208
301
  case 0:
209
- return [4 /*yield*/, this.connect()];
302
+ return [4 /*yield*/, this.run({
303
+ fn: fn,
304
+ args: args
305
+ })];
210
306
  case 1:
211
- c = _a.sent();
212
- return [2 /*return*/, this._run(fn, c, args)];
307
+ res = _a.sent();
308
+ this.state.running = false;
309
+ this.len -= 1;
310
+ return [2 /*return*/, res];
213
311
  }
214
312
  });
215
313
  });
216
- });
314
+ };
217
315
  };
218
- FtpClient.prototype._run = function (fn, c, args) {
219
- return new Promise(function (resolve, reject) {
220
- c[fn].apply(c, __spreadArray(__spreadArray([], __read(args), false), [function (err, res) {
221
- if (err) {
222
- reject(err);
223
- c.end();
224
- } else {
225
- resolve(res || 'operation successful.');
226
- if (fn === 'get') {
227
- res.once('close', function () {
228
- return c.end();
229
- });
230
- } else {
231
- c.end();
232
- }
316
+ FtpClient.prototype.get = function () {
317
+ var args = [];
318
+ for (var _i = 0; _i < arguments.length; _i++) {
319
+ args[_i] = arguments[_i];
320
+ }
321
+ return __awaiter(this, void 0, void 0, function () {
322
+ var res, stream;
323
+ var _this = this;
324
+ return __generator(this, function (_a) {
325
+ switch (_a.label) {
326
+ case 0:
327
+ res = Buffer.alloc(0);
328
+ return [4 /*yield*/, this.run({
329
+ fn: 'get',
330
+ args: args
331
+ })];
332
+ case 1:
333
+ stream = _a.sent();
334
+ return [2 /*return*/, new Promise(function (resolve) {
335
+ stream.on('data', function (chunk) {
336
+ res = Buffer.concat([res, chunk], res.byteLength + chunk.byteLength);
337
+ });
338
+ stream.on('end', function () {
339
+ resolve(res);
340
+ _this.state.running = false;
341
+ _this.len -= 1;
342
+ });
343
+ })];
233
344
  }
234
- }], false));
345
+ });
235
346
  });
236
347
  };
237
- FtpClient.prototype.connect = function () {
238
- var _this = this;
239
- return new Promise(function (resolve, reject) {
240
- var c = new _this.driver();
241
- c.once('ready', function () {
242
- resolve(c);
348
+ return FtpClient;
349
+ }();
350
+ exports.FtpClient = FtpClient;
351
+ var FtpClientPool = /** @class */function () {
352
+ function FtpClientPool(_a) {
353
+ var _b = _a.maxConnection,
354
+ maxConnection = _b === void 0 ? 5 : _b,
355
+ options = __rest(_a, ["maxConnection"]);
356
+ this.pool = [];
357
+ this.get = this.bind('get');
358
+ this.put = this.bind('put');
359
+ this["delete"] = this.bind('delete');
360
+ this.list = this.bind('list');
361
+ this.rename = this.bind('rename');
362
+ this.options = options;
363
+ this.maxConnection = maxConnection;
364
+ }
365
+ Object.defineProperty(FtpClientPool.prototype, "len", {
366
+ get: function get() {
367
+ return this.pool.length;
368
+ },
369
+ enumerable: false,
370
+ configurable: true
371
+ });
372
+ Object.defineProperty(FtpClientPool.prototype, "client", {
373
+ get: function get() {
374
+ if (this.pool.length < this.maxConnection) {
375
+ var client = new FtpClient(this.options);
376
+ this.pool.push(client);
377
+ return client;
378
+ }
379
+ this.pool.sort(function (a, b) {
380
+ return a.len - b.len;
243
381
  });
244
- c.once('error', function (err) {
245
- reject(err);
246
- c.end();
382
+ return this.pool[0];
383
+ },
384
+ enumerable: false,
385
+ configurable: true
386
+ });
387
+ FtpClientPool.prototype.bind = function (fn) {
388
+ var _this = this;
389
+ return function () {
390
+ var args = [];
391
+ for (var _i = 0; _i < arguments.length; _i++) {
392
+ args[_i] = arguments[_i];
393
+ }
394
+ return __awaiter(_this, void 0, void 0, function () {
395
+ var client, res, index;
396
+ return __generator(this, function (_a) {
397
+ switch (_a.label) {
398
+ case 0:
399
+ client = this.client;
400
+ return [4 /*yield*/, client[fn].apply(client, __spreadArray([], __read(args), false))];
401
+ case 1:
402
+ res = _a.sent();
403
+ if (client.len < 1) {
404
+ index = this.pool.findIndex(function (item) {
405
+ return item.id === client.id;
406
+ });
407
+ this.pool.splice(index, 1);
408
+ }
409
+ return [2 /*return*/, res];
410
+ }
411
+ });
247
412
  });
248
- c.connect(__assign({}, _this.options));
249
- });
413
+ };
250
414
  };
251
- return FtpClient;
415
+ return FtpClientPool;
252
416
  }();
253
- exports.FtpClient = FtpClient;
417
+ exports.FtpClientPool = FtpClientPool;
@@ -1,8 +1,10 @@
1
+ /// <reference types="node" />
1
2
  interface CreateClientProps {
2
3
  url: string;
3
4
  options?: any;
4
5
  ServiceImp: any;
5
6
  Grpc: any;
7
+ cert?: Buffer;
6
8
  }
7
- export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc }: CreateClientProps): T;
9
+ export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc, cert }: CreateClientProps): T;
8
10
  export {};
package/lib/grpc/index.js CHANGED
@@ -19,12 +19,14 @@ function createRpcClient(_a) {
19
19
  _b = _a.options,
20
20
  options = _b === void 0 ? {} : _b,
21
21
  ServiceImp = _a.ServiceImp,
22
- Grpc = _a.Grpc;
22
+ Grpc = _a.Grpc,
23
+ cert = _a.cert;
23
24
  var opt = __assign({
24
25
  'grpc.max_send_message_length': -1,
25
26
  'grpc.max_receive_message_length': -1
26
27
  }, options);
27
- var connection = new Grpc.Client(url, Grpc.credentials.createInsecure(), opt);
28
+ var credentials = cert ? Grpc.credentials.createSsl(cert) : Grpc.credentials.createInsecure();
29
+ var connection = new Grpc.Client(url, credentials, opt);
28
30
  var request = function request(service, method, data) {
29
31
  var path = "/".concat(service, "/").concat(method);
30
32
  return new Promise(function (resolve, reject) {
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { initRtspVideos, RtspVideosManager } from './rtsp-video';
3
- export { FtpClient } from './ftp-client';
3
+ export { FtpClient, FtpClientPool, FtpConnectOptions, FtpFile } from './ftp-client';
4
4
  export { md5 } from './md5';
5
5
  export { createRpcClient } from './grpc';
6
6
  export { PuboFileSystem } from './file-system';
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.PuboFileSystem = exports.createRpcClient = exports.md5 = exports.FtpClient = exports.RtspVideosManager = exports.initRtspVideos = exports.JsonStorage = void 0;
6
+ exports.PuboFileSystem = exports.createRpcClient = exports.md5 = exports.FtpClientPool = exports.FtpClient = exports.RtspVideosManager = exports.initRtspVideos = exports.JsonStorage = void 0;
7
7
  var json_1 = require("./storage/json");
8
8
  Object.defineProperty(exports, "JsonStorage", {
9
9
  enumerable: true,
@@ -31,6 +31,12 @@ Object.defineProperty(exports, "FtpClient", {
31
31
  return ftp_client_1.FtpClient;
32
32
  }
33
33
  });
34
+ Object.defineProperty(exports, "FtpClientPool", {
35
+ enumerable: true,
36
+ get: function get() {
37
+ return ftp_client_1.FtpClientPool;
38
+ }
39
+ });
34
40
  var md5_1 = require("./md5");
35
41
  Object.defineProperty(exports, "md5", {
36
42
  enumerable: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pubo-node",
3
- "version": "1.0.69",
3
+ "version": "1.0.73",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./es/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -23,5 +23,5 @@
23
23
  "pako": "^2.0.4",
24
24
  "pubo-utils": "^1.0.69"
25
25
  },
26
- "gitHead": "1d63c277c36fc7e451e0a685e9d29f9082efc483"
26
+ "gitHead": "c8e2df0ba7e4f4e0bc04f5661f9a7fbd0adf76ef"
27
27
  }