@dbcube/core 5.2.4 → 5.2.6
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/index.cjs +167 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +188 -99
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,7 +6,8 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
// src/lib/Engine.ts
|
|
9
|
-
import
|
|
9
|
+
import path5 from "path";
|
|
10
|
+
import * as fs5 from "fs";
|
|
10
11
|
|
|
11
12
|
// src/lib/Arquitecture.ts
|
|
12
13
|
import * as os from "os";
|
|
@@ -153,7 +154,7 @@ var Downloader = class {
|
|
|
153
154
|
*/
|
|
154
155
|
static async fetchLatestVersion(prefix) {
|
|
155
156
|
const url = this.VERSION_URLS[prefix];
|
|
156
|
-
return new Promise((
|
|
157
|
+
return new Promise((resolve6, reject) => {
|
|
157
158
|
https.get(url, (response) => {
|
|
158
159
|
let data = "";
|
|
159
160
|
response.on("data", (chunk) => {
|
|
@@ -163,7 +164,7 @@ var Downloader = class {
|
|
|
163
164
|
try {
|
|
164
165
|
const versions = JSON.parse(data);
|
|
165
166
|
if (versions && versions.length > 0) {
|
|
166
|
-
|
|
167
|
+
resolve6(versions[0].version);
|
|
167
168
|
} else {
|
|
168
169
|
reject(new Error("No versions found"));
|
|
169
170
|
}
|
|
@@ -372,7 +373,7 @@ var Downloader = class {
|
|
|
372
373
|
if (attempt < maxRetries && (errorMessage.includes("ECONNRESET") || errorMessage.includes("timeout") || errorMessage.includes("ETIMEDOUT") || errorMessage.includes("ENOTFOUND"))) {
|
|
373
374
|
attempt++;
|
|
374
375
|
console.log(`\u{1F504} Retrying ${binary.prefix}-engine (${attempt}/${maxRetries})...`);
|
|
375
|
-
await new Promise((
|
|
376
|
+
await new Promise((resolve6) => setTimeout(resolve6, 1e3 + Math.random() * 1e3));
|
|
376
377
|
} else {
|
|
377
378
|
throw new Error(`Error downloading ${binary.prefix}: ${errorMessage}`);
|
|
378
379
|
}
|
|
@@ -420,12 +421,12 @@ var Downloader = class {
|
|
|
420
421
|
return `[${filledBar}${emptyBar}] ${percentage}`;
|
|
421
422
|
}
|
|
422
423
|
static downloadFileWithProgress(url, outputPath, prefix) {
|
|
423
|
-
return new Promise((
|
|
424
|
+
return new Promise((resolve6, reject) => {
|
|
424
425
|
const request = https.get(url, { timeout: 0 }, (response) => {
|
|
425
426
|
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
426
427
|
const redirectUrl = response.headers.location;
|
|
427
428
|
if (redirectUrl) {
|
|
428
|
-
return this.downloadFileWithProgress(redirectUrl, outputPath, prefix).then(
|
|
429
|
+
return this.downloadFileWithProgress(redirectUrl, outputPath, prefix).then(resolve6).catch(reject);
|
|
429
430
|
}
|
|
430
431
|
}
|
|
431
432
|
if (response.statusCode !== 200) {
|
|
@@ -448,7 +449,7 @@ var Downloader = class {
|
|
|
448
449
|
}
|
|
449
450
|
});
|
|
450
451
|
response.on("end", () => {
|
|
451
|
-
file.end(() =>
|
|
452
|
+
file.end(() => resolve6());
|
|
452
453
|
});
|
|
453
454
|
response.on("error", (err) => {
|
|
454
455
|
file.close();
|
|
@@ -480,7 +481,7 @@ var Downloader = class {
|
|
|
480
481
|
this.mainSpinner.text = `\u{1F4E5} ${chalk.bold(binary)} - ${progressText}`;
|
|
481
482
|
}
|
|
482
483
|
static extractBinary(zipPath, outputPath, prefix) {
|
|
483
|
-
return new Promise((
|
|
484
|
+
return new Promise((resolve6, reject) => {
|
|
484
485
|
const outDir = path.dirname(outputPath);
|
|
485
486
|
let extracted = 0;
|
|
486
487
|
const pending = [];
|
|
@@ -515,7 +516,7 @@ var Downloader = class {
|
|
|
515
516
|
if (extracted === 0) {
|
|
516
517
|
reject(new Error(`No se encontr\xF3 archivo v\xE1lido en el ZIP para ${prefix}`));
|
|
517
518
|
} else {
|
|
518
|
-
|
|
519
|
+
resolve6();
|
|
519
520
|
}
|
|
520
521
|
}).catch((err) => {
|
|
521
522
|
this.cleanupFile(zipPath);
|
|
@@ -575,6 +576,9 @@ var Binary = class {
|
|
|
575
576
|
return;
|
|
576
577
|
}
|
|
577
578
|
const binDir = this.getBinDir();
|
|
579
|
+
if (this.binariesPresent(binDir)) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
578
582
|
if (!this.isDownloading) {
|
|
579
583
|
this.isDownloading = true;
|
|
580
584
|
this.downloadPromise = this.downloadBinaries();
|
|
@@ -586,6 +590,17 @@ var Binary = class {
|
|
|
586
590
|
}
|
|
587
591
|
}
|
|
588
592
|
}
|
|
593
|
+
/** ¿Están ya presentes query-engine y schema-engine en binDir? */
|
|
594
|
+
static binariesPresent(binDir) {
|
|
595
|
+
try {
|
|
596
|
+
const files = fs2.readdirSync(binDir);
|
|
597
|
+
const hasQuery = files.some((f) => /^query-engine-/.test(f));
|
|
598
|
+
const hasSchema = files.some((f) => /^schema-engine-/.test(f));
|
|
599
|
+
return hasQuery && hasSchema;
|
|
600
|
+
} catch {
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
589
604
|
static async downloadBinaries() {
|
|
590
605
|
try {
|
|
591
606
|
const binDir = this.getBinDir();
|
|
@@ -705,14 +720,75 @@ var Config = class {
|
|
|
705
720
|
}
|
|
706
721
|
};
|
|
707
722
|
|
|
723
|
+
// src/lib/EnvLoader.ts
|
|
724
|
+
import * as fs3 from "fs";
|
|
725
|
+
import * as path3 from "path";
|
|
726
|
+
var EnvLoader = class _EnvLoader {
|
|
727
|
+
/**
|
|
728
|
+
* Carga el archivo .env indicado (o ./.env del cwd) en process.env.
|
|
729
|
+
* Silencioso si el archivo no existe. Devuelve true si cargó algo.
|
|
730
|
+
*/
|
|
731
|
+
static load(envPath) {
|
|
732
|
+
const file = envPath || path3.resolve(process.cwd(), ".env");
|
|
733
|
+
if (!fs3.existsSync(file)) return false;
|
|
734
|
+
const proc = process;
|
|
735
|
+
if (typeof proc.loadEnvFile === "function") {
|
|
736
|
+
try {
|
|
737
|
+
proc.loadEnvFile(file);
|
|
738
|
+
return true;
|
|
739
|
+
} catch {
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
try {
|
|
743
|
+
const parsed = _EnvLoader.parse(fs3.readFileSync(file, "utf8"));
|
|
744
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
745
|
+
if (process.env[key] === void 0) {
|
|
746
|
+
process.env[key] = value;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
return true;
|
|
750
|
+
} catch {
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Parser mínimo de formato .env. Soporta:
|
|
756
|
+
* KEY=value · KEY="value con espacios" · KEY='literal' · export KEY=value
|
|
757
|
+
* comentarios con # · líneas en blanco · \n \t en comillas dobles.
|
|
758
|
+
*/
|
|
759
|
+
static parse(content) {
|
|
760
|
+
const out = {};
|
|
761
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
762
|
+
const line = rawLine.trim();
|
|
763
|
+
if (!line || line.startsWith("#")) continue;
|
|
764
|
+
const withoutExport = line.startsWith("export ") ? line.slice(7).trim() : line;
|
|
765
|
+
const eq = withoutExport.indexOf("=");
|
|
766
|
+
if (eq === -1) continue;
|
|
767
|
+
const key = withoutExport.slice(0, eq).trim();
|
|
768
|
+
if (!key) continue;
|
|
769
|
+
let value = withoutExport.slice(eq + 1).trim();
|
|
770
|
+
if (value.length >= 2 && value[0] === '"' && value[value.length - 1] === '"') {
|
|
771
|
+
value = value.slice(1, -1).replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\"/g, '"');
|
|
772
|
+
} else if (value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'") {
|
|
773
|
+
value = value.slice(1, -1);
|
|
774
|
+
} else {
|
|
775
|
+
const hash = value.indexOf(" #");
|
|
776
|
+
if (hash !== -1) value = value.slice(0, hash).trim();
|
|
777
|
+
}
|
|
778
|
+
out[key] = value;
|
|
779
|
+
}
|
|
780
|
+
return out;
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
|
|
708
784
|
// src/lib/Engine.ts
|
|
709
785
|
import { spawn as spawn2 } from "child_process";
|
|
710
786
|
import { createRequire } from "module";
|
|
711
787
|
|
|
712
788
|
// src/lib/DaemonClient.ts
|
|
713
789
|
import net from "net";
|
|
714
|
-
import
|
|
715
|
-
import
|
|
790
|
+
import fs4 from "fs";
|
|
791
|
+
import path4 from "path";
|
|
716
792
|
import { spawn } from "child_process";
|
|
717
793
|
var DaemonClient = class _DaemonClient {
|
|
718
794
|
static registry = /* @__PURE__ */ new Map();
|
|
@@ -744,10 +820,10 @@ var DaemonClient = class _DaemonClient {
|
|
|
744
820
|
return true;
|
|
745
821
|
}
|
|
746
822
|
portfilePath() {
|
|
747
|
-
return
|
|
823
|
+
return path4.join(process.cwd(), ".dbcube", "daemon", `${this.name}.json`);
|
|
748
824
|
}
|
|
749
825
|
lockfilePath() {
|
|
750
|
-
return
|
|
826
|
+
return path4.join(process.cwd(), ".dbcube", "daemon", `${this.name}.lock`);
|
|
751
827
|
}
|
|
752
828
|
/**
|
|
753
829
|
* Exclusive-create lock so two processes never spawn two daemons for the
|
|
@@ -756,16 +832,16 @@ var DaemonClient = class _DaemonClient {
|
|
|
756
832
|
acquireSpawnLock() {
|
|
757
833
|
const lockPath = this.lockfilePath();
|
|
758
834
|
try {
|
|
759
|
-
|
|
760
|
-
const fd =
|
|
761
|
-
|
|
762
|
-
|
|
835
|
+
fs4.mkdirSync(path4.dirname(lockPath), { recursive: true });
|
|
836
|
+
const fd = fs4.openSync(lockPath, "wx");
|
|
837
|
+
fs4.writeSync(fd, String(process.pid));
|
|
838
|
+
fs4.closeSync(fd);
|
|
763
839
|
return true;
|
|
764
840
|
} catch {
|
|
765
841
|
try {
|
|
766
|
-
const age = Date.now() -
|
|
842
|
+
const age = Date.now() - fs4.statSync(lockPath).mtimeMs;
|
|
767
843
|
if (age > 15e3) {
|
|
768
|
-
|
|
844
|
+
fs4.unlinkSync(lockPath);
|
|
769
845
|
return this.acquireSpawnLock();
|
|
770
846
|
}
|
|
771
847
|
} catch {
|
|
@@ -775,7 +851,7 @@ var DaemonClient = class _DaemonClient {
|
|
|
775
851
|
}
|
|
776
852
|
releaseSpawnLock() {
|
|
777
853
|
try {
|
|
778
|
-
|
|
854
|
+
fs4.unlinkSync(this.lockfilePath());
|
|
779
855
|
} catch {
|
|
780
856
|
}
|
|
781
857
|
}
|
|
@@ -799,7 +875,7 @@ var DaemonClient = class _DaemonClient {
|
|
|
799
875
|
try {
|
|
800
876
|
if (gotLock) {
|
|
801
877
|
try {
|
|
802
|
-
|
|
878
|
+
fs4.unlinkSync(this.portfilePath());
|
|
803
879
|
} catch {
|
|
804
880
|
}
|
|
805
881
|
this.spawnDaemon();
|
|
@@ -817,7 +893,7 @@ var DaemonClient = class _DaemonClient {
|
|
|
817
893
|
}
|
|
818
894
|
readPortfile() {
|
|
819
895
|
try {
|
|
820
|
-
const raw =
|
|
896
|
+
const raw = fs4.readFileSync(this.portfilePath(), "utf8");
|
|
821
897
|
const info = JSON.parse(raw);
|
|
822
898
|
const port = info?.port;
|
|
823
899
|
return Number.isInteger(port) && port > 0 && port <= 65535 ? port : null;
|
|
@@ -834,22 +910,22 @@ var DaemonClient = class _DaemonClient {
|
|
|
834
910
|
child.unref();
|
|
835
911
|
}
|
|
836
912
|
tryConnect(port) {
|
|
837
|
-
return new Promise((
|
|
913
|
+
return new Promise((resolve6) => {
|
|
838
914
|
const socket = net.createConnection({ host: "127.0.0.1", port }, async () => {
|
|
839
915
|
socket.setNoDelay(true);
|
|
840
916
|
this.attach(socket);
|
|
841
917
|
try {
|
|
842
918
|
const pong = await this.send({ action: "ping" }, 2e3);
|
|
843
|
-
|
|
919
|
+
resolve6(pong.status === 200);
|
|
844
920
|
} catch {
|
|
845
921
|
this.detach();
|
|
846
|
-
|
|
922
|
+
resolve6(false);
|
|
847
923
|
}
|
|
848
924
|
});
|
|
849
|
-
socket.once("error", () =>
|
|
925
|
+
socket.once("error", () => resolve6(false));
|
|
850
926
|
socket.setTimeout(3e3, () => {
|
|
851
927
|
socket.destroy();
|
|
852
|
-
|
|
928
|
+
resolve6(false);
|
|
853
929
|
});
|
|
854
930
|
});
|
|
855
931
|
}
|
|
@@ -897,7 +973,7 @@ var DaemonClient = class _DaemonClient {
|
|
|
897
973
|
* single socket, so pending requests resolve in send order.
|
|
898
974
|
*/
|
|
899
975
|
send(payload, timeoutMs) {
|
|
900
|
-
return new Promise((
|
|
976
|
+
return new Promise((resolve6, reject) => {
|
|
901
977
|
if (!this.socket || this.socket.destroyed) {
|
|
902
978
|
reject(new Error("Daemon not connected"));
|
|
903
979
|
return;
|
|
@@ -908,7 +984,7 @@ var DaemonClient = class _DaemonClient {
|
|
|
908
984
|
reject(new Error("Daemon request timeout"));
|
|
909
985
|
this.detach();
|
|
910
986
|
}, timeoutMs ?? this.requestTimeout);
|
|
911
|
-
this.pending.push({ resolve:
|
|
987
|
+
this.pending.push({ resolve: resolve6, reject, timer });
|
|
912
988
|
this.socket.write(JSON.stringify(payload) + "\n");
|
|
913
989
|
});
|
|
914
990
|
}
|
|
@@ -944,7 +1020,7 @@ var DaemonClient = class _DaemonClient {
|
|
|
944
1020
|
}
|
|
945
1021
|
this.detach();
|
|
946
1022
|
try {
|
|
947
|
-
|
|
1023
|
+
fs4.unlinkSync(this.portfilePath());
|
|
948
1024
|
} catch {
|
|
949
1025
|
}
|
|
950
1026
|
}
|
|
@@ -1042,6 +1118,7 @@ var Engine = class {
|
|
|
1042
1118
|
}
|
|
1043
1119
|
setArguments() {
|
|
1044
1120
|
let args = [];
|
|
1121
|
+
const motor = this.config.type === "postgres" ? "postgresql" : this.config.type;
|
|
1045
1122
|
if (this.config.type == "sqlite") {
|
|
1046
1123
|
args = [
|
|
1047
1124
|
"--id",
|
|
@@ -1051,7 +1128,7 @@ var Engine = class {
|
|
|
1051
1128
|
"--database",
|
|
1052
1129
|
this.config.config.DATABASE + ".db",
|
|
1053
1130
|
"--motor",
|
|
1054
|
-
|
|
1131
|
+
motor
|
|
1055
1132
|
];
|
|
1056
1133
|
} else {
|
|
1057
1134
|
args = [
|
|
@@ -1066,7 +1143,7 @@ var Engine = class {
|
|
|
1066
1143
|
"--port",
|
|
1067
1144
|
String(this.config.config.PORT),
|
|
1068
1145
|
"--motor",
|
|
1069
|
-
|
|
1146
|
+
motor
|
|
1070
1147
|
];
|
|
1071
1148
|
if (this.config.config.USER != null && this.config.config.USER !== "") {
|
|
1072
1149
|
args.push("--user", this.config.config.USER);
|
|
@@ -1079,8 +1156,10 @@ var Engine = class {
|
|
|
1079
1156
|
}
|
|
1080
1157
|
setConfig(name) {
|
|
1081
1158
|
const configInstance = new Config();
|
|
1159
|
+
EnvLoader.load();
|
|
1082
1160
|
try {
|
|
1083
|
-
const
|
|
1161
|
+
const cjsPath = path5.resolve(process.cwd(), "dbcube.config.cjs");
|
|
1162
|
+
const configFilePath = fs5.existsSync(cjsPath) ? cjsPath : path5.resolve(process.cwd(), "dbcube.config.js");
|
|
1084
1163
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
1085
1164
|
const require2 = createRequire(requireUrl);
|
|
1086
1165
|
if (require2.cache && require2.resolve) {
|
|
@@ -1112,7 +1191,7 @@ var Engine = class {
|
|
|
1112
1191
|
if (!this.binary) {
|
|
1113
1192
|
throw new Error("Binary not initialized");
|
|
1114
1193
|
}
|
|
1115
|
-
return new Promise((
|
|
1194
|
+
return new Promise((resolve6, reject) => {
|
|
1116
1195
|
const child = spawn2(this.binary[binary], [...this.arguments, ...args]);
|
|
1117
1196
|
let stdoutBuffer = "";
|
|
1118
1197
|
let stderrBuffer = "";
|
|
@@ -1128,7 +1207,7 @@ var Engine = class {
|
|
|
1128
1207
|
if (!isResolved) {
|
|
1129
1208
|
isResolved = true;
|
|
1130
1209
|
clearTimeout(timeoutId);
|
|
1131
|
-
|
|
1210
|
+
resolve6(response);
|
|
1132
1211
|
}
|
|
1133
1212
|
};
|
|
1134
1213
|
const tryParseLines = (buffer) => {
|
|
@@ -1155,16 +1234,21 @@ var Engine = class {
|
|
|
1155
1234
|
}
|
|
1156
1235
|
return buffer;
|
|
1157
1236
|
};
|
|
1237
|
+
const showEngineOutput = process.env.DBCUBE_DEBUG === "1" || process.env.DBCUBE_DEBUG === "true";
|
|
1158
1238
|
child.stdout.on("data", (data) => {
|
|
1159
1239
|
const text = data.toString();
|
|
1160
|
-
|
|
1161
|
-
|
|
1240
|
+
if (showEngineOutput) {
|
|
1241
|
+
const visible = text.split("\n").filter((l) => l.trim() && !l.includes("PROCESS_RESPONSE:")).join("\n");
|
|
1242
|
+
if (visible) console.log(visible);
|
|
1243
|
+
}
|
|
1162
1244
|
stdoutBuffer = tryParseLines(stdoutBuffer + text);
|
|
1163
1245
|
});
|
|
1164
1246
|
child.stderr.on("data", (data) => {
|
|
1165
1247
|
const text = data.toString();
|
|
1166
|
-
|
|
1167
|
-
|
|
1248
|
+
if (showEngineOutput) {
|
|
1249
|
+
const visible = text.split("\n").filter((l) => l.trim() && !l.includes("PROCESS_RESPONSE:")).join("\n");
|
|
1250
|
+
if (visible) console.log(visible);
|
|
1251
|
+
}
|
|
1168
1252
|
stderrBuffer = tryParseLines(stderrBuffer + text);
|
|
1169
1253
|
});
|
|
1170
1254
|
child.on("close", (code) => {
|
|
@@ -1193,11 +1277,12 @@ var Engine = class {
|
|
|
1193
1277
|
};
|
|
1194
1278
|
|
|
1195
1279
|
// src/lib/QueryEngine.ts
|
|
1196
|
-
import
|
|
1280
|
+
import path7 from "path";
|
|
1281
|
+
import * as fs7 from "fs";
|
|
1197
1282
|
|
|
1198
1283
|
// src/lib/EmbeddedEngine.ts
|
|
1199
|
-
import * as
|
|
1200
|
-
import * as
|
|
1284
|
+
import * as fs6 from "fs";
|
|
1285
|
+
import * as path6 from "path";
|
|
1201
1286
|
if (!process.env.UV_THREADPOOL_SIZE) {
|
|
1202
1287
|
process.env.UV_THREADPOOL_SIZE = "16";
|
|
1203
1288
|
}
|
|
@@ -1213,11 +1298,11 @@ function platTag() {
|
|
|
1213
1298
|
}
|
|
1214
1299
|
function findFile(name) {
|
|
1215
1300
|
const candidates = [
|
|
1216
|
-
|
|
1217
|
-
|
|
1301
|
+
path6.resolve(process.cwd(), ".dbcube", "bin", name),
|
|
1302
|
+
path6.resolve(process.cwd(), "node_modules", ".dbcube", "bin", name)
|
|
1218
1303
|
];
|
|
1219
1304
|
for (const c of candidates) {
|
|
1220
|
-
if (
|
|
1305
|
+
if (fs6.existsSync(c)) return c;
|
|
1221
1306
|
}
|
|
1222
1307
|
return null;
|
|
1223
1308
|
}
|
|
@@ -1325,10 +1410,10 @@ function loadKoffiBackend() {
|
|
|
1325
1410
|
return { status: 500, message: `Invalid embedded response: ${e.message}`, data: null };
|
|
1326
1411
|
}
|
|
1327
1412
|
};
|
|
1328
|
-
const call = (fn, ...args) => new Promise((
|
|
1413
|
+
const call = (fn, ...args) => new Promise((resolve6, reject) => {
|
|
1329
1414
|
fn.async(...args, (err, ptr) => {
|
|
1330
1415
|
if (err) return reject(err);
|
|
1331
|
-
|
|
1416
|
+
resolve6(take(ptr));
|
|
1332
1417
|
});
|
|
1333
1418
|
});
|
|
1334
1419
|
return {
|
|
@@ -1482,12 +1567,12 @@ var QueryEngine = class {
|
|
|
1482
1567
|
throw new Error("No available ports found in range 9900-9944");
|
|
1483
1568
|
}
|
|
1484
1569
|
isPortAvailable(port) {
|
|
1485
|
-
return new Promise((
|
|
1570
|
+
return new Promise((resolve6) => {
|
|
1486
1571
|
const tester = net2.createServer();
|
|
1487
|
-
tester.once("error", () =>
|
|
1572
|
+
tester.once("error", () => resolve6(false));
|
|
1488
1573
|
tester.once("listening", () => {
|
|
1489
1574
|
tester.close();
|
|
1490
|
-
|
|
1575
|
+
resolve6(true);
|
|
1491
1576
|
});
|
|
1492
1577
|
tester.listen(port, "127.0.0.1");
|
|
1493
1578
|
});
|
|
@@ -1499,6 +1584,7 @@ var QueryEngine = class {
|
|
|
1499
1584
|
}
|
|
1500
1585
|
setArguments() {
|
|
1501
1586
|
let args = [];
|
|
1587
|
+
const motor = this.config.type === "postgres" ? "postgresql" : this.config.type;
|
|
1502
1588
|
if (this.config.type == "sqlite") {
|
|
1503
1589
|
args = [
|
|
1504
1590
|
"--id",
|
|
@@ -1508,7 +1594,7 @@ var QueryEngine = class {
|
|
|
1508
1594
|
"--database",
|
|
1509
1595
|
this.config.config.DATABASE + ".db",
|
|
1510
1596
|
"--motor",
|
|
1511
|
-
|
|
1597
|
+
motor
|
|
1512
1598
|
];
|
|
1513
1599
|
} else {
|
|
1514
1600
|
args = [
|
|
@@ -1523,7 +1609,7 @@ var QueryEngine = class {
|
|
|
1523
1609
|
"--port",
|
|
1524
1610
|
String(this.config.config.PORT),
|
|
1525
1611
|
"--motor",
|
|
1526
|
-
|
|
1612
|
+
motor
|
|
1527
1613
|
];
|
|
1528
1614
|
if (this.config.config.USER != null && this.config.config.USER !== "") {
|
|
1529
1615
|
args.push("--user", this.config.config.USER);
|
|
@@ -1547,8 +1633,10 @@ var QueryEngine = class {
|
|
|
1547
1633
|
}
|
|
1548
1634
|
setConfig(name) {
|
|
1549
1635
|
const configInstance = new Config();
|
|
1636
|
+
EnvLoader.load();
|
|
1550
1637
|
try {
|
|
1551
|
-
const
|
|
1638
|
+
const cjsPath = path7.resolve(process.cwd(), "dbcube.config.cjs");
|
|
1639
|
+
const configFilePath = fs7.existsSync(cjsPath) ? cjsPath : path7.resolve(process.cwd(), "dbcube.config.js");
|
|
1552
1640
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
1553
1641
|
const require2 = createRequire2(requireUrl);
|
|
1554
1642
|
if (require2.cache && require2.resolve) {
|
|
@@ -1720,7 +1808,7 @@ var QueryEngine = class {
|
|
|
1720
1808
|
throw e;
|
|
1721
1809
|
}
|
|
1722
1810
|
}
|
|
1723
|
-
return new Promise((
|
|
1811
|
+
return new Promise((resolve6) => pool.waiters.push(resolve6));
|
|
1724
1812
|
}
|
|
1725
1813
|
releaseSocket(socket, broken) {
|
|
1726
1814
|
const pool = socketPools.get(this.connectionId);
|
|
@@ -1759,25 +1847,25 @@ var QueryEngine = class {
|
|
|
1759
1847
|
throw new Error("TCP server failed to become ready within timeout");
|
|
1760
1848
|
}
|
|
1761
1849
|
async isServerResponding(port) {
|
|
1762
|
-
return new Promise((
|
|
1850
|
+
return new Promise((resolve6) => {
|
|
1763
1851
|
const client = new net2.Socket();
|
|
1764
1852
|
const timeout = setTimeout(() => {
|
|
1765
1853
|
client.destroy();
|
|
1766
|
-
|
|
1854
|
+
resolve6(false);
|
|
1767
1855
|
}, 1e3);
|
|
1768
1856
|
client.connect(port, "127.0.0.1", () => {
|
|
1769
1857
|
clearTimeout(timeout);
|
|
1770
1858
|
client.destroy();
|
|
1771
|
-
|
|
1859
|
+
resolve6(true);
|
|
1772
1860
|
});
|
|
1773
1861
|
client.on("error", () => {
|
|
1774
1862
|
clearTimeout(timeout);
|
|
1775
|
-
|
|
1863
|
+
resolve6(false);
|
|
1776
1864
|
});
|
|
1777
1865
|
});
|
|
1778
1866
|
}
|
|
1779
1867
|
sleep(ms) {
|
|
1780
|
-
return new Promise((
|
|
1868
|
+
return new Promise((resolve6) => setTimeout(resolve6, ms));
|
|
1781
1869
|
}
|
|
1782
1870
|
getCachedDML(dmlJson) {
|
|
1783
1871
|
if (queryCache.has(dmlJson)) {
|
|
@@ -1797,7 +1885,7 @@ var QueryEngine = class {
|
|
|
1797
1885
|
throw new Error("Binary not initialized");
|
|
1798
1886
|
}
|
|
1799
1887
|
this.tcpPort = await this.findAvailablePort(this.tcpPort);
|
|
1800
|
-
return new Promise((
|
|
1888
|
+
return new Promise((resolve6, reject) => {
|
|
1801
1889
|
const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", this.tcpPort.toString()];
|
|
1802
1890
|
const serverProcess = spawn3(this.binary["query_engine"], serverArgs);
|
|
1803
1891
|
let started = false;
|
|
@@ -1817,7 +1905,7 @@ var QueryEngine = class {
|
|
|
1817
1905
|
port: this.tcpPort,
|
|
1818
1906
|
process: serverProcess
|
|
1819
1907
|
});
|
|
1820
|
-
|
|
1908
|
+
resolve6();
|
|
1821
1909
|
}
|
|
1822
1910
|
}
|
|
1823
1911
|
});
|
|
@@ -1836,7 +1924,7 @@ var QueryEngine = class {
|
|
|
1836
1924
|
});
|
|
1837
1925
|
}
|
|
1838
1926
|
async createNewConnection(port) {
|
|
1839
|
-
return new Promise((
|
|
1927
|
+
return new Promise((resolve6, reject) => {
|
|
1840
1928
|
const client = new net2.Socket();
|
|
1841
1929
|
client.setNoDelay(true);
|
|
1842
1930
|
client.setKeepAlive(true, 6e4);
|
|
@@ -1846,7 +1934,7 @@ var QueryEngine = class {
|
|
|
1846
1934
|
}, 5e3);
|
|
1847
1935
|
client.connect(port, "127.0.0.1", () => {
|
|
1848
1936
|
clearTimeout(timeout);
|
|
1849
|
-
|
|
1937
|
+
resolve6(client);
|
|
1850
1938
|
});
|
|
1851
1939
|
client.on("error", (error) => {
|
|
1852
1940
|
clearTimeout(timeout);
|
|
@@ -1855,7 +1943,7 @@ var QueryEngine = class {
|
|
|
1855
1943
|
});
|
|
1856
1944
|
}
|
|
1857
1945
|
async executeOnConnection(connection, command) {
|
|
1858
|
-
return new Promise((
|
|
1946
|
+
return new Promise((resolve6, reject) => {
|
|
1859
1947
|
let responseBuffer = "";
|
|
1860
1948
|
let isResolved = false;
|
|
1861
1949
|
const timeout = setTimeout(() => {
|
|
@@ -1880,7 +1968,7 @@ var QueryEngine = class {
|
|
|
1880
1968
|
connection.removeListener("error", onError);
|
|
1881
1969
|
try {
|
|
1882
1970
|
const response = JSON.parse(line.slice(marker + "PROCESS_RESPONSE:".length));
|
|
1883
|
-
|
|
1971
|
+
resolve6({
|
|
1884
1972
|
status: response.status,
|
|
1885
1973
|
message: response.message,
|
|
1886
1974
|
data: response.data
|
|
@@ -1909,7 +1997,7 @@ var QueryEngine = class {
|
|
|
1909
1997
|
if (!this.binary) {
|
|
1910
1998
|
throw new Error("Binary not initialized");
|
|
1911
1999
|
}
|
|
1912
|
-
return new Promise((
|
|
2000
|
+
return new Promise((resolve6, reject) => {
|
|
1913
2001
|
const child = spawn3(this.binary[binary], [...this.arguments, ...args]);
|
|
1914
2002
|
let stdoutBuffer = "";
|
|
1915
2003
|
let stderrBuffer = "";
|
|
@@ -1925,7 +2013,7 @@ var QueryEngine = class {
|
|
|
1925
2013
|
if (!isResolved) {
|
|
1926
2014
|
isResolved = true;
|
|
1927
2015
|
clearTimeout(timeoutId);
|
|
1928
|
-
|
|
2016
|
+
resolve6(response);
|
|
1929
2017
|
}
|
|
1930
2018
|
};
|
|
1931
2019
|
const tryParseLines = (buffer) => {
|
|
@@ -2012,9 +2100,9 @@ var QueryEngine = class {
|
|
|
2012
2100
|
};
|
|
2013
2101
|
|
|
2014
2102
|
// src/lib/DbConfig.ts
|
|
2015
|
-
import * as
|
|
2016
|
-
import
|
|
2017
|
-
var rootPath =
|
|
2103
|
+
import * as path8 from "path";
|
|
2104
|
+
import fs8 from "fs";
|
|
2105
|
+
var rootPath = path8.resolve(process.cwd(), ".dbcube");
|
|
2018
2106
|
var SQLite = class {
|
|
2019
2107
|
database;
|
|
2020
2108
|
engine = null;
|
|
@@ -2023,14 +2111,14 @@ var SQLite = class {
|
|
|
2023
2111
|
}
|
|
2024
2112
|
/** Ruta del archivo SQLite interno (.dbcube/<database>.db). */
|
|
2025
2113
|
dbFilePath() {
|
|
2026
|
-
return
|
|
2114
|
+
return path8.join(rootPath, this.database + ".db");
|
|
2027
2115
|
}
|
|
2028
2116
|
/** QueryEngine sqlite apuntando al archivo interno, con config explícito
|
|
2029
2117
|
* (no vive en dbcube.config.js). La ruta relativa `.dbcube/<db>` deja que
|
|
2030
2118
|
* QueryEngine le añada `.db` → `.dbcube/<db>.db`, relativo al CWD. */
|
|
2031
2119
|
getEngine() {
|
|
2032
2120
|
if (!this.engine) {
|
|
2033
|
-
const relativeDb =
|
|
2121
|
+
const relativeDb = path8.join(".dbcube", this.database).replace(/\\/g, "/");
|
|
2034
2122
|
this.engine = new QueryEngine(`dbcube-internal-${this.database}`, 3e4, {
|
|
2035
2123
|
type: "sqlite",
|
|
2036
2124
|
config: { DATABASE: relativeDb }
|
|
@@ -2039,14 +2127,14 @@ var SQLite = class {
|
|
|
2039
2127
|
return this.engine;
|
|
2040
2128
|
}
|
|
2041
2129
|
async ifExist() {
|
|
2042
|
-
if (!
|
|
2043
|
-
|
|
2130
|
+
if (!fs8.existsSync(rootPath)) {
|
|
2131
|
+
fs8.mkdirSync(rootPath, { recursive: true });
|
|
2044
2132
|
}
|
|
2045
|
-
return
|
|
2133
|
+
return fs8.existsSync(this.dbFilePath());
|
|
2046
2134
|
}
|
|
2047
2135
|
async connect() {
|
|
2048
|
-
if (!
|
|
2049
|
-
|
|
2136
|
+
if (!fs8.existsSync(rootPath)) {
|
|
2137
|
+
fs8.mkdirSync(rootPath, { recursive: true });
|
|
2050
2138
|
}
|
|
2051
2139
|
return this.getEngine();
|
|
2052
2140
|
}
|
|
@@ -2162,8 +2250,8 @@ var DbConfig = new SQLite({ DATABASE: "config" });
|
|
|
2162
2250
|
var DbConfig_default = DbConfig;
|
|
2163
2251
|
|
|
2164
2252
|
// src/lib/FileLogger.ts
|
|
2165
|
-
import * as
|
|
2166
|
-
import * as
|
|
2253
|
+
import * as fs9 from "fs";
|
|
2254
|
+
import * as path9 from "path";
|
|
2167
2255
|
import { EventEmitter } from "events";
|
|
2168
2256
|
var FileLogger = class _FileLogger extends EventEmitter {
|
|
2169
2257
|
static watchers = /* @__PURE__ */ new Map();
|
|
@@ -2178,9 +2266,9 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2178
2266
|
*/
|
|
2179
2267
|
static async write(filePath, message, level = "INFO", append = true) {
|
|
2180
2268
|
try {
|
|
2181
|
-
const dir =
|
|
2182
|
-
if (!
|
|
2183
|
-
|
|
2269
|
+
const dir = path9.dirname(filePath);
|
|
2270
|
+
if (!fs9.existsSync(dir)) {
|
|
2271
|
+
fs9.mkdirSync(dir, { recursive: true });
|
|
2184
2272
|
}
|
|
2185
2273
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
2186
2274
|
const formattedMessage = `[${timestamp}] [${level}] ${message}
|
|
@@ -2190,9 +2278,9 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2190
2278
|
return true;
|
|
2191
2279
|
}
|
|
2192
2280
|
if (append) {
|
|
2193
|
-
await
|
|
2281
|
+
await fs9.promises.appendFile(filePath, formattedMessage, "utf8");
|
|
2194
2282
|
} else {
|
|
2195
|
-
await
|
|
2283
|
+
await fs9.promises.writeFile(filePath, formattedMessage, "utf8");
|
|
2196
2284
|
}
|
|
2197
2285
|
return true;
|
|
2198
2286
|
} catch (error) {
|
|
@@ -2217,12 +2305,12 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2217
2305
|
const buffer = _FileLogger.buffers.get(filePath);
|
|
2218
2306
|
if (buffer && buffer.length > 0) {
|
|
2219
2307
|
try {
|
|
2220
|
-
const dir =
|
|
2221
|
-
if (!
|
|
2222
|
-
|
|
2308
|
+
const dir = path9.dirname(filePath);
|
|
2309
|
+
if (!fs9.existsSync(dir)) {
|
|
2310
|
+
fs9.mkdirSync(dir, { recursive: true });
|
|
2223
2311
|
}
|
|
2224
2312
|
const content = buffer.join("");
|
|
2225
|
-
await
|
|
2313
|
+
await fs9.promises.appendFile(filePath, content, "utf8");
|
|
2226
2314
|
_FileLogger.buffers.delete(filePath);
|
|
2227
2315
|
return true;
|
|
2228
2316
|
} catch (error) {
|
|
@@ -2358,10 +2446,10 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2358
2446
|
// Si debe retornar como array de líneas
|
|
2359
2447
|
} = options;
|
|
2360
2448
|
try {
|
|
2361
|
-
if (!
|
|
2449
|
+
if (!fs9.existsSync(filePath)) {
|
|
2362
2450
|
return asArray ? [] : "";
|
|
2363
2451
|
}
|
|
2364
|
-
let content = await
|
|
2452
|
+
let content = await fs9.promises.readFile(filePath, "utf8");
|
|
2365
2453
|
if (asArray) {
|
|
2366
2454
|
let linesArray = content.split("\n").filter((line) => line.trim() !== "");
|
|
2367
2455
|
if (lines !== null) {
|
|
@@ -2404,15 +2492,15 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2404
2492
|
} = options;
|
|
2405
2493
|
let lastSize = 0;
|
|
2406
2494
|
let lastPosition = 0;
|
|
2407
|
-
if (
|
|
2408
|
-
const stats =
|
|
2495
|
+
if (fs9.existsSync(filePath)) {
|
|
2496
|
+
const stats = fs9.statSync(filePath);
|
|
2409
2497
|
lastSize = stats.size;
|
|
2410
2498
|
lastPosition = fromEnd ? stats.size : 0;
|
|
2411
2499
|
}
|
|
2412
2500
|
const listener = async (curr, prev) => {
|
|
2413
2501
|
try {
|
|
2414
2502
|
if (curr.size > lastSize) {
|
|
2415
|
-
const stream =
|
|
2503
|
+
const stream = fs9.createReadStream(filePath, {
|
|
2416
2504
|
start: lastPosition,
|
|
2417
2505
|
end: curr.size - 1,
|
|
2418
2506
|
encoding: "utf8"
|
|
@@ -2440,7 +2528,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2440
2528
|
console.error("Error en watcher:", error);
|
|
2441
2529
|
}
|
|
2442
2530
|
};
|
|
2443
|
-
|
|
2531
|
+
fs9.watchFile(filePath, { persistent, interval }, listener);
|
|
2444
2532
|
const watcherId = `${filePath}_${Date.now()}`;
|
|
2445
2533
|
_FileLogger.watchers.set(watcherId, listener);
|
|
2446
2534
|
return {
|
|
@@ -2448,7 +2536,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2448
2536
|
stop: () => {
|
|
2449
2537
|
const storedListener = _FileLogger.watchers.get(watcherId);
|
|
2450
2538
|
if (storedListener) {
|
|
2451
|
-
|
|
2539
|
+
fs9.unwatchFile(filePath, storedListener);
|
|
2452
2540
|
_FileLogger.watchers.delete(watcherId);
|
|
2453
2541
|
}
|
|
2454
2542
|
},
|
|
@@ -2461,7 +2549,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2461
2549
|
static stopAllWatchers() {
|
|
2462
2550
|
for (const [watcherId] of _FileLogger.watchers) {
|
|
2463
2551
|
const filePath = watcherId.split("_")[0];
|
|
2464
|
-
|
|
2552
|
+
fs9.unwatchFile(filePath);
|
|
2465
2553
|
}
|
|
2466
2554
|
_FileLogger.watchers.clear();
|
|
2467
2555
|
}
|
|
@@ -2491,7 +2579,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2491
2579
|
if (lines.length > maxLines) {
|
|
2492
2580
|
const keepLines = lines.slice(-maxLines);
|
|
2493
2581
|
const content = keepLines.join("\n") + "\n";
|
|
2494
|
-
await
|
|
2582
|
+
await fs9.promises.writeFile(filePath, content, "utf8");
|
|
2495
2583
|
return lines.length - maxLines;
|
|
2496
2584
|
}
|
|
2497
2585
|
return 0;
|
|
@@ -2506,8 +2594,8 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
2506
2594
|
*/
|
|
2507
2595
|
static async deleteLogFile(filePath) {
|
|
2508
2596
|
try {
|
|
2509
|
-
if (
|
|
2510
|
-
await
|
|
2597
|
+
if (fs9.existsSync(filePath)) {
|
|
2598
|
+
await fs9.promises.unlink(filePath);
|
|
2511
2599
|
return true;
|
|
2512
2600
|
}
|
|
2513
2601
|
return false;
|
|
@@ -2922,6 +3010,7 @@ export {
|
|
|
2922
3010
|
DaemonClient,
|
|
2923
3011
|
DbConfig,
|
|
2924
3012
|
Engine,
|
|
3013
|
+
EnvLoader,
|
|
2925
3014
|
FileLogger,
|
|
2926
3015
|
QueryEngine,
|
|
2927
3016
|
TableProcessor,
|