com.jimuwd.xian.registry-proxy 1.0.50 → 1.0.52

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
@@ -33,7 +33,7 @@ class ConcurrencyLimiter {
33
33
  }
34
34
  }
35
35
  }
36
- const limiter = new ConcurrencyLimiter(3);
36
+ const limiter = new ConcurrencyLimiter(10);
37
37
  function removeEndingSlashAndForceStartingSlash(str) {
38
38
  if (!str)
39
39
  return '/';
@@ -144,7 +144,7 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
144
144
  const headers = registry.token ? { Authorization: `Bearer ${registry.token}` } : {};
145
145
  headers.Collection = "keep-alive";
146
146
  const response = await fetch(targetUrl, { headers });
147
- console.log(`Response from upstream ${targetUrl}: ${response.status} ${response.statusText} content-type=${response.headers.get("content-type")} content-length=${response.headers.get("content-length")}`);
147
+ console.log(`Response from upstream ${targetUrl}: ${response.status} ${response.statusText}`, { headers: [...response.headers] });
148
148
  return response.ok ? response : null;
149
149
  }
150
150
  catch (e) {
@@ -164,16 +164,13 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamRes
164
164
  if (!upstreamResponse.ok)
165
165
  throw new Error("Only 2xx upstream response is supported");
166
166
  try {
167
- const upstreamContentType = upstreamResponse.headers.get('Content-Type');
168
- const upstreamConnection = upstreamResponse.headers.get("connection");
169
- const contentType = upstreamContentType || 'application/octet-stream';
170
- const connection = upstreamConnection || 'keep-alive';
167
+ const contentType = upstreamResponse.headers.get('content-type') || 'application/octet-stream';
168
+ const connection = upstreamResponse.headers.get("connection") || 'keep-alive';
171
169
  // 准备通用头信息
172
170
  const safeHeaders = { 'connection': connection, 'content-type': contentType, };
173
171
  // 复制所有可能需要的头信息
174
172
  const headersToCopy = [
175
- 'etag', 'last-modified',
176
- 'cache-control',
173
+ // 'etag', 'last-modified', 'cache-control',
177
174
  'content-length',
178
175
  'content-encoding',
179
176
  'transfer-encoding',
@@ -208,7 +205,7 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamRes
208
205
  res.writeHead(502).end('Empty Upstream Response');
209
206
  }
210
207
  else {
211
- res.writeHead(upstreamResponse.status, /*safeHeaders*/ { connection: "keep-alive", "content-type": "application/octet-stream", "transfer-encoding": "chunked" });
208
+ res.writeHead(upstreamResponse.status, safeHeaders);
212
209
  upstreamResponse.body.pipe(res, { end: true });
213
210
  }
214
211
  }
@@ -251,9 +248,9 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
251
248
  targetRegistry = registry;
252
249
  const search = fullUrl.search || '';
253
250
  targetUrl = `${registry.normalizedRegistryUrl}${path}${search}`;
254
- const response = await fetchFromRegistry(registry, targetUrl, limiter);
255
- if (response) {
256
- successfulResponse = response;
251
+ const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, limiter);
252
+ if (okResponseOrNull) {
253
+ successfulResponse = okResponseOrNull;
257
254
  break;
258
255
  }
259
256
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.50",
3
+ "version": "1.0.52",
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
@@ -78,7 +78,7 @@ class ConcurrencyLimiter {
78
78
  }
79
79
  }
80
80
 
81
- const limiter = new ConcurrencyLimiter(3);
81
+ const limiter = new ConcurrencyLimiter(10);
82
82
 
83
83
  function removeEndingSlashAndForceStartingSlash(str: string | undefined | null): string {
84
84
  if (!str) return '/';
@@ -196,7 +196,7 @@ async function fetchFromRegistry(
196
196
  const headers: {} = registry.token ? {Authorization: `Bearer ${registry.token}`} : {};
197
197
  (headers as any).Collection = "keep-alive";
198
198
  const response = await fetch(targetUrl, {headers});
199
- console.log(`Response from upstream ${targetUrl}: ${response.status} ${response.statusText} content-type=${response.headers.get("content-type")} content-length=${response.headers.get("content-length")}`);
199
+ console.log(`Response from upstream ${targetUrl}: ${response.status} ${response.statusText}`, {headers: [...response.headers]});
200
200
  return response.ok ? response : null;
201
201
  } catch (e) {
202
202
  if (e instanceof Error) {
@@ -227,18 +227,15 @@ async function writeSuccessfulResponse(
227
227
  if (!upstreamResponse.ok) throw new Error("Only 2xx upstream response is supported");
228
228
 
229
229
  try {
230
- const upstreamContentType = upstreamResponse.headers.get('Content-Type');
231
- const upstreamConnection = upstreamResponse.headers.get("connection");
232
- const contentType = upstreamContentType || 'application/octet-stream';
233
- const connection = upstreamConnection || 'keep-alive';
230
+ const contentType = upstreamResponse.headers.get('content-type') || 'application/octet-stream';
231
+ const connection = upstreamResponse.headers.get("connection") || 'keep-alive';
234
232
 
235
233
  // 准备通用头信息
236
234
  const safeHeaders: OutgoingHttpHeaders = {'connection': connection, 'content-type': contentType,};
237
235
 
238
236
  // 复制所有可能需要的头信息
239
237
  const headersToCopy = [
240
- 'etag', 'last-modified',
241
- 'cache-control',
238
+ // 'etag', 'last-modified', 'cache-control',
242
239
  'content-length',
243
240
  'content-encoding',
244
241
  'transfer-encoding',
@@ -273,7 +270,7 @@ async function writeSuccessfulResponse(
273
270
  console.error(`Empty response body from ${targetUrl}`);
274
271
  res.writeHead(502).end('Empty Upstream Response');
275
272
  } else {
276
- res.writeHead(upstreamResponse.status, /*safeHeaders*/{connection:"keep-alive","content-type":"application/octet-stream","transfer-encoding":"chunked"});
273
+ res.writeHead(upstreamResponse.status, safeHeaders);
277
274
  upstreamResponse.body.pipe(res, {end: true});
278
275
  }
279
276
  }
@@ -326,9 +323,9 @@ export async function startProxyServer(
326
323
  targetRegistry = registry;
327
324
  const search = fullUrl.search || '';
328
325
  targetUrl = `${registry.normalizedRegistryUrl}${path}${search}`;
329
- const response = await fetchFromRegistry(registry, targetUrl, limiter);
330
- if (response) {
331
- successfulResponse = response;
326
+ const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, limiter);
327
+ if (okResponseOrNull) {
328
+ successfulResponse = okResponseOrNull;
332
329
  break;
333
330
  }
334
331
  }