http-micro
Version:
Micro-framework on top of node's http module
111 lines (110 loc) • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const router_1 = require("./router");
const os = require("os");
const error_utils_1 = require("./error-utils");
/**
* The default error handler. It's executed at the end of
* the middleware chain or whenever, an exception that's
* not caught by the application is encountered.
*
* @export
* @param {Error} err
*/
function defaultErrorHandler(err, req, res) {
error_utils_1.errorToResponse(err, res);
for (let msg of error_utils_1.recurseErrorInfo(err)) {
console.error(msg + os.EOL);
}
}
exports.defaultErrorHandler = defaultErrorHandler;
/**
* A default fall-back handler that simply ends the request, and returns.
*
* @export
* @param {IContext} context
* @param {MiddlewareWithContext} next
* @returns {Promise} Resolved Promise
*/
function defaultFallbackOkHandler(context, next) {
context.res.end();
return Promise.resolve();
}
exports.defaultFallbackOkHandler = defaultFallbackOkHandler;
/**
* The default fall-back handler that ends the request with 404 status code.
*
* @export
* @param {IContext} context
* @param {MiddlewareWithContext} next
* @returns {Promise} Resolved Promise
*/
function defaultFallbackNotFoundHandler(context, next) {
context.res.statusCode = 404;
context.res.end();
return Promise.resolve();
}
exports.defaultFallbackNotFoundHandler = defaultFallbackNotFoundHandler;
function defaultClientSocketErrorHandler(err, socket) {
socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
}
exports.defaultClientSocketErrorHandler = defaultClientSocketErrorHandler;
/**
* Composes multiple middlewares into one middleware that executes the middleware chain.
*
* @export
* @template T {IContext}
* @param {...Middleware<T>[]} middlewares
* @returns {Middleware<T>}
*/
function compose(...middlewares) {
return (context, next) => dispatch(context, next, middlewares);
}
exports.compose = compose;
function dispatch(context, next, middlewares) {
// Take the allocation-free path when no middlewares exist.
if (middlewares.length === 0) {
return next();
}
let currentIndex = 0;
let run = () => {
// Check if the next middleware in the chain
// exists, if it does, dispatch to chain,
// or else connect the 'next' to the chain
// instead.
if (currentIndex < middlewares.length) {
let c = middlewares[currentIndex];
if (c) {
currentIndex = currentIndex + 1;
return c(context, run);
}
}
return next();
};
return run();
}
exports.dispatch = dispatch;
/**
* Stringify JSON, like JSON.stringify, but v8 optimized.
*/
function stringify(value, replacer, spaces) {
// v8 checks arguments.length for optimizing simple call
// https://bugs.chromium.org/p/v8/issues/detail?id=4730
return replacer || spaces
? JSON.stringify(value, replacer, spaces)
: JSON.stringify(value);
}
exports.stringify = stringify;
function addMiddlewares(middlewares, destination) {
if (!middlewares)
return;
let len = middlewares.length;
let dlength = destination.length;
destination.length = dlength + len;
for (var i = 0; i < len; i++) {
var current = middlewares[i];
let handler = current instanceof router_1.Router ? current.build() : current;
destination[i + dlength] = handler;
}
}
exports.addMiddlewares = addMiddlewares;