@e-mc/module 0.5.2 → 0.6.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.
Files changed (2) hide show
  1. package/index.js +61 -59
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -558,7 +558,7 @@ class Module extends EventEmitter {
558
558
  this[_f] = new AbortController();
559
559
  this[_g] = null;
560
560
  }
561
- static get VERSION() { return "0.5.2" /* INTERNAL.VERSION */; }
561
+ static get VERSION() { return "0.6.0" /* INTERNAL.VERSION */; }
562
562
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
563
563
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
564
564
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -1068,7 +1068,10 @@ class Module extends EventEmitter {
1068
1068
  return cacheKey ? (0, types_1.generateUUID)() : '';
1069
1069
  }
1070
1070
  static asHash(data, algorithm, minLength = 0) {
1071
- let options;
1071
+ if (!algorithm && !minLength) {
1072
+ return crypto.createHash('sha256').update(data).digest('hex');
1073
+ }
1074
+ let options, digest;
1072
1075
  if (typeof minLength !== 'number') {
1073
1076
  options = minLength;
1074
1077
  minLength = 0;
@@ -1081,7 +1084,6 @@ class Module extends EventEmitter {
1081
1084
  options = algorithm;
1082
1085
  algorithm = '';
1083
1086
  }
1084
- let digest;
1085
1087
  if (options) {
1086
1088
  options = { ...options };
1087
1089
  if ('minLength' in options) {
@@ -1229,7 +1231,7 @@ class Module extends EventEmitter {
1229
1231
  return '';
1230
1232
  }
1231
1233
  if (/^\\\\\?\\[A-Za-z]:\\/.test(value)) {
1232
- return process.platform === 'win32' && !/\\\\|\\\.+\\|\\[^\n]+?\.+\\/.test(value = value.substring(4)) ? value : '';
1234
+ return PLATFORM_WIN32 && !/\\\\|\\\.+\\|\\[^\n]+?\.+\\/.test(value = value.substring(4)) ? value : '';
1233
1235
  }
1234
1236
  return this.fromLocalPath(value) || value;
1235
1237
  }
@@ -1620,34 +1622,32 @@ class Module extends EventEmitter {
1620
1622
  let result = '';
1621
1623
  if (cpu || all) {
1622
1624
  let usage = this.getCpuUsage(start, !cpu_bar);
1623
- if (usage) {
1624
- if (typeof usage === 'number') {
1625
- if (!cpu_single_core) {
1626
- usage /= CPU_CORETOTAL;
1627
- }
1628
- if (usage + 0.005 /* CPU_MEM.BAR_100_OFFSET */ >= 1) {
1629
- usage = 1;
1630
- }
1631
- let bar = 20 /* CPU_MEM.BAR_LENGTH */, red = 0.9 /* CPU_MEM.BAR_RED */, yellow = 0.75 /* CPU_MEM.BAR_YELLOW */;
1632
- if (typeof cpu_bar === 'number') {
1633
- bar = cpu_bar;
1634
- }
1635
- else if (Array.isArray(cpu_bar) && cpu_bar[0] >= 10 /* CPU_MEM.BAR_MIN_LENGTH */) {
1636
- let r, y;
1637
- [bar, r = 0.9 /* CPU_MEM.BAR_RED */, y = 0.75 /* CPU_MEM.BAR_YELLOW */] = cpu_bar;
1638
- if (r > 0 && r < 1 && y > 0 && y < 1 && y < r) {
1639
- red = r;
1640
- yellow = y;
1641
- }
1642
- }
1643
- const unit = Math.max(bar, 10 /* CPU_MEM.BAR_MIN_LENGTH */);
1644
- const length = Math.floor(usage * unit);
1645
- const hint = usage === 1 ? "MAX" /* CPU_MEM.BAR_100 */ : formatPercent(usage, 2);
1646
- result += chalk[usage >= red ? 'bgRed' : usage >= yellow ? 'bgYellow' : 'bgBlue'].white(':'.repeat(length)) + chalk.bgGrey.white(':'.repeat(unit - length)) + (hint ? chalk.bgWhite.grey(` ${hint} `) : '');
1625
+ if (typeof usage === 'number') {
1626
+ if (!cpu_single_core) {
1627
+ usage /= CPU_CORETOTAL;
1647
1628
  }
1648
- else {
1649
- result += usage + ' CPU' + (!cpu_single_core ? '/' + CPU_CORETOTAL : '');
1629
+ if (usage + 0.005 /* CPU_MEM.BAR_100_OFFSET */ >= 1) {
1630
+ usage = 1;
1631
+ }
1632
+ let bar = 20 /* CPU_MEM.BAR_LENGTH */, red = 0.9 /* CPU_MEM.BAR_RED */, yellow = 0.75 /* CPU_MEM.BAR_YELLOW */;
1633
+ if (typeof cpu_bar === 'number') {
1634
+ bar = cpu_bar;
1650
1635
  }
1636
+ else if (Array.isArray(cpu_bar) && cpu_bar[0] >= 10 /* CPU_MEM.BAR_MIN_LENGTH */) {
1637
+ let r, y;
1638
+ [bar, r = 0.9 /* CPU_MEM.BAR_RED */, y = 0.75 /* CPU_MEM.BAR_YELLOW */] = cpu_bar;
1639
+ if (r > 0 && r < 1 && y > 0 && y < 1 && y < r) {
1640
+ red = r;
1641
+ yellow = y;
1642
+ }
1643
+ }
1644
+ const unit = Math.max(bar, 10 /* CPU_MEM.BAR_MIN_LENGTH */);
1645
+ const length = Math.floor(usage * unit);
1646
+ const hint = usage === 1 ? "MAX" /* CPU_MEM.BAR_100 */ : usage < 0 ? "N/A" /* CPU_MEM.BAR_UNDER */ : formatPercent(usage, 2);
1647
+ result += chalk[usage >= red ? 'bgRed' : usage >= yellow ? 'bgYellow' : 'bgBlue'].white(':'.repeat(length)) + chalk.bgGrey.white(':'.repeat(unit - length)) + (hint ? chalk.bgWhite.grey(` ${hint} `) : '');
1648
+ }
1649
+ else if (usage) {
1650
+ result += usage + ' CPU' + (!cpu_single_core ? '/' + CPU_CORETOTAL : '');
1651
1651
  }
1652
1652
  }
1653
1653
  if (mem || all) {
@@ -3177,17 +3177,18 @@ class Module extends EventEmitter {
3177
3177
  }
3178
3178
  detach() {
3179
3179
  const host = this.host;
3180
- if (host) {
3181
- this.abortable = false;
3182
- host.modules.delete(this);
3183
- host.subProcesses.delete(this);
3184
- const listener = this[kAbortEvent];
3185
- if (listener) {
3186
- host.signal.removeEventListener('abort', listener);
3187
- this[kAbortEvent] = null;
3188
- }
3189
- this._host = null;
3180
+ if (!host) {
3181
+ return;
3182
+ }
3183
+ this.abortable = false;
3184
+ host.modules.delete(this);
3185
+ host.subProcesses.delete(this);
3186
+ const listener = this[kAbortEvent];
3187
+ if (listener) {
3188
+ host.signal.removeEventListener('abort', listener);
3189
+ this[kAbortEvent] = null;
3190
3190
  }
3191
+ this._host = null;
3191
3192
  }
3192
3193
  abort() {
3193
3194
  if (!this.aborted) {
@@ -3209,22 +3210,23 @@ class Module extends EventEmitter {
3209
3210
  }
3210
3211
  set host(value) {
3211
3212
  this.detach();
3212
- if (this._host = value) {
3213
- this.sessionId = value.sessionId;
3214
- this.broadcastId = value.broadcastId;
3215
- this._logEnabled = value.willLog(this.moduleName);
3216
- const aborting = this._hostEvents.includes('abort');
3217
- if (value.aborted) {
3218
- if (aborting) {
3219
- this.abort();
3220
- }
3213
+ if (!(this._host = value)) {
3214
+ return;
3215
+ }
3216
+ this.sessionId = value.sessionId;
3217
+ this.broadcastId = value.broadcastId;
3218
+ this._logEnabled = value.willLog(this.moduleName);
3219
+ const aborting = this._hostEvents.includes('abort');
3220
+ if (value.aborted) {
3221
+ if (aborting) {
3222
+ this.abort();
3221
3223
  }
3222
- else if (!value.subProcesses.has(this)) {
3223
- this.abortable = value.willAbort(this);
3224
- value.modules.add(this);
3225
- if (aborting) {
3226
- value.signal.addEventListener('abort', this[kAbortEvent] = () => this.abort(), { once: true });
3227
- }
3224
+ }
3225
+ else if (!value.subProcesses.has(this)) {
3226
+ this.abortable = value.willAbort(this);
3227
+ value.modules.add(this);
3228
+ if (aborting) {
3229
+ value.signal.addEventListener('abort', this[kAbortEvent] = () => this.abort(), { once: true });
3228
3230
  }
3229
3231
  }
3230
3232
  }
@@ -3278,11 +3280,11 @@ class Module extends EventEmitter {
3278
3280
  }
3279
3281
  get logLevel() {
3280
3282
  const value = this._logLevel;
3281
- if (value === -1) {
3282
- const host = this.host;
3283
- return host ? host.logLevel : 0;
3283
+ if (value !== -1) {
3284
+ return value;
3284
3285
  }
3285
- return value;
3286
+ const host = this.host;
3287
+ return host ? host.logLevel : 0;
3286
3288
  }
3287
3289
  get statusType() {
3288
3290
  return types_1.STATUS_TYPE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.5.2",
3
+ "version": "0.6.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.5.2",
23
+ "@e-mc/types": "0.6.0",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",