@@ -3,8 +3,6 @@ import { join } from "path";
3
3
4
4
import { SetupServer } from "msw/node" ;
5
5
6
- import fetch from "node-fetch" ;
7
-
8
6
import { DisposableObject } from "../disposable-object" ;
9
7
10
8
import {
@@ -14,14 +12,12 @@ import {
14
12
} from "./gh-api-request" ;
15
13
16
14
export class Recorder extends DisposableObject {
17
- private readonly allRequests = new Map < string , Request > ( ) ;
18
15
private currentRecordedScenario : GitHubApiRequest [ ] = [ ] ;
19
16
20
17
private _isRecording = false ;
21
18
22
19
constructor ( private readonly server : SetupServer ) {
23
20
super ( ) ;
24
- this . onRequestStart = this . onRequestStart . bind ( this ) ;
25
21
this . onResponseBypass = this . onResponseBypass . bind ( this ) ;
26
22
}
27
23
@@ -42,7 +38,6 @@ export class Recorder extends DisposableObject {
42
38
43
39
this . clear ( ) ;
44
40
45
- this . server . events . on ( "request:start" , this . onRequestStart ) ;
46
41
this . server . events . on ( "response:bypass" , this . onResponseBypass ) ;
47
42
}
48
43
@@ -53,13 +48,11 @@ export class Recorder extends DisposableObject {
53
48
54
49
this . _isRecording = false ;
55
50
56
- this . server . events . removeListener ( "request:start" , this . onRequestStart ) ;
57
51
this . server . events . removeListener ( "response:bypass" , this . onResponseBypass ) ;
58
52
}
59
53
60
54
public clear ( ) {
61
55
this . currentRecordedScenario = [ ] ;
62
- this . allRequests . clear ( ) ;
63
56
}
64
57
65
58
public async save ( scenariosPath : string , name : string ) : Promise < string > {
@@ -109,34 +102,14 @@ export class Recorder extends DisposableObject {
109
102
return scenarioDirectory ;
110
103
}
111
104
112
- private onRequestStart ( request : Request , requestId : string ) : void {
113
- if ( request . headers . has ( "x-vscode-codeql-msw-bypass" ) ) {
114
- return ;
115
- }
116
-
117
- this . allRequests . set ( requestId , request ) ;
118
- }
119
-
120
105
private async onResponseBypass (
121
106
response : Response ,
122
- _ : Request ,
123
- requestId : string ,
107
+ request : Request ,
108
+ _requestId : string ,
124
109
) : Promise < void > {
125
- const request = this . allRequests . get ( requestId ) ;
126
- this . allRequests . delete ( requestId ) ;
127
- if ( ! request ) {
128
- return ;
129
- }
130
-
131
- if ( response . body === undefined ) {
132
- return ;
133
- }
134
-
135
110
const gitHubApiRequest = await createGitHubApiRequest (
136
- request . url . toString ( ) ,
137
- response . status ,
138
- response . body ?. toString ( ) || "" ,
139
- response . headers ,
111
+ request . url ,
112
+ response ,
140
113
) ;
141
114
if ( ! gitHubApiRequest ) {
142
115
return ;
@@ -148,22 +121,23 @@ export class Recorder extends DisposableObject {
148
121
149
122
async function createGitHubApiRequest (
150
123
url : string ,
151
- status : number ,
152
- body : string ,
153
- headers : globalThis . Headers ,
124
+ response : Response ,
154
125
) : Promise < GitHubApiRequest | undefined > {
155
126
if ( ! url ) {
156
127
return undefined ;
157
128
}
158
129
130
+ const status = response . status ;
131
+ const headers = response . headers ;
132
+
159
133
if ( url . match ( / \/ r e p o s \/ [ a - z A - Z 0 - 9 - _ .] + \/ [ a - z A - Z 0 - 9 - _ .] + $ / ) ) {
160
134
return {
161
135
request : {
162
136
kind : RequestKind . GetRepo ,
163
137
} ,
164
138
response : {
165
139
status,
166
- body : JSON . parse ( body ) ,
140
+ body : await response . json ( ) ,
167
141
} ,
168
142
} ;
169
143
}
@@ -177,7 +151,7 @@ async function createGitHubApiRequest(
177
151
} ,
178
152
response : {
179
153
status,
180
- body : JSON . parse ( body ) ,
154
+ body : await response . json ( ) ,
181
155
} ,
182
156
} ;
183
157
}
@@ -193,7 +167,7 @@ async function createGitHubApiRequest(
193
167
} ,
194
168
response : {
195
169
status,
196
- body : JSON . parse ( body ) ,
170
+ body : await response . json ( ) ,
197
171
} ,
198
172
} ;
199
173
}
@@ -209,7 +183,7 @@ async function createGitHubApiRequest(
209
183
} ,
210
184
response : {
211
185
status,
212
- body : JSON . parse ( body ) ,
186
+ body : await response . json ( ) ,
213
187
} ,
214
188
} ;
215
189
}
@@ -219,25 +193,14 @@ async function createGitHubApiRequest(
219
193
/ o b j e c t s - o r i g i n \. g i t h u b u s e r c o n t e n t \. c o m \/ c o d e q l - q u e r y - c o n s o l e \/ c o d e q l - v a r i a n t - a n a l y s i s - r e p o - t a s k s \/ \d + \/ (?< repositoryId > \d + ) / ,
220
194
) ;
221
195
if ( repoDownloadMatch ?. groups ?. repositoryId ) {
222
- // msw currently doesn't support binary response bodies, so we need to download this separately
223
- // see https://github.com/mswjs/interceptors/blob/15eafa6215a328219999403e3ff110e71699b016/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts#L24-L33
224
- // Essentially, mws is trying to decode a ZIP file as UTF-8 which changes the bytes and corrupts the file.
225
- const response = await fetch ( url , {
226
- headers : {
227
- // We need to ensure we don't end up in an infinite loop, since this request will also be intercepted
228
- "x-vscode-codeql-msw-bypass" : "true" ,
229
- } ,
230
- } ) ;
231
- const responseBuffer = await response . buffer ( ) ;
232
-
233
196
return {
234
197
request : {
235
198
kind : RequestKind . GetVariantAnalysisRepoResult ,
236
199
repositoryId : parseInt ( repoDownloadMatch . groups . repositoryId , 10 ) ,
237
200
} ,
238
201
response : {
239
202
status,
240
- body : responseBuffer ,
203
+ body : Buffer . from ( await response . arrayBuffer ( ) ) ,
241
204
contentType : headers . get ( "content-type" ) ?? "application/octet-stream" ,
242
205
} ,
243
206
} ;
@@ -252,7 +215,7 @@ async function createGitHubApiRequest(
252
215
} ,
253
216
response : {
254
217
status,
255
- body : JSON . parse ( body ) ,
218
+ body : await response . json ( ) ,
256
219
} ,
257
220
} ;
258
221
}
@@ -267,7 +230,7 @@ async function createGitHubApiRequest(
267
230
} ,
268
231
response : {
269
232
status,
270
- body : JSON . parse ( body ) ,
233
+ body : await response . json ( ) ,
271
234
} ,
272
235
} ;
273
236
}
0 commit comments