com.jimuwd.xian.registry-proxy 1.0.24 → 1.0.26
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 -19
- package/package.json +1 -1
- package/src/index.ts +24 -21
package/dist/index.js
CHANGED
|
@@ -34,42 +34,49 @@ class ConcurrencyLimiter {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
const limiter = new ConcurrencyLimiter(3);
|
|
37
|
-
function
|
|
37
|
+
function removeEndingSlashAndForceStartingSlash(str) {
|
|
38
|
+
if (!str)
|
|
39
|
+
return '/';
|
|
40
|
+
let trimmed = str.trim();
|
|
41
|
+
if (trimmed === '/')
|
|
42
|
+
return '/';
|
|
43
|
+
if (trimmed === '')
|
|
44
|
+
return '/';
|
|
45
|
+
if (!trimmed.startsWith('/'))
|
|
46
|
+
trimmed = '/' + trimmed;
|
|
47
|
+
return trimmed.replace(/\/+$/, '');
|
|
48
|
+
}
|
|
49
|
+
function normalizeUrl(httpOrHttpsUrl) {
|
|
50
|
+
if (!httpOrHttpsUrl.startsWith("http"))
|
|
51
|
+
throw new Error("http(s) url must starts with 'http(s)://'");
|
|
38
52
|
try {
|
|
39
|
-
const urlObj = new URL(
|
|
53
|
+
const urlObj = new URL(httpOrHttpsUrl);
|
|
40
54
|
if (urlObj.protocol === 'http:' && (urlObj.port === '80' || urlObj.port === '')) {
|
|
41
55
|
urlObj.port = '';
|
|
42
56
|
}
|
|
43
57
|
else if (urlObj.protocol === 'https:' && (urlObj.port === '443' || urlObj.port === '')) {
|
|
44
58
|
urlObj.port = '';
|
|
45
59
|
}
|
|
46
|
-
|
|
47
|
-
return urlObj.toString();
|
|
60
|
+
return urlObj.toString().replace(/\/+$/, '');
|
|
48
61
|
}
|
|
49
62
|
catch (e) {
|
|
50
|
-
|
|
51
|
-
return url.replace(/\/+$/, '');
|
|
63
|
+
throw new Error(`Invalid URL: ${httpOrHttpsUrl}`, { cause: e });
|
|
52
64
|
}
|
|
53
65
|
}
|
|
54
66
|
function resolvePath(path) {
|
|
55
67
|
return path.startsWith('~/') ? join(homedir(), path.slice(2)) : resolve(path);
|
|
56
68
|
}
|
|
57
69
|
function removeRegistryPrefix(tarballUrl, registries) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return normalizedTarball.slice(normalizedRegistry.length) || '/';
|
|
66
|
-
}
|
|
70
|
+
const normalizedTarball = normalizeUrl(tarballUrl);
|
|
71
|
+
const normalizedRegistries = registries
|
|
72
|
+
.map(r => normalizeUrl(r.url))
|
|
73
|
+
.sort((a, b) => b.length - a.length);
|
|
74
|
+
for (const normalizedRegistry of normalizedRegistries) {
|
|
75
|
+
if (normalizedTarball.startsWith(normalizedRegistry)) {
|
|
76
|
+
return normalizedTarball.slice(normalizedRegistry.length) || '/';
|
|
67
77
|
}
|
|
68
78
|
}
|
|
69
|
-
|
|
70
|
-
console.error(`Invalid URL in removeRegistryPrefix: ${tarballUrl}`, e);
|
|
71
|
-
}
|
|
72
|
-
return tarballUrl;
|
|
79
|
+
throw new Error(`Can't find tarball url ${tarballUrl} does not match given registries ${normalizedRegistries}`);
|
|
73
80
|
}
|
|
74
81
|
async function loadProxyConfig(proxyConfigPath = './.registry-proxy.yml') {
|
|
75
82
|
const resolvedPath = resolvePath(proxyConfigPath);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -74,19 +74,27 @@ class ConcurrencyLimiter {
|
|
|
74
74
|
|
|
75
75
|
const limiter = new ConcurrencyLimiter(3);
|
|
76
76
|
|
|
77
|
-
function
|
|
77
|
+
function removeEndingSlashAndForceStartingSlash(str: string): string {
|
|
78
|
+
if (!str) return '/';
|
|
79
|
+
let trimmed = str.trim();
|
|
80
|
+
if (trimmed === '/') return '/';
|
|
81
|
+
if (trimmed === '') return '/';
|
|
82
|
+
if (!trimmed.startsWith('/')) trimmed = '/' + trimmed;
|
|
83
|
+
return trimmed.replace(/\/+$/, '');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function normalizeUrl(httpOrHttpsUrl: string): string {
|
|
87
|
+
if (!httpOrHttpsUrl.startsWith("http")) throw new Error("http(s) url must starts with 'http(s)://'");
|
|
78
88
|
try {
|
|
79
|
-
const urlObj = new URL(
|
|
89
|
+
const urlObj = new URL(httpOrHttpsUrl);
|
|
80
90
|
if (urlObj.protocol === 'http:' && (urlObj.port === '80' || urlObj.port === '')) {
|
|
81
91
|
urlObj.port = '';
|
|
82
92
|
} else if (urlObj.protocol === 'https:' && (urlObj.port === '443' || urlObj.port === '')) {
|
|
83
93
|
urlObj.port = '';
|
|
84
94
|
}
|
|
85
|
-
|
|
86
|
-
return urlObj.toString();
|
|
95
|
+
return urlObj.toString().replace(/\/+$/, '');
|
|
87
96
|
} catch (e) {
|
|
88
|
-
|
|
89
|
-
return url.replace(/\/+$/, '');
|
|
97
|
+
throw new Error(`Invalid URL: ${httpOrHttpsUrl}`, {cause: e});
|
|
90
98
|
}
|
|
91
99
|
}
|
|
92
100
|
|
|
@@ -95,21 +103,16 @@ function resolvePath(path: string): string {
|
|
|
95
103
|
}
|
|
96
104
|
|
|
97
105
|
function removeRegistryPrefix(tarballUrl: string, registries: RegistryInfo[]): string {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (normalizedTarball.startsWith(normalizedRegistry)) {
|
|
106
|
-
return normalizedTarball.slice(normalizedRegistry.length) || '/';
|
|
107
|
-
}
|
|
106
|
+
const normalizedTarball = normalizeUrl(tarballUrl);
|
|
107
|
+
const normalizedRegistries = registries
|
|
108
|
+
.map(r => normalizeUrl(r.url))
|
|
109
|
+
.sort((a, b) => b.length - a.length);
|
|
110
|
+
for (const normalizedRegistry of normalizedRegistries) {
|
|
111
|
+
if (normalizedTarball.startsWith(normalizedRegistry)) {
|
|
112
|
+
return normalizedTarball.slice(normalizedRegistry.length) || '/';
|
|
108
113
|
}
|
|
109
|
-
} catch (e) {
|
|
110
|
-
console.error(`Invalid URL in removeRegistryPrefix: ${tarballUrl}`, e);
|
|
111
114
|
}
|
|
112
|
-
|
|
115
|
+
throw new Error(`Can't find tarball url ${tarballUrl} does not match given registries ${normalizedRegistries}`)
|
|
113
116
|
}
|
|
114
117
|
|
|
115
118
|
async function loadProxyConfig(proxyConfigPath = './.registry-proxy.yml'): Promise<ProxyConfig> {
|
|
@@ -246,8 +249,8 @@ export async function startProxyServer(
|
|
|
246
249
|
const originalSearchParamsStr = originalUrl.search || '';
|
|
247
250
|
const tarballPathPrefixedWithSlash = removeRegistryPrefix(dist.tarball, registries);
|
|
248
251
|
dist.tarball = `${proxyBaseUrlNoSuffixedWithSlash}${tarballPathPrefixedWithSlash}${originalSearchParamsStr}`;
|
|
249
|
-
console.log("tarballPathPrefixedWithSlash",tarballPathPrefixedWithSlash);
|
|
250
|
-
console.log("dist.tarballUrl",dist.tarball);
|
|
252
|
+
console.log("tarballPathPrefixedWithSlash", tarballPathPrefixedWithSlash);
|
|
253
|
+
console.log("dist.tarballUrl", dist.tarball);
|
|
251
254
|
}
|
|
252
255
|
}
|
|
253
256
|
}
|