com.jimuwd.xian.registry-proxy 1.0.76 → 1.0.78

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.
Files changed (2) hide show
  1. package/dist/index.js +10 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -173,7 +173,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
173
173
  const safeHeaders = { 'connection': connection, 'content-type': contentType, };
174
174
  // 复制所有可能需要的头信息
175
175
  const headersToCopy = [
176
- 'content-length',
176
+ // 'content-length', upstream response body is modified by proxy, the content-length is not valid anymore.
177
177
  'content-encoding',
178
178
  'transfer-encoding',
179
179
  ];
@@ -198,7 +198,10 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
198
198
  }
199
199
  }
200
200
  }
201
- resToDownstreamClient.writeHead(upstreamResponse.status, { "content-type": contentType }).end(JSON.stringify(data));
201
+ const bodyData = JSON.stringify(data);
202
+ // body is modified, reset the content-length
203
+ safeHeaders["content-length"] = bodyData.length;
204
+ resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
202
205
  }
203
206
  else if (contentType.includes('application/octet-stream')) {
204
207
  // 二进制流处理
@@ -209,22 +212,19 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
209
212
  else {
210
213
  // write back to client
211
214
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
212
- // clean up when server if client is closed accidentally.
215
+ // stop pipe when req from client is closed accidentally.
213
216
  const cleanup = () => {
214
217
  reqFromDownstreamClient.off('close', cleanup);
215
- logger.warn(`Downstream client closed connection accidentally, stop pipe from upstream ${targetUrl} to downstream client.`);
218
+ logger.info(`Req from downstream client is closed, stop pipe from upstream ${targetUrl} to downstream client.`);
216
219
  upstreamResponse.body?.unpipe();
217
- resToDownstreamClient.destroy();
218
- reqFromDownstreamClient.destroy();
219
220
  };
220
221
  reqFromDownstreamClient.on('close', cleanup);
222
+ reqFromDownstreamClient.on('end', () => logger.info("Req from downstream client ends."));
221
223
  // clean up when server closes connection to downstream client.
222
224
  const cleanup0 = () => {
223
225
  resToDownstreamClient.off('close', cleanup0);
224
226
  logger.info(`Close connection to downstream client, upstream url is ${targetUrl}`);
225
- /*upstreamResponse.body?.unpipe(); 代理服务器主动关闭与客户端的连接,不需要此时对接管道已经断开了,没必要重复断开*/
226
- // resToDownstreamClient.destroy();
227
- // reqFromDownstreamClient.destroy();
227
+ upstreamResponse.body?.unpipe();
228
228
  };
229
229
  resToDownstreamClient.on('close', cleanup0);
230
230
  // write back body data (chunked probably)
@@ -250,7 +250,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
250
250
  }
251
251
  else {
252
252
  logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
253
- resToDownstreamClient.writeHead(upstreamResponse.status, { "content-type": contentType }).end(upstreamResponse.text());
253
+ resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(upstreamResponse.text());
254
254
  }
255
255
  }
256
256
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.76",
3
+ "version": "1.0.78",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",