com.jimuwd.xian.registry-proxy 1.0.22 → 1.0.23

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
@@ -176,13 +176,15 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
176
176
  try {
177
177
  const data = await successResponse.json();
178
178
  if (data.versions) {
179
- const proxyBase = `${proxyConfig.https ? 'https' : 'http'}://${req.headers.host || 'localhost:' + proxyPort}${basePathPrefixedWithSlash}`;
179
+ console.log("Requested url is", req.url);
180
+ const proxyBaseUrlNoSuffixedWithSlash = `${proxyConfig.https ? 'https' : 'http'}://${req.headers.host || 'localhost:' + proxyPort}${basePathPrefixedWithSlash === '/' ? '' : basePathPrefixedWithSlash}`;
180
181
  for (const version in data.versions) {
181
182
  const dist = data.versions[version]?.dist;
182
183
  if (dist?.tarball) {
183
184
  const originalUrl = new URL(dist.tarball);
184
- const tarballPath = removeRegistryPrefix(dist.tarball, registries);
185
- dist.tarball = `${proxyBase}${tarballPath}${originalUrl.search || ''}`;
185
+ const originalSearchParamsStr = originalUrl.search || '';
186
+ const tarballPathPrefixedWithSlash = removeRegistryPrefix(dist.tarball, registries);
187
+ dist.tarball = `${proxyBaseUrlNoSuffixedWithSlash}${tarballPathPrefixedWithSlash}${originalSearchParamsStr}`;
186
188
  }
187
189
  }
188
190
  }
@@ -202,7 +204,7 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
202
204
  }
203
205
  const contentLength = successResponse.headers.get('Content-Length');
204
206
  const safeHeaders = {
205
- 'Content-Type': successResponse.headers.get('Content-Type'),
207
+ 'Content-Type': successResponse.headers.get('Content-Type') || undefined,
206
208
  'Content-Length': contentLength && !isNaN(Number(contentLength)) ? contentLength : undefined,
207
209
  };
208
210
  res.writeHead(successResponse.status, safeHeaders);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
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,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { createServer, Server as HttpServer } from 'http';
2
+ import {createServer, Server as HttpServer, IncomingMessage, ServerResponse, OutgoingHttpHeaders} from 'http';
3
3
  import { createServer as createHttpsServer, Server as HttpsServer } from 'https';
4
4
  import { readFileSync, promises as fsPromises } from 'fs';
5
5
  import { AddressInfo } from 'net';
@@ -165,7 +165,7 @@ export async function startProxyServer(
165
165
 
166
166
  let proxyPort: number;
167
167
 
168
- const requestHandler = async (req: any, res: any) => {
168
+ const requestHandler = async (req: IncomingMessage, res: ServerResponse) => {
169
169
  if (!req.url || !req.headers.host) {
170
170
  console.error('Invalid request: missing URL or host header');
171
171
  res.writeHead(400).end('Invalid Request');
@@ -215,13 +215,15 @@ export async function startProxyServer(
215
215
  try {
216
216
  const data = await successResponse.json() as PackageData;
217
217
  if (data.versions) {
218
- const proxyBase = `${proxyConfig.https ? 'https' : 'http'}://${req.headers.host || 'localhost:' + proxyPort}${basePathPrefixedWithSlash}`;
218
+ console.log("Requested url is", req.url);
219
+ const proxyBaseUrlNoSuffixedWithSlash = `${proxyConfig.https ? 'https' : 'http'}://${req.headers.host || 'localhost:' + proxyPort}${basePathPrefixedWithSlash==='/'?'':basePathPrefixedWithSlash}`;
219
220
  for (const version in data.versions) {
220
221
  const dist = data.versions[version]?.dist;
221
222
  if (dist?.tarball) {
222
223
  const originalUrl = new URL(dist.tarball);
223
- const tarballPath = removeRegistryPrefix(dist.tarball, registries);
224
- dist.tarball = `${proxyBase}${tarballPath}${originalUrl.search || ''}`;
224
+ const originalSearchParamsStr = originalUrl.search || '';
225
+ const tarballPathPrefixedWithSlash = removeRegistryPrefix(dist.tarball, registries);
226
+ dist.tarball = `${proxyBaseUrlNoSuffixedWithSlash}${tarballPathPrefixedWithSlash}${originalSearchParamsStr}`;
225
227
  }
226
228
  }
227
229
  }
@@ -238,8 +240,8 @@ export async function startProxyServer(
238
240
  return;
239
241
  }
240
242
  const contentLength = successResponse.headers.get('Content-Length');
241
- const safeHeaders = {
242
- 'Content-Type': successResponse.headers.get('Content-Type'),
243
+ const safeHeaders:OutgoingHttpHeaders = {
244
+ 'Content-Type': successResponse.headers.get('Content-Type')||undefined,
243
245
  'Content-Length': contentLength && !isNaN(Number(contentLength)) ? contentLength : undefined,
244
246
  };
245
247
  res.writeHead(successResponse.status, safeHeaders);