Repository: withastro/astro
File: packages/astro/src/assets/build/generate.ts (TypeScript)

Code before:
				if (!JSONData.data || !JSONData.expires) {
					await fs.promises.unlink(cachedFileURL);

					throw new Error(
						`Malformed cache entry for ${filepath}, cache will be regenerated for this file.`,
					);
				}

				// If the cache entry is not expired, use it
				if (JSONData.expires > Date.now()) {
					await fs.promises.writeFile(finalFileURL, Buffer.from(JSONData.data, 'base64'));

					return {
						cached: CacheStatus.Hit,
					};
				}

				// Try to revalidate the cache
				if (JSONData.etag) {
					const revalidatedData = await revalidateRemoteImage(options.src as string, JSONData.etag);

					if (revalidatedData.data.length) {
						// Image cache was stale, update original image to avoid redownload
						originalImage = revalidatedData;
					} else {
						revalidatedData.data = Buffer.from(JSONData.data, 'base64');

						// Freshen cache on disk
						await writeRemoteCacheFile(cachedFileURL, revalidatedData);

						await fs.promises.writeFile(finalFileURL, revalidatedData.data);
						return { cached: CacheStatus.Revalidated };
					}
				}

				await fs.promises.unlink(cachedFileURL);
			}
		} catch (e: any) {
			if (e.code !== 'ENOENT') {
				throw new Error(`An error was encountered while reading the cache file. Error: ${e}`);
			}
			// If the cache file doesn't exist, just move on, and we'll generate it
		}

		// The original filepath or URL from the image transform
		const originalImagePath = isLocalImage
			? (options.src as ImageMetadata).src
			: (options.src as string);

		if (!originalImage) {
			originalImage = await loadImage(originalFilePath, env);

Review comment (bug):
Should we at least use a `try/catch` with `Buffer.from`? In case it fails, should we should handle the error gracefully 

Code after:
						`Malformed cache entry for ${filepath}, cache will be regenerated for this file.`,
					);
				}

				// If the cache entry is not expired, use it
				if (JSONData.expires > Date.now()) {
					await fs.promises.writeFile(finalFileURL, Buffer.from(JSONData.data, 'base64'));

					return {
						cached: 'hit',
					};
				}

				// Try to revalidate the cache
				if (JSONData.etag || JSONData.lastModified) {
					try {
						const revalidatedData = await revalidateRemoteImage(options.src as string, {
							etag: JSONData.etag,
							lastModified: JSONData.lastModified,
						});

						if (revalidatedData.data.length) {
							// Image cache was stale, update original image to avoid redownload
							originalImage = revalidatedData;
						} else {
							revalidatedData.data = Buffer.from(JSONData.data, 'base64');

							// Freshen cache on disk
							await writeRemoteCacheFile(cachedFileURL, revalidatedData, env);

							await fs.promises.writeFile(finalFileURL, revalidatedData.data);
							return { cached: 'revalidated' };
						}
					} catch (e) {
						// Reuse stale cache if revalidation fails
						env.logger.warn(
							null,
							`An error was encountered while revalidating a cached remote asset. Proceeding with stale cache. ${e}`,
						);

						await fs.promises.writeFile(finalFileURL, Buffer.from(JSONData.data, 'base64'));
						return { cached: 'hit' };
					}
				}

				await fs.promises.unlink(cachedFileURL);
			}
		} catch (e: any) {
			if (e.code !== 'ENOENT') {
				throw new Error(`An error was encountered while reading the cache file. Error: ${e}`);
			}