/// import { EventEmitter } from 'events'; import * as http from 'http'; import { RequestSerializer, ResponseSerializer } from './http-serializer'; /** * Event emitted whenever we intercept an HTTP request */ export interface IInterceptEvent { /** * The client request which initiated the HTTP request */ clientRequest: http.ClientRequest; /** * Request arriving to our MITM proxy */ interceptedRequest: http.IncomingMessage; /** * Response from our MITM proxy */ interceptedResponse: http.ServerResponse; /** * Proxy the intercepted request to its original destination */ proxy: () => void; requestNumber: number; requestSerializer: RequestSerializer; } /** * Configure intercept */ export interface IInterceptOptions { /** * Do not intercept outbound requests on these ports. * * By default MITM will intercept activity on any socket, HTTP or otherwise. * If you need to ignore a port (eg for a database connection), provide that port number here. * * In practice YesNo normally runs after long running connections have been established, * so this won't be a problem. */ ignorePorts?: number[]; } /** * Emit whenever we have proxied a request to its original destination */ export interface IProxiedEvent { requestSerializer: RequestSerializer; responseSerializer: ResponseSerializer; requestNumber: number; } interface IInterceptEvents { on(event: 'intercept', listener: (event: IInterceptEvent) => void): this; on(event: 'proxied', listener: (event: IProxiedEvent) => void): this; } /** * Intercept outbound HTTP requests and provide mock responses through an event API. * * Uses MITM library to spy on HTTP requests made in current NodeJS process. */ export default class Interceptor extends EventEmitter implements IInterceptEvents { requestNumber: number; private clientRequests; private mitm?; private origOnSocket?; private ignorePorts; /** * Enables intercepting all outbound HTTP requests. * @param options Intercept options */ enable(options?: IInterceptOptions): void; /** * Disables intercepting outbound HTTP requests. */ disable(): void; /** * Event handler for Mitm "connect" event. */ private mitmOnConnect; /** * Event handler for Mitm "request" event. * * Intercepted requests will be proxied if the `shouldProxy` option has been set. * @emits 'intercept' when we intercept a request * @emits 'proxied' when we have sent the response for a proxied response */ private mitmOnRequest; private trackSocketAndClientOptions; private getRequestId; } export {};