com.jimuwd.xian.registry-proxy 1.0.84 → 1.0.86

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 +25 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -138,13 +138,22 @@ 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}`);
145
- const headers = registry.token ? { Authorization: `Bearer ${registry.token}` } : {};
146
- headers.Collection = "keep-alive";
147
- const response = await fetch(targetUrl, { headers });
144
+ logger.info(`Fetching from upstream: ${targetUrl}`);
145
+ const headersFromDownstreamClient = reqFromDownstreamClient.headers;
146
+ const authorizationHeaders = registry.token ? { Authorization: `Bearer ${registry.token}` } : {};
147
+ // 合并 headersFromDownstreamClient authorizationHeaders
148
+ const mergedHeaders = { ...headersFromDownstreamClient, ...authorizationHeaders, };
149
+ // (mergedHeaders as any).connection = "keep-alive"; 不允许私自添加 connection: keep-alive header,应当最终下游客户端自己的选择
150
+ // 替换“Host”头为upstream的host
151
+ const upstreamHost = new URL(registry.normalizedRegistryUrl).host;
152
+ if (mergedHeaders.host) {
153
+ logger.info(`Replace 'Host=${mergedHeaders.host}' header in downstream request to upstream 'Host=${upstreamHost}' header when proxying to upstream ${targetUrl}.`);
154
+ mergedHeaders.host = upstreamHost;
155
+ }
156
+ const response = await fetch(targetUrl, { headers: mergedHeaders });
148
157
  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
158
  return response.ok ? response : null;
150
159
  }
@@ -176,7 +185,8 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
176
185
  // 准备通用响应头信息
177
186
  const safeHeaders = {};
178
187
  // 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如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',];
188
+ // 也不能包含cf-cache-statuscf-ray(Cloudflare 特有字段)可能干扰客户端解析。
189
+ const headersToCopy = ['cache-control', 'connection', 'content-type', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
180
190
  headersToCopy.forEach(header => {
181
191
  const value = upstreamResponse.headers.get(header);
182
192
  if (value)
@@ -203,8 +213,8 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
203
213
  }
204
214
  const bodyData = JSON.stringify(data);
205
215
  resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
206
- logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders));
207
- resToDownstreamClient.writeHead(upstreamResponse.status, /*safeHeaders*/ { 'content-type': 'application/json' }).end(bodyData);
216
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
217
+ resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders /*{'content-type': 'application/json'}*/).end(bodyData);
208
218
  }
209
219
  else if (contentType.includes('application/octet-stream')) {
210
220
  // 二进制流处理
@@ -214,6 +224,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
214
224
  }
215
225
  else {
216
226
  // write back to client
227
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
217
228
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
218
229
  // stop pipe when req from client is closed accidentally.
219
230
  const cleanup = () => {
@@ -255,6 +266,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
255
266
  logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
256
267
  const bodyData = await upstreamResponse.text();
257
268
  resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
269
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
258
270
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
259
271
  }
260
272
  }
@@ -291,12 +303,14 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
291
303
  let targetRegistry = null;
292
304
  let targetUrl = null;
293
305
  for (const registry of registryInfos) {
294
- if (reqFromDownstreamClient.destroyed)
295
- break;
306
+ if (reqFromDownstreamClient.destroyed) {
307
+ // 如果下游客户端自己提前断开(或取消)请求,那么这里不再逐个fallback方式请求(fetch)上游数据了,直接退出循环并返回
308
+ return;
309
+ }
296
310
  targetRegistry = registry;
297
311
  const search = fullUrl.search || '';
298
312
  targetUrl = `${registry.normalizedRegistryUrl}${path}${search}`;
299
- const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, limiter);
313
+ const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, limiter);
300
314
  if (okResponseOrNull) {
301
315
  successfulResponseFromUpstream = okResponseOrNull;
302
316
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.84",
3
+ "version": "1.0.86",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",