com.jimuwd.xian.registry-proxy 1.0.6 → 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 +26 -7
- package/package.json +1 -1
- package/src/index.ts +26 -8
package/dist/index.js
CHANGED
|
@@ -59,13 +59,28 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
59
59
|
token = regConfig.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || regConfig.npmAuthToken;
|
|
60
60
|
}
|
|
61
61
|
const normalizedUrl = normalizeUrl(url);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
const urlWithSlash = normalizedUrl + '/';
|
|
63
|
+
if (!token) {
|
|
64
|
+
const localConfig = localYarnConfig.npmRegistries;
|
|
65
|
+
if (localConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
66
|
+
token = localConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[normalizedUrl].npmAuthToken;
|
|
67
|
+
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (normalized)`);
|
|
68
|
+
}
|
|
69
|
+
else if (localConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
70
|
+
token = localConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[urlWithSlash].npmAuthToken;
|
|
71
|
+
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (with slash)`);
|
|
72
|
+
}
|
|
65
73
|
}
|
|
66
|
-
if (!token
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
if (!token) {
|
|
75
|
+
const globalConfig = globalYarnConfig.npmRegistries;
|
|
76
|
+
if (globalConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
77
|
+
token = globalConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[normalizedUrl].npmAuthToken;
|
|
78
|
+
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (normalized)`);
|
|
79
|
+
}
|
|
80
|
+
else if (globalConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
81
|
+
token = globalConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[urlWithSlash].npmAuthToken;
|
|
82
|
+
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (with slash)`);
|
|
83
|
+
}
|
|
69
84
|
}
|
|
70
85
|
console.log(`Registry ${url}: token=${token ? 'present' : 'missing'}`);
|
|
71
86
|
return { url, token };
|
|
@@ -75,6 +90,9 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
75
90
|
export async function startProxyServer(proxyConfigPath, localYarnConfigPath, globalYarnConfigPath, port = 0) {
|
|
76
91
|
console.log('Starting proxy server...');
|
|
77
92
|
const registries = await loadRegistries(proxyConfigPath, localYarnConfigPath, globalYarnConfigPath);
|
|
93
|
+
registries.forEach(({ url, token }) => {
|
|
94
|
+
console.log(`Registry: ${url}, Token: ${token ? 'present' : 'missing'}`);
|
|
95
|
+
});
|
|
78
96
|
const server = createServer(async (req, res) => {
|
|
79
97
|
if (!req.url || req.method !== 'GET') {
|
|
80
98
|
res.writeHead(400);
|
|
@@ -84,10 +102,12 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
|
|
|
84
102
|
const pathname = new URL(req.url, `http://${req.headers.host}`).pathname;
|
|
85
103
|
const fetchPromises = registries.map(async ({ url: registry, token }) => {
|
|
86
104
|
const targetUrl = `${registry}${pathname}`;
|
|
105
|
+
console.log(`Fetching ${targetUrl} with token: ${token ? 'present' : 'none'}`);
|
|
87
106
|
try {
|
|
88
107
|
const response = await fetch(targetUrl, {
|
|
89
108
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
90
109
|
});
|
|
110
|
+
console.log(`Response from ${targetUrl}: ${response.status}`);
|
|
91
111
|
if (response.ok)
|
|
92
112
|
return response;
|
|
93
113
|
throw new Error(`Failed: ${response.status}`);
|
|
@@ -125,7 +145,6 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
|
|
|
125
145
|
}
|
|
126
146
|
const addressInfo = address;
|
|
127
147
|
const actualPort = addressInfo.port;
|
|
128
|
-
// 从环境变量获取项目根目录,写入端口文件
|
|
129
148
|
const projectRoot = process.env.PROJECT_ROOT || process.cwd();
|
|
130
149
|
const portFilePath = join(projectRoot, '.registry-proxy-port');
|
|
131
150
|
console.log(`Proxy server started at http://localhost:${actualPort}`);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createServer, Server } from 'http';
|
|
3
|
-
import { AddressInfo } from 'net';
|
|
3
|
+
import { AddressInfo } from 'net';
|
|
4
4
|
import { readFile } from 'fs/promises';
|
|
5
5
|
import { load } from 'js-yaml';
|
|
6
6
|
import fetch, { Response } from 'node-fetch';
|
|
@@ -72,14 +72,28 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
const normalizedUrl = normalizeUrl(url);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
const urlWithSlash = normalizedUrl + '/';
|
|
76
|
+
|
|
77
|
+
if (!token) {
|
|
78
|
+
const localConfig = localYarnConfig.npmRegistries;
|
|
79
|
+
if (localConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
80
|
+
token = localConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[normalizedUrl].npmAuthToken;
|
|
81
|
+
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (normalized)`);
|
|
82
|
+
} else if (localConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
83
|
+
token = localConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[urlWithSlash].npmAuthToken;
|
|
84
|
+
console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (with slash)`);
|
|
85
|
+
}
|
|
78
86
|
}
|
|
79
87
|
|
|
80
|
-
if (!token
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
if (!token) {
|
|
89
|
+
const globalConfig = globalYarnConfig.npmRegistries;
|
|
90
|
+
if (globalConfig?.[normalizedUrl]?.npmAuthToken) {
|
|
91
|
+
token = globalConfig[normalizedUrl].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[normalizedUrl].npmAuthToken;
|
|
92
|
+
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (normalized)`);
|
|
93
|
+
} else if (globalConfig?.[urlWithSlash]?.npmAuthToken) {
|
|
94
|
+
token = globalConfig[urlWithSlash].npmAuthToken.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[urlWithSlash].npmAuthToken;
|
|
95
|
+
console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (with slash)`);
|
|
96
|
+
}
|
|
83
97
|
}
|
|
84
98
|
|
|
85
99
|
console.log(`Registry ${url}: token=${token ? 'present' : 'missing'}`);
|
|
@@ -92,6 +106,9 @@ async function loadRegistries(proxyConfigPath = './.registry-proxy.yml', localYa
|
|
|
92
106
|
export async function startProxyServer(proxyConfigPath?: string, localYarnConfigPath?: string, globalYarnConfigPath?: string, port: number = 0): Promise<Server> {
|
|
93
107
|
console.log('Starting proxy server...');
|
|
94
108
|
const registries = await loadRegistries(proxyConfigPath, localYarnConfigPath, globalYarnConfigPath);
|
|
109
|
+
registries.forEach(({ url, token }) => {
|
|
110
|
+
console.log(`Registry: ${url}, Token: ${token ? 'present' : 'missing'}`);
|
|
111
|
+
});
|
|
95
112
|
|
|
96
113
|
const server = createServer(async (req, res) => {
|
|
97
114
|
if (!req.url || req.method !== 'GET') {
|
|
@@ -104,10 +121,12 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
|
|
|
104
121
|
|
|
105
122
|
const fetchPromises = registries.map(async ({ url: registry, token }) => {
|
|
106
123
|
const targetUrl = `${registry}${pathname}`;
|
|
124
|
+
console.log(`Fetching ${targetUrl} with token: ${token ? 'present' : 'none'}`);
|
|
107
125
|
try {
|
|
108
126
|
const response = await fetch(targetUrl, {
|
|
109
127
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
110
128
|
});
|
|
129
|
+
console.log(`Response from ${targetUrl}: ${response.status}`);
|
|
111
130
|
if (response.ok) return response;
|
|
112
131
|
throw new Error(`Failed: ${response.status}`);
|
|
113
132
|
} catch (e) {
|
|
@@ -148,7 +167,6 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
|
|
|
148
167
|
const addressInfo: AddressInfo = address;
|
|
149
168
|
const actualPort: number = addressInfo.port;
|
|
150
169
|
|
|
151
|
-
// 从环境变量获取项目根目录,写入端口文件
|
|
152
170
|
const projectRoot = process.env.PROJECT_ROOT || process.cwd();
|
|
153
171
|
const portFilePath = join(projectRoot, '.registry-proxy-port');
|
|
154
172
|
console.log(`Proxy server started at http://localhost:${actualPort}`);
|