Refactor request handling and improve Docker detection

Refactored the main request handler in src/index.js for improved readability and error handling. Enhanced Docker request detection in validation.js to include additional Content-Type checks. Updated tests to increase timeouts and expand expected status codes for container registry platforms.
This commit is contained in:
xixu-me committed 2025-12-10 15:47:25 +08:00
1 parent 1c20e7af4f
commit 214e596d00
5 files changed
+618 -496

No files matched your search

+596 -483
View File
File diff suppressed because it is too large. Load diff
+10 -1
View File
@@ -22,7 +22,7 @@ import { isGitLFSRequest, isGitRequest } from '../protocols/git.js';
*/
export function isDockerRequest(request, url) {
// Check for container registry API endpoints
if (url.pathname.startsWith('/v2/')) {
if (url.pathname.includes('/v2/') || url.pathname === '/v2') {
return true;
}
@@ -42,6 +42,15 @@ export function isDockerRequest(request, url) {
return true;
}
// Check for Docker-specific Content-Type headers (for PUT/POST)
const contentType = request.headers.get('Content-Type') || '';
if (
contentType.includes('application/vnd.docker.distribution.manifest') ||
contentType.includes('application/vnd.oci.image.manifest')
) {
return true;
}
return false;
}
+1 -1
View File
@@ -240,7 +240,7 @@ describe('Security Features', () => {
it('should provide generic error messages', async () => {
const response = await SELF.fetch('https://example.com/gh/test/repo', {
method: 'INVALID',
method: 'TRACE',
redirect: 'manual'
});
+3 -3
View File
@@ -42,7 +42,7 @@ describe('Integration Tests', () => {
const response = await SELF.fetch(testUrl, { method: 'HEAD' });
expect([200, 301, 302, 404]).toContain(response.status);
});
}, 10000);
it('should handle npm package requests', async () => {
const testUrl = 'https://example.com/npm/react';
@@ -229,7 +229,7 @@ describe('Integration Tests', () => {
});
// Git requests should not be cached (no cache headers)
expect(response.headers.get('Cache-Control')).not.toContain('max-age=1800');
expect(response.headers.get('Cache-Control') || '').not.toContain('max-age=1800');
});
});
@@ -297,7 +297,7 @@ describe('Integration Tests', () => {
const response = await SELF.fetch(url, { method: 'HEAD' });
expect(response.headers.get('X-Performance-Metrics')).toBeTruthy();
}
});
}, 10000);
});
describe('Content Type Handling', () => {
+8 -8
View File
@@ -75,7 +75,7 @@ describe('Container Registry Support', () => {
// Should attempt to proxy auth requests
expect(response.status).not.toBe(400);
});
}, 15000);
it('should transform scope parameter correctly for Docker Hub', async () => {
// Test that scope parameter removes Xget path prefix
@@ -237,24 +237,24 @@ describe('Container Registry Support', () => {
describe('Container Registry Platform Support', () => {
const containerRegistries = [
{ name: 'Docker Hub', prefix: 'cr/docker', expectedStatus: [200, 301, 302, 401, 404] },
{ name: 'Quay.io', prefix: 'cr/quay', expectedStatus: [200, 301, 302, 401, 404] },
{ name: 'Docker Hub', prefix: 'cr/docker', expectedStatus: [200, 301, 302, 401, 404, 429] },
{ name: 'Quay.io', prefix: 'cr/quay', expectedStatus: [200, 301, 302, 401, 404, 429] },
{
name: 'Google Container Registry',
prefix: 'cr/gcr',
expectedStatus: [200, 301, 302, 401, 404]
expectedStatus: [200, 301, 302, 401, 404, 429]
},
{
name: 'Microsoft Container Registry',
prefix: 'cr/mcr',
expectedStatus: [200, 301, 302, 401, 404]
expectedStatus: [200, 301, 302, 401, 404, 429]
},
{
name: 'GitHub Container Registry',
prefix: 'cr/ghcr',
expectedStatus: [200, 301, 302, 401, 404]
expectedStatus: [200, 301, 302, 401, 404, 429]
},
{ name: 'Amazon ECR Public', prefix: 'cr/ecr', expectedStatus: [200, 301, 302, 401, 404] }
{ name: 'Amazon ECR Public', prefix: 'cr/ecr', expectedStatus: [200, 301, 302, 401, 404, 429] }
];
containerRegistries.forEach(({ name, prefix, expectedStatus }) => {
@@ -263,7 +263,7 @@ describe('Container Registry Support', () => {
const response = await SELF.fetch(testUrl, { method: 'HEAD' });
expect(expectedStatus).toContain(response.status);
});
}, 10000);
});
});