@e-mc/module 0.8.2 → 0.8.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 (3) hide show
  1. package/README.md +239 -3
  2. package/index.js +6 -14
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -1,7 +1,243 @@
1
- ### @e-mc/module
1
+ # @e-mc/module
2
2
 
3
- https://e-mc.readthedocs.io
3
+ * NodeJS 14
4
+ * ES2020
4
5
 
5
- ### LICENSE
6
+ ## General Usage
7
+
8
+ * [Read the Docs](https://e-mc.readthedocs.io)
9
+
10
+ ## Interface
11
+
12
+ - https://www.unpkg.com/@e-mc/types@0.8.4/lib/index.d.ts
13
+
14
+ ```typescript
15
+ import type { LogStatus } from "./squared";
16
+
17
+ import type { IHost } from "./index";
18
+ import type { IAbortComponent, IPermission } from "./core";
19
+ import type { ExecCommand, LOG_TYPE, LogArguments, LogComponent, LogDate, LogFailOptions, LogMessageOptions, LogOptions, LogProcessOptions, LogTime, LogType, LogValue, STATUS_TYPE, StatusType } from "./logger";
20
+ import type { AsHashOptions, CheckSemVerOptions, CopyDirOptions, CopyDirResult, CopyFileOptions, CreateDirOptions, DeleteFileOptions, GetTempDirOptions, MoveFileOptions, NormalizeFlags, ParseFunctionOptions, PermissionOptions, ProtocolType, ReadBufferOptions, ReadFileCallback, ReadFileOptions, ReadHashOptions, ReadTextOptions, RemoveDirOptions, WriteFileOptions } from "./module";
21
+ import type { Settings } from "./node";
22
+
23
+ import type { NoParamCallback } from "fs";
24
+ import type { SpawnOptions } from "child_process";
25
+ import type { BinaryLike } from "crypto";
26
+
27
+ import type * as EventEmitter from "events";
28
+
29
+ import type { FileTypeResult } from "file-type";
30
+
31
+ type BufferView = Buffer | string | NodeJS.ArrayBufferView;
32
+ type CpuUsage = NodeJS.CpuUsage;
33
+
34
+ interface IModule extends EventEmitter, IAbortComponent {
35
+ readonly status: LogStatus<StatusType>[];
36
+ readonly errors: unknown[];
37
+ supported(major: number, minor?: number, patch?: number, lts?: boolean): boolean;
38
+ supports(name: string, value?: boolean): boolean;
39
+ getTempDir(options: GetTempDirOptions): string;
40
+ getTempDir(uuidDir: boolean, createDir?: boolean): string;
41
+ getTempDir(pathname: string, createDir?: boolean): string;
42
+ getTempDir(uuidDir: boolean, filename: string, createDir?: boolean): string;
43
+ getTempDir(pathname?: string, filename?: string, createDir?: boolean): string;
44
+ canRead(uri: string, options?: PermissionOptions): boolean;
45
+ canWrite(uri: string, options?: PermissionOptions): boolean;
46
+ readFile(src: string): Buffer | undefined;
47
+ readFile(src: string, options?: ReadFileOptions): Promise<Buffer | string> | Buffer | string | undefined;
48
+ readFile(src: string, promises: true): Promise<Buffer | undefined>;
49
+ readFile(src: string, options: ReadFileOptions, promises: true): Promise<Buffer | string | undefined>;
50
+ readFile(src: string, callback: ReadFileCallback<Buffer>): Buffer | undefined;
51
+ readFile(src: string, options: ReadFileOptions, callback: ReadFileCallback<Buffer | string>): Buffer | string | undefined;
52
+ writeFile(src: string, data: BufferView, options?: WriteFileOptions): boolean;
53
+ writeFile(src: string, data: BufferView, promises: true): Promise<boolean>;
54
+ writeFile(src: string, data: BufferView, options: WriteFileOptions, promises: true): Promise<boolean>;
55
+ writeFile(src: string, data: BufferView, callback: NoParamCallback): void;
56
+ writeFile(src: string, data: BufferView, options: WriteFileOptions, callback: NoParamCallback): void;
57
+ deleteFile(src: string, options?: DeleteFileOptions): boolean;
58
+ deleteFile(src: string, promises: true): Promise<boolean>;
59
+ deleteFile(src: string, options: DeleteFileOptions, promises: true): Promise<boolean>;
60
+ deleteFile(src: string, callback: NoParamCallback): void;
61
+ deleteFile(src: string, options: DeleteFileOptions, callback: NoParamCallback): void;
62
+ copyFile(src: string, dest: string, options?: CopyFileOptions): boolean;
63
+ copyFile(src: string, dest: string, promises: true): Promise<boolean>;
64
+ copyFile(src: string, dest: string, options: CopyFileOptions, promises: true): Promise<boolean>;
65
+ copyFile(src: string, dest: string, callback: NoParamCallback): void;
66
+ copyFile(src: string, dest: string, options: CopyFileOptions, callback: NoParamCallback): void;
67
+ moveFile(src: string, dest: string, options?: MoveFileOptions): boolean;
68
+ moveFile(src: string, dest: string, promises: true): Promise<boolean>;
69
+ moveFile(src: string, dest: string, options: MoveFileOptions, promises: true): Promise<boolean>;
70
+ moveFile(src: string, dest: string, callback: NoParamCallback): void;
71
+ moveFile(src: string, dest: string, options: MoveFileOptions, callback: NoParamCallback): void;
72
+ createDir(src: string, options?: CreateDirOptions): boolean;
73
+ createDir(src: string, promises: true): Promise<boolean>;
74
+ createDir(src: string, options: CreateDirOptions, promises: true): Promise<boolean>;
75
+ createDir(src: string, callback: NoParamCallback): void;
76
+ createDir(src: string, options: CreateDirOptions, callback: NoParamCallback): void;
77
+ removeDir(src: string, options?: RemoveDirOptions): boolean;
78
+ removeDir(src: string, promises: true): Promise<boolean>;
79
+ removeDir(src: string, options: RemoveDirOptions, promises: true): Promise<boolean>;
80
+ removeDir(src: string, callback: NoParamCallback): void;
81
+ removeDir(src: string, options: RemoveDirOptions, callback: NoParamCallback): void;
82
+ allSettled(values: readonly PromiseLike<unknown>[], rejected?: LogValue, options?: LogFailOptions | LogType): Promise<PromiseFulfilledResult<unknown>[]>;
83
+ formatMessage(type: LogType, title: string, value: LogValue, message?: unknown, options?: LogMessageOptions): void;
84
+ formatFail(type: LogType, title: string, value: LogValue, message?: unknown, options?: LogFailOptions): void;
85
+ writeFail(value: LogValue, message?: unknown, options?: LogFailOptions | LogType): void;
86
+ writeTimeProcess(title: string, value: string, startTime: LogTime, options?: LogProcessOptions): void;
87
+ writeTimeElapsed(title: string, value: LogValue, startTime: LogTime, options?: LogMessageOptions): void;
88
+ checkPackage(err: unknown, name: string | undefined, options?: LogFailOptions | LogType): boolean;
89
+ checkPackage(err: unknown, name: string | undefined, value?: LogValue, options?: LogFailOptions | LogType): boolean;
90
+ checkFail(message: unknown, options: LogFailOptions): LogArguments | false | undefined;
91
+ writeLog(component: LogComponent, queue?: boolean): void;
92
+ writeLog(type: StatusType, value: unknown, options?: LogOptions): void;
93
+ writeLog(type: StatusType, value: unknown, timeStamp?: LogDate, duration?: number): void;
94
+ addLog(component: LogComponent, queue?: boolean): void;
95
+ addLog(type: StatusType, value: unknown, from: string, source?: string): void;
96
+ addLog(type: StatusType, value: unknown, options?: LogOptions): void;
97
+ addLog(type: StatusType, value: unknown, timeStamp?: LogDate, from?: string, source?: string): void;
98
+ addLog(type: StatusType, value: unknown, timeStamp?: LogDate, duration?: number, from?: string, source?: string): void;
99
+ getLog(...type: StatusType[]): LogStatus<StatusType>[];
100
+ flushLog(): void;
101
+ willAbort(value: unknown): boolean;
102
+ hasOwnPermission(): boolean;
103
+ isFatal(err?: unknown): boolean;
104
+ detach(): void;
105
+ reset(): void;
106
+ get moduleName(): string;
107
+ set host(value);
108
+ get host(): IHost | null;
109
+ set permission(value);
110
+ get permission(): IPermission | null;
111
+ get aborted(): boolean;
112
+ set abortable(value);
113
+ get abortable(): boolean;
114
+ get threadable(): boolean;
115
+ set sessionId(value);
116
+ get sessionId(): string;
117
+ set broadcastId(value);
118
+ get broadcastId(): string | string[];
119
+ get logType(): LOG_TYPE;
120
+ set logLevel(value: number | string);
121
+ get logLevel(): number;
122
+ get statusType(): STATUS_TYPE;
123
+ set tempDir(value);
124
+ get tempDir(): string;
125
+
126
+ /* EventEmitter */
127
+ on(event: "exec", listener: (command: ExecCommand, options?: SpawnOptions) => void): this;
128
+ on(event: "error", listener: (err: Error) => void): this;
129
+ on(event: "file:read", listener: (src: string, data: Buffer | string, options?: ReadFileOptions) => void): this;
130
+ on(event: "file:write", listener: (src: string, options?: WriteFileOptions) => void): this;
131
+ on(event: "file:delete", listener: (src: string, options?: DeleteFileOptions) => void): this;
132
+ on(event: "file:copy", listener: (dest: string, options?: CopyFileOptions) => void): this;
133
+ on(event: "file:move", listener: (dest: string, options?: MoveFileOptions) => void): this;
134
+ on(event: "dir:create", listener: (src: string, options?: CreateDirOptions) => void): this;
135
+ on(event: "dir:remove", listener: (src: string, options?: RemoveDirOptions) => void): this;
136
+ once(event: "exec", listener: (command: ExecCommand, options?: SpawnOptions) => void): this;
137
+ once(event: "error", listener: (err: Error) => void): this;
138
+ once(event: "file:read", listener: (src: string, data: Buffer | string, options?: ReadFileOptions) => void): this;
139
+ once(event: "file:write", listener: (src: string, options?: WriteFileOptions) => void): this;
140
+ once(event: "file:delete", listener: (src: string, options?: DeleteFileOptions) => void): this;
141
+ once(event: "file:copy", listener: (dest: string, options?: CopyFileOptions) => void): this;
142
+ once(event: "file:move", listener: (dest: string, options?: MoveFileOptions) => void): this;
143
+ once(event: "dir:create", listener: (src: string, options?: CreateDirOptions) => void): this;
144
+ once(event: "dir:remove", listener: (src: string, options?: RemoveDirOptions) => void): this;
145
+ emit(event: "exec", command: ExecCommand, options?: SpawnOptions): boolean;
146
+ emit(event: "error", err: Error): boolean;
147
+ emit(event: "file:read", src: string, data: Buffer | string, options?: ReadFileOptions): boolean;
148
+ emit(event: "file:write", src: string, options?: WriteFileOptions): boolean;
149
+ emit(event: "file:delete", src: string, options?: DeleteFileOptions): boolean;
150
+ emit(event: "file:copy", dest: string, options?: CopyFileOptions): boolean;
151
+ emit(event: "file:move", dest: string, options?: MoveFileOptions): boolean;
152
+ emit(event: "dir:create", src: string, options?: CreateDirOptions): boolean;
153
+ emit(event: "dir:remove", src: string, options?: RemoveDirOptions): boolean;
154
+ }
155
+
156
+ interface ModuleConstructor {
157
+ PROCESS_TIMEOUT: number;
158
+ LOG_STYLE_FAIL: LogMessageOptions;
159
+ LOG_STYLE_SUCCESS: LogMessageOptions;
160
+ LOG_STYLE_INFO: LogMessageOptions;
161
+ LOG_STYLE_WARN: LogMessageOptions;
162
+ LOG_STYLE_NOTICE: LogMessageOptions;
163
+ LOG_STYLE_REVERSE: LogMessageOptions;
164
+ readonly VERSION: string;
165
+ readonly LOG_TYPE: LOG_TYPE;
166
+ readonly STATUS_TYPE: STATUS_TYPE;
167
+ readonly MAX_TIMEOUT: number;
168
+ readonly TEMP_DIR: string;
169
+ supported(major: number, minor?: number, patch?: number, lts?: boolean): boolean;
170
+ formatMessage(type: LogType, title: string, value: LogValue, message?: unknown, options?: LogMessageOptions): void;
171
+ writeFail(value: LogValue, message?: unknown, options?: LogFailOptions | LogType): void;
172
+ enabled(key: string, username?: string): boolean;
173
+ parseFunction(value: unknown, options?: ParseFunctionOptions): ((...args: unknown[]) => Promise<unknown> | unknown) | null;
174
+ parseFunction(value: unknown, absolute: boolean, sync?: boolean): ((...args: unknown[]) => Promise<unknown> | unknown) | null;
175
+ asString(value: unknown, cacheKey?: boolean | "throws"): string;
176
+ asHash(data: BinaryLike, options: AsHashOptions): string;
177
+ asHash(data: BinaryLike, minLength: number): string;
178
+ asHash(data: BinaryLike, algorithm?: number | string | AsHashOptions, minLength?: number | AsHashOptions): string;
179
+ readHash(value: string | URL, options?: ReadHashOptions): Promise<string>;
180
+ toPosix(value: unknown, normalize: boolean): string;
181
+ toPosix(value: unknown, filename?: string, normalize?: boolean): string;
182
+ hasLogType(value: LogType): boolean;
183
+ isURL(value: string, ...exclude: string[]): boolean;
184
+ isFile(value: string | URL, type?: ProtocolType): boolean;
185
+ isDir(value: string | URL): boolean;
186
+ isPath(value: string | URL, type?: "unc" | "unc-exists"): boolean;
187
+ isPath(value: string | URL, isFile?: boolean): boolean;
188
+ isErrorCode(err: unknown, ...code: string[]): boolean;
189
+ fromLocalPath(value: string): string;
190
+ resolveFile(value: string): string;
191
+ resolvePath(value: string, base: string | URL): string;
192
+ joinPath(...values: [...paths: unknown[], normalize: boolean][]): string;
193
+ joinPath(...values: unknown[]): string;
194
+ normalizePath(value: unknown, flags?: boolean | NormalizeFlags): string;
195
+ createDir(value: string | URL, overwrite?: boolean): boolean;
196
+ removeDir(value: string | URL, sinceCreated: number, recursive?: boolean): boolean;
197
+ removeDir(value: string | URL, empty?: boolean, recursive?: boolean): boolean;
198
+ copyDir(src: string | URL, dest: string | URL, options: CopyDirOptions): Promise<CopyDirResult>;
199
+ copyDir(src: string | URL, dest: string | URL, move?: boolean, recursive?: boolean): Promise<CopyDirResult>;
200
+ renameFile(src: string | URL, dest: string | URL, throws?: boolean): boolean;
201
+ streamFile(src: string, cache: boolean): Promise<Buffer | string>;
202
+ streamFile(src: string, options: ReadBufferOptions): Promise<Buffer | string>;
203
+ streamFile(src: string, cache?: boolean | ReadBufferOptions, options?: ReadBufferOptions): Promise<Buffer | string>;
204
+ readText(value: string | URL, cache: boolean): string;
205
+ readText(value: string | URL, options: ReadTextOptions): Promise<string> | string;
206
+ readText(value: string | URL, encoding?: BufferEncoding | boolean | ReadTextOptions, cache?: boolean): string;
207
+ readBuffer(value: string | URL, options: ReadBufferOptions): Promise<Buffer | null> | Buffer | null;
208
+ readBuffer(value: string | URL, cache?: boolean): Buffer | null;
209
+ resolveMime(data: string | Buffer | Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;
210
+ lookupMime(value: string, extension?: boolean): string;
211
+ initCpuUsage(instance?: IModule): CpuUsage;
212
+ getCpuUsage(start: CpuUsage, format: true): string;
213
+ getCpuUsage(start: CpuUsage, format?: boolean): number;
214
+ getMemUsage(format: true): string;
215
+ getMemUsage(format?: boolean): number;
216
+ formatCpuMem(start: CpuUsage, all?: boolean): string;
217
+ getPackageVersion(name: string | [string, string], startDir: string): string;
218
+ getPackageVersion(name: string | [string, string], unstable?: boolean, startDir?: string): string;
219
+ checkSemVer(name: string | [string, string], options: CheckSemVerOptions): boolean;
220
+ checkSemVer(name: string | [string, string], min: number | string, max: number | string, options: CheckSemVerOptions): boolean;
221
+ checkSemVer(name: string | [string, string], min: number | string, max?: number | string, unstable?: boolean, startDir?: string): boolean;
222
+ sanitizeCmd(value: string): string;
223
+ sanitizeArgs(value: string, doubleQuote?: boolean): string;
224
+ sanitizeArgs(values: string[], doubleQuote?: boolean): string[];
225
+ purgeMemory(percent: number, parent: boolean | number): Promise<number>;
226
+ purgeMemory(percent?: number, limit?: number, parent?: boolean | number): Promise<number>;
227
+ canWrite(name: "temp" | "home"): boolean;
228
+ loadSettings(settings: Settings, password?: string): boolean;
229
+ readonly prototype: IModule<IHost>;
230
+ new(): IModule<IHost>;
231
+ }
232
+ ```
233
+
234
+ ## References
235
+
236
+ - https://www.unpkg.com/@e-mc/types@0.8.4/lib/core.d.ts
237
+ - https://www.unpkg.com/@e-mc/types@0.8.4/lib/logger.d.ts
238
+ - https://www.unpkg.com/@e-mc/types@0.8.4/lib/module.d.ts
239
+ - https://www.unpkg.com/@e-mc/types@0.8.4/lib/node.d.ts
240
+
241
+ ## LICENSE
6
242
 
7
243
  BSD 3-Clause
package/index.js CHANGED
@@ -623,7 +623,7 @@ class Module extends EventEmitter {
623
623
  this[_f] = new AbortController();
624
624
  this[_g] = null;
625
625
  }
626
- static get VERSION() { return "0.8.2" /* INTERNAL.VERSION */; }
626
+ static get VERSION() { return "0.8.4" /* INTERNAL.VERSION */; }
627
627
  static get LOG_TYPE() { return types_1.LOG_TYPE; }
628
628
  static get STATUS_TYPE() { return types_1.STATUS_TYPE; }
629
629
  static get MAX_TIMEOUT() { return 2147483647; }
@@ -734,7 +734,7 @@ class Module extends EventEmitter {
734
734
  }
735
735
  let messageUnitIndent = options.messageUnitIndent, coloring;
736
736
  if (!broadcastId) {
737
- (0, types_1.setLogCurrent)({ type, title, value, message });
737
+ (0, types_1.setLogCurrent)({ type, title, value, message, sessionId });
738
738
  coloring = !(options.useColor === false || SETTINGS.color === false);
739
739
  }
740
740
  else if ((0, types_1.isPlainObject)(broadcastId)) {
@@ -1184,10 +1184,7 @@ class Module extends EventEmitter {
1184
1184
  const { algorithm, digest, chunkSize = 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */ } = options;
1185
1185
  const hash = crypto.createHash(algorithm || "sha256" /* HASH_OUTPUT.ALGORITHM */);
1186
1186
  try {
1187
- let minStreamSize = options.minStreamSize || 0;
1188
- if ((0, types_1.isString)(minStreamSize)) {
1189
- minStreamSize = (0, types_1.formatSize)(minStreamSize);
1190
- }
1187
+ const minStreamSize = (0, types_1.alignSize)(options.minStreamSize);
1191
1188
  if (fs.statSync(value).size <= Math.min(minStreamSize > 0 ? minStreamSize : Infinity, 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */)) {
1192
1189
  return hash.update(fs.readFileSync(value)).digest(digest || "hex" /* HASH_OUTPUT.DIGEST */);
1193
1190
  }
@@ -1623,12 +1620,9 @@ class Module extends EventEmitter {
1623
1620
  ({ minStreamSize = 0, encoding, signal, cache } = options);
1624
1621
  }
1625
1622
  return new Promise(async (resolve) => {
1626
- if ((0, types_1.isString)(minStreamSize)) {
1627
- minStreamSize = (0, types_1.formatSize)(minStreamSize);
1628
- }
1629
1623
  const fileSize = fs.statSync(src).size;
1630
1624
  let data;
1631
- if (fileSize >= minStreamSize || fileSize >= 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */) {
1625
+ if (fileSize >= (0, types_1.alignSize)(minStreamSize) || fileSize >= 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */) {
1632
1626
  const readable = fs.createReadStream(src, { signal });
1633
1627
  const chunks = [];
1634
1628
  for await (const chunk of readable) {
@@ -2567,10 +2561,8 @@ class Module extends EventEmitter {
2567
2561
  result || (result = (encoding ? getCacheItem(CACHE_READTEXT, outSrc) : getCacheItem(CACHE_READBUFFER, outSrc)));
2568
2562
  }
2569
2563
  try {
2570
- if ((0, types_1.isString)(minStreamSize)) {
2571
- minStreamSize = (0, types_1.formatSize)(minStreamSize);
2572
- }
2573
- if (typeof minStreamSize === 'number' || promises && fs.statSync(outSrc).size >= 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */) {
2564
+ minStreamSize = (0, types_1.alignSize)(minStreamSize);
2565
+ if (!isNaN(minStreamSize) || promises && fs.statSync(outSrc).size >= 2097152000 /* HASH_OUTPUT.CHUNK_SIZE */) {
2574
2566
  if (result) {
2575
2567
  return Promise.resolve(result);
2576
2568
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/module",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "description": "Module base class for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "https://github.com/anpham6/e-mc.git",
12
+ "url": "git+https://github.com/anpham6/e-mc.git",
13
13
  "directory": "src/module"
14
14
  },
15
15
  "keywords": [
@@ -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.8.2",
23
+ "@e-mc/types": "0.8.4",
24
24
  "abort-controller": "^3.0.0",
25
25
  "chalk": "4.1.2",
26
26
  "event-target-shim": "^5.0.1",