@e-mc/module 0.11.2 → 0.11.4

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 (4) hide show
  1. package/README.md +11 -7
  2. package/index.js +14 -29
  3. package/lib-v4.js +158 -0
  4. package/package.json +2 -2
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.11.2/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.11.4/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -225,13 +225,17 @@ interface ModuleConstructor {
225
225
  getMemUsage(format: true): string;
226
226
  getMemUsage(format?: boolean): number;
227
227
  formatCpuMem(start: CpuUsage, all?: boolean): string;
228
+ /** @deprecated */
228
229
  getPackageVersion(name: string | [string, string], startDir: string, baseDir?: string): string;
230
+ /** @deprecated */
229
231
  getPackageVersion(name: string | [string, string], unstable?: boolean, startDir?: string, baseDir?: string): string;
230
232
  checkSemVer(name: string | [string, string], options: CheckSemVerOptions): boolean;
231
- checkSemVer(name: string | [string, string], min: number | string, max?: number | string, unstable?: boolean, startDir?: string): boolean;
232
233
  checkSemVer(name: string | [string, string], min: number | string, max: number | string, options?: Omit<CheckSemVerOptions, "min" | "max" | "equals">): boolean;
234
+ /** @deprecated */
233
235
  sanitizeCmd(value: string): string;
236
+ /** @deprecated */
234
237
  sanitizeArgs(value: string, doubleQuote?: boolean): string;
238
+ /** @deprecated */
235
239
  sanitizeArgs(values: string[], doubleQuote?: boolean): string[];
236
240
  purgeMemory(percent: number, parent: boolean): Promise<number>;
237
241
  purgeMemory(percent: number, limit: number, parent?: boolean): Promise<number>;
@@ -410,11 +414,11 @@ interface LoggerModule {
410
414
 
411
415
  ## References
412
416
 
413
- - https://www.unpkg.com/@e-mc/types@0.11.2/lib/core.d.ts
414
- - https://www.unpkg.com/@e-mc/types@0.11.2/lib/logger.d.ts
415
- - https://www.unpkg.com/@e-mc/types@0.11.2/lib/module.d.ts
416
- - https://www.unpkg.com/@e-mc/types@0.11.2/lib/node.d.ts
417
- - https://www.unpkg.com/@e-mc/types@0.11.2/lib/settings.d.ts
417
+ - https://www.unpkg.com/@e-mc/types@0.11.4/lib/core.d.ts
418
+ - https://www.unpkg.com/@e-mc/types@0.11.4/lib/logger.d.ts
419
+ - https://www.unpkg.com/@e-mc/types@0.11.4/lib/module.d.ts
420
+ - https://www.unpkg.com/@e-mc/types@0.11.4/lib/node.d.ts
421
+ - https://www.unpkg.com/@e-mc/types@0.11.4/lib/settings.d.ts
418
422
 
419
423
  * https://www.npmjs.com/package/@types/node
420
424
 
package/index.js CHANGED
@@ -144,7 +144,7 @@ const SUPPORTED_CANPARSE = (0, types_1.supported)(19, 9) || (0, types_1.supporte
144
144
  const REGEXP_TORRENT = /^(?:magnet:\?xt=|(?:https?|s?ftp):\/\/[^/][^\n]*?\.(?:torrent|metalink|meta4)(?:\?[^\n]*)?$)/i;
145
145
  const REGEXP_PROTOCOL = /^([a-z][a-z\d+-.]*):\/\/[^@:[\]\\^<>|\s]/i;
146
146
  const REGEXP_CLIESCAPE = /[^\w+-.,/:@]/g;
147
- const REGEXP_CLIOPTION = /^(-[^\s=]+|--[^-\s=][^\s=]*)(=)?\s*(.*)$/;
147
+ const REGEXP_CLIOPTION = /^(-[^\s=]+|--[^-\s=][^\s=]*)(=)?\s*(.*)$/s;
148
148
  const REGEXP_ANSIESCAPE = /[\u001B\u009B]/;
149
149
  const REGEXP_PATHEND = new RegExp(`${path.sep}+$`);
150
150
  const LOG_CPUBARCOLOR = SETTINGS.process.cpu_bar_color;
@@ -941,11 +941,10 @@ function isPathUNC(value) {
941
941
  value = value.toString();
942
942
  return (VALUES["node.posix.strict"] && value.includes('/') ? /^\/\/[\w.-]+\/[\w-]+\$?\/[^\n]*?[^/]$/ : /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?[\\/][^\n]*?[^\\/]$/).test(value);
943
943
  }
944
- const escapeArg = (value) => value.replace(REGEXP_CLIESCAPE, capture => capture === '\n' ? "'\n'" : (capture === '$' || capture === '`' ? '\\' : '') + '\\' + capture);
945
944
  const isMeterUnit = (type, ident) => (type & 256) === 256 && !ident;
946
945
  const formatLogMessage = (type, message, unit, ident) => isMeterUnit(type, ident) ? (unit + LOG_DIVIDER + message).trimEnd() : unit ? (message + (ident ? '' : ' ') + unit).trimStart() : message;
947
946
  const hideAbort = (err) => err.name === 'AbortError' && SETTINGS.abort === false;
948
- const asFile = (value) => typeof value === 'string' ? value.trim() : value instanceof URL && value.protocol === 'file:' ? (0, node_url_1.fileURLToPath)(value) : '';
947
+ const asFile = (value) => typeof value === 'string' ? PLATFORM_WIN32 ? value.trim() : value : value instanceof URL && value.protocol === 'file:' ? (0, node_url_1.fileURLToPath)(value) : '';
949
948
  const wrapQuote = (value) => '"' + value.replaceAll('"', '\\"') + '"';
950
949
  const isFunction = (value) => typeof value === 'function';
951
950
  const isFileURL = (value) => /^file:\/\//i.test(value);
@@ -985,7 +984,7 @@ class Module extends EventEmitter {
985
984
  this[_g] = null;
986
985
  }
987
986
  static get VERSION() {
988
- return "0.11.2";
987
+ return "0.11.4";
989
988
  }
990
989
  static get LOG_TYPE() {
991
990
  return types_1.LOG_TYPE;
@@ -1075,7 +1074,7 @@ class Module extends EventEmitter {
1075
1074
  if (error && hideAbort(message)) {
1076
1075
  return;
1077
1076
  }
1078
- if (options.type) {
1077
+ if (typeof options.type === 'number') {
1079
1078
  type |= options.type;
1080
1079
  }
1081
1080
  const BROADCAST_OUT = VALUES["broadcast.out"];
@@ -1560,7 +1559,7 @@ class Module extends EventEmitter {
1560
1559
  filename = undefined;
1561
1560
  }
1562
1561
  if (typeof value === 'string') {
1563
- let pathname = value.trim(), convert = true;
1562
+ let pathname = PLATFORM_WIN32 ? value.trim() : value, convert = true;
1564
1563
  if (normalize) {
1565
1564
  pathname = path.normalize(pathname);
1566
1565
  convert = PLATFORM_WIN32;
@@ -1676,7 +1675,7 @@ class Module extends EventEmitter {
1676
1675
  return false;
1677
1676
  }
1678
1677
  static resolveFile(value) {
1679
- if (isFileURL(value = value instanceof URL ? value.toString() : value.trim())) {
1678
+ if (isFileURL(value = value instanceof URL ? value.toString() : value)) {
1680
1679
  try {
1681
1680
  return (0, node_url_1.fileURLToPath)(value);
1682
1681
  }
@@ -1691,7 +1690,7 @@ class Module extends EventEmitter {
1691
1690
  }
1692
1691
  static resolvePath(value, base) {
1693
1692
  try {
1694
- if (this.isURL(value = value.trim())) {
1693
+ if (this.isURL(value)) {
1695
1694
  return isFileURL(value) ? (0, node_url_1.fileURLToPath)(value) : new URL(value).href;
1696
1695
  }
1697
1696
  if (base instanceof URL || this.isURL(base)) {
@@ -1723,7 +1722,7 @@ class Module extends EventEmitter {
1723
1722
  flags = flags ? 2 : 0;
1724
1723
  }
1725
1724
  if (typeof value === 'string') {
1726
- let result = value.trim();
1725
+ let result = PLATFORM_WIN32 ? value.trim() : value;
1727
1726
  result = flags & 1 ? path.resolve(result) : path.normalize(result);
1728
1727
  if ((flags & 2) && result.length > 0) {
1729
1728
  result = ensureDir(result);
@@ -2382,7 +2381,7 @@ class Module extends EventEmitter {
2382
2381
  value = opt[3];
2383
2382
  }
2384
2383
  if (value) {
2385
- const quoted = /^(["'])(.*)\1$/.exec(value);
2384
+ const quoted = /^(["'])(.*)\1$/s.exec(value);
2386
2385
  if (PLATFORM_WIN32) {
2387
2386
  if (quoted) {
2388
2387
  if (quoted[1] === '"') {
@@ -2395,23 +2394,9 @@ class Module extends EventEmitter {
2395
2394
  value = wrapQuote(value);
2396
2395
  }
2397
2396
  }
2398
- else {
2399
- let type = doubleQuote;
2400
- if (quoted) {
2401
- if (quoted[1] === "'") {
2402
- result[i] = leading + value;
2403
- continue;
2404
- }
2405
- value = quoted[2];
2406
- type = true;
2407
- }
2397
+ else if (!quoted) {
2408
2398
  if (REGEXP_CLIESCAPE.test(value)) {
2409
- if (type) {
2410
- value = `"${escapeArg(value)}"`;
2411
- }
2412
- else {
2413
- value = `'${value.replaceAll("'", "'\\''")}'`;
2414
- }
2399
+ value = doubleQuote ? `"${value.replace(/(?<!\\)"/g, '\\"')}"` : `'${value.replaceAll("'", "'\\''")}'`;
2415
2400
  }
2416
2401
  else if (RESERVED_SHELL.includes(value)) {
2417
2402
  value = `'${value}'`;
@@ -2421,7 +2406,7 @@ class Module extends EventEmitter {
2421
2406
  else if (opt) {
2422
2407
  leading = leading.trim();
2423
2408
  if (!PLATFORM_WIN32 && leading.length > 2 && leading[1] !== '-') {
2424
- leading = escapeArg(leading);
2409
+ leading = leading.replace(REGEXP_CLIESCAPE, capture => '\\' + capture);
2425
2410
  }
2426
2411
  }
2427
2412
  }
@@ -3718,7 +3703,7 @@ class Module extends EventEmitter {
3718
3703
  if (typeof value === 'boolean') {
3719
3704
  queue = value;
3720
3705
  }
3721
- ({ type = types_1.STATUS_TYPE.UNKNOWN, value, timeStamp, duration } = type);
3706
+ value = type.value;
3722
3707
  }
3723
3708
  const output = Module.asString(value);
3724
3709
  if (output) {
@@ -3746,7 +3731,7 @@ class Module extends EventEmitter {
3746
3731
  from = timeStamp;
3747
3732
  timeStamp = 0;
3748
3733
  }
3749
- else if ((0, types_1.isObject)(timeStamp)) {
3734
+ else if ((0, types_1.isPlainObject)(timeStamp)) {
3750
3735
  ({ timeStamp, duration, from, source } = timeStamp);
3751
3736
  }
3752
3737
  const name = types_1.STATUS_TYPE[type] || types_1.STATUS_TYPE[type = 0];
package/lib-v4.js ADDED
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ exports.validateUUID = exports.renameExt = exports.isString = exports.isPlainObject = exports.isObject = exports.formatSize = exports.escapePattern = exports.toTimeMs = exports.coerceObject = exports.cloneObject = exports.asFunction = void 0;
3
+ exports.hasSize = hasSize;
4
+ exports.getSize = getSize;
5
+ exports.hasSameStat = hasSameStat;
6
+ exports.byteLength = byteLength;
7
+ exports.cleanupStream = cleanupStream;
8
+ exports.generateUUID = generateUUID;
9
+ exports.existsSafe = existsSafe;
10
+ exports.isFileHTTP = isFileHTTP;
11
+ exports.isFileUNC = isFileUNC;
12
+ exports.isPathUNC = isPathUNC;
13
+ exports.readFileSafe = readFileSafe;
14
+ exports.getFunctions = getFunctions;
15
+ exports.allSettled = allSettled;
16
+ const path = require("path");
17
+ const fs = require("fs");
18
+ const types_1 = require("@e-mc/types");
19
+ Object.defineProperty(exports, "asFunction", { enumerable: true, get: function () { return types_1.asFunction; } });
20
+ Object.defineProperty(exports, "cloneObject", { enumerable: true, get: function () { return types_1.cloneObject; } });
21
+ Object.defineProperty(exports, "coerceObject", { enumerable: true, get: function () { return types_1.coerceObject; } });
22
+ Object.defineProperty(exports, "toTimeMs", { enumerable: true, get: function () { return types_1.convertTime; } });
23
+ Object.defineProperty(exports, "escapePattern", { enumerable: true, get: function () { return types_1.escapePattern; } });
24
+ Object.defineProperty(exports, "formatSize", { enumerable: true, get: function () { return types_1.formatSize; } });
25
+ Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return types_1.isObject; } });
26
+ Object.defineProperty(exports, "isPlainObject", { enumerable: true, get: function () { return types_1.isPlainObject; } });
27
+ Object.defineProperty(exports, "isString", { enumerable: true, get: function () { return types_1.isString; } });
28
+ Object.defineProperty(exports, "renameExt", { enumerable: true, get: function () { return types_1.renameExt; } });
29
+ Object.defineProperty(exports, "validateUUID", { enumerable: true, get: function () { return types_1.validateUUID; } });
30
+ const index_1 = require("@e-mc/module");
31
+ function hasSize(value, keepEmpty) {
32
+ try {
33
+ const statSrc = fs.statSync(value);
34
+ if (statSrc.size > 0) {
35
+ return true;
36
+ }
37
+ if (!keepEmpty) {
38
+ fs.unlinkSync(value);
39
+ }
40
+ }
41
+ catch {
42
+ }
43
+ return false;
44
+ }
45
+ function getSize(value, diskUsed) {
46
+ try {
47
+ return fs[diskUsed ? 'lstatSync' : 'statSync'](value).size;
48
+ }
49
+ catch {
50
+ return 0;
51
+ }
52
+ }
53
+ function hasSameStat(src, dest, keepEmpty) {
54
+ try {
55
+ if (fs.existsSync(dest)) {
56
+ const { size, mtimeMs } = fs.statSync(src);
57
+ if (size > 0) {
58
+ const statDest = fs.statSync(dest);
59
+ return size === statDest.size && mtimeMs === statDest.mtimeMs;
60
+ }
61
+ if (!keepEmpty) {
62
+ fs.unlinkSync(src);
63
+ }
64
+ }
65
+ }
66
+ catch {
67
+ }
68
+ return false;
69
+ }
70
+ function byteLength(value, encoding) {
71
+ return typeof value === 'string' && (path.isAbsolute(value) || index_1.isPath(value)) ? getSize(value) : Buffer.byteLength(value, encoding && (0, types_1.getEncoding)(encoding));
72
+ }
73
+ function cleanupStream(stream, uri) {
74
+ stream.removeAllListeners();
75
+ stream.destroy();
76
+ try {
77
+ if (uri && fs.existsSync(uri)) {
78
+ fs.unlinkSync(uri);
79
+ }
80
+ }
81
+ catch (err) {
82
+ if (!index_1.isErrorCode(err, 'ENOENT')) {
83
+ index_1.writeFail(["Unable to delete file", path.basename(uri)], err, 32);
84
+ }
85
+ }
86
+ }
87
+ function generateUUID(format, dictionary) {
88
+ return format ? (0, types_1.randomString)(format, dictionary) : (0, types_1.generateUUID)();
89
+ }
90
+ function existsSafe(value, isFile) {
91
+ return index_1.isPath(value, isFile);
92
+ }
93
+ function isFileHTTP(value) {
94
+ return index_1.isFile(value, 'http/s');
95
+ }
96
+ function isFileUNC(value) {
97
+ return index_1.isFile(value, 'unc');
98
+ }
99
+ function isPathUNC(value) {
100
+ return index_1.isPath(value, 'unc');
101
+ }
102
+ async function readFileSafe(value, encoding, cache) {
103
+ return encoding === 'buffer' ? index_1.readBuffer(value, cache) : index_1.readText(value, encoding, cache);
104
+ }
105
+ function getFunctions(values, absolute, sync = true, outFailed) {
106
+ let options, context;
107
+ if ((0, types_1.isObject)(absolute)) {
108
+ options = absolute;
109
+ ({ context, outFailed } = absolute);
110
+ if (options.external === undefined) {
111
+ options.external = true;
112
+ }
113
+ }
114
+ else {
115
+ options = { external: true, absolute, sync };
116
+ }
117
+ const result = [];
118
+ for (const value of values) {
119
+ let method = null;
120
+ if (typeof value === 'string') {
121
+ method = index_1.parseFunction(value, options);
122
+ }
123
+ else if (typeof value === 'function') {
124
+ method = value;
125
+ if (context !== undefined) {
126
+ method.bind(context);
127
+ }
128
+ }
129
+ if (method) {
130
+ result.push(method);
131
+ }
132
+ else if (outFailed) {
133
+ outFailed.push(index_1.asString(value) || "Unknown");
134
+ }
135
+ }
136
+ return result;
137
+ }
138
+ async function allSettled(tasks, rejected, options) {
139
+ if (rejected) {
140
+ return Promise.allSettled(tasks).then(result => {
141
+ const items = [];
142
+ for (const item of result) {
143
+ if (item.status === 'fulfilled') {
144
+ items.push(item);
145
+ }
146
+ else if (item.reason) {
147
+ index_1.writeFail(rejected, item.reason, options);
148
+ }
149
+ }
150
+ return items;
151
+ })
152
+ .catch((err) => {
153
+ index_1.writeFail(rejected, err, options);
154
+ return [];
155
+ });
156
+ }
157
+ return Promise.allSettled(tasks);
158
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.11.2",
3
+ "version": "0.11.4",
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.11.2",
23
+ "@e-mc/types": "0.11.4",
24
24
  "chalk": "4.1.2",
25
25
  "file-type": "^18.7.0",
26
26
  "js-yaml": "^4.1.0",