com.jimuwd.xian.registry-proxy 1.0.75 → 1.0.77
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 +10 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -199,9 +199,8 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
resToDownstreamClient.writeHead(upstreamResponse.status, { "content-type": contentType }).end(JSON.stringify(data));
|
|
202
|
-
reqFromDownstreamClient.destroy();
|
|
203
202
|
}
|
|
204
|
-
else {
|
|
203
|
+
else if (contentType.includes('application/octet-stream')) {
|
|
205
204
|
// 二进制流处理
|
|
206
205
|
if (!upstreamResponse.body) {
|
|
207
206
|
logger.error(`Empty response body from upstream ${targetUrl}`);
|
|
@@ -210,22 +209,19 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
210
209
|
else {
|
|
211
210
|
// write back to client
|
|
212
211
|
resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
|
|
213
|
-
//
|
|
212
|
+
// stop pipe when req from client is closed accidentally.
|
|
214
213
|
const cleanup = () => {
|
|
215
214
|
reqFromDownstreamClient.off('close', cleanup);
|
|
216
|
-
logger.
|
|
215
|
+
logger.info(`Req from downstream client is closed, stop pipe from upstream ${targetUrl} to downstream client.`);
|
|
217
216
|
upstreamResponse.body?.unpipe();
|
|
218
|
-
resToDownstreamClient.destroy();
|
|
219
|
-
reqFromDownstreamClient.destroy();
|
|
220
217
|
};
|
|
221
218
|
reqFromDownstreamClient.on('close', cleanup);
|
|
219
|
+
reqFromDownstreamClient.on('end', () => logger.info("Req from downstream client ends."));
|
|
222
220
|
// clean up when server closes connection to downstream client.
|
|
223
221
|
const cleanup0 = () => {
|
|
224
222
|
resToDownstreamClient.off('close', cleanup0);
|
|
225
223
|
logger.info(`Close connection to downstream client, upstream url is ${targetUrl}`);
|
|
226
|
-
|
|
227
|
-
// resToDownstreamClient.destroy();
|
|
228
|
-
// reqFromDownstreamClient.destroy();
|
|
224
|
+
upstreamResponse.body?.unpipe();
|
|
229
225
|
};
|
|
230
226
|
resToDownstreamClient.on('close', cleanup0);
|
|
231
227
|
// write back body data (chunked probably)
|
|
@@ -237,7 +233,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
237
233
|
logger.info(`Upstream server ${targetUrl} response.body ended.`);
|
|
238
234
|
resToDownstreamClient.end();
|
|
239
235
|
})
|
|
240
|
-
// connection will be closed automatically when all chunk data is transferred.
|
|
236
|
+
// connection will be closed automatically when all chunk data is transferred (after stream ends).
|
|
241
237
|
.on('close', () => {
|
|
242
238
|
logger.info(`Upstream server ${targetUrl} closed connection.`);
|
|
243
239
|
})
|
|
@@ -249,6 +245,10 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
249
245
|
});
|
|
250
246
|
}
|
|
251
247
|
}
|
|
248
|
+
else {
|
|
249
|
+
logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
|
|
250
|
+
resToDownstreamClient.writeHead(upstreamResponse.status, { "content-type": contentType }).end(upstreamResponse.text());
|
|
251
|
+
}
|
|
252
252
|
}
|
|
253
253
|
catch (err) {
|
|
254
254
|
logger.error('Failed to write upstreamResponse:', err);
|