@e-mc/module 0.8.26 → 0.8.28

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 +5 -5
  2. package/index.js +44 -62
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- - https://www.unpkg.com/@e-mc/types@0.8.26/lib/index.d.ts
12
+ - https://www.unpkg.com/@e-mc/types@0.8.28/lib/index.d.ts
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -231,10 +231,10 @@ interface ModuleConstructor {
231
231
 
232
232
  ## References
233
233
 
234
- - https://www.unpkg.com/@e-mc/types@0.8.26/lib/core.d.ts
235
- - https://www.unpkg.com/@e-mc/types@0.8.26/lib/logger.d.ts
236
- - https://www.unpkg.com/@e-mc/types@0.8.26/lib/module.d.ts
237
- - https://www.unpkg.com/@e-mc/types@0.8.26/lib/node.d.ts
234
+ - https://www.unpkg.com/@e-mc/types@0.8.28/lib/core.d.ts
235
+ - https://www.unpkg.com/@e-mc/types@0.8.28/lib/logger.d.ts
236
+ - https://www.unpkg.com/@e-mc/types@0.8.28/lib/module.d.ts
237
+ - https://www.unpkg.com/@e-mc/types@0.8.28/lib/node.d.ts
238
238
 
239
239
  ## LICENSE
240
240
 
package/index.js CHANGED
@@ -331,14 +331,14 @@ function tryCreateDir(value) {
331
331
  }
332
332
  return false;
333
333
  }
