UNPKG

decentralized-internet

Version:

An NPM library of programs to create decentralized web and distributed computing projects

1 lines 7 kB
var request=require("request"),_=require("underscore"),Promise=require("bluebird"),Boom=require("boom"),spawn=require("child_process").spawn,couchUpdateViews=require("couch-update-views"),path=require("path"),qs=require("querystring"),os=require("os"),tarGzip=require("node-targz");const{PassThrough:PassThrough,Writable:Writable}=require("stream"),fs=require("fs");module.exports=function(e,t){if(!e.methods.clusterprovider)throw new Error("Have you installed the 'couch-provider' plugin with namespace 'clusterprovider'?");couchUpdateViews.migrateUp(e.methods.clusterprovider.getCouchDBServer(),path.join(__dirname,"views"),!0);var r={createJob:function(t,r){var n=t.payload;return n.timestamp=new Date,n.jobstatus={status:"CREATE"},e.methods.clusterprovider.uploadDocuments(n).then(function(e){return 1===e.length?e[0]:e}).catch(function(e){return Boom.badRequest(e)})},updateJob:function(t,r){var n=t.payload,o=t.auth.credentials,u=[e.methods.clusterprovider.validateJobOwnership(n,o),e.methods.clusterprovider.getDocument(n._id).then(function(t){return e.methods.clusterprovider.validateJobOwnership(t,o)}).catch(function(e){throw Boom.unauthorized("You are not allowed to update the document, it belongs to someone else")})];return Promise.all(u).then(function(){return e.methods.clusterprovider.uploadDocuments(n)}).catch(function(e){return Boom.badRequest(e)})},addData:function(t,r){return e.methods.clusterprovider.getDocument(t.params.id).then(function(t){return e.methods.clusterprovider.isJobDocument(t)}).then(function(r){return e.methods.clusterprovider.validateJobOwnership(r,t.auth.credentials)}).then(function(r){return e.methods.clusterprovider.addDocumentAttachment(r,t.params.name,t.payload)}).catch(function(e){return Boom.badRequest(e)})},getJob:function(t,r){return e.methods.clusterprovider.getDocument(t.params.id).then(function(r){return e.methods.clusterprovider.validateJobOwnership(r,t.auth.credentials)}).then(function(r){return t.params.name?e.methods.clusterprovider.getDocumentStreamAttachment(r,t.params.name):r}).catch(function(e){return Boom.notFound(e)})},getJobCount:function(t,r){return e.methods.clusterprovider.getView("_design/getJob/_view/count?group=true").then(function(e){return e}).catch(function(e){return Boom.badRequest(e)})},getDownloadToken:function(t,r){return e.methods.clusterprovider.getDocument(t.params.id).then(function(r){return e.methods.clusterprovider.validateJobOwnership(r,t.auth.credentials)}).then(function(r){var n=t.params.name;return e.methods.jwtauth.sign({_id:r._id,name:n})}).catch(function(e){return Boom.badRequest(e)})},downloadAttachment:function(t,r){var n=t.params.token;try{var o=e.methods.jwt.verify(n);return e.methods.clusterprovider.getDocument(o._id).then(function(t){return console.log(o.name),e.methods.clusterprovider.getDocumentStreamAttachment(t,o.name)}).catch(function(e){return Boom.unauthorized(e)})}catch(e){return Boom.unauthorized(e)}}};e.method({name:"dataprovider.jobDelete",method:function(t){return e.methods.clusterprovider.getDocument(t._id).then(function(t){return e.methods.clusterprovider.deleteDocument(t)})},options:{}}),r.deleteJob=function(t,r){return e.methods.clusterprovider.getDocument(t.params.id).then(function(t){return e.methods.clusterprovider.isJobDocument(t)}).then(function(r){return e.methods.clusterprovider.validateJobOwnership(r,t.auth.credentials)}).then(function(t){return t.jobstatus.status="DELETE",e.methods.clusterprovider.uploadDocuments(t).then(function(r){return e.methods.cronprovider.addJobToDeleteQueue(t)}).then(function(){return t.jobstatus})}).catch(function(e){return Boom.badRequest(e)})},r.getUserJobs=function(t,r){var n=t.auth.credentials,o=n.email;if(t.query.userEmail&&o!==t.query.userEmail&&-1===n.scope.indexOf("admin"))throw Boom.unauthorized("You are not allowed to view the jobs of other users.");t.query.userEmail&&(o=t.query.userEmail);var u,i=t.query.jobstatus,s=t.query.executable,c=t.query.executionserver;if(i&&s){var a=[o,i,s];u="_design/searchJob/_view/useremailjobstatusexecutable?include_docs=true&key="+JSON.stringify(a)}else if(i&&o){a=[o,i];u="_design/searchJob/_view/useremailjobstatus?include_docs=true&key="+JSON.stringify(a)}else if(s){a=[o,s];u="_design/searchJob/_view/useremailexecutable?include_docs=true&key="+JSON.stringify(a)}else if(c){a={key:JSON.stringify([c,i]),include_docs:!0};u="_design/searchJob/_view/executionserverjobstatus?"+qs.stringify(a)}else u="_design/searchJob/_view/useremail?include_docs=true&key="+JSON.stringify(o);return e.methods.clusterprovider.getView(u).then(function(e){return _.pluck(e,"doc")}).then(function(t){return Promise.map(n.scope,function(t){var r={key:JSON.stringify(t),include_docs:!0},n="_design/searchJob/_view/scope?"+qs.stringify(r);return e.methods.clusterprovider.getView(n).then(function(e){var t=_.pluck(e,"doc");return s&&(t=_.filter(t,function(e){return e.executable==s})),i&&(t=_.filter(t,function(e){return!(!e.jobstatus||!e.jobstatus.status)&&e.jobstatus.status==i})),t})}).then(function(e){return _.union(t,e)}).then(function(e){return _.uniq(_.compact(_.flatten(e)))})}).catch(function(e){return Boom.badRequest(e)})},r.getAllJobs=function(t,r){var n=t.query.executable;if(n){var o=n;view="_design/searchJob/_view/executable?include_docs=true&key="+JSON.stringify(o)}else view="_design/searchJob/_view/executable?include_docs=true";return e.methods.clusterprovider.getView(view).then(function(e){return _.pluck(e,"doc")}).catch(function(e){return Boom.badRequest(e)})};return r.getDownload=function(t,r){var n=path.join(os.tmpdir(),"clusterpost",t.params.id),o=n+".tar.gz";return e.methods.clusterprovider.getDocument(t.params.id).then(function(r){return e.methods.clusterprovider.validateJobOwnership(r,t.auth.credentials)}).then(function(t){return e.methods.clusterprovider.mkdirp(n).then(function(){var r=_.compact(_.union(_.map(t._attachments,(e,t)=>t),_.map(t.attachments,(e,t)=>t)));return Promise.map(r,function(r){return e.methods.clusterprovider.mkdirp(path.join(n,path.dirname(r))).then(function(){return e.methods.clusterprovider.getDocumentStreamAttachment(t,r)}).then(function(e){return function(e,t){return new Promise(function(r,n){var o=fs.createWriteStream(t);e.pipe(o),o.on("finish",function(e){e?n({path:t,status:!1,error:e}):r({path:t,status:!0})})})}(e,path.join(n,r))}).catch(function(e){})}).then(function(){return new Promise(function(e,t){tarGzip.compress({source:n,destination:o},function(){e()})})}).then(function(){e.methods.clusterprovider.removeDirectorySync(n);var t=fs.createReadStream(o);return t.on("end",function(){fs.unlinkSync(o)}),t})})}).catch(function(t){return e.methods.clusterprovider.removeDirectorySync(n),fs.existsSync(o)&&fs.unlinkSync(o),Boom.badRequest(t)})},r.deleteDownload=function(t,r){try{var n=path.join(os.tmpdir(),"clusterpost",t.params.id);e.methods.clusterpost.removeDirectorySync(n);var o=n+".tar.gz";fs.statSync(o)&&fs.unlinkSync(o)}catch(e){return console.error(e),Boom.badImplementation(e)}return!0},r};