com.jimuwd.xian.registry-proxy 1.0.38 → 1.0.40

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.js CHANGED
@@ -165,7 +165,9 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, response, r
165
165
  if (!response.ok)
166
166
  throw new Error("Only 2xx response is supported");
167
167
  try {
168
- const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
168
+ const upstreamContentType = response.headers.get('Content-Type');
169
+ console.log("Upstream Content-Type", upstreamContentType);
170
+ const contentType = upstreamContentType || 'application/octet-stream';
169
171
  // 准备通用头信息
170
172
  const safeHeaders = {
171
173
  'content-type': contentType,
@@ -181,27 +183,28 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, response, r
181
183
  if (value)
182
184
  safeHeaders[header] = value;
183
185
  });
184
- res.writeHead(response.status, safeHeaders);
185
186
  if (contentType.includes('application/json')) {
186
187
  // JSON 处理逻辑
187
188
  const data = await response.json();
188
189
  if (data.versions) {
189
190
  const host = req.headers.host || `localhost:${proxyPort}`;
190
191
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
191
- for (const version in data.versions) {
192
- const tarball = data.versions[version]?.dist?.tarball;
192
+ for (const versionKey in data.versions) {
193
+ const packageVersion = data.versions[versionKey];
194
+ const tarball = packageVersion?.dist?.tarball;
193
195
  if (tarball) {
194
196
  const path = removeRegistryPrefix(tarball, registryInfos);
195
197
  const proxiedTarballUrl = `${baseUrl}${path}${new URL(tarball).search || ''}`;
196
- data.versions[version].dist.tarball = proxiedTarballUrl;
197
- console.log("proxiedTarballUrl", proxiedTarballUrl);
198
+ packageVersion.dist.tarball = proxiedTarballUrl;
198
199
  }
199
200
  }
200
201
  }
202
+ res.writeHead(200, { "content-type": contentType });
201
203
  res.end(JSON.stringify(data));
202
204
  }
203
205
  else {
204
206
  // 二进制流处理
207
+ res.writeHead(response.status, safeHeaders);
205
208
  if (!response.body) {
206
209
  console.error(`Empty response body from ${targetUrl}`);
207
210
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.38",
3
+ "version": "1.0.40",
4
4
  "type": "module",
5
5
  "description": "A lightweight npm registry proxy with fallback support",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -228,7 +228,9 @@ async function writeSuccessfulResponse(
228
228
  if (!response.ok) throw new Error("Only 2xx response is supported");
229
229
 
230
230
  try {
231
- const contentType = response.headers.get('Content-Type') || 'application/octet-stream';
231
+ const upstreamContentType = response.headers.get('Content-Type');
232
+ console.log("Upstream Content-Type", upstreamContentType);
233
+ const contentType = upstreamContentType || 'application/octet-stream';
232
234
 
233
235
  // 准备通用头信息
234
236
  const safeHeaders: OutgoingHttpHeaders = {
@@ -247,9 +249,6 @@ async function writeSuccessfulResponse(
247
249
  if (value) safeHeaders[header] = value;
248
250
  });
249
251
 
250
-
251
- res.writeHead(response.status, safeHeaders);
252
-
253
252
  if (contentType.includes('application/json')) {
254
253
  // JSON 处理逻辑
255
254
  const data = await response.json() as PackageData;
@@ -257,19 +256,21 @@ async function writeSuccessfulResponse(
257
256
  const host = req.headers.host || `localhost:${proxyPort}`;
258
257
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
259
258
 
260
- for (const version in data.versions) {
261
- const tarball = data.versions[version]?.dist?.tarball;
259
+ for (const versionKey in data.versions) {
260
+ const packageVersion = data.versions[versionKey];
261
+ const tarball = packageVersion?.dist?.tarball;
262
262
  if (tarball) {
263
263
  const path = removeRegistryPrefix(tarball, registryInfos);
264
- const proxiedTarballUrl = `${baseUrl}${path}${new URL(tarball).search || ''}`;
265
- data.versions[version]!.dist!.tarball = proxiedTarballUrl;
266
- console.log("proxiedTarballUrl", proxiedTarballUrl);
264
+ const proxiedTarballUrl: string = `${baseUrl}${path}${new URL(tarball).search || ''}`;
265
+ packageVersion!.dist!.tarball = proxiedTarballUrl as string;
267
266
  }
268
267
  }
269
268
  }
269
+ res.writeHead(200, {"content-type":contentType});
270
270
  res.end(JSON.stringify(data));
271
271
  } else {
272
272
  // 二进制流处理
273
+ res.writeHead(response.status, safeHeaders);
273
274
  if (!response.body) {
274
275
  console.error(`Empty response body from ${targetUrl}`);
275
276
  } else {