UNPKG

nest-local-https-proxy

Version:

A simple library to add HTTPS support to your HTTP NestJS application for local develoment, debugging, and testing.

61 lines (60 loc) 3.26 kB
import { EventEmitter } from 'events'; import { SecureContextOptions } from 'tls'; import { INestApplication } from '@nestjs/common'; /** * **⚠ DISCLAIMER: For local development use only! ⚠** * * This is intended only for local development, testing, and troubleshooting. * It is not recommended to use this in any production context or public-facing environment. * * @fires LocalHttpsProxy#listening when when the encapsulated {@link https.Server} successfully binds to a port and begins listening for connections. * @fires LocalHttpsProxy#error for any error condition which occurs after initialization of the LocalHttpsProxy instance. */ export declare class LocalHttpsProxy extends EventEmitter implements LocalHttpsProxy { private readonly httpsProxyServer; /** * @param nestApp NestJS application instance. * @param httpsOptions Accepts {@link https.createServer}(`options`: {@link SecureContextOptions}) parameter from `node:https`. Object _must_ include `cert` and `key` properties. * @param errorCallback Optional: Callback function called when any error condition occurs within the LocalHttpsProxy instance. * @param listeningCallback Optional: Callback function called when the encapsulated {@link https.Server} successfully binds to a port and begins listening for connections. * @throws When initialization of {@link https.Server} fails. */ constructor(nestApp: INestApplication, httpsOptions: SecureContextOptions, errorCallback?: (error: Error) => void, listeningCallback?: (port: number) => void); /** * Start LocalHttpsProxy instance. * @param listeningPort Optional: Port on which to listen for HTTPS connections, if not provided then an open port will be automatically assigned. * @example <caption>Start a local HTTPS proxy listening on `0.0.0.0:43000`, proxying requests to your Nest app's HTTP server:</caption> * const localHttpsProxy = new LocalHttpsProxy(app, httpsOptions); * localHttpsProxy.start(43000); */ start(listeningPort?: number): void; /** * Stop LocalHttpsProxy and close all open connections. */ close(): void; /** * Initialize private encapsulated instance of `https.Server`. * @param httpsOptions Options used during `https.Server` initialization. * @param nestApp NestJS application instance. * @returns New {@link https.Server} instance configured using {@link httpsOptions}. */ private initHttpsServer; /** * Derive the requestListener from the HTTP server in the Nest Application instance. * @param nestApp NestJS application instance. * @returns requestListener from the HTTP server within Nest Application, or undefined if unavailable. */ private getNestAppRequestListener; private onError; private onListening; } export declare interface LocalHttpsProxy { /** * Emitted when the encapsulated {@link https.Server} successfully binds to a port and begins listening for connections. */ on(event: 'listening', listener: (port: number) => void): this; /** * Emitted for any error condition which occurs within the LocalHttpsProxy instance. */ on(event: 'error', listener: (error: Error) => void): this; }