/** * Simple Cloudflare Worker for testing CloudflareCache functionality * This provides real cache API access for integration tests */ import { CloudflareCache } from '../src/Cache/CloudflareCache'; export default { async fetch(request: Request, _env: any, _ctx: any): Promise { const url = new URL(request.url); // Create cache instance with proper cache access const cache = new CloudflareCache('http://localhost:8788', async () => { // In Cloudflare Workers, use caches.default or ctx.caches if available return caches.default || (globalThis as any).caches?.default; }); // CORS headers for cross-origin requests const corsHeaders = new Headers(); corsHeaders.set('Access-Control-Allow-Origin', '*'); corsHeaders.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS'); corsHeaders.set('Access-Control-Allow-Headers', 'Content-Type'); // Handle preflight requests if (request.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders }); } try { // Route handling if (url.pathname === '/cache/set' && request.method === 'POST') { const { key, value, ttl } = await request.json() as { key: string; value: any; ttl?: number }; console.log(`[DEBUG] Setting key: ${key}, value: ${JSON.stringify(value)}`); const result = await cache.set(key, value, ttl); console.log(`[DEBUG] Set result: ${result}`); const responseHeaders = new Headers(corsHeaders); responseHeaders.set('Content-Type', 'application/json'); return new Response(JSON.stringify({ success: result }), { headers: responseHeaders }); } if (url.pathname === '/cache/get' && request.method === 'POST') { const { key } = await request.json() as { key: string }; console.log(`[DEBUG] Getting key: ${key}`); const result = await cache.get(key); console.log(`[DEBUG] Get result: ${result}`); const responseHeaders = new Headers(corsHeaders); responseHeaders.set('Content-Type', 'application/json'); return new Response(JSON.stringify({ value: result, debug: { key, result } }), { headers: responseHeaders }); } if (url.pathname === '/cache/delete' && request.method === 'POST') { const { key } = await request.json() as { key: string }; const result = await cache.delete(key); const responseHeaders = new Headers(corsHeaders); responseHeaders.set('Content-Type', 'application/json'); return new Response(JSON.stringify({ success: result }), { headers: responseHeaders }); } if (url.pathname === '/cache/has' && request.method === 'POST') { const { key } = await request.json() as { key: string }; const result = await cache.has(key); const responseHeaders = new Headers(corsHeaders); responseHeaders.set('Content-Type', 'application/json'); return new Response(JSON.stringify({ exists: result }), { headers: responseHeaders }); } if (url.pathname === '/cache/test-pattern-deletion' && request.method === 'POST') { // TypeCacheKey pattern deletion test const testData = [ { key: { namespace: 'license', id: '123', context: { action: 'activation', domain: 'example.com' } }, value: { type: 'activation', license: '123' } }, { key: { namespace: 'license', id: '123', context: { action: 'validation', domain: 'example.com' } }, value: { type: 'validation', license: '123' } }, { key: { namespace: 'license', id: '123', context: { action: 'activation', domain: 'other.com' } }, value: { type: 'activation', license: '123' } }, { key: { namespace: 'license', id: '456', context: { action: 'activation', domain: 'example.com' } }, value: { type: 'activation', license: '456' } }, { key: 'other-key-string', value: { type: 'unrelated' } } ]; // Set test data for (const item of testData) { await cache.set(item.key, item.value); } // Verify all data exists const beforeDeletion = []; for (const item of testData) { const exists = await cache.has(item.key); beforeDeletion.push({ key: typeof item.key === 'string' ? item.key : `${item.key.namespace}|${item.key.id}|...`, exists }); } // Test TypeCacheKey pattern deletion - delete all license:123 entries const deleteResult = await cache.delete({ namespace: 'license', id: '123' }); // Verify results const afterDeletion = []; for (const item of testData) { const exists = await cache.has(item.key); afterDeletion.push({ key: typeof item.key === 'string' ? item.key : `${item.key.namespace}|${item.key.id}|...`, exists }); } const responseHeaders = new Headers(corsHeaders); responseHeaders.set('Content-Type', 'application/json'); return new Response(JSON.stringify({ success: true, deleteResult, beforeDeletion, afterDeletion, testDescription: 'TypeCacheKey pattern deletion: delete all license:123 entries' }), { headers: responseHeaders }); } // Health check if (url.pathname === '/health') { return new Response('OK', { headers: corsHeaders }); } // Default response return new Response('Cloudflare Cache Test Server\n\nEndpoints:\n- POST /cache/set {key, value, ttl?}\n- POST /cache/get {key}\n- POST /cache/delete {key}\n- POST /cache/has {key}\n- POST /cache/test-pattern-deletion\n- GET /health', { headers: corsHeaders }); } catch (error) { const errorHeaders = new Headers(corsHeaders); errorHeaders.set('Content-Type', 'application/json'); return new Response(JSON.stringify({ error: error instanceof Error ? error.message : 'Unknown error', stack: error instanceof Error ? error.stack : undefined }), { status: 500, headers: errorHeaders }); } } };