-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathutils.js
693 lines (616 loc) · 16.9 KB
/
utils.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
let http = require('follow-redirects').http;
let https = require('follow-redirects').https;
let toposortGraph = require('./toposort.js');
const {canonicalHost} = require('./host');
const balanceRegex = /([0-9]+) ?([a-zA-Z]*)/;
function joinPath() {
const path = require('path');
return path.join.apply(path.join, arguments);
}
function dirname() {
const path = require('path');
return path.dirname.apply(path.dirname, arguments);
}
function filesMatchingPattern(files) {
const globule = require('globule');
return globule.find(files, {nonull: true});
}
function fileMatchesPattern(patterns, intendedPath) {
const globule = require('globule');
return globule.isMatch(patterns, intendedPath);
}
function recursiveMerge(target, source) {
const merge = require('merge');
return merge.recursive(target, source);
}
function checkIsAvailable(url, callback) {
http.get(url, function (_res) {
callback(true);
}).on('error', function (_res) {
callback(false);
});
}
function httpGetRequest(httpObj, url, callback) {
httpObj.get(url, function (res) {
let body = '';
res.on('data', function (d) {
body += d;
});
res.on('end', function () {
callback(null, body);
});
}).on('error', function (err) {
callback(err);
});
}
function httpGet(url, callback) {
httpGetRequest(http, url, callback);
}
function httpsGet(url, callback) {
httpGetRequest(https, url, callback);
}
function httpGetJson(url, callback) {
httpGetRequest(http, url, function (err, body) {
try {
let parsed = body && JSON.parse(body);
return callback(err, parsed);
} catch (e) {
return callback(e);
}
});
}
function httpsGetJson(url, callback) {
httpGetRequest(https, url, function (err, body) {
try {
let parsed = JSON.parse(body);
return callback(err, parsed);
} catch (e) {
return callback(e);
}
});
}
function getJson(url, cb) {
if (url.indexOf('https') === 0) {
return httpsGetJson(url, cb);
}
httpGetJson(url, cb);
}
function pingEndpoint(host, port, type, protocol, origin, callback, count = 0, start = Date.now()) {
// remove trailing api key from infura, e.g. rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
const _host = host.indexOf('/') > -1 ? host.split('/')[0] : host;
const maxWait = 600000;
const timeout = 100;
const adjustTimeout = () => {
const t = Math.floor(
(1 + (0.1 * (count && (Math.pow(2, count - 1) / 1)))) * timeout
);
return t > 3000 ? 3000 : t;
};
let alreadyClosed = false;
let retrying = false;
const shouldCallback = (...args) => {
if (args.length) {
callback._shouldCallback = !!args[0];
}
let shouldCallback;
if (callback.hasOwnProperty('_shouldCallback')) {
shouldCallback = !!callback._shouldCallback;
} else {
shouldCallback = true;
}
return shouldCallback;
};
const cleanup = (req, closeMethod) => {
if (!alreadyClosed) {
alreadyClosed = true;
setImmediate(() => { req[closeMethod](); });
}
};
const handleEvent = (req, closeMethod, retryCond, ...args) => {
if (shouldCallback()) {
if (!retrying && retryCond) {
retrying = true;
setImmediate(() => {
pingEndpoint(
host, port, type, protocol, origin, callback, ++count, start
);
});
} else if (!alreadyClosed) {
shouldCallback(false);
callback(...args);
}
}
// following cleanup any later events on req will effectively be ignored
cleanup(req, closeMethod);
};
const handleError = (req, closeMethod) => {
req.on('error', (err) => {
handleEvent(
req,
closeMethod,
(/timed out/).test(err.message) && Date.now() - start < maxWait,
err
);
});
};
const handleSuccess = (req, closeMethod, event) => {
req.once(event, () => { handleEvent(req, closeMethod, false); });
};
const handleRequest = (req, closeMethod, event) => {
handleError(req, closeMethod);
handleSuccess(req, closeMethod, event);
};
if (type === 'ws') {
const req = new (require('ws'))(
`${protocol === 'https' ? 'wss' : 'ws'}://${_host}:${port}/`,
{handshakeTimeout: adjustTimeout(), origin}
);
handleRequest(req, 'close', 'open');
} else {
const req = (protocol === 'https' ? require('https') : require('http')).get(
{host: _host, origin, port}
);
handleRequest(req, 'abort', 'response');
req.setTimeout(adjustTimeout(), () => {
req.emit('error', new Error('timed out'));
});
}
}
function runCmd(cmd, options, callback) {
const shelljs = require('shelljs');
options = Object.assign({silent: true, exitOnError: true, async: true}, options || {});
const outputToConsole = !options.silent;
options.silent = true;
let result = shelljs.exec(cmd, options, function (code, stdout) {
if(code !== 0) {
if (options.exitOnError) {
return exit();
}
if(typeof callback === 'function') {
callback(`shell returned code ${code}`);
}
} else {
if(typeof callback === 'function') {
return callback(null, stdout);
}
}
});
result.stdout.on('data', function(data) {
if(outputToConsole) {
console.log(data);
}
});
result.stderr.on('data', function(data) {
if (outputToConsole) {
console.log(data);
}
});
}
function cd(folder) {
const shelljs = require('shelljs');
shelljs.cd(folder);
}
function sed(file, pattern, replace) {
const shelljs = require('shelljs');
shelljs.sed('-i', pattern, replace, file);
}
function exit(code) {
process.exit(code);
}
function downloadFile(url, dest, cb) {
const o_fs = require('fs-extra');
var file = o_fs.createWriteStream(dest);
(url.substring(0, 5) === 'https' ? https : http).get(url, function (response) {
if (response.statusCode !== 200) {
cb(`Download failed, response code ${response.statusCode}`);
return;
}
response.pipe(file);
file.on('finish', function () {
file.close(cb);
});
}).on('error', function (err) {
o_fs.unlink(dest);
cb(err.message);
});
}
function extractTar(filename, packageDirectory, cb) {
const o_fs = require('fs-extra');
const tar = require('tar');
o_fs.createReadStream(filename).pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function () {
cb();
})
);
}
function extractZip(filename, packageDirectory, opts, cb) {
const decompress = require('decompress');
decompress(filename, packageDirectory, opts).then((_files) => {
cb();
});
}
function proposeAlternative(word, _dictionary, _exceptions) {
const propose = require('propose');
let exceptions = _exceptions || [];
let dictionary = _dictionary.filter((entry) => {
return exceptions.indexOf(entry) < 0;
});
return propose(word, dictionary, {threshold: 0.3});
}
function getExternalContractUrl(file,providerUrl) {
const constants = require('../constants');
let url;
const RAW_URL = 'https://raw.githubusercontent.com/';
const DEFAULT_SWARM_GATEWAY = 'https://swarm-gateways.net/';
const MALFORMED_SWARM_ERROR = 'Malformed Swarm gateway URL for ';
const MALFORMED_ERROR = 'Malformed Github URL for ';
const MALFORMED_IPFS_ERROR = 'Malformed IPFS URL for ';
const IPFS_GETURL_NOTAVAILABLE = 'IPFS getUrl is not available. Please set it in your storage config. For more info: https://embark.status.im/docs/storage_configuration.html';
if (file.startsWith('https://github')) {
const match = file.match(/https:\/\/github\.[a-z]+\/(.*)/);
if (!match) {
console.error(MALFORMED_ERROR + file);
return null;
}
url = `${RAW_URL}${match[1].replace('blob/', '')}`;
} else if (file.startsWith('ipfs')) {
if(!providerUrl) {
console.error(IPFS_GETURL_NOTAVAILABLE);
return null;
}
let match = file.match(/ipfs:\/\/([-a-zA-Z0-9]+)\/(.*)/);
if(!match) {
match = file.match(/ipfs:\/\/([-a-zA-Z0-9]+)/);
if(!match) {
console.error(MALFORMED_IPFS_ERROR + file);
return null;
}
}
let matchResult = match[1];
if(match[2]) {
matchResult += '/' + match[2];
}
url = `${providerUrl}${matchResult}`;
return {
url,
filePath: constants.httpContractsDirectory + matchResult
};
} else if (file.startsWith('git')) {
// Match values
// [0] entire input
// [1] git://
// [2] user
// [3] repository
// [4] path
// [5] branch
const match = file.match(
/(git:\/\/)?github\.[a-z]+\/([-a-zA-Z0-9@:%_+.~#?&=]+)\/([-a-zA-Z0-9@:%_+.~#?&=]+)\/([-a-zA-Z0-9@:%_+.~?\/&=]+)#?([a-zA-Z0-9\/_.-]*)?/
);
if (!match) {
console.error(MALFORMED_ERROR + file);
return null;
}
let branch = match[5];
if (!branch) {
branch = 'master';
}
url = `${RAW_URL}${match[2]}/${match[3]}/${branch}/${match[4]}`;
} else if (file.startsWith('http')) {
url = file;
} else if(file.startsWith('bzz')){
if(!providerUrl) {
url = DEFAULT_SWARM_GATEWAY + file;
} else {
let match = file.match(/bzz:\/([-a-zA-Z0-9]+)\/(.*)/);
if(!match){
match = file.match(/bzz:\/([-a-zA-Z0-9]+)/);
if(!match){
console.log(MALFORMED_SWARM_ERROR + file);
return null;
}
}
url = providerUrl + '/' + file;
}
} else {
return null;
}
const match = url.match(
/\.[a-z]+\/([-a-zA-Z0-9@:%_+.~#?&\/=]+)/
);
return {
url,
filePath: constants.httpContractsDirectory + match[1]
};
}
function hexToNumber(hex) {
const Web3 = require('web3');
return Web3.utils.hexToNumber(hex);
}
function isHex(hex) {
const Web3 = require('web3');
return Web3.utils.isHex(hex);
}
function hashTo32ByteHexString(hash) {
if (isHex(hash)) {
if (!hash.startsWith('0x')) {
hash = '0x' + hash;
}
return hash;
}
const multihash = require('multihashes');
let buf = multihash.fromB58String(hash);
let digest = multihash.decode(buf).digest;
return '0x' + multihash.toHexString(digest);
}
function isValidDomain(v) {
// from: https://github.com/miguelmota/is-valid-domain
if (typeof v !== 'string') return false;
var parts = v.split('.');
if (parts.length <= 1) return false;
var tld = parts.pop();
var tldRegex = /^(?:xn--)?[a-zA-Z0-9]+$/gi;
if (!tldRegex.test(tld)) return false;
var isValid = parts.every(function(host) {
var hostRegex = /^(?!:\/\/)([a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$/gi;
return hostRegex.test(host);
});
return isValid;
}
function decodeParams(typesArray, hexString) {
var Web3EthAbi = require('web3-eth-abi');
return Web3EthAbi.decodeParameters(typesArray, hexString);
}
function toChecksumAddress(address) {
const Web3 = require('web3');
return Web3.utils.toChecksumAddress(address);
}
function sha3(arg) {
const Web3 = require('web3');
return Web3.utils.sha3(arg);
}
function soliditySha3(arg) {
const Web3 = require('web3');
return Web3.utils.soliditySha3(arg);
}
function normalizeInput(input) {
if(typeof input === 'string') return input;
let args = Object.values(input);
if (args.length === 0) {
return "";
}
if (args.length === 1) {
if (Array.isArray(args[0])) {
return args[0].join(',');
}
return args[0] || "";
}
return ('[' + args.map((x) => {
if (x === null) {
return "null";
}
if (x === undefined) {
return "undefined";
}
if (Array.isArray(x)) {
return x.join(',');
}
return x;
}).toString() + ']');
}
/**
* Builds a URL
*
* @param {string} protocol
* The URL protocol, defaults to http.
* @param {string} host
* The URL host, required.
* @param {string} port
* The URL port, default to empty string.
* @param {string} [type]
* Type of connection
* @returns {string} the constructued URL, with defaults
*/
function buildUrl(protocol, host, port, type) {
if (!host) throw new Error('utils.buildUrl: parameter \'host\' is required');
if (port) port = ':' + port;
else port = '';
if (!protocol) {
protocol = type === 'ws' ? 'ws' : 'http';
}
return `${protocol}://${host}${port}`;
}
/**
* Builds a URL
*
* @param {object} configObj Object containing protocol, host, and port to be used to construct the url.
* * protocol {String} (optional) The URL protocol, defaults to http.
* * host {String} (required) The URL host.
* * port {String} (optional) The URL port, default to empty string.
* @returns {string} the constructued URL, with defaults
*/
function buildUrlFromConfig(configObj) {
if (!configObj) throw new Error('[utils.buildUrlFromConfig]: config object must cannot be null');
if (!configObj.host) throw new Error('[utils.buildUrlFromConfig]: object must contain a \'host\' property');
return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port, configObj.type);
}
function deconstructUrl(endpoint) {
const matches = endpoint.match(/(ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?/);
return {
protocol: matches[1],
host: matches[2],
port: matches[3],
type: matches[1] === 'ws' ? 'ws' : 'rpc'
};
}
function getWeiBalanceFromString(balanceString, web3){
if(!web3){
throw new Error(__('[utils.getWeiBalanceFromString]: Missing parameter \'web3\''));
}
if (!balanceString) {
return 0;
}
const match = balanceString.match(balanceRegex);
if (!match) {
throw new Error(__('Unrecognized balance string "%s"', balanceString));
}
if (!match[2]) {
return web3.utils.toHex(parseInt(match[1], 10));
}
return web3.utils.toWei(match[1], match[2]);
}
function getHexBalanceFromString(balanceString, web3) {
if(!web3){
throw new Error(__('[utils.getWeiBalanceFromString]: Missing parameter \'web3\''));
}
if (!balanceString) {
return 0xFFFFFFFFFFFFFFFFFF;
}
if (web3.utils.isHexStrict(balanceString)) {
return balanceString;
}
const match = balanceString.match(balanceRegex);
if (!match) {
throw new Error(__('Unrecognized balance string "%s"', balanceString));
}
if (!match[2]) {
return web3.utils.toHex(parseInt(match[1], 10));
}
return web3.utils.toHex(web3.utils.toWei(match[1], match[2]));
}
function compact(array) {
return array.filter(n => n);
}
function groupBy(array, key) {
return array.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
function sample(array) {
return array[Math.floor(Math.random() * array.length)];
}
function last(array) {
return array[array.length - 1];
}
function interceptLogs(consoleContext, logger) {
let context = {};
context.console = consoleContext;
context.console.log = function () {
logger.info(normalizeInput(arguments));
};
context.console.warn = function () {
logger.warn(normalizeInput(arguments));
};
context.console.info = function () {
logger.info(normalizeInput(arguments));
};
context.console.debug = function () {
// TODO: ue JSON.stringify
logger.debug(normalizeInput(arguments));
};
context.console.trace = function () {
logger.trace(normalizeInput(arguments));
};
context.console.dir = function () {
logger.dir(normalizeInput(arguments));
};
}
function errorMessage(e) {
if (typeof e === 'string') {
return e;
} else if (e && e.message) {
return e.message;
}
return e;
}
function timer(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function isFolder(node) {
return node.children && node.children.length;
}
function isNotFolder(node){
return !isFolder(node);
}
function byName(a, b) {
return a.name.localeCompare(b.name);
}
function fileTreeSort(nodes){
const folders = nodes.filter(isFolder).sort(byName);
const files = nodes.filter(isNotFolder).sort(byName);
return folders.concat(files);
}
function copyToClipboard(text) {
const clipboardy = require('clipboardy');
clipboardy.writeSync(text);
}
function fuzzySearch(text, list, filter) {
const fuzzy = require('fuzzy');
return fuzzy.filter(text, list, {extract: (filter || function () {})});
}
function jsonFunctionReplacer(_key, value) {
if (typeof value === 'function') {
return value.toString();
}
return value;
}
function getWindowSize() {
const windowSize = require('window-size');
return windowSize.get();
}
function toposort(graph) {
return toposortGraph(graph);
}
module.exports = {
joinPath,
dirname,
filesMatchingPattern,
fileMatchesPattern,
recursiveMerge,
checkIsAvailable,
httpGet,
httpsGet,
httpGetJson,
httpsGetJson,
getJson,
hexToNumber,
isHex,
hashTo32ByteHexString,
isValidDomain,
pingEndpoint,
decodeParams,
runCmd,
cd,
sed,
exit,
downloadFile,
extractTar,
extractZip,
proposeAlternative,
getExternalContractUrl,
toChecksumAddress,
sha3,
soliditySha3,
normalizeInput,
buildUrl,
buildUrlFromConfig,
deconstructUrl,
getWeiBalanceFromString,
getHexBalanceFromString,
compact,
groupBy,
sample,
last,
interceptLogs,
errorMessage,
timer,
fileTreeSort,
copyToClipboard,
fuzzySearch,
jsonFunctionReplacer,
getWindowSize,
toposort
};