@dbcube/core 3.0.7 → 3.0.8

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
@@ -47,6 +47,9 @@ declare class QueryEngine {
47
47
  private sleep;
48
48
  private startTcpServer;
49
49
  private sendTcpRequest;
50
+ private sendTcpRequestFast;
51
+ private sendOnExistingConnection;
52
+ private createPersistentConnection;
50
53
  private createProcess;
51
54
  disconnect(): Promise<ResponseEngine>;
52
55
  }
package/dist/index.d.ts CHANGED
@@ -47,6 +47,9 @@ declare class QueryEngine {
47
47
  private sleep;
48
48
  private startTcpServer;
49
49
  private sendTcpRequest;
50
+ private sendTcpRequestFast;
51
+ private sendOnExistingConnection;
52
+ private createPersistentConnection;
50
53
  private createProcess;
51
54
  disconnect(): Promise<ResponseEngine>;
52
55
  }
package/dist/index.js CHANGED
@@ -729,6 +729,7 @@ import { createRequire as createRequire2 } from "module";
729
729
  import * as net from "net";
730
730
  import { spawn as spawn2 } from "child_process";
731
731
  var globalTcpServers = /* @__PURE__ */ new Map();
732
+ var globalTcpConnections = /* @__PURE__ */ new Map();
732
733
  var QueryEngine = class {
733
734
  name;
734
735
  config;
@@ -847,10 +848,10 @@ var QueryEngine = class {
847
848
  if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
848
849
  console.log("\u{1F680} Starting TCP server for ultra-fast queries...");
849
850
  await this.startTcpServer();
851
+ await this.waitForServerReady();
850
852
  }
851
- await this.waitForServerReady();
852
853
  console.log("\u26A1 Using TCP server (ultra-fast)");
853
- return this.sendTcpRequest(args);
854
+ return this.sendTcpRequestFast(args);
854
855
  }
855
856
  async waitForServerReady() {
856
857
  const maxRetries = 10;
@@ -998,6 +999,137 @@ var QueryEngine = class {
998
999
  });
999
1000
  });
1000
1001
  }
