-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
324 lines (281 loc) · 7.77 KB
/
index.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
import './alfy.fix.js';
import alfy from 'alfy';
import bytes from 'bytes';
import dayjs from 'dayjs';
import utils from './utils.js';
import alfredNotifier from 'alfred-notifier';
const inpData = String(alfy.input || '');
const action = process.argv[3] || 'encode';
const actionExtData = process.argv[4] || '';
const actions = {
// UUID
uuid() {
const uuid = utils.uuid();
const uuidUp = uuid.replace(/-/g, '').toUpperCase();
const typeid = utils.typeid();
return [
{
title: uuid,
subtitle: 'UUID v4',
arg: uuid
}, {
title: uuidUp,
subtitle: 'UUID v4 with upper',
arg: uuidUp
}, {
title: typeid,
subtitle: 'TypeID(UUIDv7)',
arg: typeid
}
];
},
// TypeID
typeid() {
const items = [];
const typeidDecoded = utils.decodeTypeID(inpData);
if (typeidDecoded) {
items.push({
title: typeidDecoded,
subtitle: 'TypeID decoded',
arg: typeidDecoded
});
}
if (!typeidDecoded) {
const typeid = utils.typeid(inpData);
items.push({
title: typeid,
subtitle: 'TypeID(UUIDv7)',
arg: typeid
});
}
return items;
},
// Format or parse bytes(filesize)
bytes() {
if (isFinite(inpData)) {
const output = bytes.format(+inpData || 0);
return [
{
title: output,
subtitle: 'bytes formated',
arg: output
}
];
}
const output = bytes.parse(inpData);
return [
{
title: output,
subtitle: 'bytes pasred',
arg: output
}
];
},
// Codec
encode() {
const base64 = utils.encodeBase64(inpData);
const safeBase64 = utils.base64ToURLSafe(base64);
const urlEnocded = utils.encodeURI(inpData);
return [
{
title: urlEnocded,
subtitle: 'URL encoded',
arg: urlEnocded
}, {
title: base64,
subtitle: 'base64 encoded',
arg: base64
}, {
title: safeBase64,
subtitle: 'base64(urlsafe) encoded',
arg: safeBase64
}
];
},
decode() {
const urlDeocded = utils.decodeURI(inpData);
const items = [
{
title: urlDeocded,
subtitle: 'URL decoded',
arg: urlDeocded
}
];
// Hex, Unicode: \x4A\x{61}\u7a7a\u{6c14}
const rHex = /\\([xu]){?([A-F0-9]{2,4})}?/ig;
const hexDeStr = inpData.replace(rHex, (a, r, n) => {
const hex = n.padStart(r === 'u' ? 4 : 2, '0');
const point = parseInt(hex, 16) || 0;
return point > 0 ? String.fromCodePoint(point) : a;
});
if(hexDeStr !== inpData) {
items.unshift({
title: hexDeStr,
subtitle: 'HEX/Unicode decoded',
arg: hexDeStr
});
}
const base64Decoded = utils.decodeBase64(inpData);
if(base64Decoded) {
const base64Str = base64Decoded.toString();
items.push({
title: base64Str,
subtitle: 'Base64 decoded',
arg: base64Str
});
}
const typeidDecoded = utils.decodeTypeID(inpData);
if (typeidDecoded) {
items.push({
title: typeidDecoded,
subtitle: 'TypeID decoded',
arg: typeidDecoded
});
}
return items;
},
// Hash
md5() {
const md5Hash = utils.md5(inpData);
return {
title: md5Hash,
subtitle: 'md5',
arg: md5Hash
};
},
sha1() {
const sha1Hash = utils.sha1(inpData);
return {
title: sha1Hash,
subtitle: 'sha1',
arg: sha1Hash
};
},
hash() {
return [
this.md5(),
this.sha1()
];
},
// Datetime
datetime() {
const timestamp = +inpData || 0;
const date = inpData && inpData !== 'now'
? dayjs(timestamp > 0 ? timestamp : inpData)
: dayjs();
const unixSecs = date.unix();
const ms = date.valueOf();
const f1 = date.format('YYYY-MM-DD HH:mm:ss');
// const f2 = date.$d.toString();
const f3 = date.$d.toUTCString();
const f4 = date.$d.toISOString();
return [
{
title: unixSecs,
subtitle: 'UTC Timestamp',
arg: unixSecs
}, {
title: ms,
subtitle: 'UTC Timestamp in milliseconds',
arg: ms
},
{ title: f1, arg: f1 },
// { title: f2, arg: f2 },
{ title: f3, arg: f3 },
{ title: f4, arg: f4 }
];
},
// IP
async localIP() {
const subtitle = 'Press enter to copy, ⌘Enter to get ip info';
const localIP = await utils.getLocalIP();
const localIPv6 = await utils.getLocalIP(6);
const externalIP = await utils.getExternalIP();
const items = [
{
title: `Local IP: ${localIP}`,
arg: localIP,
subtitle
}, {
title: `External IP: ${externalIP}`,
arg: externalIP,
subtitle
}
];
if(localIPv6) {
items.push({
title: `Local IPv6: ${localIPv6}`,
arg: localIPv6,
subtitle
});
}
return items;
},
async ipInfo(ip = inpData) {
const ipInfo = await utils.ipInfo(ip, actionExtData);
const items = [
{
title: ipInfo.ip || ip,
subtitle: 'IP Address'
}
];
const location = [
ipInfo.country || '',
ipInfo.region || '',
ipInfo.city || ''
].join(' ').trim();
if(location) {
items.push({
title: location,
subtitle: 'Location'
});
}
if(ipInfo.isp) {
items.push({
title: ipInfo.isp,
subtitle: 'ISP'
});
}
if(ipInfo.hostname) {
items.push({
title: ipInfo.hostname,
subtitle: 'Hostname'
});
}
return items;
},
async ip() {
try {
const ip = inpData.trim();
if(!ip) {
return await this.localIP();
}
return await this.ipInfo(ip);
}
catch(err) {
return {
title: err.message,
subtitle: 'Press enter to copy error stack',
arg: err.stack
};
}
}
};
async function main() {
let items = [{
title: `No ${action} action defeined.`
}];
if(actions[action]) {
items = (await actions[action]()) || [];
if(!Array.isArray(items)) {
items = [ items ];
}
}
// Debug
// items.push({ title: JSON.stringify(alfy.meta) });
// items.push({ title: JSON.stringify(alfy.alfred) });
alfy.output(items);
// Checks for available update and updates the `info.plist`
alfredNotifier();
}
main();