Files

9 lines
15 KiB
JavaScript
Raw Permalink Normal View History

/**
* 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