@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.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,16 +936,24 @@ 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", () => {
887
948
  const dmlIndex = args.findIndex((arg) => arg === "--dml");
888
949
  const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
889
950
  if (!dmlJson) {
890
- client.destroy();
891
- reject(new Error("No DML found in arguments"));
951
+ if (!isResolved) {
952
+ isResolved = true;
953
+ clearTimeout(timeout);
954
+ client.destroy();
955
+ reject(new Error("No DML found in arguments"));
956
+ }
892
957
  return;
893
958
  }
894
959
  const command = {
@@ -900,7 +965,8 @@ var QueryEngine = class {
900
965
  client.on("data", (data) => {
901
966
  responseBuffer += data.toString();
902
967
  const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
903
- if (match) {
968
+ if (match && !isResolved) {
969
+ isResolved = true;
904
970
  clearTimeout(timeout);
905
971
  client.destroy();
906
972
  try {
@@ -916,13 +982,18 @@ var QueryEngine = class {
916
982
  }
917
983
  });
918
984
  client.on("error", (error) => {
919
- clearTimeout(timeout);
920
- reject(error);
985
+ if (!isResolved) {
986
+ isResolved = true;
987
+ clearTimeout(timeout);
988
+ reject(error);
989
+ }
921
990
  });
922
991
  client.on("close", () => {
923
- clearTimeout(timeout);
924
- if (responseBuffer && !responseBuffer.includes("\n")) {
925
- 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
+ }
926
997
  }
927
998
  });
928
999
  });