@@ -4,32 +4,19 @@ import { observable, computed, action, runInAction, when } from 'mobx';
44import  { 
55    HtkRequest , 
66    HtkResponse , 
7-     Headers , 
8-     MessageBody , 
97    InputRequest , 
108    InputResponse , 
119    InputFailedRequest , 
1210    TimingEvents , 
13-     InputMessage , 
1411    MockttpBreakpointedRequest , 
1512    MockttpBreakpointedResponse , 
1613    InputCompletedRequest , 
1714    MockttpBreakpointResponseResult , 
18-     InputRuleEventDataMap , 
19-     RawHeaders 
15+     InputRuleEventDataMap 
2016}  from  "../../types" ; 
21- import  { 
22-     fakeBuffer , 
23-     FakeBuffer , 
24-     stringToBuffer , 
25- }  from  '../../util/buffer' ; 
2617import  {  UnreachableCheck  }  from  '../../util/error' ; 
27- import  {  lazyObservablePromise ,  ObservablePromise ,  observablePromise  }  from  "../../util/observable" ; 
28- import  { 
29-     asHeaderArray , 
30-     getHeaderValue , 
31-     getHeaderValues 
32- }  from  '../../util/headers' ; 
18+ import  {  lazyObservablePromise  }  from  "../../util/observable" ; 
19+ import  {  getHeaderValue  }  from  '../../util/headers' ; 
3320import  {  ParsedUrl  }  from  '../../util/url' ; 
3421
3522import  {  logError  }  from  '../../errors' ; 
@@ -46,7 +33,7 @@ import { OpenApiExchange } from '../api/openapi';
4633import  {  parseRpcApiExchange  }  from  '../api/jsonrpc' ; 
4734import  {  ApiMetadata  }  from  '../api/api-interfaces' ; 
4835
49- import  {  decodeBody  }  from  '../../services/ui-worker-api ' ; 
36+ import  {  HttpBody  }  from  './http-body ' ; 
5037import  { 
5138    RequestBreakpoint , 
5239    ResponseBreakpoint , 
@@ -122,82 +109,6 @@ function addResponseMetadata(response: InputResponse): HtkResponse {
122109    } )  as  HtkResponse ; 
123110} 
124111
125- export  class  HttpBody  implements  MessageBody  { 
126- 
127-     constructor ( 
128-         message : InputMessage  |  {  body : Uint8Array  } , 
129-         headers : Headers  |  RawHeaders 
130-     )  { 
131-         if  ( ! ( 'body'  in  message )  ||  ! message . body )  { 
132-             this . _encoded  =  stringToBuffer ( "" ) ; 
133-         }  else  if  ( 'buffer'  in  message . body )  { 
134-             this . _encoded  =  message . body . buffer ; 
135-         }  else  { 
136-             this . _encoded  =  fakeBuffer ( message . body . encodedLength ) ; 
137-             this . _decoded  =  message . body . decoded ; 
138-         } 
139- 
140-         this . _contentEncoding  =  asHeaderArray ( getHeaderValues ( headers ,  'content-encoding' ) ) ; 
141-     } 
142- 
143-     private  _contentEncoding : string [ ] ; 
144-     private  _encoded : FakeBuffer  |  Buffer ; 
145-     get  encoded ( )  { 
146-         return  this . _encoded ; 
147-     } 
148- 
149-     private  _decoded : Buffer  |  undefined ; 
150- 
151-     @observable 
152-     decodingError : Error  |  undefined ; 
153- 
154-     decodedPromise : ObservablePromise < Buffer  |  undefined >  =  lazyObservablePromise ( async  ( )  =>  { 
155-         // Exactly one of _encoded & _decoded is a buffer, never neither/both. 
156-         if  ( this . _decoded )  return  this . _decoded ; 
157-         const  encodedBuffer  =  this . encoded  as  Buffer ; 
158- 
159-         // Temporarily change to a fake buffer, while the web worker takes the data to decode 
160-         const  encodedLength  =  encodedBuffer . byteLength ; 
161-         this . _encoded  =  fakeBuffer ( encodedLength ) ; 
162- 
163-         try  { 
164-             const  {  decoded,  encoded }  =  await  decodeBody ( encodedBuffer ,  this . _contentEncoding ) ; 
165-             this . _encoded  =  encoded ; 
166-             return  decoded ; 
167-         }  catch  ( e : any )  { 
168-             logError ( e ) ; 
169- 
170-             // In most cases, we get the encoded data back regardless, so recapture it here: 
171-             if  ( e . inputBuffer )  { 
172-                 this . _encoded  =  e . inputBuffer ; 
173-             } 
174-             runInAction ( ( )  =>  { 
175-                 this . decodingError  =  e ; 
176-             } ) ; 
177- 
178-             return  undefined ; 
179-         } 
180-     } ) ; 
181- 
182-     get  decoded ( )  { 
183-         // We exclude 'Error' from the value - errors should always become undefined 
184-         return  this . decodedPromise . value  as  Buffer  |  undefined ; 
185-     } 
186- 
187-     // Must only be called when the exchange & body will no longer be used. Ensures that large data is 
188-     // definitively unlinked, since some browser issues can result in exchanges not GCing immediately. 
189-     // Important: for safety, this leaves the body in a *VALID* but reset state - not a totally blank one. 
190-     cleanup ( )  { 
191-         const  emptyBuffer  =  Buffer . from ( [ ] ) ; 
192- 
193-         // Set to a valid state for an un-decoded but totally empty body. 
194-         this . _decoded  =  undefined ; 
195-         this . _encoded  =  emptyBuffer ; 
196-         this . decodingError  =  undefined ; 
197-         this . decodedPromise  =  observablePromise ( Promise . resolve ( emptyBuffer ) ) ; 
198-     } 
199- } 
200- 
201112export  type  CompletedRequest  =  Omit < ViewableHttpExchange ,  'request' >  &  { 
202113    matchedRule : {  id : string ,  handlerRype : HandlerClassKey  }  |  false 
203114} ; 
0 commit comments