
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
Platform-agnostic utilities for enterprise edge computing and serverless environments with production-grade features for modern web applications.
Edge-utils is a comprehensive toolkit designed specifically for edge computing and serverless environments. It provides production-ready utilities that work seamlessly across multiple platforms including Cloudflare Workers, Vercel Edge Functions, Deno Deploy, and traditional Node.js serverless functions.
Edge-utils is built with a modular architecture where you can pick and choose the features you need:
For detailed documentation on each module, see the following guides:
const { createEdgeHandler, RateLimitManager, SecurityHeadersManager } = require('edge-utils');
const rateLimiter = new RateLimitManager();
rateLimiter.addStrategy('api', {
type: 'token-bucket',
refillRate: 100, // requests per second
capacity: 1000
});
const securityHeaders = new SecurityHeadersManager({
contentSecurityPolicy: { enabled: true },
hsts: { maxAge: 31536000, includeSubDomains: true }
});
const handler = createEdgeHandler({
rateLimiting: { strategy: 'api' },
security: { headers: true },
cache: { strategy: 'memory', ttl: 300 },
geo: { routing: 'nearest', regions: ['us', 'eu', 'asia'] },
cors: { origins: ['*'], methods: ['GET', 'POST'] }
});
module.exports = handler;
| Platform | Supported | Notes |
|---|---|---|
| Cloudflare Workers | Yes | Full KV support, Durable Objects |
| Vercel Edge | Yes | Edge Config, KV support |
| Deno Deploy | Yes | Deno KV, native performance |
| AWS Lambda@Edge | Yes | Basic support |
| Fastly Compute@Edge | Yes | Advanced caching |
| Node.js Serverless | Yes | Fallback mode |
Distributed rate limiting with multiple algorithms for protecting your APIs.
const { RateLimitManager, TokenBucketLimiter } = require('edge-utils/rate-limiting');
// Token Bucket Algorithm
const limiter = new TokenBucketLimiter({
refillRate: 10, // tokens per second
capacity: 100, // burst capacity
storage: kvStorage // optional distributed storage
});
// Sliding Window Algorithm
const slidingLimiter = new SlidingWindowLimiter({
windowSize: 60, // seconds
maxRequests: 100,
storage: kvStorage
});
// Rate Limit Manager with strategies
const rateLimiter = new RateLimitManager({ storage: kvStorage });
rateLimiter.addStrategy('api', {
type: 'token-bucket',
refillRate: 100,
capacity: 1000
});
// Middleware usage
const middleware = rateLimiter.middleware({ strategy: 'api' });
Comprehensive security features including headers, CSRF protection, XSS prevention, and DDoS mitigation.
const { SecurityHeadersManager, CSRFProtection, RequestValidator } = require('edge-utils/security');
// Security Headers
const headersManager = new SecurityHeadersManager({
contentSecurityPolicy: {
enabled: true,
scriptSrc: "'self' 'nonce-abc123'"
},
hsts: { maxAge: 31536000, includeSubDomains: true }
});
// CSRF Protection
const csrf = new CSRFProtection({
secret: 'your-secret-key',
cookieName: 'csrf-token'
});
// Request Validation with JSON Schema
const validator = new RequestValidator();
validator.addSchema('user', {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' }
},
required: ['name', 'email']
});
JWT tokens, API keys, and session management for edge environments.
const { JWTManager, APIKeyManager, EdgeSessionManager } = require('edge-utils/auth');
// JWT Management
const jwt = new JWTManager({
secret: 'your-secret',
issuer: 'your-app',
audience: 'your-users'
});
const token = jwt.generate({ userId: 123, role: 'admin' });
const verified = jwt.verify(token);
// API Key Management
const apiKeys = new APIKeyManager({
hmacSecret: 'key-secret',
storage: kvStorage
});
const key = apiKeys.generate({
permissions: ['read', 'write'],
quota: { limit: 1000, period: 'hour' }
});
// Session Management
const sessions = new EdgeSessionManager({
secret: 'session-secret',
storage: kvStorage,
ttl: 24 * 60 * 60 * 1000 // 24 hours
});
const sessionId = await sessions.create({ userId: 123 }, 'user-123');
Metrics collection, structured logging, distributed tracing, and health checks.
const { MetricsCollector, StructuredLogger, TracingManager, HealthCheckManager } = require('edge-utils/monitoring');
// Metrics Collection
const metrics = new MetricsCollector({ storage: kvStorage });
metrics.increment('requests_total', 1, { method: 'GET', status: '200' });
metrics.histogram('request_duration', 150, { endpoint: '/api/users' });
metrics.gauge('active_connections', 42);
// Structured Logging
const logger = new StructuredLogger({
level: 'info',
format: 'json',
redaction: ['password', 'apiKey']
});
logger.info('User login successful', {
userId: 123,
ip: '192.168.1.1',
userAgent: 'Mozilla/5.0...'
});
// Distributed Tracing
const tracer = new TracingManager({
serviceName: 'edge-api',
sampler: () => Math.random() < 0.1 // 10% sampling
});
const span = tracer.startSpan('handle_request');
tracer.addEvent(span.spanId, 'processing_started');
tracer.endSpan(span.spanId);
// Health Checks
const health = new HealthCheckManager();
health.addCheck('database', async () => {
// Check database connection
return { healthy: true, latency: 5 };
});
health.addCheck('external_api', async () => {
// Check external service
return true;
});
Intelligent request distribution with health monitoring, circuit breakers, and sticky sessions for high availability.
const { LoadBalancer, CircuitBreaker, StickySessionManager } = require('edge-utils/load-balancing');
// Load Balancer with multiple algorithms
const loadBalancer = new LoadBalancer({
algorithm: 'round-robin', // round-robin, weighted-round-robin, least-connections, random, ip-hash, alter
endpoints: [
{ url: 'https://api1.example.com', weight: 2 },
{ url: 'https://api2.example.com', weight: 1 },
{ url: 'https://api3.example.com', weight: 1 }
],
healthCheckInterval: 30000, // Check every 30 seconds
failureThreshold: 3, // Mark unhealthy after 3 failures
timeout: 5000 // 5 second timeout
});
// Get next endpoint
const endpoint = loadBalancer.getNextEndpoint();
// Record request metrics
loadBalancer.recordRequestStart(endpoint);
loadBalancer.recordRequestEnd(endpoint, 150, true); // 150ms response time, success
// Get load balancer stats
const stats = loadBalancer.getStats();
console.log(`Healthy endpoints: ${stats.healthyEndpoints}/${stats.totalEndpoints}`);
// Circuit Breaker for fault tolerance
const circuitBreaker = new CircuitBreaker({
failureThreshold: 5, // Open after 5 failures
recoveryTimeout: 60000 // Try to close after 1 minute
});
const apiCall = async () => {
const response = await fetch('https://api.example.com/data');
return response.json();
};
try {
const result = await circuitBreaker.execute(apiCall);
console.log('Success:', result);
} catch (error) {
console.log('Circuit breaker open or operation failed:', error.message);
}
// Sticky Session Manager for session affinity
const stickyManager = new StickySessionManager({
ttl: 30 * 60 * 1000, // 30 minutes
storage: kvStorage // Optional distributed storage
});
const endpoints = ['server1', 'server2', 'server3'];
const clientId = 'user-123';
// Get consistent endpoint for client
const stickyEndpoint = stickyManager.getStickyEndpoint(clientId, endpoints);
// Clean up expired sessions
stickyManager.cleanup();
The ALTER (Adaptive Load balancing with Enhanced Response Time) algorithm is a custom intelligent load balancing strategy that considers multiple performance factors:
// Using the ALTER algorithm for optimal performance
const loadBalancer = new LoadBalancer({
algorithm: 'alter', // Custom intelligent algorithm
endpoints: [
{ url: 'https://api1.example.com', weight: 1 },
{ url: 'https://api2.example.com', weight: 1 },
{ url: 'https://api3.example.com', weight: 1 }
]
});
// The algorithm automatically selects the best endpoint based on real-time metrics
const endpoint = loadBalancer.getNextEndpoint();
createEdgeHandler(options) - Universal handler creator with all featuresdetectPlatform() - Detects current runtimeisCloudflareWorker(), isVercelEdge() - Platform checksMemoryCache - In-memory cache with TTLEdgeCache - Platform-specific cache (KV, Edge Config)cacheWarming(cache, keys, fetcher) - Pre-populate cachecacheInvalidation(cache, pattern) - Remove matching keysgeoRoute(headers, regions) - Route based on locationgetCountry(headers) - Extract country from headersnearestRegion(userRegion, availableRegions) - Find closest regionminimizeColdStart(init) - Run initialization oncekeepAlive(interval) - Prevent cold startsstreamResponse(stream, headers) - Stream responsescompressGzip(data), compressBrotli(data) - Compress dataEdgeError - Custom error classhandleError(error, options) - Standardized error responsesretryWithBackoff(fn, attempts) - Retry with exponential backoffcircuitBreaker(fn, threshold, timeout) - Circuit breaker patternLoadBalancer - Intelligent request distribution with health monitoringCircuitBreaker - Fault tolerance with automatic recoveryStickySessionManager - Session affinity for stateful applicationsconst {
createEdgeHandler,
RateLimitManager,
SecurityHeadersManager,
JWTManager,
MetricsCollector,
StructuredLogger
} = require('edge-utils');
// Initialize components
const rateLimiter = new RateLimitManager();
rateLimiter.addStrategy('api', {
type: 'token-bucket',
refillRate: 100,
capacity: 1000
});
const securityHeaders = new SecurityHeadersManager({
contentSecurityPolicy: { enabled: true },
hsts: { maxAge: 31536000, includeSubDomains: true }
});
const jwtManager = new JWTManager({
secret: process.env.JWT_SECRET,
issuer: 'my-app'
});
const metrics = new MetricsCollector();
const logger = new StructuredLogger({ level: 'info' });
// Create handler with all features
const handler = createEdgeHandler({
rateLimiting: { strategy: 'api' },
security: { headers: true },
auth: { required: true },
monitoring: { metrics: true, logging: true },
cache: { strategy: 'edge', ttl: 300 },
cors: { origins: ['https://myapp.com'], credentials: true }
});
// Custom middleware
handler.use(async (request, context) => {
const startTime = Date.now();
// Authentication
const authHeader = request.headers.authorization;
if (authHeader?.startsWith('Bearer ')) {
const token = authHeader.substring(7);
const verified = jwtManager.verify(token);
if (verified.valid) {
context.user = verified.payload;
}
}
// Metrics
metrics.increment('requests_total', 1, {
method: request.method,
path: new URL(request.url).pathname
});
// Logging
logger.info('Request received', {
method: request.method,
url: request.url,
userId: context.user?.userId
});
// Continue to next middleware
const response = await context.next();
// Record response metrics
metrics.timing('request_duration', startTime, {
method: request.method,
status: response.status
});
// Add security headers
const secureHeaders = securityHeaders.generate();
for (const [key, value] of Object.entries(secureHeaders)) {
response.headers.set(key, value);
}
return response;
});
module.exports = handler;
const { RateLimitManager } = require('edge-utils/rate-limiting');
const rateLimiter = new RateLimitManager({ storage: kvStorage });
// Per-user rate limiting
rateLimiter.addStrategy('per_user', {
type: 'token-bucket',
refillRate: 10, // 10 requests per second per user
capacity: 50
});
// API endpoint rate limiting
rateLimiter.addStrategy('api_endpoint', {
type: 'sliding-window',
windowSize: 60, // 1 minute window
maxRequests: 100 // 100 requests per minute
});
// Global rate limiting
rateLimiter.addStrategy('global', {
type: 'token-bucket',
refillRate: 1000, // 1000 requests per second globally
capacity: 5000
});
// Middleware with conditional rate limiting
const middleware = (options = {}) => async (request, context) => {
let strategy = 'global';
if (context.user) {
strategy = 'per_user';
} else if (request.url.includes('/api/')) {
strategy = 'api_endpoint';
}
const result = await rateLimiter.check(request, {
strategy,
by: context.user ? 'user' : 'ip'
});
if (!result.allowed) {
return new Response('Rate limit exceeded', {
status: 429,
headers: {
'Retry-After': result.resetTime.toString(),
...result.headers
}
});
}
context.rateLimitHeaders = result.headers;
return null;
};
const {
SecurityHeadersManager,
CSRFProtection,
XSSPrevention,
RequestValidator,
DDoSProtection
} = require('edge-utils/security');
// Initialize security components
const headersManager = new SecurityHeadersManager({
contentSecurityPolicy: {
enabled: true,
defaultSrc: "'self'",
scriptSrc: "'self' 'unsafe-inline'",
styleSrc: "'self' 'unsafe-inline' https://fonts.googleapis.com",
fontSrc: "'self' https://fonts.gstatic.com",
imgSrc: "'self' data: https:",
connectSrc: "'self' https://api.example.com"
},
hsts: { maxAge: 31536000, includeSubDomains: true, preload: true },
frameOptions: { action: 'DENY' },
contentTypeOptions: 'nosniff',
referrerPolicy: 'strict-origin-when-cross-origin',
permissionsPolicy: {
geolocation: 'none',
camera: 'none',
microphone: 'none'
}
});
const csrfProtection = new CSRFProtection({
secret: process.env.CSRF_SECRET,
sameSite: 'strict'
});
const xssPrevention = new XSSPrevention({
sanitizationRules: [
{
context: 'html',
pattern: /<script[^>]*>.*?<\/script>/gi,
replacement: ''
}
]
});
const requestValidator = new RequestValidator({
ajvOptions: { allErrors: true, removeAdditional: true }
});
// Add validation schemas
requestValidator.addSchema('createUser', {
type: 'object',
properties: {
name: { type: 'string', minLength: 1, maxLength: 100 },
email: { type: 'string', format: 'email' },
password: { type: 'string', minLength: 8 }
},
required: ['name', 'email', 'password']
});
const ddosProtection = new DDoSProtection({
spikeThreshold: 100, // requests per minute
blockDuration: 15 * 60 * 1000, // 15 minutes
challengeEnabled: true
});
// Security middleware chain
const securityMiddleware = [
ddosProtection.middleware(),
csrfProtection.middleware(),
async (request, context) => {
// Input validation
if (request.method === 'POST' && request.url.includes('/api/users')) {
const validation = requestValidator.validate(request, 'createUser');
if (!validation.valid) {
return new Response(JSON.stringify({
error: 'Validation failed',
details: validation.errors
}), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
}
// XSS prevention
if (request.body && typeof request.body === 'string') {
request.body = xssPrevention.sanitize(request.body);
}
return null;
}
];
const { LoadBalancer, CircuitBreaker, StickySessionManager } = require('edge-utils/load-balancing');
// Initialize load balancer with health monitoring
const loadBalancer = new LoadBalancer({
algorithm: 'weighted-round-robin',
endpoints: [
{ url: 'https://api-us-east.example.com', weight: 3 },
{ url: 'https://api-us-west.example.com', weight: 2 },
{ url: 'https://api-eu-west.example.com', weight: 2 }
],
healthCheckInterval: 15000, // Check every 15 seconds
failureThreshold: 2,
successThreshold: 2,
timeout: 3000
});
// Circuit breaker for API calls
const circuitBreaker = new CircuitBreaker({
failureThreshold: 3,
recoveryTimeout: 30000,
monitoringPeriod: 60000
});
// Sticky sessions for user-specific data
const stickyManager = new StickySessionManager({
ttl: 60 * 60 * 1000, // 1 hour
storage: kvStorage
});
// Load balancing middleware
const loadBalancingMiddleware = async (request, context) => {
const clientId = context.user?.id || getClientIP(request);
// Get sticky endpoint for session affinity
const endpoints = Array.from(loadBalancer.endpoints.keys());
const targetEndpoint = stickyManager.getStickyEndpoint(clientId, endpoints);
if (!targetEndpoint) {
return new Response('No healthy endpoints available', { status: 503 });
}
// Execute request with circuit breaker
try {
const result = await circuitBreaker.execute(async () => {
loadBalancer.recordRequestStart(targetEndpoint);
const startTime = Date.now();
const response = await fetch(`${targetEndpoint}${new URL(request.url).pathname}`, {
method: request.method,
headers: request.headers,
body: request.body
});
const responseTime = Date.now() - startTime;
loadBalancer.recordRequestEnd(targetEndpoint, responseTime, response.ok);
return response;
});
return result;
} catch (error) {
// Record failed request
loadBalancer.recordRequestEnd(targetEndpoint, 0, false);
return new Response('Service temporarily unavailable', { status: 503 });
}
};
// Health check endpoint
const healthMiddleware = async (request, context) => {
if (request.url.endsWith('/health')) {
const stats = loadBalancer.getStats();
const circuitStats = circuitBreaker.getStats();
return new Response(JSON.stringify({
loadBalancer: {
healthy: stats.healthyEndpoints > 0,
totalEndpoints: stats.totalEndpoints,
healthyEndpoints: stats.healthyEndpoints
},
circuitBreaker: {
state: circuitStats.state,
failureRate: circuitStats.failureRate
}
}), {
headers: { 'Content-Type': 'application/json' }
});
}
return null;
};
npm install edge-utils
npm run build
npm test
PRs welcome. See CONTRIBUTING.md for details.
Please read our Code of Conduct before contributing.
See SECURITY.md for security-related information.
MIT
FAQs
Platform-agnostic utilities for edge computing and serverless environments
We found that edge-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.