@dbcube/core 1.0.24 → 1.0.28
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/bin.cjs +143 -120
- package/dist/bin.cjs.map +1 -1
- package/dist/bin.js +143 -120
- package/dist/bin.js.map +1 -1
- package/dist/index.cjs +352 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +360 -60
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
- package/dist/install.cjs +0 -331
- package/dist/install.cjs.map +0 -1
- package/dist/install.d.mts +0 -2
- package/dist/install.d.ts +0 -2
- package/dist/install.js +0 -307
- package/dist/install.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
// src/lib/Engine.ts
|
|
9
|
-
import
|
|
9
|
+
import path3 from "path";
|
|
10
10
|
|
|
11
11
|
// src/lib/Arquitecture.ts
|
|
12
12
|
import * as os from "os";
|
|
@@ -130,18 +130,314 @@ var Arquitecture = class {
|
|
|
130
130
|
}
|
|
131
131
|
};
|
|
132
132
|
|
|
133
|
+
// src/lib/Donwloader.ts
|
|
134
|
+
import * as fs from "fs";
|
|
135
|
+
import * as path from "path";
|
|
136
|
+
import * as os2 from "os";
|
|
137
|
+
import { https } from "follow-redirects";
|
|
138
|
+
import * as unzipper from "unzipper";
|
|
139
|
+
var Downloader = class {
|
|
140
|
+
static get(prefix) {
|
|
141
|
+
const arch2 = new Arquitecture();
|
|
142
|
+
const platform2 = arch2.getPlatform();
|
|
143
|
+
const architecture = arch2.getArchitecture();
|
|
144
|
+
const platformMap = {
|
|
145
|
+
windows: "windows",
|
|
146
|
+
linux: "linux",
|
|
147
|
+
darwin: "macos"
|
|
148
|
+
};
|
|
149
|
+
const archMap = {
|
|
150
|
+
x86_64: "x64",
|
|
151
|
+
aarch64: "arm64"
|
|
152
|
+
};
|
|
153
|
+
const plat = platformMap[platform2];
|
|
154
|
+
const archSuffix = archMap[architecture];
|
|
155
|
+
if (plat && archSuffix) {
|
|
156
|
+
const baseName = `${prefix}-engine-${plat}-${archSuffix}`;
|
|
157
|
+
const binaryName = platform2 === "windows" ? `${baseName}.exe` : baseName;
|
|
158
|
+
const url = `https://github.com/Dbcube/binaries/releases/download/${prefix}-engine/${prefix}-engine-latest-${plat}-${archSuffix}.zip`;
|
|
159
|
+
return {
|
|
160
|
+
name: binaryName,
|
|
161
|
+
url,
|
|
162
|
+
query_engine: binaryName,
|
|
163
|
+
schema_engine: `${prefix}-engine-${plat}-${archSuffix}${platform2 === "windows" ? ".exe" : ""}`
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
name: "",
|
|
168
|
+
url: "",
|
|
169
|
+
query_engine: "",
|
|
170
|
+
schema_engine: ""
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
static async download(targetDir) {
|
|
174
|
+
const binaries = ["schema", "query", "sqlite"];
|
|
175
|
+
const binDir = targetDir || this.getDefaultBinDir();
|
|
176
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
177
|
+
for (const prefix of binaries) {
|
|
178
|
+
const maxRetries = 3;
|
|
179
|
+
let attempt = 0;
|
|
180
|
+
while (attempt <= maxRetries) {
|
|
181
|
+
try {
|
|
182
|
+
console.log(`\u{1F4E5} Descargando ${prefix} (intento ${attempt + 1}/${maxRetries + 1})...`);
|
|
183
|
+
const binaryInfo = this.get(prefix);
|
|
184
|
+
if (!binaryInfo.name || !binaryInfo.url) {
|
|
185
|
+
throw new Error("Plataforma o arquitectura no soportada");
|
|
186
|
+
}
|
|
187
|
+
const tempZipPath = path.join(os2.tmpdir(), `dbcube-${prefix}-${Date.now()}.zip`);
|
|
188
|
+
const finalBinaryPath = path.join(binDir, binaryInfo.name);
|
|
189
|
+
if (fs.existsSync(finalBinaryPath)) {
|
|
190
|
+
console.log(`\u2705 ${prefix} ya existe, omitiendo`);
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
await this.downloadFile(binaryInfo.url, tempZipPath, prefix);
|
|
194
|
+
await this.extractBinary(tempZipPath, finalBinaryPath, prefix);
|
|
195
|
+
console.log(`\u2705 ${prefix} descargado correctamente`);
|
|
196
|
+
break;
|
|
197
|
+
} catch (error) {
|
|
198
|
+
const errorMessage = error instanceof Error ? error.message : "Error desconocido";
|
|
199
|
+
if (attempt < maxRetries && (errorMessage.includes("ECONNRESET") || errorMessage.includes("timeout") || errorMessage.includes("ETIMEDOUT") || errorMessage.includes("ENOTFOUND"))) {
|
|
200
|
+
console.warn(`\u26A0\uFE0F Error de red para ${prefix}: ${errorMessage}`);
|
|
201
|
+
console.log(`\u{1F504} Reintentando en 3 segundos...`);
|
|
202
|
+
attempt++;
|
|
203
|
+
await new Promise((resolve5) => setTimeout(resolve5, 3e3));
|
|
204
|
+
} else {
|
|
205
|
+
console.error(`\u274C Error final descargando ${prefix}:`, errorMessage);
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
static downloadFile(url, outputPath, prefix) {
|
|
213
|
+
return new Promise((resolve5, reject) => {
|
|
214
|
+
const request = https.get(url, { timeout: 6e4 }, (response) => {
|
|
215
|
+
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
216
|
+
const redirectUrl = response.headers.location;
|
|
217
|
+
if (redirectUrl) {
|
|
218
|
+
return this.downloadFile(redirectUrl, outputPath, prefix).then(resolve5).catch(reject);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (response.statusCode !== 200) {
|
|
222
|
+
reject(new Error(`HTTP ${response.statusCode} para ${prefix}`));
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const file = fs.createWriteStream(outputPath);
|
|
226
|
+
const totalBytes = parseInt(response.headers["content-length"] || "0", 10);
|
|
227
|
+
let downloadedBytes = 0;
|
|
228
|
+
if (totalBytes > 0) {
|
|
229
|
+
console.log(`\u{1F4E6} Descargando ${(totalBytes / 1024 / 1024).toFixed(1)}MB...`);
|
|
230
|
+
}
|
|
231
|
+
response.on("data", (chunk) => {
|
|
232
|
+
downloadedBytes += chunk.length;
|
|
233
|
+
file.write(chunk);
|
|
234
|
+
if (totalBytes > 0) {
|
|
235
|
+
const progress = (downloadedBytes / totalBytes * 100).toFixed(1);
|
|
236
|
+
process.stdout.write(`\r\u23F3 Progreso: ${progress}%`);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
response.on("end", () => {
|
|
240
|
+
file.end();
|
|
241
|
+
console.log(`
|
|
242
|
+
\u2705 Descarga de ${prefix} completada`);
|
|
243
|
+
resolve5();
|
|
244
|
+
});
|
|
245
|
+
response.on("error", (err) => {
|
|
246
|
+
file.close();
|
|
247
|
+
this.cleanupFile(outputPath);
|
|
248
|
+
reject(err);
|
|
249
|
+
});
|
|
250
|
+
file.on("error", (err) => {
|
|
251
|
+
file.close();
|
|
252
|
+
this.cleanupFile(outputPath);
|
|
253
|
+
reject(err);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
request.on("error", reject);
|
|
257
|
+
request.on("timeout", () => {
|
|
258
|
+
request.destroy();
|
|
259
|
+
reject(new Error(`Timeout descargando ${prefix}`));
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
static extractBinary(zipPath, outputPath, prefix) {
|
|
264
|
+
return new Promise((resolve5, reject) => {
|
|
265
|
+
let extracted = false;
|
|
266
|
+
fs.createReadStream(zipPath).pipe(unzipper.Parse()).on("entry", (entry) => {
|
|
267
|
+
if (entry.type === "File" && !extracted) {
|
|
268
|
+
extracted = true;
|
|
269
|
+
const writeStream = fs.createWriteStream(outputPath);
|
|
270
|
+
entry.pipe(writeStream);
|
|
271
|
+
writeStream.on("finish", () => {
|
|
272
|
+
if (process.platform !== "win32") {
|
|
273
|
+
fs.chmodSync(outputPath, 493);
|
|
274
|
+
}
|
|
275
|
+
this.cleanupFile(zipPath);
|
|
276
|
+
console.log(`\u{1F4C1} ${prefix} extra\xEDdo correctamente`);
|
|
277
|
+
resolve5();
|
|
278
|
+
});
|
|
279
|
+
writeStream.on("error", (err) => {
|
|
280
|
+
this.cleanupFile(zipPath);
|
|
281
|
+
reject(err);
|
|
282
|
+
});
|
|
283
|
+
} else {
|
|
284
|
+
entry.autodrain();
|
|
285
|
+
}
|
|
286
|
+
}).on("error", (err) => {
|
|
287
|
+
this.cleanupFile(zipPath);
|
|
288
|
+
reject(err);
|
|
289
|
+
}).on("close", () => {
|
|
290
|
+
if (!extracted) {
|
|
291
|
+
this.cleanupFile(zipPath);
|
|
292
|
+
reject(new Error(`No se encontr\xF3 archivo v\xE1lido en el ZIP para ${prefix}`));
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
static cleanupFile(filePath) {
|
|
298
|
+
try {
|
|
299
|
+
if (fs.existsSync(filePath)) {
|
|
300
|
+
fs.unlinkSync(filePath);
|
|
301
|
+
}
|
|
302
|
+
} catch {
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
static getDefaultBinDir() {
|
|
306
|
+
const possibleDirs = [
|
|
307
|
+
path.resolve(process.cwd(), "bin"),
|
|
308
|
+
path.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
|
|
309
|
+
path.resolve(__dirname, "..", "bin")
|
|
310
|
+
];
|
|
311
|
+
for (const dir of possibleDirs) {
|
|
312
|
+
try {
|
|
313
|
+
if (!fs.existsSync(dir)) {
|
|
314
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
315
|
+
}
|
|
316
|
+
const testFile = path.join(dir, ".test");
|
|
317
|
+
fs.writeFileSync(testFile, "test");
|
|
318
|
+
fs.unlinkSync(testFile);
|
|
319
|
+
return dir;
|
|
320
|
+
} catch {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const tempDir = path.join(os2.tmpdir(), "dbcube-bin");
|
|
325
|
+
fs.mkdirSync(tempDir, { recursive: true });
|
|
326
|
+
return tempDir;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
133
330
|
// src/lib/Binary.ts
|
|
331
|
+
import * as fs2 from "fs";
|
|
332
|
+
import * as path2 from "path";
|
|
134
333
|
var Binary = class {
|
|
135
|
-
static
|
|
334
|
+
static isDownloading = false;
|
|
335
|
+
static downloadPromise = null;
|
|
336
|
+
static async ensureBinariesExist() {
|
|
337
|
+
if (this.isDownloading && this.downloadPromise) {
|
|
338
|
+
await this.downloadPromise;
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const binDir = this.getBinDir();
|
|
342
|
+
const binaries = ["schema", "query", "sqlite"];
|
|
343
|
+
const allExist = binaries.every((prefix) => {
|
|
344
|
+
const binaryInfo = Downloader.get(prefix);
|
|
345
|
+
if (!binaryInfo.name) return false;
|
|
346
|
+
const binaryPath = path2.join(binDir, binaryInfo.name);
|
|
347
|
+
return fs2.existsSync(binaryPath);
|
|
348
|
+
});
|
|
349
|
+
if (allExist) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (!this.isDownloading) {
|
|
353
|
+
this.isDownloading = true;
|
|
354
|
+
this.downloadPromise = this.downloadBinaries();
|
|
355
|
+
try {
|
|
356
|
+
await this.downloadPromise;
|
|
357
|
+
} finally {
|
|
358
|
+
this.isDownloading = false;
|
|
359
|
+
this.downloadPromise = null;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
static async downloadBinaries() {
|
|
364
|
+
console.log("\u{1F680} DBCube: Descargando binarios necesarios...");
|
|
365
|
+
try {
|
|
366
|
+
const binDir = this.getBinDir();
|
|
367
|
+
await Downloader.download(binDir);
|
|
368
|
+
console.log("\u2705 DBCube: Binarios descargados correctamente");
|
|
369
|
+
} catch (error) {
|
|
370
|
+
console.warn("\u26A0\uFE0F DBCube: Error descargando binarios:", error.message);
|
|
371
|
+
console.log("\u{1F527} Los binarios se intentar\xE1n descargar en la pr\xF3xima ejecuci\xF3n");
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
static getBinDir() {
|
|
375
|
+
const possibleDirs = [
|
|
376
|
+
path2.resolve(process.cwd(), "bin"),
|
|
377
|
+
path2.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
|
|
378
|
+
path2.resolve(__dirname, "..", "bin")
|
|
379
|
+
];
|
|
380
|
+
for (const dir of possibleDirs) {
|
|
381
|
+
try {
|
|
382
|
+
if (!fs2.existsSync(dir)) {
|
|
383
|
+
fs2.mkdirSync(dir, { recursive: true });
|
|
384
|
+
}
|
|
385
|
+
return dir;
|
|
386
|
+
} catch {
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return process.cwd();
|
|
391
|
+
}
|
|
392
|
+
static async get() {
|
|
393
|
+
await this.ensureBinariesExist();
|
|
136
394
|
const arch2 = new Arquitecture();
|
|
137
395
|
const platform2 = arch2.getPlatform();
|
|
138
396
|
const architecture = arch2.getArchitecture();
|
|
397
|
+
const binDir = this.getBinDir();
|
|
398
|
+
const getFullPath = (binaryName) => {
|
|
399
|
+
return path2.join(binDir, binaryName);
|
|
400
|
+
};
|
|
139
401
|
switch (platform2) {
|
|
140
402
|
case "windows":
|
|
141
403
|
if (architecture == "x86_64") {
|
|
142
404
|
return {
|
|
143
|
-
query_engine: "query-engine-windows-x64.exe",
|
|
144
|
-
schema_engine: "schema-engine-windows-x64.exe"
|
|
405
|
+
query_engine: getFullPath("query-engine-windows-x64.exe"),
|
|
406
|
+
schema_engine: getFullPath("schema-engine-windows-x64.exe")
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
if (architecture == "aarch64") {
|
|
410
|
+
return {
|
|
411
|
+
query_engine: getFullPath("query-engine-windows-arm64.exe"),
|
|
412
|
+
schema_engine: getFullPath("schema-engine-windows-arm64.exe")
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
case "linux":
|
|
417
|
+
if (architecture == "x86_64") {
|
|
418
|
+
return {
|
|
419
|
+
query_engine: getFullPath("query-engine-linux-x64"),
|
|
420
|
+
schema_engine: getFullPath("schema-engine-linux-x64")
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
if (architecture == "aarch64") {
|
|
424
|
+
return {
|
|
425
|
+
query_engine: getFullPath("query-engine-linux-arm64"),
|
|
426
|
+
schema_engine: getFullPath("schema-engine-linux-arm64")
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
case "macos":
|
|
431
|
+
if (architecture == "x86_64") {
|
|
432
|
+
return {
|
|
433
|
+
query_engine: getFullPath("query-engine-macos-x64"),
|
|
434
|
+
schema_engine: getFullPath("schema-engine-macos-x64")
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
if (architecture == "aarch64") {
|
|
438
|
+
return {
|
|
439
|
+
query_engine: getFullPath("query-engine-macos-arm64"),
|
|
440
|
+
schema_engine: getFullPath("schema-engine-macos-arm64")
|
|
145
441
|
};
|
|
146
442
|
}
|
|
147
443
|
break;
|
|
@@ -198,19 +494,19 @@ var Engine = class {
|
|
|
198
494
|
name;
|
|
199
495
|
config;
|
|
200
496
|
arguments;
|
|
201
|
-
binary;
|
|
497
|
+
binary = null;
|
|
202
498
|
timeout;
|
|
203
499
|
constructor(name, timeout = 3e4) {
|
|
204
500
|
this.name = name;
|
|
205
501
|
this.config = this.setConfig(name);
|
|
206
|
-
const binary = Binary.get();
|
|
207
|
-
this.binary = {
|
|
208
|
-
query_engine: path.resolve(__dirname, "../bin", binary.query_engine),
|
|
209
|
-
schema_engine: path.resolve(__dirname, "../bin", binary.schema_engine)
|
|
210
|
-
};
|
|
211
502
|
this.arguments = this.setArguments();
|
|
212
503
|
this.timeout = timeout;
|
|
213
504
|
}
|
|
505
|
+
async initializeBinary() {
|
|
506
|
+
if (!this.binary) {
|
|
507
|
+
this.binary = await Binary.get();
|
|
508
|
+
}
|
|
509
|
+
}
|
|
214
510
|
setArguments() {
|
|
215
511
|
let args = [];
|
|
216
512
|
if (this.config.type == "sqlite") {
|
|
@@ -248,7 +544,7 @@ var Engine = class {
|
|
|
248
544
|
}
|
|
249
545
|
setConfig(name) {
|
|
250
546
|
const configInstance = new Config();
|
|
251
|
-
const configFilePath =
|
|
547
|
+
const configFilePath = path3.resolve(process.cwd(), "dbcube.config.js");
|
|
252
548
|
const configFn = __require(configFilePath);
|
|
253
549
|
if (typeof configFn === "function") {
|
|
254
550
|
configFn(configInstance);
|
|
@@ -261,7 +557,11 @@ var Engine = class {
|
|
|
261
557
|
return this.config;
|
|
262
558
|
}
|
|
263
559
|
async run(binary, args) {
|
|
264
|
-
|
|
560
|
+
await this.initializeBinary();
|
|
561
|
+
if (!this.binary) {
|
|
562
|
+
throw new Error("Binary not initialized");
|
|
563
|
+
}
|
|
564
|
+
return new Promise((resolve5, reject) => {
|
|
265
565
|
const child = spawn(this.binary[binary], [...this.arguments, ...args]);
|
|
266
566
|
let stdoutBuffer = "";
|
|
267
567
|
let stderrBuffer = "";
|
|
@@ -277,7 +577,7 @@ var Engine = class {
|
|
|
277
577
|
if (!isResolved) {
|
|
278
578
|
isResolved = true;
|
|
279
579
|
clearTimeout(timeoutId);
|
|
280
|
-
|
|
580
|
+
resolve5(response);
|
|
281
581
|
}
|
|
282
582
|
};
|
|
283
583
|
child.stdout.on("data", (data) => {
|
|
@@ -347,7 +647,7 @@ var Engine = class {
|
|
|
347
647
|
|
|
348
648
|
// src/lib/SqliteExecutor.ts
|
|
349
649
|
import { exec } from "child_process";
|
|
350
|
-
import * as
|
|
650
|
+
import * as path4 from "path";
|
|
351
651
|
import { promisify } from "util";
|
|
352
652
|
var execAsync = promisify(exec);
|
|
353
653
|
var SqliteExecutor = class {
|
|
@@ -358,11 +658,11 @@ var SqliteExecutor = class {
|
|
|
358
658
|
this.binaryPath = this.getBinaryPath();
|
|
359
659
|
}
|
|
360
660
|
getBinaryPath() {
|
|
361
|
-
const binDir =
|
|
661
|
+
const binDir = path4.resolve(__dirname, "..", "bin");
|
|
362
662
|
const platform2 = process.platform;
|
|
363
663
|
const extension = platform2 === "win32" ? ".exe" : "";
|
|
364
664
|
const binaryName = `sqlite-engine${extension}`;
|
|
365
|
-
return
|
|
665
|
+
return path4.join(binDir, binaryName);
|
|
366
666
|
}
|
|
367
667
|
async executeBinary(args) {
|
|
368
668
|
const command = `"${this.binaryPath}" ${args.map((arg) => `"${arg}"`).join(" ")}`;
|
|
@@ -504,9 +804,9 @@ var SqliteExecutor = class {
|
|
|
504
804
|
};
|
|
505
805
|
|
|
506
806
|
// src/lib/DbConfig.ts
|
|
507
|
-
import * as
|
|
508
|
-
import
|
|
509
|
-
var rootPath =
|
|
807
|
+
import * as path5 from "path";
|
|
808
|
+
import fs3 from "fs";
|
|
809
|
+
var rootPath = path5.resolve(process.cwd(), ".dbcube");
|
|
510
810
|
var SQLite = class {
|
|
511
811
|
executor = null;
|
|
512
812
|
database;
|
|
@@ -516,11 +816,11 @@ var SQLite = class {
|
|
|
516
816
|
async ifExist() {
|
|
517
817
|
if (this.database) {
|
|
518
818
|
const dbPath = this.database || ":memory:";
|
|
519
|
-
const configPath =
|
|
520
|
-
if (!
|
|
521
|
-
|
|
819
|
+
const configPath = path5.join(rootPath, dbPath + ".db");
|
|
820
|
+
if (!fs3.existsSync(rootPath)) {
|
|
821
|
+
fs3.mkdirSync(rootPath, { recursive: true });
|
|
522
822
|
}
|
|
523
|
-
if (
|
|
823
|
+
if (fs3.existsSync(configPath)) {
|
|
524
824
|
return true;
|
|
525
825
|
}
|
|
526
826
|
if (!this.executor) {
|
|
@@ -531,13 +831,13 @@ var SQLite = class {
|
|
|
531
831
|
return false;
|
|
532
832
|
}
|
|
533
833
|
async connect() {
|
|
534
|
-
return new Promise(async (
|
|
834
|
+
return new Promise(async (resolve5, reject) => {
|
|
535
835
|
try {
|
|
536
836
|
if (!this.executor) {
|
|
537
837
|
const dbPath = this.database || ":memory:";
|
|
538
|
-
const configPath =
|
|
539
|
-
if (!
|
|
540
|
-
|
|
838
|
+
const configPath = path5.join(rootPath, dbPath + ".db");
|
|
839
|
+
if (!fs3.existsSync(rootPath)) {
|
|
840
|
+
fs3.mkdirSync(rootPath, { recursive: true });
|
|
541
841
|
}
|
|
542
842
|
this.executor = new SqliteExecutor(configPath);
|
|
543
843
|
const connected = await this.executor.connect();
|
|
@@ -545,22 +845,22 @@ var SQLite = class {
|
|
|
545
845
|
throw new Error("Failed to connect to SQLite database");
|
|
546
846
|
}
|
|
547
847
|
}
|
|
548
|
-
|
|
848
|
+
resolve5(this.executor);
|
|
549
849
|
} catch (error) {
|
|
550
850
|
reject(error);
|
|
551
851
|
}
|
|
552
852
|
});
|
|
553
853
|
}
|
|
554
854
|
async disconnect() {
|
|
555
|
-
return new Promise((
|
|
855
|
+
return new Promise((resolve5) => {
|
|
556
856
|
if (this.executor) {
|
|
557
857
|
this.executor = null;
|
|
558
858
|
}
|
|
559
|
-
|
|
859
|
+
resolve5();
|
|
560
860
|
});
|
|
561
861
|
}
|
|
562
862
|
async query(sqlQuery) {
|
|
563
|
-
return new Promise(async (
|
|
863
|
+
return new Promise(async (resolve5) => {
|
|
564
864
|
try {
|
|
565
865
|
if (typeof sqlQuery !== "string") {
|
|
566
866
|
throw new Error("The SQL query must be a string.");
|
|
@@ -573,20 +873,20 @@ var SQLite = class {
|
|
|
573
873
|
}
|
|
574
874
|
const result = await this.executor.queryMultiple(sqlQuery);
|
|
575
875
|
if (result.status === "error") {
|
|
576
|
-
|
|
876
|
+
resolve5({
|
|
577
877
|
status: "error",
|
|
578
878
|
message: result.message,
|
|
579
879
|
data: null
|
|
580
880
|
});
|
|
581
881
|
} else {
|
|
582
|
-
|
|
882
|
+
resolve5({
|
|
583
883
|
status: "success",
|
|
584
884
|
message: "Query executed successfully",
|
|
585
885
|
data: result.data
|
|
586
886
|
});
|
|
587
887
|
}
|
|
588
888
|
} catch (error) {
|
|
589
|
-
|
|
889
|
+
resolve5({
|
|
590
890
|
status: "error",
|
|
591
891
|
message: error.message || "An error occurred while executing the query.",
|
|
592
892
|
data: null
|
|
@@ -595,7 +895,7 @@ var SQLite = class {
|
|
|
595
895
|
});
|
|
596
896
|
}
|
|
597
897
|
async queryWithParameters(sqlQuery, params = []) {
|
|
598
|
-
return new Promise(async (
|
|
898
|
+
return new Promise(async (resolve5) => {
|
|
599
899
|
try {
|
|
600
900
|
if (typeof sqlQuery !== "string") {
|
|
601
901
|
throw new Error("The SQL query must be a string.");
|
|
@@ -611,13 +911,13 @@ var SQLite = class {
|
|
|
611
911
|
}
|
|
612
912
|
const result = await this.executor.query(sqlQuery, params);
|
|
613
913
|
if (result.status === "error") {
|
|
614
|
-
|
|
914
|
+
resolve5({
|
|
615
915
|
status: "error",
|
|
616
916
|
message: result.message,
|
|
617
917
|
data: null
|
|
618
918
|
});
|
|
619
919
|
} else {
|
|
620
|
-
|
|
920
|
+
resolve5({
|
|
621
921
|
status: "success",
|
|
622
922
|
message: "Query executed successfully",
|
|
623
923
|
data: result.data
|
|
@@ -625,7 +925,7 @@ var SQLite = class {
|
|
|
625
925
|
}
|
|
626
926
|
} catch (error) {
|
|
627
927
|
console.log(error);
|
|
628
|
-
|
|
928
|
+
resolve5({
|
|
629
929
|
status: "error",
|
|
630
930
|
message: error.message || "An error occurred while executing the query.",
|
|
631
931
|
data: null
|
|
@@ -719,8 +1019,8 @@ var DbConfig = new SQLite({ DATABASE: "config" });
|
|
|
719
1019
|
var DbConfig_default = DbConfig;
|
|
720
1020
|
|
|
721
1021
|
// src/lib/FileLogger.ts
|
|
722
|
-
import * as
|
|
723
|
-
import * as
|
|
1022
|
+
import * as fs4 from "fs";
|
|
1023
|
+
import * as path6 from "path";
|
|
724
1024
|
import { EventEmitter } from "events";
|
|
725
1025
|
var FileLogger = class _FileLogger extends EventEmitter {
|
|
726
1026
|
static watchers = /* @__PURE__ */ new Map();
|
|
@@ -735,9 +1035,9 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
735
1035
|
*/
|
|
736
1036
|
static async write(filePath, message, level = "INFO", append = true) {
|
|
737
1037
|
try {
|
|
738
|
-
const dir =
|
|
739
|
-
if (!
|
|
740
|
-
|
|
1038
|
+
const dir = path6.dirname(filePath);
|
|
1039
|
+
if (!fs4.existsSync(dir)) {
|
|
1040
|
+
fs4.mkdirSync(dir, { recursive: true });
|
|
741
1041
|
}
|
|
742
1042
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
743
1043
|
const formattedMessage = `[${timestamp}] [${level}] ${message}
|
|
@@ -747,9 +1047,9 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
747
1047
|
return true;
|
|
748
1048
|
}
|
|
749
1049
|
if (append) {
|
|
750
|
-
await
|
|
1050
|
+
await fs4.promises.appendFile(filePath, formattedMessage, "utf8");
|
|
751
1051
|
} else {
|
|
752
|
-
await
|
|
1052
|
+
await fs4.promises.writeFile(filePath, formattedMessage, "utf8");
|
|
753
1053
|
}
|
|
754
1054
|
return true;
|
|
755
1055
|
} catch (error) {
|
|
@@ -774,12 +1074,12 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
774
1074
|
const buffer = _FileLogger.buffers.get(filePath);
|
|
775
1075
|
if (buffer && buffer.length > 0) {
|
|
776
1076
|
try {
|
|
777
|
-
const dir =
|
|
778
|
-
if (!
|
|
779
|
-
|
|
1077
|
+
const dir = path6.dirname(filePath);
|
|
1078
|
+
if (!fs4.existsSync(dir)) {
|
|
1079
|
+
fs4.mkdirSync(dir, { recursive: true });
|
|
780
1080
|
}
|
|
781
1081
|
const content = buffer.join("");
|
|
782
|
-
await
|
|
1082
|
+
await fs4.promises.appendFile(filePath, content, "utf8");
|
|
783
1083
|
_FileLogger.buffers.delete(filePath);
|
|
784
1084
|
return true;
|
|
785
1085
|
} catch (error) {
|
|
@@ -915,10 +1215,10 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
915
1215
|
// Si debe retornar como array de líneas
|
|
916
1216
|
} = options;
|
|
917
1217
|
try {
|
|
918
|
-
if (!
|
|
1218
|
+
if (!fs4.existsSync(filePath)) {
|
|
919
1219
|
return asArray ? [] : "";
|
|
920
1220
|
}
|
|
921
|
-
let content = await
|
|
1221
|
+
let content = await fs4.promises.readFile(filePath, "utf8");
|
|
922
1222
|
if (asArray) {
|
|
923
1223
|
let linesArray = content.split("\n").filter((line) => line.trim() !== "");
|
|
924
1224
|
if (lines !== null) {
|
|
@@ -961,15 +1261,15 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
961
1261
|
} = options;
|
|
962
1262
|
let lastSize = 0;
|
|
963
1263
|
let lastPosition = 0;
|
|
964
|
-
if (
|
|
965
|
-
const stats =
|
|
1264
|
+
if (fs4.existsSync(filePath)) {
|
|
1265
|
+
const stats = fs4.statSync(filePath);
|
|
966
1266
|
lastSize = stats.size;
|
|
967
1267
|
lastPosition = fromEnd ? stats.size : 0;
|
|
968
1268
|
}
|
|
969
1269
|
const listener = async (curr, prev) => {
|
|
970
1270
|
try {
|
|
971
1271
|
if (curr.size > lastSize) {
|
|
972
|
-
const stream =
|
|
1272
|
+
const stream = fs4.createReadStream(filePath, {
|
|
973
1273
|
start: lastPosition,
|
|
974
1274
|
end: curr.size - 1,
|
|
975
1275
|
encoding: "utf8"
|
|
@@ -997,7 +1297,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
997
1297
|
console.error("Error en watcher:", error);
|
|
998
1298
|
}
|
|
999
1299
|
};
|
|
1000
|
-
|
|
1300
|
+
fs4.watchFile(filePath, { persistent, interval }, listener);
|
|
1001
1301
|
const watcherId = `${filePath}_${Date.now()}`;
|
|
1002
1302
|
_FileLogger.watchers.set(watcherId, listener);
|
|
1003
1303
|
return {
|
|
@@ -1005,7 +1305,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
1005
1305
|
stop: () => {
|
|
1006
1306
|
const storedListener = _FileLogger.watchers.get(watcherId);
|
|
1007
1307
|
if (storedListener) {
|
|
1008
|
-
|
|
1308
|
+
fs4.unwatchFile(filePath, storedListener);
|
|
1009
1309
|
_FileLogger.watchers.delete(watcherId);
|
|
1010
1310
|
}
|
|
1011
1311
|
},
|
|
@@ -1018,7 +1318,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
1018
1318
|
static stopAllWatchers() {
|
|
1019
1319
|
for (const [watcherId] of _FileLogger.watchers) {
|
|
1020
1320
|
const filePath = watcherId.split("_")[0];
|
|
1021
|
-
|
|
1321
|
+
fs4.unwatchFile(filePath);
|
|
1022
1322
|
}
|
|
1023
1323
|
_FileLogger.watchers.clear();
|
|
1024
1324
|
}
|
|
@@ -1048,7 +1348,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
1048
1348
|
if (lines.length > maxLines) {
|
|
1049
1349
|
const keepLines = lines.slice(-maxLines);
|
|
1050
1350
|
const content = keepLines.join("\n") + "\n";
|
|
1051
|
-
await
|
|
1351
|
+
await fs4.promises.writeFile(filePath, content, "utf8");
|
|
1052
1352
|
return lines.length - maxLines;
|
|
1053
1353
|
}
|
|
1054
1354
|
return 0;
|
|
@@ -1063,8 +1363,8 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
1063
1363
|
*/
|
|
1064
1364
|
static async deleteLogFile(filePath) {
|
|
1065
1365
|
try {
|
|
1066
|
-
if (
|
|
1067
|
-
await
|
|
1366
|
+
if (fs4.existsSync(filePath)) {
|
|
1367
|
+
await fs4.promises.unlink(filePath);
|
|
1068
1368
|
return true;
|
|
1069
1369
|
}
|
|
1070
1370
|
return false;
|