@dbcube/core 3.0.4 → 3.0.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 +402 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +25 -12
- package/dist/index.d.ts +25 -12
- package/dist/index.js +399 -147
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -556,10 +556,6 @@ var Engine = class {
|
|
|
556
556
|
arguments;
|
|
557
557
|
binary = null;
|
|
558
558
|
timeout;
|
|
559
|
-
childProcess = null;
|
|
560
|
-
pendingRequests = /* @__PURE__ */ new Map();
|
|
561
|
-
requestCounter = 0;
|
|
562
|
-
isProcessStarting = false;
|
|
563
559
|
constructor(name, timeout = 3e4) {
|
|
564
560
|
this.name = name;
|
|
565
561
|
this.config = this.setConfig(name);
|
|
@@ -582,9 +578,7 @@ var Engine = class {
|
|
|
582
578
|
"--database",
|
|
583
579
|
this.config.config.DATABASE + ".db",
|
|
584
580
|
"--motor",
|
|
585
|
-
this.config.type
|
|
586
|
-
"--persistent"
|
|
587
|
-
// Agregar flag para modo persistente si el binario lo soporta
|
|
581
|
+
this.config.type
|
|
588
582
|
];
|
|
589
583
|
} else {
|
|
590
584
|
args = [
|
|
@@ -603,9 +597,7 @@ var Engine = class {
|
|
|
603
597
|
"--password",
|
|
604
598
|
this.config.config.PASSWORD,
|
|
605
599
|
"--motor",
|
|
606
|
-
this.config.type
|
|
607
|
-
"--persistent"
|
|
608
|
-
// Agregar flag para modo persistente si el binario lo soporta
|
|
600
|
+
this.config.type
|
|
609
601
|
];
|
|
610
602
|
}
|
|
611
603
|
return args;
|
|
@@ -635,160 +627,419 @@ var Engine = class {
|
|
|
635
627
|
getConfig() {
|
|
636
628
|
return this.config;
|
|
637
629
|
}
|
|
638
|
-
async
|
|
639
|
-
|
|
640
|
-
|
|
630
|
+
async run(binary, args) {
|
|
631
|
+
await this.initializeBinary();
|
|
632
|
+
if (!this.binary) {
|
|
633
|
+
throw new Error("Binary not initialized");
|
|
641
634
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
635
|
+
return new Promise((resolve5, reject) => {
|
|
636
|
+
const child = spawn(this.binary[binary], [...this.arguments, ...args]);
|
|
637
|
+
let stdoutBuffer = "";
|
|
638
|
+
let stderrBuffer = "";
|
|
639
|
+
let isResolved = false;
|
|
640
|
+
const timeoutId = setTimeout(() => {
|
|
641
|
+
if (!isResolved) {
|
|
642
|
+
isResolved = true;
|
|
643
|
+
child.kill();
|
|
644
|
+
reject(new Error("Process timeout"));
|
|
645
|
+
}
|
|
646
|
+
}, this.timeout);
|
|
647
|
+
const resolveOnce = (response) => {
|
|
648
|
+
if (!isResolved) {
|
|
649
|
+
isResolved = true;
|
|
650
|
+
clearTimeout(timeoutId);
|
|
651
|
+
resolve5(response);
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
child.stdout.on("data", (data) => {
|
|
655
|
+
stdoutBuffer += data.toString();
|
|
656
|
+
const match = stdoutBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
657
|
+
if (match) {
|
|
658
|
+
try {
|
|
659
|
+
const response = JSON.parse(match[1]);
|
|
660
|
+
resolveOnce({
|
|
661
|
+
status: response.status,
|
|
662
|
+
message: response.message,
|
|
663
|
+
data: response.data
|
|
664
|
+
});
|
|
665
|
+
} catch (error) {
|
|
666
|
+
resolveOnce({
|
|
667
|
+
status: 500,
|
|
668
|
+
message: "Failed to parse response JSON",
|
|
669
|
+
data: null
|
|
670
|
+
});
|
|
649
671
|
}
|
|
650
|
-
}
|
|
651
|
-
checkProcess();
|
|
672
|
+
}
|
|
652
673
|
});
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
674
|
+
child.stderr.on("data", (data) => {
|
|
675
|
+
stderrBuffer += data.toString();
|
|
676
|
+
const match = stderrBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
677
|
+
if (match) {
|
|
678
|
+
try {
|
|
679
|
+
const response = JSON.parse(match[1]);
|
|
680
|
+
resolveOnce({
|
|
681
|
+
status: response.status,
|
|
682
|
+
message: response.message,
|
|
683
|
+
data: response.data
|
|
684
|
+
});
|
|
685
|
+
} catch (error) {
|
|
686
|
+
resolveOnce({
|
|
687
|
+
status: 500,
|
|
688
|
+
message: "Failed to parse response JSON",
|
|
689
|
+
data: null
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
}
|
|
662
693
|
});
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
let stdoutBuffer = "";
|
|
671
|
-
let stderrBuffer = "";
|
|
672
|
-
this.childProcess.stdout?.on("data", (data) => {
|
|
673
|
-
stdoutBuffer += data.toString();
|
|
674
|
-
this.processBuffer(stdoutBuffer, "stdout");
|
|
675
|
-
});
|
|
676
|
-
this.childProcess.stderr?.on("data", (data) => {
|
|
677
|
-
stderrBuffer += data.toString();
|
|
678
|
-
this.processBuffer(stderrBuffer, "stderr");
|
|
679
|
-
});
|
|
680
|
-
this.childProcess.on("close", (code) => {
|
|
681
|
-
this.handleProcessClose(code);
|
|
682
|
-
});
|
|
683
|
-
this.childProcess.on("error", (error) => {
|
|
684
|
-
this.handleProcessError(error);
|
|
685
|
-
});
|
|
686
|
-
this.childProcess.unref();
|
|
687
|
-
}
|
|
688
|
-
processBuffer(buffer, source) {
|
|
689
|
-
const responsePattern = /PROCESS_RESPONSE:(\{[^}]*\})/g;
|
|
690
|
-
let match;
|
|
691
|
-
while ((match = responsePattern.exec(buffer)) !== null) {
|
|
692
|
-
try {
|
|
693
|
-
const response = JSON.parse(match[1]);
|
|
694
|
-
const requestId = response.requestId || "unknown";
|
|
695
|
-
const pendingRequest = this.pendingRequests.get(requestId);
|
|
696
|
-
if (pendingRequest) {
|
|
697
|
-
clearTimeout(pendingRequest.timeoutId);
|
|
698
|
-
this.pendingRequests.delete(requestId);
|
|
699
|
-
pendingRequest.resolve({
|
|
700
|
-
status: response.status,
|
|
701
|
-
message: response.message,
|
|
702
|
-
data: response.data
|
|
694
|
+
child.on("close", (code) => {
|
|
695
|
+
clearTimeout(timeoutId);
|
|
696
|
+
if (!isResolved) {
|
|
697
|
+
resolveOnce({
|
|
698
|
+
status: code === 0 ? 200 : 500,
|
|
699
|
+
message: code === 0 ? "Process completed" : `Process exited with code ${code}`,
|
|
700
|
+
data: null
|
|
703
701
|
});
|
|
704
702
|
}
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
|
|
703
|
+
});
|
|
704
|
+
child.on("error", (error) => {
|
|
705
|
+
clearTimeout(timeoutId);
|
|
706
|
+
if (!isResolved) {
|
|
707
|
+
resolveOnce({
|
|
708
|
+
status: 500,
|
|
709
|
+
message: `Process error: ${error.message}`,
|
|
710
|
+
data: null
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
child.unref();
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
// src/lib/QueryEngine.ts
|
|
720
|
+
import path4 from "path";
|
|
721
|
+
import { createRequire as createRequire2 } from "module";
|
|
722
|
+
import * as net from "net";
|
|
723
|
+
import { spawn as spawn2 } from "child_process";
|
|
724
|
+
var globalTcpServers = /* @__PURE__ */ new Map();
|
|
725
|
+
var QueryEngine = class {
|
|
726
|
+
name;
|
|
727
|
+
config;
|
|
728
|
+
arguments;
|
|
729
|
+
binary = null;
|
|
730
|
+
timeout;
|
|
731
|
+
connectionId;
|
|
732
|
+
tcpPort;
|
|
733
|
+
constructor(name, timeout = 3e4) {
|
|
734
|
+
this.name = name;
|
|
735
|
+
this.config = this.setConfig(name);
|
|
736
|
+
this.arguments = this.setArguments();
|
|
737
|
+
this.timeout = timeout;
|
|
738
|
+
this.connectionId = `${name}_${this.config.type}_${this.config.config.DATABASE}_${this.config.config.HOST || "localhost"}`;
|
|
739
|
+
this.tcpPort = this.generatePort();
|
|
740
|
+
}
|
|
741
|
+
generatePort() {
|
|
742
|
+
let hash = 0;
|
|
743
|
+
for (let i = 0; i < this.connectionId.length; i++) {
|
|
744
|
+
const char = this.connectionId.charCodeAt(i);
|
|
745
|
+
hash = (hash << 5) - hash + char;
|
|
746
|
+
hash = hash & hash;
|
|
708
747
|
}
|
|
748
|
+
return 8100 + Math.abs(hash % 900);
|
|
709
749
|
}
|
|
710
|
-
|
|
711
|
-
this.
|
|
712
|
-
|
|
713
|
-
clearTimeout(request.timeoutId);
|
|
714
|
-
request.reject(new Error(`Process closed with code ${code}`));
|
|
750
|
+
async initializeBinary() {
|
|
751
|
+
if (!this.binary) {
|
|
752
|
+
this.binary = await Binary.get();
|
|
715
753
|
}
|
|
716
|
-
this.pendingRequests.clear();
|
|
717
754
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
755
|
+
setArguments() {
|
|
756
|
+
let args = [];
|
|
757
|
+
if (this.config.type == "sqlite") {
|
|
758
|
+
args = [
|
|
759
|
+
"--id",
|
|
760
|
+
"dbcube-" + this.name,
|
|
761
|
+
"--database-ref",
|
|
762
|
+
this.name,
|
|
763
|
+
"--database",
|
|
764
|
+
this.config.config.DATABASE + ".db",
|
|
765
|
+
"--motor",
|
|
766
|
+
this.config.type
|
|
767
|
+
];
|
|
768
|
+
} else {
|
|
769
|
+
args = [
|
|
770
|
+
"--id",
|
|
771
|
+
"dbcube-" + this.name,
|
|
772
|
+
"--database-ref",
|
|
773
|
+
this.name,
|
|
774
|
+
"--database",
|
|
775
|
+
this.config.config.DATABASE,
|
|
776
|
+
"--host",
|
|
777
|
+
this.config.config.HOST,
|
|
778
|
+
"--port",
|
|
779
|
+
this.config.config.PORT,
|
|
780
|
+
"--user",
|
|
781
|
+
this.config.config.USER,
|
|
782
|
+
"--password",
|
|
783
|
+
this.config.config.PASSWORD,
|
|
784
|
+
"--motor",
|
|
785
|
+
this.config.type
|
|
786
|
+
];
|
|
723
787
|
}
|
|
724
|
-
|
|
788
|
+
return args;
|
|
789
|
+
}
|
|
790
|
+
setConfig(name) {
|
|
791
|
+
const configInstance = new Config();
|
|
792
|
+
try {
|
|
793
|
+
const configFilePath = path4.resolve(process.cwd(), "dbcube.config.js");
|
|
794
|
+
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
795
|
+
const require2 = createRequire2(requireUrl);
|
|
796
|
+
delete require2.cache[require2.resolve(configFilePath)];
|
|
797
|
+
const configModule = require2(configFilePath);
|
|
798
|
+
const configFn = configModule.default || configModule;
|
|
799
|
+
if (typeof configFn === "function") {
|
|
800
|
+
configFn(configInstance);
|
|
801
|
+
} else {
|
|
802
|
+
console.error("\u274C El archivo dbcube.config.js no exporta una funci\xF3n.");
|
|
803
|
+
}
|
|
804
|
+
} catch (error) {
|
|
805
|
+
console.error("\u274C Error loading config file:", error.message);
|
|
806
|
+
if (error.code === "MODULE_NOT_FOUND") {
|
|
807
|
+
console.error("\u274C Config file not found, please create a dbcube.config.js file");
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return configInstance.getDatabase(name);
|
|
811
|
+
}
|
|
812
|
+
getConfig() {
|
|
813
|
+
return this.config;
|
|
725
814
|
}
|
|
726
815
|
async run(binary, args) {
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
816
|
+
const actionIndex = args.findIndex((arg) => arg === "--action");
|
|
817
|
+
const isExecuteAction = actionIndex !== -1 && args[actionIndex + 1] === "execute";
|
|
818
|
+
if (isExecuteAction) {
|
|
819
|
+
return this.executeWithTcpServer(args);
|
|
820
|
+
} else {
|
|
821
|
+
return this.createProcess(binary, args);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
async executeWithTcpServer(args) {
|
|
825
|
+
const serverInfo = globalTcpServers.get(this.connectionId);
|
|
826
|
+
if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
|
|
827
|
+
console.log("\u{1F680} Starting TCP server for ultra-fast queries...");
|
|
828
|
+
await this.startTcpServer();
|
|
829
|
+
}
|
|
830
|
+
console.log("\u26A1 Using TCP server (ultra-fast)");
|
|
831
|
+
return this.sendTcpRequest(args);
|
|
832
|
+
}
|
|
833
|
+
async startTcpServer() {
|
|
834
|
+
await this.initializeBinary();
|
|
835
|
+
if (!this.binary) {
|
|
836
|
+
throw new Error("Binary not initialized");
|
|
730
837
|
}
|
|
731
838
|
return new Promise((resolve5, reject) => {
|
|
732
|
-
const
|
|
733
|
-
const
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
839
|
+
const serverArgs = [...this.arguments, "--action", "server", "--port", this.tcpPort.toString()];
|
|
840
|
+
const serverProcess = spawn2(this.binary["query_engine"], serverArgs);
|
|
841
|
+
let started = false;
|
|
842
|
+
const timeout = setTimeout(() => {
|
|
843
|
+
if (!started) {
|
|
844
|
+
serverProcess.kill();
|
|
845
|
+
reject(new Error("TCP server startup timeout"));
|
|
738
846
|
}
|
|
847
|
+
}, 1e4);
|
|
848
|
+
serverProcess.stdout.on("data", (data) => {
|
|
849
|
+
const output = data.toString();
|
|
850
|
+
console.log(`[TCP Server] ${output.trim()}`);
|
|
851
|
+
if (output.includes("TCP Server listening on")) {
|
|
852
|
+
if (!started) {
|
|
853
|
+
started = true;
|
|
854
|
+
clearTimeout(timeout);
|
|
855
|
+
globalTcpServers.set(this.connectionId, {
|
|
856
|
+
port: this.tcpPort,
|
|
857
|
+
process: serverProcess
|
|
858
|
+
});
|
|
859
|
+
resolve5();
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
serverProcess.stderr.on("data", (data) => {
|
|
864
|
+
console.error(`[TCP Server Error] ${data.toString().trim()}`);
|
|
865
|
+
});
|
|
866
|
+
serverProcess.on("close", (code) => {
|
|
867
|
+
console.log(`[TCP Server] Process exited with code ${code}`);
|
|
868
|
+
globalTcpServers.delete(this.connectionId);
|
|
869
|
+
});
|
|
870
|
+
serverProcess.on("error", (error) => {
|
|
871
|
+
if (!started) {
|
|
872
|
+
clearTimeout(timeout);
|
|
873
|
+
reject(error);
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
async sendTcpRequest(args) {
|
|
879
|
+
return new Promise((resolve5, reject) => {
|
|
880
|
+
const client = new net.Socket();
|
|
881
|
+
let responseBuffer = "";
|
|
882
|
+
const timeout = setTimeout(() => {
|
|
883
|
+
client.destroy();
|
|
884
|
+
reject(new Error("TCP request timeout"));
|
|
739
885
|
}, this.timeout);
|
|
740
|
-
this.
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
886
|
+
client.connect(this.tcpPort, "127.0.0.1", () => {
|
|
887
|
+
const dmlIndex = args.findIndex((arg) => arg === "--dml");
|
|
888
|
+
const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
|
|
889
|
+
if (!dmlJson) {
|
|
890
|
+
client.destroy();
|
|
891
|
+
reject(new Error("No DML found in arguments"));
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
const command = {
|
|
895
|
+
action: "execute",
|
|
896
|
+
dml: dmlJson
|
|
897
|
+
};
|
|
898
|
+
client.write(JSON.stringify(command));
|
|
745
899
|
});
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
900
|
+
client.on("data", (data) => {
|
|
901
|
+
responseBuffer += data.toString();
|
|
902
|
+
const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
903
|
+
if (match) {
|
|
904
|
+
clearTimeout(timeout);
|
|
905
|
+
client.destroy();
|
|
906
|
+
try {
|
|
907
|
+
const response = JSON.parse(match[1]);
|
|
908
|
+
resolve5({
|
|
909
|
+
status: response.status,
|
|
910
|
+
message: response.message,
|
|
911
|
+
data: response.data
|
|
912
|
+
});
|
|
913
|
+
} catch (error) {
|
|
914
|
+
reject(new Error("Failed to parse TCP response"));
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
client.on("error", (error) => {
|
|
919
|
+
clearTimeout(timeout);
|
|
755
920
|
reject(error);
|
|
756
|
-
}
|
|
921
|
+
});
|
|
922
|
+
client.on("close", () => {
|
|
923
|
+
clearTimeout(timeout);
|
|
924
|
+
if (responseBuffer && !responseBuffer.includes("\n")) {
|
|
925
|
+
reject(new Error("Incomplete TCP response"));
|
|
926
|
+
}
|
|
927
|
+
});
|
|
757
928
|
});
|
|
758
929
|
}
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
if (
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
930
|
+
async createProcess(binary, args) {
|
|
931
|
+
await this.initializeBinary();
|
|
932
|
+
if (!this.binary) {
|
|
933
|
+
throw new Error("Binary not initialized");
|
|
934
|
+
}
|
|
935
|
+
return new Promise((resolve5, reject) => {
|
|
936
|
+
const child = spawn2(this.binary[binary], [...this.arguments, ...args]);
|
|
937
|
+
let stdoutBuffer = "";
|
|
938
|
+
let stderrBuffer = "";
|
|
939
|
+
let isResolved = false;
|
|
940
|
+
const timeoutId = setTimeout(() => {
|
|
941
|
+
if (!isResolved) {
|
|
942
|
+
isResolved = true;
|
|
943
|
+
child.kill();
|
|
944
|
+
reject(new Error("Process timeout"));
|
|
945
|
+
}
|
|
946
|
+
}, this.timeout);
|
|
947
|
+
const resolveOnce = (response) => {
|
|
948
|
+
if (!isResolved) {
|
|
949
|
+
isResolved = true;
|
|
950
|
+
clearTimeout(timeoutId);
|
|
951
|
+
resolve5(response);
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
child.stdout.on("data", (data) => {
|
|
955
|
+
stdoutBuffer += data.toString();
|
|
956
|
+
const match = stdoutBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
957
|
+
if (match) {
|
|
958
|
+
try {
|
|
959
|
+
const response = JSON.parse(match[1]);
|
|
960
|
+
resolveOnce({
|
|
961
|
+
status: response.status,
|
|
962
|
+
message: response.message,
|
|
963
|
+
data: response.data
|
|
964
|
+
});
|
|
965
|
+
} catch (error) {
|
|
966
|
+
resolveOnce({
|
|
967
|
+
status: 500,
|
|
968
|
+
message: "Failed to parse response JSON",
|
|
969
|
+
data: null
|
|
970
|
+
});
|
|
775
971
|
}
|
|
776
|
-
}
|
|
972
|
+
}
|
|
777
973
|
});
|
|
778
|
-
|
|
974
|
+
child.stderr.on("data", (data) => {
|
|
975
|
+
stderrBuffer += data.toString();
|
|
976
|
+
const match = stderrBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
977
|
+
if (match) {
|
|
978
|
+
try {
|
|
979
|
+
const response = JSON.parse(match[1]);
|
|
980
|
+
resolveOnce({
|
|
981
|
+
status: response.status,
|
|
982
|
+
message: response.message,
|
|
983
|
+
data: response.data
|
|
984
|
+
});
|
|
985
|
+
} catch (error) {
|
|
986
|
+
resolveOnce({
|
|
987
|
+
status: 500,
|
|
988
|
+
message: "Failed to parse response JSON",
|
|
989
|
+
data: null
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
child.on("close", (code) => {
|
|
995
|
+
clearTimeout(timeoutId);
|
|
996
|
+
if (!isResolved) {
|
|
997
|
+
resolveOnce({
|
|
998
|
+
status: code === 0 ? 200 : 500,
|
|
999
|
+
message: code === 0 ? "Process completed" : `Process exited with code ${code}`,
|
|
1000
|
+
data: null
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
child.on("error", (error) => {
|
|
1005
|
+
clearTimeout(timeoutId);
|
|
1006
|
+
if (!isResolved) {
|
|
1007
|
+
resolveOnce({
|
|
1008
|
+
status: 500,
|
|
1009
|
+
message: `Process error: ${error.message}`,
|
|
1010
|
+
data: null
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
});
|
|
1014
|
+
child.unref();
|
|
1015
|
+
});
|
|
779
1016
|
}
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
1017
|
+
async disconnect() {
|
|
1018
|
+
const serverInfo = globalTcpServers.get(this.connectionId);
|
|
1019
|
+
if (serverInfo && serverInfo.process && !serverInfo.process.killed) {
|
|
1020
|
+
console.log("\u{1F50C} Stopping TCP server...");
|
|
1021
|
+
serverInfo.process.kill();
|
|
1022
|
+
globalTcpServers.delete(this.connectionId);
|
|
1023
|
+
return {
|
|
1024
|
+
status: 200,
|
|
1025
|
+
message: "TCP server stopped",
|
|
1026
|
+
data: null
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
return {
|
|
1030
|
+
status: 200,
|
|
1031
|
+
message: "No TCP server to stop",
|
|
1032
|
+
data: null
|
|
1033
|
+
};
|
|
783
1034
|
}
|
|
784
1035
|
};
|
|
785
1036
|
|
|
786
1037
|
// src/lib/SqliteExecutor.ts
|
|
787
1038
|
import { exec } from "child_process";
|
|
788
|
-
import * as
|
|
1039
|
+
import * as path5 from "path";
|
|
789
1040
|
import * as fs3 from "fs";
|
|
790
1041
|
import { promisify } from "util";
|
|
791
|
-
import { createRequire as
|
|
1042
|
+
import { createRequire as createRequire3 } from "module";
|
|
792
1043
|
var execAsync = promisify(exec);
|
|
793
1044
|
var SqliteExecutor = class {
|
|
794
1045
|
binaryPath;
|
|
@@ -799,27 +1050,27 @@ var SqliteExecutor = class {
|
|
|
799
1050
|
}
|
|
800
1051
|
getBinaryPath() {
|
|
801
1052
|
const possibleDirs = [
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
1053
|
+
path5.resolve(process.cwd(), ".dbcube", "bin"),
|
|
1054
|
+
path5.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
|
|
1055
|
+
path5.resolve(__dirname, "..", "bin")
|
|
805
1056
|
];
|
|
806
1057
|
const platform2 = process.platform;
|
|
807
1058
|
const extension = platform2 === "win32" ? ".exe" : "";
|
|
808
1059
|
const binaryName = `sqlite-engine-${platform2 === "win32" ? "windows" : platform2 === "darwin" ? "macos" : "linux"}-x64${extension}`;
|
|
809
1060
|
for (const dir of possibleDirs) {
|
|
810
|
-
const fullPath =
|
|
1061
|
+
const fullPath = path5.join(dir, binaryName);
|
|
811
1062
|
if (fs3.existsSync(fullPath)) {
|
|
812
1063
|
return fullPath;
|
|
813
1064
|
}
|
|
814
1065
|
}
|
|
815
1066
|
const fallbackName = `sqlite-engine${extension}`;
|
|
816
1067
|
for (const dir of possibleDirs) {
|
|
817
|
-
const fullPath =
|
|
1068
|
+
const fullPath = path5.join(dir, fallbackName);
|
|
818
1069
|
if (fs3.existsSync(fullPath)) {
|
|
819
1070
|
return fullPath;
|
|
820
1071
|
}
|
|
821
1072
|
}
|
|
822
|
-
return
|
|
1073
|
+
return path5.join(possibleDirs[0], binaryName);
|
|
823
1074
|
}
|
|
824
1075
|
async executeBinary(args) {
|
|
825
1076
|
const escapedArgs = args.map((arg) => {
|
|
@@ -921,7 +1172,7 @@ var SqliteExecutor = class {
|
|
|
921
1172
|
// Para compatibilidad con better-sqlite3 API sincrona usando deasync si es necesario
|
|
922
1173
|
prepareSync(sql) {
|
|
923
1174
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
924
|
-
const require2 =
|
|
1175
|
+
const require2 = createRequire3(requireUrl);
|
|
925
1176
|
const deasync = require2("deasync");
|
|
926
1177
|
return {
|
|
927
1178
|
all: (...params) => {
|
|
@@ -969,9 +1220,9 @@ var SqliteExecutor = class {
|
|
|
969
1220
|
};
|
|
970
1221
|
|
|
971
1222
|
// src/lib/DbConfig.ts
|
|
972
|
-
import * as
|
|
1223
|
+
import * as path6 from "path";
|
|
973
1224
|
import fs4 from "fs";
|
|
974
|
-
var rootPath =
|
|
1225
|
+
var rootPath = path6.resolve(process.cwd(), ".dbcube");
|
|
975
1226
|
var SQLite = class {
|
|
976
1227
|
executor = null;
|
|
977
1228
|
database;
|
|
@@ -981,7 +1232,7 @@ var SQLite = class {
|
|
|
981
1232
|
async ifExist() {
|
|
982
1233
|
if (this.database) {
|
|
983
1234
|
const dbPath = this.database || ":memory:";
|
|
984
|
-
const configPath =
|
|
1235
|
+
const configPath = path6.join(rootPath, dbPath + ".db");
|
|
985
1236
|
if (!fs4.existsSync(rootPath)) {
|
|
986
1237
|
fs4.mkdirSync(rootPath, { recursive: true });
|
|
987
1238
|
}
|
|
@@ -1000,7 +1251,7 @@ var SQLite = class {
|
|
|
1000
1251
|
try {
|
|
1001
1252
|
if (!this.executor) {
|
|
1002
1253
|
const dbPath = this.database || ":memory:";
|
|
1003
|
-
const configPath =
|
|
1254
|
+
const configPath = path6.join(rootPath, dbPath + ".db");
|
|
1004
1255
|
if (!fs4.existsSync(rootPath)) {
|
|
1005
1256
|
fs4.mkdirSync(rootPath, { recursive: true });
|
|
1006
1257
|
}
|
|
@@ -1185,7 +1436,7 @@ var DbConfig_default = DbConfig;
|
|
|
1185
1436
|
|
|
1186
1437
|
// src/lib/FileLogger.ts
|
|
1187
1438
|
import * as fs5 from "fs";
|
|
1188
|
-
import * as
|
|
1439
|
+
import * as path7 from "path";
|
|
1189
1440
|
import { EventEmitter } from "events";
|
|
1190
1441
|
var FileLogger = class _FileLogger extends EventEmitter {
|
|
1191
1442
|
static watchers = /* @__PURE__ */ new Map();
|
|
@@ -1200,7 +1451,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
1200
1451
|
*/
|
|
1201
1452
|
static async write(filePath, message, level = "INFO", append = true) {
|
|
1202
1453
|
try {
|
|
1203
|
-
const dir =
|
|
1454
|
+
const dir = path7.dirname(filePath);
|
|
1204
1455
|
if (!fs5.existsSync(dir)) {
|
|
1205
1456
|
fs5.mkdirSync(dir, { recursive: true });
|
|
1206
1457
|
}
|
|
@@ -1239,7 +1490,7 @@ var FileLogger = class _FileLogger extends EventEmitter {
|
|
|
1239
1490
|
const buffer = _FileLogger.buffers.get(filePath);
|
|
1240
1491
|
if (buffer && buffer.length > 0) {
|
|
1241
1492
|
try {
|
|
1242
|
-
const dir =
|
|
1493
|
+
const dir = path7.dirname(filePath);
|
|
1243
1494
|
if (!fs5.existsSync(dir)) {
|
|
1244
1495
|
fs5.mkdirSync(dir, { recursive: true });
|
|
1245
1496
|
}
|
|
@@ -1962,6 +2213,7 @@ export {
|
|
|
1962
2213
|
DbConfig,
|
|
1963
2214
|
Engine,
|
|
1964
2215
|
FileLogger,
|
|
2216
|
+
QueryEngine,
|
|
1965
2217
|
TableProcessor,
|
|
1966
2218
|
TriggerProcessor
|
|
1967
2219
|
};
|