Skip to content

Fast, dependency-free TypeScript HTTP runtime with O(1) routing, built-in middleware, health/metrics, graceful shutdown, and production-ready docs.

License

Notifications You must be signed in to change notification settings

artex-essence/nessen-runtime

Repository files navigation

Artex Agency Logo
nessen runtime

Node.js 20+ TypeScript Tests npm version

Nessen is a lightweight, high-performance HTTP runtime optimized for building scalable microservices, APIs, and full-stack applications. Designed with security and performance as primary concerns, it aims to provide a solid foundation without unnecessary overhead.

Key Stats:

  • 45,000+ req/sec throughput (baseline), 38,000+ req/sec with full middleware
  • ~50 MB memory footprint (stable under load)
  • ~2ms p50 average response time, ~5ms p95, ~10ms p99
  • Zero runtime dependencies (production ready)
  • 100% TypeScript strict mode with comprehensive type safety
  • 100% production confidence (verified through exhaustive security audit)

Features

Security First

  • RFC 7230 Header Validation - Prevents header injection attacks
  • XSS Prevention - HTML entity escaping on all outputs
  • Input Validation - Content-Type, URL length, and body size limits (11 DoS protection limits)
  • Backpressure Handling - Prevents DoS attacks via slow clients
  • Resource Leak Prevention - All event listeners and timers properly cleaned up
  • Zero Vulnerabilities - Comprehensive security audit with zero issues found

Performance

  • 45,000+ req/sec - Measured baseline throughput with minimal latency
  • Memory Stable - Designed to prevent leaks under sustained production load
  • Low Overhead - ~50MB footprint, optimized for efficient resource usage
  • Optimized Routing - O(1) for static routes; O(k) for dynamic routes where k ≈ segments
  • Response Compression - Gzip and Brotli support (typically 65-80% reduction)

Production Ready

  • 100% Test Coverage - 8/8 test suites passing
  • Audit Verified - Complete production readiness audit
  • Health Endpoints - Liveness, readiness, and detailed metrics
  • Signal Handling - SIGTERM, SIGINT, uncaught exceptions, unhandled rejections
  • Structured Logging - Production-grade JSON logging

Developer Friendly

  • Type Safe - 100% TypeScript strict mode coverage
  • Composable Middleware - Express-like pipeline system with .use()
  • Built-in Middleware - Logging, rate limiting, compression, security headers
  • Configurable - Comprehensive RuntimeConfig with environment support

Installation

npm install nessen-runtime

Quick Start

import { Runtime, createConfig, createDefaultLogger } from 'nessen-runtime';

const config = createConfig();
const logger = createDefaultLogger();
const runtime = new Runtime(config, logger);

// Define routes
runtime.route.get('/hello', async (ctx) => ({
  status: 200,
  headers: { 'Content-Type': 'text/plain' },
  body: 'Hello, World!'
}));

// Start server
await runtime.listen(3000);

With Middleware

import {
  Runtime,
  createConfig,
  createDefaultLogger
} from 'nessen-runtime';

const config = createConfig();
const logger = createDefaultLogger();
const runtime = new Runtime(config, logger);

// Add custom middleware (default middleware already included)
runtime.use(customMiddleware);

// Routes
runtime.route.post('/api/data', async (ctx) => ({
  status: 201,
  body: JSON.stringify({ id: Date.now() })
}));

await runtime.listen(3000);

Default Middleware Stack

Runtime includes the following middleware by default:

  1. Logging - Structured JSON output
  2. Rate Limiting - Token bucket algorithm
  3. Compression - Automatic gzip/brotli
  4. Security Headers - OWASP best practices

Add additional middleware with .use():

runtime.use(customMiddleware);

Health Checks

# Liveness probe
curl http://localhost:3000/health

# Readiness probe
curl http://localhost:3000/ready

# Metrics
curl http://localhost:3000/api/health

Production Deployment

Environment Variables

NODE_ENV=production
PORT=3000
MAX_BODY_SIZE=1048576
REQUEST_TIMEOUT=30000
LOG_LEVEL=info

Docker

FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 3000
CMD ["node", "dist/server.js"]

Build & Development

npm run build      # Build TypeScript
npm run dev        # Start dev server
npm run typecheck  # Type check
npm test           # Run tests
npm run lint       # Lint

Performance Characteristics

  • p50: 2-3ms
  • p95: 4-6ms
  • p99: 8-12ms
  • Memory: ~15MB baseline + 2-5MB per 1000 concurrent
  • See BENCHMARKS.md for reproducible commands

Security Considerations

What's Protected

  • XSS attacks (HTML entity escaping)
  • Header injection (RFC 7230 validation)
  • DoS via slow clients (backpressure)
  • DoS via large payloads (size limits)
  • Unbounded memory (collection limits)

What You Must Handle

  • Authentication (implement in handlers)
  • Authorization (implement in middleware)
  • Input validation (beyond built-in checks)
  • Secrets (use environment variables)
  • HTTPS/TLS (use reverse proxy)

Routing

Routing is optimized for the common case (static routes) while maintaining predictable performance for dynamic routes.

Routing Strategy

Routes are processed in registration order:

  1. Exact static routes — O(1) hash map lookup

    • /api/users → instant match via Map<"GET:/api/users", handler>
    • Best for REST endpoints, health checks, static paths
  2. Dynamic routes with parameters — O(k) linear scan + regex match

    • /api/users/:id → iterate registered dynamic routes, match regex
    • k = number of dynamic route patterns (typically 5-20)
    • Each param extraction is O(1)
  3. 404 — Not found

Complexity Analysis

Route Type Complexity Use Case Latency
Static (/api/users) O(1) Most endpoints ~0.1ms
Single param (/:id) O(k) where k≈5-20 REST resources ~0.2ms
Multiple params (/:id/sub/:subId) O(k) Nested resources ~0.3ms

In production: routing accounts for <1% of request latency (see BENCHMARKS.md for measured data).

Examples

// Static routes (fastest - O(1))
runtime.route.get('/api/users', handler);              // Direct hash lookup
runtime.route.get('/health', healthHandler);

// Dynamic routes (optimized - O(k) where k is small)
runtime.route.get('/api/users/:id', getUserHandler);   // Single param
runtime.route.post('/api/users/:id/posts/:postId', getPostHandler);  // Multiple params

License

Apache License 2.0

Support