Skip to content

Commit

Permalink
Handle media errors
Browse files Browse the repository at this point in the history
## Summary

- Proxy should pass through non-2xx status codes to the caller for media requests
- Adds a test to verify
  • Loading branch information
neekolas authored Mar 25, 2024
1 parent 9da3649 commit d6d6be2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/server/src/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('media', () => {
headers['content-type'] = contentType;
}
const filePath = `fixtures/${path}`;
const file = await readFile(filePath);
try {
const file = await readFile(filePath);
res.writeHead(200, headers);
res.end(file);
} catch (e) {
Expand Down Expand Up @@ -109,4 +109,12 @@ describe('media', () => {
// Use the content type from the media server, not just the file name
expect(response.headers.get('content-type')).toEqual('image/png');
});

test('should pass through status when non-200', async () => {
const mediaUrl = `http://localhost:${MEDIA_PORT}/doesnotexist.png`;
const mediaRequest = new Request(`http://localhost/media?url=${encodeURIComponent(mediaUrl)}`);
const response = await handleMedia(mediaRequest);
// Use the content type from the media server, not just the file name
expect(response.status).toEqual(404);
});
});
8 changes: 8 additions & 0 deletions packages/server/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export async function handleMedia(req: Request) {
const media = await fetch(url, {
headers: getProxySafeMediaHeaders(req),
});

if (!media.ok) {
return new Response(media.body, {
headers: { ...CORS_HEADERS },
status: media.status,
});
}

// This will include the cache control headers
const mediaHeaders = Object.fromEntries(media.headers.entries());
const responseHeaders = new Headers({ ...mediaHeaders, ...CORS_HEADERS });
Expand Down

0 comments on commit d6d6be2

Please sign in to comment.