New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

express-service-bootstrap

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-service-bootstrap - npm Package Compare versions

Comparing version 0.7.0 to 0.8.0

dist/src/sorted-map.d.ts

2

dist/package.json
{
"name": "express-service-bootstrap",
"version": "0.7.0",
"version": "0.8.0",
"description": "This is a convenience package for starting a express API with security, health checks, process exits etc.",

@@ -5,0 +5,0 @@ "main": "dist/src/index.js",

@@ -8,2 +8,3 @@ /// <reference types="node" />

export type ApplicationBuilderMiddleware = (request: Request, response: Response, next: NextFunction) => Promise<void> | void;
export type HostingPath = string | "*";
export declare enum ApplicationTypes {

@@ -22,10 +23,5 @@ Main = 0,

* It sets up the application's health status, ports, middlewares, routers, and error handling.
*
* @property {K8SHealthStatus} healthStatus - The health status of the application.
* @property {string} applicationName - The name of the application.
* @property {any} swaggerDocument - The swagger document for the application(JSON representation).
*/
export declare class ApplicationBuilder {
applicationName: string;
swaggerDocument: any;
startupHandler: (rootRouter: IRouter, DIContainer: DisposableSingletonContainer, applicationBuilder: ApplicationBuilder) => Promise<IProbeResult<ApplicationStartupStatus>>;

@@ -41,5 +37,4 @@ shutdownHandler: () => Promise<IProbeResult<ApplicationShutdownStatus>>;

private healthPort;
private appMiddlewares;
private healthMiddlewares;
private appRouters;
private appHandlers;
private healthHandlers;
private catchAllErrorResponseTransformer;

@@ -51,3 +46,2 @@ private readonly exitHandler;

* @param {string} applicationName - The name of the application.
* @param {any} swaggerDocument - The swagger document for the application(JSON representation).
* @param startupHandler - The startup handler that has to be invoked before application starts, used to indicate the application's startup status.

@@ -61,3 +55,3 @@ * @param shutdownHandler - The shutdown handler that has to be invoked before application shutdowns, used to indicate the application's liveliness status.

*/
constructor(applicationName?: string, swaggerDocument?: any, startupHandler?: (rootRouter: IRouter, DIContainer: DisposableSingletonContainer, applicationBuilder: ApplicationBuilder) => Promise<IProbeResult<ApplicationStartupStatus>>, shutdownHandler?: () => Promise<IProbeResult<ApplicationShutdownStatus>>, livenessProbe?: IProbe<ApplicationStatus>, readinessProbe?: IProbe<ApplicationStatus>, currentProcess?: NodeJS.Process, exitSignals?: NodeJS.Signals[], container?: DisposableSingletonContainer);
constructor(applicationName?: string, startupHandler?: (rootRouter: IRouter, DIContainer: DisposableSingletonContainer, applicationBuilder: ApplicationBuilder) => Promise<IProbeResult<ApplicationStartupStatus>>, shutdownHandler?: () => Promise<IProbeResult<ApplicationShutdownStatus>>, livenessProbe?: IProbe<ApplicationStatus>, readinessProbe?: IProbe<ApplicationStatus>, currentProcess?: NodeJS.Process, exitSignals?: NodeJS.Signals[], container?: DisposableSingletonContainer);
/**

@@ -107,14 +101,10 @@ * Used to override the startup handler.

* Used to register a SYNC/ASYNC middleware.
* @param {ApplicationBuilderMiddleware} middleware middleware to be registered.
* @param {ApplicationBuilderMiddleware} handler handler to be registered.
* @param {HostingPath} hostingPath path where the handler has to be registered, use "*" for global.
* @param {number} order order in which the handler has to be registered.
* @param {ApplicationTypes} appliesTo type of application to which the handler has to be registered.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
registerApplicationMiddleware(middleware: ApplicationBuilderMiddleware, appliesTo?: ApplicationTypes): ApplicationBuilder;
registerApplicationHandler(handler: ApplicationBuilderMiddleware | IRouter, hostingPath: HostingPath, order?: number | undefined, appliesTo?: ApplicationTypes): ApplicationBuilder;
/**
* Used to register a router.
* @param {string} path path for the router.
* @param {IRouter} router router to be registered.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
registerApplicationRoutes(path: string, router: IRouter): ApplicationBuilder;
/**
* Used to start the application using the configured parameters.

@@ -121,0 +111,0 @@ * @returns {Promise<void>} Promise that resolves when the application is started.

@@ -37,6 +37,6 @@ "use strict";

const express_1 = __importStar(require("express"));
const swaggerUi = __importStar(require("swagger-ui-express"));
const enum_application_life_cycle_status_1 = require("./enum-application-life-cycle-status");
const disposable_singleton_container_1 = require("./disposable-singleton-container");
const null_probe_1 = require("./null-probe");
const sorted_map_1 = require("./sorted-map");
var ApplicationTypes;

@@ -51,6 +51,2 @@ (function (ApplicationTypes) {

* It sets up the application's health status, ports, middlewares, routers, and error handling.
*
* @property {K8SHealthStatus} healthStatus - The health status of the application.
* @property {string} applicationName - The name of the application.
* @property {any} swaggerDocument - The swagger document for the application(JSON representation).
*/

@@ -62,3 +58,2 @@ class ApplicationBuilder {

* @param {string} applicationName - The name of the application.
* @param {any} swaggerDocument - The swagger document for the application(JSON representation).
* @param startupHandler - The startup handler that has to be invoked before application starts, used to indicate the application's startup status.

@@ -72,5 +67,4 @@ * @param shutdownHandler - The shutdown handler that has to be invoked before application shutdowns, used to indicate the application's liveliness status.

*/
constructor(applicationName = 'Application', swaggerDocument = null, startupHandler = () => __awaiter(this, void 0, void 0, function* () { return ({ status: enum_application_life_cycle_status_1.ApplicationStartupStatus.UP, data: {} }); }), shutdownHandler = () => __awaiter(this, void 0, void 0, function* () { return ({ status: enum_application_life_cycle_status_1.ApplicationShutdownStatus.STOPPED, data: {} }); }), livenessProbe = new null_probe_1.NullProbe(enum_application_life_cycle_status_1.ApplicationStatus.UP), readinessProbe = new null_probe_1.NullProbe(enum_application_life_cycle_status_1.ApplicationStatus.UP), currentProcess = process, exitSignals = ['SIGINT', 'SIGTERM'], container = new disposable_singleton_container_1.DisposableSingletonContainer()) {
constructor(applicationName = 'Application', startupHandler = () => __awaiter(this, void 0, void 0, function* () { return ({ status: enum_application_life_cycle_status_1.ApplicationStartupStatus.UP, data: {} }); }), shutdownHandler = () => __awaiter(this, void 0, void 0, function* () { return ({ status: enum_application_life_cycle_status_1.ApplicationShutdownStatus.STOPPED, data: {} }); }), livenessProbe = new null_probe_1.NullProbe(enum_application_life_cycle_status_1.ApplicationStatus.UP), readinessProbe = new null_probe_1.NullProbe(enum_application_life_cycle_status_1.ApplicationStatus.UP), currentProcess = process, exitSignals = ['SIGINT', 'SIGTERM'], container = new disposable_singleton_container_1.DisposableSingletonContainer()) {
this.applicationName = applicationName;
this.swaggerDocument = swaggerDocument;
this.startupHandler = startupHandler;

@@ -86,5 +80,4 @@ this.shutdownHandler = shutdownHandler;

this.healthPort = 5678;
this.appMiddlewares = new Array();
this.healthMiddlewares = new Array();
this.appRouters = new Map();
this.appHandlers = new sorted_map_1.SortedMap();
this.healthHandlers = new sorted_map_1.SortedMap();
this.exitHandler = this[Symbol.asyncDispose].bind(this);

@@ -167,16 +160,19 @@ this.exitSignals.forEach(signal => {

* Used to register a SYNC/ASYNC middleware.
* @param {ApplicationBuilderMiddleware} middleware middleware to be registered.
* @param {ApplicationBuilderMiddleware} handler handler to be registered.
* @param {HostingPath} hostingPath path where the handler has to be registered, use "*" for global.
* @param {number} order order in which the handler has to be registered.
* @param {ApplicationTypes} appliesTo type of application to which the handler has to be registered.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
registerApplicationMiddleware(middleware, appliesTo = ApplicationTypes.Main) {
registerApplicationHandler(handler, hostingPath, order = undefined, appliesTo = ApplicationTypes.Main) {
switch (appliesTo) {
case ApplicationTypes.Main:
this.appMiddlewares.push(middleware);
this.appHandlers.set(hostingPath, handler, order);
break;
case ApplicationTypes.Health:
this.healthMiddlewares.push(middleware);
this.healthHandlers.set(hostingPath, handler, order);
break;
case ApplicationTypes.Both:
this.appMiddlewares.push(middleware);
this.healthMiddlewares.push(middleware);
this.appHandlers.set(hostingPath, handler, order);
this.healthHandlers.set(hostingPath, handler, order);
break;

@@ -189,12 +185,2 @@ default:

/**
* Used to register a router.
* @param {string} path path for the router.
* @param {IRouter} router router to be registered.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
registerApplicationRoutes(path, router) {
this.appRouters.set(path, router);
return this;
}
/**
* Used to start the application using the configured parameters.

@@ -211,3 +197,3 @@ * @returns {Promise<void>} Promise that resolves when the application is started.

if (this.applicationStatus.status === enum_application_life_cycle_status_1.ApplicationStartupStatus.UP) {
this.registerApplicationRoutes("/", rootRouter);
this.registerApplicationHandler(rootRouter, "/", 0, ApplicationTypes.Main);
yield this.container.createInstanceWithoutConstructor('healthExpress', this.healthExpressListen.bind(this));

@@ -243,5 +229,4 @@ yield this.container.createInstanceWithoutConstructor('applicationExpress', this.appExpressListen.bind(this));

yield this.container.disposeAll();
this.appMiddlewares = [];
this.healthMiddlewares = [];
this.appRouters.clear();
this.appHandlers.clear();
this.healthHandlers.clear();
this.applicationStatus = result;

@@ -258,11 +243,10 @@ this.applicationStatus.data["shutdownTime"] = Date.now() - startTime;

try {
if (this.swaggerDocument != null) {
applicationExpressInstance.use('/api-docs', swaggerUi.serve, swaggerUi.setup(this.swaggerDocument));
for (const [path, handler] of this.appHandlers.sort()) {
if (path === "*") {
applicationExpressInstance.use(handler);
}
else {
applicationExpressInstance.use(path, handler);
}
}
for (const middleware of this.appMiddlewares) {
applicationExpressInstance.use(middleware);
}
for (const [path, router] of this.appRouters) {
applicationExpressInstance.use(path, router);
}
applicationExpressInstance.use(this.errorHandler.bind(this));

@@ -290,4 +274,9 @@ const server = applicationExpressInstance.listen(this.applicationPort, () => { a(server); });

try {
for (const middleware of this.healthMiddlewares) {
healthExpressInstance.use(middleware);
for (const [path, handler] of this.healthHandlers.sort()) {
if (path === "*") {
healthExpressInstance.use(handler);
}
else {
healthExpressInstance.use(path, handler);
}
}

@@ -294,0 +283,0 @@ healthExpressInstance.get(`/health/startup`, (req, res) => __awaiter(this, void 0, void 0, function* () { return this.checkHealthStatus("startup", res); }));

@@ -6,2 +6,7 @@ /// <reference types="node" />

import { HelmetOptions } from "helmet";
import { IRouter } from "express";
export type ApplicationRouter = {
hostingPath: string;
router: IRouter;
};
/**

@@ -35,2 +40,3 @@ * A convenience class that provides a way to create middleware instances without the need to manually create them.

helmetMiddleware(helmetOptions?: Readonly<HelmetOptions>): (req: import("http").IncomingMessage, res: import("http").ServerResponse<import("http").IncomingMessage>, next: (err?: unknown) => void) => void;
swaggerAPIDocs(swaggerDocument: any, hostPath?: string): ApplicationRouter;
}
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -10,2 +33,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

const helmet_1 = __importDefault(require("helmet"));
const express_1 = require("express");
const swaggerUi = __importStar(require("swagger-ui-express"));
/**

@@ -46,4 +71,9 @@ * A convenience class that provides a way to create middleware instances without the need to manually create them.

}
swaggerAPIDocs(swaggerDocument, hostPath = '/api-docs') {
const swaggerRouter = this.customConstructor.createInstanceWithoutConstructor(express_1.Router);
swaggerRouter.use(swaggerUi.serve, swaggerUi.setup(swaggerDocument));
return { hostingPath: hostPath, router: swaggerRouter };
}
}
exports.Convenience = Convenience;
//# sourceMappingURL=convenience.js.map
import { ApplicationShutdownStatus, ApplicationStartupStatus, ApplicationStatus } from './enum-application-life-cycle-status';
import { ApplicationBuilder, ApplicationBuilderMiddleware, ApplicationTypes } from './application-builder';
import { ApplicationBuilder, ApplicationBuilderMiddleware, ApplicationTypes, HostingPath } from './application-builder';
import { BootstrapConstructor } from './bootstrap-constructor';

@@ -8,3 +8,3 @@ import { DisposableSingletonContainer } from './disposable-singleton-container';

import { IProbe } from './i-probe';
import { Convenience } from './convenience';
export { ApplicationStatus, ApplicationShutdownStatus, ApplicationStartupStatus, ApplicationBuilder, BootstrapConstructor, DisposableSingletonContainer, IProbeResult, NullProbe, IProbe, ApplicationBuilderMiddleware, ApplicationTypes, Convenience };
import { ApplicationRouter, Convenience } from './convenience';
export { ApplicationStatus, ApplicationShutdownStatus, ApplicationStartupStatus, ApplicationBuilder, BootstrapConstructor, DisposableSingletonContainer, IProbeResult, NullProbe, IProbe, ApplicationBuilderMiddleware, ApplicationTypes, Convenience, ApplicationRouter, HostingPath };
{
"name": "express-service-bootstrap",
"version": "0.7.0",
"version": "0.8.0",
"description": "This is a convenience package for starting a express API with security, health checks, process exits etc.",

@@ -5,0 +5,0 @@ "main": "dist/src/index.js",

@@ -39,8 +39,10 @@ # express-service-bootstrap

//Configure your application.
application.overrideAppPort(8080) //override the default port 8080
.overrideHealthPort(8081) //override the default health port 8081
.registerApplicationMiddleware(utilities.helmetMiddleware(), ApplicationTypes.Both) //register helmet middleware for both application and health
.registerApplicationMiddleware(utilities.bodyParserURLEncodingMiddleware()) //register body parser url middleware for application
.registerApplicationMiddleware(utilities.bodyParserJSONEncodingMiddleware({ limit: '50M' })) //register body parser json middleware for application
.overrideCatchAllErrorResponseTransformer((req, error) => ({ //override the default catch all error response transformer
const apiDocs = utilities.swaggerAPIDocs(OpenApiDefinition);
application.overrideAppPort(8080) //override the default port 8080(Default 3000)
.overrideHealthPort(8081) //override the default health port 8081(Default 5678)
.registerApplicationHandler(utilities.helmetMiddleware(), "*", 0, ApplicationTypes.Both) //register helmet middleware for both application and health
.registerApplicationHandler(utilities.bodyParserURLEncodingMiddleware(), "*", 1, ApplicationTypes.Main) //register body parser url middleware for application
.registerApplicationHandler(utilities.bodyParserJSONEncodingMiddleware({ limit: '50M' }), "*", 2, ApplicationTypes.Main) //register body parser json middleware for application
.registerApplicationHandler(apiDocs.router, apiDocs.hostingPath, 3, ApplicationTypes.Main) //register api docs
.overrideCatchAllErrorResponseTransformer((req, error) => ({ //override the default catch all error response transformer
path: req.path,

@@ -62,2 +64,10 @@ status: 500,

.catch(console.error);
//*******************************
// You can wait for your application to be called by process exit signal
// OR
// If you are done call the Dispose method to stop the application
// await app[Symbol.asyncDispose](); //stop the application and release all resources
//*******************************
```

@@ -64,0 +74,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc