2013-09-25 12:01:55 +02:00
|
|
|
var request = require('request');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
function Server(url) {
|
|
|
|
if (!(this instanceof Server)) return new Server(url);
|
|
|
|
this.url = url.replace(/\/$/, '');
|
|
|
|
this.userAgent = 'node/v0.10.8 linux x64';
|
|
|
|
}
|
|
|
|
|
2013-09-27 04:27:11 +02:00
|
|
|
function prep(cb) {
|
|
|
|
return function(err, res, body) {
|
|
|
|
if (err) throw err;
|
|
|
|
cb(res, body);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-09-25 12:01:55 +02:00
|
|
|
Server.prototype.request = function(options, cb) {
|
|
|
|
options.headers = options.headers || {};
|
|
|
|
return request({
|
|
|
|
url: this.url + options.uri,
|
|
|
|
method: options.method || 'GET',
|
|
|
|
headers: {
|
|
|
|
accept: options.headers.accept || 'application/json',
|
|
|
|
'user-agent': options.headers['user-agent'] || this.userAgent,
|
2013-09-27 04:27:11 +02:00
|
|
|
'content-type': options.headers['content-type'],
|
2013-09-25 12:01:55 +02:00
|
|
|
authorization: this.auth,
|
|
|
|
},
|
2013-09-27 04:27:11 +02:00
|
|
|
json: options.json || true,
|
2013-09-25 12:01:55 +02:00
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.auth = function(user, pass, cb) {
|
|
|
|
this.auth = 'Basic '+(new Buffer(user+':'+pass)).toString('base64');
|
|
|
|
this.request({
|
|
|
|
uri: '/-/user/org.couchdb.user:'+escape(user)+'/-rev/undefined',
|
|
|
|
method: 'PUT',
|
|
|
|
json: {
|
|
|
|
content: "doesn't matter, 'cause sinopia uses info from Authorization header anywayz",
|
|
|
|
}
|
2013-09-27 04:27:11 +02:00
|
|
|
}, prep(cb));
|
2013-09-25 12:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.get_package = function(name, cb) {
|
2013-09-27 04:27:11 +02:00
|
|
|
this.request({
|
2013-09-25 12:01:55 +02:00
|
|
|
uri: '/'+name,
|
|
|
|
method: 'GET',
|
2013-09-27 04:27:11 +02:00
|
|
|
}, prep(cb));
|
2013-09-25 12:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.put_package = function(name, data, cb) {
|
2013-09-27 04:27:11 +02:00
|
|
|
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data);
|
|
|
|
this.request({
|
2013-09-25 12:01:55 +02:00
|
|
|
uri: '/'+name,
|
|
|
|
method: 'PUT',
|
2013-09-27 04:27:11 +02:00
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
}, prep(cb)).end(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.get_tarball = function(name, filename, cb) {
|
|
|
|
this.request({
|
|
|
|
uri: '/'+name+'/-/'+filename,
|
|
|
|
method: 'GET',
|
|
|
|
}, prep(cb));
|
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.put_tarball = function(name, filename, data, cb) {
|
|
|
|
this.request({
|
|
|
|
uri: '/'+name+'/-/'+filename+'/whatever',
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/octet-stream'
|
|
|
|
},
|
|
|
|
}, prep(cb)).end(data);
|
2013-09-25 12:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Server;
|
|
|
|
|