com.jimuwd.xian.registry-proxy 1.0.6 → 1.0.7

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
@@ -58,14 +58,32 @@ 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
61
62
  const normalizedUrl = normalizeUrl(url);
62
- if (!token && localYarnConfig.npmRegistries?.[normalizedUrl] && 'npmAuthToken' in localYarnConfig.npmRegistries[normalizedUrl]) {
63
- token = localYarnConfig.npmRegistries[normalizedUrl].npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localYarnConfig.npmRegistries[normalizedUrl].npmAuthToken;
64
- console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config`);
63
+ const urlWithSlash = normalizedUrl + '/';
64
+ if (!token) {
65
+ // 检查本地 Yarn 配置
66
+ const localConfig = localYarnConfig.npmRegistries;
67
+ if (localConfig?.[normalizedUrl] && 'npmAuthToken' in localConfig[normalizedUrl]) {
68
+ token = localConfig[normalizedUrl].npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[normalizedUrl].npmAuthToken;
69
+ console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (normalized)`);
70
+ }
71
+ else if (localConfig?.[urlWithSlash] && 'npmAuthToken' in localConfig[urlWithSlash]) {
72
+ token = localConfig[urlWithSlash].npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[urlWithSlash].npmAuthToken;
73
+ console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (with slash)`);
74
+ }
65
75
  }
66
- if (!token && globalYarnConfig.npmRegistries?.[normalizedUrl] && 'npmAuthToken' in globalYarnConfig.npmRegistries[normalizedUrl]) {
67
- token = globalYarnConfig.npmRegistries[normalizedUrl].npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalYarnConfig.npmRegistries[normalizedUrl].npmAuthToken;
68
- console.log(`Token for ${url} not found in local Yarn config, using global Yarn config`);
76
+ if (!token) {
77
+ // 检查全局 Yarn 配置
78
+ const globalConfig = globalYarnConfig.npmRegistries;
79
+ if (globalConfig?.[normalizedUrl] && 'npmAuthToken' in globalConfig[normalizedUrl]) {
80
+ token = globalConfig[normalizedUrl].npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[normalizedUrl].npmAuthToken;
81
+ console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (normalized)`);
82
+ }
83
+ else if (globalConfig?.[urlWithSlash] && 'npmAuthToken' in globalConfig[urlWithSlash]) {
84
+ token = globalConfig[urlWithSlash].npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[urlWithSlash].npmAuthToken;
85
+ console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (with slash)`);
86
+ }
69
87
  }
70
88
  console.log(`Registry ${url}: token=${token ? 'present' : 'missing'}`);
71
89
  return { url, token };
@@ -125,7 +143,6 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
125
143
  }
126
144
  const addressInfo = address;
127
145
  const actualPort = addressInfo.port;
128
- // 从环境变量获取项目根目录,写入端口文件
129
146
  const projectRoot = process.env.PROJECT_ROOT || process.cwd();
130
147
  const portFilePath = join(projectRoot, '.registry-proxy-port');
131
148
  console.log(`Proxy server started at http://localhost:${actualPort}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { createServer, Server } from 'http';
3
- import { AddressInfo } from 'net'; // 导入 AddressInfo 类型
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';
@@ -71,15 +71,32 @@ 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
74
75
  const normalizedUrl = normalizeUrl(url);
75
- if (!token && localYarnConfig.npmRegistries?.[normalizedUrl] && 'npmAuthToken' in localYarnConfig.npmRegistries[normalizedUrl]) {
76
- token = localYarnConfig.npmRegistries[normalizedUrl]!.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localYarnConfig.npmRegistries[normalizedUrl]!.npmAuthToken;
77
- console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config`);
76
+ const urlWithSlash = normalizedUrl + '/';
77
+
78
+ if (!token) {
79
+ // 检查本地 Yarn 配置
80
+ const localConfig = localYarnConfig.npmRegistries;
81
+ if (localConfig?.[normalizedUrl] && 'npmAuthToken' in localConfig[normalizedUrl]) {
82
+ token = localConfig[normalizedUrl]!.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[normalizedUrl]!.npmAuthToken;
83
+ console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (normalized)`);
84
+ } else if (localConfig?.[urlWithSlash] && 'npmAuthToken' in localConfig[urlWithSlash]) {
85
+ token = localConfig[urlWithSlash]!.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || localConfig[urlWithSlash]!.npmAuthToken;
86
+ console.log(`Token for ${url} not found in ${resolvedProxyPath}, using local Yarn config (with slash)`);
87
+ }
78
88
  }
79
89
 
80
- if (!token && globalYarnConfig.npmRegistries?.[normalizedUrl] && 'npmAuthToken' in globalYarnConfig.npmRegistries[normalizedUrl]) {
81
- token = globalYarnConfig.npmRegistries[normalizedUrl]!.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalYarnConfig.npmRegistries[normalizedUrl]!.npmAuthToken;
82
- console.log(`Token for ${url} not found in local Yarn config, using global Yarn config`);
90
+ if (!token) {
91
+ // 检查全局 Yarn 配置
92
+ const globalConfig = globalYarnConfig.npmRegistries;
93
+ if (globalConfig?.[normalizedUrl] && 'npmAuthToken' in globalConfig[normalizedUrl]) {
94
+ token = globalConfig[normalizedUrl]!.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[normalizedUrl]!.npmAuthToken;
95
+ console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (normalized)`);
96
+ } else if (globalConfig?.[urlWithSlash] && 'npmAuthToken' in globalConfig[urlWithSlash]) {
97
+ token = globalConfig[urlWithSlash]!.npmAuthToken?.replace(/\${(.+)}/, (_, key) => process.env[key] || '') || globalConfig[urlWithSlash]!.npmAuthToken;
98
+ console.log(`Token for ${url} not found in local Yarn config, using global Yarn config (with slash)`);
99
+ }
83
100
  }
84
101
 
85
102
  console.log(`Registry ${url}: token=${token ? 'present' : 'missing'}`);
@@ -148,7 +165,6 @@ export async function startProxyServer(proxyConfigPath?: string, localYarnConfig
148
165
  const addressInfo: AddressInfo = address;
149
166
  const actualPort: number = addressInfo.port;
150
167
 
151
- // 从环境变量获取项目根目录,写入端口文件
152
168
  const projectRoot = process.env.PROJECT_ROOT || process.cwd();
153
169
  const portFilePath = join(projectRoot, '.registry-proxy-port');
154
170
  console.log(`Proxy server started at http://localhost:${actualPort}`);