All files / coherent.js/packages/runtime/src/runtimes edge.js

25.66% Statements 97/378
100% Branches 3/3
8.57% Functions 3/35
25.66% Lines 97/378

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 4261x 1x 1x 1x       1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   1x 2x 2x 2x           2x 2x 2x   1x 1x         1x       1x 1x       1x                   1x                                                             1x 1x                                                                                                 1x           1x                     1x 1x                                                                                                                               1x                   1x                   1x                                               1x 1x                                                                               1x 1x                   1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x           2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x           2x   2x 2x   1x 1x         1x                             1x   1x   1x               1x           1x                
/**
 * Edge Runtime - For Cloudflare Workers, Deno, Bun, and other edge environments
 * Provides server-side rendering without Node.js dependencies
 */
 
import { renderToString } from '@coherentjs/core';
 
export class EdgeRuntime {
  constructor(options = {}) {
    this.options = {
      caching: true,
      streaming: false,
      headers: {
        'Content-Type': 'text/html; charset=utf-8',
        'Cache-Control': 'public, max-age=3600',
        ...options.headers
      },
      ...options
    };
    
    this.componentRegistry = new Map();
    this.routeRegistry = new Map();
    this.cache = new Map();
    this.renderCount = 0;
    
    // Initialize cache cleanup if caching is enabled
    if (this.options.caching) {
      this.initializeCacheCleanup();
    }
  }
 
  initializeCacheCleanup() {
    // Simple LRU-style cleanup
    if (typeof setInterval !== 'undefined') {
      setInterval(() => {
        if (this.cache.size > 1000) {
          const entries = Array.from(this.cache.entries());
          const toDelete = entries.slice(0, entries.length - 500);
          toDelete.forEach(([key]) => this.cache.delete(key));
        }
      }, 300000); // Every 5 minutes
    }
  }
 
  // Component management
  registerComponent(name, component) {
    this.componentRegistry.set(name, component);
    return component;
  }
 
  getComponent(name) {
    return this.componentRegistry.get(name);
  }
 
  // Route management
  addRoute(pattern, handler) {
    this.routeRegistry.set(pattern, handler);
  }
 
  matchRoute(pathname) {
    for (const [pattern, handler] of this.routeRegistry) {
      const match = this.matchPattern(pattern, pathname);
      if (match) {
        return { handler, params: match.params };
      }
    }
    return null;
  }
 
  matchPattern(pattern, pathname) {
    // Simple pattern matching - supports :param and * wildcards
    if (pattern === '*' || pattern === pathname) {
      return { params: {} };
    }
 
    const patternParts = pattern.split('/').filter(Boolean);
    const pathParts = pathname.split('/').filter(Boolean);
 
    if (patternParts.length !== pathParts.length) {
      return null;
    }
 
    const params = {};
    
    for (let i = 0; i < patternParts.length; i++) {
      const patternPart = patternParts[i];
      const pathPart = pathParts[i];
 
      if (patternPart.startsWith(':')) {
        // Parameter
        params[patternPart.slice(1)] = pathPart;
      } else if (patternPart !== pathPart) {
        // Literal mismatch
        return null;
      }
    }
 
    return { params };
  }
 
  // Rendering
  async renderComponent(component, props = {}, options = {}) {
    const _startTime = Date.now();
    
    try {
      // Resolve component
      const resolvedComponent = typeof component === 'string' 
        ? this.getComponent(component) 
        : component;
 
      if (!resolvedComponent) {
        throw new Error(`Component not found: ${component}`);
      }
 
      // Check cache
      const cacheKey = this.generateCacheKey(resolvedComponent, props);
      if (this.options.caching && this.cache.has(cacheKey)) {
        const cached = this.cache.get(cacheKey);
        if (Date.now() - cached.timestamp < (options.cacheMaxAge || 300000)) { // 5 min default
          return cached.html;
        }
        this.cache.delete(cacheKey);
      }
 
      // Render component
      const vdom = resolvedComponent(props);
      const html = renderToString(vdom);
 
      // Cache result
      if (this.options.caching && options.cacheable !== false) {
        this.cache.set(cacheKey, {
          html,
          timestamp: Date.now()
        });
      }
 
      this.renderCount++;
      
      return html;
    } catch (error) {
      console.error('Edge render error:', error);
      throw error;
    } finally {
      // Track render time
      if (typeof performance !== 'undefined' && performance.mark) {
        performance.mark(`coherent-edge-render-${Date.now()}`);
      }
    }
  }
 
  generateCacheKey(component, props) {
    const componentName = component.name || 'anonymous';
    const propsHash = this.hashObject(props);
    return `${componentName}-${propsHash}`;
  }
 