334
- function tryRemoveDir(value, empty, recursive) {
334
+ function tryRemoveDir(srcDir, empty, recursive) {
335
335
  const temp = typeof empty === 'number';
336
336
  let options;
337
337
  if (!temp) {
338
338
  try {
339
- fs.rmSync(value, { recursive, force: true });
339
+ fs.rmSync(srcDir, { recursive, force: true });
340
340
  if (empty) {
341
- fs.mkdirSync(value);
341
+ fs.mkdirSync(srcDir);
342
342
  }
343
343
  return [];
344
344
  }
@@ -346,30 +346,11 @@ function tryRemoveDir(value, empty, recursive) {
346
346
  }
347
347
  options = { recursive };
348
348
  }
349
- value = Module.normalizePath(value, 1);
350
349
  const failed = [];
351
- const nameMap = new WeakMap();
352
- let files;
353
- if (temp) {
354
- files = [];
355
- fs.readdirSync(value).forEach(name => {
356
- try {
357
- const stat = fs.statSync(value + name);
358
- nameMap.set(stat, name);
359
- files.push(stat);
360
- }
361
- catch {
362
- failed.push(value + name);
363
- }
364
- });
365
- }
366
- else {
367
- files = fs.readdirSync(value, { withFileTypes: true });
368
- }
369
350
  const current = Date.now();
370
- for (let i = 0, length = files.length; i < length; ++i) {
371
- const file = files[i];
372
- const pathname = value + (file.name || nameMap.get(file));
351
+ let entry;
352
+ fs.readdirSync(srcDir, { withFileTypes: true }).forEach(file => {
353
+ const pathname = path.join(srcDir, file.name);
373
354
  try {
374
355
  if (file.isDirectory()) {
375
356
  if (recursive) {
@@ -381,16 +362,16 @@ function tryRemoveDir(value, empty, recursive) {
381
362
  }
382
363
  }
383
364
  }
384
- else if (!temp || file.ctimeMs + empty <= current) {
365
+ else if (!temp || (entry = fs.statSync(pathname)) && entry.ctimeMs + empty <= current) {
385
366
  fs.unlinkSync(pathname);
386
367
  }
387
368
  }
388
369
  catch {
389
370
  failed.push(pathname);
390
371
  }
391
- }
372
+ });
392
373
  if (!empty && failed.length === 0) {
393
- fs.rmdirSync(value);
374
+ fs.rmdirSync(srcDir);
394
375
  }
395
376
  return failed;
396
377
  }
@@ -717,7 +698,7 @@ class Module extends EventEmitter {
717
698
  this[_f] = new AbortController();
718
699
  this[_g] = null;
719
700
  }
720
- static get VERSION() { return "0.8.26"; }
701
+ static get VERSION() { return "0.8.28"; }
721
702
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
722
703
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
723
704
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -1235,35 +1216,35 @@ class Module extends EventEmitter {
1235
1216
  return cacheKey ? (0, types_1.generateUUID)() : '';
1236
1217
  }
1237
1218
  static asHash(data, algorithm, digest) {
1238
- if (!algorithm && !digest) {
1239
- return crypto.createHash("sha256").update(data).digest("hex");
1240
- }
1241
- let options;
1242
- if ((0, types_1.isObject)(algorithm)) {
1243
- options = { ...algorithm };
1244
- if ('algorithm' in options) {
1245
- algorithm = options.algorithm;
1246
- delete options.algorithm;
1219
+ try {
1220
+ if (!algorithm && !digest) {
1221
+ return crypto.createHash("sha256").update(data).digest("hex");
1247
1222
  }
1248
- else {
1249
- algorithm = undefined;
1223
+ let options;
1224
+ if ((0, types_1.isObject)(algorithm)) {
1225
+ options = { ...algorithm };
1226
+ if ('algorithm' in options) {
1227
+ algorithm = options.algorithm;
1228
+ delete options.algorithm;
1229
+ }
1230
+ else {
1231
+ algorithm = undefined;
1232
+ }
1233
+ if ('digest' in options) {
1234
+ digest = options.digest;
1235
+ delete options.digest;
1236
+ }
1237
+ else {
1238
+ digest = undefined;
1239
+ }
1250
1240
  }
1251
- if ('digest' in options) {
1252
- digest = options.digest;
1253
- delete options.digest;
1241
+ else if ((0, types_1.isObject)(digest)) {
1242
+ options = digest;
1243
+ digest = undefined;
1254
1244
  }
1255
- else {
1245
+ else if (typeof digest !== 'string') {
1256
1246
  digest = undefined;
1257
1247
  }
1258
- }
1259
- else if ((0, types_1.isObject)(digest)) {
1260
- options = digest;
1261
- digest = undefined;
1262
- }
1263
- else if (typeof digest !== 'string') {
1264
- digest = undefined;
1265
- }
1266
- try {
1267
1248
  return crypto.createHash(algorithm || "sha256", options).update(data).digest(digest || "hex");
1268
1249
  }
1269
1250
  catch {
@@ -1340,8 +1321,7 @@ class Module extends EventEmitter {
1340
1321
  }
1341
1322
  static isFile(value, type) {
1342
1323
  if (!type) {
1343
- value = asFile(value);
1344
- return !!value && this.isPath(value, true);
1324
+ return this.isPath(value = asFile(value), true);
1345
1325
  }
1346
1326
  if (typeof value === 'string') {
1347
1327
  switch (type) {
@@ -1451,11 +1431,13 @@ class Module extends EventEmitter {
1451
1431
  if (typeof values[values.length - 1] === 'boolean') {
1452
1432
  normalize = values.pop();
1453
1433
  }
1454
- const paths = values.map(item => typeof item === 'string' ? item.trim() : '').filter(item => item).map(value => this.toPosix(value, normalize));
1434
+ const paths = values.filter(item => (0, types_1.isString)(item)).map(value => this.toPosix(value, normalize));
1455
1435
  let result = paths[0] || '';
1456
1436
  for (let i = 1; i < paths.length; ++i) {
1457
1437
  const trailing = paths[i];
1458
- result += (!trailing.startsWith('/') && !result.endsWith('/') ? '/' : '') + trailing;
1438
+ if (trailing) {
1439
+ result += (!trailing.startsWith('/') && !result.endsWith('/') ? '/' : '') + trailing;
1440
+ }
1459
1441
  }
1460
1442
  return result;
1461
1443
  }
@@ -1550,11 +1532,11 @@ class Module extends EventEmitter {
1550
1532
  }
1551
1533
  static async copyDir(src, dest, move, recursive = true) {
1552
1534
  const srcOut = sanitizePath(asFile(src));
1553
- if (!srcOut || !this.isDir(srcOut)) {
1535
+ if (!this.isDir(srcOut)) {
1554
1536
  return Promise.reject(errorDirectory(asFile(src) || "Unknown"));
1555
1537
  }
1556
1538
  const destOut = sanitizePath(asFile(dest));
1557
- if (!destOut || !this.createDir(destOut)) {
1539
+ if (!this.createDir(destOut)) {
1558
1540
  return Promise.reject(errorDirectory(asFile(dest) || "Unknown"));
1559
1541
  }
1560
1542
  let symFile, symDir, ignoreFile, ignoreDir, silent, overwrite;
@@ -1746,7 +1728,7 @@ class Module extends EventEmitter {
1746
1728
  ({ minStreamSize, encoding, cache } = options);
1747
1729
  }
1748
1730
  const src = sanitizePath(asFile(value));
1749
- if (src && this.isPath(src, true)) {
1731
+ if (this.isPath(src, true)) {
1750
1732
  let result;
1751
1733
  if (cache && (result = getCacheItem(CACHE_READTEXT, src))) {
1752
1734
  return (minStreamSize !== undefined ? Promise.resolve(result) : result);
@@ -1775,7 +1757,7 @@ class Module extends EventEmitter {
1775
1757
  ({ minStreamSize, cache } = options);
1776
1758
  }
1777
1759
  const src = sanitizePath(asFile(value));
1778
- if (src && this.isPath(src, true)) {
1760
+ if (this.isPath(src, true)) {
1779
1761
  let result;
1780
1762
  if (cache && (result = getCacheItem(CACHE_READBUFFER, src))) {
1781
1763
  return (minStreamSize !== undefined ? Promise.resolve(result) : result);
@@ -2484,7 +2466,7 @@ class Module extends EventEmitter {
2484
2466
  dir = dir.substring(ensureDir(PROCESS_CWD).length);
2485
2467
  }
2486
2468
  else {
2487
- this.formatMessage(1, 'WARN', 'Temp directory can only be relative', dir, { ...this.LOG_STYLE_WARN, newline: true });
2469
+ this.formatMessage(1, 'WARN', "Not permitted to set absolute path", dir, { ...this.LOG_STYLE_WARN, newline: true });
2488
2470
  dir = undefined;
2489
2471
  }
2490
2472
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.8.26",
3
+ "version": "0.8.28",
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": "MIT",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/types": "0.8.26",
23
+ "@e-mc/types": "0.8.28",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",