1002
+ async sendTcpRequestFast(args) {
1003
+ const existingConnection = globalTcpConnections.get(this.connectionId);
1004
+ if (existingConnection && existingConnection.readyState === "open") {
1005
+ try {
1006
+ return await this.sendOnExistingConnection(existingConnection, args);
1007
+ } catch (error) {
1008
+ globalTcpConnections.delete(this.connectionId);
1009
+ existingConnection.destroy();
1010
+ }
1011
+ }
1012
+ return await this.createPersistentConnection(args);
1013
+ }
1014
+ async sendOnExistingConnection(connection, args) {
1015
+ return new Promise((resolve5, reject) => {
1016
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
1017
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1018
+ if (!dmlJson) {
1019
+ reject(new Error("No DML found in arguments"));
1020
+ return;
1021
+ }
1022
+ let responseBuffer = "";
1023
+ let isResolved = false;
1024
+ const timeout = setTimeout(() => {
1025
+ if (!isResolved) {
1026
+ isResolved = true;
1027
+ reject(new Error("TCP request timeout"));
1028
+ }
1029
+ }, 5e3);
1030
+ const onData = (data) => {
1031
+ responseBuffer += data.toString();
1032
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1033
+ if (match && !isResolved) {
1034
+ isResolved = true;
1035
+ clearTimeout(timeout);
1036
+ connection.off("data", onData);
1037
+ connection.off("error", onError);
1038
+ try {
1039
+ const response = JSON.parse(match[1]);
1040
+ resolve5({
1041
+ status: response.status,
1042
+ message: response.message,
1043
+ data: response.data
1044
+ });
1045
+ } catch (error) {
1046
+ reject(new Error("Failed to parse TCP response"));
1047
+ }
1048
+ }
1049
+ };
1050
+ const onError = (error) => {
1051
+ if (!isResolved) {
1052
+ isResolved = true;
1053
+ clearTimeout(timeout);
1054
+ connection.off("data", onData);
1055
+ connection.off("error", onError);
1056
+ reject(error);
1057
+ }
1058
+ };
1059
+ connection.on("data", onData);
1060
+ connection.on("error", onError);
1061
+ const command = {
1062
+ action: "execute",
1063
+ dml: dmlJson
1064
+ };
1065
+ connection.write(JSON.stringify(command));
1066
+ });
1067
+ }
1068
+ async createPersistentConnection(args) {
1069
+ return new Promise((resolve5, reject) => {
1070
+ const client = new net.Socket();
1071
+ let responseBuffer = "";
1072
+ let isResolved = false;
1073
+ client.setNoDelay(true);
1074
+ client.setKeepAlive(true, 6e4);
1075
+ const timeout = setTimeout(() => {
1076
+ if (!isResolved) {
1077
+ isResolved = true;
1078
+ client.destroy();
1079
+ reject(new Error("TCP connection timeout"));
1080
+ }
1081
+ }, 5e3);
1082
+ client.connect(this.tcpPort, "127.0.0.1", () => {
1083
+ clearTimeout(timeout);
1084
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
1085
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1086
+ if (!dmlJson) {
1087
+ if (!isResolved) {
1088
+ isResolved = true;
1089
+ client.destroy();
1090
+ reject(new Error("No DML found in arguments"));
1091
+ }
1092
+ return;
1093
+ }
1094
+ globalTcpConnections.set(this.connectionId, client);
1095
+ const command = {
1096
+ action: "execute",
1097
+ dml: dmlJson
1098
+ };
1099
+ client.write(JSON.stringify(command));
1100
+ });
1101
+ client.on("data", (data) => {
1102
+ responseBuffer += data.toString();
1103
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1104
+ if (match && !isResolved) {
1105
+ isResolved = true;
1106
+ try {
1107
+ const response = JSON.parse(match[1]);
1108
+ resolve5({
1109
+ status: response.status,
1110
+ message: response.message,
1111
+ data: response.data
1112
+ });
1113
+ } catch (error) {
1114
+ reject(new Error("Failed to parse TCP response"));
1115
+ }
1116
+ }
1117
+ });
1118
+ client.on("error", (error) => {
1119
+ if (!isResolved) {
1120
+ isResolved = true;
1121
+ globalTcpConnections.delete(this.connectionId);
1122
+ reject(error);
1123
+ }
1124
+ });
1125
+ client.on("close", () => {
1126
+ globalTcpConnections.delete(this.connectionId);
1127
+ if (!isResolved && !responseBuffer.includes("PROCESS_RESPONSE:")) {
1128
+ reject(new Error("Connection closed without valid response"));
1129
+ }
1130
+ });
1131
+ });
1132
+ }
1001
1133
  async createProcess(binary, args) {
1002
1134
  await this.initializeBinary();
1003
1135
  if (!this.binary) {
@@ -1086,6 +1218,12 @@ var QueryEngine = class {
1086
1218
  });
1087
1219
  }
1088
1220
  async disconnect() {
1221
+ const connection = globalTcpConnections.get(this.connectionId);
1222
+ if (connection && connection.readyState === "open") {
1223
+ connection.write(JSON.stringify({ action: "disconnect" }));
1224
+ connection.destroy();
1225
+ globalTcpConnections.delete(this.connectionId);
1226
+ }
1089
1227
  const serverInfo = globalTcpServers.get(this.connectionId);
1090
1228
  if (serverInfo && serverInfo.process && !serverInfo.process.killed) {
1091
1229
  console.log("\u{1F50C} Stopping TCP server...");
@@ -1093,7 +1231,7 @@ var QueryEngine = class {
1093
1231
  globalTcpServers.delete(this.connectionId);
1094
1232
  return {
1095
1233
  status: 200,
1096
- message: "TCP server stopped",
1234
+ message: "TCP server and connections stopped",
1097
1235
  data: null
1098
1236
  };
1099
1237
  }