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

fix: a bug about crashes down when there are two slashes. #6855

Merged
merged 9 commits into from
Sep 16, 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
5 changes: 5 additions & 0 deletions .changeset/forty-humans-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik': patch
---

gracefully handle image dimensions service errors
52 changes: 28 additions & 24 deletions packages/qwik/src/optimizer/src/plugins/image-size-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import heif_1 from 'image-size/dist/types/heif.js';

import type { Connect } from 'vite';
import type { OptimizerSystem } from '../types';
import { formatError } from './vite-utils';

// This map helps avoid validating for every single image type
const firstBytes: Record<number, keyof typeof types> = {
Expand Down Expand Up @@ -110,30 +111,30 @@ export const getImageSizeServer = (
srcDir: string
): Connect.NextHandleFunction => {
return async (req, res, next) => {
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');
const path: typeof import('path') = await sys.dynamicImport('node:path');
try {
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');
const path: typeof import('path') = await sys.dynamicImport('node:path');

const url = new URL(req.url!, 'http://localhost:3000/');
if (req.method === 'GET' && url.pathname === '/__image_info') {
const imageURL = url.searchParams.get('url');
res.setHeader('content-type', 'application/json');
if (imageURL) {
const info = await getInfoForSrc(imageURL);
res.setHeader('cache-control', 'public, max-age=31536000, immutable');
if (!info) {
res.statusCode = 404;
const url = new URL(req.url!, 'http://localhost:3000/');
if (req.method === 'GET' && url.pathname === '/__image_info') {
const imageURL = url.searchParams.get('url');
res.setHeader('content-type', 'application/json');
if (imageURL) {
const info = await getInfoForSrc(imageURL);
res.setHeader('cache-control', 'public, max-age=31536000, immutable');
if (!info) {
res.statusCode = 404;
} else {
res.write(JSON.stringify(info));
}
} else {
res.statusCode = 500;
const info = { message: 'error' };
res.write(JSON.stringify(info));
}
} else {
res.statusCode = 500;
const info = { message: 'error' };
res.write(JSON.stringify(info));
}
res.end();
return;
} else if (req.method === 'POST' && url.pathname === '/__image_fix') {
try {
res.end();
return;
} else if (req.method === 'POST' && url.pathname === '/__image_fix') {
const loc = url.searchParams.get('loc') as string;
const width = url.searchParams.get('width');
const height = url.searchParams.get('height');
Expand Down Expand Up @@ -232,11 +233,14 @@ export const getImageSizeServer = (
}
text = text.slice(0, offset) + imgTag + text.slice(end);
fs.writeFileSync(filePath, text);
} catch (e) {
console.error('Error auto fixing image', e, url);
} else {
next();
}
} catch (e) {
if (e instanceof Error) {
await formatError(sys, e);
}
} else {
next();
next(e);
}
};
};
Expand Down