Skip to content

Commit d721746

Browse files
fix: bound image attachment downloads (#1625)
1 parent 5da4c76 commit d721746

2 files changed

Lines changed: 144 additions & 14 deletions

File tree

‎src/github/utils/image-downloader.ts‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const SIGNED_URL_HOST = "private-user-images.githubusercontent.com";
3434
const SIGNED_URL_PATH_REGEX =
3535
/^\/[^/]+\/[^/]*-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:\.[a-z0-9]+)?$/i;
3636

37+
const DEFAULT_IMAGE_DOWNLOAD_TIMEOUT_MS = 30_000;
38+
3739
function extractSignedUrlAssetGuid(signedUrl: string): string | undefined {
3840
let parsed: URL;
3941
try {
@@ -85,13 +87,19 @@ export type CommentWithImages =
8587
| IssueBody
8688
| PullRequestBody;
8789

90+
type ImageDownloadOptions = {
91+
timeoutMs?: number;
92+
};
93+
8894
export async function downloadCommentImages(
8995
octokits: Octokits,
9096
owner: string,
9197
repo: string,
9298
comments: CommentWithImages[],
99+
options: ImageDownloadOptions = {},
93100
): Promise<Map<string, string>> {
94101
const urlToPathMap = new Map<string, string>();
102+
const timeoutMs = options.timeoutMs ?? DEFAULT_IMAGE_DOWNLOAD_TIMEOUT_MS;
95103
const downloadsDir = "/tmp/github-images";
96104

97105
await fs.mkdir(downloadsDir, { recursive: true });
@@ -241,15 +249,7 @@ export async function downloadCommentImages(
241249
try {
242250
console.log(`Downloading ${originalUrl}...`);
243251

244-
const imageResponse = await fetch(signedUrl);
245-
if (!imageResponse.ok) {
246-
throw new Error(
247-
`HTTP ${imageResponse.status}: ${imageResponse.statusText}`,
248-
);
249-
}
250-
251-
const arrayBuffer = await imageResponse.arrayBuffer();
252-
const buffer = Buffer.from(arrayBuffer);
252+
const buffer = await fetchImage(signedUrl, timeoutMs);
253253

254254
// GitHub user-attachment URLs (/user-attachments/assets/<uuid>) carry
255255
// no file extension, so the URL-based guess silently falls back to
@@ -289,6 +289,37 @@ export async function downloadCommentImages(
289289
return urlToPathMap;
290290
}
291291

292+
async function fetchImage(url: string, timeoutMs: number): Promise<Buffer> {
293+
const controller = new AbortController();
294+
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
295+
const timeoutPromise = new Promise<never>((_, reject) => {
296+
timeoutHandle = setTimeout(() => {
297+
controller.abort();
298+
reject(new Error(`Image download timed out after ${timeoutMs}ms`));
299+
}, timeoutMs);
300+
});
301+
302+
try {
303+
const response = await Promise.race([
304+
fetch(url, { signal: controller.signal }),
305+
timeoutPromise,
306+
]);
307+
if (!response.ok) {
308+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
309+
}
310+
311+
const arrayBuffer = await Promise.race([
312+
response.arrayBuffer(),
313+
timeoutPromise,
314+
]);
315+
return Buffer.from(arrayBuffer);
316+
} finally {
317+
if (timeoutHandle !== undefined) {
318+
clearTimeout(timeoutHandle);
319+
}
320+
}
321+
}
322+
292323
function getImageExtension(url: string): string {
293324
const urlParts = url.split("/");
294325
const filename = urlParts[urlParts.length - 1];

‎test/image-downloader.test.ts‎

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ describe("downloadCommentImages", () => {
148148
mediaType: { format: "full+json" },
149149
});
150150

151-
expect(fetchSpy).toHaveBeenCalledWith(signedUrl);
151+
expect(fetchSpy).toHaveBeenCalledWith(signedUrl, {
152+
signal: expect.any(AbortSignal),
153+
});
152154
expect(fsWriteFileSpy).toHaveBeenCalledWith(
153155
"/tmp/github-images/image-1704067200000-0.png",
154156
Buffer.from(mockArrayBuffer),
@@ -481,8 +483,12 @@ describe("downloadCommentImages", () => {
481483
);
482484

483485
expect(fetchSpy).toHaveBeenCalledTimes(2);
484-
expect(fetchSpy).toHaveBeenNthCalledWith(1, signedUrl1);
485-
expect(fetchSpy).toHaveBeenNthCalledWith(2, signedUrl2);
486+
expect(fetchSpy).toHaveBeenNthCalledWith(1, signedUrl1, {
487+
signal: expect.any(AbortSignal),
488+
});
489+
expect(fetchSpy).toHaveBeenNthCalledWith(2, signedUrl2, {
490+
signal: expect.any(AbortSignal),
491+
});
486492
expect(result.get(imageUrl1)).toBe(
487493
"/tmp/github-images/image-1704067200000-0.png",
488494
);
@@ -523,7 +529,9 @@ describe("downloadCommentImages", () => {
523529
comments,
524530
);
525531

526-
expect(fetchSpy).toHaveBeenCalledWith(signedUrl);
532+
expect(fetchSpy).toHaveBeenCalledWith(signedUrl, {
533+
signal: expect.any(AbortSignal),
534+
});
527535
expect(result.get(imageUrl)).toBe(
528536
"/tmp/github-images/image-1704067200000-0.png",
529537
);
@@ -766,6 +774,95 @@ describe("downloadCommentImages", () => {
766774
);
767775
});
768776

777+
test("should skip an image when the fetch times out", async () => {
778+
const mockOctokit = createMockOctokit();
779+
const imageUrl = assetUrl(GUID_1);
780+
const signedUrl = signedUrlFor(GUID_1, ".png");
781+
let signal: AbortSignal | null | undefined;
782+
783+
// @ts-expect-error Mock implementation doesn't match full type signature
784+
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
785+
data: {
786+
body_html: `<img src="${signedUrl}">`,
787+
},
788+
});
789+
790+
fetchSpy = spyOn(global, "fetch");
791+
fetchSpy.mockImplementation((_input: unknown, init?: RequestInit) => {
792+
signal = init?.signal;
793+
return new Promise<Response>(() => {});
794+
});
795+
796+
const result = await downloadCommentImages(
797+
mockOctokit,
798+
"owner",
799+
"repo",
800+
[
801+
{
802+
type: "issue_comment",
803+
id: "445",
804+
body: `Stalled image: ![stalled](${imageUrl})`,
805+
},
806+
],
807+
{ timeoutMs: 5 },
808+
);
809+
810+
expect(result.size).toBe(0);
811+
expect(signal?.aborted).toBe(true);
812+
expect(consoleErrorSpy).toHaveBeenCalledWith(
813+
expect.stringContaining("Failed to download"),
814+
expect.objectContaining({
815+
message: "Image download timed out after 5ms",
816+
}),
817+
);
818+
});
819+
820+
test("should time out while reading a response body", async () => {
821+
const mockOctokit = createMockOctokit();
822+
const imageUrl = assetUrl(GUID_1);
823+
const signedUrl = signedUrlFor(GUID_1, ".png");
824+
let signal: AbortSignal | null | undefined;
825+
826+
// @ts-expect-error Mock implementation doesn't match full type signature
827+
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
828+
data: {
829+
body_html: `<img src="${signedUrl}">`,
830+
},
831+
});
832+
833+
fetchSpy = spyOn(global, "fetch");
834+
fetchSpy.mockImplementation((_input: unknown, init?: RequestInit) => {
835+
signal = init?.signal;
836+
return Promise.resolve({
837+
ok: true,
838+
arrayBuffer: () => new Promise<ArrayBuffer>(() => {}),
839+
} as Response);
840+
});
841+
842+
const result = await downloadCommentImages(
843+
mockOctokit,
844+
"owner",
845+
"repo",
846+
[
847+
{
848+
type: "issue_comment",
849+
id: "446",
850+
body: `Stalled body: ![stalled](${imageUrl})`,
851+
},
852+
],
853+
{ timeoutMs: 5 },
854+
);
855+
856+
expect(result.size).toBe(0);
857+
expect(signal?.aborted).toBe(true);
858+
expect(consoleErrorSpy).toHaveBeenCalledWith(
859+
expect.stringContaining("Failed to download"),
860+
expect.objectContaining({
861+
message: "Image download timed out after 5ms",
862+
}),
863+
);
864+
});
865+
769866
test("should handle API errors gracefully", async () => {
770867
const mockOctokit = createMockOctokit();
771868
const imageUrl = assetUrl(GUID_1);
@@ -936,7 +1033,9 @@ describe("downloadCommentImages", () => {
9361033
mediaType: { format: "full+json" },
9371034
});
9381035

939-
expect(fetchSpy).toHaveBeenCalledWith(signedUrl);
1036+
expect(fetchSpy).toHaveBeenCalledWith(signedUrl, {
1037+
signal: expect.any(AbortSignal),
1038+
});
9401039
expect(fsWriteFileSpy).toHaveBeenCalledWith(
9411040
"/tmp/github-images/image-1704067200000-0.png",
9421041
Buffer.from(mockArrayBuffer),

0 commit comments

Comments
 (0)