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

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 +16 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -163,21 +163,28 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
163
163
  limiter.release();
164
164
  }
165
165
  }
166
+ function resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData) {
167
+ if (!safeHeaders["transfer-encoding"]) {
168
+ logger.info(`Transfer-Encoding header is absent, then set the content-length header, upstream url is ${targetUrl}`);
169
+ safeHeaders["content-length"] = bodyData.length;
170
+ }
171
+ }
166
172
  async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDownstreamClient, upstreamResponse, reqFromDownstreamClient, proxyInfo, proxyPort, registryInfos) {
167
173
  if (!upstreamResponse.ok)
168
174
  throw new Error("Only 2xx upstream response is supported");
169
175
  try {
170
- const contentType = upstreamResponse.headers.get('content-type') || 'application/octet-stream';
171
- const connection = upstreamResponse.headers.get("connection") || 'keep-alive';
172
- // 准备通用头信息
173
- const safeHeaders = { 'connection': connection, 'content-type': contentType, };
174
- // 复制所有可能需要的头信息
175
- const headersToCopy = ['content-encoding', 'transfer-encoding',];
176
+ // 准备通用响应头信息
177
+ const safeHeaders = {};
178
+ // 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如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',];
176
180
  headersToCopy.forEach(header => {
177
181
  const value = upstreamResponse.headers.get(header);
178
182
  if (value)
179
183
  safeHeaders[header] = value;
180
184
  });
185
+ if (!safeHeaders['content-type'])
186
+ safeHeaders['content-type'] = 'application/octet-stream';
187
+ const contentType = safeHeaders['content-type'];
181
188
  if (contentType.includes('application/json')) {
182
189
  // JSON 处理逻辑
183
190
  const data = await upstreamResponse.json();
@@ -195,10 +202,8 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
195
202
  }
196
203
  }
197
204
  const bodyData = JSON.stringify(data);
198
- if (!safeHeaders["transfer-encoding"]) {
199
- // if transfer-encoding header is absent, then set the content-length header.
200
- safeHeaders["content-length"] = bodyData.length;
201
- }
205
+ resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
206
+ logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders));
202
207
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
203
208
  }
204
209
  else if (contentType.includes('application/octet-stream')) {
@@ -249,10 +254,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
249
254
  else {
250
255
  logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
251
256
  const bodyData = await upstreamResponse.text();
252
- if (!safeHeaders["transfer-encoding"]) {
253
- // if transfer-encoding header is absent, then set the content-length header.
254
- safeHeaders["content-length"] = bodyData.length;
255
- }
257
+ resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
256
258
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
257
259
  }
258
260
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.81",
3
+ "version": "1.0.83",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",