@e-mc/module 0.12.14 → 0.12.16

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 (3) hide show
  1. package/README.md +18 -11
  2. package/index.js +31 -20
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.12.14/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.12.16/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -223,11 +223,17 @@ interface ModuleConstructor {
223
223
  readBuffer(value: string | URL, cache?: boolean | ReadBufferOptions): Buffer | null;
224
224
  resolveMime(data: string | Buffer | Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;
225
225
  lookupMime(value: string, extension?: boolean): string;
226
- initCpuUsage(instance?: IModule): CpuUsage;
227
- getCpuUsage(start: CpuUsage, format: true): string;
228
- getCpuUsage(start: CpuUsage, format?: boolean): number;
226
+ /** @deprecated class */
227
+ initCpuUsage(instance?: IModule, thread?: boolean): CpuUsage;
228
+ /** @deprecated private */
229
+ getCpuUsage(start: CpuUsage, format: true, thread?: boolean): string;
230
+ /** @deprecated private */
231
+ getCpuUsage(start: CpuUsage, format?: boolean, thread?: boolean): number;
232
+ /** @deprecated private */
229
233
  getMemUsage(format: true | "%" | "b" | "gb" | "kb" | "mb" | "pb" | "tb" | "B" | "GB" | "KB" | "MB" | "PB" | "TB", free?: boolean): string;
234
+ /** @deprecated private */
230
235
  getMemUsage(format?: boolean, free?: boolean): number;
236
+ /** @deprecated private */
231
237
  formatCpuMem(start: CpuUsage, all?: boolean): string;
232
238
  getPackageVersion(name: string | [string, string], options?: PackageVersionOptions): string;
233
239
  checkSemVer(name: string | [string, string], options: CheckSemVerOptions): boolean;
@@ -255,8 +261,8 @@ interface ModuleConstructor {
255
261
  import type { BackgroundColor, ForegroundColor, LogMessageOptions, LogTypeValue, LoggerProgress, LoggerStatus } from "./logger";
256
262
  import type { LoggerProcessSettings } from "./settings";
257
263
 
258
- import type { BinaryLike, CipherGCMTypes } from "crypto";
259
- import type { SecureVersion } from "tls";
264
+ import type { BinaryLike, CipherGCMTypes } from "node:crypto";
265
+ import type { SecureVersion } from "node:tls";
260
266
 
261
267
  interface NodeModule {
262
268
  process?: {
@@ -416,13 +422,14 @@ interface LoggerModule {
416
422
 
417
423
  ## References
418
424
 
419
- - https://www.unpkg.com/@e-mc/types@0.12.14/lib/core.d.ts
420
- - https://www.unpkg.com/@e-mc/types@0.12.14/lib/logger.d.ts
421
- - https://www.unpkg.com/@e-mc/types@0.12.14/lib/module.d.ts
422
- - https://www.unpkg.com/@e-mc/types@0.12.14/lib/node.d.ts
423
- - https://www.unpkg.com/@e-mc/types@0.12.14/lib/settings.d.ts
425
+ - https://www.unpkg.com/@e-mc/types@0.12.16/lib/core.d.ts
426
+ - https://www.unpkg.com/@e-mc/types@0.12.16/lib/logger.d.ts
427
+ - https://www.unpkg.com/@e-mc/types@0.12.16/lib/module.d.ts
428
+ - https://www.unpkg.com/@e-mc/types@0.12.16/lib/node.d.ts
429
+ - https://www.unpkg.com/@e-mc/types@0.12.16/lib/settings.d.ts
424
430
 
425
431
  * https://www.npmjs.com/package/@types/node
432
+ * https://www.npmjs.com/package/file-type
426
433
 
427
434
  ## LICENSE
428
435
 
package/index.js CHANGED
@@ -18,8 +18,10 @@ const PROCESS_CWD = process.cwd();
18
18
  const PROCESS_STDOUT = process.stdout;
19
19
  const PLATFORM_WIN32 = process.platform === 'win32';
20
20
  const OS_HOMEDIR = os.homedir();
21
- const MEM_TOTAL = os.totalmem();
21
+ const MEM_TOTAL = ((0, types_1.supported)(19, 6) || (0, types_1.supported)(18, 15, true)) && process.constrainedMemory() || os.totalmem();
22
22
  const CPU_CORETOTAL = os.cpus().length;
23
+ const SUPPORTED_CPUTHREAD = (0, types_1.supported)(23, 9);
24
+ const SUPPORTED_CANPARSE = (0, types_1.supported)(19, 9) || (0, types_1.supported)(18, 17, true);
23
25
  const CACHE_READTEXT = new Map();
24
26
  const CACHE_READBUFFER = new Map();
25
27
  const CACHE_READCJS = new Map();
@@ -138,7 +140,6 @@ const MEMORY_CACHE_DISK = {
138
140
  };
139
141
  const PROCESS_CIPHER = {};
140
142
  const REGEXP_TORRENT = /^(?:magnet:\?xt=|(?:https?|s?ftp):\/\/[^/][^\n]*?\.(?:torrent|metalink|meta4)(?:\?[^\n]*)?$)/i;
141
- const REGEXP_PROTOCOL = /^([a-z][a-z\d+-.]*):\/\/[^@:[\]\\^<>|\s]/i;
142
143
  const REGEXP_ANSIESCAPE = /[\u001B\u009B]/;
143
144
  const REGEXP_PATHEND = new RegExp(`${path.sep}+$`);
144
145
  const LOG_DIVIDER = chalk.blackBright('|');
@@ -850,7 +851,7 @@ class Module extends EventEmitter {
850
851
  static LOG_STYLE_NOTICE = Object.freeze({ titleBgColor: 'bgGrey', titleColor: 'white' });
851
852
  static LOG_STYLE_REVERSE = Object.freeze({ titleBgColor: 'bgWhite', titleColor: 'black', messageBgColor: 'bgGrey' });
852
853
  static get VERSION() {
853
- return "0.12.14";
854
+ return "0.12.16";
854
855
  }
855
856
  static get LOG_TYPE() {
856
857
  return types_1.LOG_TYPE;
@@ -1258,10 +1259,14 @@ class Module extends EventEmitter {
1258
1259
  }
1259
1260
  this.formatMessage(((type || 1) | 512), "FAIL!", value, message, applyStyle(options, this.LOG_STYLE_FAIL));
1260
1261
  }
1261
- static parseFunction(value, absolute, sync = true) {
1262
- let context, requireExt, external;
1263
- if ((0, types_1.isObject)(absolute)) {
1264
- ({ context, requireExt, external, absolute, sync = true } = absolute);
1262
+ static parseFunction(value, options, sync = true) {
1263
+ let context, requireExt, absolute, external;
1264
+ if (typeof options === 'boolean') {
1265
+ absolute = options;
1266
+ options = undefined;
1267
+ }
1268
+ else if (options) {
1269
+ ({ context, requireExt, external, absolute, sync = true } = options);
1265
1270
  }
1266
1271
  if (requireExt === undefined || requireExt === true) {
1267
1272
  requireExt = VALUES["node.require.ext"];
@@ -1275,7 +1280,7 @@ class Module extends EventEmitter {
1275
1280
  else if (pathname = absolute && path.isAbsolute(location) ? location : this.fromLocalPath(location)) {
1276
1281
  if (requireExt && VALUES["node.require.ext"] && hasString(requireExt, getExtension(pathname))) {
1277
1282
  try {
1278
- result = checkFunction(require(pathname));
1283
+ result = checkFunction(options?.default ? (0, types_1.requireESM)(pathname, options.url) : require(pathname));
1279
1284
  }
1280
1285
  catch {
1281
1286
  }
@@ -1289,7 +1294,7 @@ class Module extends EventEmitter {
1289
1294
  }
1290
1295
  if (external && !pathname && VALUES["node.require.npm"] && /^(?:@[a-z\d-*~][a-z\d-*._~]*\/|node:)?[a-z\d-~][a-z\d-._~]*(?:\/.*)?$/.test(location)) {
1291
1296
  try {
1292
- result = checkFunction(require(location));
1297
+ result = checkFunction(options?.default ? (0, types_1.requireESM)(location, options.url) : require(location));
1293
1298
  }
1294
1299
  catch {
1295
1300
  }
@@ -1453,8 +1458,14 @@ class Module extends EventEmitter {
1453
1458
  return filename || '';
1454
1459
  }
1455
1460
  static isURL(value, ...exclude) {
1456
- const match = REGEXP_PROTOCOL.exec(value);
1457
- return !!match && !exclude.includes(match[1].toLowerCase());
1461
+ let match;
1462
+ if (!SUPPORTED_CANPARSE) {
1463
+ match = /^([a-z][a-z\d+-.]*):\/\/[^@:[\]\\^<>|\s]/i.exec(value);
1464
+ }
1465
+ else if (URL.canParse(value)) {
1466
+ match = /^([a-z][a-z\d+-.]*):\/\//i.exec(value);
1467
+ }
1468
+ return !!match && (exclude.length === 0 || !exclude.includes(match[1].toLowerCase()));
1458
1469
  }
1459
1470
  static isFile(value, type) {
1460
1471
  if (!type) {
@@ -1986,18 +1997,18 @@ class Module extends EventEmitter {
1986
1997
  static lookupMime(value, extension) {
1987
1998
  return (extension ? mime.extension(value) : mime.lookup(value)) || '';
1988
1999
  }
1989
- static initCpuUsage(instance) {
2000
+ static initCpuUsage(instance, thread = true) {
1990
2001
  if (!VALUES["node.process.cpu_usage"]) {
1991
2002
  return { user: 0, system: 0 };
1992
2003
  }
1993
- const result = process.cpuUsage();
2004
+ const result = thread && SUPPORTED_CPUTHREAD ? process.threadCpuUsage() : process.cpuUsage();
1994
2005
  CACHE_CPU.set(result, getCpuTimes());
1995
2006
  if (instance) {
1996
2007
  CACHE_CPUHOST.set(instance, result);
1997
2008
  }
1998
2009
  return result;
1999
2010
  }
2000
- static getCpuUsage(start, format) {
2011
+ static getCpuUsage(start, format, thread = true) {
2001
2012
  if (VALUES["node.process.cpu_usage"]) {
2002
2013
  let value = CACHE_CPU.get(start);
2003
2014
  if (value === undefined) {
@@ -2006,9 +2017,9 @@ class Module extends EventEmitter {
2006
2017
  else if ((value = getCpuTimes() - value) >= 0) {
2007
2018
  let result = 0;
2008
2019
  if (value > 0) {
2009
- const { user, system } = process.cpuUsage(start);
2020
+ const { user, system } = thread && SUPPORTED_CPUTHREAD ? process.threadCpuUsage(start) : process.cpuUsage(start);
2010
2021
  const usage = user + system;
2011
- result = usage > 0 ? Math.min(usage / value, 1) * (typeof SETTINGS.process === 'object' && SETTINGS.process.cpu_single_core ? 1 : CPU_CORETOTAL) : 0;
2022
+ result = usage > 0 ? Math.min(usage / value, 1) * (SETTINGS.process?.cpu_single_core ? 1 : CPU_CORETOTAL) : 0;
2012
2023
  }
2013
2024
  return format ? formatPercent(result, 3) : result;
2014
2025
  }
@@ -2811,7 +2822,7 @@ class Module extends EventEmitter {
2811
2822
  }
2812
2823
  if (!result) {
2813
2824
  try {
2814
- result = require(outSrc);
2825
+ result = options.default ? (0, types_1.requireESM)(outSrc, options.url) : require(outSrc);
2815
2826
  setCache(result, true);
2816
2827
  checkFunction(result);
2817
2828
  }
@@ -2984,13 +2995,13 @@ class Module extends EventEmitter {
2984
2995
  else {
2985
2996
  options.outSrc = outSrc;
2986
2997
  if (promises) {
2987
- return fs.promises.copyFile(outSrc, outDest).then(() => {
2998
+ return fs.promises.copyFile(outSrc, outDest, options.mode).then(() => {
2988
2999
  this.emit('file:copy', outDest, options);
2989
3000
  return true;
2990
3001
  });
2991
3002
  }
2992
3003
  if ((0, types_1.isFunction)(callback)) {
2993
- fs.copyFile(outSrc, outDest, err => {
3004
+ fs.copyFile(outSrc, outDest, options.mode || 0, err => {
2994
3005
  if (!err) {
2995
3006
  this.emit('file:copy', outDest, options);
2996
3007
  }
@@ -2999,7 +3010,7 @@ class Module extends EventEmitter {
2999
3010
  return;
3000
3011
  }
3001
3012
  try {
3002
- fs.copyFileSync(outSrc, outDest);
3013
+ fs.copyFileSync(outSrc, outDest, options.mode);
3003
3014
  this.emit('file:copy', outDest, options);
3004
3015
  return true;
3005
3016
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.12.14",
3
+ "version": "0.12.16",
4
4
  "description": "Module base class for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -19,11 +19,11 @@
19
19
  "license": "BSD-3-Clause",
20
20
  "homepage": "https://github.com/anpham6/e-mc#readme",
21
21
  "dependencies": {
22
- "@e-mc/types": "0.12.14",
22
+ "@e-mc/types": "0.12.16",
23
23
  "chalk": "4.1.2",
24
24
  "file-type": "^18.7.0",
25
25
  "js-yaml": "^4.1.1",
26
26
  "mime-types": "^2.1.35",
27
- "picomatch": "^4.0.3"
27
+ "picomatch": "^4.0.4"
28
28
  }
29
29
  }