@e-mc/module 0.12.9 → 0.13.1

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 +21 -16
  2. package/index.js +171 -127
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @e-mc/module
2
2
 
3
- * NodeJS 18
3
+ * NodeJS 18.20.5 LTS
4
4
  * ES2022
5
5
 
6
6
  ## General Usage
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.12.9/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.13.1/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { LogStatus } from "./squared";
@@ -17,8 +17,8 @@ import type { LogStatus } from "./squared";
17
17
  import type { IHost } from "./index";
18
18
  import type { IAbortComponent, IPermission } from "./core";
19
19
  import type { LOG_TYPE, STATUS_TYPE, ExecCommand, LogArguments, LogComponent, LogDate, LogFailOptions, LogMessageOptions, LogOptions, LogProcessOptions, LogTime, LogType, LogValue, LoggerFormat, StatusType } from "./logger";
20
- import type { AsHashOptions, CheckSemVerOptions, CopyDirOptions, CopyDirResult, CopyFileOptions, CreateDirOptions, DeleteFileOptions, GlobDirOptions, MoveFileOptions, PackageVersionOptions, ParseFunctionOptions, PermissionOptions, ProtocolType, ReadBufferOptions, ReadFileCallback, ReadFileOptions, ReadHashOptions, ReadTextOptions, RemoveDirOptions, TempDirOptions, WriteFileOptions } from "./module";
21
- import type { Settings } from "./node";
20
+ import type { AsHashOptions, CheckSemVerOptions, CopyDirOptions, CopyDirResult, CopyFileOptions, CreateDirOptions, DeleteFileOptions, DirectoryActionType, FileActionType, GlobDirOptions, MoveFileOptions, PackageVersionOptions, ParseFunctionOptions, PermissionOptions, ProtocolType, ReadBufferOptions, ReadFileCallback, ReadFileOptions, ReadHashOptions, ReadTextOptions, RemoveDirOptions, TempDirOptions, WriteFileOptions } from "./module";
21
+ import type { ErrorCode, Settings } from "./node";
22
22
  import type { LoggerFormatSettings } from "/settings";
23
23
 
24
24
  import type { SpawnOptions } from "node:child_process";
