Files
jibo-cli/node_modules/jibo-sync/lib/jibo-sync.js

9 lines
15 KiB
JavaScript

/**
* jibo-sync - Sync an entire a directory over HTTP; including incremental changes
* @version v0.2.8
* @license Copyright (c) 2017, Jibo, Inc. All rights reserved.
* All use of the Jibo SDK is subject to the Jibo SDK End User License Agreement (EULA)
* distributed herewith. If you did not receive a copy of the EULA, you may view a
* copy at https://developers.jibo.com/license.
*/
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.jiboSync=e()}}(function(){return function e(t,n,o){function r(a,u){if(!n[a]){if(!t[a]){var s="function"==typeof require&&require;if(!u&&s)return s(a,!0);if(i)return i(a,!0);var l=new Error("Cannot find module '"+a+"'");throw l.code="MODULE_NOT_FOUND",l}var f=n[a]={exports:{}};t[a][0].call(f.exports,function(e){var n=t[a][1][e];return r(n?n:e)},f,f.exports,e,t,n,o)}return n[a].exports}for(var i="function"==typeof require&&require,a=0;a<o.length;a++)r(o[a]);return r}({1:[function(e,t,n){"use strict";var o=e("./Client"),r=e("./Server"),i=e("./Stopper"),a=e("./Command"),u=new i.default,s=function(){function e(){}return Object.defineProperty(e,"Command",{get:function(){return a.default},enumerable:!0,configurable:!0}),e.createServer=function(e,t,n,o){r.default.start(e,t,n,function(e,t){e?(console.log(e),o(e)):(console.log(t),o(null))})},e.uploadToServer=function(e,t,n,r,i){u.reset(),o.default.push(e,t,r,function(t,a){n?o.default.done(e,function(e){e&&r&&console.log(e),i(t,a)}):i(t,a)})},e.closeServer=function(e,t){o.default.done(e,function(e,n){t(e,n)})},e.stop=function(){u.stop()},e}();Object.defineProperty(n,"__esModule",{value:!0}),n.default=s},{"./Client":2,"./Command":3,"./Server":6,"./Stopper":7}],2:[function(e,t,n){"use strict";function o(e){return"string"!=typeof e?".":e}function r(e,t){return new Promise(function(n,o){if(m.doStop)return void o(h.default.STOPPED);v.default.log("Generating server list... ");var r=Date.now(),i={host:e,port:t,path:"/checksum",method:"POST"},a=l.request(i,function(i){if(m.doStop)return void o(h.default.STOPPED);i.setEncoding("utf8");var a="";i.on("data",function(e){a+=e}),i.on("end",function(i){if(m.doStop)o(h.default.STOPPED);else if(i)o("Cannot connect to "+e+":"+t+" or it is already in use.");else{var u=JSON.parse(a);v.default.log("\ttook "+(Date.now()-r)/1e3+" secs"),n(u)}})});a.end()})}function i(e,t){return new Promise(function(n,o){return m.doStop?void o(h.default.STOPPED):void v.default.generateChecksum(e,!0).then(function(e){if(m.doStop)return void o(h.default.STOPPED);var r=[],i=[];for(var a in e)t.hasOwnProperty(a)?(t[a]!==e[a]&&i.push(a),delete t[a]):i.push(a);for(var u in t)r.push(u);n({updateList:i,deleteList:r})}).catch(function(e){o("Error retrieving update list: "+e)})})}function a(e,t,n,o,r){return new Promise(function(i,a){return m.doStop?void a(h.default.STOPPED):0===r.length&&0===o.length?(v.default.log("Everything is already up to date"),void i({size:0,compSize:0})):void u(e,t,r,function(r){return m.doStop?void a(h.default.STOPPED):(r&&v.default.log("Delete failed",r),void s(e,t,o,n,function(e,t,n){m.doStop?a(h.default.STOPPED):e?a(e):i({size:t,compSize:n})}))})})}function u(e,t,n,o){if(n.length>0){v.default.log("\nDeleting "+n.length+" files");var r=JSON.stringify({deleteList:n}),i={host:e,port:t,path:"/delete",method:"POST",headers:{"Content-Type":"application/json","Content-Length":r.length}},a=l.request(i,function(e){var t="";e.on("data",function(e){t+=e}),e.on("end",function(){o("ERROR"===e.status?t:null)})});a.on("error",function(e){o(e)}),a.write(r),a.end()}else o(null)}function s(e,t,n,o,r){if(n.length>0){v.default.log("\nUpdating "+n.length+" files from "+o);var i="http://"+e+":"+t+"/upload",a=p.post(i),u=new d,s=new d,l=c.pack(o,{entries:n,dereference:!0}),g=l.pipe(u).pipe(f.createGzip({level:9})).pipe(s).pipe(a);m.once("stop",function(){g.abort(),r(h.default.STOPPED)}),g.on("end",function(e){r(e,u.bytes,s.bytes)}),g.on("error",function(e){r(e,null,null)})}else r(null,0,0)}var l=e("http"),f=e("zlib"),c=e("tar-fs"),d=e("stream-meter"),p=e("request"),v=e("./Common"),h=e("./FailErrors"),g=e("./Stopper"),m=new g.default,O=function(){function e(){}return e.push=function(e,t,n,u){v.default.setVerbose(n);var s=v.default.getHostPort(e);if(!s)return void u("Error parsing url into host:port");var l=s.host,f=s.port,c=o(t),d=Date.now();r(l,f).then(function(e){return i(c,e)}).then(function(e){var t=e.updateList,n=e.deleteList;return a(l,f,c,t,n)}).then(function(){u(null,"Done in "+(Date.now()-d)/1e3+" seconds")}).catch(function(e){u(e)})},e.done=function(e,t){var n=v.default.getHostPort(e);if(!n)return void t("Error parsing url into host:port",null);var o={host:n.host,port:n.port,path:"/close",method:"POST"},r=l.request(o,function(e){var n="";e.on("data",function(e){n+=e}),e.on("end",function(){var o;try{o=JSON.parse(n)}catch(t){return e.writeHead(200,{"Content-Type":"text/html"}),e.write(JSON.stringify({Status:"ERROR",Message:"Invalid request to close sync server. Request body is not valid JSON"})),void e.end()}"ERROR"===o.Status?t(o.Message,null):t(null,o.Message)})});r.end()},e}();O.FAIL_ERRORS=h.default,Object.defineProperty(n,"__esModule",{value:!0}),n.default=O},{"./Common":4,"./FailErrors":5,"./Stopper":7,http:void 0,request:void 0,"stream-meter":void 0,"tar-fs":void 0,zlib:void 0}],3:[function(e,t,n){"use strict";var o=e("./API"),r=e("path"),i=function(){function t(e){this.allowedCommands=[["create-server","[port] [dest] [--verbose]","Create a sync server on current IP with specified port and designated destination directory"],["start","[url] [source] [--verbose]","Sync source directory to specified server url (ip:port)"]],this.allowedOptions=[["-h","--help","Output usage information"],["-V","--version","Output the version number"]],this.rootPath=r.dirname(e),this.parse()}return t.prototype.parse=function(){var t=this,n=!1,o=process.argv[2];o!==this.allowedOptions[0][0]&&o!==this.allowedOptions[0][1]||(n=!0,this.printUsage());var i=e(r.join(this.rootPath,"package.json"));if(o!==this.allowedOptions[1][0]&&o!==this.allowedOptions[1][1]||(n=!0,console.log("\n"+i.version+"\n")),!n)for(var a=this.allowedCommands.length,u=0;u<a;u++){var s=this.allowedCommands[u][0];if(o===s){n=!0;var l=process.argv.slice(3,process.argv.length);this.processCommand(s,l,function(e,n){e?(console.log("\n"+e),t.printUsage()):console.log("\n"+n+"\n")})}}return n||this.printUsage(),n},t.prototype.printUsage=function(){var e=40;console.log("\n Usage: sync [options] [command]\n\n"),console.log(" Commands:\n"),this.allowedCommands.forEach(function(t){var n=e-t[0].length-t[1].length;n<=0&&(n=4);for(var o="",r=0;r<n;r++)o+=" ";console.log(" "+t[0]+" "+t[1]+o+t[2])}),console.log("\n Options:\n"),this.allowedOptions.forEach(function(t){var n=", ",o=e-t[0].length-t[1].length-n.length;o<=0&&(o=4);for(var r="",i=0;i<o;i++)r+=" ";console.log(" "+t[0]+n+t[1]+" "+r+t[2])}),console.log("")},t.prototype.processCommand=function(e,t,n){if("create-server"===e){var r=!0;"--verbose"===t[2]&&(r=t[3]),o.default.createServer(t[0],t[1],r,function(e,t){n(e,t)})}else if("start"===e){var r=!0;"--verbose"===t[2]&&(r=t[3]),o.default.uploadToServer(t[0],t[1],!1,r,function(e,t){n(e,t)})}else n("Undefined command: "+e,null)},t}();Object.defineProperty(n,"__esModule",{value:!0}),n.default=i},{"./API":1,path:void 0}],4:[function(e,t,n){"use strict";function o(e){m.log(e)}function r(e,t,n,i){return g.doStop?void i(v.default.STOPPED):void s.readdir(t,function(u,f){if(u)return void i(u,null);var d,p=[];f=f.filter(function(o){for(var r=l.join(t,o),i=l.relative(e,r),a=n.length,u=!0,s=0;s<a;++s)if(c(i,n[s])){u=!1;break}return u}),d=f.filter(function(e){if(!t)return!1;var n=l.join(t,e);try{var r=s.statSync(n);return r&&r.isDirectory()}catch(e){return o("Error getting file info on "+n+"; skipping.."),!1}}),f=f.filter(function(e){if(!t)return!1;var n=l.join(t,e);try{var r=s.statSync(n);return r&&r.isFile()}catch(e){return o("Error getting file info on "+n+"; skipping.."),!1}}),f=f.map(function(e){return l.join(t,e)}),p=p.concat(f);var h=[];d.forEach(function(o){h.push(function(i){var a=l.join(t,o);r(e,a,n,function(e,t){p=p.concat(t),i()})})}),a.parallelLimit(h,5,function(e){g.doStop?i(v.default.STOPPED):e?(console.error("Error executing recursive file find: "+e),i(e,null)):i(null,p)})})}function i(e,t){return e?void(h?s.stat(l.normalize(e),function(n,r){if(r){var i=r.mtime;return d.isDate(i)&&(i=Math.trunc(i.getTime()/1e3)),void t(i)}o("Error getting checksum for "+e+": "+n),t(null)}):s.readFile(l.normalize(e),function(n,r){if(r){var i=u.createHash("md5");return i.update(r),void t(i.digest("hex"))}o("Error getting checksum for "+e+": "+n),t(null)})):void t(null)}var a=e("async"),u=e("crypto"),s=e("fs"),l=e("path"),f=e("parse-gitignore"),c=e("minimatch"),d=e("util"),p=e("./Stopper"),v=e("./FailErrors"),h=!0,g=new p.default;Math.trunc=Math.trunc||function(e){return e<0?Math.ceil(e):Math.floor(e)};var m=function(){function e(){}return e.generateChecksum=function(e,t){return new Promise(function(n,u){if(!e)return void u(v.default.NO_DIRECTORY);var c=t?f(l.resolve(e,"./.syncignore")):[],d=e.replace(/\/$/,"");s.stat(d,function(e,t){if(e||t&&!t.isDirectory()){var s="Directory "+d+" does not exist.";return void u(s)}var f=Date.now();o("Retrieving file list from "+d+"..."),r(d,d,c,function(e,t){if(e)return void u(e);o("\ttook "+(Date.now()-f)/1e3+" secs");var r=t;r=r.map(function(e){return l.relative(d,e)});var s=[],c={};r.forEach(function(e){s.push(function(t){if(g.doStop)return void t(v.default.STOPPED);e=l.normalize(e);var n=l.join(d,e);i(n,function(n){n&&(e=e.replace(/\\/g,"/"),c[e]=n),t()})})}),o("Generating checksum list..."),f=Date.now(),a.parallelLimit(s,5,function(e){e?(o("Error executing checksums: "+e),u(e)):(o("\ttook "+(Date.now()-f)/1e3+" secs"),n(c))})})})})},e.getHostPort=function(e){var t=e.split(":");if(2!==t.length)return void o("Url is not specified as [IP:port]: "+e);var n=t[0],r=Number(t[1]);return isNaN(r)?void o("Invalid port defined "+r):{host:n,port:r}},e.setVerbose=function(t){e.verboseLog=t},e.log=function(t){e.verboseLog&&console.log(t)},e.error=function(t){e.verboseLog&&console.error(t)},e}();m.verboseLog=!1,Object.defineProperty(n,"__esModule",{value:!0}),n.default=m},{"./FailErrors":5,"./Stopper":7,async:void 0,crypto:void 0,fs:void 0,minimatch:void 0,"parse-gitignore":void 0,path:void 0,util:void 0}],5:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default={NO_PORT_SPECIFIED:"NO_PORT_SPECIFIED",CANNOT_CONNECT:"CANNOT_CONNECT",NO_DIRECTORY:"No directory was specified. Aborting checksum.",ERROR:"ERROR",STOPPED:"STOPPED"}},{}],6:[function(e,t,n){"use strict";function o(e,t){if("POST"===e.method)switch(e.url){case w:a(e,t);break;case E:s(e,t);break;case b:i(e,t);break;case T:l(e,t);break;case C:f(e,t);break;default:t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"ERROR",Message:"Undefined url: "+e.url})),t.end()}}function r(e){y?(y.close(),y=void 0,e(null,"Server closed on port "+S)):e("No server to close",null)}function i(e,t){"POST"===e.method&&O.default.generateChecksum(P,!1).then(function(e){t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify(e)),t.end()}).catch(function(e){t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"ERROR",Message:e})),t.end()})}function a(e,t){O.default.log("Receiving upload...");var n=Date.now();"POST"===e.method&&u(e,P,function(e,o,r){O.default.log("\ttook "+(Date.now()-n)/1e3+" secs"),O.default.log("\t"+o+" bytes uploaded ("+r+" compressed)"),t.writeHead(200,"OK",{"Content-Type":"text/html"}),t.end()})}function u(e,t,n){d.ensureDirSync(t);var o=new v,r=new v,i={dmode:365,fmode:365};e.pipe(r).pipe(m.createGunzip()).pipe(o).pipe(g.extract(t,i)).on("finish",function(e){e?(O.default.log("Problem extracting tar stream",e),n(e)):n(null,o.bytes,r.bytes)}),e.on("error",function(e){e&&O.default.log("problem with POST stream",e),n(e)})}function s(e,t){O.default.log("Receiving delete request...");var n=Date.now();if("POST"===e.method){var o="";e.on("data",function(e){o+=e}),e.on("end",function(){var e=void 0;try{e=JSON.parse(o)}catch(e){return t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"ERROR",Message:"Invalid delete request. Request body is not valid JSON"})),void t.end()}var r=e.deleteList;if(void 0!==r){var i=[];r.forEach(function(e){i.push(function(t){var n=h.resolve(P,e);d.remove(n,t)})}),c.parallelLimit(i,5,function(e){e?(O.default.log("Error executing deletes: "+e),t.writeHead(200,"OK",{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"ERROR",Message:e})),t.end()):(t.writeHead(200,"OK",{"Content-Type":"text/html"}),t.end()),O.default.log("\ttook "+(Date.now()-n)/1e3+" secs")})}})}}function l(e,t){r(function(e,n){e?(O.default.log(e),t.writeHead(200,{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"ERROR",Message:e})),t.end()):(O.default.log(n),t.writeHead(200,"OK",{"Content-Type":"text/html"}),t.write(JSON.stringify({Status:"OK",Message:n})),t.end())})}function f(e,t){"POST"===e.method&&(t.writeHead(200,"OK",{"Content-Type":"text/html"}),t.end())}var c=e("async"),d=e("fs-extra"),p=e("http"),v=e("stream-meter"),h=e("path"),g=e("tar-fs"),m=e("zlib"),O=e("./Common"),y=void 0,S=void 0,P=".",w="/upload",E="/delete",b="/checksum",T="/close",C="/ping",R=function(){function e(){}return e.start=function(e,t,n,r){if(O.default.setVerbose(n),S=Number(e),isNaN(S))return void r("\nInvalid port defined: "+S+" Aborting.",null);if(y)t&&(P=t),r(null,"\nSync server already listening on port "+S);else{t&&(P=t),y=p.createServer(o);try{y.listen(S,"0.0.0.0",function(e){e?(y.close(),y=void 0,r(e,null)):r(null,"\nSync server listening on port "+S+". Press CTRL+C or call stop() to exit.\n")}),y.timeout=0}catch(e){y.close(),y=void 0,r(e,null)}}},e}();Object.defineProperty(n,"__esModule",{value:!0}),n.default=R},{"./Common":4,async:void 0,"fs-extra":void 0,http:void 0,path:void 0,"stream-meter":void 0,"tar-fs":void 0,zlib:void 0}],7:[function(e,t,n){"use strict";var o=this&&this.__extends||function(e,t){function n(){this.constructor=e}for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o]);e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)},r=e("events"),i=function(e){function t(){var t=e.call(this)||this;return t._doStop=!1,t}return o(t,e),t.prototype.stop=function(){this._doStop=!0,this.emit("stop")},Object.defineProperty(t.prototype,"doStop",{get:function(){return this._doStop},enumerable:!0,configurable:!0}),t.prototype.reset=function(){this.removeAllListeners(),this._doStop=!1},t}(r.EventEmitter);Object.defineProperty(n,"__esModule",{value:!0}),n.default=i},{events:void 0}],8:[function(e,t,n){"use strict";var o=e("./API");Object.defineProperty(n,"__esModule",{value:!0}),n.default=o.default,t.exports=o.default},{"./API":1}]},{},[8])(8)});