Add npm registry URL rewriting and improve formatting
Introduces logic to rewrite npm registry tarball URLs in JSON responses to route through the proxy endpoint, ensuring package names are preserved. Also improves code formatting and consistency across source and test files, and adds related test cases for npm URL rewriting.
This commit is contained in:
1 parent
31e25a1c6f
commit
b147ea7052
14 files changed
+867
-753
No files matched your search
+40
-50
@@ -26,13 +26,13 @@ let testMetrics = {
|
||||
beforeAll(async () => {
|
||||
console.log('🚀 Starting Xget test suite...');
|
||||
testStartTime = Date.now();
|
||||
|
||||
|
||||
// Initialize test environment
|
||||
await setupTestEnvironment();
|
||||
|
||||
|
||||
// Verify test dependencies
|
||||
await verifyTestDependencies();
|
||||
|
||||
|
||||
console.log('✅ Test environment initialized');
|
||||
});
|
||||
|
||||
@@ -41,29 +41,29 @@ beforeAll(async () => {
|
||||
*/
|
||||
afterAll(async () => {
|
||||
const duration = Date.now() - testStartTime;
|
||||
|
||||
|
||||
console.log('\n📊 Test Suite Summary:');
|
||||
console.log(` Duration: ${duration}ms`);
|
||||
console.log(` Total: ${testMetrics.totalTests}`);
|
||||
console.log(` Passed: ${testMetrics.passedTests}`);
|
||||
console.log(` Failed: ${testMetrics.failedTests}`);
|
||||
console.log(` Skipped: ${testMetrics.skippedTests}`);
|
||||
|
||||
|
||||
// Cleanup test environment
|
||||
await cleanupTestEnvironment();
|
||||
|
||||
|
||||
console.log('🏁 Test suite completed');
|
||||
});
|
||||
|
||||
/**
|
||||
* Setup before each test
|
||||
*/
|
||||
beforeEach(async (context) => {
|
||||
beforeEach(async context => {
|
||||
testMetrics.totalTests++;
|
||||
|
||||
|
||||
// Reset any global state
|
||||
resetGlobalState();
|
||||
|
||||
|
||||
// Setup test-specific environment
|
||||
await setupTestCase(context);
|
||||
});
|
||||
@@ -71,7 +71,7 @@ beforeEach(async (context) => {
|
||||
/**
|
||||
* Cleanup after each test
|
||||
*/
|
||||
afterEach(async (context) => {
|
||||
afterEach(async context => {
|
||||
// Update test metrics based on result
|
||||
if (context.meta?.result === 'pass') {
|
||||
testMetrics.passedTests++;
|
||||
@@ -80,7 +80,7 @@ afterEach(async (context) => {
|
||||
} else if (context.meta?.result === 'skip') {
|
||||
testMetrics.skippedTests++;
|
||||
}
|
||||
|
||||
|
||||
// Cleanup test-specific resources
|
||||
await cleanupTestCase(context);
|
||||
});
|
||||
@@ -93,17 +93,17 @@ async function setupTestEnvironment() {
|
||||
if (typeof globalThis.fetch === 'undefined') {
|
||||
throw new Error('fetch is not available in test environment');
|
||||
}
|
||||
|
||||
|
||||
// Setup global test utilities
|
||||
globalThis.TEST_CONFIG = TEST_CONFIG;
|
||||
|
||||
|
||||
// Initialize performance monitoring
|
||||
if (typeof performance === 'undefined') {
|
||||
globalThis.performance = {
|
||||
now: () => Date.now()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Setup console overrides for testing
|
||||
setupConsoleOverrides();
|
||||
}
|
||||
@@ -112,20 +112,14 @@ async function setupTestEnvironment() {
|
||||
* Verify test dependencies
|
||||
*/
|
||||
async function verifyTestDependencies() {
|
||||
const requiredGlobals = [
|
||||
'Request',
|
||||
'Response',
|
||||
'Headers',
|
||||
'URL',
|
||||
'URLSearchParams'
|
||||
];
|
||||
|
||||
const requiredGlobals = ['Request', 'Response', 'Headers', 'URL', 'URLSearchParams'];
|
||||
|
||||
for (const global of requiredGlobals) {
|
||||
if (typeof globalThis[global] === 'undefined') {
|
||||
throw new Error(`Required global ${global} is not available`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Verify SELF is available for Cloudflare Workers testing
|
||||
try {
|
||||
const { SELF } = await import('cloudflare:test');
|
||||
@@ -142,17 +136,17 @@ async function verifyTestDependencies() {
|
||||
*/
|
||||
function setupConsoleOverrides() {
|
||||
const originalConsole = { ...console };
|
||||
|
||||
|
||||
// Store original console methods
|
||||
globalThis.originalConsole = originalConsole;
|
||||
|
||||
|
||||
// Override console methods for testing
|
||||
console.warn = (...args) => {
|
||||
if (process.env.NODE_ENV !== 'test' || process.env.VERBOSE_TESTS) {
|
||||
originalConsole.warn(...args);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
console.log = (...args) => {
|
||||
if (process.env.NODE_ENV !== 'test' || process.env.VERBOSE_TESTS) {
|
||||
originalConsole.log(...args);
|
||||
@@ -168,7 +162,7 @@ function resetGlobalState() {
|
||||
if (globalThis.testCache) {
|
||||
globalThis.testCache.clear();
|
||||
}
|
||||
|
||||
|
||||
// Reset performance counters
|
||||
if (globalThis.testPerformance) {
|
||||
globalThis.testPerformance.reset();
|
||||
@@ -181,11 +175,11 @@ function resetGlobalState() {
|
||||
async function setupTestCase(context) {
|
||||
// Create test-specific cache
|
||||
globalThis.testCache = new Map();
|
||||
|
||||
|
||||
// Setup test-specific performance monitoring
|
||||
globalThis.testPerformance = {
|
||||
marks: new Map(),
|
||||
mark: (name) => {
|
||||
mark: name => {
|
||||
globalThis.testPerformance.marks.set(name, performance.now());
|
||||
},
|
||||
measure: (name, startMark, endMark) => {
|
||||
@@ -197,7 +191,7 @@ async function setupTestCase(context) {
|
||||
globalThis.testPerformance.marks.clear();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Mark test start time
|
||||
globalThis.testPerformance.mark('test-start');
|
||||
}
|
||||
@@ -208,22 +202,18 @@ async function setupTestCase(context) {
|
||||
async function cleanupTestCase(context) {
|
||||
// Mark test end time
|
||||
globalThis.testPerformance.mark('test-end');
|
||||
|
||||
|
||||
// Log test performance if verbose
|
||||
if (process.env.VERBOSE_TESTS) {
|
||||
const duration = globalThis.testPerformance.measure(
|
||||
'test-duration',
|
||||
'test-start',
|
||||
'test-end'
|
||||
);
|
||||
const duration = globalThis.testPerformance.measure('test-duration', 'test-start', 'test-end');
|
||||
console.log(`Test "${context.meta?.name}" took ${duration.toFixed(2)}ms`);
|
||||
}
|
||||
|
||||
|
||||
// Cleanup test-specific resources
|
||||
if (globalThis.testCache) {
|
||||
globalThis.testCache.clear();
|
||||
}
|
||||
|
||||
|
||||
if (globalThis.testPerformance) {
|
||||
globalThis.testPerformance.reset();
|
||||
}
|
||||
@@ -238,7 +228,7 @@ async function cleanupTestEnvironment() {
|
||||
Object.assign(console, globalThis.originalConsole);
|
||||
delete globalThis.originalConsole;
|
||||
}
|
||||
|
||||
|
||||
// Cleanup global test utilities
|
||||
delete globalThis.TEST_CONFIG;
|
||||
delete globalThis.testCache;
|
||||
@@ -252,49 +242,49 @@ globalThis.testUtils = {
|
||||
/**
|
||||
* Create a test timeout
|
||||
*/
|
||||
timeout: (ms) => new Promise((_, reject) => {
|
||||
setTimeout(() => reject(new Error(`Test timed out after ${ms}ms`)), ms);
|
||||
}),
|
||||
|
||||
timeout: ms =>
|
||||
new Promise((_, reject) => {
|
||||
setTimeout(() => reject(new Error(`Test timed out after ${ms}ms`)), ms);
|
||||
}),
|
||||
|
||||
/**
|
||||
* Wait for a condition to be true
|
||||
*/
|
||||
waitFor: async (condition, timeout = 5000, interval = 100) => {
|
||||
const start = Date.now();
|
||||
|
||||
|
||||
while (Date.now() - start < timeout) {
|
||||
if (await condition()) {
|
||||
return true;
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, interval));
|
||||
}
|
||||
|
||||
|
||||
throw new Error(`Condition not met within ${timeout}ms`);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Retry a function with exponential backoff
|
||||
*/
|
||||
retry: async (fn, maxRetries = 3, baseDelay = 100) => {
|
||||
let lastError;
|
||||
|
||||
|
||||
for (let i = 0; i < maxRetries; i++) {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
|
||||
|
||||
if (i < maxRetries - 1) {
|
||||
const delay = baseDelay * Math.pow(2, i);
|
||||
await new Promise(resolve => setTimeout(resolve, delay));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
};
|
||||
|
||||
// Export test configuration for use in other files
|
||||
export { TEST_CONFIG, testMetrics };
|
||||
|
||||
Reference in new issue
Block a user