jopi-toolkit 3.0.24 → 3.1.25
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.
- package/dist/jk_app/common.d.ts +6 -3
- package/dist/jk_app/common.js +28 -7
- package/dist/jk_app/common.js.map +1 -1
- package/dist/jk_events/index.d.ts +2 -0
- package/dist/jk_events/index.js +16 -2
- package/dist/jk_events/index.js.map +1 -1
- package/dist/jk_fs/jBundler_ifBrowser.d.ts +2 -0
- package/dist/jk_fs/jBundler_ifBrowser.js +6 -0
- package/dist/jk_fs/jBundler_ifBrowser.js.map +1 -1
- package/dist/jk_fs/jBundler_ifServer.d.ts +11 -9
- package/dist/jk_fs/jBundler_ifServer.js +44 -13
- package/dist/jk_fs/jBundler_ifServer.js.map +1 -1
- package/dist/jk_logs/jBundler_ifServer.js +5 -0
- package/dist/jk_logs/jBundler_ifServer.js.map +1 -1
- package/dist/jk_process/jBundler_ifServer.d.ts +4 -0
- package/dist/jk_process/jBundler_ifServer.js +31 -0
- package/dist/jk_process/jBundler_ifServer.js.map +1 -1
- package/dist/jk_tools/jBundler_ifServer.js +19 -7
- package/dist/jk_tools/jBundler_ifServer.js.map +1 -1
- package/package.json +25 -13
- package/src/jk_app/common.js +40 -24
- package/src/jk_app/common.ts +34 -9
- package/src/jk_events/index.ts +34 -14
- package/src/jk_fs/jBundler_ifBrowser.ts +8 -0
- package/src/jk_fs/jBundler_ifServer.ts +38 -6
- package/src/jk_logs/jBundler_ifServer.ts +6 -0
- package/src/{jk_fs → jk_process}/index.js +0 -1
- package/src/jk_process/jBundler_ifServer.ts +35 -1
- package/src/jk_tools/jBundler_ifServer.ts +19 -6
- package/src/jk_what/jBundler_ifServer.js +8 -5
- package/src/jk_fs/common.js +0 -1
- package/src/jk_fs/jBundler_ifServer.js +0 -764
|
@@ -1,764 +0,0 @@
|
|
|
1
|
-
// noinspection JSUnusedGlobalSymbols
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
28
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
-
function step(op) {
|
|
31
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
-
switch (op[0]) {
|
|
36
|
-
case 0: case 1: t = op; break;
|
|
37
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
-
default:
|
|
41
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
-
if (t[2]) _.ops.pop();
|
|
46
|
-
_.trys.pop(); continue;
|
|
47
|
-
}
|
|
48
|
-
op = body.call(thisArg, _);
|
|
49
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
import fs from "node:fs/promises";
|
|
54
|
-
import fss, { createReadStream, createWriteStream } from "node:fs";
|
|
55
|
-
import { fileURLToPath as n_fileURLToPath, pathToFileURL as n_pathToFileURL } from "node:url";
|
|
56
|
-
import { lookup } from "mime-types";
|
|
57
|
-
import { Readable } from "node:stream";
|
|
58
|
-
import path from "node:path";
|
|
59
|
-
import { createHash } from "node:crypto";
|
|
60
|
-
import { isBunJS } from "jopi-toolkit/jk_what";
|
|
61
|
-
import AdmZip from 'adm-zip';
|
|
62
|
-
var WebToNodeReadableStreamAdapter = /** @class */ (function (_super) {
|
|
63
|
-
__extends(WebToNodeReadableStreamAdapter, _super);
|
|
64
|
-
function WebToNodeReadableStreamAdapter(webStream) {
|
|
65
|
-
var _this = _super.call(this) || this;
|
|
66
|
-
_this.webStreamReader = webStream.getReader();
|
|
67
|
-
return _this;
|
|
68
|
-
}
|
|
69
|
-
WebToNodeReadableStreamAdapter.prototype._read = function () {
|
|
70
|
-
var _this = this;
|
|
71
|
-
this.webStreamReader.read().then(function (_a) {
|
|
72
|
-
var done = _a.done, value = _a.value;
|
|
73
|
-
if (done) {
|
|
74
|
-
_this.push(null);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
var buffer = Buffer.from(value);
|
|
78
|
-
if (!_this.push(buffer))
|
|
79
|
-
_this.webStreamReader.cancel().then();
|
|
80
|
-
}).catch(function (err) {
|
|
81
|
-
_this.destroy(err);
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
WebToNodeReadableStreamAdapter.prototype._destroy = function (err, callback) {
|
|
85
|
-
this.webStreamReader.cancel().finally(function () { callback(err); });
|
|
86
|
-
};
|
|
87
|
-
return WebToNodeReadableStreamAdapter;
|
|
88
|
-
}(Readable));
|
|
89
|
-
function writeResponseToFile_node(response_1, filePath_1) {
|
|
90
|
-
return __awaiter(this, arguments, void 0, function (response, filePath, createDir) {
|
|
91
|
-
var bufferDonnees, bufferNode;
|
|
92
|
-
if (createDir === void 0) { createDir = true; }
|
|
93
|
-
return __generator(this, function (_a) {
|
|
94
|
-
switch (_a.label) {
|
|
95
|
-
case 0:
|
|
96
|
-
if (!createDir) return [3 /*break*/, 2];
|
|
97
|
-
return [4 /*yield*/, mkDir(path.dirname(filePath))];
|
|
98
|
-
case 1:
|
|
99
|
-
_a.sent();
|
|
100
|
-
_a.label = 2;
|
|
101
|
-
case 2: return [4 /*yield*/, response.arrayBuffer()];
|
|
102
|
-
case 3:
|
|
103
|
-
bufferDonnees = _a.sent();
|
|
104
|
-
bufferNode = Buffer.from(bufferDonnees);
|
|
105
|
-
return [4 /*yield*/, fs.writeFile(filePath, bufferNode)];
|
|
106
|
-
case 4:
|
|
107
|
-
_a.sent();
|
|
108
|
-
return [2 /*return*/];
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
export var writeResponseToFile = isBunJS
|
|
114
|
-
? function (r, p) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
115
|
-
switch (_a.label) {
|
|
116
|
-
case 0: return [4 /*yield*/, Bun.file(p).write(r)];
|
|
117
|
-
case 1:
|
|
118
|
-
_a.sent();
|
|
119
|
-
return [2 /*return*/];
|
|
120
|
-
}
|
|
121
|
-
}); }); }
|
|
122
|
-
: writeResponseToFile_node;
|
|
123
|
-
export function nodeStreamToWebStream(nodeStream, debugInfos) {
|
|
124
|
-
var dataListener;
|
|
125
|
-
var endListener;
|
|
126
|
-
var errorListener;
|
|
127
|
-
return new ReadableStream({
|
|
128
|
-
start: function (controller) {
|
|
129
|
-
dataListener = function (chunk) {
|
|
130
|
-
try {
|
|
131
|
-
controller.enqueue(chunk);
|
|
132
|
-
}
|
|
133
|
-
catch (e) {
|
|
134
|
-
if (debugInfos) {
|
|
135
|
-
console.log("nodeStreamToWebStream - enqueue failed for " + debugInfos, e);
|
|
136
|
-
}
|
|
137
|
-
nodeStream.destroy(new Error("WebStream controller closed unexpectedly"));
|
|
138
|
-
nodeStream.off('data', dataListener);
|
|
139
|
-
nodeStream.off('end', endListener);
|
|
140
|
-
nodeStream.off('error', errorListener);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
endListener = function () {
|
|
144
|
-
try {
|
|
145
|
-
controller.close();
|
|
146
|
-
}
|
|
147
|
-
catch (e) {
|
|
148
|
-
if (debugInfos) {
|
|
149
|
-
console.log("nodeStreamToWebStream - close failed for " + debugInfos, e);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
errorListener = function (err) {
|
|
154
|
-
controller.error(err);
|
|
155
|
-
nodeStream.off('data', dataListener);
|
|
156
|
-
nodeStream.off('end', endListener);
|
|
157
|
-
};
|
|
158
|
-
nodeStream.on('data', dataListener);
|
|
159
|
-
nodeStream.on('end', endListener);
|
|
160
|
-
nodeStream.on('error', errorListener);
|
|
161
|
-
},
|
|
162
|
-
cancel: function (reason) {
|
|
163
|
-
if (debugInfos) {
|
|
164
|
-
console.log("nodeStreamToWebStream - stream cancelled for ".concat(debugInfos, ". Reason: ").concat(reason || 'Client disconnected'));
|
|
165
|
-
}
|
|
166
|
-
if (dataListener)
|
|
167
|
-
nodeStream.off('data', dataListener);
|
|
168
|
-
if (endListener)
|
|
169
|
-
nodeStream.off('end', endListener);
|
|
170
|
-
if (errorListener)
|
|
171
|
-
nodeStream.off('error', errorListener);
|
|
172
|
-
if (typeof nodeStream.destroy === 'function') {
|
|
173
|
-
nodeStream.destroy();
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
export function webStreamToNodeStream(webStream) {
|
|
179
|
-
return new WebToNodeReadableStreamAdapter(webStream);
|
|
180
|
-
}
|
|
181
|
-
function createResponseFromFile_node(filePath, status, headers) {
|
|
182
|
-
if (status === void 0) { status = 200; }
|
|
183
|
-
var nodeReadStream = createReadStream(filePath);
|
|
184
|
-
var webReadableStream = nodeStreamToWebStream(nodeReadStream, filePath);
|
|
185
|
-
return new Response(webReadableStream, { status: status, headers: headers });
|
|
186
|
-
}
|
|
187
|
-
export var createResponseFromFile = isBunJS
|
|
188
|
-
? function (filePath, status, headers) {
|
|
189
|
-
if (status === void 0) { status = 200; }
|
|
190
|
-
return new Response(Bun.file(filePath), { status: status, headers: headers });
|
|
191
|
-
}
|
|
192
|
-
: createResponseFromFile_node;
|
|
193
|
-
export function getFileSize(filePath) {
|
|
194
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
195
|
-
var _a;
|
|
196
|
-
return __generator(this, function (_b) {
|
|
197
|
-
switch (_b.label) {
|
|
198
|
-
case 0:
|
|
199
|
-
_b.trys.push([0, 2, , 3]);
|
|
200
|
-
return [4 /*yield*/, fs.stat(filePath)];
|
|
201
|
-
case 1: return [2 /*return*/, (_b.sent()).size];
|
|
202
|
-
case 2:
|
|
203
|
-
_a = _b.sent();
|
|
204
|
-
return [2 /*return*/, 0];
|
|
205
|
-
case 3: return [2 /*return*/];
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
export function getFileStat(filePath) {
|
|
211
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
212
|
-
var _a;
|
|
213
|
-
return __generator(this, function (_b) {
|
|
214
|
-
switch (_b.label) {
|
|
215
|
-
case 0:
|
|
216
|
-
_b.trys.push([0, 2, , 3]);
|
|
217
|
-
return [4 /*yield*/, fs.stat(filePath)];
|
|
218
|
-
case 1: return [2 /*return*/, _b.sent()];
|
|
219
|
-
case 2:
|
|
220
|
-
_a = _b.sent();
|
|
221
|
-
return [2 /*return*/, undefined];
|
|
222
|
-
case 3: return [2 /*return*/];
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
export function getMimeTypeFromName(fileName) {
|
|
228
|
-
var found = lookup(fileName);
|
|
229
|
-
if (found === false)
|
|
230
|
-
return "";
|
|
231
|
-
return found;
|
|
232
|
-
}
|
|
233
|
-
export function mkDir(dirPath) {
|
|
234
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
235
|
-
var _a;
|
|
236
|
-
return __generator(this, function (_b) {
|
|
237
|
-
switch (_b.label) {
|
|
238
|
-
case 0:
|
|
239
|
-
_b.trys.push([0, 2, , 3]);
|
|
240
|
-
return [4 /*yield*/, fs.mkdir(dirPath, { recursive: true })];
|
|
241
|
-
case 1:
|
|
242
|
-
_b.sent();
|
|
243
|
-
return [2 /*return*/, true];
|
|
244
|
-
case 2:
|
|
245
|
-
_a = _b.sent();
|
|
246
|
-
return [2 /*return*/, false];
|
|
247
|
-
case 3: return [2 /*return*/];
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
export function rmDir(dirPath) {
|
|
253
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
254
|
-
var _a;
|
|
255
|
-
return __generator(this, function (_b) {
|
|
256
|
-
switch (_b.label) {
|
|
257
|
-
case 0:
|
|
258
|
-
_b.trys.push([0, 2, , 3]);
|
|
259
|
-
return [4 /*yield*/, fs.rm(dirPath, { recursive: true, force: true })];
|
|
260
|
-
case 1:
|
|
261
|
-
_b.sent();
|
|
262
|
-
return [2 /*return*/, true];
|
|
263
|
-
case 2:
|
|
264
|
-
_a = _b.sent();
|
|
265
|
-
return [2 /*return*/, false];
|
|
266
|
-
case 3: return [2 /*return*/];
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
export var fileURLToPath = isBunJS
|
|
272
|
-
? function (url) { return Bun.fileURLToPath(url); }
|
|
273
|
-
: n_fileURLToPath;
|
|
274
|
-
export var pathToFileURL = isBunJS
|
|
275
|
-
? function (fsPath) { return Bun.pathToFileURL(fsPath); }
|
|
276
|
-
: n_pathToFileURL;
|
|
277
|
-
export function unlink(filePath) {
|
|
278
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
279
|
-
var _a;
|
|
280
|
-
return __generator(this, function (_b) {
|
|
281
|
-
switch (_b.label) {
|
|
282
|
-
case 0:
|
|
283
|
-
_b.trys.push([0, 2, , 3]);
|
|
284
|
-
return [4 /*yield*/, fs.unlink(filePath)];
|
|
285
|
-
case 1:
|
|
286
|
-
_b.sent();
|
|
287
|
-
return [2 /*return*/, true];
|
|
288
|
-
case 2:
|
|
289
|
-
_a = _b.sent();
|
|
290
|
-
return [2 /*return*/, false];
|
|
291
|
-
case 3: return [2 /*return*/];
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
export function writeTextToFile(filePath_1, text_1) {
|
|
297
|
-
return __awaiter(this, arguments, void 0, function (filePath, text, createDir) {
|
|
298
|
-
if (createDir === void 0) { createDir = true; }
|
|
299
|
-
return __generator(this, function (_a) {
|
|
300
|
-
switch (_a.label) {
|
|
301
|
-
case 0:
|
|
302
|
-
if (!createDir) return [3 /*break*/, 2];
|
|
303
|
-
return [4 /*yield*/, mkDir(path.dirname(filePath))];
|
|
304
|
-
case 1:
|
|
305
|
-
_a.sent();
|
|
306
|
-
_a.label = 2;
|
|
307
|
-
case 2: return [4 /*yield*/, fs.writeFile(filePath, text, { encoding: 'utf8', flag: 'w' })];
|
|
308
|
-
case 3:
|
|
309
|
-
_a.sent();
|
|
310
|
-
return [2 /*return*/];
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
export function writeTextToFileSync(filePath, text, createDir) {
|
|
316
|
-
if (createDir === void 0) { createDir = true; }
|
|
317
|
-
if (createDir) {
|
|
318
|
-
try {
|
|
319
|
-
fss.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
320
|
-
}
|
|
321
|
-
catch (_a) { }
|
|
322
|
-
}
|
|
323
|
-
fss.writeFileSync(filePath, text, { encoding: 'utf8', flag: 'w' });
|
|
324
|
-
}
|
|
325
|
-
export function readTextFromFile(filePath_1) {
|
|
326
|
-
return __awaiter(this, arguments, void 0, function (filePath, throwError) {
|
|
327
|
-
var _a;
|
|
328
|
-
if (throwError === void 0) { throwError = false; }
|
|
329
|
-
return __generator(this, function (_b) {
|
|
330
|
-
switch (_b.label) {
|
|
331
|
-
case 0:
|
|
332
|
-
if (throwError) {
|
|
333
|
-
return [2 /*return*/, fs.readFile(filePath, 'utf8')];
|
|
334
|
-
}
|
|
335
|
-
_b.label = 1;
|
|
336
|
-
case 1:
|
|
337
|
-
_b.trys.push([1, 3, , 4]);
|
|
338
|
-
return [4 /*yield*/, fs.readFile(filePath, 'utf8')];
|
|
339
|
-
case 2: return [2 /*return*/, _b.sent()];
|
|
340
|
-
case 3:
|
|
341
|
-
_a = _b.sent();
|
|
342
|
-
// @ts-ignore
|
|
343
|
-
return [2 /*return*/, undefined];
|
|
344
|
-
case 4: return [2 /*return*/];
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
export function readJsonFromFile(filePath_1) {
|
|
350
|
-
return __awaiter(this, arguments, void 0, function (filePath, throwError) {
|
|
351
|
-
var txt, txt, _a;
|
|
352
|
-
if (throwError === void 0) { throwError = false; }
|
|
353
|
-
return __generator(this, function (_b) {
|
|
354
|
-
switch (_b.label) {
|
|
355
|
-
case 0:
|
|
356
|
-
if (!throwError) return [3 /*break*/, 2];
|
|
357
|
-
return [4 /*yield*/, fs.readFile(filePath, 'utf8')];
|
|
358
|
-
case 1:
|
|
359
|
-
txt = _b.sent();
|
|
360
|
-
return [2 /*return*/, JSON.parse(txt)];
|
|
361
|
-
case 2:
|
|
362
|
-
_b.trys.push([2, 4, , 5]);
|
|
363
|
-
return [4 /*yield*/, fs.readFile(filePath, 'utf8')];
|
|
364
|
-
case 3:
|
|
365
|
-
txt = _b.sent();
|
|
366
|
-
return [2 /*return*/, JSON.parse(txt)];
|
|
367
|
-
case 4:
|
|
368
|
-
_a = _b.sent();
|
|
369
|
-
// @ts-ignore
|
|
370
|
-
return [2 /*return*/, undefined];
|
|
371
|
-
case 5: return [2 /*return*/];
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
export function readTextFromFileSync(filePath) {
|
|
377
|
-
return fss.readFileSync(filePath, 'utf8');
|
|
378
|
-
}
|
|
379
|
-
export function isFile(filePath) {
|
|
380
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
381
|
-
var stats;
|
|
382
|
-
return __generator(this, function (_a) {
|
|
383
|
-
switch (_a.label) {
|
|
384
|
-
case 0: return [4 /*yield*/, getFileStat(filePath)];
|
|
385
|
-
case 1:
|
|
386
|
-
stats = _a.sent();
|
|
387
|
-
if (!stats)
|
|
388
|
-
return [2 /*return*/, false];
|
|
389
|
-
return [2 /*return*/, stats.isFile()];
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
export function isDirectory(filePath) {
|
|
395
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
396
|
-
var stats;
|
|
397
|
-
return __generator(this, function (_a) {
|
|
398
|
-
switch (_a.label) {
|
|
399
|
-
case 0: return [4 /*yield*/, getFileStat(filePath)];
|
|
400
|
-
case 1:
|
|
401
|
-
stats = _a.sent();
|
|
402
|
-
if (!stats)
|
|
403
|
-
return [2 /*return*/, false];
|
|
404
|
-
return [2 /*return*/, stats.isDirectory()];
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
export function isDirectorySync(dirPath) {
|
|
410
|
-
try {
|
|
411
|
-
var stats = fss.statSync(dirPath);
|
|
412
|
-
return stats.isDirectory();
|
|
413
|
-
}
|
|
414
|
-
catch (_a) {
|
|
415
|
-
}
|
|
416
|
-
return false;
|
|
417
|
-
}
|
|
418
|
-
export function isFileSync(dirPath) {
|
|
419
|
-
try {
|
|
420
|
-
var stats = fss.statSync(dirPath);
|
|
421
|
-
return stats.isFile();
|
|
422
|
-
}
|
|
423
|
-
catch (_a) {
|
|
424
|
-
}
|
|
425
|
-
return false;
|
|
426
|
-
}
|
|
427
|
-
function readFileToBytes_node(filePath) {
|
|
428
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
429
|
-
var buffer;
|
|
430
|
-
return __generator(this, function (_a) {
|
|
431
|
-
switch (_a.label) {
|
|
432
|
-
case 0: return [4 /*yield*/, fs.readFile(filePath)];
|
|
433
|
-
case 1:
|
|
434
|
-
buffer = _a.sent();
|
|
435
|
-
return [2 /*return*/, new Uint8Array(buffer)];
|
|
436
|
-
}
|
|
437
|
-
});
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
export var readFileToBytes = isBunJS
|
|
441
|
-
? function (filePath) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
442
|
-
return [2 /*return*/, Bun.file(filePath).bytes()];
|
|
443
|
-
}); }); }
|
|
444
|
-
: readFileToBytes_node;
|
|
445
|
-
export function getRelativePath(fromDir, absolutePath) {
|
|
446
|
-
return path.relative(fromDir, absolutePath);
|
|
447
|
-
}
|
|
448
|
-
export function dirItemFromFile(filePath) {
|
|
449
|
-
return {
|
|
450
|
-
name: basename(filePath),
|
|
451
|
-
fullPath: filePath,
|
|
452
|
-
isFile: true, isDirectory: false, isSymbolicLink: false
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
export function dirItemFromDir(dirPath) {
|
|
456
|
-
return {
|
|
457
|
-
name: basename(dirPath),
|
|
458
|
-
fullPath: dirPath,
|
|
459
|
-
isFile: false, isDirectory: true, isSymbolicLink: false
|
|
460
|
-
};
|
|
461
|
-
}
|
|
462
|
-
export function listDir(dirPath) {
|
|
463
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
464
|
-
var ditItems, result, _i, ditItems_1, dirItem, toAdd, stats;
|
|
465
|
-
return __generator(this, function (_a) {
|
|
466
|
-
switch (_a.label) {
|
|
467
|
-
case 0: return [4 /*yield*/, isDirectory(dirPath)];
|
|
468
|
-
case 1:
|
|
469
|
-
if (!(_a.sent()))
|
|
470
|
-
return [2 /*return*/, []];
|
|
471
|
-
return [4 /*yield*/, fs.readdir(dirPath)];
|
|
472
|
-
case 2:
|
|
473
|
-
ditItems = _a.sent();
|
|
474
|
-
result = [];
|
|
475
|
-
_i = 0, ditItems_1 = ditItems;
|
|
476
|
-
_a.label = 3;
|
|
477
|
-
case 3:
|
|
478
|
-
if (!(_i < ditItems_1.length)) return [3 /*break*/, 6];
|
|
479
|
-
dirItem = ditItems_1[_i];
|
|
480
|
-
toAdd = {
|
|
481
|
-
name: dirItem,
|
|
482
|
-
fullPath: path.join(dirPath, dirItem),
|
|
483
|
-
isFile: false, isDirectory: false, isSymbolicLink: false
|
|
484
|
-
};
|
|
485
|
-
return [4 /*yield*/, fs.stat(toAdd.fullPath)];
|
|
486
|
-
case 4:
|
|
487
|
-
stats = _a.sent();
|
|
488
|
-
if (stats.isFile()) {
|
|
489
|
-
toAdd.isFile = true;
|
|
490
|
-
toAdd.isDirectory = false;
|
|
491
|
-
toAdd.isSymbolicLink = false;
|
|
492
|
-
}
|
|
493
|
-
else if (stats.isDirectory()) {
|
|
494
|
-
toAdd.isDirectory = true;
|
|
495
|
-
toAdd.isFile = false;
|
|
496
|
-
toAdd.isSymbolicLink = false;
|
|
497
|
-
}
|
|
498
|
-
else if (stats.isSymbolicLink()) {
|
|
499
|
-
toAdd.isSymbolicLink = true;
|
|
500
|
-
toAdd.isDirectory = false;
|
|
501
|
-
toAdd.isFile = false;
|
|
502
|
-
}
|
|
503
|
-
else {
|
|
504
|
-
return [3 /*break*/, 5];
|
|
505
|
-
}
|
|
506
|
-
result.push(toAdd);
|
|
507
|
-
_a.label = 5;
|
|
508
|
-
case 5:
|
|
509
|
-
_i++;
|
|
510
|
-
return [3 /*break*/, 3];
|
|
511
|
-
case 6: return [2 /*return*/, result];
|
|
512
|
-
}
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
var MEGA = 1024 * 1024;
|
|
517
|
-
function calcFileHash_bun(filePath) {
|
|
518
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
519
|
-
var file, _a, _b;
|
|
520
|
-
return __generator(this, function (_c) {
|
|
521
|
-
switch (_c.label) {
|
|
522
|
-
case 0:
|
|
523
|
-
file = Bun.file(filePath);
|
|
524
|
-
return [4 /*yield*/, file.exists()];
|
|
525
|
-
case 1:
|
|
526
|
-
if (!(_c.sent()))
|
|
527
|
-
return [2 /*return*/, undefined];
|
|
528
|
-
if (file.size > 10 * MEGA) {
|
|
529
|
-
return [2 /*return*/, calcFileHash_bun_streamed(file)];
|
|
530
|
-
}
|
|
531
|
-
_b = (_a = Bun).hash;
|
|
532
|
-
return [4 /*yield*/, file.arrayBuffer()];
|
|
533
|
-
case 2: return [2 /*return*/, _b.apply(_a, [_c.sent(), 12346]).toString()];
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
function calcFileHash_bun_streamed(file) {
|
|
539
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
540
|
-
var stream, reader, hasher, _a, done, value;
|
|
541
|
-
return __generator(this, function (_b) {
|
|
542
|
-
switch (_b.label) {
|
|
543
|
-
case 0:
|
|
544
|
-
stream = file.stream();
|
|
545
|
-
reader = stream.getReader();
|
|
546
|
-
hasher = new Bun.CryptoHasher("sha256");
|
|
547
|
-
_b.label = 1;
|
|
548
|
-
case 1:
|
|
549
|
-
_b.trys.push([1, , 5, 6]);
|
|
550
|
-
_b.label = 2;
|
|
551
|
-
case 2:
|
|
552
|
-
if (!true) return [3 /*break*/, 4];
|
|
553
|
-
return [4 /*yield*/, reader.read()];
|
|
554
|
-
case 3:
|
|
555
|
-
_a = _b.sent(), done = _a.done, value = _a.value;
|
|
556
|
-
if (done)
|
|
557
|
-
return [3 /*break*/, 4];
|
|
558
|
-
hasher.update(value);
|
|
559
|
-
return [3 /*break*/, 2];
|
|
560
|
-
case 4: return [2 /*return*/, hasher.digest("hex")];
|
|
561
|
-
case 5:
|
|
562
|
-
reader.releaseLock();
|
|
563
|
-
return [7 /*endfinally*/];
|
|
564
|
-
case 6: return [2 /*return*/];
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
function calcFileHash_node(filePath) {
|
|
570
|
-
if (!isFile(filePath))
|
|
571
|
-
return Promise.resolve(undefined);
|
|
572
|
-
return new Promise(function (resolve, reject) {
|
|
573
|
-
var hash = createHash('sha256');
|
|
574
|
-
var stream = createReadStream(filePath);
|
|
575
|
-
stream.on('data', function (data) { return hash.update(data); });
|
|
576
|
-
stream.on('end', function () { return resolve(hash.digest('hex')); });
|
|
577
|
-
stream.on('error', function (error) { return reject(error); });
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
/**
|
|
581
|
-
* Calculate the hash of a file.
|
|
582
|
-
* Allows using it for HTTP ETag or another change proof.
|
|
583
|
-
* This version is optimized to use streams and avoid loading the whole file in memory.
|
|
584
|
-
*/
|
|
585
|
-
export var calcFileHash = isBunJS ? calcFileHash_bun : calcFileHash_node;
|
|
586
|
-
/**
|
|
587
|
-
* Convert a simple win32 path to a linux path.
|
|
588
|
-
*/
|
|
589
|
-
export function win32ToLinuxPath(filePath) {
|
|
590
|
-
return filePath.replace(/\\/g, '/');
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Copy a directory recursively.
|
|
594
|
-
* Is optimized for large files.
|
|
595
|
-
*/
|
|
596
|
-
export function copyDirectory(srcDir, destDir) {
|
|
597
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
598
|
-
var entries;
|
|
599
|
-
var _this = this;
|
|
600
|
-
return __generator(this, function (_a) {
|
|
601
|
-
switch (_a.label) {
|
|
602
|
-
case 0: return [4 /*yield*/, isDirectory(srcDir)];
|
|
603
|
-
case 1:
|
|
604
|
-
if (!(_a.sent())) {
|
|
605
|
-
throw new Error("Directory doesn't exist : ".concat(srcDir));
|
|
606
|
-
}
|
|
607
|
-
return [4 /*yield*/, mkDir(destDir)];
|
|
608
|
-
case 2:
|
|
609
|
-
_a.sent();
|
|
610
|
-
return [4 /*yield*/, fs.readdir(srcDir, { withFileTypes: true })];
|
|
611
|
-
case 3:
|
|
612
|
-
entries = _a.sent();
|
|
613
|
-
return [4 /*yield*/, Promise.all(entries.map(function (entry) { return __awaiter(_this, void 0, void 0, function () {
|
|
614
|
-
var srcPath, destPath, _a;
|
|
615
|
-
return __generator(this, function (_b) {
|
|
616
|
-
switch (_b.label) {
|
|
617
|
-
case 0:
|
|
618
|
-
srcPath = path.join(srcDir, entry.name);
|
|
619
|
-
destPath = path.join(destDir, entry.name);
|
|
620
|
-
if (!entry.isDirectory()) return [3 /*break*/, 2];
|
|
621
|
-
return [4 /*yield*/, copyDirectory(srcPath, destPath)];
|
|
622
|
-
case 1:
|
|
623
|
-
_b.sent();
|
|
624
|
-
return [3 /*break*/, 5];
|
|
625
|
-
case 2:
|
|
626
|
-
_b.trys.push([2, 4, , 5]);
|
|
627
|
-
return [4 /*yield*/, copyFile(srcPath, destPath)];
|
|
628
|
-
case 3:
|
|
629
|
-
_b.sent();
|
|
630
|
-
return [3 /*break*/, 5];
|
|
631
|
-
case 4:
|
|
632
|
-
_a = _b.sent();
|
|
633
|
-
console.warn("jk_fs.copyDirectory - Failed to copy file ".concat(srcPath));
|
|
634
|
-
return [3 /*break*/, 5];
|
|
635
|
-
case 5: return [2 /*return*/];
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
}); }))];
|
|
639
|
-
case 4:
|
|
640
|
-
_a.sent();
|
|
641
|
-
return [2 /*return*/];
|
|
642
|
-
}
|
|
643
|
-
});
|
|
644
|
-
});
|
|
645
|
-
}
|
|
646
|
-
/**
|
|
647
|
-
* Copy of a file.
|
|
648
|
-
* Is optimized for large files.
|
|
649
|
-
*/
|
|
650
|
-
export function copyFile(srcPath, destPath) {
|
|
651
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
652
|
-
var stat, symStat;
|
|
653
|
-
return __generator(this, function (_a) {
|
|
654
|
-
switch (_a.label) {
|
|
655
|
-
case 0: return [4 /*yield*/, getFileStat(srcPath)];
|
|
656
|
-
case 1:
|
|
657
|
-
stat = _a.sent();
|
|
658
|
-
if (!stat)
|
|
659
|
-
return [2 /*return*/];
|
|
660
|
-
if (!stat.isSymbolicLink()) return [3 /*break*/, 3];
|
|
661
|
-
return [4 /*yield*/, fs.lstat(srcPath)];
|
|
662
|
-
case 2:
|
|
663
|
-
symStat = _a.sent();
|
|
664
|
-
if (!symStat)
|
|
665
|
-
return [2 /*return*/];
|
|
666
|
-
if (!symStat.isFile())
|
|
667
|
-
return [2 /*return*/];
|
|
668
|
-
_a.label = 3;
|
|
669
|
-
case 3: return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
670
|
-
var readStream = createReadStream(srcPath);
|
|
671
|
-
var writeStream = createWriteStream(destPath);
|
|
672
|
-
readStream.on('error', reject);
|
|
673
|
-
writeStream.on('error', reject);
|
|
674
|
-
writeStream.on('finish', resolve);
|
|
675
|
-
readStream.pipe(writeStream);
|
|
676
|
-
})];
|
|
677
|
-
}
|
|
678
|
-
});
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
/**
|
|
682
|
-
* Unzip a .zip file in an optimized way.
|
|
683
|
-
* Note was using : "@types/unzipper": "^0.10.11"
|
|
684
|
-
* which has some bug, files/folders was forgottens.
|
|
685
|
-
* import * as unzipper from "unzipper";
|
|
686
|
-
*/
|
|
687
|
-
/*export async function unzipFile_old(zipFilePath: string, outputDir: string): Promise<void> {
|
|
688
|
-
if (!await isFile(zipFilePath)) {
|
|
689
|
-
throw new Error(`File doesn't exist : ${zipFilePath}`);
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
if (!await isDirectory(outputDir)) {
|
|
693
|
-
await mkDir(outputDir);
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
await createReadStream(zipFilePath)
|
|
697
|
-
.pipe(unzipper.Extract({path: outputDir}))
|
|
698
|
-
.promise();
|
|
699
|
-
}*/
|
|
700
|
-
/**
|
|
701
|
-
* Unzip a .zip file in an optimized way.
|
|
702
|
-
*/
|
|
703
|
-
export function unzipFile(zipFilePath, outputDir) {
|
|
704
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
705
|
-
var zip;
|
|
706
|
-
return __generator(this, function (_a) {
|
|
707
|
-
switch (_a.label) {
|
|
708
|
-
case 0: return [4 /*yield*/, isFile(zipFilePath)];
|
|
709
|
-
case 1:
|
|
710
|
-
if (!(_a.sent())) {
|
|
711
|
-
throw new Error("File doesn't exist : ".concat(zipFilePath));
|
|
712
|
-
}
|
|
713
|
-
return [4 /*yield*/, isDirectory(outputDir)];
|
|
714
|
-
case 2:
|
|
715
|
-
if (!!(_a.sent())) return [3 /*break*/, 4];
|
|
716
|
-
return [4 /*yield*/, mkDir(outputDir)];
|
|
717
|
-
case 3:
|
|
718
|
-
_a.sent();
|
|
719
|
-
_a.label = 4;
|
|
720
|
-
case 4:
|
|
721
|
-
zip = new AdmZip(zipFilePath);
|
|
722
|
-
zip.extractAllTo(outputDir, true);
|
|
723
|
-
return [2 /*return*/];
|
|
724
|
-
}
|
|
725
|
-
});
|
|
726
|
-
});
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Create a temporary directory.
|
|
730
|
-
* Return an object containing the directory path and a cleanup function.
|
|
731
|
-
*/
|
|
732
|
-
export function createTempDir(prefix) {
|
|
733
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
734
|
-
var dirPath;
|
|
735
|
-
var _this = this;
|
|
736
|
-
return __generator(this, function (_a) {
|
|
737
|
-
switch (_a.label) {
|
|
738
|
-
case 0: return [4 /*yield*/, fs.mkdtemp(prefix)];
|
|
739
|
-
case 1:
|
|
740
|
-
dirPath = _a.sent();
|
|
741
|
-
return [2 /*return*/, {
|
|
742
|
-
path: dirPath,
|
|
743
|
-
remove: function () { return __awaiter(_this, void 0, void 0, function () {
|
|
744
|
-
return __generator(this, function (_a) {
|
|
745
|
-
return [2 /*return*/, fs.rm(dirPath, { recursive: true, force: true })];
|
|
746
|
-
});
|
|
747
|
-
}); }
|
|
748
|
-
}];
|
|
749
|
-
}
|
|
750
|
-
});
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
//region Node.js functions
|
|
754
|
-
export var join = path.join;
|
|
755
|
-
export var resolve = path.resolve;
|
|
756
|
-
export var dirname = path.dirname;
|
|
757
|
-
export var extname = path.extname;
|
|
758
|
-
export var sep = path.sep;
|
|
759
|
-
export var isAbsolute = path.isAbsolute;
|
|
760
|
-
export var normalize = path.normalize;
|
|
761
|
-
export var basename = path.basename;
|
|
762
|
-
export var symlink = fs.symlink;
|
|
763
|
-
export var rename = fs.rename;
|
|
764
|
-
//endregion
|