com.jimuwd.xian.registry-proxy 1.0.66 → 1.0.68

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
- import { Server as HttpServer } from 'http';
3
- import { Server as HttpsServer } from 'https';
2
+ import { Server as HttpServer } from 'node:http';
3
+ import { Server as HttpsServer } from 'node:https';
4
4
  export declare function startProxyServer(proxyConfigPath?: string, localYarnConfigPath?: string, globalYarnConfigPath?: string, port?: number): Promise<HttpServer | HttpsServer>;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import { createServer } from 'http';
3
- import { createServer as createHttpsServer } from 'https';
2
+ import http, { createServer } from 'node:http';
3
+ import https, { createServer as createHttpsServer } from 'node:https';
4
4
  import { promises as fsPromises, readFileSync } from 'fs';
5
5
  import { load } from 'js-yaml';
6
6
  import fetch from 'node-fetch';
@@ -34,7 +34,7 @@ class ConcurrencyLimiter {
34
34
  }
35
35
  }
36
36
  }
37
- const limiter = new ConcurrencyLimiter(1000);
37
+ const limiter = new ConcurrencyLimiter(10);
38
38
  function removeEndingSlashAndForceStartingSlash(str) {
39
39
  if (!str)
40
40
  return '/';
@@ -160,7 +160,7 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
160
160
  limiter.release();
161
161
  }
162
162
  }
163
- async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDownstreamClient, upstreamResponse, downstreamReq, proxyInfo, proxyPort, registryInfos) {
163
+ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDownstreamClient, upstreamResponse, reqFromDownstreamClient, proxyInfo, proxyPort, registryInfos) {
164
164
  if (!upstreamResponse.ok)
165
165
  throw new Error("Only 2xx upstream response is supported");
166
166
  try {
@@ -183,7 +183,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
183
183
  // JSON 处理逻辑
184
184
  const data = await upstreamResponse.json();
185
185
  if (data.versions) {
186
- const host = downstreamReq.headers.host || `localhost:${proxyPort}`;
186
+ const host = reqFromDownstreamClient.headers.host || `localhost:${proxyPort}`;
187
187
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
188
188
  for (const versionKey in data.versions) {
189
189
  const packageVersion = data.versions[versionKey];
@@ -206,13 +206,24 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
206
206
  else {
207
207
  // write back to client
208
208
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
209
- // stop transfer if client is closed accidentally.
209
+ // clean up when server if client is closed accidentally.
210
210
  const cleanup = () => {
211
- downstreamReq.off('close', cleanup);
212
- logger.warn(`Downstream client closed accidentally, stop pipe from upstream ${targetUrl} to downstream client.`);
211
+ reqFromDownstreamClient.off('close', cleanup);
212
+ logger.warn(`Downstream client closed connection accidentally, stop pipe from upstream ${targetUrl} to downstream client.`);
213
213
  upstreamResponse.body?.unpipe();
214
+ resToDownstreamClient.destroy();
215
+ reqFromDownstreamClient.destroy();
216
+ };
217
+ reqFromDownstreamClient.on('close', cleanup);
218
+ // clean up when server closes connection to downstream client.
219
+ const cleanup0 = () => {
220
+ resToDownstreamClient.off('close', cleanup0);
221
+ logger.info(`Downstream client closed connection, destroy connection, upstream url is ${targetUrl}`);
222
+ /*upstreamResponse.body?.unpipe(); 代理服务器主动关闭与客户端的连接,不需要此时对接管道已经断开了,没必要重复断开*/
223
+ resToDownstreamClient.destroy();
224
+ reqFromDownstreamClient.destroy();
214
225
  };
215
- downstreamReq.on('close', cleanup);
226
+ resToDownstreamClient.on('close', cleanup0);
216
227
  // write back body data (chunked probably)
217
228
  // pipe upstream body to downstream client
218
229
  upstreamResponse.body.pipe(resToDownstreamClient, { end: false });
@@ -222,16 +233,15 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
222
233
  logger.info(`Upstream server ${targetUrl} response.body ended.`);
223
234
  resToDownstreamClient.end();
224
235
  })
236
+ // connection will be closed automatically when all chunk data is transferred.
225
237
  .on('close', () => {
226
- const errMsg = `Upstream server ${targetUrl} closed connection while transferring data.`;
227
- logger.error(errMsg);
228
- // resToDownstreamClient.destroy(new Error(errMsg));
229
- resToDownstreamClient.destroy();
238
+ logger.info(`Upstream server ${targetUrl} closed connection.`);
230
239
  })
231
240
  .on('error', (err) => {
232
241
  const errMsg = `Stream error: ${err.message}`;
233
242
  logger.error(errMsg);
234
243
  resToDownstreamClient.destroy(new Error(errMsg, { cause: err, }));
244
+ reqFromDownstreamClient.destroy(new Error(errMsg, { cause: err, }));
235
245
  });
236
246
  }
237
247
  }
@@ -306,10 +316,16 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
306
316
  cert: readFileSync(certPath),
307
317
  };
308
318
  server = createHttpsServer(httpsOptions, requestHandler);
319
+ logger.info("Proxy server's maxSockets is", https.globalAgent.maxSockets);
309
320
  }
310
321
  else {
311
322
  server = createServer(requestHandler);
323
+ logger.info("Proxy server's maxSockets is", http.globalAgent.maxSockets);
312
324
  }
325
+ logger.info(`Proxy server's initial maxConnections is ${server.maxConnections}, adjusting to 10000`);
326
+ server.maxConnections = 10000;
327
+ logger.info(`Proxy server's initial timeout is ${server.timeout}, adjusting to 60000ms`);
328
+ server.timeout = 60000;
313
329
  const promisedServer = new Promise((resolve, reject) => {
314
330
  server.on('error', (err) => {
315
331
  if (err.code === 'EADDRINUSE') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.66",
3
+ "version": "1.0.68",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",