utilitas 1989.9.36 → 1989.9.40

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/storage.mjs CHANGED
@@ -85,10 +85,10 @@ const assertPath = async (path, type, mode, message, status = 500, options = {})
85
85
  case '*': case '':
86
86
  break;
87
87
  case 'R':
88
- fs.promises.access(path, fs.constants.R_OK);
88
+ await fs.promises.access(path, fs.constants.R_OK);
89
89
  break;
90
90
  case 'W':
91
- fs.promises.access(path, fs.constants.R_OK | fs.constants.W_OK);
91
+ await fs.promises.access(path, fs.constants.R_OK | fs.constants.W_OK);
92
92
  break;
93
93
  default:
94
94
  modeErr = message || `Unsupported access mode: '${mode}'.`;
@@ -98,6 +98,19 @@ const assertPath = async (path, type, mode, message, status = 500, options = {})
98
98
  return stat;
99
99
  };
100
100
 
101
+ const isTextFile = async (filename, options) => {
102
+ let [fh, result] = [await fs.promises.open(filename, 'r'), true];
103
+ for (let i = 0; i < (~~options?.length || 1000); i++) {
104
+ const buf = Buffer.alloc(1);
105
+ const bytes = fs.readSync(fh.fd, buf, 0, 1, i);
106
+ if (bytes === 0) { break; } else if (
107
+ bytes === 1 && buf.toString().charCodeAt() === 0
108
+ ) { result = false; break; }
109
+ }
110
+ fh.close()
111
+ return result;
112
+ };
113
+
101
114
  const getConfigFilename = async (options) => {
102
115
  options = options || {};
103
116
  const file = options.config || path.join(os.homedir(
@@ -155,6 +168,7 @@ export {
155
168
  exists,
156
169
  getConfig,
157
170
  getConfigFilename,
171
+ isTextFile,
158
172
  legalFilename,
159
173
  readIni,
160
174
  readJson,
package/lib/tape.mjs CHANGED
@@ -3,6 +3,7 @@ import * as event from './event.mjs';
3
3
  import * as utilitas from './utilitas.mjs';
4
4
 
5
5
  const consoleMap = ['log', 'info', 'debug', 'warn', 'error'];
6
+ const trace = { trace: true };
6
7
  const [TAPE, BOT, maxLength, defBufCycle, maxBufCycle] = ['TAPE', 'BOT', 4096, 10, 100];
7
8
  const stdout = (message) => { return process.stdout.write(`${message}\n`); };
8
9
  const modLog = (c, o) => { utilitas.modLog(c, TAPE, { time: true, ...o || {} }); };
@@ -10,6 +11,7 @@ const getSendTxt = (arr) => { return arr.map(x => x[1]).join('\n'); };
10
11
  const getSndSize = (arr) => { return getSendTxt(arr).length; };
11
12
  const getBufSize = () => { return maxLength * bufferCycle; };
12
13
  const nextLen = () => { return botBuffer?.[0]?.[1].length || (maxLength + 1); };
14
+ const stringify = any => utilitas.ensureString(any, trace);
13
15
 
14
16
  let botBuffer, bufferCycle, chatIds, log, tarLevel;
15
17
 
@@ -19,7 +21,7 @@ const hookConsole = () => {
19
21
  console[bakTar] = console[tar];
20
22
  console[tar] = function() {
21
23
  console[bakTar].apply(console, arguments);
22
- const str = [...arguments].map(utilitas.ensureString).join(' ')
24
+ const str = [...arguments].map(stringify).join(' ')
23
25
  .replace(/\u001b\[\d+m/g, '')
24
26
  .split('\n').filter(x => x.length).join('\n').split('');
25
27
  while (str.length) {
package/lib/utilitas.mjs CHANGED
@@ -104,14 +104,15 @@ const ensureArray = (array) => {
104
104
  return isSet(array, true) ? (Array.isArray(array) ? array : [array]) : []
105
105
  };
106
106
 
107
- const rawEnsureString = (any) => {
107
+ const rawEnsureString = (any, options) => {
108
108
  if (isObject(any)) { return JSON.stringify(any); }
109
109
  else if (isDate(any)) { return any.toISOString(); }
110
+ else if (isError(any)) { return options?.trace ? any.stack : any.message; }
110
111
  return String(any || '');
111
112
  };
112
113
 
113
114
  const ensureString = (str, options) => {
114
- str = rawEnsureString(str);
115
+ str = rawEnsureString(str, options);
115
116
  if (options?.case) {
116
117
  switch (rawEnsureString(options?.case).trim().toUpperCase()) {
117
118
  case 'UP':
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.9.36",
4
+ "version": "1989.9.40",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
@@ -25,10 +25,10 @@
25
25
  "url": "https://github.com/Leask/utilitas.git"
26
26
  },
27
27
  "dependencies": {
28
- "@sentry/node": "^6.17.7",
28
+ "@sentry/node": "^6.17.8",
29
29
  "base64url": "^3.0.1",
30
30
  "colors": "1.4.0",
31
- "fast-geoip": "^1.1.58",
31
+ "fast-geoip": "^1.1.59",
32
32
  "file-type": "^17.1.1",
33
33
  "ini": "github:Leask/ini",
34
34
  "ioredis": "^4.28.5",