@spinajs/fs 2.0.180 → 2.0.182

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 (73) hide show
  1. package/lib/cjs/cli/rm.d.ts +11 -11
  2. package/lib/cjs/cli/rm.js +28 -28
  3. package/lib/cjs/cli/rm.js.map +1 -1
  4. package/lib/cjs/cli/stat.d.ts +10 -10
  5. package/lib/cjs/cli/stat.js +26 -26
  6. package/lib/cjs/cli/stat.js.map +1 -1
  7. package/lib/cjs/config/fs.d.ts +33 -16
  8. package/lib/cjs/config/fs.d.ts.map +1 -1
  9. package/lib/cjs/config/fs.js +50 -36
  10. package/lib/cjs/config/fs.js.map +1 -1
  11. package/lib/cjs/decorators.d.ts +7 -7
  12. package/lib/cjs/decorators.js +23 -23
  13. package/lib/cjs/file-hasher.d.ts +12 -8
  14. package/lib/cjs/file-hasher.d.ts.map +1 -1
  15. package/lib/cjs/file-hasher.js +51 -42
  16. package/lib/cjs/file-hasher.js.map +1 -1
  17. package/lib/cjs/file-info.d.ts +10 -10
  18. package/lib/cjs/file-info.js +124 -124
  19. package/lib/cjs/file-info.js.map +1 -1
  20. package/lib/cjs/index.d.ts +10 -10
  21. package/lib/cjs/index.d.ts.map +1 -1
  22. package/lib/cjs/index.js +72 -71
  23. package/lib/cjs/index.js.map +1 -1
  24. package/lib/cjs/interfaces.d.ts +180 -157
  25. package/lib/cjs/interfaces.d.ts.map +1 -1
  26. package/lib/cjs/interfaces.js +44 -33
  27. package/lib/cjs/interfaces.js.map +1 -1
  28. package/lib/cjs/local-provider.d.ts +96 -100
  29. package/lib/cjs/local-provider.d.ts.map +1 -1
  30. package/lib/cjs/local-provider.js +321 -278
  31. package/lib/cjs/local-provider.js.map +1 -1
  32. package/lib/cjs/local-temp-provider.d.ts +18 -18
  33. package/lib/cjs/local-temp-provider.d.ts.map +1 -1
  34. package/lib/cjs/local-temp-provider.js +59 -54
  35. package/lib/cjs/local-temp-provider.js.map +1 -1
  36. package/lib/mjs/cli/rm.d.ts +11 -11
  37. package/lib/mjs/cli/rm.js +25 -25
  38. package/lib/mjs/cli/rm.js.map +1 -1
  39. package/lib/mjs/cli/stat.d.ts +10 -10
  40. package/lib/mjs/cli/stat.js +23 -23
  41. package/lib/mjs/cli/stat.js.map +1 -1
  42. package/lib/mjs/config/fs.d.ts +33 -16
  43. package/lib/mjs/config/fs.d.ts.map +1 -1
  44. package/lib/mjs/config/fs.js +48 -34
  45. package/lib/mjs/config/fs.js.map +1 -1
  46. package/lib/mjs/decorators.d.ts +7 -7
  47. package/lib/mjs/decorators.js +19 -19
  48. package/lib/mjs/file-hasher.d.ts +12 -8
  49. package/lib/mjs/file-hasher.d.ts.map +1 -1
  50. package/lib/mjs/file-hasher.js +45 -36
  51. package/lib/mjs/file-hasher.js.map +1 -1
  52. package/lib/mjs/file-info.d.ts +10 -10
  53. package/lib/mjs/file-info.js +121 -121
  54. package/lib/mjs/file-info.js.map +1 -1
  55. package/lib/mjs/index.d.ts +10 -10
  56. package/lib/mjs/index.d.ts.map +1 -1
  57. package/lib/mjs/index.js +55 -54
  58. package/lib/mjs/index.js.map +1 -1
  59. package/lib/mjs/interfaces.d.ts +180 -157
  60. package/lib/mjs/interfaces.d.ts.map +1 -1
  61. package/lib/mjs/interfaces.js +38 -27
  62. package/lib/mjs/interfaces.js.map +1 -1
  63. package/lib/mjs/local-provider.d.ts +96 -100
  64. package/lib/mjs/local-provider.d.ts.map +1 -1
  65. package/lib/mjs/local-provider.js +315 -272
  66. package/lib/mjs/local-provider.js.map +1 -1
  67. package/lib/mjs/local-temp-provider.d.ts +18 -18
  68. package/lib/mjs/local-temp-provider.d.ts.map +1 -1
  69. package/lib/mjs/local-temp-provider.js +56 -51
  70. package/lib/mjs/local-temp-provider.js.map +1 -1
  71. package/lib/tsconfig.cjs.tsbuildinfo +1 -1
  72. package/lib/tsconfig.mjs.tsbuildinfo +1 -1
  73. package/package.json +6 -2
