-
Notifications
You must be signed in to change notification settings - Fork 89
/
clientBinariesManager.js
647 lines (534 loc) · 17.1 KB
/
clientBinariesManager.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
"use strict";
const got = require('got'),
_ = require('lodash'),
fs = require('fs'),
crypto = require('crypto'),
path = require('path'),
tmp = require('tmp'),
mkdirp = require('mkdirp'),
unzip = require('node-unzip-2'),
spawn = require('buffered-spawn'),
log = require('electron-log'),
progress = require('cli-progress'),
EventEmitter = require('events').EventEmitter;
function copyFile(src, dst) {
return new Promise((resolve, reject) => {
var rd = fs.createReadStream(src);
rd.on("error", (err) => {
reject(err);
});
var wr = fs.createWriteStream(dst);
wr.on("error", (err) => {
reject(err);
});
wr.on("close", (ex) => {
resolve();
});
rd.pipe(wr);
});
}
function checksum(filePath, algorithm) {
return new Promise((resolve, reject) => {
const checksum = crypto.createHash(algorithm);
const stream = fs.ReadStream(filePath);
stream.on('data', (d) => checksum.update(d));
stream.on('end', () => {
resolve(checksum.digest('hex'));
});
stream.on('error', reject);
});
}
class Manager extends EventEmitter {
/**
* Construct a new instance.
*
* @param {Object} [config] The configuraton to use. If ommitted then the
* default configuration (`DefaultConfig`) will be used.
*/
constructor (config) {
super();
this._config = config;
this._logger = log;
}
/**
* Get configuration.
* @return {Object}
*/
get config () {
return this._config;
}
/**
* Get info on available clients.
*
* This will return an object, each item having the structure:
*
* "client name": {
* id: "client name"
* homepage: "client homepage url"
* version: "client version"
* versionNotes: "client version notes url"
* cli: {... info on all available platforms...},
* activeCli: {
* ...info for this platform...
* }
* status: {
"available": true OR false (depending on status)
"failReason": why it is not available (`sanityCheckFail`, `notFound`, etc)
* }
* }
*
* @return {Object}
*/
get clients () {
return this._clients;
}
/**
* Initialize the manager.
*
* This will scan for clients.
* Upon completion `this.clients` will have all the info you need.
*
* @param {Object} [options] Additional options.
* @param {Array} [options.folders] Additional folders to search in for client binaries.
*
* @return {Promise}
*/
init(options) {
this._logger.debug('Initializing Manager...');
this._resolvePlatform();
return this._scan(options);
}
/**
* Download a particular client.
*
* If client supports this platform then
* it will be downloaded from the download URL, whether it is already available
* on the system or not.
*
* If client doesn't support this platform then the promise will be rejected.
*
* Upon completion the `clients` property will have been updated with the new
* availability status of this client. In addition the following info will
* be returned from the promise:
*
* ```
* {
* downloadFolder: ...where archive got downloaded...
* downloadFile: ...location of downloaded file...
* unpackFolder: ...where archive was unpacked to...
* client: ...updated client object (contains availability info and full binary path)...
* }
* ```
*
* @param {Object} [options] Options.
* @param {Object} [options.downloadFolder] Folder to download client to, and to unzip it in.
* @param {Function} [options.unpackHandler] Custom download archive unpack handling function.
* @param {RegExp} [options.urlRegex] Regex to check the download URL against (this is a security measure).
*
* @return {Promise}
*/
download (clientId, options) {
options = Object.assign({
downloadFolder: null,
unpackHandler: null,
urlRegex: null
}, options);
this._logger.info(`Download binary for ${clientId} ...`);
const client = _.get(this._clients, clientId);
const activeCli = _.get(client, `activeCli`),
downloadCfg = _.get(activeCli, `download`);
return Promise.resolve()
.then(() => {
// not for this machine?
if (!client) {
throw new Error(`${clientId} missing configuration for this platform.`);
}
if (!_.get(downloadCfg, 'url') || !_.get(downloadCfg, 'type')) {
throw new Error(`Download info not available for ${clientId}`);
}
if (options.urlRegex) {
this._logger.debug('Checking download URL against regex ...');
if (!options.urlRegex.test(downloadCfg.url)) {
throw new Error(`Download URL failed regex check`);
}
}
let resolve, reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
this._logger.debug('Generating download folder path ...');
const downloadFolder = path.join(
options.downloadFolder || tmp.dirSync().name,
client.id
);
this._logger.debug(`Ensure download folder ${downloadFolder} exists ...`);
mkdirp.sync(downloadFolder);
const downloadFile = path.join(downloadFolder, `archive.${downloadCfg.type}`);
this._logger.debug(`Downloading package from ${downloadCfg.url} to ${downloadFile} ...`);
const writeStream = fs.createWriteStream(downloadFile);
const stream = got.stream(downloadCfg.url);
let progressBar = undefined;
stream.pipe(writeStream);
stream.on('downloadProgress', (info) => {
if (progressBar) {
progressBar.update(info.transferred);
this.emit('download', {
status: 'busy',
transferred: info.transferred,
total: info.total
});
} else {
progressBar = new progress.Bar({}, progress.Presets.shades_classic);
progressBar.start(info.total, info.transferred);
this.emit('download', {
status: 'started',
transferred: info.transferred,
total: info.total
});
}
});
stream.on('error', (err) => {
if (progressBar) {
progressBar.stop();
this.emit('download', {
status: 'error'
});
}
this._logger.error(err);
reject(new Error(`Error downloading package for ${clientId}: ${err.message}`));
});
stream.on('end', () => {
if (progressBar) {
progressBar.stop();
this.emit('download', {
status: 'done'
});
}
this._logger.debug(`Downloaded ${downloadCfg.url} to ${downloadFile}`);
try {
fs.accessSync(downloadFile, fs.F_OK | fs.R_OK);
resolve({
downloadFolder: downloadFolder,
downloadFile: downloadFile
});
} catch (err) {
reject(new Error(`Error downloading package for ${clientId}: ${err.message}`));
}
});
return promise;
})
.then((dInfo) => {
const downloadFolder = dInfo.downloadFolder,
downloadFile = dInfo.downloadFile;
// test checksum
let value, algorithm, expectedHash;
if ((value = _.get(downloadCfg, 'md5'))) {
expectedHash = value;
algorithm = 'md5';
} else if ((value = _.get(downloadCfg, 'sha256'))) {
expectedHash = value;
algorithm = 'sha256';
}
if (algorithm) {
return checksum(dInfo.downloadFile, algorithm)
.then((hash) => {
this._logger.silly('algorithm: ', algorithm);
this._logger.silly('file hash: ', hash);
this._logger.silly('expected: ', expectedHash);
if (expectedHash !== hash) {
throw new Error(`Hash mismatch: ${expectedHash}`);
}
return dInfo;
});
} else {
return dInfo;
}
})
.then((dInfo) => {
const downloadFolder = dInfo.downloadFolder,
downloadFile = dInfo.downloadFile;
const unpackFolder = path.join(downloadFolder, 'unpacked');
this._logger.debug(`Ensure unpack folder ${unpackFolder} exists ...`);
mkdirp.sync(unpackFolder);
this._logger.debug(`Unzipping ${downloadFile} to ${unpackFolder} ...`);
let promise;
if (options.unpackHandler) {
this._logger.debug(`Invoking custom unpack handler ...`);
promise = options.unpackHandler(downloadFile, unpackFolder);
} else {
switch (downloadCfg.type) {
case 'zip':
this._logger.debug(`Using unzip ...`);
promise = new Promise((resolve, reject) => {
try {
fs.createReadStream(downloadFile)
.pipe(
unzip.Extract({ path: unpackFolder })
.on('close', resolve)
.on('error', reject)
)
.on('error', reject);
} catch (err) {
reject(err);
}
});
break;
case 'tar':
this._logger.debug(`Using tar ...`);
promise = this._spawn('tar', ['-xf', downloadFile, '-C', unpackFolder]);
break;
default:
throw new Error(`Unsupported archive type: ${downloadCfg.type}`);
}
}
return promise.then(() => {
this._logger.debug(`Unzipped ${downloadFile} to ${unpackFolder}`);
const linkPath = path.join(unpackFolder, activeCli.bin);
// need to rename binary?
if (downloadCfg.bin) {
let realPath = path.join(unpackFolder, downloadCfg.bin);
try {
fs.accessSync(linkPath, fs.R_OK);
fs.unlinkSync(linkPath);
} catch (e) {
if (e.code !== 'ENOENT')
this._logger.warn(e);
}
return copyFile(realPath, linkPath).then(() => linkPath);
} else {
return Promise.resolve(linkPath);
}
})
.then((binPath) => {
// make binary executable
try {
fs.chmodSync(binPath, '755');
} catch (e) {
this._logger.warn(e);
}
return {
downloadFolder: downloadFolder,
downloadFile: downloadFile,
unpackFolder: unpackFolder
};
});
})
.then((info) => {
return this._verifyClientStatus(client, {
folders: [info.unpackFolder]
})
.then(() => {
info.client = client;
return info;
});
});
}
_resolvePlatform () {
this._logger.debug('Resolving platform...');
// platform
switch (process.platform) {
case 'win32':
this._os = 'win';
break;
case 'darwin':
this._os = 'mac';
break;
default:
this._os = process.platform;
}
// architecture
this._arch = process.arch;
return Promise.resolve();
}
/**
* Scan the local machine for client software, as defined in the configuration.
*
* Upon completion `this._clients` will be set.
*
* @param {Object} [options] Additional options.
* @param {Array} [options.folders] Additional folders to search in for client binaries.
*
* @return {Promise}
*/
_scan (options) {
this._clients = {};
return this._calculatePossibleClients()
.then((clients) => {
this._clients = clients;
const count = Object.keys(this._clients).length;
this._logger.debug(`${count} possible clients.`);
if (_.isEmpty(this._clients)) {
return;
}
this._logger.debug(`Verifying status of all ${count} possible clients...`);
return Promise.all(_.values(this._clients).map(
(client) => this._verifyClientStatus(client, options)
));
});
}
/**
* Calculate possible clients for this machine by searching for binaries.
* @return {Promise}
*/
_calculatePossibleClients () {
return Promise.resolve()
.then(() => {
// get possible clients
this._logger.debug('Calculating possible clients...');
const possibleClients = {};
for (let clientName in _.get(this._config, 'clients', {})) {
let client = this._config.clients[clientName];
if (_.get(client, `platforms.${this._os}.${this._arch}`)) {
possibleClients[clientName] =
Object.assign({}, client, {
id: clientName,
activeCli: client.platforms[this._os][this._arch]
});
}
}
return possibleClients;
});
}
/**
* This will modify the passed-in `client` item according to check results.
*
* @param {Object} [options] Additional options.
* @param {Array} [options.folders] Additional folders to search in for client binaries.
*
* @return {Promise}
*/
_verifyClientStatus (client, options) {
options = Object.assign({
folders: []
}, options);
this._logger.debug(`Verify ${client.id} status ...`);
return Promise.resolve().then(() => {
const binName = client.activeCli.bin;
// reset state
client.state = {};
delete client.activeCli.binPath;
this._logger.debug(`${client.id} binary name: ${binName}`);
const binPaths = [];
let command;
let args = [];
if (process.platform === 'win32') {
command = 'where';
} else {
command = 'which';
}
args.push(binName);
return this._spawn(command, args)
.then((output) => {
const systemPath = _.get(output, 'stdout', '').trim();
if (_.get(systemPath, 'length')) {
this._logger.debug(`Got PATH binary for ${client.id}: ${systemPath}`);
binPaths.push(systemPath);
}
}, (err) => {
this._logger.debug(`Command ${binName} not found in path.`);
})
.then(() => {
// now let's search additional folders
if (_.get(options, 'folders.length')) {
options.folders.forEach((folder) => {
this._logger.debug(`Checking for ${client.id} binary in ${folder} ...`);
const fullPath = path.join(folder, binName);
try {
fs.accessSync(fullPath, fs.F_OK | fs.X_OK);
this._logger.debug(`Got optional folder binary for ${client.id}: ${fullPath}`);
binPaths.push(fullPath);
} catch (err) {
/* do nothing */
}
});
}
})
.then(() => {
if (!binPaths.length) {
throw new Error(`No binaries found for ${client.id}`);
}
})
.catch((err) => {
this._logger.error(`Unable to resolve ${client.id} executable: ${binName}`);
client.state.available = false;
client.state.failReason = 'notFound';
throw err;
})
.then(() => {
// sanity check each available binary until a good one is found
return Promise.all(binPaths.map((binPath) => {
this._logger.debug(`Running ${client.id} sanity check for binary: ${binPath} ...`);
return this._runSanityCheck(client, binPath)
.catch((err) => {
this._logger.debug(`Sanity check failed for: ${binPath}`);
});
}))
.then(() => {
// if one succeeded then we're good
if (client.activeCli.fullPath) {
return;
}
client.state.available = false;
client.state.failReason = 'sanityCheckFail';
throw new Error('All sanity checks failed');
});
})
.then(() => {
client.state.available = true;
})
.catch((err) => {
this._logger.debug(`${client.id} deemed unavailable`);
client.state.available = false;
})
});
}
/**
* Run sanity check for client.
* @param {Object} client Client config info.
* @param {String} binPath Path to binary (to sanity-check).
*
* @return {Promise}
*/
_runSanityCheck (client, binPath) {
this._logger.debug(`${client.id} binary path: ${binPath}`);
this._logger.debug(`Checking for ${client.id} sanity check ...`);
const sanityCheck = _.get(client, 'activeCli.commands.sanity');
return Promise.resolve()
.then(() => {
if (!sanityCheck) {
throw new Error(`No ${client.id} sanity check found.`);
}
})
.then(() => {
this._logger.debug(`Checking sanity for ${client.id} ...`)
return this._spawn(binPath, sanityCheck.args);
})
.then((output) => {
const haystack = output.stdout + output.stderr;
this._logger.debug(`Sanity check output: ${haystack}`);
const needles = sanityCheck.output || [];
for (let needle of needles) {
if (0 > haystack.indexOf(needle)) {
throw new Error(`Unable to find "${needle}" in ${client.id} output`);
}
}
this._logger.info(`Sanity check passed for ${binPath}`);
// set it!
client.activeCli.fullPath = binPath;
})
.catch((err) => {
this._logger.error(`Sanity check failed for ${client.id}`, err);
throw err;
});
}
/**
* @return {Promise} Resolves to { stdout, stderr } object
*/
_spawn(cmd, args) {
args = args || [];
this._logger.debug(`Exec: "${cmd} ${args.join(' ')}"`);
return spawn(cmd, args);
}
}
exports.Manager = Manager;