@dbcube/core 3.0.6 → 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.cjs CHANGED
@@ -784,13 +784,27 @@ var QueryEngine = class {
784
784
  this.tcpPort = this.generatePort();
785
785
  }
786
786
  generatePort() {
787
- let hash = 0;
788
- for (let i = 0; i < this.connectionId.length; i++) {
789
- const char = this.connectionId.charCodeAt(i);
790
- hash = (hash << 5) - hash + char;
791
- hash = hash & hash;
787
+ return 9944;
788
+ }
789
+ async findAvailablePort(startPort) {
790
+ for (let port = startPort; port >= 9900; port--) {
791
+ if (await this.isPortAvailable(port)) {
792
+ return port;
793
+ }
792
794
  }
793
- return 8100 + Math.abs(hash % 900);
795
+ throw new Error("No available ports found in range 9900-9944");
796
+ }
797
+ isPortAvailable(port) {
798
+ return new Promise((resolve5) => {
799
+ const net2 = require("net");
800
+ const tester = net2.createServer();
801
+ tester.once("error", () => resolve5(false));
802
+ tester.once("listening", () => {
803
+ tester.close();
804
+ resolve5(true);
805
+ });
806
+ tester.listen(port, "127.0.0.1");
807
+ });
794
808
  }
795
809
  async initializeBinary() {
796
810
  if (!this.binary) {
@@ -872,14 +886,50 @@ var QueryEngine = class {
872
886
  console.log("\u{1F680} Starting TCP server for ultra-fast queries...");
873
887
  await this.startTcpServer();
874
888
  }
889
+ await this.waitForServerReady();
875
890
  console.log("\u26A1 Using TCP server (ultra-fast)");
876
891
  return this.sendTcpRequest(args);
877
892
  }
893
+ async waitForServerReady() {
894
+ const maxRetries = 10;
895
+ const retryDelay = 500;
896
+ for (let i = 0; i < maxRetries; i++) {
897
+ if (await this.isServerResponding()) {
898
+ return;
899
+ }
900
+ console.log(`\u23F3 Waiting for TCP server to be ready... (${i + 1}/${maxRetries})`);
901
+ await this.sleep(retryDelay);
902
+ }
903
+ throw new Error("TCP server failed to become ready within timeout");
904
+ }
905
+ async isServerResponding() {
906
+ return new Promise((resolve5) => {
907
+ const client = new net.Socket();
908
+ const timeout = setTimeout(() => {
909
+ client.destroy();
910
+ resolve5(false);
911
+ }, 1e3);
912
+ client.connect(this.tcpPort, "127.0.0.1", () => {
913
+ clearTimeout(timeout);
914
+ client.destroy();
915
+ resolve5(true);
916
+ });
917
+ client.on("error", () => {
918
+ clearTimeout(timeout);
919
+ resolve5(false);
920
+ });
921
+ });
922
+ }
923
+ sleep(ms) {
924
+ return new Promise((resolve5) => setTimeout(resolve5, ms));
925
+ }
878
926
  async startTcpServer() {
879
927
  await this.initializeBinary();
880
928
  if (!this.binary) {
881
929
  throw new Error("Binary not initialized");
882
930
  }
931
+ this.tcpPort = await this.findAvailablePort(this.tcpPort);
932
+ console.log(`\u{1F310} Using port ${this.tcpPort} for TCP server`);
883
933
  return new Promise((resolve5, reject) => {
884
934
  const serverArgs = [...this.arguments, "--action", "server", "--port", this.tcpPort.toString()];
885
935
  const serverProcess = (0, import_child_process2.spawn)(this.binary["query_engine"], serverArgs);
@@ -889,7 +939,7 @@ var QueryEngine = class {
889
939
  serverProcess.kill();
890
940
  reject(new Error("TCP server startup timeout"));
891
941
  }
892
- }, 1e4);
942
+ }, 15e3);
893
943
  serverProcess.stdout.on("data", (data) => {
894
944
  const output = data.toString();
895
945
  console.log(`[TCP Server] ${output.trim()}`);
@@ -924,16 +974,24 @@ var QueryEngine = class {
924
974
  return new Promise((resolve5, reject) => {
925
975
  const client = new net.Socket();
926
976
  let responseBuffer = "";
977
+ let isResolved = false;
927
978
  const timeout = setTimeout(() => {
928
- client.destroy();
929
- reject(new Error("TCP request timeout"));
930
- }, this.timeout);
979
+ if (!isResolved) {
980
+ isResolved = true;
981
+ client.destroy();
982
+ reject(new Error("TCP request timeout"));
983
+ }
984
+ }, this.timeout * 2);
931
985
  client.connect(this.tcpPort, "127.0.0.1", () => {
932
986
  const dmlIndex = args.findIndex((arg) => arg === "--dml");
933
987
  const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
934
988
  if (!dmlJson) {
935
- client.destroy();
936
- reject(new Error("No DML found in arguments"));
989
+ if (!isResolved) {
990
+ isResolved = true;
991
+ clearTimeout(timeout);
992
+ client.destroy();
993
+ reject(new Error("No DML found in arguments"));
994
+ }
937
995
  return;
938
996
  }
939
997
  const command = {
@@ -945,7 +1003,8 @@ var QueryEngine = class {
945
1003
  client.on("data", (data) => {
946
1004
  responseBuffer += data.toString();
947
1005
  const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
948
- if (match) {
1006
+ if (match && !isResolved) {
1007
+ isResolved = true;
949
1008
  clearTimeout(timeout);
950
1009
  client.destroy();
951
1010
  try {
@@ -961,13 +1020,18 @@ var QueryEngine = class {
961
1020
  }
962
1021
  });
963
1022
  client.on("error", (error) => {
964
- clearTimeout(timeout);
965
- reject(error);
1023
+ if (!isResolved) {
1024
+ isResolved = true;
1025
+ clearTimeout(timeout);
1026
+ reject(error);
1027
+ }
966
1028
  });
967
1029
  client.on("close", () => {
968
- clearTimeout(timeout);
969
- if (responseBuffer && !responseBuffer.includes("\n")) {
970
- reject(new Error("Incomplete TCP response"));
1030
+ if (!isResolved) {
1031
+ clearTimeout(timeout);
1032
+ if (!responseBuffer.includes("PROCESS_RESPONSE:")) {
1033
+ reject(new Error("Connection closed without valid response"));
1034
+ }
971
1035
  }
972
1036
  });
973
1037
  });