fortify2-js
Version:
MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.
196 lines (192 loc) • 6.96 kB
JavaScript
;
var express = require('express');
var helmet = require('helmet');
var cors = require('cors');
var compression = require('compression');
var safeSerializer = require('../../components/fortified-function/serializer/safe-serializer.js');
require('./server/utils/Logger.js');
var FastServer = require('./server/FastServer.js');
/***************************************************************************
* FortifyJS - Advanced JavaScript Security Library
*
* @author Nehonix
* @license MIT
*
* Copyright (c) 2025 Nehonix. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***************************************************************************** */
/**
* Ultra-Fast Express (UFE) Server Factory
* Express applications with intelligent caching integration
* Zero-async initialization for immediate use
*/
/**
* Create ultra-fast Express server (zero-async)
* Returns app instance ready to use immediately
*/
function createServer(options = {}) {
if (options.env) {
process.env["NODE_ENV"] = options.env;
}
const server = new FastServer.UltraFastServer(options);
return server.getApp();
}
/**
* Create ultra-fast Express server class instance
*/
function createServerInstance(options = {}) {
return new FastServer.UltraFastServer(options);
}
/**
* Generate cache key for request
*/
function generateCacheKey(req, customKey) {
if (typeof customKey === "function") {
return customKey(req);
}
if (typeof customKey === "string") {
return customKey;
}
// Auto-generate key based on route and params
const baseKey = `${req.method}:${req.route?.path || req.path}`;
const params = Object.keys(req.params).length > 0
? `:${JSON.stringify(req.params)}`
: "";
const query = Object.keys(req.query).length > 0
? `:${JSON.stringify(req.query)}`
: "";
return `${baseKey}${params}${query}`;
}
/**
* Create cache middleware for routes
*/
function createCacheMiddleware(cache, options = {}) {
return async (req, res, next) => {
// Skip caching if disabled
if (options.cache?.enabled === false) {
return next();
}
// Only cache GET requests by default
if (req.method !== "GET") {
return next();
}
try {
const cacheKey = generateCacheKey(req, options.cache?.key);
const startTime = Date.now();
// Try to get from cache
const cachedData = await cache.get(cacheKey);
if (cachedData) {
const cacheTime = Date.now() - startTime;
// Log ultra-fast cache hits
if (cacheTime < 5) {
console.log(` CACHE HIT (${cacheTime}ms): ${cacheKey}`);
}
else {
console.log(` CACHE HIT (${cacheTime}ms): ${cacheKey}`);
}
// Set cache headers
res.set("X-Cache", "HIT");
res.set("X-Cache-Time", `${cacheTime}ms`);
return res.json(cachedData);
}
// Cache miss - continue to handler
res.set("X-Cache", "MISS");
// Override res.json to cache the response
const originalJson = res.json.bind(res);
res.json = function (data) {
// Cache the response asynchronously
setImmediate(async () => {
try {
const ttl = options.cache?.ttl || 300000; // 5 minutes default
await cache.set(cacheKey, data, {
ttl,
tags: options.cache?.tags,
});
console.log(` CACHED: ${cacheKey} (TTL: ${ttl}ms)`);
}
catch (error) {
console.error("Cache set error:", error);
}
});
return originalJson(data);
};
next();
}
catch (error) {
console.error("Cache middleware error:", error);
next(); // Continue without caching on error
}
};
}
/**
* Configure Express middleware
*/
async function UFSMiddleware(app, options) {
console.log("Configuring middleware...");
// Trust proxy if configured
if (options.server?.trustProxy) {
app.set("trust proxy", true);
}
// Security middleware
if (options.security?.helmet) {
try {
app.use(helmet());
}
catch (error) {
console.warn("Helmet not available, skipping security headers");
}
}
if (options.security?.cors) {
try {
app.use(cors());
}
catch (error) {
console.warn("CORS not available, skipping CORS headers");
}
}
// Performance middleware
if (options.performance?.compression) {
try {
app.use(compression());
}
catch (error) {
console.warn("Compression not available, skipping compression");
}
}
// Body parsing middleware
app.use(express.json({ limit: options.server?.jsonLimit }));
app.use(express.urlencoded({
extended: true,
limit: options.server?.urlEncodedLimit,
}));
// Performance tracking middleware
}
Object.defineProperty(exports, 'Router', {
enumerable: true,
get: function () { return express.Router; }
});
exports.expressStringify = safeSerializer.expressStringify;
exports.fastStringify = safeSerializer.fastStringify;
exports.safeStringify = safeSerializer.safeStringify;
exports.UFSMiddleware = UFSMiddleware;
exports.createCacheMiddleware = createCacheMiddleware;
exports.createServer = createServer;
exports.createServerInstance = createServerInstance;
//# sourceMappingURL=ServerFactory.js.map