decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 4.06 kB
JavaScript
var configuration,request=require("request"),_=require("underscore"),Promise=require("bluebird"),path=require("path"),fs=require("fs");exports.setConfiguration=function(t){if(!t||!t.default&&!t.hostname&&!t.database){var e={default:"codename",codename:{hostname:"http://localhost:5984",database:"db"}};throw console.error("No default database name, your conf should look like:",JSON.stringify(e,null,2),"or",JSON.stringify(e.codename,null,2)),"Bad couchdb configuration"}configuration=t},exports.getCouchDBServer=function(t){var e;if(t?e=configuration[t]:configuration.default?e=configuration[configuration.default]:configuration.hostname&&configuration.database&&(e=configuration),!e)throw"No couchdb server found in configuration with "+t;return e.hostname+"/"+e.database},exports.uploadDocuments=function(t,e){var r={};return _.isArray(t)?r.docs=t:_.isObject(t)&&(r.docs=[t]),new Promise(function(t,n){var o={uri:exports.getCouchDBServer(e)+"/_bulk_docs",method:"POST",json:r};request(o,function(e,r,o){e?n({id:"uploadDocumentsDataProvider",message:e.message}):o.error?n(o.error):t(o)})})},exports.getDocument=function(t,e){return new Promise(function(r,n){try{var o={uri:exports.getCouchDBServer(e)+"/"+t};request(o,function(t,e,o){if(t)n(t);else{var a=JSON.parse(o);a.error?n(a.error):r(a)}})}catch(t){n(t)}})},exports.deleteDocument=function(t,e){return new Promise(function(r,n){try{var o={uri:exports.getCouchDBServer(e)+"/"+t._id+"?rev="+t._rev,method:"DELETE"};request(o,function(t,e,o){if(t)n(t);else{var a=JSON.parse(o);a.error?n(a):r(a)}})}catch(t){n(t)}})},exports.addDocumentAttachment=function(t,e,r,n){return new Promise(function(o,a){var i;if((i=n?configuration[n]:configuration[configuration.default]).datapath){var c=path.join(i.datapath,t._id);fs.existsSync(c)||fs.mkdirSync(c);var u=path.join(c,e);"."!=path.dirname(e)&&(c=path.join(c,path.parse(e).dir),fs.existsSync(c)||fs.mkdirSync(c),c=path.join(c,path.parse(e).base));var s=path.join(t._id,e),f=u,h=fs.createWriteStream(f);h.on("finish",function(r){t.attachments||(t.attachments={}),t.attachments[e]={path:s},exports.uploadDocuments(t).then(function(t){o(t)}).catch(a)}),r.pipe(h)}else try{var d={uri:exports.getCouchDBServer(n)+"/"+t._id+"/"+encodeURIComponent(e)+"?rev="+t._rev,method:"PUT",headers:{"Content-type":"application/octet-stream"}};r.pipe(request(d,function(t,e,r){t?a(t):o(r)}))}catch(t){a(t)}})},exports.addDocumentAttachmentFolder=function(t,e,r,n){fs.readdirSync(e);var o=path.parse(e).base;n=n?path.join(n,o):o;var a=this;return Promise.map(fs.readdirSync(e),function(o){var i=fs.statSync(path.join(e,o));if(i&&i.isFile()){var c=fs.createReadStream(path.join(e,o)),u=path.join(n,o);return a.addDocumentAttachment(t,u,c,r).then(function(e){if(_.isArray(e))t._rev=e[0].rev;else{var r=JSON.parse(e);t._rev=r.rev}})}return i&&i.isDirectory()?a.addDocumentAttachmentFolder(t,path.join(e,o),r,n):Promise.resolve()},{concurrency:1})},exports.getDocumentURIAttachment=function(t,e,r){r=r||configuration.default;var n,o=configuration[r];return t._attachments&&null!=t._attachments[e]?(n=t._id+"/"+e,{uri:exports.getCouchDBServer(r)+"/"+n}):t.attachments&&null!=t.attachments[e]?{uri:exports.getCouchDBServer(r)+"/"+t._id,onResponse:function(r,n,a,i,c,u){var s=path.join(o.datapath,t._id,e);i(fs.createReadStream(s))}}:void 0},exports.getDocumentAttachment=function(t,e,r){var n;n=r?configuration[r]:configuration[configuration.default];exports.getCouchDBServer(r),t._id,t._rev;if(t.attachments){if(null!=t.attachments[e])return new Promise(function(r,o){try{var a=path.join(n.datapath,t._id,e);r(fs.readFileSync(a).toString())}catch(t){o(t)}})}else{if(!t._attachments)return console.log("no attachment"),!1;if(null!=t._attachments[e])return new Promise(function(t,e){try{var n=getDocumentURIAttachment(n.uri,r);request(n,function(r,n,o){r?e(r):t(o)})}catch(t){e(t)}})}},exports.getView=function(t,e){return new Promise(function(r,n){try{var o={uri:exports.getCouchDBServer(e)+"/"+t};request(o,function(t,e,o){if(t)n(t);else{var a=JSON.parse(o);r(a.rows)}})}catch(t){n(t)}})};