com.jimuwd.xian.registry-proxy 1.0.8 → 1.0.10

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 (3) hide show
  1. package/dist/index.js +18 -12
  2. package/package.json +1 -1
  3. package/src/index.ts +18 -11
package/dist/index.js CHANGED
@@ -95,22 +95,26 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
95
95
  });
96
96
  const server = createServer(async (req, res) => {
97
97
  if (!req.url || req.method !== 'GET') {
98
- res.writeHead(400);
98
+ console.log(`Invalid request: URL=${req.url}, Method=${req.method}`);
99
+ res.writeHead(400, { 'Content-Type': 'text/plain' });
99
100
  res.end('Bad Request');
100
101
  return;
101
102
  }
102
- const pathname = new URL(req.url, `http://${req.headers.host}`).pathname;
103
+ const fullUrl = new URL(req.url, `http://${req.headers.host}`);
104
+ console.log(`Received request: ${fullUrl.pathname} (Full URL: ${fullUrl.href})`); // 增强日志
105
+ const pathname = fullUrl.pathname;
103
106
  const fetchPromises = registries.map(async ({ url: registry, token }) => {
104
107
  const targetUrl = `${registry}${pathname}`;
105
- console.log(`Fetching ${targetUrl} with token: ${token ? 'present' : 'none'}`);
108
+ const headers = token ? { Authorization: `Bearer ${token}` } : undefined;
109
+ console.log(`Fetching ${targetUrl} with headers:`, JSON.stringify(headers, null, 2)); // 打印完整 headers
106
110
  try {
107
- const response = await fetch(targetUrl, {
108
- headers: token ? { Authorization: `Bearer ${token}` } : undefined,
109
- });
110
- console.log(`Response from ${targetUrl}: ${response.status}`);
111
- if (response.ok)
112
- return response;
113
- throw new Error(`Failed: ${response.status}`);
111
+ const response = await fetch(targetUrl, { headers });
112
+ console.log(`Response from ${targetUrl}: ${response.status} ${response.statusText}`);
113
+ if (!response.ok) {
114
+ const errorBody = await response.text();
115
+ console.log(`Error body from ${targetUrl}: ${errorBody}`);
116
+ }
117
+ return response;
114
118
  }
115
119
  catch (e) {
116
120
  console.error(`Fetch failed for ${targetUrl}: ${e.message}`);
@@ -118,15 +122,17 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
118
122
  }
119
123
  });
120
124
  const responses = await Promise.all(fetchPromises);
121
- const successResponse = responses.find((r) => r !== null);
125
+ const successResponse = responses.find((r) => r?.ok);
122
126
  if (successResponse) {
127
+ console.log(`Forwarding successful response from ${successResponse.url}: ${successResponse.status} ${successResponse.statusText}`);
123
128
  res.writeHead(successResponse.status, {
124
129
  'Content-Type': successResponse.headers.get('Content-Type') || 'application/octet-stream',
125
130
  });
126
131
  successResponse.body?.pipe(res);
127
132
  }
128
133
  else {
129
- res.writeHead(404);
134
+ console.log('No successful response found, returning 404');
135
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
130
136
  res.end('Package not found');
131
137
  }
132
138
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
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
@@ -112,23 +112,28 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
112
112
 
113
113
  const server = createServer(async (req, res) => {
114
114
  if (!req.url || req.method !== 'GET') {
115
- res.writeHead(400);
115
+ console.log(`Invalid request: URL=${req.url}, Method=${req.method}`);
116
+ res.writeHead(400, { 'Content-Type': 'text/plain' });
116
117
  res.end('Bad Request');
117
118
  return;
118
119
  }
119
120
 
120
- const pathname = new URL(req.url, `http://${req.headers.host}`).pathname;
121
+ const fullUrl = new URL(req.url, `http://${req.headers.host}`);
122
+ console.log(`Received request: ${fullUrl.pathname} (Full URL: ${fullUrl.href})`); // 增强日志
123
+ const pathname = fullUrl.pathname;
121
124
 
122
125
  const fetchPromises = registries.map(async ({ url: registry, token }) => {
123
126
  const targetUrl = `${registry}${pathname}`;
124
- console.log(`Fetching ${targetUrl} with token: ${token ? 'present' : 'none'}`);
127
+ const headers: Record<string, string> | undefined = token ? { Authorization: `Bearer ${token}` } : undefined;
128
+ console.log(`Fetching ${targetUrl} with headers:`, JSON.stringify(headers, null, 2)); // 打印完整 headers
125
129
  try {
126
- const response = await fetch(targetUrl, {
127
- headers: token ? { Authorization: `Bearer ${token}` } : undefined,
128
- });
129
- console.log(`Response from ${targetUrl}: ${response.status}`);
130
- if (response.ok) return response;
131
- throw new Error(`Failed: ${response.status}`);
130
+ const response = await fetch(targetUrl, { headers });
131
+ console.log(`Response from ${targetUrl}: ${response.status} ${response.statusText}`);
132
+ if (!response.ok) {
133
+ const errorBody = await response.text();
134
+ console.log(`Error body from ${targetUrl}: ${errorBody}`);
135
+ }
136
+ return response;
132
137
  } catch (e) {
133
138
  console.error(`Fetch failed for ${targetUrl}: ${(e as Error).message}`);
134
139
  return null;
@@ -136,15 +141,17 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
136
141
  });
137
142
 
138
143
  const responses = await Promise.all(fetchPromises);
139
- const successResponse = responses.find((r: Response | null) => r !== null);
144
+ const successResponse = responses.find((r: Response | null) => r?.ok);
140
145
 
141
146
  if (successResponse) {
147
+ console.log(`Forwarding successful response from ${successResponse.url}: ${successResponse.status} ${successResponse.statusText}`);
142
148
  res.writeHead(successResponse.status, {
143
149
  'Content-Type': successResponse.headers.get('Content-Type') || 'application/octet-stream',
144
150
  });
145
151
  successResponse.body?.pipe(res);
146
152
  } else {
147
- res.writeHead(404);
153
+ console.log('No successful response found, returning 404');
154
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
148
155
  res.end('Package not found');
149
156
  }
150
157
  });