Skip to content

Commit 6539417

Browse files
committed
Fix recording of binary response bodies
1 parent 095d56e commit 6539417

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

extensions/ql-vscode/src/common/mock-gh-api/recorder.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ensureDir, writeFile } from "fs-extra";
22
import { join } from "path";
33

4+
import fetch from "node-fetch";
45
import { SetupServer } from "msw/node";
56

67
import { DisposableObject } from "../disposable-object";
@@ -116,6 +117,10 @@ export class Recorder extends DisposableObject {
116117
request: Request,
117118
_requestId: string,
118119
): Promise<void> {
120+
if (request.headers.has("x-vscode-codeql-msw-bypass")) {
121+
return;
122+
}
123+
119124
const gitHubApiRequest = await createGitHubApiRequest(
120125
request.url,
121126
response,
@@ -137,7 +142,6 @@ async function createGitHubApiRequest(
137142
}
138143

139144
const status = response.status;
140-
const headers = response.headers;
141145

142146
if (url.match(/\/repos\/[a-zA-Z0-9-_.]+\/[a-zA-Z0-9-_.]+$/)) {
143147
return {
@@ -210,15 +214,27 @@ async function createGitHubApiRequest(
210214
/objects-origin\.githubusercontent\.com\/codeql-query-console\/codeql-variant-analysis-repo-tasks\/\d+\/(?<repositoryId>\d+)/,
211215
);
212216
if (repoDownloadMatch?.groups?.repositoryId) {
217+
// msw currently doesn't support binary response bodies, so we need to download this separately
218+
// see https://github.com/mswjs/interceptors/blob/15eafa6215a328219999403e3ff110e71699b016/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts#L24-L33
219+
// Essentially, mws is trying to decode a ZIP file as UTF-8 which changes the bytes and corrupts the file.
220+
const response = await fetch(url, {
221+
headers: {
222+
// We need to ensure we don't end up in an infinite loop, since this request will also be intercepted
223+
"x-vscode-codeql-msw-bypass": "true",
224+
},
225+
});
226+
const responseBuffer = await response.buffer();
227+
213228
return {
214229
request: {
215230
kind: RequestKind.GetVariantAnalysisRepoResult,
216231
repositoryId: parseInt(repoDownloadMatch.groups.repositoryId, 10),
217232
},
218233
response: {
219-
status,
220-
body: Buffer.from(await responseBody(response)),
221-
contentType: headers.get("content-type") ?? "application/octet-stream",
234+
status: response.status,
235+
body: responseBuffer,
236+
contentType:
237+
response.headers.get("content-type") ?? "application/octet-stream",
222238
},
223239
};
224240
}

0 commit comments

Comments
 (0)