-
Notifications
You must be signed in to change notification settings - Fork 133
/
utils.ts
107 lines (98 loc) · 3.43 KB
/
utils.ts
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
import * as vscode from 'vscode';
export class Utils {
public static quote(str: string): string {
if (str.includes(' ')) return `"${str.replace('"', '\\"')}"`;
return str;
}
public static apiKeyInvalid(key?: string): string {
const err = 'Invalid api key... check https://wakatime.com/api-key for your key';
if (!key) return err;
const re = new RegExp(
'^(waka_)?[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
'i',
);
if (!re.test(key)) return err;
return '';
}
public static validateProxy(proxy: string): string {
if (!proxy) return '';
let re;
if (proxy.indexOf('\\') === -1) {
re = new RegExp('^((https?|socks5):https://)?([^:@]+(:([^:@])+)?@)?[\\w\\.-]+(:\\d+)?$', 'i');
} else {
re = new RegExp('^.*\\\\.+$', 'i');
}
if (!re.test(proxy)) {
const ipv6 = new RegExp(
'^((https?|socks5):https://)?([^:@]+(:([^:@])+)?@)?(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(:\\d+)?$',
'i',
);
if (!ipv6.test(proxy)) {
return 'Invalid proxy. Valid formats are https://user:pass@host:port or socks5:https://user:pass@host:port or domain\\user:pass';
}
}
return '';
}
public static formatDate(date: Date): String {
let months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
let ampm = 'AM';
let hour = date.getHours();
if (hour > 11) {
ampm = 'PM';
hour = hour - 12;
}
if (hour == 0) {
hour = 12;
}
let minute = date.getMinutes();
return `${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()} ${hour}:${
minute < 10 ? `0${minute}` : minute
} ${ampm}`;
}
public static obfuscateKey(key: string): string {
let newKey = '';
if (key) {
newKey = key;
if (key.length > 4)
newKey = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' + key.substring(key.length - 4);
}
return newKey;
}
public static wrapArg(arg: string): string {
if (arg.indexOf(' ') > -1) return '"' + arg.replace(/"/g, '\\"') + '"';
return arg;
}
public static formatArguments(binary: string, args: string[]): string {
let clone = args.slice(0);
clone.unshift(this.wrapArg(binary));
let newCmds: string[] = [];
let lastCmd = '';
for (let i = 0; i < clone.length; i++) {
if (lastCmd == '--key') newCmds.push(this.wrapArg(this.obfuscateKey(clone[i])));
else newCmds.push(this.wrapArg(clone[i]));
lastCmd = clone[i];
}
return newCmds.join(' ');
}
public static isRemoteUri(uri: vscode.Uri): boolean {
if (!uri) return false;
return uri.scheme == 'vscode-remote';
}
public static isPullRequest(uri: vscode.Uri): boolean {
if (!uri) return false;
return uri.scheme == 'pr';
}
}