@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.cjs CHANGED
@@ -767,6 +767,7 @@ var import_module2 = require("module");
767
767
  var net = __toESM(require("net"));
768
768
  var import_child_process2 = require("child_process");
769
769
  var globalTcpServers = /* @__PURE__ */ new Map();
770
+ var globalTcpConnections = /* @__PURE__ */ new Map();
770
771
  var QueryEngine = class {
771
772
  name;
772
773
  config;
@@ -885,10 +886,10 @@ var QueryEngine = class {
885
886
  if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
886
887
  console.log("\u{1F680} Starting TCP server for ultra-fast queries...");
887
888
  await this.startTcpServer();
889
+ await this.waitForServerReady();
888
890
  }
889
- await this.waitForServerReady();
890
891
  console.log("\u26A1 Using TCP server (ultra-fast)");
891
- return this.sendTcpRequest(args);
892
+ return this.sendTcpRequestFast(args);
892
893
  }
893
894
  async waitForServerReady() {
894
895
  const maxRetries = 10;
@@ -1036,6 +1037,137 @@ var QueryEngine = class {
1036
1037
  });
1037
1038
  });
1038
1039
  }
1040
+ async sendTcpRequestFast(args) {
1041
+ const existingConnection = globalTcpConnections.get(this.connectionId);
1042
+ if (existingConnection && existingConnection.readyState === "open") {
1043
+ try {
1044
+ return await this.sendOnExistingConnection(existingConnection, args);
1045
+ } catch (error) {
1046
+ globalTcpConnections.delete(this.connectionId);
1047
+ existingConnection.destroy();
1048
+ }
1049
+ }
1050
+ return await this.createPersistentConnection(args);
1051
+ }
1052
+ async sendOnExistingConnection(connection, args) {
1053
+ return new Promise((resolve5, reject) => {
1054
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
1055
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1056
+ if (!dmlJson) {
1057
+ reject(new Error("No DML found in arguments"));
1058
+ return;
1059
+ }
1060
+ let responseBuffer = "";
1061
+ let isResolved = false;
1062
+ const timeout = setTimeout(() => {
1063
+ if (!isResolved) {
1064
+ isResolved = true;
1065
+ reject(new Error("TCP request timeout"));
1066
+ }
1067
+ }, 5e3);
1068
+ const onData = (data) => {
1069
+ responseBuffer += data.toString();
1070
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1071
+ if (match && !isResolved) {
1072
+ isResolved = true;
1073
+ clearTimeout(timeout);
1074
+ connection.off("data", onData);
1075
+ connection.off("error", onError);
1076
+ try {
1077
+ const response = JSON.parse(match[1]);
1078
+ resolve5({
1079
+ status: response.status,
1080
+ message: response.message,
1081
+ data: response.data
1082
+ });
1083
+ } catch (error) {
1084
+ reject(new Error("Failed to parse TCP response"));
1085
+ }
1086
+ }
1087
+ };
1088
+ const onError = (error) => {
1089
+ if (!isResolved) {
1090
+ isResolved = true;
1091
+ clearTimeout(timeout);
1092
+ connection.off("data", onData);
1093
+ connection.off("error", onError);
1094
+ reject(error);
1095
+ }
1096
+ };
1097
+ connection.on("data", onData);
1098
+ connection.on("error", onError);
1099
+ const command = {
1100
+ action: "execute",
1101
+ dml: dmlJson
1102
+ };
1103
+ connection.write(JSON.stringify(command));
1104
+ });
1105
+ }
1106
+ async createPersistentConnection(args) {
1107
+ return new Promise((resolve5, reject) => {
1108
+ const client = new net.Socket();
1109
+ let responseBuffer = "";
1110
+ let isResolved = false;
1111
+ client.setNoDelay(true);
1112
+ client.setKeepAlive(true, 6e4);
1113
+ const timeout = setTimeout(() => {
1114
+ if (!isResolved) {
1115
+ isResolved = true;
1116
+ client.destroy();
1117
+ reject(new Error("TCP connection timeout"));
1118
+ }
1119
+ }, 5e3);
1120
+ client.connect(this.tcpPort, "127.0.0.1", () => {
1121
+ clearTimeout(timeout);
1122
+ const dmlIndex = args.findIndex((arg) => arg === "--dml");
1123
+ const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1124
+ if (!dmlJson) {
1125
+ if (!isResolved) {
1126
+ isResolved = true;
1127
+ client.destroy();
1128
+ reject(new Error("No DML found in arguments"));
1129
+ }
1130
+ return;
1131
+ }
1132
+ globalTcpConnections.set(this.connectionId, client);
1133
+ const command = {
1134
+ action: "execute",
1135
+ dml: dmlJson
1136
+ };
1137
+ client.write(JSON.stringify(command));
1138
+ });
1139
+ client.on("data", (data) => {
1140
+ responseBuffer += data.toString();
1141
+ const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1142
+ if (match && !isResolved) {
1143
+ isResolved = true;
1144
+ try {
1145
+ const response = JSON.parse(match[1]);
1146
+ resolve5({
1147
+ status: response.status,
1148
+ message: response.message,
1149
+ data: response.data
1150
+ });
1151
+ } catch (error) {
1152
+ reject(new Error("Failed to parse TCP response"));
1153
+ }
1154
+ }
1155
+ });
1156
+ client.on("error", (error) => {
1157
+ if (!isResolved) {
1158
+ isResolved = true;
1159
+ globalTcpConnections.delete(this.connectionId);
1160
+ reject(error);
1161
+ }
1162
+ });
1163
+ client.on("close", () => {
1164
+ globalTcpConnections.delete(this.connectionId);
1165
+ if (!isResolved && !responseBuffer.includes("PROCESS_RESPONSE:")) {
1166
+ reject(new Error("Connection closed without valid response"));
1167
+ }
1168
+ });
1169
+ });
1170
+ }
1039
1171
  async createProcess(binary, args) {
1040
1172
  await this.initializeBinary();
1041
1173
  if (!this.binary) {
@@ -1124,6 +1256,12 @@ var QueryEngine = class {
1124
1256
  });
1125
1257
  }
1126
1258
  async disconnect() {
1259
+ const connection = globalTcpConnections.get(this.connectionId);
1260
+ if (connection && connection.readyState === "open") {
1261
+ connection.write(JSON.stringify({ action: "disconnect" }));
1262
+ connection.destroy();
1263
+ globalTcpConnections.delete(this.connectionId);
1264
+ }
1127
1265
  const serverInfo = globalTcpServers.get(this.connectionId);
1128
1266
  if (serverInfo && serverInfo.process && !serverInfo.process.killed) {
1129
1267
  console.log("\u{1F50C} Stopping TCP server...");
@@ -1131,7 +1269,7 @@ var QueryEngine = class {
1131
1269
  globalTcpServers.delete(this.connectionId);
1132
1270
  return {
1133
1271
  status: 200,
1134
- message: "TCP server stopped",
1272
+ message: "TCP server and connections stopped",
1135
1273
  data: null
1136
1274
  };
1137
1275
  }