@dbcube/core 5.1.12 → 5.1.15
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 +337 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +70 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.js +332 -58
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -34,6 +34,7 @@ __export(index_exports, {
|
|
|
34
34
|
Binary: () => Binary,
|
|
35
35
|
ComputedFieldProcessor: () => ComputedFieldProcessor,
|
|
36
36
|
Config: () => Config,
|
|
37
|
+
DaemonClient: () => DaemonClient,
|
|
37
38
|
DbConfig: () => DbConfig,
|
|
38
39
|
Engine: () => Engine,
|
|
39
40
|
FileLogger: () => FileLogger,
|
|
@@ -44,7 +45,7 @@ __export(index_exports, {
|
|
|
44
45
|
module.exports = __toCommonJS(index_exports);
|
|
45
46
|
|
|
46
47
|
// src/lib/Engine.ts
|
|
47
|
-
var
|
|
48
|
+
var import_path4 = __toESM(require("path"));
|
|
48
49
|
|
|
49
50
|
// src/lib/Arquitecture.ts
|
|
50
51
|
var os = __toESM(require("os"));
|
|
@@ -734,14 +735,216 @@ var Config = class {
|
|
|
734
735
|
};
|
|
735
736
|
|
|
736
737
|
// src/lib/Engine.ts
|
|
737
|
-
var
|
|
738
|
+
var import_child_process2 = require("child_process");
|
|
738
739
|
var import_module = require("module");
|
|
740
|
+
|
|
741
|
+
// src/lib/DaemonClient.ts
|
|
742
|
+
var import_net = __toESM(require("net"));
|
|
743
|
+
var import_fs = __toESM(require("fs"));
|
|
744
|
+
var import_path3 = __toESM(require("path"));
|
|
745
|
+
var import_child_process = require("child_process");
|
|
746
|
+
var DaemonClient = class _DaemonClient {
|
|
747
|
+
static registry = /* @__PURE__ */ new Map();
|
|
748
|
+
name;
|
|
749
|
+
binaryPath;
|
|
750
|
+
engineArgs;
|
|
751
|
+
socket = null;
|
|
752
|
+
buffer = "";
|
|
753
|
+
pending = [];
|
|
754
|
+
starting = null;
|
|
755
|
+
requestTimeout;
|
|
756
|
+
constructor(name, binaryPath, engineArgs, requestTimeout = 3e4) {
|
|
757
|
+
this.name = name;
|
|
758
|
+
this.binaryPath = binaryPath;
|
|
759
|
+
this.engineArgs = engineArgs;
|
|
760
|
+
this.requestTimeout = requestTimeout;
|
|
761
|
+
}
|
|
762
|
+
static get(name, binaryPath, engineArgs) {
|
|
763
|
+
let client = this.registry.get(name);
|
|
764
|
+
if (!client) {
|
|
765
|
+
client = new _DaemonClient(name, binaryPath, engineArgs);
|
|
766
|
+
this.registry.set(name, client);
|
|
767
|
+
}
|
|
768
|
+
return client;
|
|
769
|
+
}
|
|
770
|
+
static isEnabled() {
|
|
771
|
+
const flag = process.env.DBCUBE_DAEMON;
|
|
772
|
+
if (flag === "0" || flag === "false") return false;
|
|
773
|
+
return true;
|
|
774
|
+
}
|
|
775
|
+
portfilePath() {
|
|
776
|
+
return import_path3.default.join(process.cwd(), ".dbcube", "daemon", `${this.name}.json`);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Ensures a usable daemon connection. Returns false when the daemon
|
|
780
|
+
* can't be used (old binary, startup failure) so callers fall back
|
|
781
|
+
* to one-shot spawn mode.
|
|
782
|
+
*/
|
|
783
|
+
async ensure() {
|
|
784
|
+
if (this.socket && !this.socket.destroyed) return true;
|
|
785
|
+
if (this.starting) return this.starting;
|
|
786
|
+
this.starting = this.connectOrStart().catch(() => false);
|
|
787
|
+
const result = await this.starting;
|
|
788
|
+
this.starting = null;
|
|
789
|
+
return result;
|
|
790
|
+
}
|
|
791
|
+
async connectOrStart() {
|
|
792
|
+
const port = this.readPortfile();
|
|
793
|
+
if (port && await this.tryConnect(port)) return true;
|
|
794
|
+
try {
|
|
795
|
+
import_fs.default.unlinkSync(this.portfilePath());
|
|
796
|
+
} catch {
|
|
797
|
+
}
|
|
798
|
+
this.spawnDaemon();
|
|
799
|
+
const deadline = Date.now() + 1e4;
|
|
800
|
+
while (Date.now() < deadline) {
|
|
801
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
802
|
+
const newPort = this.readPortfile();
|
|
803
|
+
if (newPort && await this.tryConnect(newPort)) return true;
|
|
804
|
+
}
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
readPortfile() {
|
|
808
|
+
try {
|
|
809
|
+
const raw = import_fs.default.readFileSync(this.portfilePath(), "utf8");
|
|
810
|
+
const info = JSON.parse(raw);
|
|
811
|
+
return typeof info.port === "number" ? info.port : null;
|
|
812
|
+
} catch {
|
|
813
|
+
return null;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
spawnDaemon() {
|
|
817
|
+
const child = (0, import_child_process.spawn)(this.binaryPath, [...this.engineArgs, "--action", "server", "--tcp-port", "9944"], {
|
|
818
|
+
detached: true,
|
|
819
|
+
stdio: "ignore",
|
|
820
|
+
cwd: process.cwd()
|
|
821
|
+
});
|
|
822
|
+
child.unref();
|
|
823
|
+
}
|
|
824
|
+
tryConnect(port) {
|
|
825
|
+
return new Promise((resolve5) => {
|
|
826
|
+
const socket = import_net.default.createConnection({ host: "127.0.0.1", port }, async () => {
|
|
827
|
+
socket.setNoDelay(true);
|
|
828
|
+
this.attach(socket);
|
|
829
|
+
try {
|
|
830
|
+
const pong = await this.send({ action: "ping" });
|
|
831
|
+
resolve5(pong.status === 200);
|
|
832
|
+
} catch {
|
|
833
|
+
this.detach();
|
|
834
|
+
resolve5(false);
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
socket.once("error", () => resolve5(false));
|
|
838
|
+
socket.setTimeout(3e3, () => {
|
|
839
|
+
socket.destroy();
|
|
840
|
+
resolve5(false);
|
|
841
|
+
});
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
attach(socket) {
|
|
845
|
+
this.socket = socket;
|
|
846
|
+
this.buffer = "";
|
|
847
|
+
socket.setTimeout(0);
|
|
848
|
+
socket.on("data", (chunk) => this.onData(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
|
|
849
|
+
socket.on("close", () => this.detach());
|
|
850
|
+
socket.on("error", () => this.detach());
|
|
851
|
+
}
|
|
852
|
+
detach() {
|
|
853
|
+
if (this.socket) {
|
|
854
|
+
this.socket.removeAllListeners();
|
|
855
|
+
this.socket.destroy();
|
|
856
|
+
this.socket = null;
|
|
857
|
+
}
|
|
858
|
+
for (const req of this.pending.splice(0)) {
|
|
859
|
+
clearTimeout(req.timer);
|
|
860
|
+
req.reject(new Error("Daemon connection lost"));
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
onData(chunk) {
|
|
864
|
+
this.buffer += chunk.toString("utf8");
|
|
865
|
+
let idx;
|
|
866
|
+
while ((idx = this.buffer.indexOf("\n")) !== -1) {
|
|
867
|
+
const line = this.buffer.slice(0, idx).trim();
|
|
868
|
+
this.buffer = this.buffer.slice(idx + 1);
|
|
869
|
+
if (!line) continue;
|
|
870
|
+
const marker = line.indexOf("PROCESS_RESPONSE:");
|
|
871
|
+
if (marker === -1) continue;
|
|
872
|
+
const jsonPart = line.slice(marker + "PROCESS_RESPONSE:".length);
|
|
873
|
+
const req = this.pending.shift();
|
|
874
|
+
if (!req) continue;
|
|
875
|
+
clearTimeout(req.timer);
|
|
876
|
+
try {
|
|
877
|
+
req.resolve(JSON.parse(jsonPart));
|
|
878
|
+
} catch (e) {
|
|
879
|
+
req.reject(new Error(`Invalid daemon response: ${e.message}`));
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Sends a request to the daemon. Responses arrive in FIFO order on the
|
|
885
|
+
* single socket, so pending requests resolve in send order.
|
|
886
|
+
*/
|
|
887
|
+
send(payload) {
|
|
888
|
+
return new Promise((resolve5, reject) => {
|
|
889
|
+
if (!this.socket || this.socket.destroyed) {
|
|
890
|
+
reject(new Error("Daemon not connected"));
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
const timer = setTimeout(() => {
|
|
894
|
+
const i = this.pending.findIndex((p) => p.timer === timer);
|
|
895
|
+
if (i !== -1) this.pending.splice(i, 1);
|
|
896
|
+
reject(new Error("Daemon request timeout"));
|
|
897
|
+
}, this.requestTimeout);
|
|
898
|
+
this.pending.push({ resolve: resolve5, reject, timer });
|
|
899
|
+
this.socket.write(JSON.stringify(payload) + "\n");
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
async execute(dml, txId) {
|
|
903
|
+
const payload = { action: "execute", dml: JSON.stringify(dml) };
|
|
904
|
+
if (txId) payload.tx_id = txId;
|
|
905
|
+
return this.send(payload);
|
|
906
|
+
}
|
|
907
|
+
async raw(query, params = [], txId) {
|
|
908
|
+
const payload = { action: "raw", query, params };
|
|
909
|
+
if (txId) payload.tx_id = txId;
|
|
910
|
+
return this.send(payload);
|
|
911
|
+
}
|
|
912
|
+
async begin() {
|
|
913
|
+
const res = await this.send({ action: "begin" });
|
|
914
|
+
if (res.status !== 200 || !res.data?.tx_id) {
|
|
915
|
+
throw new Error(res.message || "Failed to begin transaction");
|
|
916
|
+
}
|
|
917
|
+
return res.data.tx_id;
|
|
918
|
+
}
|
|
919
|
+
async commit(txId) {
|
|
920
|
+
const res = await this.send({ action: "commit", tx_id: txId });
|
|
921
|
+
if (res.status !== 200) throw new Error(res.message || "Failed to commit transaction");
|
|
922
|
+
}
|
|
923
|
+
async rollback(txId) {
|
|
924
|
+
const res = await this.send({ action: "rollback", tx_id: txId });
|
|
925
|
+
if (res.status !== 200) throw new Error(res.message || "Failed to rollback transaction");
|
|
926
|
+
}
|
|
927
|
+
async shutdown() {
|
|
928
|
+
try {
|
|
929
|
+
await this.send({ action: "shutdown" });
|
|
930
|
+
} catch {
|
|
931
|
+
}
|
|
932
|
+
this.detach();
|
|
933
|
+
try {
|
|
934
|
+
import_fs.default.unlinkSync(this.portfilePath());
|
|
935
|
+
} catch {
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
// src/lib/Engine.ts
|
|
739
941
|
var Engine = class {
|
|
740
942
|
name;
|
|
741
943
|
config;
|
|
742
944
|
arguments;
|
|
743
945
|
binary = null;
|
|
744
946
|
timeout;
|
|
947
|
+
daemonFailed = false;
|
|
745
948
|
constructor(name, timeout = 3e4) {
|
|
746
949
|
this.name = name;
|
|
747
950
|
this.config = this.setConfig(name);
|
|
@@ -753,6 +956,77 @@ var Engine = class {
|
|
|
753
956
|
this.binary = await Binary.get();
|
|
754
957
|
}
|
|
755
958
|
}
|
|
959
|
+
/**
|
|
960
|
+
* Returns a connected DaemonClient for this database, or null when
|
|
961
|
+
* daemon mode is disabled/unavailable (then callers use spawn mode).
|
|
962
|
+
*/
|
|
963
|
+
async getDaemon() {
|
|
964
|
+
if (this.daemonFailed || !DaemonClient.isEnabled()) return null;
|
|
965
|
+
await this.initializeBinary();
|
|
966
|
+
if (!this.binary) return null;
|
|
967
|
+
const binaryPath = this.binary["query_engine"];
|
|
968
|
+
if (!binaryPath) return null;
|
|
969
|
+
const client = DaemonClient.get(this.name, binaryPath, this.arguments);
|
|
970
|
+
const ok = await client.ensure();
|
|
971
|
+
if (!ok) {
|
|
972
|
+
this.daemonFailed = true;
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
return client;
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Executes a DML plan. Uses the persistent daemon (sub-millisecond
|
|
979
|
+
* overhead) when available; falls back to one-shot spawn otherwise.
|
|
980
|
+
*/
|
|
981
|
+
async executeDml(dml, txId) {
|
|
982
|
+
const daemon = await this.getDaemon();
|
|
983
|
+
if (daemon) {
|
|
984
|
+
try {
|
|
985
|
+
return await daemon.execute(dml, txId);
|
|
986
|
+
} catch (error) {
|
|
987
|
+
if (txId) throw error;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
if (txId) {
|
|
991
|
+
throw new Error("Transactions require daemon mode. Make sure the query-engine binary is up to date (npx dbcube update).");
|
|
992
|
+
}
|
|
993
|
+
return this.run("query_engine", ["--action", "execute", "--dml", JSON.stringify(dml)]);
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Executes raw SQL (or a MongoDB command document) with bound parameters.
|
|
997
|
+
*/
|
|
998
|
+
async rawQuery(query, params = [], txId) {
|
|
999
|
+
const daemon = await this.getDaemon();
|
|
1000
|
+
if (daemon) {
|
|
1001
|
+
try {
|
|
1002
|
+
return await daemon.raw(query, params, txId);
|
|
1003
|
+
} catch (error) {
|
|
1004
|
+
if (txId) throw error;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
if (txId) {
|
|
1008
|
+
throw new Error("Transactions require daemon mode. Make sure the query-engine binary is up to date (npx dbcube update).");
|
|
1009
|
+
}
|
|
1010
|
+
return this.run("query_engine", ["--action", "raw", "--query", query, "--params", JSON.stringify(params)]);
|
|
1011
|
+
}
|
|
1012
|
+
/** Starts a transaction (daemon mode only). Returns the transaction id. */
|
|
1013
|
+
async beginTransaction() {
|
|
1014
|
+
const daemon = await this.getDaemon();
|
|
1015
|
+
if (!daemon) {
|
|
1016
|
+
throw new Error("Transactions require daemon mode. Make sure the query-engine binary is up to date (npx dbcube update).");
|
|
1017
|
+
}
|
|
1018
|
+
return daemon.begin();
|
|
1019
|
+
}
|
|
1020
|
+
async commitTransaction(txId) {
|
|
1021
|
+
const daemon = await this.getDaemon();
|
|
1022
|
+
if (!daemon) throw new Error("Daemon connection lost during transaction");
|
|
1023
|
+
return daemon.commit(txId);
|
|
1024
|
+
}
|
|
1025
|
+
async rollbackTransaction(txId) {
|
|
1026
|
+
const daemon = await this.getDaemon();
|
|
1027
|
+
if (!daemon) throw new Error("Daemon connection lost during transaction");
|
|
1028
|
+
return daemon.rollback(txId);
|
|
1029
|
+
}
|
|
756
1030
|
setArguments() {
|
|
757
1031
|
let args = [];
|
|
758
1032
|
if (this.config.type == "sqlite") {
|
|
@@ -791,7 +1065,7 @@ var Engine = class {
|
|
|
791
1065
|
setConfig(name) {
|
|
792
1066
|
const configInstance = new Config();
|
|
793
1067
|
try {
|
|
794
|
-
const configFilePath =
|
|
1068
|
+
const configFilePath = import_path4.default.resolve(process.cwd(), "dbcube.config.js");
|
|
795
1069
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
796
1070
|
const require2 = (0, import_module.createRequire)(requireUrl);
|
|
797
1071
|
if (require2.cache && require2.resolve) {
|
|
@@ -824,7 +1098,7 @@ var Engine = class {
|
|
|
824
1098
|
throw new Error("Binary not initialized");
|
|
825
1099
|
}
|
|
826
1100
|
return new Promise((resolve5, reject) => {
|
|
827
|
-
const child = (0,
|
|
1101
|
+
const child = (0, import_child_process2.spawn)(this.binary[binary], [...this.arguments, ...args]);
|
|
828
1102
|
let stdoutBuffer = "";
|
|
829
1103
|
let stderrBuffer = "";
|
|
830
1104
|
let isResolved = false;
|
|
@@ -910,10 +1184,10 @@ var Engine = class {
|
|
|
910
1184
|
};
|
|
911
1185
|
|
|
912
1186
|
// src/lib/QueryEngine.ts
|
|
913
|
-
var
|
|
1187
|
+
var import_path5 = __toESM(require("path"));
|
|
914
1188
|
var import_module2 = require("module");
|
|
915
|
-
var
|
|
916
|
-
var
|
|
1189
|
+
var net2 = __toESM(require("net"));
|
|
1190
|
+
var import_child_process3 = require("child_process");
|
|
917
1191
|
var globalTcpServers = /* @__PURE__ */ new Map();
|
|
918
1192
|
var globalTcpConnections = /* @__PURE__ */ new Map();
|
|
919
1193
|
var connectionQueues = /* @__PURE__ */ new Map();
|
|
@@ -950,7 +1224,7 @@ var QueryEngine = class {
|
|
|
950
1224
|
}
|
|
951
1225
|
isPortAvailable(port) {
|
|
952
1226
|
return new Promise((resolve5) => {
|
|
953
|
-
const tester =
|
|
1227
|
+
const tester = net2.createServer();
|
|
954
1228
|
tester.once("error", () => resolve5(false));
|
|
955
1229
|
tester.once("listening", () => {
|
|
956
1230
|
tester.close();
|
|
@@ -1002,7 +1276,7 @@ var QueryEngine = class {
|
|
|
1002
1276
|
setConfig(name) {
|
|
1003
1277
|
const configInstance = new Config();
|
|
1004
1278
|
try {
|
|
1005
|
-
const configFilePath =
|
|
1279
|
+
const configFilePath = import_path5.default.resolve(process.cwd(), "dbcube.config.js");
|
|
1006
1280
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
1007
1281
|
const require2 = (0, import_module2.createRequire)(requireUrl);
|
|
1008
1282
|
if (require2.cache && require2.resolve) {
|
|
@@ -1064,7 +1338,7 @@ var QueryEngine = class {
|
|
|
1064
1338
|
}
|
|
1065
1339
|
async isServerResponding(port) {
|
|
1066
1340
|
return new Promise((resolve5) => {
|
|
1067
|
-
const client = new
|
|
1341
|
+
const client = new net2.Socket();
|
|
1068
1342
|
const timeout = setTimeout(() => {
|
|
1069
1343
|
client.destroy();
|
|
1070
1344
|
resolve5(false);
|
|
@@ -1102,7 +1376,7 @@ var QueryEngine = class {
|
|
|
1102
1376
|
this.tcpPort = await this.findAvailablePort(this.tcpPort);
|
|
1103
1377
|
return new Promise((resolve5, reject) => {
|
|
1104
1378
|
const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", this.tcpPort.toString()];
|
|
1105
|
-
const serverProcess = (0,
|
|
1379
|
+
const serverProcess = (0, import_child_process3.spawn)(this.binary["query_engine"], serverArgs);
|
|
1106
1380
|
let started = false;
|
|
1107
1381
|
const timeout = setTimeout(() => {
|
|
1108
1382
|
if (!started) {
|
|
@@ -1185,7 +1459,7 @@ var QueryEngine = class {
|
|
|
1185
1459
|
}
|
|
1186
1460
|
async createNewConnection(port) {
|
|
1187
1461
|
return new Promise((resolve5, reject) => {
|
|
1188
|
-
const client = new
|
|
1462
|
+
const client = new net2.Socket();
|
|
1189
1463
|
client.setNoDelay(true);
|
|
1190
1464
|
client.setKeepAlive(true, 6e4);
|
|
1191
1465
|
const timeout = setTimeout(() => {
|
|
@@ -1262,7 +1536,7 @@ var QueryEngine = class {
|
|
|
1262
1536
|
throw new Error("Binary not initialized");
|
|
1263
1537
|
}
|
|
1264
1538
|
return new Promise((resolve5, reject) => {
|
|
1265
|
-
const child = (0,
|
|
1539
|
+
const child = (0, import_child_process3.spawn)(this.binary[binary], [...this.arguments, ...args]);
|
|
1266
1540
|
let stdoutBuffer = "";
|
|
1267
1541
|
let stderrBuffer = "";
|
|
1268
1542
|
let isResolved = false;
|
|
@@ -1369,15 +1643,15 @@ var QueryEngine = class {
|
|
|
1369
1643
|
};
|
|
1370
1644
|
|
|
1371
1645
|
// src/lib/SqliteExecutor.ts
|
|
1372
|
-
var
|
|
1373
|
-
var
|
|
1374
|
-
var
|
|
1646
|
+
var import_child_process4 = require("child_process");
|
|
1647
|
+
var path6 = __toESM(require("path"));
|
|
1648
|
+
var fs4 = __toESM(require("fs"));
|
|
1375
1649
|
var import_util = require("util");
|
|
1376
1650
|
var import_module3 = require("module");
|
|
1377
1651
|
var import_url3 = require("url");
|
|
1378
|
-
var
|
|
1652
|
+
var import_path6 = require("path");
|
|
1379
1653
|
var import_meta3 = {};
|
|
1380
|
-
var execAsync = (0, import_util.promisify)(
|
|
1654
|
+
var execAsync = (0, import_util.promisify)(import_child_process4.exec);
|
|
1381
1655
|
var SqliteExecutor = class {
|
|
1382
1656
|
binaryPath;
|
|
1383
1657
|
dbPath;
|
|
@@ -1387,13 +1661,13 @@ var SqliteExecutor = class {
|
|
|
1387
1661
|
}
|
|
1388
1662
|
findVersionedBinary(binDir, platform2) {
|
|
1389
1663
|
try {
|
|
1390
|
-
const files =
|
|
1664
|
+
const files = fs4.readdirSync(binDir);
|
|
1391
1665
|
const extension = platform2 === "win32" ? ".exe" : "";
|
|
1392
1666
|
const platformName = platform2 === "win32" ? "windows" : platform2 === "darwin" ? "macos" : "linux";
|
|
1393
1667
|
const pattern = new RegExp(`^sqlite-engine-v\\d+\\.\\d+\\.\\d+-${platformName}-x64${extension.replace(".", "\\.")}$`);
|
|
1394
1668
|
const matchingFile = files.find((f) => pattern.test(f));
|
|
1395
1669
|
if (matchingFile) {
|
|
1396
|
-
return
|
|
1670
|
+
return path6.join(binDir, matchingFile);
|
|
1397
1671
|
}
|
|
1398
1672
|
} catch (error) {
|
|
1399
1673
|
}
|
|
@@ -1401,36 +1675,36 @@ var SqliteExecutor = class {
|
|
|
1401
1675
|
}
|
|
1402
1676
|
getBinaryPath() {
|
|
1403
1677
|
const __filename2 = typeof import_meta3 !== "undefined" && import_meta3.url ? (0, import_url3.fileURLToPath)(import_meta3.url) : "";
|
|
1404
|
-
const __dirname = __filename2 ? (0,
|
|
1678
|
+
const __dirname = __filename2 ? (0, import_path6.dirname)(__filename2) : process.cwd();
|
|
1405
1679
|
const possibleDirs = [
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1680
|
+
path6.resolve(process.cwd(), ".dbcube", "bin"),
|
|
1681
|
+
path6.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
|
|
1682
|
+
path6.resolve(__dirname, "..", "bin")
|
|
1409
1683
|
];
|
|
1410
1684
|
const platform2 = process.platform;
|
|
1411
1685
|
const extension = platform2 === "win32" ? ".exe" : "";
|
|
1412
1686
|
const platformName = platform2 === "win32" ? "windows" : platform2 === "darwin" ? "macos" : "linux";
|
|
1413
1687
|
for (const dir of possibleDirs) {
|
|
1414
1688
|
const versionedPath = this.findVersionedBinary(dir, platform2);
|
|
1415
|
-
if (versionedPath &&
|
|
1689
|
+
if (versionedPath && fs4.existsSync(versionedPath)) {
|
|
1416
1690
|
return versionedPath;
|
|
1417
1691
|
}
|
|
1418
1692
|
}
|
|
1419
1693
|
const binaryName = `sqlite-engine-${platformName}-x64${extension}`;
|
|
1420
1694
|
for (const dir of possibleDirs) {
|
|
1421
|
-
const fullPath =
|
|
1422
|
-
if (
|
|
1695
|
+
const fullPath = path6.join(dir, binaryName);
|
|
1696
|
+
if (fs4.existsSync(fullPath)) {
|
|
1423
1697
|
return fullPath;
|
|
1424
1698
|
}
|
|
1425
1699
|
}
|
|
1426
1700
|
const fallbackName = `sqlite-engine${extension}`;
|
|
1427
1701
|
for (const dir of possibleDirs) {
|
|
1428
|
-
const fullPath =
|
|
1429
|
-
if (
|
|
1702
|
+
const fullPath = path6.join(dir, fallbackName);
|
|
1703
|
+
if (fs4.existsSync(fullPath)) {
|
|
1430
1704
|
return fullPath;
|
|
1431
1705
|
}
|
|
1432
1706
|
}
|
|
1433
|
-
return
|
|
1707
|
+
return path6.join(possibleDirs[0], binaryName);
|
|
1434
1708
|
}
|
|
1435
1709
|
async executeBinary(args) {
|
|
1436
1710
|
const escapedArgs = args.map((arg) => {
|
|
@@ -1581,9 +1855,9 @@ var SqliteExecutor = class {
|
|
|
1581
1855
|
};
|
|
1582
1856
|
|
|
1583
1857
|
// src/lib/DbConfig.ts
|
|
1584
|
-
var
|
|
1585
|
-
var
|
|
1586
|
-
var rootPath =
|
|
1858
|
+
var path7 = __toESM(require("path"));
|
|
1859
|
+
var import_fs2 = __toESM(require("fs"));
|
|
1860
|
+
var rootPath = path7.resolve(process.cwd(), ".dbcube");
|
|
1587
1861
|
var SQLite = class {
|
|
1588
1862
|
executor = null;
|
|
1589
1863
|
database;
|
|
@@ -1593,11 +1867,11 @@ var SQLite = class {
|
|
|
1593
1867
|
async ifExist() {
|
|
1594
1868
|
if (this.database) {
|
|
1595
1869
|
const dbPath = this.database || ":memory:";
|
|
1596
|
-
const configPath =
|
|
1597
|
-
if (!
|
|
1598
|
-
|
|
1870
|
+
const configPath = path7.join(rootPath, dbPath + ".db");
|
|
1871
|
+
if (!import_fs2.default.existsSync(rootPath)) {
|
|
1872
|
+
import_fs2.default.mkdirSync(rootPath, { recursive: true });
|
|
1599
1873
|
}
|
|
1600
|
-
if (
|
|
1874
|
+
if (import_fs2.default.existsSync(configPath)) {
|
|
1601
1875
|
return true;
|
|
1602
1876
|
}
|
|
1603
1877
|
if (!this.executor) {
|
|
@@ -1612,9 +1886,9 @@ var SQLite = class {
|
|
|
1612
1886
|
try {
|
|
1613
1887
|
if (!this.executor) {
|
|
1614
1888
|
const dbPath = this.database || ":memory:";
|
|
1615
|
-
const configPath =
|
|
1616
|
-
if (!
|
|
1617
|
-
|
|
1889
|
+
const configPath = path7.join(rootPath, dbPath + ".db");
|
|
1890
|
+
if (!import_fs2.default.existsSync(rootPath)) {
|
|
1891
|
+
import_fs2.default.mkdirSync(rootPath, { recursive: true });
|
|
1618
1892
|
}
|
|
1619
1893
|
this.executor = new SqliteExecutor(configPath);
|
|
1620
1894
|
const connected = await this.executor.connect();
|
|
@@ -1796,8 +2070,8 @@ var DbConfig = new SQLite({ DATABASE: "config" });
|
|
|
1796
2070
|
var DbConfig_default = DbConfig;
|
|
1797
2071
|
|
|
1798
2072
|
// src/lib/FileLogger.ts
|
|
1799
|
-
var
|
|
1800
|
-
var
|
|
2073
|
+
var fs6 = __toESM(require("fs"));
|
|
2074
|
+
var path8 = __toESM(require("path"));
|
|
1801
2075
|
var import_events = require("events");
|
|
1802
2076
|
var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
1803
2077
|
static watchers = /* @__PURE__ */ new Map();
|
|
@@ -1812,9 +2086,9 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
1812
2086
|
*/
|
|
1813
2087
|
static async write(filePath, message, level = "INFO", append = true) {
|
|
1814
2088
|
try {
|
|
1815
|
-
const dir =
|
|
1816
|
-
if (!
|
|
1817
|
-
|
|
2089
|
+
const dir = path8.dirname(filePath);
|
|
2090
|
+
if (!fs6.existsSync(dir)) {
|
|
2091
|
+
fs6.mkdirSync(dir, { recursive: true });
|
|
1818
2092
|
}
|
|
1819
2093
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
1820
2094
|
const formattedMessage = `[${timestamp}] [${level}] ${message}
|
|
@@ -1824,9 +2098,9 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
1824
2098
|
return true;
|
|
1825
2099
|
}
|
|
1826
2100
|
if (append) {
|
|
1827
|
-
await
|
|
2101
|
+
await fs6.promises.appendFile(filePath, formattedMessage, "utf8");
|
|
1828
2102
|
} else {
|
|
1829
|
-
await
|
|
2103
|
+
await fs6.promises.writeFile(filePath, formattedMessage, "utf8");
|
|
1830
2104
|
}
|
|
1831
2105
|
return true;
|
|
1832
2106
|
} catch (error) {
|
|
@@ -1851,12 +2125,12 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
1851
2125
|
const buffer = _FileLogger.buffers.get(filePath);
|
|
1852
2126
|
if (buffer && buffer.length > 0) {
|
|
1853
2127
|
try {
|
|
1854
|
-
const dir =
|
|
1855
|
-
if (!
|
|
1856
|
-
|
|
2128
|
+
const dir = path8.dirname(filePath);
|
|
2129
|
+
if (!fs6.existsSync(dir)) {
|
|
2130
|
+
fs6.mkdirSync(dir, { recursive: true });
|
|
1857
2131
|
}
|
|
1858
2132
|
const content = buffer.join("");
|
|
1859
|
-
await
|
|
2133
|
+
await fs6.promises.appendFile(filePath, content, "utf8");
|
|
1860
2134
|
_FileLogger.buffers.delete(filePath);
|
|
1861
2135
|
return true;
|
|
1862
2136
|
} catch (error) {
|
|
@@ -1992,10 +2266,10 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
1992
2266
|
// Si debe retornar como array de líneas
|
|
1993
2267
|
} = options;
|
|
1994
2268
|
try {
|
|
1995
|
-
if (!
|
|
2269
|
+
if (!fs6.existsSync(filePath)) {
|
|
1996
2270
|
return asArray ? [] : "";
|
|
1997
2271
|
}
|
|
1998
|
-
let content = await
|
|
2272
|
+
let content = await fs6.promises.readFile(filePath, "utf8");
|
|
1999
2273
|
if (asArray) {
|
|
2000
2274
|
let linesArray = content.split("\n").filter((line) => line.trim() !== "");
|
|
2001
2275
|
if (lines !== null) {
|
|
@@ -2038,15 +2312,15 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2038
2312
|
} = options;
|
|
2039
2313
|
let lastSize = 0;
|
|
2040
2314
|
let lastPosition = 0;
|
|
2041
|
-
if (
|
|
2042
|
-
const stats =
|
|
2315
|
+
if (fs6.existsSync(filePath)) {
|
|
2316
|
+
const stats = fs6.statSync(filePath);
|
|
2043
2317
|
lastSize = stats.size;
|
|
2044
2318
|
lastPosition = fromEnd ? stats.size : 0;
|
|
2045
2319
|
}
|
|
2046
2320
|
const listener = async (curr, prev) => {
|
|
2047
2321
|
try {
|
|
2048
2322
|
if (curr.size > lastSize) {
|
|
2049
|
-
const stream =
|
|
2323
|
+
const stream = fs6.createReadStream(filePath, {
|
|
2050
2324
|
start: lastPosition,
|
|
2051
2325
|
end: curr.size - 1,
|
|
2052
2326
|
encoding: "utf8"
|
|
@@ -2074,7 +2348,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2074
2348
|
console.error("Error en watcher:", error);
|
|
2075
2349
|
}
|
|
2076
2350
|
};
|
|
2077
|
-
|
|
2351
|
+
fs6.watchFile(filePath, { persistent, interval }, listener);
|
|
2078
2352
|
const watcherId = `${filePath}_${Date.now()}`;
|
|
2079
2353
|
_FileLogger.watchers.set(watcherId, listener);
|
|
2080
2354
|
return {
|
|
@@ -2082,7 +2356,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2082
2356
|
stop: () => {
|
|
2083
2357
|
const storedListener = _FileLogger.watchers.get(watcherId);
|
|
2084
2358
|
if (storedListener) {
|
|
2085
|
-
|
|
2359
|
+
fs6.unwatchFile(filePath, storedListener);
|
|
2086
2360
|
_FileLogger.watchers.delete(watcherId);
|
|
2087
2361
|
}
|
|
2088
2362
|
},
|
|
@@ -2095,7 +2369,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2095
2369
|
static stopAllWatchers() {
|
|
2096
2370
|
for (const [watcherId] of _FileLogger.watchers) {
|
|
2097
2371
|
const filePath = watcherId.split("_")[0];
|
|
2098
|
-
|
|
2372
|
+
fs6.unwatchFile(filePath);
|
|
2099
2373
|
}
|
|
2100
2374
|
_FileLogger.watchers.clear();
|
|
2101
2375
|
}
|
|
@@ -2125,7 +2399,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2125
2399
|
if (lines.length > maxLines) {
|
|
2126
2400
|
const keepLines = lines.slice(-maxLines);
|
|
2127
2401
|
const content = keepLines.join("\n") + "\n";
|
|
2128
|
-
await
|
|
2402
|
+
await fs6.promises.writeFile(filePath, content, "utf8");
|
|
2129
2403
|
return lines.length - maxLines;
|
|
2130
2404
|
}
|
|
2131
2405
|
return 0;
|
|
@@ -2140,8 +2414,8 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2140
2414
|
*/
|
|
2141
2415
|
static async deleteLogFile(filePath) {
|
|
2142
2416
|
try {
|
|
2143
|
-
if (
|
|
2144
|
-
await
|
|
2417
|
+
if (fs6.existsSync(filePath)) {
|
|
2418
|
+
await fs6.promises.unlink(filePath);
|
|
2145
2419
|
return true;
|
|
2146
2420
|
}
|
|
2147
2421
|
return false;
|
|
@@ -2572,6 +2846,7 @@ function convertToType(value, type2) {
|
|
|
2572
2846
|
Binary,
|
|
2573
2847
|
ComputedFieldProcessor,
|
|
2574
2848
|
Config,
|
|
2849
|
+
DaemonClient,
|
|
2575
2850
|
DbConfig,
|
|
2576
2851
|
Engine,
|
|
2577
2852
|
FileLogger,
|