1- import { MockConfig , RequestInfo , SniffConfig } from './types' ;
1+ import { MockConfig , RecordConfig , RequestInfo , SniffConfig } from './types' ;
22import { Proxy as HttpProxy , IContext , IProxyOptions } from 'http-mitm-proxy' ;
33import { v4 as uuid } from 'uuid' ;
44import {
@@ -19,7 +19,8 @@ import { Mock } from './mock';
1919import { RequestInterceptor } from './interceptor' ;
2020import { ApiSniffer } from './api-sniffer' ;
2121import _ from 'lodash' ;
22- import logger from './logger' ;
22+ import log from './logger' ;
23+ import { RecordingManager } from './recording-manager' ;
2324
2425export interface ProxyOptions {
2526 deviceUDID : string ;
@@ -31,20 +32,35 @@ export interface ProxyOptions {
3132
3233export class Proxy {
3334 private _started = false ;
35+ private _replayStarted = false ;
3436 private readonly mocks = new Map < string , Mock > ( ) ;
3537 private readonly sniffers = new Map < string , ApiSniffer > ( ) ;
3638
3739 private readonly httpProxy : HttpProxy ;
40+ private readonly recordingManager : RecordingManager ;
3841
3942 public isStarted ( ) : boolean {
4043 return this . _started ;
4144 }
4245
46+ public isReplayStarted ( ) : boolean {
47+ return this . _replayStarted ;
48+ }
49+
50+ public startReplaying ( ) : void {
51+ this . _replayStarted = true ;
52+ }
53+
4354 constructor ( private readonly options : ProxyOptions ) {
4455 this . httpProxy = new HttpProxy ( ) ;
56+ this . recordingManager = new RecordingManager ( options ) ;
4557 addDefaultMocks ( this ) ;
4658 }
4759
60+ public getRecordingManager ( ) : RecordingManager {
61+ return this . recordingManager ;
62+ }
63+
4864 public get port ( ) : number {
4965 return this . options . port ;
5066 }
@@ -81,7 +97,7 @@ export class Proxy {
8197 this . httpProxy . onRequest ( this . handleMockApiRequest . bind ( this ) ) ;
8298
8399 this . httpProxy . onError ( ( context , error , errorType ) => {
84- logger . error ( `${ errorType } : ${ error } ` ) ;
100+ log . error ( `${ errorType } : ${ error } ` ) ;
85101 } ) ;
86102
87103 await new Promise ( ( resolve ) => {
@@ -123,26 +139,37 @@ export class Proxy {
123139 return id ;
124140 }
125141
126- public removeSniffer ( id ?: string ) : RequestInfo [ ] {
127- const _sniffers = [ ...this . sniffers . values ( ) ] ;
128- if ( id && ! _ . isNil ( this . sniffers . get ( id ) ) ) {
129- _sniffers . push ( this . sniffers . get ( id ) ! ) ;
142+ public removeSniffer ( record : boolean , id ?: string ) : RequestInfo [ ] {
143+ const _sniffers = [ ...this . sniffers . values ( ) ] ;
144+ if ( id && ! _ . isNil ( this . sniffers . get ( id ) ) ) {
145+ _sniffers . push ( this . sniffers . get ( id ) ! ) ;
146+ }
147+ let apiRequests ;
148+ if ( record ) {
149+ apiRequests = this . recordingManager . getCapturedTraffic ( _sniffers ) ;
150+ }
151+ else {
152+ apiRequests = _sniffers . reduce ( ( acc , sniffer ) => {
153+ acc . push ( ...sniffer . getRequests ( ) ) ;
154+ return acc ;
155+ } , [ ] as RequestInfo [ ] ) ;
156+ }
157+ _sniffers . forEach ( ( sniffer ) => this . sniffers . delete ( sniffer . getId ( ) ) ) ;
158+ return apiRequests ;
130159 }
131- const apiRequests = _sniffers . reduce ( ( acc , sniffer ) => {
132- acc . push ( ...sniffer . getRequests ( ) ) ;
133- return acc ;
134- } , [ ] as RequestInfo [ ] ) ;
135- _sniffers . forEach ( ( sniffer ) => this . sniffers . delete ( sniffer . getId ( ) ) ) ;
136- return apiRequests ;
137- }
138160
139161 private async handleMockApiRequest ( ctx : IContext , next : ( ) => void ) : Promise < void > {
140- const matchedMocks = await this . findMatchingMocks ( ctx ) ;
141- if ( matchedMocks . length ) {
142- const compiledMock = compileMockConfig ( matchedMocks ) ;
143- this . applyMockToRequest ( ctx , compiledMock , next ) ;
144- } else {
145- next ( ) ;
162+ if ( this . isReplayStarted ( ) ) {
163+ this . recordingManager . handleRecordingApiRequest ( ctx , next ) ;
164+ } else if ( ! this . isReplayStarted ( ) ) {
165+ const matchedMocks = await this . findMatchingMocks ( ctx ) ;
166+ if ( matchedMocks . length ) {
167+ const compiledMock = compileMockConfig ( matchedMocks ) ;
168+ this . applyMockToRequest ( ctx , compiledMock , next ) ;
169+ }
170+ else {
171+ next ( ) ;
172+ }
146173 }
147174 }
148175
@@ -197,4 +224,4 @@ export class Proxy {
197224 next ( ) ;
198225 }
199226 }
200- }
227+ }
0 commit comments