-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
148 lines (132 loc) · 3.57 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
const url = require('url');
const Promise = require('bluebird');
const U = require('lodash');
const yargs = require('yargs');
const filepath = require('filepath');
const Client = require('./lib/client');
const OddworksClient = require('./lib/oddworks-client');
const Runner = require('./lib/runner');
exports.main = function main() {
const args = yargs
.usage('Usage: $0 [options]')
.option('config', {
demand: true,
describe: 'Path to the JSON config file'
})
.option('rate', {
demand: true,
describe: 'Number of requests per minute',
type: 'number'
})
.option('limit', {
describe: 'Limit the number of requests',
default: 50,
type: 'number'
})
.help();
const argv = args.argv;
return loadConfigs(argv.config)
.then(config => {
config = config || {};
if (!config.jwtSecret) {
return Promise.reject(new Error('Config is missing jwtSecret'));
}
if (!config.jwtIssuer) {
return Promise.reject(new Error('Config is missing jwtIssuer'));
}
if (!config.channel) {
return Promise.reject(new Error('Config is missing channel'));
}
if (!config.baseUrl) {
return Promise.reject(new Error('Config is missing baseUrl'));
}
if (!U.isString(config.pathPrefix)) {
return Promise.reject(new Error('Config is missing pathPrefix'));
}
if (!config.authHeader) {
return Promise.reject(new Error('Config is missing authHeader'));
}
if (!Array.isArray(config.platforms)) {
return Promise.reject(new Error('Config is missing platforms'));
}
let baseUrl;
try {
baseUrl = url.parse(config.baseUrl);
} catch (err) {
const uriError = new Error(`Config baseUrl is invalid: ${err.message}`);
return Promise.reject(uriError);
}
return config.platforms.map(spec => {
if (!spec.id) {
throw new Error('Config platform is missing an ID');
}
if (!Array.isArray(spec.paths)) {
return Promise.reject(new Error('Config platform is missing a paths Array'));
}
return new Client({
jwtSecret: config.jwtSecret,
jwtIssuer: config.jwtIssuer,
channel: config.channel,
platform: spec.id,
paths: spec.paths,
oddworksClient: new OddworksClient({
protocol: baseUrl.protocol.replace(/:$/, ''),
host: baseUrl.hostname,
port: baseUrl.port,
pathPrefix: config.pathPrefix,
authHeader: config.authHeader
})
});
});
})
.then(clients => {
const promises = clients.map(client => {
return client.authenticate();
});
return Promise.all(promises);
})
.then(clients => {
let pending = 0;
const runner = new Runner({
rate: argv.rate,
limit: argv.limit,
clients
});
runner.on('request', () => {
pending += 1;
});
runner.on('response', res => {
pending -= 1;
outputResponse(pending, res);
});
return runner.run();
});
};
function loadConfigs(path) {
const file = filepath.create(path);
if (!file.isFile()) {
throw new Error(`Config path is not a file "${path}"`);
}
return file.read().then(text => {
let config;
try {
config = JSON.parse(text);
} catch (err) {
const jsonError = new Error(`JSON config file parsing error: ${err.message}`);
return Promise.reject(jsonError);
}
return config;
});
}
function outputResponse(pending, res) {
const latency = res.end - res.start;
const method = res.method;
const status = res.status;
const path = res.uri.path;
if (status !== 200) {
console.error(`Unexpected ${status} response:`);
console.error(res.body);
}
console.log('%s,%s,"%s",%s,"%s"', pending, latency, method, status, path);
}