com.jimuwd.xian.registry-proxy 1.0.83 → 1.0.85

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 +18 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -138,13 +138,16 @@ async function loadProxyInfo(proxyConfigPath = './.registry-proxy.yml', localYar
138
138
  const basePath = removeEndingSlashAndForceStartingSlash(proxyConfig.basePath);
139
139
  return { registries, https, basePath };
140
140
  }
141
- async function fetchFromRegistry(registry, targetUrl, limiter) {
141
+ async function fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, limiter) {
142
142
  await limiter.acquire();
143
143
  try {
144
- logger.info(`Fetching from: ${targetUrl}`);
144
+ logger.info(`Fetching from upstream: ${targetUrl}`);
145
+ const headersFromDownstreamClient = reqFromDownstreamClient.headers;
145
146
  const headers = registry.token ? { Authorization: `Bearer ${registry.token}` } : {};
146
- headers.Collection = "keep-alive";
147
- const response = await fetch(targetUrl, { headers });
147
+ // 合并 headersFromDownstreamClient 和 headers
148
+ const mergedHeaders = { ...headersFromDownstreamClient, ...headers, };
149
+ // (mergedHeaders as any).connection = "keep-alive"; 不允许私自添加 connection: keep-alive header,应当最终下游客户端自己的选择
150
+ const response = await fetch(targetUrl, { headers: mergedHeaders });
148
151
  logger.info(`Response from upstream ${targetUrl}: ${response.status} ${response.statusText} content-type=${response.headers.get('content-type')} content-length=${response.headers.get('content-length')} transfer-encoding=${response.headers.get('transfer-encoding')}`);
149
152
  return response.ok ? response : null;
150
153
  }
@@ -176,7 +179,8 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
176
179
  // 准备通用响应头信息
177
180
  const safeHeaders = {};
178
181
  // 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如access-control-allow-origin、set-cookie、server、strict-transport-security等,这意味着代理服务器向下游客户端屏蔽了这些认证等安全数据)
179
- const headersToCopy = ['cache-control', 'cf-cache-status', 'cf-ray', 'connection', 'content-type', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
182
+ // 也不能包含cf-cache-statuscf-ray(Cloudflare 特有字段)可能干扰客户端解析。
183
+ const headersToCopy = ['cache-control', 'connection', 'content-type', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
180
184
  headersToCopy.forEach(header => {
181
185
  const value = upstreamResponse.headers.get(header);
182
186
  if (value)
@@ -203,8 +207,8 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
203
207
  }
204
208
  const bodyData = JSON.stringify(data);
205
209
  resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
206
- logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders));
207
- resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
210
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
211
+ resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders /*{'content-type': 'application/json'}*/).end(bodyData);
208
212
  }
209
213
  else if (contentType.includes('application/octet-stream')) {
210
214
  // 二进制流处理
@@ -214,6 +218,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
214
218
  }
215
219
  else {
216
220
  // write back to client
221
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
217
222
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
218
223
  // stop pipe when req from client is closed accidentally.
219
224
  const cleanup = () => {
@@ -255,6 +260,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
255
260
  logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
256
261
  const bodyData = await upstreamResponse.text();
257
262
  resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
263
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
258
264
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
259
265
  }
260
266
  }
@@ -291,12 +297,14 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
291
297
  let targetRegistry = null;
292
298
  let targetUrl = null;
293
299
  for (const registry of registryInfos) {
294
- if (reqFromDownstreamClient.destroyed)
295
- break;
300
+ if (reqFromDownstreamClient.destroyed) {
301
+ // 如果下游客户端自己提前断开(或取消)请求,那么这里不再逐个fallback方式请求(fetch)上游数据了,直接退出循环并返回
302
+ return;
303
+ }
296
304
  targetRegistry = registry;
297
305
  const search = fullUrl.search || '';
298
306
  targetUrl = `${registry.normalizedRegistryUrl}${path}${search}`;
299
- const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, limiter);
307
+ const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, limiter);
300
308
  if (okResponseOrNull) {
301
309
  successfulResponseFromUpstream = okResponseOrNull;
302
310
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.83",
3
+ "version": "1.0.85",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",