utilitas 1989.9.46 → 1989.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +94 -36
- package/lib/shot.mjs +33 -7
- package/lib/uoid.mjs +0 -1
- package/lib/utilitas.mjs +2 -3
- package/package.json +8 -2
package/index.mjs
CHANGED
|
@@ -1,39 +1,97 @@
|
|
|
1
1
|
// dependencies
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
import { default as colors } from 'colors/safe.js';
|
|
3
|
+
import { default as fetch } from 'node-fetch';
|
|
4
|
+
import { Tail as tail } from 'tail';
|
|
5
|
+
import * as base64url from 'base64url';
|
|
6
|
+
import * as fileType from 'file-type';
|
|
7
|
+
import * as geoIp from 'fast-geoip';
|
|
8
|
+
import * as ini from 'ini';
|
|
9
|
+
import * as jwt from 'jsonwebtoken';
|
|
10
|
+
import * as mailgun from 'mailgun-js';
|
|
11
|
+
import * as mailjet from 'node-mailjet';
|
|
12
|
+
import * as math from 'mathjs';
|
|
13
|
+
import * as mysql from 'mysql2/promise';
|
|
14
|
+
import * as ping from 'ping';
|
|
15
|
+
import * as qs from 'qs';
|
|
16
|
+
import * as redis from 'ioredis';
|
|
17
|
+
import * as sentry from '@sentry/node';
|
|
18
|
+
import * as telegraf from 'telegraf';
|
|
19
|
+
import * as telesign from 'telesignsdk';
|
|
20
|
+
import * as twilio from 'twilio';
|
|
21
|
+
import * as uuid from 'uuid';
|
|
23
22
|
// features
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
23
|
+
import * as bot from './lib/bot.mjs';
|
|
24
|
+
import * as cache from './lib/cache.mjs';
|
|
25
|
+
import * as dbio from './lib/dbio.mjs';
|
|
26
|
+
import * as email from './lib/email.mjs';
|
|
27
|
+
import * as encryption from './lib/encryption.mjs';
|
|
28
|
+
import * as event from './lib/event.mjs';
|
|
29
|
+
import * as network from './lib/network.mjs';
|
|
30
|
+
import * as sentinel from './lib/sentinel.mjs';
|
|
31
|
+
import * as shell from './lib/shell.mjs';
|
|
32
|
+
import * as shot from './lib/shot.mjs';
|
|
33
|
+
import * as sms from './lib/sms.mjs';
|
|
34
|
+
import * as storage from './lib/storage.mjs';
|
|
35
|
+
import * as tape from './lib/tape.mjs';
|
|
36
|
+
import * as uoid from './lib/uoid.mjs';
|
|
37
|
+
import * as utilitas from './lib/utilitas.mjs';
|
|
38
|
+
|
|
39
|
+
// Export
|
|
39
40
|
export * as default from './lib/utilitas.mjs';
|
|
41
|
+
export {
|
|
42
|
+
// dependencies
|
|
43
|
+
colors,
|
|
44
|
+
fetch,
|
|
45
|
+
tail,
|
|
46
|
+
base64url,
|
|
47
|
+
fileType,
|
|
48
|
+
geoIp,
|
|
49
|
+
ini,
|
|
50
|
+
jwt,
|
|
51
|
+
mailgun,
|
|
52
|
+
mailjet,
|
|
53
|
+
math,
|
|
54
|
+
mysql,
|
|
55
|
+
ping,
|
|
56
|
+
qs,
|
|
57
|
+
redis,
|
|
58
|
+
sentry,
|
|
59
|
+
telegraf,
|
|
60
|
+
telesign,
|
|
61
|
+
twilio,
|
|
62
|
+
uuid,
|
|
63
|
+
// features
|
|
64
|
+
bot,
|
|
65
|
+
cache,
|
|
66
|
+
dbio,
|
|
67
|
+
email,
|
|
68
|
+
encryption,
|
|
69
|
+
event,
|
|
70
|
+
network,
|
|
71
|
+
sentinel,
|
|
72
|
+
shell,
|
|
73
|
+
shot,
|
|
74
|
+
sms,
|
|
75
|
+
storage,
|
|
76
|
+
tape,
|
|
77
|
+
uoid,
|
|
78
|
+
utilitas,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Browser
|
|
82
|
+
let runningInBrowser = false;
|
|
83
|
+
try { runningInBrowser = !!window; } catch (e) { }
|
|
84
|
+
// import * as package from './package.json';
|
|
85
|
+
// https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/
|
|
86
|
+
const pack = {
|
|
87
|
+
...await storage.readJson('./package.json'),
|
|
88
|
+
runningInBrowser,
|
|
89
|
+
};
|
|
90
|
+
if (runningInBrowser) {
|
|
91
|
+
window._utilitas = pack;
|
|
92
|
+
window.utilitas = {
|
|
93
|
+
base64url, colors, encryption, event, fetch, fileType, ini, jwt, math,
|
|
94
|
+
network, qs, shell, shot, storage, uoid, utilitas, uuid,
|
|
95
|
+
};
|
|
96
|
+
console.log('[UTILITAS](https://github.com/Leask/utilitas) is ready!');
|
|
97
|
+
} else { global._utilitas = pack; }
|
package/lib/shot.mjs
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import { sha256 } from './encryption.mjs';
|
|
1
3
|
import * as fileType from 'file-type';
|
|
2
4
|
import * as storage from './storage.mjs';
|
|
3
5
|
import * as utilitas from './utilitas.mjs';
|
|
4
6
|
import fetch from 'node-fetch';
|
|
7
|
+
import path from 'path';
|
|
5
8
|
|
|
6
9
|
const defFetchOpt = { redirect: 'follow', follow: 3, timeout: 1000 * 10 };
|
|
10
|
+
const fakeRsp = { status: 200, statusText: 'OK', headers: { get() { }, raw() { } } };
|
|
11
|
+
const buf2utf = buf => buf.toString('utf8');
|
|
7
12
|
|
|
8
13
|
const getVersionOnNpm = async (packName) => {
|
|
9
14
|
utilitas.assert(packName, 'Package name is required.', 400);
|
|
@@ -40,15 +45,29 @@ const getCurrentPosition = async () => {
|
|
|
40
45
|
return rp;
|
|
41
46
|
};
|
|
42
47
|
|
|
48
|
+
// @todo: support etag cache and expire time features:
|
|
49
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
|
|
43
50
|
const get = async (url, options) => {
|
|
44
51
|
utilitas.assert(url, 'URL is required.');
|
|
45
52
|
options = options || {};
|
|
46
53
|
options.encode = utilitas.ensureString(options.encode, { case: 'UP' });
|
|
47
|
-
const
|
|
48
|
-
const
|
|
54
|
+
const requestHash = sha256(JSON.stringify({ url, ...options }));
|
|
55
|
+
const cachePath = options.cache?.tmp || process.env.TMPDIR;
|
|
56
|
+
const cacheFile = cachePath ? path.join(cachePath, requestHash) : null;
|
|
57
|
+
const cacheable = options.cache && cacheFile;
|
|
58
|
+
let cacheAction, content;
|
|
59
|
+
const cache = cacheable && await utilitas.ignoreErrFunc(async () => {
|
|
60
|
+
const resp = await fs.readFile(cacheFile);
|
|
61
|
+
cacheAction = 'READ';
|
|
62
|
+
return resp;
|
|
63
|
+
});
|
|
64
|
+
const r = cache
|
|
65
|
+
? { ...fakeRsp, async buffer() { return cache } }
|
|
66
|
+
: await fetch(url, { defFetchOpt, ...options.fetch || {} });
|
|
49
67
|
const [htpMime, buffer] = [r.headers.get('content-type'), await r.buffer()];
|
|
50
68
|
const bufMime = utilitas.extract(await fileType.fileTypeFromBuffer(buffer), 'mime');
|
|
51
|
-
|
|
69
|
+
const mimeType = bufMime || htpMime;
|
|
70
|
+
const length = buffer.length;
|
|
52
71
|
switch (options.encode) {
|
|
53
72
|
case 'BUFFER':
|
|
54
73
|
content = buffer;
|
|
@@ -60,18 +79,25 @@ const get = async (url, options) => {
|
|
|
60
79
|
content = storage.encodeBase64DataURL(mimeType, buffer);
|
|
61
80
|
break;
|
|
62
81
|
case 'JSON':
|
|
63
|
-
try { content = JSON.parse(
|
|
82
|
+
try { content = JSON.parse(buf2utf(buffer)); } catch (e) { }
|
|
64
83
|
break;
|
|
65
84
|
case 'TEXT':
|
|
66
|
-
content =
|
|
85
|
+
content = buf2utf(buffer);
|
|
67
86
|
break;
|
|
68
87
|
default:
|
|
69
88
|
utilitas.assert(!options.encode, 'Invalid encoding.', 400);
|
|
70
|
-
content =
|
|
89
|
+
content = buf2utf(buffer);
|
|
71
90
|
}
|
|
91
|
+
cacheable && !cache && length && r.status === 200
|
|
92
|
+
&& await utilitas.ignoreErrFunc(async () => {
|
|
93
|
+
const resp = await fs.writeFile(cacheFile, buffer);
|
|
94
|
+
cacheAction = 'WRITE';
|
|
95
|
+
return resp;
|
|
96
|
+
});
|
|
72
97
|
return {
|
|
73
|
-
statusCode: r.status, statusText: r.statusText, length
|
|
98
|
+
statusCode: r.status, statusText: r.statusText, length,
|
|
74
99
|
mimeType, content, headers: r.headers.raw(), response: r,
|
|
100
|
+
cache: cacheable && { filename: cacheFile, action: cacheAction },
|
|
75
101
|
};
|
|
76
102
|
}
|
|
77
103
|
|
package/lib/uoid.mjs
CHANGED
|
@@ -4,7 +4,6 @@ import * as uuid from 'uuid';
|
|
|
4
4
|
|
|
5
5
|
const fileURLToPath = (await import('url')).fileURLToPath
|
|
6
6
|
|| ((url) => { return new URL('', url).pathname; });
|
|
7
|
-
|
|
8
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
9
8
|
const uuidRegTxt = '[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}';
|
|
10
9
|
const compactUuid = (uuid) => uuid.replace(/\-/ig, '');
|
package/lib/utilitas.mjs
CHANGED
|
@@ -14,7 +14,6 @@ RegExp.escape = RegExp.escape || ((str) => { //$& means the whole matched string
|
|
|
14
14
|
|
|
15
15
|
const fileURLToPath = (await import('url')).fileURLToPath
|
|
16
16
|
|| ((url) => { return new URL('', url).pathname; });
|
|
17
|
-
|
|
18
17
|
const __filename = fileURLToPath(import.meta.url);
|
|
19
18
|
const invalidTime = 'Invalid time.';
|
|
20
19
|
|
|
@@ -591,8 +590,8 @@ const checkInterval = (itv, sed) => {
|
|
|
591
590
|
return !((Math.round(Date.now() / 1000) + ensureInt(sed)) % ensureInt(itv));
|
|
592
591
|
};
|
|
593
592
|
|
|
594
|
-
const ignoreErrFunc = async (
|
|
595
|
-
try { return await
|
|
593
|
+
const ignoreErrFunc = async (func, options) => {
|
|
594
|
+
try { return await func(); } catch (e) { options?.log && console.error(e); }
|
|
596
595
|
};
|
|
597
596
|
|
|
598
597
|
const tryUntil = async (fnTry, options) => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilitas",
|
|
3
3
|
"description": "Just another common utility for Node.js.",
|
|
4
|
-
"version": "1989.
|
|
4
|
+
"version": "1989.10.3",
|
|
5
5
|
"private": false,
|
|
6
6
|
"homepage": "https://github.com/Leask/utilitas",
|
|
7
7
|
"main": "index.mjs",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"ncuinit": "npm install -g npm-check-updates",
|
|
17
17
|
"updep": "npx ncu -u && npm install && npm install node-fetch@2 && ( git commit -am 'update dependencies' || true )",
|
|
18
18
|
"gitsync": "git pull && git push",
|
|
19
|
-
"
|
|
19
|
+
"build": "./node_modules/.bin/webpack-cli --config webpack.config.mjs",
|
|
20
|
+
"prepublishOnly": "npm run build && npm version patch && npm run updep && npm run gitsync"
|
|
20
21
|
},
|
|
21
22
|
"author": "Leask Wong <i@leaskh.com>",
|
|
22
23
|
"license": "MIT",
|
|
@@ -45,5 +46,10 @@
|
|
|
45
46
|
"telesignsdk": "^2.2.1",
|
|
46
47
|
"twilio": "^3.75.1",
|
|
47
48
|
"uuid": "^8.3.2"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"browserify-fs": "^1.0.0",
|
|
52
|
+
"node-polyfill-webpack-plugin": "^1.1.4",
|
|
53
|
+
"webpack-cli": "^4.9.2"
|
|
48
54
|
}
|
|
49
55
|
}
|