com.jimuwd.xian.registry-proxy 1.0.119 → 1.0.121

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,7 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  import { Server as HttpServer } from 'node:http';
3
3
  import { Server as HttpsServer } from 'node:https';
4
- export declare function startProxyServer(proxyConfigPath?: string, localYarnConfigPath?: string, globalYarnConfigPath?: string, port?: number): Promise<{
5
- serverIpv6: HttpServer | HttpsServer;
6
- serverIpv4: HttpServer | HttpsServer;
7
- }>;
4
+ export declare function startProxyServer(proxyConfigPath?: string, localYarnConfigPath?: string, globalYarnConfigPath?: string, port?: number): Promise<HttpServer | HttpsServer>;
package/dist/index.js CHANGED
@@ -307,8 +307,6 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
307
307
  logger.info('Active registries:', registryInfos.map(r => r.normalizedRegistryUrl));
308
308
  logger.info('Proxy base path:', basePathPrefixedWithSlash);
309
309
  logger.info('HTTPS:', !!proxyInfo.https);
310
- // the real port server is listening on if configured port is empty or 0
311
- let realPort = port;
312
310
  const requestHandler = async (reqFromDownstreamClient, resToDownstreamClient) => {
313
311
  const downstreamUserAgent = reqFromDownstreamClient.headers["user-agent"]; // "curl/x.x.x"
314
312
  const downstreamIp = getDownstreamClientIp(reqFromDownstreamClient);
@@ -354,15 +352,14 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
354
352
  }
355
353
  // 统一回写响应
356
354
  if (successfulResponseFromUpstream) {
357
- await writeResponseToDownstreamClient(targetRegistry, targetUrl, resToDownstreamClient, successfulResponseFromUpstream, reqFromDownstreamClient, proxyInfo, realPort, registryInfos);
355
+ await writeResponseToDownstreamClient(targetRegistry, targetUrl, resToDownstreamClient, successfulResponseFromUpstream, reqFromDownstreamClient, proxyInfo, port, registryInfos);
358
356
  }
359
357
  else {
360
358
  resToDownstreamClient.writeHead(404).end('All upstream registries failed');
361
359
  }
362
360
  };
363
- // 同时启动ipv6,ipv4监听,比如当客户端访问http://localhost:port时,无论客户端DNS解析到IPV4-127.0.0.1还是IPV6-::1地址,咱server都能轻松应对!
364
- let serverIpv6;
365
- let serverIpv4;
361
+ // 注意:需要同时启动ipv6,ipv4监听,比如当客户端访问http://localhost:port时,无论客户端DNS解析到IPV4-127.0.0.1还是IPV6-::1地址,咱server都能轻松应对!
362
+ let server;
366
363
  if (proxyInfo.https) {
367
364
  const { key, cert } = proxyInfo.https;
368
365
  const keyPath = resolvePath(key);
@@ -379,28 +376,24 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
379
376
  key: readFileSync(keyPath),
380
377
  cert: readFileSync(certPath),
381
378
  };
382
- serverIpv6 = createHttpsServer(httpsOptions, requestHandler);
383
- serverIpv4 = createHttpsServer(httpsOptions, requestHandler);
379
+ server = createHttpsServer(httpsOptions, requestHandler);
384
380
  logger.info("Proxy server's maxSockets is", https.globalAgent.maxSockets);
385
381
  }
386
382
  else {
387
- serverIpv6 = createServer(requestHandler);
388
- serverIpv4 = createServer(requestHandler);
383
+ server = createServer(requestHandler);
389
384
  logger.info("Proxy server's maxSockets is", http.globalAgent.maxSockets);
390
385
  }
391
386
  // server参数暂时写死
392
387
  const serverMaxConnections = 10000;
393
388
  const serverTimeoutMs = 60000;
394
- logger.info(`Proxy server's initial maxConnections is ${serverIpv4.maxConnections}, adjusting to ${serverMaxConnections}`);
395
- serverIpv6.maxConnections = serverMaxConnections;
396
- serverIpv4.maxConnections = serverMaxConnections;
397
- logger.info(`Proxy server's initial timeout is ${serverIpv4.timeout}ms, adjusting to ${serverTimeoutMs}ms`);
398
- serverIpv6.timeout = serverTimeoutMs;
399
- serverIpv4.timeout = serverTimeoutMs;
389
+ logger.info(`Proxy server's initial maxConnections is ${server.maxConnections}, adjusting to ${serverMaxConnections}`);
390
+ server.maxConnections = serverMaxConnections;
391
+ logger.info(`Proxy server's initial timeout is ${server.timeout}ms, adjusting to ${serverTimeoutMs}ms`);
392
+ server.timeout = serverTimeoutMs;
400
393
  const promisedServer = new Promise((resolve, reject) => {
401
394
  const errHandler = (err) => {
402
395
  if (err.code === 'EADDRINUSE') {
403
- logger.error(`Port ${realPort} is in use, please specify a different port or free it.`, err);
396
+ logger.error(`Port ${port} is in use, please specify a different port or free it.`, err);
404
397
  process.exit(1);
405
398
  }
406
399
  logger.error('Server error:', err);
@@ -411,21 +404,17 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
411
404
  socket.setTimeout(60000);
412
405
  socket.setKeepAlive(true, 30000);
413
406
  };
414
- serverIpv6.on('error', errHandler /*this handler will call 'reject'*/);
415
- serverIpv4.on('error', errHandler /*this handler will call 'reject'*/);
416
- serverIpv6.on('connection', connectionHandler);
417
- serverIpv4.on('connection', connectionHandler);
418
- // 为了代理服务器的健壮性,先启动ipv6监听,然后再在其回调函数中启动ipv4监听
419
- const listenOptions = { port, host: '*', ipv6Only: false };
420
- serverIpv6.listen(listenOptions, () => {
421
- const addressInfo = serverIpv6.address();
422
- // 回写上层局部变量
423
- realPort = addressInfo.port;
424
- //logger.info(`前面已经监听了ipv6端口 ${addressInfo.address} ${addressInfo.port},追加监听Ipv4同端口号 0.0.0.0:${addressInfo.port}`);
407
+ server.on('error', errHandler /*this handler will call 'reject'*/);
408
+ server.on('connection', connectionHandler);
409
+ // 为了代理服务器的健壮性,要同时监听ipv4、v6地址
410
+ const listenOptions = { port, ipv6Only: false };
411
+ server.listen(listenOptions, () => {
412
+ const addressInfo = server.address();
413
+ port = addressInfo.port; // 回写上层局部变量
425
414
  const portFile = join(process.env.PROJECT_ROOT || process.cwd(), '.registry-proxy-port');
426
415
  writeFile(portFile, addressInfo.port.toString()).catch(e => logger.error(`Failed to write port file: ${portFile}`, e));
427
416
  logger.info(`Proxy server running on ${proxyInfo.https ? 'https' : 'http'}://localhost:${addressInfo.port}${basePathPrefixedWithSlash === '/' ? '' : basePathPrefixedWithSlash}`);
428
- resolve({ serverIpv6, serverIpv4, });
417
+ resolve(server);
429
418
  });
430
419
  });
431
420
  return promisedServer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.119",
3
+ "version": "1.0.121",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",