com.jimuwd.xian.registry-proxy 1.0.7 → 1.0.9
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 +29 -21
- package/package.json +1 -1
- package/src/index.ts +29 -20
package/dist/index.js
CHANGED
|
@@ -58,30 +58,27 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
58
58
|
if (regConfig && 'npmAuthToken' in regConfig) {
|
|
59
59
|
token = regConfig.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || regConfig.npmAuthToken;
|
|
60
60
|
}
|
|
61
|
-
// 使用 normalizeUrl 和 normalizeUrl + "/" 查找 token
|
|
62
61
|
const normalizedUrl = normalizeUrl(url);
|
|
63
62
|
const urlWithSlash = normalizedUrl + '/';
|
|
64
63
|
if (!token) {
|
|
65
|
-
// 检查本地 Yarn 配置
|
|
66
64
|
const localConfig = localYarnConfig.npmRegistries;
|
|
67
|
-
if (localConfig?.[normalizedUrl]
|
|
68
|
-
token = localConfig[normalizedUrl].npmAuthToken
|
|
65
|
+
if (localConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
66
|
+
token = localConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[normalizedUrl].npmAuthToken;
|
|
69
67
|
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (normalized)`);
|
|
70
68
|
}
|
|
71
|
-
else if (localConfig?.[urlWithSlash]
|
|
72
|
-
token = localConfig[urlWithSlash].npmAuthToken
|
|
69
|
+
else if (localConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
70
|
+
token = localConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[urlWithSlash].npmAuthToken;
|
|
73
71
|
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (with slash)`);
|
|
74
72
|
}
|
|
75
73
|
}
|
|
76
74
|
if (!token) {
|
|
77
|
-
// 检查全局 Yarn 配置
|
|
78
75
|
const globalConfig = globalYarnConfig.npmRegistries;
|
|
79
|
-
if (globalConfig?.[normalizedUrl]
|
|
80
|
-
token = globalConfig[normalizedUrl].npmAuthToken
|
|
76
|
+
if (globalConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
77
|
+
token = globalConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[normalizedUrl].npmAuthToken;
|
|
81
78
|
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (normalized)`);
|
|
82
79
|
}
|
|
83
|
-
else if (globalConfig?.[urlWithSlash]
|
|
84
|
-
token = globalConfig[urlWithSlash].npmAuthToken
|
|
80
|
+
else if (globalConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
81
|
+
token = globalConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[urlWithSlash].npmAuthToken;
|
|
85
82
|
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (with slash)`);
|
|
86
83
|
}
|
|
87
84
|
}
|
|
@@ -93,22 +90,31 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
93
90
|
export async function startProxyServer(proxyConfigPath, localYarnConfigPath, globalYarnConfigPath, port = 0) {
|
|
94
91
|
console.log('Starting proxy server...');
|
|
95
92
|
const registries = await loadRegistries(proxyConfigPath, localYarnConfigPath, globalYarnConfigPath);
|
|
93
|
+
registries.forEach(({ url, token }) => {
|
|
94
|
+
console.log(`Registry: ${url}, Token: ${token ? 'present' : 'missing'}`);
|
|
95
|
+
});
|
|
96
96
|
const server = createServer(async (req, res) => {
|
|
97
97
|
if (!req.url || req.method !== 'GET') {
|
|
98
|
-
|
|
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
|
|
103
|
+
const fullUrl = new URL(req.url, `http://${req.headers.host}`);
|
|
104
|
+
const pathname = fullUrl.pathname;
|
|
105
|
+
console.log(`Received request: ${pathname}`);
|
|
103
106
|
const fetchPromises = registries.map(async ({ url: registry, token }) => {
|
|
104
107
|
const targetUrl = `${registry}${pathname}`;
|
|
108
|
+
const headers = token ? { Authorization: `Bearer ${token}` } : undefined;
|
|
109
|
+
console.log(`Fetching ${targetUrl} with headers:`, headers);
|
|
105
110
|
try {
|
|
106
|
-
const response = await fetch(targetUrl, {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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;
|
|
112
118
|
}
|
|
113
119
|
catch (e) {
|
|
114
120
|
console.error(`Fetch failed for ${targetUrl}: ${e.message}`);
|
|
@@ -116,15 +122,17 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
|
|
|
116
122
|
}
|
|
117
123
|
});
|
|
118
124
|
const responses = await Promise.all(fetchPromises);
|
|
119
|
-
const successResponse = responses.find((r) => r
|
|
125
|
+
const successResponse = responses.find((r) => r?.ok);
|
|
120
126
|
if (successResponse) {
|
|
127
|
+
console.log(`Forwarding successful response from ${successResponse.url}: ${successResponse.status} ${successResponse.statusText}`);
|
|
121
128
|
res.writeHead(successResponse.status, {
|
|
122
129
|
'Content-Type': successResponse.headers.get('Content-Type') || 'application/octet-stream',
|
|
123
130
|
});
|
|
124
131
|
successResponse.body?.pipe(res);
|
|
125
132
|
}
|
|
126
133
|
else {
|
|
127
|
-
|
|
134
|
+
console.log('No successful response found, returning 404');
|
|
135
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
128
136
|
res.end('Package not found');
|
|
129
137
|
}
|
|
130
138
|
});
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -71,30 +71,27 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
71
71
|
token = regConfig.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || regConfig.npmAuthToken;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
// 使用 normalizeUrl 和 normalizeUrl + "/" 查找 token
|
|
75
74
|
const normalizedUrl = normalizeUrl(url);
|
|
76
75
|
const urlWithSlash = normalizedUrl + '/';
|
|
77
76
|
|
|
78
77
|
if (!token) {
|
|
79
|
-
// 检查本地 Yarn 配置
|
|
80
78
|
const localConfig = localYarnConfig.npmRegistries;
|
|
81
|
-
if (localConfig?.[normalizedUrl]
|
|
82
|
-
token = localConfig[normalizedUrl]
|
|
79
|
+
if (localConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
80
|
+
token = localConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[normalizedUrl].npmAuthToken;
|
|
83
81
|
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (normalized)`);
|
|
84
|
-
} else if (localConfig?.[urlWithSlash]
|
|
85
|
-
token = localConfig[urlWithSlash]
|
|
82
|
+
} else if (localConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
83
|
+
token = localConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[urlWithSlash].npmAuthToken;
|
|
86
84
|
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (with slash)`);
|
|
87
85
|
}
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
if (!token) {
|
|
91
|
-
// 检查全局 Yarn 配置
|
|
92
89
|
const globalConfig = globalYarnConfig.npmRegistries;
|
|
93
|
-
if (globalConfig?.[normalizedUrl]
|
|
94
|
-
token = globalConfig[normalizedUrl]
|
|
90
|
+
if (globalConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
91
|
+
token = globalConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[normalizedUrl].npmAuthToken;
|
|
95
92
|
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (normalized)`);
|
|
96
|
-
} else if (globalConfig?.[urlWithSlash]
|
|
97
|
-
token = globalConfig[urlWithSlash]
|
|
93
|
+
} else if (globalConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
94
|
+
token = globalConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[urlWithSlash].npmAuthToken;
|
|
98
95
|
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (with slash)`);
|
|
99
96
|
}
|
|
100
97
|
}
|
|
@@ -109,24 +106,34 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
109
106
|
export async function startProxyServer(proxyConfigPath?: string, localYarnConfigPath?: string, globalYarnConfigPath?: string, port: number = 0): Promise<Server> {
|
|
110
107
|
console.log('Starting proxy server...');
|
|
111
108
|
const registries = await loadRegistries(proxyConfigPath, localYarnConfigPath, globalYarnConfigPath);
|
|
109
|
+
registries.forEach(({ url, token }) => {
|
|
110
|
+
console.log(`Registry: ${url}, Token: ${token ? 'present' : 'missing'}`);
|
|
111
|
+
});
|
|
112
112
|
|
|
113
113
|
const server = createServer(async (req, res) => {
|
|
114
114
|
if (!req.url || req.method !== 'GET') {
|
|
115
|
-
|
|
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
|
|
121
|
+
const fullUrl = new URL(req.url, `http://${req.headers.host}`);
|
|
122
|
+
const pathname = fullUrl.pathname;
|
|
123
|
+
console.log(`Received request: ${pathname}`);
|
|
121
124
|
|
|
122
125
|
const fetchPromises = registries.map(async ({ url: registry, token }) => {
|
|
123
126
|
const targetUrl = `${registry}${pathname}`;
|
|
127
|
+
const headers: Record<string, string> | undefined = token ? { Authorization: `Bearer ${token}` } : undefined;
|
|
128
|
+
console.log(`Fetching ${targetUrl} with headers:`, headers);
|
|
124
129
|
try {
|
|
125
|
-
const response = await fetch(targetUrl, {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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;
|
|
130
137
|
} catch (e) {
|
|
131
138
|
console.error(`Fetch failed for ${targetUrl}: ${(e as Error).message}`);
|
|
132
139
|
return null;
|
|
@@ -134,15 +141,17 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
|
|
|
134
141
|
});
|
|
135
142
|
|
|
136
143
|
const responses = await Promise.all(fetchPromises);
|
|
137
|
-
const successResponse = responses.find((r: Response | null) => r
|
|
144
|
+
const successResponse = responses.find((r: Response | null) => r?.ok);
|
|
138
145
|
|
|
139
146
|
if (successResponse) {
|
|
147
|
+
console.log(`Forwarding successful response from ${successResponse.url}: ${successResponse.status} ${successResponse.statusText}`);
|
|
140
148
|
res.writeHead(successResponse.status, {
|
|
141
149
|
'Content-Type': successResponse.headers.get('Content-Type') || 'application/octet-stream',
|
|
142
150
|
});
|
|
143
151
|
successResponse.body?.pipe(res);
|
|
144
152
|
} else {
|
|
145
|
-
|
|
153
|
+
console.log('No successful response found, returning 404');
|
|
154
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
146
155
|
res.end('Package not found');
|
|
147
156
|
}
|
|
148
157
|
});
|