@@ -93,9 +93,7 @@ interface IModule extends EventEmitter, IAbortComponent {
93
93
  checkFail(message: unknown, options: LogFailOptions): LogArguments | false | undefined;
94
94
  writeLog(component: LogComponent, queue?: boolean): void;
95
95
  writeLog(type: StatusType, value: unknown, options: LogOptions): void;
96
- writeLog(type: StatusType, value: unknown, from: string, source?: string): void;
97
- writeLog(type: StatusType, value: unknown, timeStamp?: LogDate, from?: string, source?: string): void;
98
- writeLog(type: StatusType, value: unknown, timeStamp?: LogDate, duration?: number, from?: string, source?: string): void;
96
+ writeLog(type: StatusType, value: unknown, timeStamp?: LogDate, duration?: number): void;
99
97
  addLog(component: LogComponent, queue?: boolean): void;
100
98
  addLog(type: StatusType, value: unknown, options: LogOptions): void;
101
99
  addLog(type: StatusType, value: unknown, from: string, source?: string): void;
@@ -140,26 +138,32 @@ interface IModule extends EventEmitter, IAbortComponent {
140
138
  on(event: "file:delete", listener: (src: string, options?: DeleteFileOptions) => void): this;
141
139
  on(event: "file:copy", listener: (dest: string, options?: CopyFileOptions) => void): this;
142
140
  on(event: "file:move", listener: (dest: string, options?: MoveFileOptions) => void): this;
141
+ on(event: "file:permission", listener: (src: string, type?: FileActionType) => void): this;
143
142
  on(event: "dir:create", listener: (src: string, options?: CreateDirOptions) => void): this;
144
143
  on(event: "dir:remove", listener: (src: string, options?: RemoveDirOptions) => void): this;
144
+ on(event: "dir:permission", listener: (src: string, type?: DirectoryActionType) => void): this;
145
145
  once(event: "exec", listener: (command: ExecCommand, options?: SpawnOptions) => void): this;
146
146
  once(event: "error", listener: (err: Error) => void): this;
147
- once(event: "file:read", listener: (src: string, data: Buffer | string, options?: ReadFileOptions) => void): this;
147
+ once(event: "file:read", listener: (src: string, data: Buffer | string,, options?: ReadFileOptions) => void): this;
148
148
  once(event: "file:write", listener: (src: string, options?: WriteFileOptions) => void): this;
149
149
  once(event: "file:delete", listener: (src: string, options?: DeleteFileOptions) => void): this;
150
150
  once(event: "file:copy", listener: (dest: string, options?: CopyFileOptions) => void): this;
151
151
  once(event: "file:move", listener: (dest: string, options?: MoveFileOptions) => void): this;
152
+ once(event: "file:permission", listener: (src: string, type?: FileActionType) => void): this;
152
153
  once(event: "dir:create", listener: (src: string, options?: CreateDirOptions) => void): this;
153
154
  once(event: "dir:remove", listener: (src: string, options?: RemoveDirOptions) => void): this;
155
+ once(event: "dir:permission", listener: (src: string, type?: DirectoryActionType) => void): this;
154
156
  emit(event: "exec", command: ExecCommand, options?: SpawnOptions): boolean;
155
157
  emit(event: "error", err: Error): boolean;
156
- emit(event: "file:read", src: string, data: Buffer | string, options?: ReadFileOptions): boolean;
158
+ emit(event: "file:read", src: string, data: Buffer | string,, options?: ReadFileOptions): boolean;
157
159
  emit(event: "file:write", src: string, options?: WriteFileOptions): boolean;
158
160
  emit(event: "file:delete", src: string, options?: DeleteFileOptions): boolean;
159
161
  emit(event: "file:copy", dest: string, options?: CopyFileOptions): boolean;
160
162
  emit(event: "file:move", dest: string, options?: MoveFileOptions): boolean;
163
+ emit(event: "file:permission", src: string, type?: FileActionType): boolean;
161
164
  emit(event: "dir:create", src: string, options?: CreateDirOptions): boolean;
162
165
  emit(event: "dir:remove", src: string, options?: RemoveDirOptions): boolean;
166
+ emit(event: "dir:permission", src: string, type?: DirectoryActionType): boolean;
163
167
  }
164
168
 
165
169
  interface ModuleConstructor {
@@ -195,10 +199,11 @@ interface ModuleConstructor {
195
199
  hasLogType(value: LogType): boolean;
196
200
  isURL(value: string, ...exclude: string[]): boolean;
197
201
  isFile(value: string | URL, type?: ProtocolType): boolean;
198
- isDir(value: string | URL): boolean;
202
+ isDir(value: string | URL, absolute?: boolean): boolean;
199
203
  isPath(value: string | URL, type?: "unc" | "unc-exists"): boolean;
200
204
  isPath(value: string | URL, isFile?: boolean): boolean;
201
- isErrorCode(err: unknown, ...code: string[]): boolean;
205
+ /** @deprecated @e-mc/types */
206
+ isErrorCode(err: unknown, ...code: unknown[]): err is Required<ErrorCode>;
202
207
  fromLocalPath(value: string): string;
203
208
  resolveFile(value: string): string;
204
209
  resolvePath(value: string, base: string | URL): string;
@@ -416,11 +421,11 @@ interface LoggerModule {
416
421
 
417
422
  ## References
418
423
 
419
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/core.d.ts
420
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/logger.d.ts
421
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/module.d.ts
422
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/node.d.ts
423
- - https://www.unpkg.com/@e-mc/types@0.12.9/lib/settings.d.ts
424
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/core.d.ts
425
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/logger.d.ts
426
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/module.d.ts
427
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/node.d.ts
428
+ - https://www.unpkg.com/@e-mc/types@0.13.1/lib/settings.d.ts
424
429
 
425
430
  * https://www.npmjs.com/package/@types/node
426
431
 
package/index.js CHANGED
@@ -579,48 +579,77 @@ function addCacheItem(map, key, data, cache = false) {
579
579
  const length = Buffer.byteLength(data);
580
580
  if (length >= MEMORY_CACHE_DISK.min_size && length <= MEMORY_CACHE_DISK.max_size) {
581
581
  key = Module.toPosix(key, true);
582
- let valid = true;
583
- if (MEMORY_CACHE_DISK.exclude) {
584
- for (const value of MEMORY_CACHE_DISK.exclude) {
585
- if (value.startsWith('!')) {
586
- if (isMatch(key, value.substring(1))) {
587
- valid = true;
588
- break;
589
- }
590
- }
591
- else if (valid && isMatch(key, value)) {
592
- valid = false;
593
- }
594
- }
582
+ if (MEMORY_CACHE_DISK.exclude && isMatch(key, MEMORY_CACHE_DISK.exclude)) {
583
+ return;
595
584
  }
596
- if (valid && (cache || !MEMORY_CACHE_DISK.include || MEMORY_CACHE_DISK.include.some(value => isMatch(key, value)))) {
585
+ if (cache || !MEMORY_CACHE_DISK.include || isMatch(key, MEMORY_CACHE_DISK.include)) {
597
586
  map.set(key, [Date.now(), data]);
598
587
  }
599
588
  }
600
589
  }
601
- function getCacheTotal() {
590
+ function getCacheSize() {
602
591
  return CACHE_READTEXT.size + CACHE_READBUFFER.size + CACHE_READCJS.size;
603
592
  }
604
- function isMatch(key, value) {
605
- return pm.isMatch(key, value, { nocase: PLATFORM_WIN32, matchBase: value.startsWith('*') && !value.includes('/') });
593
+ function createMatchNegate(values) {
594
+ if (!(0, types_1.isArray)(values)) {
595
+ return null;
596
+ }
597
+ const result = [[], []];
598
+ const base = [];
599
+ const baseAbs = [];
600
+ const relative = [];
601
+ const relativeAbs = [];
602
+ for (let value of values) {
603
+ value = Module.toPosix(value, true);
604
+ const invert = value.startsWith('!');
605
+ if (invert) {
606
+ value = value.substring(1);
607
+ }
608
+ if (value.startsWith('*') && !value.includes('/')) {
609
+ (invert ? relativeAbs : relative).push(value);
610
+ }
611
+ else {
612
+ (invert ? baseAbs : base).push(value);
613
+ }
614
+ }
615
+ if (base.length > 0) {
616
+ result[0].push(pm(base, { nocase: PLATFORM_WIN32 }));
617
+ }
618
+ if (relative.length > 0) {
619
+ result[0].push(pm(relative, { nocase: PLATFORM_WIN32, matchBase: true }));
620
+ }
621
+ if (baseAbs.length > 0) {
622
+ result[1].push(pm(baseAbs, { nocase: PLATFORM_WIN32 }));
623
+ }
624
+ if (relativeAbs.length > 0) {
625
+ result[1].push(pm(relativeAbs, { nocase: PLATFORM_WIN32, matchBase: true }));
626
+ }
627
+ return result;
628
+ }
629
+ function isMatch(value, globs) {
630
+ return !globs[1].some(match => match(value)) && globs[0].some(match => match(value));
606
631
  }
607
632
  function listDir(src, paths, depth, include, exclude, excludeDir, outFiles) {
608
633
  const srcDir = path.join(src, ...paths);
609
- fs.readdirSync(srcDir, { withFileTypes: true }).forEach(file => {
610
- if (file.isFile()) {
611
- const pathname = path.join(srcDir, file.name);
612
- const pathglob = pathname.substring(src.length);
613
- if (include(pathglob) && !exclude?.(pathglob)) {
614
- outFiles.push(pathname);
634
+ try {
635
+ fs.readdirSync(srcDir, { withFileTypes: true }).forEach(file => {
636
+ if (file.isFile()) {
637
+ const pathname = path.join(srcDir, file.name);
638
+ const pathglob = pathname.substring(src.length);
639
+ if (include(pathglob) && !exclude?.(pathglob)) {
640
+ outFiles.push(pathname);
641
+ }
615
642
  }
616
- }
617
- else if (depth > 0 && file.isDirectory()) {
618
- const subDirs = paths.concat(file.name);
619
- if (!excludeDir?.includes(subDirs.join('/') + '/')) {
620
- listDir(src, subDirs, depth - 1, include, exclude, excludeDir, outFiles);
643
+ else if (depth > 0 && file.isDirectory()) {
644
+ const subDirs = paths.concat(file.name);
645
+ if (!excludeDir?.includes(subDirs.join('/') + '/')) {
646
+ listDir(src, subDirs, depth - 1, include, exclude, excludeDir, outFiles);
647
+ }
621
648
  }
622
- }
623
- });
649
+ });
650
+ }
651
+ catch {
652
+ }
624
653
  }
625
654
  function checkFunction(value) {
626
655
  if (typeof value === 'function') {
@@ -688,7 +717,7 @@ function setCpuAndMem(instance, options) {
688
717
  }
689
718
  }
690
719
  function checkExDev(err, src, dest) {
691
- if (Module.isErrorCode(err, 'EXDEV')) {
720
+ if ((0, types_1.isErrorCode)(err, 'EXDEV')) {
692
721
  try {
693
722
  fs.copyFileSync(src, dest);
694
723
  fs.unlinkSync(src);
@@ -819,7 +848,6 @@ function isPathUNC(value) {
819
848
  value = value.toString();
820
849
  return (VALUES["node.posix.strict"] && value.includes('/') ? /^\/\/[\w.-]+\/[\w-]+\$?\/[^\n]*?[^/]$/ : /^(?:\\\\|\/\/)[\w.-]+[\\/][\w-]+\$?[\\/][^\n]*?[^\\/]$/).test(value);
821
850
  }
822
- const isErrnoException = (value) => value instanceof Error && typeof value.code === 'string';
823
851
  const isConstructor = (value) => typeof value === 'function' && !!value.prototype?.constructor.name;
824
852
  const isHost = (value) => 'username' in value && typeof value.username === 'string';
825
853
  const hasAuth = (users, username) => !!username && (users === true || Array.isArray(users) && users.includes(username));
@@ -827,7 +855,7 @@ const formatLogMessage = (type, message, unit, ident) => !ident && (type & 256)
827
855
  const hideAbort = (err) => err.name === 'AbortError' && SETTINGS.abort === false;
828
856
  const asFile = (value) => typeof value === 'string' ? PLATFORM_WIN32 ? value.trim() : value : value instanceof URL && value.protocol === 'file:' ? (0, node_url_1.fileURLToPath)(value) : '';
829
857
  const isFileURL = (value) => /^file:\/\//i.test(value);
830
- const sanitizePath = (value) => value ? path.resolve(value) : '';
858
+ const sanitizePath = (value) => value && path.resolve(value);
831
859
  const stripPath = (value) => path.normalize(value).replace(REGEXP_PATHEND, '');
832
860
  const ensureDir = (value) => value.endsWith(path.sep) ? value : value + path.sep;
833
861
  const trimDir = (value) => value.endsWith(path.sep) ? value.substring(0, value.length - 1) : value;
@@ -850,7 +878,7 @@ class Module extends EventEmitter {
850
878
  static LOG_STYLE_NOTICE = Object.freeze({ titleBgColor: 'bgGrey', titleColor: 'white' });
851
879
  static LOG_STYLE_REVERSE = Object.freeze({ titleBgColor: 'bgWhite', titleColor: 'black', messageBgColor: 'bgGrey' });
852
880
  static get VERSION() {
853
- return "0.12.9";
881
+ return "0.13.1";
854
882
  }
855
883
  static get LOG_TYPE() {
856
884
  return types_1.LOG_TYPE;
@@ -1369,11 +1397,14 @@ class Module extends EventEmitter {
1369
1397
  else {
1370
1398
  algorithm = undefined;
1371
1399
  }
1372
- if ('digestEncoding' in options || 'digest' in options) {
1373
- encoding = options.digestEncoding || options.digest;
1374
- delete options.digest;
1400
+ if ('digestEncoding' in options) {
1401
+ encoding = options.digestEncoding;
1375
1402
  delete options.digestEncoding;
1376
1403
  }
1404
+ else if ('digest' in options) {
1405
+ encoding = options.digest;
1406
+ delete options.digest;
1407
+ }
1377
1408
  else {
1378
1409
  encoding = undefined;
1379
1410
  }
@@ -1398,7 +1429,7 @@ class Module extends EventEmitter {
1398
1429
  }
1399
1430
  }
1400
1431
  catch (err) {
1401
- if (this.isErrorCode(err, 'ENOENT', 'EACCES', 'ELOOP', 'ENAMETOOLONG', 'EINVAL')) {
1432
+ if ((0, types_1.isErrorCode)(err, 'ENOENT', 'EACCES', 'ELOOP', 'ENAMETOOLONG', 'EINVAL')) {
1402
1433
  throw err;
1403
1434
  }
1404
1435
  }
@@ -1506,7 +1537,10 @@ class Module extends EventEmitter {
1506
1537
  }
1507
1538
  return false;
1508
1539
  }
1509
- static isDir(value) {
1540
+ static isDir(value, absolute) {
1541
+ if (absolute && !path.isAbsolute(value = asFile(value))) {
1542
+ return false;
1543
+ }
1510
1544
  try {
1511
1545
  return fs.statSync(value).isDirectory();
1512
1546
  }
@@ -1533,11 +1567,11 @@ class Module extends EventEmitter {
1533
1567
  }
1534
1568
  }
1535
1569
  catch (err) {
1536
- return !type && this.isErrorCode(err, 'EBUSY', 'EPERM');
1570
+ return !type && (0, types_1.isErrorCode)(err, 'EBUSY', 'EPERM');
1537
1571
  }
1538
1572
  }
1539
1573
  static isErrorCode(err, ...code) {
1540
- return isErrnoException(err) && code.includes(err.code);
1574
+ return (0, types_1.isErrorCode)(err, ...code);
1541
1575
  }
1542
1576
  static resolveFile(value) {
1543
1577
  if (isFileURL(value = value instanceof URL ? value.toString() : value)) {
@@ -1571,7 +1605,7 @@ class Module extends EventEmitter {
1571
1605
  }
1572
1606
  static joinPath(...values) {
1573
1607
  let normalize = false;
1574
- if (typeof values[values.length - 1] === 'boolean') {
1608
+ if (typeof values.at(-1) === 'boolean') {
1575
1609
  normalize = values.pop();
1576
1610
  }
1577
1611
  const paths = values.filter(item => (0, types_1.isString)(item)).map(value => this.toPosix(value, normalize));
@@ -1639,7 +1673,7 @@ class Module extends EventEmitter {
1639
1673
  }
1640
1674
  }
1641
1675
  catch (err) {
1642
- if (this.isErrorCode(err, 'EBUSY', 'EPERM')) {
1676
+ if ((0, types_1.isErrorCode)(err, 'EBUSY', 'EPERM')) {
1643
1677
  return false;
1644
1678
  }
1645
1679
  }
@@ -1693,7 +1727,7 @@ class Module extends EventEmitter {
1693
1727
  const tasks = [];
1694
1728
  const success = [];
1695
1729
  const failed = [];
1696
- const ignored = [];
1730
+ const ignored = new Set();
1697
1731
  const methodName = move ? 'rename' : 'copyFile';
1698
1732
  const srcFound = [];
1699
1733
  const symFound = [];
@@ -1707,11 +1741,6 @@ class Module extends EventEmitter {
1707
1741
  const name = file.name;
1708
1742
  const destPath = path.join(destDir, name);
1709
1743
  let srcPath = path.join(srcDir, name), isDir = false, isFile = file.isFile() ? 1 : 0;
1710
- const ignore = () => {
1711
- if (!ignored.includes(srcPath)) {
1712
- ignored.push(srcPath);
1713
- }
1714
- };
1715
1744
  if (file.isSymbolicLink()) {
1716
1745
  const previous = symFound.includes(srcPath);
1717
1746
  symFound.push(srcPath);
@@ -1720,7 +1749,7 @@ class Module extends EventEmitter {
1720
1749
  const stat = fs.statSync(realPath);
1721
1750
  if (stat.isFile()) {
1722
1751
  if (symFile === false || !(fs.existsSync(destPath) && !fs.statSync(destPath).isFile())) {
1723
- ignore();
1752
+ ignored.add(srcPath);
1724
1753
  return;
1725
1754
  }
1726
1755
  srcPath = realPath;
@@ -1728,7 +1757,7 @@ class Module extends EventEmitter {
1728
1757
  }
1729
1758
  else {
1730
1759
  if (symDir !== true || previous || !stat.isDirectory()) {
1731
- ignore();
1760
+ ignored.add(srcPath);
1732
1761
  return;
1733
1762
  }
1734
1763
  isFile = 0;
@@ -1736,24 +1765,24 @@ class Module extends EventEmitter {
1736
1765
  }
1737
1766
  }
1738
1767
  catch {
1739
- ignore();
1768
+ ignored.add(srcPath);
1740
1769
  return;
1741
1770
  }
1742
1771
  }
1743
1772
  if (isFile) {
1744
1773
  if (ignoreFile?.test(name)) {
1745
- ignore();
1774
+ ignored.add(srcPath);
1746
1775
  return;
1747
1776
  }
1748
1777
  if (overwrite === false) {
1749
1778
  try {
1750
1779
  if (fs.existsSync(destPath)) {
1751
- ignore();
1780
+ ignored.add(srcPath);
1752
1781
  return;
1753
1782
  }
1754
1783
  }
1755
1784
  catch {
1756
- ignore();
1785
+ ignored.add(srcPath);
1757
1786
  return;
1758
1787
  }
1759
1788
  }
@@ -1770,7 +1799,7 @@ class Module extends EventEmitter {
1770
1799
  }
1771
1800
  else if (depth > 0 && (isDir || file.isDirectory())) {
1772
1801
  if (ignoreDir?.test(name)) {
1773
- ignore();
1802
+ ignored.add(srcPath);
1774
1803
  return;
1775
1804
  }
1776
1805
  recurse.call(this, paths.concat(name), depth - 1);
@@ -1812,7 +1841,7 @@ class Module extends EventEmitter {
1812
1841
  }
1813
1842
  }
1814
1843
  }
1815
- resolve({ success, failed, ignored });
1844
+ resolve({ success, failed, ignored: Array.from(ignored) });
1816
1845
  });
1817
1846
  });
1818
1847
  }
@@ -1862,7 +1891,7 @@ class Module extends EventEmitter {
1862
1891
  return true;
1863
1892
  }
1864
1893
  catch (err) {
1865
- if (this.isErrorCode(err, 'EXDEV')) {
1894
+ if ((0, types_1.isErrorCode)(err, 'EXDEV')) {
1866
1895
  let copied = false;
1867
1896
  try {
1868
1897
  fs.copyFileSync(src, dest);
@@ -1981,7 +2010,7 @@ class Module extends EventEmitter {
1981
2010
  }
1982
2011
  static async resolveMime(data) {
1983
2012
  FILETYPE_ESM ||= await (0, types_1.importESM)('file-type');
1984
- return typeof data === 'string' ? FILETYPE_ESM.fileTypeFromFile(data) : data instanceof stream.Readable ? FILETYPE_ESM.fileTypeFromStream(data) : FILETYPE_ESM.fileTypeFromBuffer(data);
2013
+ return typeof data === 'string' ? FILETYPE_ESM.fileTypeFromFile(data) : stream.Readable.isReadable(data) ? FILETYPE_ESM.fileTypeFromStream(data) : FILETYPE_ESM.fileTypeFromBuffer(data);
1985
2014
  }
1986
2015
  static lookupMime(value, extension) {
1987
2016
  return (extension ? mime.extension(value) : mime.lookup(value)) || '';
@@ -2240,9 +2269,9 @@ class Module extends EventEmitter {
2240
2269
  limit = 0;
2241
2270
  }
2242
2271
  let result = 0;
2243
- if (limit === 0 || limit >= getCacheTotal()) {
2272
+ if (limit === 0 || limit >= getCacheSize()) {
2244
2273
  if (percent >= 1) {
2245
- result = getCacheTotal();
2274
+ result = getCacheSize();
2246
2275
  CACHE_READTEXT.clear();
2247
2276
  CACHE_READBUFFER.clear();
2248
2277
  CACHE_READCJS.clear();
@@ -2394,14 +2423,8 @@ class Module extends EventEmitter {
2394
2423
  if (expires !== undefined && (expires = (0, types_1.parseExpires)(expires)) > 0) {
2395
2424
  MEMORY_CACHE_DISK.expires = expires;
2396
2425
  }
2397
- MEMORY_CACHE_DISK.include = null;
2398
- MEMORY_CACHE_DISK.exclude = null;
2399
- if ((0, types_1.isArray)(include)) {
2400
- MEMORY_CACHE_DISK.include = include.map(value => this.toPosix(value, true));
2401
- }
2402
- if ((0, types_1.isArray)(exclude)) {
2403
- MEMORY_CACHE_DISK.exclude = exclude.map(value => this.toPosix(value, true));
2404
- }
2426
+ MEMORY_CACHE_DISK.include = createMatchNegate(include);
2427
+ MEMORY_CACHE_DISK.exclude = createMatchNegate(exclude);
2405
2428
  }
2406
2429
  }
2407
2430
  if (logger) {
@@ -2616,13 +2639,13 @@ class Module extends EventEmitter {
2616
2639
  }
2617
2640
  write = temp.write;
2618
2641
  }
2619
- if (env && path.isAbsolute(env) && this.isDir(env)) {
2642
+ if (env && this.isDir(env, true)) {
2620
2643
  (0, types_1.setTempDir)(TEMP_DIR = stripPath(env));
2621
2644
  dir = path.relative(PROCESS_CWD, TEMP_DIR);
2622
2645
  VALUES["temp.dir"] = dir;
2623
2646
  modified = true;
2624
2647
  }
2625
- else if ((0, types_1.isString)(dir)) {
2648
+ else if (dir) {
2626
2649
  if (path.isAbsolute(dir = stripPath(dir))) {
2627
2650
  if (withinDir(dir, PROCESS_CWD)) {
2628
2651
  dir = dir.substring(ensureDir(PROCESS_CWD).length);
@@ -2742,48 +2765,54 @@ class Module extends EventEmitter {
2742
2765
  return result || path.join(...leading);
2743
2766
  }
2744
2767
  canRead(uri, options) {
2745
- let permission = this.permission, ownPermissionOnly = false;
2768
+ uri = asFile(uri);
2769
+ if (!uri) {
2770
+ return false;
2771
+ }
2772
+ let permission, ownPermissionOnly = false;
2746
2773
  if (options) {
2747
2774
  if (options.hostPermissionOnly) {
2748
- let host;
2749
- if (!(permission = (host = this.host) && host.permission)) {
2775
+ permission = this.host?.permission;
2776
+ if (!permission) {
2750
2777
  return true;
2751
2778
  }
2752
2779
  }
2753
2780
  else if (options.ownPermissionOnly) {
2754
- if (!(permission = this.#permission)) {
2781
+ permission = this.#permission;
2782
+ if (!permission) {
2755
2783
  return true;
2756
2784
  }
2757
2785
  ownPermissionOnly = true;
2758
2786
  }
2759
2787
  }
2760
- if (!(uri = asFile(uri))) {
2761
- return false;
2762
- }
2788
+ permission ??= this.permission;
2763
2789
  if (permission && (_a.isFile(uri, 'unc') ? permission.hasUNCRead(uri) || ownPermissionOnly && !permission.getUNCRead() : path.isAbsolute(uri = path.resolve(uri)) && (permission.hasDiskRead(uri) || ownPermissionOnly && !permission.getDiskRead()))) {
2764
2790
  return true;
2765
2791
  }
2766
2792
  return VALUES["permission.home_read"] && withinDir(uri, OS_HOMEDIR, true);
2767
2793
  }
2768
2794
  canWrite(uri, options) {
2769
- let permission = this.permission, ownPermissionOnly = false;
2795
+ uri = asFile(uri);
2796
+ if (!uri) {
2797
+ return false;
2798
+ }
2799
+ let permission, ownPermissionOnly = false;
2770
2800
  if (options) {
2771
2801
  if (options.hostPermissionOnly) {
2772
- let host;
2773
- if (!(permission = (host = this.host) && host.permission)) {
2802
+ permission = this.host?.permission;
2803
+ if (!permission) {
2774
2804
  return true;
2775
2805
  }
2776
2806
  }
2777
2807
  else if (options.ownPermissionOnly) {
2778
- if (!(permission = this.#permission)) {
2808
+ permission = this.#permission;
2809
+ if (!permission) {
2779
2810
  return true;
2780
2811
  }
2781
2812
  ownPermissionOnly = true;
2782
2813
  }
2783
2814
  }
2784
- if (!(uri = asFile(uri))) {
2785
- return false;
2786
- }
2815
+ permission ??= this.permission;
2787
2816
  if (permission && (_a.isFile(uri, 'unc') ? permission.hasUNCWrite(uri) || ownPermissionOnly && !permission.getUNCWrite() : path.isAbsolute(uri = path.resolve(uri)) && (permission.hasDiskWrite(uri) || ownPermissionOnly && !permission.getDiskWrite()))) {
2788
2817
  return true;
2789
2818
  }
@@ -2877,11 +2906,14 @@ class Module extends EventEmitter {
2877
2906
  this.writeFail(["Unable to read file", path.basename(asFile(src))], err, 32);
2878
2907
  }
2879
2908
  }
2880
- else if (promises) {
2881
- return Promise.reject(errorPermission(src));
2882
- }
2883
- else if ((0, types_1.isFunction)(callback)) {
2884
- callback(errorPermission(src));
2909
+ else {
2910
+ this.emit('file:permission', src, 'read');
2911
+ if (promises) {
2912
+ return Promise.reject(errorPermission(src));
2913
+ }
2914
+ if ((0, types_1.isFunction)(callback)) {
2915
+ callback(errorPermission(src));
2916
+ }
2885
2917
  }
2886
2918
  }
2887
2919
  writeFile(src, data, options = {}, callback) {
@@ -2913,12 +2945,15 @@ class Module extends EventEmitter {
2913
2945
  this.writeFail(["Unable to write file", path.basename(outSrc)], err, 32);
2914
2946
  }
2915
2947
  }
2916
- else if (promises) {
2917
- return Promise.reject(errorPermission(src));
2918
- }
2919
- else if ((0, types_1.isFunction)(callback)) {
2920
- callback(errorPermission(src));
2921
- return;
2948
+ else {
2949
+ this.emit('file:permission', src, 'write');
2950
+ if (promises) {
2951
+ return Promise.reject(errorPermission(src));
2952
+ }
2953
+ if ((0, types_1.isFunction)(callback)) {
2954
+ callback(errorPermission(src));
2955
+ return;
2956
+ }
2922
2957
  }
2923
2958
  return false;
2924
2959
  }
@@ -2946,7 +2981,7 @@ class Module extends EventEmitter {
2946
2981
  fs.unlinkSync(outSrc);
2947
2982
  }
2948
2983
  catch (err) {
2949
- if (!_a.isErrorCode(err, 'ENOENT')) {
2984
+ if (!(0, types_1.isErrorCode)(err, 'ENOENT')) {
2950
2985
  this.writeFail(["Unable to delete file", path.basename(outSrc)], err, 32);
2951
2986
  return false;
2952
2987
  }
@@ -2954,10 +2989,11 @@ class Module extends EventEmitter {
2954
2989
  this.emit('file:delete', outSrc, options);
2955
2990
  return true;
2956
2991
  }
2957
- else if (promises) {
2992
+ this.emit('file:permission', src, 'delete');
2993
+ if (promises) {
2958
2994
  return Promise.reject(errorPermission(src));
2959
2995
  }
2960
- else if ((0, types_1.isFunction)(callback)) {
2996
+ if ((0, types_1.isFunction)(callback)) {
2961
2997
  callback(errorPermission(src));
2962
2998
  return;
2963
2999
  }
@@ -3008,12 +3044,15 @@ class Module extends EventEmitter {
3008
3044
  }
3009
3045
  }
3010
3046
  }
3011
- else if (promises) {
3012
- return Promise.reject(errorPermission(src));
3013
- }
3014
- else if ((0, types_1.isFunction)(callback)) {
3015
- callback(errorPermission(!outSrc ? src : dest));
3016
- return;
3047
+ else {
3048
+ this.emit('file:permission', src, 'copy');
3049
+ if (promises) {
3050
+ return Promise.reject(errorPermission(src));
3051
+ }
3052
+ if ((0, types_1.isFunction)(callback)) {
3053
+ callback(errorPermission(!outSrc ? src : dest));
3054
+ return;
3055
+ }
3017
3056
  }
3018
3057
  return false;
3019
3058
  }
@@ -3083,6 +3122,7 @@ class Module extends EventEmitter {
3083
3122
  if (outSrc) {
3084
3123
  src = dest;
3085
3124
  }
3125
+ this.emit('file:permission', src, 'move');
3086
3126
  if (promises) {
3087
3127
  return Promise.reject(errorPermission(src));
3088
3128
  }
@@ -3149,12 +3189,15 @@ class Module extends EventEmitter {
3149
3189
  this.writeFail(["Unable to create directory", path.basename(outSrc)], err, 32);
3150
3190
  }
3151
3191
  }
3152
- else if (promises) {
3153
- return Promise.reject(errorPermission(src));
3154
- }
3155
- else if ((0, types_1.isFunction)(callback)) {
3156
- callback(errorPermission(src));
3157
- return;
3192
+ else {
3193
+ this.emit('dir:permission', src, 'create');
3194
+ if (promises) {
3195
+ return Promise.reject(errorPermission(src));
3196
+ }
3197
+ if ((0, types_1.isFunction)(callback)) {
3198
+ callback(errorPermission(src));
3199
+ return;
3200
+ }
3158
3201
  }
3159
3202
  return false;
3160
3203
  }
@@ -3165,7 +3208,7 @@ class Module extends EventEmitter {
3165
3208
  if (outSrc) {
3166
3209
  try {
3167
3210
  if (_a.isDir(outSrc)) {
3168
- const { emptyDir = false, recursive = true } = options;
3211
+ const { emptyDir = false, recursive } = options;
3169
3212
  if (promises || (0, types_1.isFunction)(callback)) {
3170
3213
  const result = new Promise((resolve, reject) => {
3171
3214
  const failed = tryRemoveDir(outSrc, emptyDir, !!recursive);
@@ -3176,12 +3219,12 @@ class Module extends EventEmitter {
3176
3219
  reject(errorAccess(failed));
3177
3220
  }
3178
3221
  })
3179
- .then(success => {
3222
+ .then(() => {
3180
3223
  this.emit('dir:remove', outSrc, options);
3181
3224
  if (callback) {
3182
3225
  callback(null);
3183
3226
  }
3184
- return success;
3227
+ return true;
3185
3228
  });
3186
3229
  if (callback) {
3187
3230
  result.catch((err) => {
@@ -3221,12 +3264,15 @@ class Module extends EventEmitter {
3221
3264
  this.writeFail(["Unable to remove directory", path.basename(outSrc)], err, 32);
3222
3265
  }
3223
3266
  }
3224
- else if (promises) {
3225
- return Promise.reject(errorPermission(src));
3226
- }
3227
- else if ((0, types_1.isFunction)(callback)) {
3228
- callback(errorPermission(src));
3229
- return;
3267
+ else {
3268
+ this.emit('dir:permission', src, 'remove');
3269
+ if (promises) {
3270
+ return Promise.reject(errorPermission(src));
3271
+ }
3272
+ if ((0, types_1.isFunction)(callback)) {
3273
+ callback(errorPermission(src));
3274
+ return;
3275
+ }
3230
3276
  }
3231
3277
  return false;
3232
3278
  }
@@ -3496,7 +3542,7 @@ class Module extends EventEmitter {
3496
3542
  options = value;
3497
3543
  value = undefined;
3498
3544
  }
3499
- if (name && isErrnoException(err) && (err.code === types_1.ERR_CODE.MODULE_NOT_FOUND || err.code === types_1.ERR_CODE.ERR_MODULE_NOT_FOUND)) {
3545
+ if (name && (0, types_1.isErrorCode)(err, types_1.ERR_CODE.MODULE_NOT_FOUND, types_1.ERR_CODE.ERR_MODULE_NOT_FOUND)) {
3500
3546
  if (typeof options === 'number') {
3501
3547
  options = undefined;
3502
3548
  }
@@ -3510,7 +3556,7 @@ class Module extends EventEmitter {
3510
3556
  return false;
3511
3557
  }
3512
3558
  checkFail(message, options) {
3513
- switch (options.code || isErrnoException(message) && message.code) {
3559
+ switch (options.code || message.code) {
3514
3560
  case types_1.ERR_CODE.MODULE_NOT_FOUND:
3515
3561
  case types_1.ERR_CODE.ERR_MODULE_NOT_FOUND:
3516
3562
  if (options.exec) {
@@ -3539,7 +3585,7 @@ class Module extends EventEmitter {
3539
3585
  break;
3540
3586
  }
3541
3587
  }
3542
- writeLog(type, value, timeStamp, duration, from, source) {
3588
+ writeLog(type, value, timeStamp, duration) {
3543
3589
  let queue = false;
3544
3590
  if ((0, types_1.isObject)(type)) {
3545
3591
  if (typeof value === 'boolean') {
@@ -3556,7 +3602,7 @@ class Module extends EventEmitter {
3556
3602
  writeLine(output);
3557
3603
  }
3558
3604
  }
3559
- this.addLog(type, value, timeStamp, duration, from, source);
3605
+ this.addLog(type, value, timeStamp, duration);
3560
3606
  }
3561
3607
  addLog(type, value, timeStamp, duration, from, source) {
3562
3608
  if (!this._logEnabled || SETTINGS.production && process.env.NODE_ENV === 'production' && (SETTINGS.production.includes(this.moduleName) || from && SETTINGS.production.includes(from))) {
@@ -3833,9 +3879,7 @@ class Module extends EventEmitter {
3833
3879
  this.abortable = value.willAbort(this);
3834
3880
  value.modules.add(this);
3835
3881
  if (aborting) {
3836
- value.signal.addEventListener('abort', this.#abortEvent = () => {
3837
- this.abort(new Error("Aborted by host"));
3838
- }, { once: true });
3882
+ value.signal.addEventListener('abort', this.#abortEvent = () => this.abort(new Error("Aborted by host")), { once: true });
3839
3883
  }
3840
3884
  }
3841
3885
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.12.9",
3
+ "version": "0.13.1",
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.9",
22
+ "@e-mc/types": "0.13.1",
23
23
  "chalk": "4.1.2",
24
- "file-type": "^18.7.0",
24
+ "file-type": "^20.5.0",
25
25
  "js-yaml": "^4.1.0",
26
- "mime-types": "^2.1.35",
26
+ "mime-types": "^3.0.1",
27
27
  "picomatch": "^4.0.3"
28
28
  }
29
29
  }