@dbcube/core 3.0.5 → 3.0.7

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.d.mts CHANGED
@@ -32,6 +32,8 @@ declare class QueryEngine {
32
32
  private tcpPort;
33
33
  constructor(name: string, timeout?: number);
34
34
  private generatePort;
35
+ private findAvailablePort;
36
+ private isPortAvailable;
35
37
  initializeBinary(): Promise<void>;
36
38
  setArguments(): any[];
37
39
  setConfig(name: string): {
@@ -40,6 +42,9 @@ declare class QueryEngine {
40
42
  getConfig(): any;
41
43
  run(binary: string, args: string[]): Promise<ResponseEngine>;
42
44
  private executeWithTcpServer;
45
+ private waitForServerReady;
46
+ private isServerResponding;
47
+ private sleep;
43
48
  private startTcpServer;
44
49
  private sendTcpRequest;
45
50
  private createProcess;
package/dist/index.d.ts CHANGED
@@ -32,6 +32,8 @@ declare class QueryEngine {
32
32
  private tcpPort;
33
33
  constructor(name: string, timeout?: number);
34
34
  private generatePort;
35
+ private findAvailablePort;
36
+ private isPortAvailable;
35
37
  initializeBinary(): Promise<void>;
36
38
  setArguments(): any[];
37
39
  setConfig(name: string): {
@@ -40,6 +42,9 @@ declare class QueryEngine {
40
42
  getConfig(): any;
41
43
  run(binary: string, args: string[]): Promise<ResponseEngine>;
42
44
  private executeWithTcpServer;
45
+ private waitForServerReady;
46
+ private isServerResponding;
47
+ private sleep;
43
48
  private startTcpServer;
44
49
  private sendTcpRequest;
45
50
  private createProcess;
package/dist/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/lib/Engine.ts
2
9
  import path3 from "path";
3
10
 
@@ -739,13 +746,27 @@ var QueryEngine = class {
739
746
  this.tcpPort = this.generatePort();
740
747
  }
741
748
  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;
749
+ return 9944;
750
+ }
751
+ async findAvailablePort(startPort) {
752
+ for (let port = startPort; port >= 9900; port--) {
753
+ if (await this.isPortAvailable(port)) {
754
+ return port;
755
+ }
747
756
  }
748
- return 8100 + Math.abs(hash % 900);
757
+ throw new Error("No available ports found in range 9900-9944");
758
+ }
759
+ isPortAvailable(port) {
760
+ return new Promise((resolve5) => {
761
+ const net2 = __require("net");
762
+ const tester = net2.createServer();
763
+ tester.once("error", () => resolve5(false));
764
+ tester.once("listening", () => {
765
+ tester.close();
766
+ resolve5(true);
767
+ });
768
+ tester.listen(port, "127.0.0.1");
769
+ });
749
770
  }
750
771
  async initializeBinary() {
751
772
  if (!this.binary) {
@@ -827,14 +848,50 @@ var QueryEngine = class {
827
848
  console.log("\u{1F680} Starting TCP server for ultra-fast queries...");
828
849
  await this.startTcpServer();
829
850
  }
851
+ await this.waitForServerReady();
830
852
  console.log("\u26A1 Using TCP server (ultra-fast)");
831
853
  return this.sendTcpRequest(args);
832
854
  }
855
+ async waitForServerReady() {
856
+ const maxRetries = 10;
857
+ const retryDelay = 500;
858
+ for (let i = 0; i < maxRetries; i++) {
859
+ if (await this.isServerResponding()) {
860
+ return;
861
+ }
862
+ console.log(`\u23F3 Waiting for TCP server to be ready... (${i + 1}/${maxRetries})`);
863
+ await this.sleep(retryDelay);
864
+ }
865
+ throw new Error("TCP server failed to become ready within timeout");
866
+ }
867
+ async isServerResponding() {
868
+ return new Promise((resolve5) => {
869
+ const client = new net.Socket();
870
+ const timeout = setTimeout(() => {
871
+ client.destroy();
872
+ resolve5(false);
873
+ }, 1e3);
874
+ client.connect(this.tcpPort, "127.0.0.1", () => {
875
+ clearTimeout(timeout);
876
+ client.destroy();
877
+ resolve5(true);
878
+ });
879
+ client.on("error", () => {
880
+ clearTimeout(timeout);
881
+ resolve5(false);
882
+ });
883
+ });
884
+ }
885
+ sleep(ms) {
886
+ return new Promise((resolve5) => setTimeout(resolve5, ms));
887
+ }
833
888
  async startTcpServer() {
834
889
  await this.initializeBinary();
835
890
  if (!this.binary) {
836
891
  throw new Error("Binary not initialized");
837
892
  }
893
+ this.tcpPort = await this.findAvailablePort(this.tcpPort);
894
+ console.log(`\u{1F310} Using port ${this.tcpPort} for TCP server`);
838
895
  return new Promise((resolve5, reject) => {
839
896
  const serverArgs = [...this.arguments, "--action", "server", "--port", this.tcpPort.toString()];
840
897
  const serverProcess = spawn2(this.binary["query_engine"], serverArgs);
@@ -844,7 +901,7 @@ var QueryEngine = class {
844
901
  serverProcess.kill();
845
902
  reject(new Error("TCP server startup timeout"));
846
903
  }
847
- }, 1e4);
904
+ }, 15e3);
848
905
  serverProcess.stdout.on("data", (data) => {
849
906
  const output = data.toString();
850
907
  console.log(`[TCP Server] ${output.trim()}`);
@@ -879,27 +936,44 @@ var QueryEngine = class {
879
936
  return new Promise((resolve5, reject) => {
880
937
  const client = new net.Socket();
881
938
  let responseBuffer = "";
939
+ let isResolved = false;
882
940
  const timeout = setTimeout(() => {
883
- client.destroy();
884
- reject(new Error("TCP request timeout"));
885
- }, this.timeout);
941
+ if (!isResolved) {
942
+ isResolved = true;
943
+ client.destroy();
944
+ reject(new Error("TCP request timeout"));
945
+ }
946
+ }, this.timeout * 2);
886
947
  client.connect(this.tcpPort, "127.0.0.1", () => {
948
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
949
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
950
+ if (!dmlJson) {
951
+ if (!isResolved) {
952
+ isResolved = true;
953
+ clearTimeout(timeout);
954
+ client.destroy();
955
+ reject(new Error("No DML found in arguments"));
956
+ }
957
+ return;
958
+ }
887
959
  const command = {
888
960
  action: "execute",
889
- args
961
+ dml: dmlJson
890
962
  };
891
- client.write(JSON.stringify(command) + "\n");
963
+ client.write(JSON.stringify(command));
892
964
  });
893
965
  client.on("data", (data) => {
894
966
  responseBuffer += data.toString();
895
- if (responseBuffer.includes("\n")) {
967
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
968
+ if (match && !isResolved) {
969
+ isResolved = true;
896
970
  clearTimeout(timeout);
897
971
  client.destroy();
898
972
  try {
899
- const response = JSON.parse(responseBuffer.trim());
973
+ const response = JSON.parse(match[1]);
900
974
  resolve5({
901
- status: response.status || 200,
902
- message: response.message || "Success",
975
+ status: response.status,
976
+ message: response.message,
903
977
  data: response.data
904
978
  });
905
979
  } catch (error) {
@@ -908,13 +982,18 @@ var QueryEngine = class {
908
982
  }
909
983
  });
910
984
  client.on("error", (error) => {
911
- clearTimeout(timeout);
912
- reject(error);
985
+ if (!isResolved) {
986
+ isResolved = true;
987
+ clearTimeout(timeout);
988
+ reject(error);
989
+ }
913
990
  });
914
991
  client.on("close", () => {
915
- clearTimeout(timeout);
916
- if (responseBuffer && !responseBuffer.includes("\n")) {
917
- reject(new Error("Incomplete TCP response"));
992
+ if (!isResolved) {
993
+ clearTimeout(timeout);
994
+ if (!responseBuffer.includes("PROCESS_RESPONSE:")) {
995
+ reject(new Error("Connection closed without valid response"));
996
+ }
918
997
  }
919
998
  });
920
999
  });