@@ -4,32 +4,19 @@ import { observable, computed, action, runInAction, when } from 'mobx';
4
4
import {
5
5
HtkRequest ,
6
6
HtkResponse ,
7
- Headers ,
8
- MessageBody ,
9
7
InputRequest ,
10
8
InputResponse ,
11
9
InputFailedRequest ,
12
10
TimingEvents ,
13
- InputMessage ,
14
11
MockttpBreakpointedRequest ,
15
12
MockttpBreakpointedResponse ,
16
13
InputCompletedRequest ,
17
14
MockttpBreakpointResponseResult ,
18
- InputRuleEventDataMap ,
19
- RawHeaders
15
+ InputRuleEventDataMap
20
16
} from "../../types" ;
21
- import {
22
- fakeBuffer ,
23
- FakeBuffer ,
24
- stringToBuffer ,
25
- } from '../../util/buffer' ;
26
17
import { 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' ;
33
20
import { ParsedUrl } from '../../util/url' ;
34
21
35
22
import { logError } from '../../errors' ;
@@ -46,7 +33,7 @@ import { OpenApiExchange } from '../api/openapi';
46
33
import { parseRpcApiExchange } from '../api/jsonrpc' ;
47
34
import { ApiMetadata } from '../api/api-interfaces' ;
48
35
49
- import { decodeBody } from '../../services/ui-worker-api ' ;
36
+ import { HttpBody } from './http-body ' ;
50
37
import {
51
38
RequestBreakpoint ,
52
39
ResponseBreakpoint ,
@@ -122,82 +109,6 @@ function addResponseMetadata(response: InputResponse): HtkResponse {
122
109
} ) as HtkResponse ;
123
110
}
124
111
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
-
201
112
export type CompletedRequest = Omit < ViewableHttpExchange , 'request' > & {
202
113
matchedRule : { id : string , handlerRype : HandlerClassKey } | false
203
114
} ;
0 commit comments