@dbcube/core 4.0.1 → 4.0.3

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.js CHANGED
@@ -1069,68 +1069,98 @@ var QueryEngine = class {
1069
1069
  }
1070
1070
  async createPersistentConnection(args) {
1071
1071
  return new Promise((resolve5, reject) => {
1072
- const client = new net.Socket();
1072
+ let client = globalTcpConnections.get(this.connectionId);
1073
+ let isNewConnection = false;
1074
+ if (!client || client.destroyed) {
1075
+ client = new net.Socket();
1076
+ isNewConnection = true;
1077
+ client.setNoDelay(true);
1078
+ client.setKeepAlive(true, 6e4);
1079
+ client.setMaxListeners(20);
1080
+ }
1073
1081
  let responseBuffer = "";
1074
1082
  let isResolved = false;
1075
- client.setNoDelay(true);
1076
- client.setKeepAlive(true, 6e4);
1077
1083
  const timeout = setTimeout(() => {
1078
1084
  if (!isResolved) {
1079
1085
  isResolved = true;
1080
- client.destroy();
1081
1086
  reject(new Error("TCP connection timeout"));
1082
1087
  }
1083
1088
  }, 5e3);
1084
- client.connect(this.tcpPort, "127.0.0.1", () => {
1085
- clearTimeout(timeout);
1089
+ const executeQuery = () => {
1086
1090
  const dmlIndex = args.findIndex((arg) => arg === "--dml");
1087
1091
  const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1088
1092
  if (!dmlJson) {
1093
+ clearTimeout(timeout);
1089
1094
  if (!isResolved) {
1090
1095
  isResolved = true;
1091
- client.destroy();
1092
1096
  reject(new Error("No DML found in arguments"));
1093
1097
  }
1094
1098
  return;
1095
1099
  }
1096
- globalTcpConnections.set(this.connectionId, client);
1097
1100
  const command = {
1098
1101
  action: "execute",
1099
1102
  dml: dmlJson
1100
1103
  };
1101
- console.log(dmlJson);
1102
- client.write(JSON.stringify(command));
1103
- });
1104
- client.on("data", (data) => {
1104
+ const commandStr = JSON.stringify(command);
1105
+ const canWrite = client.write(commandStr);
1106
+ if (!canWrite) {
1107
+ client.once("drain", () => {
1108
+ });
1109
+ }
1110
+ };
1111
+ const dataHandler = (data) => {
1105
1112
  responseBuffer += data.toString();
1106
1113
  const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1107
1114
  if (match && !isResolved) {
1108
1115
  isResolved = true;
1116
+ clearTimeout(timeout);
1109
1117
  try {
1110
1118
  const response = JSON.parse(match[1]);
1119
+ responseBuffer = "";
1120
+ client.removeListener("data", dataHandler);
1111
1121
  resolve5({
1112
1122
  status: response.status,
1113
1123
  message: response.message,
1114
1124
  data: response.data
1115
1125
  });
1116
1126
  } catch (error) {
1127
+ client.removeListener("data", dataHandler);
1117
1128
  reject(new Error("Failed to parse TCP response"));
1118
1129
  }
1119
1130
  }
1120
- });
1121
- client.on("error", (error) => {
1131
+ };
1132
+ const errorHandler = (error) => {
1122
1133
  if (!isResolved) {
1123
1134
  isResolved = true;
1135
+ clearTimeout(timeout);
1124
1136
  globalTcpConnections.delete(this.connectionId);
1137
+ client.removeListener("data", dataHandler);
1125
1138
  reject(error);
1126
1139
  }
1127
- });
1128
- client.on("close", () => {
1140
+ };
1141
+ const closeHandler = () => {
1129
1142
  globalTcpConnections.delete(this.connectionId);
1130
- if (!isResolved && !responseBuffer.includes("PROCESS_RESPONSE:")) {
1131
- reject(new Error("Connection closed without valid response"));
1143
+ if (!isResolved) {
1144
+ isResolved = true;
1145
+ clearTimeout(timeout);
1146
+ client.removeListener("data", dataHandler);
1147
+ reject(new Error("Connection closed unexpectedly"));
1132
1148
  }
1133
- });
1149
+ };
1150
+ if (isNewConnection) {
1151
+ client.connect(this.tcpPort, "127.0.0.1", () => {
1152
+ clearTimeout(timeout);
1153
+ globalTcpConnections.set(this.connectionId, client);
1154
+ client.on("error", errorHandler);
1155
+ client.on("close", closeHandler);
1156
+ client.on("data", dataHandler);
1157
+ executeQuery();
1158
+ });
1159
+ } else {
1160
+ clearTimeout(timeout);
1161
+ client.on("data", dataHandler);
1162
+ executeQuery();
1163
+ }
1134
1164
  });
1135
1165
  }
1136
1166
  async createProcess(binary, args) {