@e-mc/file-manager 0.8.7 → 0.9.0

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 (5) hide show
  1. package/LICENSE +10 -10
  2. package/README.md +149 -36
  3. package/index.d.ts +5 -5
  4. package/index.js +594 -270
  5. package/package.json +12 -11
package/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Copyright 2024 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2024 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @e-mc/file-manager
2
2
 
3
- * NodeJS 14
3
+ * NodeJS 14/16
4
4
  * ES2020
5
5
 
6
6
  ## General Usage
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/index.d.ts
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.0/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { DataSource, IncrementalMatch, TaskAction } from "./squared";
@@ -17,7 +17,7 @@ import type { DataSource, IncrementalMatch, TaskAction } from "./squared";
17
17
  import type { DocumentConstructor, HostConstructor, ICloud, ICompress, IDocument, IHost, IImage, IModule, IRequest, ITask, ImageConstructor, TaskConstructor, WatchInstance } from "./index";
18
18
  import type { ExternalAsset, FileCommand, FileData, IFileThread, OutputFinalize } from "./asset";
19
19
  import type { IPermission, PermissionReadWrite } from "./core";
20
- import type { AssetContentOptions, ChecksumOptions, DeleteFileAddendum, FileOutput, FinalizeResult, FindAssetOptions, IHttpDiskCache, IHttpMemoryCache, InstallData, PostFinalizeCallback, ReplaceOptions } from "./filemanager";
20
+ import type { AssetContentOptions, ChecksumOptions, DeleteFileAddendum, FileOutput, FinalizeResult, FindAssetOptions, IHttpDiskCache, IHttpMemoryCache, ImageMimeMap, InstallData, PostFinalizeCallback, ReplaceOptions } from "./filemanager";
21
21
  import type { ExecCommand } from "./logger";
22
22
  import type { CopyFileOptions, CreateDirOptions, DeleteFileOptions, MoveFileOptions, ReadFileOptions, RemoveDirOptions, WriteFileOptions } from "./module";
23
23
  import type { RequestData, Settings } from "./node";
@@ -28,14 +28,13 @@ import type { SpawnOptions } from "child_process";
28
28
  import type { NoParamCallback } from "fs";
29
29
 
