@e-mc/module 0.11.1 → 0.11.3

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 +9 -6
  2. package/index.js +28 -31
  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.1/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.11.3/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -263,6 +263,9 @@ interface NodeModule {
263
263
  npm?: boolean;
264
264
  inline?: boolean;
265
265
  };
266
+ posix?: {
267
+ strict?: boolean;
268
+ };
266
269
  settings?: {
267
270
  package_manager?: "npm" | "yarn" | "pnpm";
268
271
  };
@@ -407,11 +410,11 @@ interface LoggerModule {
407
410
 
408
411
  ## References
409
412
 
410
- - https://www.unpkg.com/@e-mc/types@0.11.1/lib/core.d.ts
411
- - https://www.unpkg.com/@e-mc/types@0.11.1/lib/logger.d.ts
412
- - https://www.unpkg.com/@e-mc/types@0.11.1/lib/module.d.ts
413
- - https://www.unpkg.com/@e-mc/types@0.11.1/lib/node.d.ts
414
- - https://www.unpkg.com/@e-mc/types@0.11.1/lib/settings.d.ts
413
+ - https://www.unpkg.com/@e-mc/types@0.11.3/lib/core.d.ts
414
+ - https://www.unpkg.com/@e-mc/types@0.11.3/lib/logger.d.ts
415
+ - https://www.unpkg.com/@e-mc/types@0.11.3/lib/module.d.ts
416
+ - https://www.unpkg.com/@e-mc/types@0.11.3/lib/node.d.ts
417
+ - https://www.unpkg.com/@e-mc/types@0.11.3/lib/settings.d.ts
415
418
 
416
419
  * https://www.npmjs.com/package/@types/node
417
420
 
package/index.js CHANGED
@@ -116,6 +116,7 @@ const VALUES = {
116
116
  ["node.process.cpu_usage"]: true,
117
117
  ["node.process.memory_usage"]: true,
118
118
  ["node.process.inline"]: true,
119
+ ["node.posix.strict"]: false,
119
120
  ["node.settings.package_manager"]: '',
120
121
  ["temp.dir"]: "tmp",
121
122
  ["temp.write"]: false,
@@ -143,8 +144,9 @@ const SUPPORTED_CANPARSE = (0, types_1.supported)(19, 9) || (0, types_1.supporte
143
144
  const REGEXP_TORRENT = /^(?:magnet:\?xt=|(?:https?|s?ftp):\/\/[^/][^\n]*?\.(?:torrent|metalink|meta4)(?:\?[^\n]*)?$)/i;
144
145
  const REGEXP_PROTOCOL = /^([a-z][a-z\d+-.]*):\/\/[^@:[\]\\^<>|\s]/i;
145
146
  const REGEXP_CLIESCAPE = /[^\w+-.,/:@]/g;
146
- const REGEXP_CLIOPTION = /^(-[^\s=]+|--[^-\s=][^\s=]*)(=)?\s*(.*)$/;
147
+ const REGEXP_CLIOPTION = /^(-[^\s=]+|--[^-\s=][^\s=]*)(=)?\s*(.*)$/s;
147
148
  const REGEXP_ANSIESCAPE = /[\u001B\u009B]/;
149
+ const REGEXP_PATHEND = new RegExp(`${path.sep}+$`);
148
150
  const LOG_CPUBARCOLOR = SETTINGS.process.cpu_bar_color;
149
151
  const LOG_DIVIDER = chalk.blackBright('|');
150
152
  const RESERVED_SHELL = [
@@ -935,7 +937,10 @@ function truncateStart(value, length = LOG_MIN_WIDTH, style = false) {
935
937
  function truncateEnd(value, length) {
936
938
  return (REGEXP_ANSIESCAPE.test(value) ? (0, node_util_1.stripVTControlCharacters)(value) : value).length > length ? '...' + value.substring(value.length - length + 3) : value;
937
939
  }
938
- const escapeArg = (value) => value.replace(REGEXP_CLIESCAPE, capture => capture === '\n' ? "'\n'" : (capture === '$' || capture === '`' ? '\\' : '') + '\\' + capture);
940
+ function isPathUNC(value) {
941
+ value = value.toString();
942
+ return (VALUES["node.posix.strict"] && value.includes('/') ? /^\/\/[\w.-]+\/[\w-]+\$?\/[^\n]*?[^/]$/ : /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?[\\/][^\n]*?[^\\/]$/).test(value);
943
+ }
939
944
  const isMeterUnit = (type, ident) => (type & 256) === 256 && !ident;
940
945
  const formatLogMessage = (type, message, unit, ident) => isMeterUnit(type, ident) ? (unit + LOG_DIVIDER + message).trimEnd() : unit ? (message + (ident ? '' : ' ') + unit).trimStart() : message;
941
946
  const hideAbort = (err) => err.name === 'AbortError' && SETTINGS.abort === false;
@@ -944,7 +949,7 @@ const wrapQuote = (value) => '"' + value.replaceAll('"', '\\"') + '"';
944
949
  const isFunction = (value) => typeof value === 'function';
945
950
  const isFileURL = (value) => /^file:\/\//i.test(value);
946
951
  const sanitizePath = (value) => value ? path.resolve(value) : '';
947
- const stripPath = (value) => path.normalize(value).replace(/[\\/]+$/, '');
952
+ const stripPath = (value) => path.normalize(value).replace(REGEXP_PATHEND, '');
948
953
  const ensureDir = (value) => value.endsWith(path.sep) ? value : value + path.sep;
949
954
  const trimDir = (value) => value.endsWith(path.sep) ? value.substring(0, value.length - 1) : value;
950
955
  const getExtension = (value) => path.extname(value).toLowerCase().substring(1);
@@ -979,7 +984,7 @@ class Module extends EventEmitter {
979
984
  this[_g] = null;
980
985
  }
981
986
  static get VERSION() {
982
- return "0.11.1";
987
+ return "0.11.3";
983
988
  }
984
989
  static get LOG_TYPE() {
985
990
  return types_1.LOG_TYPE;
@@ -1017,6 +1022,7 @@ class Module extends EventEmitter {
1017
1022
  case "node.process.cpu_usage":
1018
1023
  case "node.process.memory_usage":
1019
1024
  case "node.process.inline":
1025
+ case "node.posix.strict":
1020
1026
  case "temp.write":
1021
1027
  case "permission.home_read":
1022
1028
  case "permission.home_write":
@@ -1594,7 +1600,7 @@ class Module extends EventEmitter {
1594
1600
  case 'http/s':
1595
1601
  return /^https?:\/\/[^/]/i.test(value);
1596
1602
  case 'unc':
1597
- return /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?[\\/][^\n]*?[^\\/]$/.test(value);
1603
+ return isPathUNC(value);
1598
1604
  case 'ftp':
1599
1605
  return /^ftp:\/\/[^/]/i.test(value);
1600
1606
  case 'sftp':
@@ -1616,7 +1622,7 @@ class Module extends EventEmitter {
1616
1622
  case 'http/s':
1617
1623
  return value.protocol === 'http:' || value.protocol === 'https:';
1618
1624
  case 'unc':
1619
- return /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?[\\/][^\n]*?[^\\/]$/.test(value.toString());
1625
+ return isPathUNC(value);
1620
1626
  case 'ftp':
1621
1627
  return value.protocol === 'ftp:';
1622
1628
  case 'sftp':
@@ -1653,7 +1659,8 @@ class Module extends EventEmitter {
1653
1659
  return false;
1654
1660
  }
1655
1661
  case 'unc':
1656
- return /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?(?:[\\/]?|[\\/][^\n]+)$/.test(asFile(value));
1662
+ value = asFile(value);
1663
+ return (VALUES["node.posix.strict"] && value.includes('/') ? /^\/\/[\w.-]+\/[\w-]+\$?(?:\/?|\/[^\n]+)$/ : /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?(?:[\\/]?|[\\/][^\n]+)$/).test(value);
1657
1664
  }
1658
1665
  }
1659
1666
  catch (err) {
@@ -1726,7 +1733,7 @@ class Module extends EventEmitter {
1726
1733
  }
1727
1734
  static fromLocalPath(value) {
1728
1735
  const ch = value[0];
1729
- if (ch === '.' && /^\.{1,2}[\\/][^\\/]/.test(value)) {
1736
+ if (ch === '.' && (VALUES["node.posix.strict"] && value.includes('/') ? /^\.{1,2}\/[^/]/ : /^\.{1,2}[\\/][^\\/]/).test(value)) {
1730
1737
  return path.join(PROCESS_CWD, value);
1731
1738
  }
1732
1739
  if (ch === '~' && value[1] === '/') {
@@ -1964,7 +1971,8 @@ class Module extends EventEmitter {
1964
1971
  const items = Array.isArray(exclude) ? exclude : [exclude];
1965
1972
  exclude = [];
1966
1973
  for (const value of items) {
1967
- if (/^[^\\/][^*]+[\\/]$/.test(value) && !(0, types_1.hasGlob)(value)) {
1974
+ const dirEnd = value.endsWith('/') ? /^[^/][^*]+\/$/ : value.endsWith('\\') ? /^[^\\][^*]+\\$/ : null;
1975
+ if (dirEnd?.test(value) && !(0, types_1.hasGlob)(value)) {
1968
1976
  (excludeDir ||= []).push(this.normalizePath(value, 4));
1969
1977
  }
1970
1978
  else {
@@ -2226,7 +2234,7 @@ class Module extends EventEmitter {
2226
2234
  }
2227
2235
  const folders = [PROCESS_CWD];
2228
2236
  if (startDir && withinDir(startDir = path.normalize(startDir), PROCESS_CWD)) {
2229
- startDir = startDir.replace(/[\\/]+$/, '');
2237
+ startDir = startDir.replace(REGEXP_PATHEND, '');
2230
2238
  let i = -1, j = PROCESS_CWD.length + 1;
2231
2239
  while ((i = startDir.indexOf(path.sep, j)) !== -1) {
2232
2240
  const current = startDir.substring(0, i);
@@ -2373,7 +2381,7 @@ class Module extends EventEmitter {
2373
2381
  value = opt[3];
2374
2382
  }
2375
2383
  if (value) {
2376
- const quoted = /^(["'])(.*)\1$/.exec(value);
2384
+ const quoted = /^(["'])(.*)\1$/s.exec(value);
2377
2385
  if (PLATFORM_WIN32) {
2378
2386
  if (quoted) {
2379
2387
  if (quoted[1] === '"') {
@@ -2386,33 +2394,19 @@ class Module extends EventEmitter {
2386
2394
  value = wrapQuote(value);
2387
2395
  }
2388
2396
  }
2389
- else {
2390
- let type = doubleQuote;
2391
- if (quoted) {
2392
- if (quoted[1] === "'") {
2393
- result[i] = leading + value;
2394
- continue;
2395
- }
2396
- value = quoted[2];
2397
- type = true;
2398
- }
2397
+ else if (!quoted) {
2399
2398
  if (REGEXP_CLIESCAPE.test(value)) {
2400
- if (type) {
2401
- value = '"' + escapeArg(value) + '"';
2402
- }
2403
- else {
2404
- value = "'" + value.replaceAll("'", "'\\''") + "'";
2405
- }
2399
+ value = doubleQuote ? `"${value.replace(/(?<!\\)"/g, '\\"')}"` : `'${value.replaceAll("'", "'\\''")}'`;
2406
2400
  }
2407
2401
  else if (RESERVED_SHELL.includes(value)) {
2408
- value = "'" + value + "'";
2402
+ value = `'${value}'`;
2409
2403
  }
2410
2404
  }
2411
2405
  }
2412
2406
  else if (opt) {
2413
2407
  leading = leading.trim();
2414
2408
  if (!PLATFORM_WIN32 && leading.length > 2 && leading[1] !== '-') {
2415
- leading = escapeArg(leading);
2409
+ leading = leading.replace(REGEXP_CLIESCAPE, capture => '\\' + capture);
2416
2410
  }
2417
2411
  }
2418
2412
  }
@@ -2491,7 +2485,7 @@ class Module extends EventEmitter {
2491
2485
  }
2492
2486
  const { temp, node, permission, memory, error, logger } = settings;
2493
2487
  if (node) {
2494
- const { process: proc, require: req } = node;
2488
+ const { process: proc, require: req, posix } = node;
2495
2489
  if (proc) {
2496
2490
  const { cpu_usage, memory_usage, inline } = proc;
2497
2491
  if (typeof cpu_usage === 'boolean') {
@@ -2526,6 +2520,9 @@ class Module extends EventEmitter {
2526
2520
  VALUES["node.require.inline"] = inline;
2527
2521
  }
2528
2522
  }
2523
+ if (typeof posix?.strict === 'boolean') {
2524
+ VALUES["node.posix.strict"] = posix.strict;
2525
+ }
2529
2526
  const manager = node.settings?.package_manager;
2530
2527
  switch (manager) {
2531
2528
  case 'npm':
@@ -3429,7 +3426,7 @@ class Module extends EventEmitter {
3429
3426
  const increment = options.meterIncrement || (http ? meter.http
3430
3427
  : type & 2048 ? meter.image
3431
3428
  : type & 8 ? meter.compress
3432
- : meter.process) || 250;
3429
+ : meter.process) || types_1.THRESHOLD.LOGGER_METER_INCREMENT;
3433
3430
  const duration = getTimeOffset(startTime, 0);
3434
3431
  let meterTime = duration, delayTime = options.delayTime;
3435
3432
  if (typeof delayTime === 'bigint' || Array.isArray(delayTime)) {
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.1",
3
+ "version": "0.11.3",
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.1",
23
+ "@e-mc/types": "0.11.3",
24
24
  "chalk": "4.1.2",
25
25
  "file-type": "^18.7.0",
26
26
  "js-yaml": "^4.1.0",