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

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 +27 -28
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -154,7 +154,13 @@ async function fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, l
154
154
  mergedHeaders.host = upstreamHost;
155
155
  }
156
156
  const response = await fetch(targetUrl, { headers: mergedHeaders });
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')}`);
157
+ logger.info(`
158
+ Response from upstream ${targetUrl}: ${response.status} ${response.statusText}
159
+ content-type=${response.headers.get('content-type')}
160
+ content-encoding=${response.headers.get('content-encoding')}
161
+ content-length=${response.headers.get('content-length')}
162
+ transfer-encoding=${response.headers.get('transfer-encoding')}
163
+ `);
158
164
  return response.ok ? response : null;
159
165
  }
160
166
  catch (e) {
@@ -172,33 +178,14 @@ async function fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, l
172
178
  limiter.release();
173
179
  }
174
180
  }
175
- function resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData) {
176
- if (!safeHeaders["transfer-encoding"]) {
177
- logger.info(`Transfer-Encoding header is absent, then set the content-length header, upstream url is ${targetUrl}`);
178
- safeHeaders["content-length"] = bodyData.length;
179
- }
180
- }
181
181
  async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDownstreamClient, upstreamResponse, reqFromDownstreamClient, proxyInfo, proxyPort, registryInfos) {
182
182
  if (!upstreamResponse.ok)
183
183
  throw new Error("Only 2xx upstream response is supported");
184
184
  try {
185
- // 准备通用响应头信息
186
- const safeHeaders = {};
187
- // 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如access-control-allow-origin、set-cookie、server、strict-transport-security等,这意味着代理服务器向下游客户端屏蔽了这些认证等安全数据)
188
- // 也不能包含cf-cache-status、cf-ray(Cloudflare 特有字段)可能干扰客户端解析。
189
- const headersToCopy = ['cache-control', 'connection', 'content-type', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
190
- headersToCopy.forEach(header => {
191
- const value = upstreamResponse.headers.get(header);
192
- if (value)
193
- safeHeaders[header] = value;
194
- });
195
- if (!safeHeaders['content-type'])
196
- safeHeaders['content-type'] = 'application/octet-stream';
197
- const contentType = safeHeaders['content-type'];
198
- if (contentType.includes('application/json')) {
199
- // JSON 处理逻辑
185
+ const contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
186
+ if (contentType.includes('application/json')) { // JSON 处理逻辑
200
187
  const data = await upstreamResponse.json();
201
- if (data.versions) {
188
+ if (data.versions) { // 处理node依赖包元数据
202
189
  const host = reqFromDownstreamClient.headers.host || `localhost:${proxyPort}`;
203
190
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
204
191
  for (const versionKey in data.versions) {
@@ -212,12 +199,24 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
212
199
  }
213
200
  }
214
201
  const bodyData = JSON.stringify(data);
215
- resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
202
+ const safeHeaders = { 'content-type': contentType, 'content-length': Buffer.byteLength(bodyData) };
216
203
  logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
217
- resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders /*{'content-type': 'application/json'}*/).end(bodyData);
204
+ resToDownstreamClient.writeHead(upstreamResponse.status, { 'content-type': 'application/json' }).end(bodyData);
218
205
  }
219
- else if (contentType.includes('application/octet-stream')) {
220
- // 二进制流处理
206
+ else if (contentType.includes('application/octet-stream')) { // 二进制流处理
207
+ // 准备通用响应头信息
208
+ const safeHeaders = {};
209
+ // 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如access-control-allow-origin、set-cookie、server、strict-transport-security等,这意味着代理服务器向下游客户端屏蔽了这些认证等安全数据)
210
+ // 也不能包含cf-cache-status、cf-ray(Cloudflare 特有字段)可能干扰客户端解析。
211
+ const headersToCopy = ['cache-control', 'connection', 'content-type', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
212
+ headersToCopy.forEach(header => {
213
+ const value = upstreamResponse.headers.get(header);
214
+ if (value)
215
+ safeHeaders[header] = value;
216
+ });
217
+ if (!safeHeaders['content-type'])
218
+ safeHeaders['content-type'] = 'application/octet-stream';
219
+ const contentType = safeHeaders['content-type'];
221
220
  if (!upstreamResponse.body) {
222
221
  logger.error(`Empty response body from upstream ${targetUrl}`);
223
222
  resToDownstreamClient.writeHead(502).end('Empty Upstream Response');
@@ -265,7 +264,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
265
264
  else {
266
265
  logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
267
266
  const bodyData = await upstreamResponse.text();
268
- resetHeaderContentLengthIfTransferEncodingIsAbsent(safeHeaders, targetUrl, bodyData);
267
+ const safeHeaders = { 'content-type': contentType, 'content-length': Buffer.byteLength(bodyData) };
269
268
  logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
270
269
  resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
271
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.86",
3
+ "version": "1.0.88",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",