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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /**
* Node.js Runtime - For Node.js with framework integrations
* Provides server-side rendering with Express, Fastify, Koa support
*/
import { render } from '@coherent.js/core';
import { createServer } from 'http';
export class NodeRuntime {
constructor(options = {}) {
this.options = {
port: 3000,
host: 'localhost',
caching: true,
framework: null, // 'express', 'fastify', 'koa', or null for standalone
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.middleware = [];
this.cache = new Map();
this.renderCount = 0;
this.server = null;
// Initialize cache cleanup if caching is enabled
Eif (this.options.caching) {
this.initializeCacheCleanup();
}
}
initializeCacheCleanup() {
// Simple LRU-style cleanup
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.entries()) {
const match = this.matchPattern(pattern, pathname);
if (match) {
return { handler, params: match.params };
}
}
return null;
}
matchPattern(pattern, pathname) {
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(':')) {
params[patternPart.slice(1)] = pathPart;
} else if (patternPart !== pathPart) {
return null;
}
}
return { params };
}
// Rendering
async renderComponent(component, props = {}, options = {}) {
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)) {
return cached.html;
}
this.cache.delete(cacheKey);
}
// Render component
const vdom = resolvedComponent(props);
const html = render(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('Node render error:', error);
throw error;
}
}
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;
}
return hash.toString(36);
}
// HTTP Request handling (for standalone mode)
async handleRequest(req, res) {
try {
const url = new URL(req.url, `http://${req.headers.host}`);
const pathname = url.pathname;
// Create context
const context = {
req,
res,
url,
pathname,
params: {},
query: Object.fromEntries(url.searchParams),
method: req.method,
headers: req.headers,
runtime: this,
state: {}
};
// Execute middleware chain
let middlewareIndex = 0;
const next = async () => {
if (middlewareIndex < this.middleware.length) {
const middleware = this.middleware[middlewareIndex++];
return await middleware(context, next);
}
};
// Run middleware
if (this.middleware.length > 0) {
await next();
}
// Check if middleware already sent a response
if (res.headersSent) {
return;
}
// Find matching route
const match = this.matchRoute(pathname);
if (!match) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1>');
return;
}
// Add route params to context
context.params = match.params;
// Execute route handler
const result = await match.handler(context);
// Handle different response types
if (res.headersSent) {
return; // Handler already sent response
}
if (typeof result === 'string') {
res.writeHead(200, this.options.headers);
res.end(result);
return;
}
if (result && typeof result === 'object') {
if (result.component) {
const html = await this.renderComponent(
result.component,
result.props || {},
result.options || {}
);
res.writeHead(result.status || 200, {
...this.options.headers,
...result.headers
});
res.end(html);
return;
}
if (result.json !== undefined) {
res.writeHead(result.status || 200, {
'Content-Type': 'application/json',
...result.headers
});
res.end(JSON.stringify(result.json));
return;
}
if (result.redirect) {
res.writeHead(result.status || 302, {
'Location': result.redirect
});
res.end();
return;
}
// Default: send as JSON
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(result));
return;
}
// Fallback
res.writeHead(200, this.options.headers);
res.end(String(result));
} catch (error) {
console.error('Request handling error:', error);
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: error.message }));
}
}
}
// Create app factory
createApp() {
const app = {
// Component registration
component: (name, component) => this.registerComponent(name, component),
// 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),
// Middleware support
use: (middleware) => {
if (typeof middleware !== 'function') {
throw new Error('Middleware must be a function');
}
this.middleware.push(middleware);
return app;
},
// Server control
listen: (port, callback) => {
const serverPort = port || this.options.port;
this.server = createServer((req, res) => this.handleRequest(req, res));
this.server.listen(serverPort, this.options.host, () => {
console.log(`Coherent.js Node runtime listening on http://${this.options.host}:${serverPort}`);
if (callback) callback();
});
return this.server;
},
close: (callback) => {
if (this.server) {
this.server.close(callback);
}
},
// 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,
middlewareCount: this.middleware.length
})
};
return app;
}
// Framework integration helpers
expressMiddleware() {
return async (req, res, next) => {
req.coherent = {
render: async (component, props, options) => {
const html = await this.renderComponent(component, props, options);
res.send(html);
},
runtime: this
};
next();
};
}
fastifyPlugin() {
return async (fastify) => {
fastify.decorate('coherent', {
render: async (component, props, renderOptions) => {
return await this.renderComponent(component, props, renderOptions);
},
runtime: this
});
};
}
koaMiddleware() {
return async (ctx, next) => {
ctx.coherent = {
render: async (component, props, options) => {
const html = await this.renderComponent(component, props, options);
ctx.type = 'html';
ctx.body = html;
},
runtime: this
};
await next();
};
}
}
/**
* Create a Node.js runtime instance
*/
export function createNodeRuntime(options = {}) {
return new NodeRuntime(options);
}
export default NodeRuntime;
|