@e-mc/module 0.7.1 → 0.8.0

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/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Copyright 2023 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2023 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { ModuleConstructor } from '../types/lib';
2
-
3
- declare const Module: ModuleConstructor;
4
-
1
+ import type { ModuleConstructor } from '../types/lib';
2
+
3
+ declare const Module: ModuleConstructor;
4
+
5
5
  export = Module;
package/index.js CHANGED
@@ -74,6 +74,7 @@ const SETTINGS = {
74
74
  session_id: 0,
75
75
  message: true,
76
76
  stack_trace: false,
77
+ abort: true,
77
78
  unknown: true,
78
79
  system: true,
79
80
  node: true,
@@ -321,8 +322,38 @@ function isFailed(options) {
321
322
  }
322
323
  return false;
323
324
  }
325
+ function getErrorCause(err, out, visited) {
326
+ let message;
327
+ if (visited) {
328
+ message = SETTINGS.stack_trace && err.stack || err.message;
329
+ }
330
+ else {
331
+ visited = new WeakSet();
332
+ }
333
+ visited.add(err);
334
+ if (err.hasOwnProperty('cause')) { // eslint-disable-line no-prototype-builtins
335
+ const cause = err.cause;
336
+ if (cause instanceof Error) {
337
+ if (message) {
338
+ out.push(message);
339
+ }
340
+ if (!visited.has(cause)) {
341
+ getErrorCause(cause, out, visited);
342
+ }
343
+ return;
344
+ }
345
+ if ((0, types_1.isString)(cause)) {
346
+ message = message ? message + ` (${cause})` : cause; // eslint-disable-line @typescript-eslint/restrict-template-expressions
347
+ }
348
+ }
349
+ if (message) {
350
+ out.push(message);
351
+ }
352
+ }
324
353
  function getErrorMessage(err) {
325
- return SETTINGS.stack_trace && err.stack || err.message || err.toString() || "Unknown" /* ERR_MESSAGE.UNKNOWN */;
354
+ const cause = [];
355
+ getErrorCause(err, cause);
356
+ return (SETTINGS.stack_trace && err.stack || err.message || err.toString() || "Unknown" /* ERR_MESSAGE.UNKNOWN */) + (cause.length ? cause.map((value, index) => `\n\nCause #${cause.length - index}: ` + value).join('') : '');
326
357
  }
327
358
  function writeLine(value) {
328
359
  PROCESS_STDOUT.write((!LOG_NEWLINE ? '\n' : '') + value + '\n');
@@ -525,14 +556,15 @@ function tryIncrementDir(value, increment) {
525
556
  } while (increment-- > 0);
526
557
  return [outErr, -1];
527
558
  }
559
+ const hideAbort = (err) => err.name === 'AbortError' && SETTINGS.abort === false;
528
560
  const asFile = (value) => value instanceof URL ? value.protocol === 'file:' ? url.fileURLToPath(value) : '' : value;
