UNPKG

decentralized-internet

Version:

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

1 lines 5.43 kB
var request=require("request"),_=require("underscore"),Promise=require("bluebird"),mkdirp=require("mkdirp"),path=require("path"),fs=require("fs"),concat=require("concat-stream");const{PassThrough:PassThrough,Writable:Writable}=require("stream");exports.configuration={},exports.setConfiguration=function(e){if(!e||!e.default&&!e.hostname&&!e.database){var t={default:"codename",codename:{hostname:"http://localhost:5984",database:"db"}};throw console.error("No default database name, your conf should look like:",JSON.stringify(t,null,2),"or",JSON.stringify(t.codename,null,2)),"Bad couchdb configuration"}exports.configuration=e},exports.createDB=function(e){var t=exports.getCouchDBServer(e);return new Promise(function(e,r){request.put(t,function(n,o,i){if(n)r(n.message);else try{"not_found"===JSON.parse(i).error?request.put(t,function(t,r,n){e(JSON.parse(n))}):e(JSON.parse(i))}catch(e){console.error(t),console.error(e),r(e)}})})},exports.getConfiguration=function(e){return e?exports.configuration[e]:exports.configuration.default?exports.configuration[exports.configuration.default]:exports.configuration},exports.getCouchDBServer=function(e){var t=exports.getConfiguration(e);if(!t)throw"No couchdb server found in configuration with "+e;return t.hostname+"/"+t.database},exports.uploadDocuments=function(e,t){var r={};return _.isArray(e)?r.docs=e:_.isObject(e)&&(r.docs=[e]),new Promise(function(e,n){var o={uri:exports.getCouchDBServer(t)+"/_bulk_docs",method:"POST",json:r};request(o,function(t,r,o){t?n({id:"uploadDocumentsDataProvider",message:t.message}):o.error?n(o.error):e(o)})})},exports.getDocument=function(e,t){return new Promise(function(r,n){try{var o={uri:exports.getCouchDBServer(t)+"/"+e};request(o,function(e,t,o){if(e)n(e);else{var i=JSON.parse(o);i.error?n(i.error):r(i)}})}catch(e){n(e)}})},exports.mkdirp=function(e){return new Promise(function(t,r){mkdirp(e,function(e){e?r(e):t(!0)})})},exports.removeDirectorySync=function(e){fs.existsSync(e)&&fs.statSync(e).isDirectory()&&(_.map(fs.readdirSync(e),function(t){var r=path.join(e,t);if(fs.existsSync(r))fs.statSync(r).isDirectory()?exports.removeDirectorySync(r):fs.unlinkSync(r);else try{fs.unlinkSync(r)}catch(e){console.error(e)}}),fs.rmdirSync(e))},exports.deleteDocument=function(e,t){if(e.attachments){var r=exports.getConfiguration(t),n=path.join(r.datapath,e._id);if(n===r.datapath)throw"Something is terrible wrong with the doc._id";exports.removeDirectorySync(n)}return new Promise(function(r,n){try{var o={uri:exports.getCouchDBServer(t)+"/"+e._id,method:"DELETE",qs:{rev:e._rev}};request(o,function(e,t,o){if(e)n(e);else{var i=JSON.parse(o);i.error?n(i):r(i)}})}catch(e){n(e)}})},exports.deleteAttachment=function(e,t,r){return new Promise(function(n,o){if(e.attachments&&e.attachments[t]){var i=exports.getConfiguration(r),a=path.join(i.datapath,e.attachments[t].path);try{fs.unlinkSync(a);for(var s=path.normalize(path.join(i.datapath,e._id));path.normalize(a)!==s;)a=path.dirname(a),fs.statSync(a).isDirectory()&&0===fs.readdirSync(a).length&&fs.rmdirSync(a);delete e.attachments[t],exports.uploadDocuments(e,r).then(function(e){n(e)})}catch(e){o(e)}}else{if(!e._attachments||!e._attachments[t])throw"Attachement not found";try{var c={uri:exports.getCouchDBServer(r)+"/"+e._id+"/"+t,method:"DELETE",qs:{rev:e._rev}};request(c,function(e,t,r){if(e)o(e);else{var i=JSON.parse(r);i.error?o(i):n(i)}})}catch(e){o(e)}}})},exports.addDocumentAttachment=function(e,t,r,n){return new Promise(function(o,i){var a=exports.getConfiguration(n);if(a.datapath){var s=path.join(a.datapath,e._id),c=path.join(s,t);exports.mkdirp(path.dirname(c)).then(function(){var a=path.join(e._id,t),s=fs.createWriteStream(c);s.on("finish",function(r){e.attachments||(e.attachments={}),e.attachments[t]={path:a},exports.uploadDocuments([e],n).then(function(e){o(e[0])}).catch(i)}),r.pipe(s)})}else try{var u={uri:exports.getCouchDBServer(n)+"/"+e._id+"/"+encodeURIComponent(t),method:"PUT",headers:{"Content-type":"application/octet-stream"},qs:{rev:e._rev}};r.pipe(request(u,function(e,t,r){e?i(e):o(r)}))}catch(e){i(e)}})},exports.getDocumentStreamAttachmentUri=function(e,t,r){if(e.attachments&&e.attachments[t]){var n=exports.getConfiguration(r),o=path.join(n.datapath,e._id,t);if(!fs.existsSync(o))throw"File not found";return Promise.resolve({file:o})}return e._attachments&&e._attachments[t]?Promise.resolve({uri:exports.getCouchDBServer(r)+"/"+e._id+"/"+t}):Promise.reject("Document is missing attachment")},exports.getDocumentStreamAttachment=function(e,t,r){return exports.getDocumentStreamAttachmentUri(e,t,r).then(function(e){if(e.file)return Promise.resolve(fs.createReadStream(e.file));if(e.uri){var t=new PassThrough;return request(e).pipe(t),Promise.resolve(t)}return Promise.reject("Document is missing attachment")})},exports.getDocumentAttachment=function(e,t,r){return new Promise(function(n,o){return exports.getDocumentStreamAttachment(e,t,r).then(function(e){var t=concat(n);e.pipe(t),e.on("error",o)}).catch(o)})},exports.getView=function(e,t){return new Promise(function(r,n){try{var o={uri:exports.getCouchDBServer(t)+"/"+e};request(o,function(e,t,o){if(e)n(e);else{var i=JSON.parse(o);r(i.rows)}})}catch(e){n(e)}})},exports.getViewQs=function(e,t,r){return new Promise(function(n,o){try{var i={uri:exports.getCouchDBServer(r)+"/"+e,qs:t};request(i,function(e,t,r){if(e)o(e);else{var i=JSON.parse(r);n(i.rows)}})}catch(e){o(e)}})};