30
30
  interface IFileManager extends IHost, Set<string> {
31
- finalizeState: number;
32
31
  processTimeout: number;
33
32
  cacheToDisk: IHttpDiskCache<ExternalAsset>;
34
33
  cacheToMemory: IHttpMemoryCache<ExternalAsset>;
35
34
  Request: IRequest<RequestModule>;
36
35
  Document: InstallData<IDocument<IFileManager, ExternalAsset>, DocumentConstructor<IFileManager, ExternalAsset>>[];
37
36
  Task: InstallData<ITask, TaskConstructor>[];
38
- Image: Map<string, InstallData<IImage, ImageConstructor>> | null;
37
+ Image: Map<ImageMimeMap> | null;
39
38
  Cloud: ICloud | null;
40
39
  Watch: WatchInstance<ExternalAsset> | null;
41
40
  Compress: ICompress<CompressModule> | null;
@@ -52,31 +51,34 @@ interface IFileManager extends IHost, Set<string> {
52
51
  readonly fetchedAssets: ExternalAsset[];
53
52
  readonly copiedAssets: ExternalAsset[];
54
53
  readonly emptyDir: Set<string>;
55
- install(name: "document", target: DocumentConstructor<IFileManager, ExternalAsset>, module?: DocumentModule): IDocument<IFileManager, ExternalAsset> | undefined;
56
- install(name: "task", target: TaskConstructor, module?: TaskModule): ITask | undefined;
57
- install(name: "cloud", handler: string, module?: CloudModule): ICloud | undefined;
58
- install(name: "cloud", module?: CloudModule): ICloud | undefined;
59
- install(name: "db", module?: DbModule): IDb | undefined;
54
+ install(name: "document", handler: string, module?: DocumentModule, ...args: unknown[]): IDocument | undefined;
55
+ install(name: "document", target: DocumentConstructor, module?: DocumentModule, ...args: unknown[]): IDocument | undefined;
56
+ install(name: "task", handler: string, module?: TaskModule, ...args: unknown[]): ITask | undefined;
57
+ install(name: "task", target: TaskConstructor, module?: TaskModule, ...args: unknown[]): ITask | undefined;
58
+ install(name: "cloud", handler: string, module?: CloudModule, ...args: unknown[]): ICloud | undefined;
59
+ install(name: "cloud", module?: CloudModule, ...args: unknown[]): ICloud | undefined;
60
+ install(name: "image", handler: string, module?: ImageModule, ...args: unknown[]): IImage | undefined;
61
+ install(name: "image", target: ImageConstructor, module?: ImageModule, ...args: unknown[]): IImage | undefined;
60
62
  install(name: "image", targets: Map<string, ImageConstructor>, module?: ImageModule): void;
61
63
  install(name: "watch", module: WatchModule): WatchInstance<ExternalAsset> | undefined;
62
64
  install(name: "watch", interval?: number | string, port?: number | string, securePort?: number | string, extensions?: unknown[]): WatchInstance<ExternalAsset> | undefined;
63
65
  install(name: "compress", module?: CompressModule): ICompress<CompressModule> | undefined;
64
66
  install(name: string, ...args: unknown[]): IModule | undefined;
65
67
  using(...items: ExternalAsset[] | [boolean, ...ExternalAsset[]]): this;
66
- contains(item: ExternalAsset, condition?: (...args: [ExternalAsset]) => boolean): boolean;
68
+ contains(item: ExternalAsset, condition?: (target: ExternalAsset) => boolean): boolean;
67
69
  removeCwd(value: unknown): string;
68
70
  findAsset(value: string | URL, instance?: IModule | FindAssetOptions<ExternalAsset>): ExternalAsset | undefined;
69
71
  removeAsset(file: ExternalAsset): boolean;
70
- replace(file: ExternalAsset, replaceWith: string, options: ReplaceOptions): boolean;
71
- replace(file: ExternalAsset, replaceWith: string, mimeType?: string | ReplaceOptions): boolean;
72
+ replace(file: ExternalAsset, replaceWith: string, mimeType: Undef<string>): boolean;
73
+ replace(file: ExternalAsset, replaceWith: string, options?: ReplaceOptions): boolean;
72
74
  rename(file: ExternalAsset, value: string): boolean;
73
75
  performAsyncTask(): void;
74
76
  removeAsyncTask(): void;
75
77
  completeAsyncTask(err?: unknown, uri?: string, parent?: ExternalAsset, type?: number): void;
76
78
  performFinalize(override?: boolean): void;
77
79
  hasDocument(instance: IModule, document: string | string[] | undefined): boolean;
78
- getDocumentAssets(instance: IModule, condition?: (...args: [ExternalAsset]) => boolean): ExternalAsset[];
79
- getDataSourceItems(instance: IModule, condition?: (...args: [DataSource]) => boolean): DataSource[];
80
+ getDocumentAssets(instance: IModule, condition?: (target: ExternalAsset) => boolean): ExternalAsset[];
81
+ getDataSourceItems(instance: IModule, condition?: (target: DataSource) => boolean): DataSource[];
80
82
  setLocalUri(file: ExternalAsset, replace?: boolean): FileOutput;
81
83
  getLocalUri(data: FileData<ExternalAsset>): string;
82
84
  getMimeType(data: FileData<ExternalAsset>): string;
@@ -86,14 +88,17 @@ interface IFileManager extends IHost, Set<string> {
86
88
  removeProcessTimeout(instance: IModule, file: ExternalAsset): void;
87
89
  getProcessTimeout(handler: InstallData): number;
88
90
  clearProcessTimeout(): void;
91
+ scheduleTask(url: string | URL, data: unknown, priority: number): Promise<unknown>;
92
+ scheduleTask(url: string | URL, data: unknown, thenCallback?: (...args: unknown[]) => unknown, catchCallback?: (...args: unknown[]) => unknown, priority?: number): Promise<unknown>;
93
+ setTaskLimit(value: number): void;
89
94
  addDownload(value: number | Buffer | string, encoding: BufferEncoding): number;
90
- addDownload(value: number | Buffer | string, type?: number, encoding?: BufferEncoding): number;
95
+ addDownload(value: number | Buffer | string, type?: number | BufferEncoding, encoding?: BufferEncoding): number;
91
96
  getDownload(type?: number): [number, number];
92
- transformAsset(data: IFileThread, parent?: ExternalAsset): Promise<void>;
97
+ transformAsset(data: IFileThread, parent?: ExternalAsset, override?: boolean): Promise<boolean>;
93
98
  addCopy(data: FileCommand<ExternalAsset>, saveAs?: string, replace?: boolean): string | undefined;
94
99
  findMime(file: ExternalAsset, rename?: boolean): Promise<string>;
95
100
  getUTF8String(file: ExternalAsset, uri?: string): string;
96
- getBuffer(file: ExternalAsset, minStreamSize?: number): Promise<Buffer> | Buffer | null;
101
+ getBuffer(file: ExternalAsset, minStreamSize?: number): Promise<Buffer | null> | Buffer | null;
97
102
  getCacheDir(url: string | URL, createDir?: boolean): string;
98
103
  setAssetContent(file: ExternalAsset, content: string, options?: AssetContentOptions): string;
99
104
  getAssetContent(file: ExternalAsset, content?: string): string | undefined;
@@ -101,10 +106,11 @@ interface IFileManager extends IHost, Set<string> {
101
106
  writeImage(document: string | string[], output: OutputFinalize<ExternalAsset>): boolean;
102
107
  compressFile(file: ExternalAsset, overwrite?: boolean): Promise<unknown>;
103
108
  fetchObject(uri: string | URL, format: BufferFormat): Promise<object | null>;
104
- fetchObject(uri: string | URL, options?: OpenOptions | BufferFormat): Promise<object | null>;
109
+ fetchObject(uri: string | URL, options?: OpenOptions): Promise<object | null>;
105
110
  fetchBuffer(uri: string | URL, options?: OpenOptions): Promise<Buffer | string | null>;
106
111
  fetchFiles(uri: string | URL, pathname: string): Promise<string[]>;
107
- fetchFiles(uri: string | URL, options?: Aria2Options | string): Promise<string[]>;
112
+ fetchFiles(uri: string | URL, options?: Aria2Options): Promise<string[]>;
113
+ updateProgress(name: "request", id: number | string, receivedBytes: number, totalBytes: number, dataTime?: HighResolutionTime): void;
108
114
  start(emptyDir?: boolean): Promise<FinalizeResult>;
109
115
  processAssets(emptyDir?: boolean, using?: ExternalAsset[]): void;
110
116
  deleteFile(src: string, promises: boolean): Promise<void>;
@@ -130,6 +136,8 @@ interface IFileManager extends IHost, Set<string> {
130
136
  get delayed(): number;
131
137
  set cleared(value);
132
138
  get cleared(): boolean;
139
+ set finalizeState(value);
140
+ get finalizeState(): number;
133
141
 
134
142
  /* Set */
135
143
  add(value: string, parent?: ExternalAsset, type?: number): this;
@@ -170,36 +178,141 @@ interface IFileManager extends IHost, Set<string> {
170
178
  }
171
179
 
172
180
  interface FileManagerConstructor extends HostConstructor {
181
+ purgeMemory(percent?: number, limit?: number | boolean, parent?: number | boolean): Promise<number>;
173
182
  loadSettings(settings: Settings, password?: string): boolean;
174
183
  loadSettings(settings: Settings, permission?: PermissionReadWrite, password?: string): boolean;
175
184
  sanitizeAssets(assets: ExternalAsset[], exclusions?: string[]): ExternalAsset[];
176
- writeChecksum(root: string, options?: ChecksumOptions): Promise<string[]>;
177
- writeChecksum(root: string, to: string | undefined, options?: ChecksumOptions): Promise<string[] | null>;
178
- writeChecksum(root: string, to?: string | ChecksumOptions, options?: ChecksumOptions): Promise<string[] | null>;
179
- verifyChecksum(root: string, options?: ChecksumOptions): Promise<[string[], string[]] | null>;
180
- verifyChecksum(root: string, from: string | undefined, options?: ChecksumOptions): Promise<[string[], string[]] | null>;
181
- verifyChecksum(root: string, from?: string | ChecksumOptions, options?: ChecksumOptions): Promise<[string[], string[]] | null>;
185
+ writeChecksum(root: string, options: ChecksumOptions): Promise<string[]>;
186
+ writeChecksum(root: string, to?: string, options?: ChecksumOptions): Promise<string[] | null>;
187
+ verifyChecksum(root: string, options: ChecksumOptions): Promise<[string[], string[], number] | null>;
188
+ verifyChecksum(root: string, from?: string, options?: ChecksumOptions): Promise<[string[], string[], number] | null>;
182
189
  createFileThread(host: IFileManager, file: ExternalAsset): IFileThread;
183
190
  setTimeout(options: Record<string, number | string>): void;
184
191
  defineHttpCache(options: HttpMemorySettings, disk?: boolean): void;
185
192
  defineHttpConnect(options: HttpConnectSettings): void;
186
193
  readonly prototype: IFileManager;
187
194
  new(baseDirectory: string, config: RequestData, postFinalize?: PostFinalizeCallback): IFileManager;
188
- new(baseDirectory: string, config: RequestData, permission?: IPermission, postFinalize?: PostFinalizeCallback): IFileManager;
195
+ new(baseDirectory: string, config: RequestData, permission?: IPermission | null, postFinalize?: PostFinalizeCallback): IFileManager;
189
196
  }
190
197
  ```
191
198
 
199
+ ## Settings
200
+
201
+ ```typescript
202
+ interface ProcessModule {
203
+ thread?: {
204
+ sub_limit?: number;
205
+ };
206
+ }
207
+
208
+ interface RequestModule {
209
+ timeout?: number | string;
210
+ disk?: {
211
+ enabled?: boolean;
212
+ expires?: number | string;
213
+ limit?: number | string;
214
+ include?: string[];
215
+ exclude?: string[];
216
+ };
217
+ buffer?: {
218
+ enabled?: boolean;
219
+ expires?: number | string;
220
+ limit?: number | string;
221
+ include?: string[];
222
+ exclude?: string[];
223
+ limit_all?: number | string;
224
+ purge_amount?: number | string;
225
+ to_disk?: number | string | [number | string, (number | string)?];
226
+ };
227
+ connect?: {
228
+ timeout?: number | string;
229
+ retry_wait?: number | string;
230
+ retry_after?: number | string;
231
+ retry_limit?: number;
232
+ redirect_limit?: number;
233
+ };
234
+ }
235
+
236
+ interface ErrorModule {
237
+ recursion_limit?: number;
238
+ }
239
+
240
+ interface LoggerModule {
241
+ session_id?: boolean | number;
242
+ }
243
+ ```
244
+
245
+ ### Example usage
246
+
247
+ ```javascript
248
+ const FileManager = require("@e-mc/file-manager");
249
+
250
+ FileManager.loadSettings({ // Global
251
+ process: {
252
+ thread: { sub_limit: 16 }
253
+ },
254
+ request: {
255
+ timeout: "15s",
256
+ disk: {
257
+ enabled: true,
258
+ limit: "1gb", // Content-Length
259
+ expires: "1d",
260
+ exclude: ["https://github.com", "zip"]
261
+ },
262
+ buffer: {
263
+ enabled: true,
264
+ limit: "64mb",
265
+ limit_all: "512mb",
266
+ expires: "1h",
267
+ purge_amount: 0.25 // When limit_all exceeded
268
+ }
269
+ },
270
+ permission: {
271
+ disk_read: ["**/*"],
272
+ disk_write: ["/tmp/**"]
273
+ }
274
+ });
275
+
276
+ const assets = [
277
+ { pathname: "output", filename: "image1.png", uri: "http://hostname/path/document1.png" },
278
+ { pathname: "output", filename: "image2.png", uri: "http://hostname/path/document2.png" }
279
+ ];
280
+
281
+ const instance = new FileManager("/path/workspace", { assets, incremental: "etag" }, { disk_write: ["/path/workspace/output/**"] });
282
+ await instance.start();
283
+ ```
284
+
285
+ NOTE: **FileManager** is a sub-class of [Host](https://www.npmjs.com/package/@e-mc/core) and [Module](https://www.npmjs.com/package/@e-mc/module). Their ``loadSettings`` will be called as well which uses a combined [Settings](https://e-mc.readthedocs.io/en/latest/references.html#references-e-mc-types-lib-node) object.
286
+
287
+ ## NodeJS 14 LTS
288
+
289
+ Any optional fail safe dependencies were removed as of `E-mc 0.9`. The code itself will still be *ES2020* and will continue to work equivalently when self-installing these dependencies:
290
+
291
+ ### Under 15.4 + 16.0
292
+
293
+ ```sh
294
+ npm i abort-controller event-target-shim
295
+ ```
296
+
297
+ ### Under 14.17 + 15.6
298
+
299
+ ```sh
300
+ npm i uuid
301
+ ```
302
+
192
303
  ## References
193
304
 
194
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/squared.d.ts
195
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/asset.d.ts
196
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/core.d.ts
197
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/filemanager.d.ts
198
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/logger.d.ts
199
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/module.d.ts
200
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/node.d.ts
201
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/request.d.ts
202
- - https://www.unpkg.com/@e-mc/types@0.8.7/lib/settings.d.ts
305
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/squared.d.ts
306
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/asset.d.ts
307
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/core.d.ts
308
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/filemanager.d.ts
309
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/logger.d.ts
310
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/module.d.ts
311
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/node.d.ts
312
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/request.d.ts
313
+ - https://www.unpkg.com/@e-mc/types@0.9.0/lib/settings.d.ts
314
+
315
+ * https://www.npmjs.com/package/@types/node
203
316
 
204
317
  ## LICENSE
205
318
 
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { FileManagerConstructor } from '../types/lib';
2
- import type { ExternalAsset } from '../types/lib/asset';
3
-
4
- declare const FileManager: FileManagerConstructor<ExternalAsset>;
5
-
1
+ import type { FileManagerConstructor } from '../types/lib';
2
+ import type { ExternalAsset } from '../types/lib/asset';
3
+
4
+ declare const FileManager: FileManagerConstructor<ExternalAsset>;
5
+
6
6
  export = FileManager;