@@ -1,279 +1,322 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.fsNative = void 0;
16
- /* eslint-disable security/detect-non-literal-fs-filename */
17
- const exceptions_1 = require("@spinajs/exceptions");
18
- const fs_1 = require("fs");
19
- const promises_1 = require("node:fs/promises");
20
- const luxon_1 = require("luxon");
21
- const di_1 = require("@spinajs/di");
22
- const interfaces_js_1 = require("./interfaces.js");
23
- const path_1 = require("path");
24
- const log_common_1 = require("@spinajs/log-common");
25
- const archiver_1 = __importDefault(require("archiver"));
26
- const unzipper_1 = __importDefault(require("unzipper"));
27
- /**
28
- * Abstract layer for file operations.
29
- * Basic implementation is just wrapper for native node fs functions
30
- *
31
- * It allows to wrap other libs eg. aws s3, ftp
32
- * and inject it into code without changing logic that use them.
33
- *
34
- * TODO: map errors to some kind of common errors shared with other implementations
35
- */
36
- let fsNative = class fsNative extends interfaces_js_1.fs {
37
- /**
38
- * Name of provider. We can have multiple providers of the same type but with different options.
39
- * Also used in InjectService decorator for mapping
40
- */
41
- get Name() {
42
- return this.Options.name;
43
- }
44
- constructor(Options) {
45
- super();
46
- this.Options = Options;
47
- }
48
- async resolve() {
49
- // yup, exceptions as conditional execution path is bad :)
50
- try {
51
- await (0, promises_1.access)(this.Options.basePath, fs_1.constants.F_OK);
52
- }
53
- catch {
54
- this.Logger.warn(`Base path ${this.Options.basePath} for file provider ${this.Options.name} not exists, trying to create base folder`);
55
- await (0, promises_1.mkdir)(this.Options.basePath, { recursive: true });
56
- this.Logger.success(`Base path ${this.Options.basePath} created`);
57
- }
58
- }
59
- /**
60
- *
61
- * Tries to download file to local filesystem, then returns local filesystem path.
62
- * Native implementation simply returns local path and does nothing.
63
- *
64
- * @param path - file to download
65
- */
66
- async download(path) {
67
- const p = this.resolvePath(path);
68
- const exists = await this.exists(path);
69
- if (!exists) {
70
- throw new exceptions_1.IOFail(`file ${p} does not exists`);
71
- }
72
- return p;
73
- }
74
- async upload(srcPath, destPath) {
75
- if (!(0, fs_1.existsSync)(srcPath)) {
76
- throw new exceptions_1.IOFail(`file ${srcPath} does not exists`);
77
- }
78
- const dPath = this.resolvePath(destPath ?? (0, path_1.basename)(srcPath));
79
- (0, fs_1.copyFileSync)(srcPath, dPath);
80
- }
81
- /**
82
- * read all content of file
83
- */
84
- async read(path, encoding) {
85
- return new Promise((resolve, reject) => {
86
- (0, fs_1.readFile)(this.resolvePath(path), { encoding: encoding ?? 'binary' }, (err, data) => {
87
- if (err) {
88
- reject(err);
89
- }
90
- else {
91
- resolve(data);
92
- }
93
- });
94
- });
95
- }
96
- readStream(path, encoding) {
97
- return Promise.resolve((0, fs_1.createReadStream)(this.resolvePath(path), { encoding: encoding ?? 'binary' }));
98
- }
99
- /**
100
- * Write to file string or buffer
101
- */
102
- async write(path, data, encoding) {
103
- const fDesc = await (0, promises_1.open)(this.resolvePath(path), 'w');
104
- return await fDesc.writeFile(data, { encoding });
105
- }
106
- async append(path, data, encoding) {
107
- await (0, promises_1.appendFile)(path, data, encoding);
108
- }
109
- writeStream(path, rStream, encoding) {
110
- return Promise.resolve((0, fs_1.createWriteStream)(this.resolvePath(path), {
111
- encoding: (typeof rStream === 'string' ? rStream : encoding) ?? 'binary',
112
- }));
113
- }
114
- /**
115
- * Checks if file existst
116
- * @param path - path to check
117
- */
118
- async exists(path) {
119
- try {
120
- await (0, promises_1.access)(this.resolvePath(path), fs_1.constants.F_OK);
121
- return true;
122
- }
123
- catch {
124
- return false;
125
- }
126
- }
127
- async dirExists(path) {
128
- return this.exists(this.resolvePath(path));
129
- }
130
- /**
131
- * Copy file to another location
132
- * @param path - src path
133
- * @param dest - dest path
134
- */
135
- async copy(path, dest) {
136
- await (0, promises_1.copyFile)(this.resolvePath(path), this.resolvePath(dest));
137
- }
138
- /**
139
- * Copy file to another location and deletes src file
140
- */
141
- async move(oldPath, newPath) {
142
- const oPath = this.resolvePath(oldPath);
143
- const nPath = this.resolvePath(newPath);
144
- try {
145
- await (0, promises_1.rename)(oPath, nPath);
146
- }
147
- catch (err) {
148
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
149
- if (err.code === 'EXDEV') {
150
- await (0, promises_1.copyFile)(oPath, nPath);
151
- await (0, promises_1.unlink)(oPath);
152
- }
153
- else {
154
- throw err;
155
- }
156
- }
157
- }
158
- /**
159
- * Change name of a file
160
- */
161
- async rename(oldPath, newPath) {
162
- await (0, promises_1.rename)(this.resolvePath(oldPath), this.resolvePath(newPath));
163
- }
164
- /**
165
- * Deletes file permanently
166
- *
167
- * @param path - path to file that will be deleted
168
- * @param onlyTemp - remote filesystems need to download file before, if so, calling unlink with this flag removes only local temp file after we finished processing
169
- */
170
- async unlink(path, onlyTemp) {
171
- // local fs returns only local path to file, we dont want to delete it
172
- if (onlyTemp) {
173
- return;
174
- }
175
- await (0, promises_1.unlink)(this.resolvePath(path));
176
- }
177
- /**
178
- *
179
- * Deletes dir recursively & all contents inside
180
- *
181
- * @param path - dir to remove
182
- */
183
- async rm(path) {
184
- await (0, promises_1.rm)(this.resolvePath(path), { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 });
185
- }
186
- /**
187
- *
188
- * Creates directory, recursively
189
- *
190
- * @param path - directory to create
191
- */
192
- async mkdir(path) {
193
- await (0, promises_1.mkdir)(this.resolvePath(path), { recursive: true });
194
- }
195
- /**
196
- * Returns file statistics, not all fields may be accesible
197
- */
198
- async stat(path) {
199
- const result = await (0, promises_1.stat)(this.resolvePath(path));
200
- return {
201
- IsDirectory: result.isDirectory(),
202
- IsFile: result.isFile(),
203
- CreationTime: luxon_1.DateTime.fromJSDate(result.ctime),
204
- ModifiedTime: luxon_1.DateTime.fromJSDate(result.mtime),
205
- AccessTime: luxon_1.DateTime.fromJSDate(result.atime),
206
- Size: result.size,
207
- };
208
- }
209
- tmppath() {
210
- return (0, path_1.join)(this.Options.basePath, super.tmpname());
211
- }
212
- /**
213
- * List content of directory
214
- *
215
- * @param path - path to directory
216
- */
217
- async list(path) {
218
- return await (0, promises_1.readdir)(this.resolvePath(path));
219
- }
220
- async unzip(path, destPath) {
221
- return new Promise((resolve, reject) => {
222
- (0, fs_1.createReadStream)(this.resolvePath(path)).pipe(unzipper_1.default
223
- .Extract({
224
- path: this.resolvePath(destPath),
225
- })
226
- .on('close', resolve)
227
- .on('error', reject));
228
- });
229
- }
230
- async zip(path, zName) {
231
- const fs = await di_1.DI.resolve('__file_provider__', ['fs-temp']);
232
- const outFile = fs.resolvePath(fs.tmpname() + '.zip');
233
- const output = (0, fs_1.createWriteStream)(outFile);
234
- const pStat = await this.stat(path);
235
- const fPath = this.resolvePath(path);
236
- const archive = (0, archiver_1.default)('zip', {
237
- zlib: { level: 9 }, // Sets the compression level.
238
- });
239
- // pipe archive data to the file
240
- archive.pipe(output);
241
- if (pStat.IsDirectory) {
242
- archive.directory(fPath, false);
243
- }
244
- else {
245
- archive.file(fPath, { name: zName ?? (0, path_1.basename)(fPath) });
246
- }
247
- await archive.finalize();
248
- return {
249
- asFilePath: () => {
250
- return outFile;
251
- },
252
- asStream: (encoding) => {
253
- return (0, fs_1.createReadStream)(outFile, {
254
- encoding: encoding ?? 'binary',
255
- });
256
- },
257
- asBase64: () => {
258
- return (0, fs_1.readFileSync)(outFile, { encoding: 'base64' });
259
- },
260
- };
261
- }
262
- resolvePath(path) {
263
- if (path.startsWith(this.Options.basePath)) {
264
- return path;
265
- }
266
- return (0, path_1.join)(this.Options.basePath, path);
267
- }
268
- };
269
- __decorate([
270
- (0, log_common_1.Logger)('fs'),
271
- __metadata("design:type", log_common_1.Log)
272
- ], fsNative.prototype, "Logger", void 0);
273
- fsNative = __decorate([
274
- (0, di_1.Injectable)('fs'),
275
- (0, di_1.PerInstanceCheck)(),
276
- __metadata("design:paramtypes", [Object])
277
- ], fsNative);
278
- exports.fsNative = fsNative;
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.fsNative = void 0;
16
+ /* eslint-disable security/detect-non-literal-fs-filename */
17
+ const exceptions_1 = require("@spinajs/exceptions");
18
+ const fs_1 = require("fs");
19
+ const promises_1 = require("node:fs/promises");
20
+ const luxon_1 = require("luxon");
21
+ const di_1 = require("@spinajs/di");
22
+ const interfaces_js_1 = require("./interfaces.js");
23
+ const path_1 = require("path");
24
+ const log_common_1 = require("@spinajs/log-common");
25
+ const archiver_1 = __importDefault(require("archiver"));
26
+ const unzipper_1 = __importDefault(require("unzipper"));
27
+ const promises_2 = require("fs/promises");
28
+ const util_1 = require("@spinajs/util");
29
+ /**
30
+ * Abstract layer for file operations.
31
+ * Basic implementation is just wrapper for native node fs functions
32
+ *
33
+ * It allows to wrap other libs eg. aws s3, ftp
34
+ * and inject it into code without changing logic that use them.
35
+ *
36
+ * TODO: map errors to some kind of common errors shared with other implementations
37
+ */
38
+ let fsNative = class fsNative extends interfaces_js_1.fs {
39
+ /**
40
+ * Name of provider. We can have multiple providers of the same type but with different options.
41
+ * Also used in InjectService decorator for mapping
42
+ */
43
+ get Name() {
44
+ return this.Options.name;
45
+ }
46
+ constructor(Options) {
47
+ super();
48
+ this.Options = Options;
49
+ }
50
+ async resolve() {
51
+ if (!this.Options.basePath) {
52
+ throw new exceptions_1.IOFail(`Base path for file provider ${this.Options.name} not set`);
53
+ }
54
+ if ((await this.exists(this.Options.basePath)) === false) {
55
+ this.Logger.warn(`Base path ${this.Options.basePath} for file provider ${this.Options.name} not exists, trying to create base folder`);
56
+ await (0, promises_1.mkdir)(this.Options.basePath, { recursive: true });
57
+ this.Logger.success(`Base path ${this.Options.basePath} created`);
58
+ }
59
+ }
60
+ /**
61
+ *
62
+ * Tries to download file to local filesystem, then returns local filesystem path.
63
+ * Native implementation simply returns local path and does nothing.
64
+ *
65
+ * @param path - file to download
66
+ */
67
+ async download(path) {
68
+ const p = this.resolvePath(path);
69
+ const exists = await this.exists(path);
70
+ if (!exists) {
71
+ throw new exceptions_1.IOFail(`file ${p} does not exists`);
72
+ }
73
+ return p;
74
+ }
75
+ async upload(srcPath, destPath) {
76
+ if (!(0, fs_1.existsSync)(srcPath)) {
77
+ throw new exceptions_1.IOFail(`file ${srcPath} does not exists`);
78
+ }
79
+ const dPath = this.resolvePath(destPath ?? (0, path_1.basename)(srcPath));
80
+ await (0, promises_2.cp)(srcPath, dPath, { force: true, recursive: true });
81
+ }
82
+ /**
83
+ * read all content of file
84
+ */
85
+ async read(path, encoding) {
86
+ return new Promise((resolve, reject) => {
87
+ (0, fs_1.readFile)(this.resolvePath(path), { encoding: encoding ?? 'binary' }, (err, data) => {
88
+ if (err) {
89
+ reject(err);
90
+ }
91
+ else {
92
+ resolve(data);
93
+ }
94
+ });
95
+ });
96
+ }
97
+ readStream(path, encoding) {
98
+ return Promise.resolve((0, fs_1.createReadStream)(this.resolvePath(path), { encoding: encoding ?? 'binary' }));
99
+ }
100
+ /**
101
+ * Write to file string or buffer
102
+ */
103
+ async write(path, data, encoding) {
104
+ let fHandle = null;
105
+ try {
106
+ fHandle = await (0, promises_1.open)(this.resolvePath(path), 'w');
107
+ await fHandle.writeFile(data, { encoding });
108
+ }
109
+ catch (err) {
110
+ throw new exceptions_1.IOFail(`Cannot write to file ${path}`, err);
111
+ }
112
+ finally {
113
+ if (fHandle) {
114
+ await fHandle.sync();
115
+ await fHandle.close();
116
+ }
117
+ }
118
+ }
119
+ async append(path, data, encoding) {
120
+ await (0, promises_1.appendFile)(path, data, encoding);
121
+ }
122
+ writeStream(path, rStream, encoding) {
123
+ const stream = (0, fs_1.createWriteStream)(this.resolvePath(path), {
124
+ encoding: (typeof rStream === 'string' ? rStream : encoding) ?? 'binary',
125
+ });
126
+ if (rStream instanceof fs_1.ReadStream) {
127
+ rStream.pipe(stream);
128
+ }
129
+ return Promise.resolve(stream);
130
+ }
131
+ /**
132
+ * Checks if file existst
133
+ * @param path - path to check
134
+ */
135
+ async exists(path) {
136
+ try {
137
+ await (0, promises_1.access)(this.resolvePath(path), fs_1.constants.F_OK);
138
+ return true;
139
+ }
140
+ catch {
141
+ return false;
142
+ }
143
+ }
144
+ async dirExists(path) {
145
+ return this.exists(this.resolvePath(path));
146
+ }
147
+ /**
148
+ * Copy file or dir to another location
149
+ * @param path - src path
150
+ * @param dest - dest path
151
+ */
152
+ async copy(path, dest, dstFs) {
153
+ const sPath = this.resolvePath(path);
154
+ const exists = await this.exists(path);
155
+ if (!exists) {
156
+ throw new exceptions_1.IOFail(`File ${path} not exists`);
157
+ }
158
+ if (dstFs) {
159
+ await dstFs.upload(sPath, dest);
160
+ }
161
+ else {
162
+ const dPath = this.resolvePath(dest);
163
+ await (0, promises_2.cp)(sPath, dPath, {
164
+ recursive: true,
165
+ force: true,
166
+ });
167
+ }
168
+ }
169
+ /**
170
+ * Copy file to another location and deletes src file
171
+ */
172
+ async move(oldPath, newPath, dstFs) {
173
+ const oPath = this.resolvePath(oldPath);
174
+ if (dstFs) {
175
+ await dstFs.upload(oPath, newPath);
176
+ await this.rm(oldPath);
177
+ }
178
+ else {
179
+ const nPath = this.resolvePath(newPath);
180
+ await (0, promises_1.rename)(oPath, nPath);
181
+ }
182
+ }
183
+ /**
184
+ * Change name of a file
185
+ */
186
+ async rename(oldPath, newPath) {
187
+ await (0, promises_1.rename)(this.resolvePath(oldPath), this.resolvePath(newPath));
188
+ }
189
+ /**
190
+ *
191
+ * Deletes file OR
192
+ * Deletes dir recursively & all contents inside
193
+ *
194
+ * @param path - dir to remove
195
+ */
196
+ async rm(path) {
197
+ await (0, promises_1.rm)(this.resolvePath(path), { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 });
198
+ }
199
+ /**
200
+ *
201
+ * Creates directory, recursively
202
+ *
203
+ * @param path - directory to create
204
+ */
205
+ async mkdir(path) {
206
+ await (0, promises_1.mkdir)(this.resolvePath(path), { recursive: true });
207
+ }
208
+ /**
209
+ * Returns file statistics, not all fields may be accesible
210
+ */
211
+ async stat(path) {
212
+ const result = await (0, promises_1.stat)(this.resolvePath(path));
213
+ return {
214
+ IsDirectory: result.isDirectory(),
215
+ IsFile: result.isFile(),
216
+ CreationTime: luxon_1.DateTime.fromJSDate(result.ctime),
217
+ ModifiedTime: luxon_1.DateTime.fromJSDate(result.mtime),
218
+ AccessTime: luxon_1.DateTime.fromJSDate(result.atime),
219
+ Size: result.size,
220
+ };
221
+ }
222
+ tmppath() {
223
+ return (0, path_1.join)(this.Options.basePath, super.tmpname());
224
+ }
225
+ /**
226
+ * List content of directory
227
+ *
228
+ * @param path - path to directory
229
+ */
230
+ async list(path) {
231
+ return await (0, promises_1.readdir)(this.resolvePath(path));
232
+ }
233
+ async unzip(path, destPath, dstFs) {
234
+ const dFs = dstFs ?? this;
235
+ const dst = dFs.resolvePath(destPath ?? dFs.tmpname());
236
+ return new Promise((resolve, reject) => {
237
+ (0, fs_1.createReadStream)(this.resolvePath(path)).pipe(unzipper_1.default
238
+ .Extract({
239
+ path: dst,
240
+ })
241
+ .on('close', resolve)
242
+ .on('error', reject));
243
+ });
244
+ }
245
+ async isDir(path) {
246
+ try {
247
+ const pStat = await this.stat(path);
248
+ return pStat.IsDirectory;
249
+ }
250
+ catch {
251
+ return false;
252
+ }
253
+ }
254
+ async zip(path, dstFs, dstFile) {
255
+ const paths = (0, util_1.toArray)(path);
256
+ const fs = dstFs ?? (await di_1.DI.resolve('__file_provider__', ['fs-temp']));
257
+ const outFile = dstFile ?? `${fs.tmpname()}.zip`;
258
+ const wStream = await fs.writeStream(outFile);
259
+ const archive = (0, archiver_1.default)('zip');
260
+ // pipe archive data to the file
261
+ archive.pipe(wStream);
262
+ for (const p of paths.map((p) => this.resolvePath(p))) {
263
+ if (await this.isDir(p)) {
264
+ archive.directory(p, false);
265
+ continue;
266
+ }
267
+ archive.file(p, { name: (0, path_1.basename)(p) });
268
+ }
269
+ return new Promise((resolve, reject) => {
270
+ archive.on('warning', (err) => {
271
+ if (err.code === 'ENOENT') {
272
+ this.Logger.error(err, `File not found ( archiving file ), reason: ${err.message})`);
273
+ return;
274
+ }
275
+ reject(new exceptions_1.IOFail('Archiving error', err));
276
+ });
277
+ archive.on('error', (err) => {
278
+ reject(new exceptions_1.IOFail('Archiving error', err));
279
+ });
280
+ archive.on('entry', (entry) => {
281
+ this.Logger.trace(`Archiving file ${entry.name}, size: ${entry.stats?.size} into ${outFile}, fs: ${fs.Name}`);
282
+ });
283
+ wStream.on('close', () => {
284
+ const result = {
285
+ fs,
286
+ asFilePath: () => {
287
+ return outFile;
288
+ },
289
+ asStream: (encoding) => {
290
+ return (0, fs_1.createReadStream)(outFile, {
291
+ encoding: encoding ?? 'binary',
292
+ });
293
+ },
294
+ asBase64: () => {
295
+ return (0, fs_1.readFileSync)(outFile, { encoding: 'base64' });
296
+ },
297
+ unlink: async () => {
298
+ return await fs.rm(outFile);
299
+ },
300
+ };
301
+ resolve(result);
302
+ });
303
+ archive.finalize();
304
+ });
305
+ }
306
+ resolvePath(path) {
307
+ if (!this.Options.basePath || path.startsWith(this.Options.basePath))
308
+ return path;
309
+ return (0, path_1.join)(this.Options.basePath, path);
310
+ }
311
+ };
312
+ exports.fsNative = fsNative;
313
+ __decorate([
314
+ (0, log_common_1.Logger)('fs'),
315
+ __metadata("design:type", log_common_1.Log)
316
+ ], fsNative.prototype, "Logger", void 0);
317
+ exports.fsNative = fsNative = __decorate([
318
+ (0, di_1.Injectable)('fs'),
319
+ (0, di_1.PerInstanceCheck)(),
320
+ __metadata("design:paramtypes", [Object])
321
+ ], fsNative);
279
322
  //# sourceMappingURL=local-provider.js.map