com.jimuwd.xian.registry-proxy 1.0.7 → 1.0.8
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 +13 -11
- package/package.json +1 -1
- package/src/index.ts +13 -11
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,6 +90,9 @@ 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
|
res.writeHead(400);
|
|
@@ -102,10 +102,12 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
|
|
|
102
102
|
const pathname = new URL(req.url, `http://${req.headers.host}`).pathname;
|
|
103
103
|
const fetchPromises = registries.map(async ({ url: registry, token }) => {
|
|
104
104
|
const targetUrl = `${registry}${pathname}`;
|
|
105
|
+
console.log(`Fetching ${targetUrl} with token: ${token ? 'present' : 'none'}`);
|
|
105
106
|
try {
|
|
106
107
|
const response = await fetch(targetUrl, {
|
|
107
108
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
108
109
|
});
|
|
110
|
+
console.log(`Response from ${targetUrl}: ${response.status}`);
|
|
109
111
|
if (response.ok)
|
|
110
112
|
return response;
|
|
111
113
|
throw new Error(`Failed: ${response.status}`);
|
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,6 +106,9 @@ 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') {
|
|
@@ -121,10 +121,12 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
|
|
|
121
121
|
|
|
122
122
|
const fetchPromises = registries.map(async ({ url: registry, token }) => {
|
|
123
123
|
const targetUrl = `${registry}${pathname}`;
|
|
124
|
+
console.log(`Fetching ${targetUrl} with token: ${token ? 'present' : 'none'}`);
|
|
124
125
|
try {
|
|
125
126
|
const response = await fetch(targetUrl, {
|
|
126
127
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
127
128
|
});
|
|
129
|
+
console.log(`Response from ${targetUrl}: ${response.status}`);
|
|
128
130
|
if (response.ok) return response;
|
|
129
131
|
throw new Error(`Failed: ${response.status}`);
|
|
130
132
|
} catch (e) {
|