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';
|
2019-07-16 01:40:01 -05:00
|
|
|
import {IServerBridge} from '../types';
|
2019-05-19 14:37:43 -05:00
|
|
|
import {API_MESSAGE, HEADERS, HTTP_STATUS, TOKEN_BASIC} from '../../src/lib/constants';
|
2018-06-19 15:02:29 -05:00
|
|
|
import {buildToken} from "../../src/lib/utils";
|
2018-06-24 03:11:52 -05:00
|
|
|
import {CREDENTIALS} from "../functional/config.functional";
|
2019-07-16 01:40:01 -05:00
|
|
|
import getPackage from '../functional/fixtures/package';
|
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 {
|
2019-07-16 01:40:01 -05:00
|
|
|
public url: string;
|
|
|
|
public userAgent: string;
|
|
|
|
public authstr: string;
|
2017-06-28 15:56:02 -05:00
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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: {
|
2019-07-16 01:40:01 -05:00
|
|
|
name,
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public logout(token: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/-/user/token/${encodeURIComponent(token)}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public getPackage(name: string) {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public putPackage(name: string, data) {
|
2017-06-28 15:56:02 -05:00
|
|
|
if (_.isObject(data) && !Buffer.isBuffer(data)) {
|
|
|
|
data = JSON.stringify(data);
|
|
|
|
}
|
2019-07-16 01:40:01 -05:00
|
|
|
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}`,
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).send(data);
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).send(data);
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.OCTET_STREAM,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).send(data);
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public removeTarball(name: string) {
|
2017-07-01 02:09:44 -05:00
|
|
|
return this.request({
|
|
|
|
uri: `/${encodeURIComponent(name)}/-rev/whatever`,
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.JSON_CHARSET,
|
2017-07-01 02:09:44 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.JSON_CHARSET,
|
2017-07-30 10:55:41 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
}).send(JSON.stringify(version));
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public putTarballIncomplete(name: string, filename: string, data: any, size: number, cb: Function): Promise<any> {
|
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: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.OCTET_STREAM,
|
|
|
|
[HEADERS.CONTENT_LENGTH]: size,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public addPackage(name: string) {
|
|
|
|
return this.putPackage(name, getPackage(name))
|
2018-06-19 15:02:29 -05:00
|
|
|
.status(HTTP_STATUS.CREATED)
|
2019-05-19 14:37:43 -05:00
|
|
|
.body_ok(API_MESSAGE.PKG_CREATED);
|
2017-06-28 15:56:02 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public ping() {
|
2017-07-01 17:05:58 -05:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public debug() {
|
2017-06-28 15:56:02 -05:00
|
|
|
return this.request({
|
|
|
|
uri: '/-/_debug',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
2019-05-19 14:37:43 -05:00
|
|
|
[HEADERS.CONTENT_TYPE]: HEADERS.JSON,
|
2017-06-28 15:56:02 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|