529
561
  const wrapQuote = (value) => '"' + value.replace(/"/g, '\\"') + '"';
530
562
  const isFunction = (value) => typeof value === 'function';
531
563
  const isFileURL = (value) => /^file:\/\//i.test(value);
532
564
  const errorDirectory = (value) => (0, types_1.errorValue)("Path is not a directory" /* ERR_MESSAGE.NOT_DIRECTORY */, value);
533
565
  const sanitizePath = (value) => (0, types_1.isString)(value) ? path.resolve(value.trim()) : '';
534
- const ensureDir = (value) => value[value.length - 1] !== path.sep ? value + path.sep : value;
535
- const trimDir = (value) => value[value.length - 1] === path.sep ? value.substring(0, value.length - 1) : value;
566
+ const ensureDir = (value) => value.endsWith(path.sep) ? value : value + path.sep;
567
+ const trimDir = (value) => value.endsWith(path.sep) ? value.substring(0, value.length - 1) : value;
536
568
  const getExtension = (value) => path.extname(value).toLowerCase().substring(1);
537
569
  const hasString = (item, value) => (0, types_1.isString)(value) && (item === value || Array.isArray(item) && item.includes(value));
538
570
  const getCpuTimes = () => os.cpus().reduce((a, b) => a + b.times.user + b.times.sys, 0) * 1000 /* TIME.S */;
@@ -558,7 +590,7 @@ class Module extends EventEmitter {
558
590
  this[_f] = new AbortController();
559
591
  this[_g] = null;
560
592
  }
561
- static get VERSION() { return "0.7.1" /* INTERNAL.VERSION */; }
593
+ static get VERSION() { return "0.8.0" /* INTERNAL.VERSION */; }
562
594
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
563
595
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
564
596
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -637,6 +669,10 @@ class Module extends EventEmitter {
637
669
  return true;
638
670
  }
639
671
  static formatMessage(type, title, value, message, options = {}) {
672
+ const error = !!message && message instanceof Error;
673
+ if (error && hideAbort(message)) {
674
+ return;
675
+ }
640
676
  if (options.type) {
641
677
  type |= options.type;
642
678
  }
@@ -675,14 +711,13 @@ class Module extends EventEmitter {
675
711
  const formatValue = format.value;
676
712
  const id = sessionId && SETTINGS.session_id ? ' ' + sessionId.padStart(SETTINGS.session_id, '0') + ' ' : '';
677
713
  const titleIndent = options.titleIndent ? typeof options.titleIndent === 'number' ? Math.max(options.titleIndent, 0) : 0 : -1;
678
- let output, error, hint, valueWidth = Math.max(formatValue.width - (id ? SETTINGS.session_id + 1 : 0), 1), titleJustify = options.titleJustify || ((type & 512 /* LOG_VALUE.FAIL */) || options.failed ? 'center' : formatTitle.justify);
714
+ let output, hint, valueWidth = Math.max(formatValue.width - (id ? SETTINGS.session_id + 1 : 0), 1), titleJustify = options.titleJustify || ((type & 512 /* LOG_VALUE.FAIL */) || options.failed ? 'center' : formatTitle.justify);
679
715
  if (Array.isArray(value)) {
680
716
  hint = value[1] ?? '';
681
717
  value = value[0];
682
718
  }
683
- if (message && message instanceof Error) {
719
+ if (error) {
684
720
  message = getErrorMessage(message);
685
- error = true;
686
721
  }
687
722
  if (messageUnitIndent) {
688
723
  let indentChar;
@@ -724,7 +759,7 @@ class Module extends EventEmitter {
724
759
  indent = match[1];
725
760
  if (bgAltColor) {
726
761
  try {
727
- indent = (bgAltColor[0] === '#' ? chalk.bgHex(bgAltColor) : chalk[bgAltColor])(indent);
762
+ indent = (bgAltColor.startsWith('#') ? chalk.bgHex(bgAltColor) : chalk[bgAltColor])(indent);
728
763
  }
729
764
  catch {
730
765
  }
@@ -747,10 +782,10 @@ class Module extends EventEmitter {
747
782
  try {
748
783
  let current = bold ? chalk.bold : chalk;
749
784
  if (typeof color === 'string' && color.length > 1) {
750
- current = color[0] === '#' ? current.hex(color) : current[color];
785
+ current = color.startsWith('#') ? current.hex(color) : current[color];
751
786
  }
752
787
  if (typeof bgColor === 'string' && bgColor.length > 1) {
753
- current = bgColor[0] === '#' ? current.bgHex(bgColor) : current[bgColor];
788
+ current = bgColor.startsWith('#') ? current.bgHex(bgColor) : current[bgColor];
754
789
  }
755
790
  return indent + current(content);
756
791
  }
@@ -1291,7 +1326,7 @@ class Module extends EventEmitter {
1291
1326
  let result = paths[0] || '';
1292
1327
  for (let i = 1; i < paths.length; ++i) {
1293
1328
  const trailing = paths[i];
1294
- result += (trailing[0] !== '/' && result[result.length - 1] !== '/' ? '/' : '') + trailing;
1329
+ result += (!trailing.startsWith('/') && !result.endsWith('/') ? '/' : '') + trailing;
1295
1330
  }
1296
1331
  return result;
1297
1332
  }
@@ -1383,7 +1418,7 @@ class Module extends EventEmitter {
1383
1418
  }
1384
1419
  return false;
1385
1420
  }
1386
- static copyDir(src, dest, move, recursive = true) {
1421
+ static async copyDir(src, dest, move, recursive = true) {
1387
1422
  const srcOut = sanitizePath(asFile(src));
1388
1423
  if (!srcOut || !this.isDir(srcOut)) {
1389
1424
  return Promise.reject(errorDirectory(asFile(src) || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
@@ -1583,7 +1618,7 @@ class Module extends EventEmitter {
1583
1618
  }
1584
1619
  return null;
1585
1620
  }
1586
- static resolveMime(data) {
1621
+ static async resolveMime(data) {
1587
1622
  return typeof data === 'string' ? filetype.fromFile(data) : filetype.fromBuffer(data);
1588
1623
  }
1589
1624
  static lookupMime(value, extension) {
@@ -1869,7 +1904,7 @@ class Module extends EventEmitter {
1869
1904
  return true;
1870
1905
  }
1871
1906
  static sanitizeCmd(value) {
1872
- if (value.indexOf(' ') !== -1) {
1907
+ if (value.includes(' ')) {
1873
1908
  return PLATFORM_WIN32 ? wrapQuote(value) : value.replace(/[ ]/g, '\\ ');
1874
1909
  }
1875
1910
  return value;
@@ -1900,7 +1935,7 @@ class Module extends EventEmitter {
1900
1935
  }
1901
1936
  value = quoted[2];
1902
1937
  }
1903
- if (value.indexOf(' ') !== -1) {
1938
+ if (value.includes(' ')) {
1904
1939
  value = wrapQuote(value);
1905
1940
  }
1906
1941
  }
@@ -1935,7 +1970,7 @@ class Module extends EventEmitter {
1935
1970
  }
1936
1971
  return (typeof values === 'string' ? result[0] : result);
1937
1972
  }
1938
- static purgeMemory(percent = 1, limit = 0) {
1973
+ static async purgeMemory(percent = 1, limit = 0) {
1939
1974
  if (typeof limit === 'boolean') {
1940
1975
  limit = 0;
1941
1976
  }
@@ -1968,7 +2003,7 @@ class Module extends EventEmitter {
1968
2003
  CACHE_TOTAL = stored.length - result;
1969
2004
  }
1970
2005
  }
1971
- return Promise.resolve(result);
2006
+ return result;
1972
2007
  }
1973
2008
  static canWrite(name) {
1974
2009
  switch (name) {
@@ -2176,11 +2211,13 @@ class Module extends EventEmitter {
2176
2211
  }
2177
2212
  break;
2178
2213
  }
2179
- default:
2180
- if (attr in SETTINGS) {
2181
- SETTINGS[attr] = logger[attr];
2214
+ default: {
2215
+ const value = logger[attr];
2216
+ if (attr in SETTINGS && value !== undefined) {
2217
+ SETTINGS[attr] = value;
2182
2218
  }
2183
2219
  break;
2220
+ }
2184
2221
  }
2185
2222
  }
2186
2223
  if (broadcast?.out) {
@@ -2311,7 +2348,7 @@ class Module extends EventEmitter {
2311
2348
  result = output;
2312
2349
  }
2313
2350
  if (filename) {
2314
- const trailing = (filename[0] === '.' ? (0, types_1.generateUUID)() : '') + filename;
2351
+ const trailing = (filename.startsWith('.') ? (0, types_1.generateUUID)() : '') + filename;
2315
2352
  if (result) {
2316
2353
  return path.join(result, trailing);
2317
2354
  }
@@ -2783,7 +2820,7 @@ class Module extends EventEmitter {
2783
2820
  }
2784
2821
  return false;
2785
2822
  }
2786
- allSettled(tasks, rejected, options) {
2823
+ async allSettled(tasks, rejected, options) {
2787
2824
  rejected || (rejected = "Unknown" /* ERR_MESSAGE.UNKNOWN */);
2788
2825
  return Promise.allSettled(tasks).then(result => {
2789
2826
  const items = [];
@@ -3004,7 +3041,7 @@ class Module extends EventEmitter {
3004
3041
  }
3005
3042
  }
3006
3043
  if (message) {
3007
- if (this._logEnabled) {
3044
+ if (this._logEnabled && !(message instanceof Error && hideAbort(message))) {
3008
3045
  if (fatal) {
3009
3046
  const timeStamp = Date.now();
3010
3047
  this.addLog(types_1.STATUS_TYPE.FATAL, message, timeStamp, options.startTime && getTimeOffset(options.startTime, timeStamp));
@@ -3221,9 +3258,9 @@ class Module extends EventEmitter {
3221
3258
  }
3222
3259
  this._host = null;
3223
3260
  }
3224
- abort() {
3261
+ abort(reason) {
3225
3262
  if (!this.aborted) {
3226
- this[kAbortHandler].abort();
3263
+ this[kAbortHandler].abort(reason);
3227
3264
  }
3228
3265
  }
3229
3266
  willAbort(value) {
@@ -3250,14 +3287,14 @@ class Module extends EventEmitter {
3250
3287
  const aborting = this._hostEvents.includes('abort');
3251
3288
  if (value.aborted) {
3252
3289
  if (aborting) {
3253
- this.abort();
3290
+ this.abort(new Error("Aborted by host" /* ERR_MESSAGE.ABORTED_HOST */));
3254
3291
  }
3255
3292
  }
3256
3293
  else if (!value.subProcesses.has(this)) {
3257
3294
  this.abortable = value.willAbort(this);
3258
3295
  value.modules.add(this);
3259
3296
  if (aborting) {
3260
- value.signal.addEventListener('abort', this[kAbortEvent] = () => this.abort(), { once: true });
3297
+ value.signal.addEventListener('abort', this[kAbortEvent] = () => this.abort(new Error("Aborted by host" /* ERR_MESSAGE.ABORTED_HOST */)), { once: true });
3261
3298
  }
3262
3299
  }
3263
3300
  }
package/lib-v4.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { IModuleLibV4 } from '../types/lib/compat-v4';
2
-
3
- declare const LibV4: IModuleLibV4;
4
-
1
+ import type { IModuleLibV4 } from '../types/lib/compat-v4';
2
+
3
+ declare const LibV4: IModuleLibV4;
4
+
5
5
  export = LibV4;
package/lib-v4.js CHANGED
@@ -135,7 +135,7 @@ function getFunctions(values, absolute, sync = true, outFailed) {
135
135
  return result;
136
136
  }
137
137
  exports.getFunctions = getFunctions;
138
- function allSettled(tasks, rejected, options) {
138
+ async function allSettled(tasks, rejected, options) {
139
139
  if (rejected) {
140
140
  return Promise.allSettled(tasks).then(result => {
141
141
  const items = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "Module base class for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/types": "0.7.1",
23
+ "@e-mc/types": "0.8.0",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",