2014-11-12 06:14:37 -05:00
|
|
|
var assert = require('assert')
|
2013-12-19 10:11:54 -05:00
|
|
|
var request = require('request')
|
2013-09-25 05:01:55 -05:00
|
|
|
|
|
|
|
function Server(url) {
|
2014-11-12 06:14:37 -05:00
|
|
|
var self = Object.create(Server.prototype)
|
|
|
|
self.url = url.replace(/\/$/, '')
|
|
|
|
self.userAgent = 'node/v0.10.8 linux x64'
|
|
|
|
self.authstr = 'Basic '+(new Buffer('test:test')).toString('base64')
|
|
|
|
return self
|
2013-09-25 05:01:55 -05:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:27:11 -05:00
|
|
|
function prep(cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
return function(err, res, body) {
|
|
|
|
if (err) throw err
|
|
|
|
cb(res, body)
|
|
|
|
}
|
2013-09-26 21:27:11 -05:00
|
|
|
}
|
|
|
|
|
2013-09-25 05:01:55 -05:00
|
|
|
Server.prototype.request = function(options, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
assert(options.uri)
|
|
|
|
var headers = options.headers || {}
|
|
|
|
headers.accept = headers.accept || 'application/json'
|
|
|
|
headers['user-agent'] = headers['user-agent'] || this.userAgent
|
|
|
|
headers.authorization = headers.authorization || this.authstr
|
|
|
|
return request({
|
|
|
|
url: this.url + options.uri,
|
|
|
|
method: options.method || 'GET',
|
|
|
|
headers: headers,
|
|
|
|
encoding: options.encoding,
|
|
|
|
json: options.json != null ? options.json : true,
|
|
|
|
}, cb)
|
2013-09-25 05:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.auth = function(user, pass, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.authstr = 'Basic '+(Buffer(user+':'+pass)).toString('base64')
|
|
|
|
this.request({
|
|
|
|
uri: '/-/user/org.couchdb.user:'+encodeURIComponent(user)+'/-rev/undefined',
|
|
|
|
method: 'PUT',
|
|
|
|
json: {
|
|
|
|
name: user,
|
|
|
|
password: pass,
|
|
|
|
email: 'test@example.com',
|
|
|
|
_id: 'org.couchdb.user:' + user,
|
|
|
|
type: 'user',
|
|
|
|
roles: [],
|
|
|
|
date: new Date(),
|
|
|
|
}
|
|
|
|
}, prep(cb))
|
2013-09-25 05:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.get_package = function(name, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.request({
|
|
|
|
uri: '/'+name,
|
|
|
|
method: 'GET',
|
|
|
|
}, prep(cb))
|
2013-09-25 05:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.put_package = function(name, data, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data)
|
|
|
|
this.request({
|
|
|
|
uri: '/'+encodeURIComponent(name),
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
}, prep(cb)).end(data)
|
2013-09-27 03:56:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.put_version = function(name, version, data, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data)
|
|
|
|
this.request({
|
|
|
|
uri: '/'+encodeURIComponent(name)+'/'+encodeURIComponent(version)+'/-tag/latest',
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
}, prep(cb)).end(data)
|
2013-09-26 21:27:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.get_tarball = function(name, filename, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.request({
|
|
|
|
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename),
|
|
|
|
method: 'GET',
|
|
|
|
}, prep(cb))
|
2013-09-26 21:27:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server.prototype.put_tarball = function(name, filename, data, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.request({
|
|
|
|
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename)+'/whatever',
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/octet-stream'
|
|
|
|
},
|
|
|
|
}, prep(cb)).end(data)
|
2013-09-25 05:01:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-29 01:40:47 -05:00
|
|
|
Server.prototype.add_tag = function(name, tag, version, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.request({
|
|
|
|
uri: '/'+encodeURIComponent(name)+'/'+encodeURIComponent(tag),
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
}, prep(cb)).end(JSON.stringify(version))
|
2013-12-29 01:40:47 -05:00
|
|
|
}
|
|
|
|
|
2013-09-27 06:31:28 -05:00
|
|
|
Server.prototype.put_tarball_incomplete = function(name, filename, data, size, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
var req = this.request({
|
|
|
|
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename)+'/whatever',
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/octet-stream',
|
|
|
|
'content-length': size,
|
|
|
|
},
|
|
|
|
timeout: 1000,
|
|
|
|
}, function(err) {
|
|
|
|
assert(err)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
req.write(data)
|
|
|
|
setTimeout(function() {
|
|
|
|
req.req.abort()
|
|
|
|
}, 20)
|
2013-09-27 06:31:28 -05:00
|
|
|
}
|
|
|
|
|
2013-12-29 01:40:47 -05:00
|
|
|
Server.prototype.add_package = function(name, cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.put_package(name, require('./package')(name), function(res, body) {
|
|
|
|
assert.equal(res.statusCode, 201)
|
|
|
|
assert(~body.ok.indexOf('created new package'))
|
|
|
|
cb()
|
|
|
|
})
|
2013-12-29 01:40:47 -05:00
|
|
|
}
|
|
|
|
|
2014-09-24 20:21:59 -05:00
|
|
|
Server.prototype.debug = function(cb) {
|
2014-11-12 06:14:37 -05:00
|
|
|
this.request({
|
|
|
|
uri: '/-/_debug',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
}, prep(cb))
|
2014-09-24 20:21:59 -05:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:11:54 -05:00
|
|
|
module.exports = Server
|
2013-09-25 05:01:55 -05:00
|
|
|
|