2017-11-19 10:08:04 -05:00
|
|
|
// @flow
|
2017-04-19 14:15:28 -05:00
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
import _ from 'lodash';
|
2017-10-22 05:28:38 -05:00
|
|
|
import assert from 'assert';
|
2017-11-19 10:08:04 -05:00
|
|
|
import smartRequest from './request';
|
|
|
|
import type {IServerBridge} from './types';
|
2013-09-25 05:01:55 -05:00
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
const buildAuthHeader = (user, pass): string => {
|
2017-06-28 15:56:02 -05:00
|
|
|
return `Basic ${(new Buffer(`${user}:${pass}`)).toString('base64')}`;
|
2017-04-19 14:15:28 -05:00
|
|
|
};
|
2013-09-25 05:01:55 -05:00
|
|
|
|
2017-12-01 19:50:09 -05:00
|
|
|
export default class Server implements IServerBridge {
|
2017-11-19 10:08:04 -05:00
|
|
|
url: string;
|
|
|
|
userAgent: string;
|
|
|
|
authstr: string;
|
2017-06-28 15:56:02 -05:00
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
constructor(url: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
this.url = url.replace(/\/$/, '');
|
2017-06-30 16:11:12 -05:00
|
|
|
this.userAgent = 'node/v8.1.2 linux x64';
|
2017-06-28 15:56:02 -05:00
|
|
|
this.authstr = buildAuthHeader('test', 'test');
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
request(options: any): any {
|
2017-12-01 19:50:09 -05:00
|
|
|
// console.log("--->$$$$ REQUEST", options);
|
2017-06-28 15:56:02 -05:00
|
|
|
assert(options.uri);
|
2017-12-03 16:23:06 -05:00
|
|
|
const headers = options.headers || {};
|
2017-11-19 10:08:04 -05:00
|
|
|
|
2017-06-28 15:56:02 -05:00
|
|
|
headers.accept = headers.accept || 'application/json';
|
|
|
|
headers['user-agent'] = headers['user-agent'] || this.userAgent;
|
|
|
|
headers.authorization = headers.authorization || this.authstr;
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
return smartRequest({
|
2017-06-28 15:56:02 -05:00
|
|
|
url: this.url + options.uri,
|
|
|
|
method: options.method || 'GET',
|
|
|
|
headers: headers,
|
|
|
|
encoding: options.encoding,
|
|
|
|
json: _.isNil(options.json) === false ? options.json : true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
auth(name: string, password: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
this.authstr = buildAuthHeader(name, password);
|
|
|
|
return this.request({
|
|
|
|
uri: `/-/user/org.couchdb.user:${encodeURIComponent(name)}/-rev/undefined`,
|
|
|
|
method: 'PUT',
|
|
|
|
json: {
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
email: 'test@example.com',
|
|
|
|
_id: `org.couchdb.user:${name}`,
|
|
|
|
type: 'user',
|
|
|
|
roles: [],
|
|
|
|
date: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
logout(token: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/-/user/token/${encodeURIComponent(token)}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
getPackage(name: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
putPackage(name: string, data) {
|
2017-06-28 15:56:02 -05:00
|
|
|
if (_.isObject(data) && !Buffer.isBuffer(data)) {
|
|
|
|
data = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}`,
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
}).send(data);
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
putVersion(name: string, version: string, data: any) {
|
2017-06-28 15:56:02 -05:00
|
|
|
if (_.isObject(data) && !Buffer.isBuffer(data)) {
|
|
|
|
data = JSON.stringify(data);
|
|
|
|
}
|
2017-11-19 10:08:04 -05:00
|
|
|
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/${encodeURIComponent(version)}/-tag/latest`,
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
}).send(data);
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
getTarball(name: string, filename: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/-/${encodeURIComponent(filename)}`,
|
|
|
|
method: 'GET',
|
|
|
|
encoding: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
putTarball(name: string, filename: string, data: any) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/-/${encodeURIComponent(filename)}/whatever`,
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/octet-stream',
|
|
|
|
},
|
|
|
|
}).send(data);
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
removeTarball(name: string) {
|
2017-07-01 02:09:44 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/-rev/whatever`,
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json; charset=utf-8',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
removeSingleTarball(name: string, filename: string) {
|
2017-07-30 10:55:41 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/-/${filename}/-rev/whatever`,
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json; charset=utf-8',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
addTag(name: string, tag: string, version: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/${encodeURIComponent(tag)}`,
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
}).send(JSON.stringify(version));
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
putTarballIncomplete(name: string, filename: string, data: any, size: number, cb: Function) {
|
2017-06-28 15:56:02 -05:00
|
|
|
let promise = this.request({
|
2017-06-30 16:11:12 -05:00
|
|
|
uri: `/${encodeURIComponent(name)}/-/${encodeURIComponent(filename)}/whatever`,
|
2017-06-28 15:56:02 -05:00
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/octet-stream',
|
|
|
|
'content-length': size,
|
|
|
|
},
|
|
|
|
timeout: 1000,
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.request(function(req) {
|
|
|
|
req.write(data);
|
|
|
|
setTimeout(function() {
|
|
|
|
req.req.abort();
|
|
|
|
}, 20);
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
promise
|
|
|
|
.then(function() {
|
|
|
|
reject(Error('no error'));
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
if (err.code === 'ECONNRESET') {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
addPackage(name: string) {
|
2017-08-06 14:54:15 -05:00
|
|
|
return this.putPackage(name, require('../fixtures/package')(name))
|
2017-06-28 15:56:02 -05:00
|
|
|
.status(201)
|
|
|
|
.body_ok('created new package');
|
|
|
|
}
|
|
|
|
|
|
|
|
whoami() {
|
2017-06-30 16:11:12 -05:00
|
|
|
return this.request({
|
|
|
|
uri: '/-/whoami'
|
|
|
|
}).status(200)
|
|
|
|
.then(function(body) {
|
|
|
|
return body.username;
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2017-06-28 15:56:02 -05:00
|
|
|
}
|
|
|
|
|
2017-07-01 17:05:58 -05:00
|
|
|
ping() {
|
|
|
|
return this.request({
|
|
|
|
uri: '/-/ping'
|
|
|
|
}).status(200)
|
|
|
|
.then(function(body) {
|
|
|
|
return body;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:56:02 -05:00
|
|
|
debug() {
|
|
|
|
return this.request({
|
|
|
|
uri: '/-/_debug',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|