  hashObject(obj) {
    const str = JSON.stringify(obj, Object.keys(obj).sort());
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
      const char = str.charCodeAt(i);
      hash = ((hash << 5) - hash) + char;
      hash = hash & hash; // Convert to 32-bit integer
    }
    return hash.toString(36);
  }
 
  // HTTP Request handling
  async handleRequest(request) {
    try {
      const url = new URL(request.url);
      const pathname = url.pathname;
      
      // Find matching route
      const match = this.matchRoute(pathname);
      
      if (!match) {
        return this.createErrorResponse(404, 'Not Found');
      }
 
      // Execute route handler
      const context = {
        request,
        url,
        pathname,
        params: match.params,
        searchParams: Object.fromEntries(url.searchParams),
        method: request.method,
        headers: Object.fromEntries(request.headers),
        runtime: this
      };
 
      const result = await match.handler(context);
      
      // Handle different response types
      if (result instanceof Response) {
        return result;
      }
 
      if (typeof result === 'string') {
        return this.createHtmlResponse(result);
      }
 
      if (result && typeof result === 'object') {
        if (result.component) {
          // Render component
          const html = await this.renderComponent(
            result.component, 
            result.props || {}, 
            result.options || {}
          );
          return this.createHtmlResponse(html, result.status, result.headers);
        }
 
        if (result.json) {
          return this.createJsonResponse(result.json, result.status, result.headers);
        }
 
        if (result.redirect) {
          return Response.redirect(result.redirect, result.status || 302);
        }
      }
 
      // Default JSON response
      return this.createJsonResponse(result);
 
    } catch (error) {
      console.error('Request handling error:', error);
      return this.createErrorResponse(500, 'Internal Server Error');
    }
  }
 
  createHtmlResponse(html, status = 200, customHeaders = {}) {
    return new Response(html, {
      status,
      headers: {
        ...this.options.headers,
        ...customHeaders
      }
    });
  }
 
  createJsonResponse(data, status = 200, customHeaders = {}) {
    return new Response(JSON.stringify(data), {
      status,
      headers: {
        'Content-Type': 'application/json',
        ...customHeaders
      }
    });
  }
 
  createErrorResponse(status, message) {
    const errorHtml = `
      <!DOCTYPE html>
      <html>
        <head>
          <title>Error ${status}</title>
          <style>
            body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
            h1 { color: #e74c3c; }
          </style>
        </head>
        <body>
          <h1>Error ${status}</h1>
          <p>${message}</p>
        </body>
      </html>
    `;
    
    return new Response(errorHtml, {
      status,
      headers: { 'Content-Type': 'text/html; charset=utf-8' }
    });
  }
 
  // API Routes
  async handleApiRequest(request, handler) {
    try {
      const url = new URL(request.url);
      const context = {
        request,
        url,
        method: request.method,
        headers: Object.fromEntries(request.headers),
        searchParams: Object.fromEntries(url.searchParams),
        runtime: this
      };
 
      // Parse body for POST/PUT requests
      if (request.method === 'POST' || request.method === 'PUT') {
        const contentType = request.headers.get('content-type') || '';
        
        if (contentType.includes('application/json')) {
          context.body = await request.json();
        } else if (contentType.includes('application/x-www-form-urlencoded')) {
          const formData = await request.formData();
          context.body = Object.fromEntries(formData);
        } else {
          context.body = await request.text();
        }
      }
 
      const result = await handler(context);
      
      if (result instanceof Response) {
        return result;
      }
      
      return this.createJsonResponse(result);
      
    } catch (error) {
      console.error('API request error:', error);
      return this.createJsonResponse({ error: error.message }, 500);
    }
  }
 
  // Streaming support (for environments that support it)
  async renderStream(component, props = {}) {
    if (!this.options.streaming) {
      return await this.renderComponent(component, props);
    }
 
    // TODO: Implement streaming rendering
    // This would require chunked rendering of the component tree
    return await this.renderComponent(component, props);
  }
 
  // Create app factory
  createApp() {
    const app = {
      // Component registration
      component: (name, component, opts) => this.registerComponent(name, component, opts),
      
      // Routing
      get: (pattern, handler) => this.addRoute(pattern, handler),
      post: (pattern, handler) => this.addRoute(pattern, handler),
      put: (pattern, handler) => this.addRoute(pattern, handler),
      delete: (pattern, handler) => this.addRoute(pattern, handler),
      route: (pattern, handler) => this.addRoute(pattern, handler),
      
      // API endpoints
      api: (pattern, handler) => {
        this.addRoute(pattern, (context) => this.handleApiRequest(context.request, handler));
      },
      
      // Static file serving (basic)
      static: (pattern) => {
        this.addRoute(pattern, async () => {
          // Basic static file handling
          // In a real implementation, this would serve actual files
          return this.createErrorResponse(404, 'Static file serving not implemented');
        });
      },
      
      // Middleware (simplified)
      use: (_middleware) => {
        // TODO: Implement middleware support
        console.warn('Middleware not yet implemented in EdgeRuntime');
      },
      
      // Request handler
      fetch: (request) => this.handleRequest(request),
      
      // Utilities
      render: (component, props, options) => this.renderComponent(component, props, options),
      getRuntime: () => this,
      getStats: () => ({
        renderCount: this.renderCount,
        cacheSize: this.cache.size,
        componentCount: this.componentRegistry.size,
        routeCount: this.routeRegistry.size
      })
    };
 
    return app;
  }
 
  // Static factory methods
  static createApp(options = {}) {
    const runtime = new EdgeRuntime(options);
    return runtime.createApp(options);
  }
 
  static async handleRequest(request, components = {}, routes = {}) {
    const runtime = new EdgeRuntime();
    
    // Register components
    Object.entries(components).forEach(([name, component]) => {
      runtime.registerComponent(name, component);
    });
    
    // Register routes
    Object.entries(routes).forEach(([pattern, handler]) => {
      runtime.addRoute(pattern, handler);
    });
    
    return await runtime.handleRequest(request);
  }
}
 
// Helper functions for common edge runtime patterns
 
export function createCloudflareWorker(app) {
  return {
    async fetch(request, _env, _ctx) {
      return await app.fetch(request);
    }
  };
}
 
export function createDenoHandler(app) {
  return async (request) => {
    return await app.fetch(request);
  };
}
 
export function createBunHandler(app) {
  return {
    async fetch(request) {
      return await app.fetch(request);
    },
    port: 3000
  };
}