utilitas 1989.8.70 → 1989.8.75

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/lib/encryption.js CHANGED
@@ -38,7 +38,7 @@ const random = (length) => {
38
38
  const randomString = (length = 128, encoding = 'HEX') => {
39
39
  let byteLength = Math.ceil(~~length / 2);
40
40
  byteLength = byteLength > 0 ? byteLength : 1;
41
- return random(byteLength).toString(encoding).substr(0, length);
41
+ return random(byteLength).toString(encoding).substring(0, length);
42
42
  };
43
43
 
44
44
  const digestObject = (object, algorithm) => {
package/lib/storage.js CHANGED
@@ -1,11 +1,27 @@
1
1
  'use strict';
2
2
 
3
- const encoding = 'utf8';
3
+ const [encoding, mode] = ['utf8', '0644'];
4
4
 
5
5
  const handleError = (err, opts) => {
6
6
  if (opts?.throw) { throw err; } else if (opts?.log) { console.log(err); }
7
7
  };
8
8
 
9
+ const stringify = (data, opts) => {
10
+ return JSON.stringify(data || {}, opts?.replacer || null, opts?.space || 4);
11
+ };
12
+
13
+ const writeTempFile = async (data, options) => {
14
+ let { filename, encoding: ec, mode: md, prefix, suffix: s } = options || {};
15
+ filename = `${prefix || ''}${filename || uuid.v4()}${s ? `.${s}` : ''}`;
16
+ if (String(ec).toUpperCase() === 'JSON') {
17
+ data = stringify(data, options); ec = null;
18
+ }
19
+ const filepath = path.join(os.tmpdir(), filename);
20
+ await fs.promises.writeFile(filepath, data, ec || encoding);
21
+ await fs.promises.chmod(filepath, md || mode);
22
+ return filepath;
23
+ };
24
+
9
25
  const readJson = async (filename, options) => {
10
26
  let data = {};
11
27
  try {
@@ -17,11 +33,8 @@ const readJson = async (filename, options) => {
17
33
  };
18
34
 
19
35
  const writeJson = async (filename, data, options) => {
20
- const json = JSON.stringify(
21
- data || {}, options?.replacer || null, options?.space || 4
22
- );
23
36
  return await fs.promises.writeFile(
24
- filename, json, options?.encoding || encoding
37
+ filename, stringify(data, options), options?.encoding || encoding
25
38
  );
26
39
  };
27
40
 
@@ -143,10 +156,12 @@ module.exports = {
143
156
  setConfig,
144
157
  writeIni,
145
158
  writeJson,
159
+ writeTempFile,
146
160
  }
147
161
 
148
- const utilitas = require('./utilitas');
149
- const path = require('path');
162
+ const fs = require('fs');
150
163
  const ini = require('ini');
151
164
  const os = require('os');
152
- const fs = require('fs');
165
+ const path = require('path');
166
+ const utilitas = require('./utilitas');
167
+ const uuid = require('uuid');
package/lib/utilitas.js CHANGED
@@ -12,6 +12,11 @@ RegExp.escape = RegExp.escape || ((str) => { //$& means the whole matched string
12
12
 
13
13
  const invalidTime = 'Invalid time.';
14
14
 
15
+ const tryOpts = {
16
+ interval: 1000 * 1, maxTry: 10, log: false,
17
+ verify: async (err, res) => { return !err && res; },
18
+ };
19
+
15
20
  const clone = (any) => {
16
21
  let resp = any; // typeof Object === 'function' || etc
17
22
  switch (getType(any)) {
@@ -577,6 +582,23 @@ const ignoreErrFunc = async (fn, args, options) => {
577
582
  catch (err) { return options?.log ? console.error(err) : err; }
578
583
  };
579
584
 
585
+ const tryUntil = async (fnTry, options) => {
586
+ options = { ...tryOpts, ...options || {} };
587
+ let [curTry, result, err] = [0, null, null];
588
+ while (curTry++ < options.maxTry && !(await options.verify(err, result))) {
589
+ try {
590
+ result = await fnTry();
591
+ err = null;
592
+ } catch (e) {
593
+ err = e;
594
+ options?.log && console.error(e);
595
+ await timeout(options.interval);
596
+ }
597
+ }
598
+ err && throwError(err, 500);
599
+ return result;
600
+ };
601
+
580
602
  // https://www.unicode.org/Public/13.0.0/ucd/emoji/emoji-data.txt
581
603
  // https://stackoverflow.com/questions/10992921/how-to-remove-emoji-code-using-javascript
582
604
  // https://twitter.com/_cxa/status/1384304473493831681?s=20
@@ -660,6 +682,7 @@ module.exports = {
660
682
  timeout,
661
683
  toExponential,
662
684
  trim,
685
+ tryUntil,
663
686
  uniqueArray,
664
687
  verifyEmail,
665
688
  verifyPhone,
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.8.70",
4
+ "version": "1989.8.75",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.js",
@@ -10,7 +10,7 @@
10
10
  "debug": "node --inspect --trace-warnings debug.js",
11
11
  "test": "node --inspect --trace-warnings test.js",
12
12
  "ncuinit": "npm install -g npm-check-updates",
13
- "updep": "npx ncu -u && npm install && npm install node-fetch@2 && ( git commit -am 'update dependencies' || true )",
13
+ "updep": "npx ncu -u && npm install && npm install file-type@16 && npm install node-fetch@2 && npm install public-ip@4 && ( git commit -am 'update dependencies' || true )",
14
14
  "gitsync": "git pull && git push",
15
15
  "prepublishOnly": "npm version patch && npm run updep && npm run gitsync"
16
16
  },
@@ -21,26 +21,26 @@
21
21
  "url": "https://github.com/Leask/utilitas.git"
22
22
  },
23
23
  "dependencies": {
24
- "@sentry/node": "^6.15.0",
24
+ "@sentry/node": "^6.16.1",
25
25
  "base64url": "^3.0.1",
26
- "colors": "^1.4.0",
27
- "fast-geoip": "^1.1.47",
28
- "file-type": "^17.0.0",
26
+ "colors": "^1.4.2",
27
+ "fast-geoip": "^1.1.53",
28
+ "file-type": "^16.5.3",
29
29
  "ini": "github:Leask/ini",
30
- "ioredis": "^4.28.1",
30
+ "ioredis": "^4.28.2",
31
31
  "jsonwebtoken": "^8.5.1",
32
32
  "mailgun-js": "^0.22.0",
33
- "mathjs": "^10.0.0",
33
+ "mathjs": "^10.0.2",
34
34
  "mysql2": "^2.3.3",
35
35
  "node-fetch": "^2.6.6",
36
36
  "node-mailjet": "^3.3.4",
37
37
  "ping": "^0.4.1",
38
38
  "public-ip": "^4.0.4",
39
- "qs": "^6.10.1",
39
+ "qs": "^6.10.2",
40
40
  "telesignsdk": "^2.2.1",
41
- "twilio": "^3.71.2",
41
+ "twilio": "^3.72.0",
42
42
  "uuid": "^8.3.2",
43
- "winston": "^3.3.3",
43
+ "winston": "^3.3.4",
44
44
  "winston-papertrail-mproved": "^1.0.7"
45
45
  }
46
46
  }