@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.cjs CHANGED
@@ -1114,68 +1114,98 @@ var QueryEngine = class {
1114
1114
  }
1115
1115
  async createPersistentConnection(args) {
1116
1116
  return new Promise((resolve5, reject) => {
1117
- const client = new net.Socket();
1117
+ let client = globalTcpConnections.get(this.connectionId);
1118
+ let isNewConnection = false;
1119
+ if (!client || client.destroyed) {
1120
+ client = new net.Socket();
1121
+ isNewConnection = true;
1122
+ client.setNoDelay(true);
1123
+ client.setKeepAlive(true, 6e4);
1124
+ client.setMaxListeners(20);
1125
+ }
1118
1126
  let responseBuffer = "";
1119
1127
  let isResolved = false;
1120
- client.setNoDelay(true);
1121
- client.setKeepAlive(true, 6e4);
1122
1128
  const timeout = setTimeout(() => {
1123
1129
  if (!isResolved) {
1124
1130
  isResolved = true;
1125
- client.destroy();
1126
1131
  reject(new Error("TCP connection timeout"));
1127
1132
  }
1128
1133
  }, 5e3);
1129
- client.connect(this.tcpPort, "127.0.0.1", () => {
1130
- clearTimeout(timeout);
1134
+ const executeQuery = () => {
1131
1135
  const dmlIndex = args.findIndex((arg) => arg === "--dml");
1132
1136
  const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1133
1137
  if (!dmlJson) {
1138
+ clearTimeout(timeout);
1134
1139
  if (!isResolved) {
1135
1140
  isResolved = true;
1136
- client.destroy();
1137
1141
  reject(new Error("No DML found in arguments"));
1138
1142
  }
1139
1143
  return;
1140
1144
  }
1141
- globalTcpConnections.set(this.connectionId, client);
1142
1145
  const command = {
1143
1146
  action: "execute",
1144
1147
  dml: dmlJson
1145
1148
  };
1146
- console.log(dmlJson);
1147
- client.write(JSON.stringify(command));
1148
- });
1149
- client.on("data", (data) => {
1149
+ const commandStr = JSON.stringify(command);
1150
+ const canWrite = client.write(commandStr);
1151
+ if (!canWrite) {
1152
+ client.once("drain", () => {
1153
+ });
1154
+ }
1155
+ };
1156
+ const dataHandler = (data) => {
1150
1157
  responseBuffer += data.toString();
1151
1158
  const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1152
1159
  if (match && !isResolved) {
1153
1160
  isResolved = true;
1161
+ clearTimeout(timeout);
1154
1162
  try {
1155
1163
  const response = JSON.parse(match[1]);
1164
+ responseBuffer = "";
1165
+ client.removeListener("data", dataHandler);
1156
1166
  resolve5({
1157
1167
  status: response.status,
1158
1168
  message: response.message,
1159
1169
  data: response.data
1160
1170
  });
1161
1171
  } catch (error) {
1172
+ client.removeListener("data", dataHandler);
1162
1173
  reject(new Error("Failed to parse TCP response"));
1163
1174
  }
1164
1175
  }
1165
- });
1166
- client.on("error", (error) => {
1176
+ };
1177
+ const errorHandler = (error) => {
1167
1178
  if (!isResolved) {
1168
1179
  isResolved = true;
1180
+ clearTimeout(timeout);
1169
1181
  globalTcpConnections.delete(this.connectionId);
1182
+ client.removeListener("data", dataHandler);
1170
1183
  reject(error);
1171
1184
  }
1172
- });
1173
- client.on("close", () => {
1185
+ };
1186
+ const closeHandler = () => {
1174
1187
  globalTcpConnections.delete(this.connectionId);
1175
- if (!isResolved && !responseBuffer.includes("PROCESS_RESPONSE:")) {
1176
- reject(new Error("Connection closed without valid response"));
1188
+ if (!isResolved) {
1189
+ isResolved = true;
1190
+ clearTimeout(timeout);
1191
+ client.removeListener("data", dataHandler);
1192
+ reject(new Error("Connection closed unexpectedly"));
1177
1193
  }
1178
- });
1194
+ };
1195
+ if (isNewConnection) {
1196
+ client.connect(this.tcpPort, "127.0.0.1", () => {
1197
+ clearTimeout(timeout);
1198
+ globalTcpConnections.set(this.connectionId, client);
1199
+ client.on("error", errorHandler);
1200
+ client.on("close", closeHandler);
1201
+ client.on("data", dataHandler);
1202
+ executeQuery();
1203
+ });
1204
+ } else {
1205
+ clearTimeout(timeout);
1206
+ client.on("data", dataHandler);
1207
+ executeQuery();
1208
+ }
1179
1209
  });
1180
1210
  }
1181
1211
  async createProcess(binary, args) {