Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle media errors #20

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading