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';
|
2018-06-17 06:34:59 -05:00
|
|
|
import type {IServerBridge} from '../types';
|
2018-06-19 15:02:29 -05:00
|
|
|
import {HEADERS, HTTP_STATUS, TOKEN_BASIC} from '../../src/lib/constants';
|
|
|
|
import {buildToken} from "../../src/lib/utils";
|
2018-06-24 03:11:52 -05:00
|
|
|
import {CREDENTIALS} from "../functional/config.functional";
|
2013-09-25 05:01:55 -05:00
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
const buildAuthHeader = (user, pass): string => {
|
2018-06-19 15:02:29 -05:00
|
|
|
return buildToken(TOKEN_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';
|
2018-06-21 16:33:20 -05:00
|
|
|
this.authstr = buildAuthHeader(CREDENTIALS.user, CREDENTIALS.password);
|
2017-06-28 15:56:02 -05:00
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
request(options: any): any {
|
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
|
|
|
|
2018-04-30 08:41:04 -05:00
|
|
|
headers.accept = headers.accept || HEADERS.JSON;
|
2017-06-28 15:56:02 -05:00
|
|
|
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,
|
2018-06-21 16:33:20 -05:00
|
|
|
email: `${CREDENTIALS.user}@example.com`,
|
2017-06-28 15:56:02 -05:00
|
|
|
_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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.OCTET_STREAM,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.JSON_CHARSET,
|
2017-07-01 02:09:44 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.JSON_CHARSET,
|
2017-07-30 10:55:41 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).send(JSON.stringify(version));
|
|
|
|
}
|
|
|
|
|
2018-01-15 17:40:47 -05:00
|
|
|
putTarballIncomplete(name: string, filename: string, data: any, size: number, cb: Function): Promise<*> {
|
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: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.OCTET_STREAM,
|
2017-06-28 15:56:02 -05:00
|
|
|
'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) {
|
2018-02-09 02:42:34 -05:00
|
|
|
return this.putPackage(name, require('../functional/fixtures/package')(name))
|
2018-06-19 15:02:29 -05:00
|
|
|
.status(HTTP_STATUS.CREATED)
|
2017-06-28 15:56:02 -05:00
|
|
|
.body_ok('created new package');
|
|
|
|
}
|
|
|
|
|
|
|
|
whoami() {
|
2017-06-30 16:11:12 -05:00
|
|
|
return this.request({
|
|
|
|
uri: '/-/whoami'
|
2018-06-19 15:02:29 -05:00
|
|
|
}).status(HTTP_STATUS.OK)
|
2017-06-30 16:11:12 -05:00
|
|
|
.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'
|
2018-06-19 15:02:29 -05:00
|
|
|
}).status(HTTP_STATUS.OK)
|
2017-07-01 17:05:58 -05:00
|
|
|
.then(function(body) {
|
|
|
|
return body;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:56:02 -05:00
|
|
|
debug() {
|
|
|
|
return this.request({
|
|
|
|
uri: '/-/_debug',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
2018-04-30 08:41:04 -05:00
|
|
|
'content-type': HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|