@@ -4,12 +4,21 @@ import { join } from "path";
44import { SetupServer } from "msw/node" ;
55
66import { DisposableObject } from "../disposable-object" ;
7+ import { gzipDecode } from "../zlib" ;
78
89import {
10+ AutoModelResponse ,
11+ BasicErorResponse ,
12+ CodeSearchResponse ,
913 GetVariantAnalysisRepoResultRequest ,
1014 GitHubApiRequest ,
1115 RequestKind ,
1216} from "./gh-api-request" ;
17+ import {
18+ VariantAnalysis ,
19+ VariantAnalysisRepoTask ,
20+ } from "../../variant-analysis/gh-api/variant-analysis" ;
21+ import { Repository } from "../../variant-analysis/gh-api/repository" ;
1322
1423export class Recorder extends DisposableObject {
1524 private currentRecordedScenario : GitHubApiRequest [ ] = [ ] ;
@@ -81,7 +90,7 @@ export class Recorder extends DisposableObject {
8190
8291 let bodyFileLink = undefined ;
8392 if ( writtenRequest . response . body ) {
84- await writeFile ( bodyFilePath , writtenRequest . response . body || "" ) ;
93+ await writeFile ( bodyFilePath , writtenRequest . response . body ) ;
8594 bodyFileLink = `file:${ bodyFileName } ` ;
8695 }
8796
@@ -137,7 +146,9 @@ async function createGitHubApiRequest(
137146 } ,
138147 response : {
139148 status,
140- body : await response . json ( ) ,
149+ body : await jsonResponseBody <
150+ Repository | BasicErorResponse | undefined
151+ > ( response ) ,
141152 } ,
142153 } ;
143154 }
@@ -151,7 +162,9 @@ async function createGitHubApiRequest(
151162 } ,
152163 response : {
153164 status,
154- body : await response . json ( ) ,
165+ body : await jsonResponseBody <
166+ VariantAnalysis | BasicErorResponse | undefined
167+ > ( response ) ,
155168 } ,
156169 } ;
157170 }
@@ -167,7 +180,9 @@ async function createGitHubApiRequest(
167180 } ,
168181 response : {
169182 status,
170- body : await response . json ( ) ,
183+ body : await jsonResponseBody <
184+ VariantAnalysis | BasicErorResponse | undefined
185+ > ( response ) ,
171186 } ,
172187 } ;
173188 }
@@ -183,7 +198,9 @@ async function createGitHubApiRequest(
183198 } ,
184199 response : {
185200 status,
186- body : await response . json ( ) ,
201+ body : await jsonResponseBody <
202+ VariantAnalysisRepoTask | BasicErorResponse | undefined
203+ > ( response ) ,
187204 } ,
188205 } ;
189206 }
@@ -200,7 +217,7 @@ async function createGitHubApiRequest(
200217 } ,
201218 response : {
202219 status,
203- body : Buffer . from ( await response . arrayBuffer ( ) ) ,
220+ body : Buffer . from ( await responseBody ( response ) ) ,
204221 contentType : headers . get ( "content-type" ) ?? "application/octet-stream" ,
205222 } ,
206223 } ;
@@ -215,7 +232,9 @@ async function createGitHubApiRequest(
215232 } ,
216233 response : {
217234 status,
218- body : await response . json ( ) ,
235+ body : await jsonResponseBody <
236+ CodeSearchResponse | BasicErorResponse | undefined
237+ > ( response ) ,
219238 } ,
220239 } ;
221240 }
@@ -230,14 +249,36 @@ async function createGitHubApiRequest(
230249 } ,
231250 response : {
232251 status,
233- body : await response . json ( ) ,
252+ body : await jsonResponseBody <
253+ BasicErorResponse | AutoModelResponse | undefined
254+ > ( response ) ,
234255 } ,
235256 } ;
236257 }
237258
238259 return undefined ;
239260}
240261
262+ async function responseBody ( response : Response ) : Promise < Uint8Array > {
263+ const body = await response . arrayBuffer ( ) ;
264+ const view = new Uint8Array ( body ) ;
265+
266+ if ( view [ 0 ] === 0x1f && view [ 1 ] === 0x8b ) {
267+ // Response body is gzipped, so we need to un-gzip it.
268+
269+ return await gzipDecode ( view ) ;
270+ } else {
271+ return view ;
272+ }
273+ }
274+
275+ async function jsonResponseBody < T > ( response : Response ) : Promise < T > {
276+ const body = await responseBody ( response ) ;
277+ const text = new TextDecoder ( "utf-8" ) . decode ( body ) ;
278+
279+ return JSON . parse ( text ) ;
280+ }
281+
241282function shouldWriteBodyToFile (
242283 request : GitHubApiRequest ,
243284) : request is GetVariantAnalysisRepoResultRequest {
0 commit comments