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

Expose inferRemoteSize function #11098

Merged
merged 16 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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/large-geese-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": minor
---

Exposes the `inferRemoteSize` function used by `inferSize`
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/astro/src/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getConfiguredImageService, getImage } from './internal.js';
export { baseService, isLocalService } from './services/service.js';
export { inferRemoteSize } from './utils/remoteProbe.js';
export { type LocalImageProps, type RemoteImageProps } from './types.js';
6 changes: 3 additions & 3 deletions packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
UnresolvedImageTransform,
} from './types.js';
import { isESMImportedImage, isRemoteImage, resolveSrc } from './utils/imageKind.js';
import { probe } from './utils/remoteProbe.js';
import { inferRemoteSize } from './utils/remoteProbe.js';

export async function getConfiguredImageService(): Promise<ImageService> {
if (!globalThis?.astroAsset?.imageService) {
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function getImage(
// Infer size for remote images if inferSize is true
if (options.inferSize && isRemoteImage(resolvedOptions.src)) {
try {
const result = await probe(resolvedOptions.src); // Directly probe the image URL
const result = await inferRemoteSize(resolvedOptions.src); // Directly probe the image URL
resolvedOptions.width ??= result.width;
resolvedOptions.height ??= result.height;
delete resolvedOptions.inferSize; // Delete so it doesn't end up in the attributes
Expand All @@ -82,7 +82,7 @@ export async function getImage(
// Causing our generate step to think the image is used outside of the image optimization pipeline
const clonedSrc = isESMImportedImage(resolvedOptions.src)
? // @ts-expect-error - clone is a private, hidden prop
resolvedOptions.src.clone ?? resolvedOptions.src
resolvedOptions.src.clone ?? resolvedOptions.src
: resolvedOptions.src;

resolvedOptions.src = clonedSrc;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/assets/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {
type RemotePattern,
} from './remotePattern.js';
export { hashTransform, propsToFilename } from './transformToPath.js';
export { inferRemoteSize } from './remoteProbe.js';
12 changes: 7 additions & 5 deletions packages/astro/src/assets/utils/remoteProbe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { lookup } from './vendor/image-size/lookup.js';
import type { ISize } from './vendor/image-size/types/interface.ts';
import type { ImageMetadata } from '../types.js';
import { imageMetadata } from './metadata.js';

export async function probe(url: string): Promise<ISize> {
export async function inferRemoteSize(url: string): Promise<Omit<ImageMetadata, 'src' | 'fsPath'>> {
// Start fetching the image
const response = await fetch(url);
if (!response.body || !response.ok) {
Expand Down Expand Up @@ -31,13 +31,15 @@ export async function probe(url: string): Promise<ISize> {

try {
// Attempt to determine the size with each new chunk
const dimensions = lookup(accumulatedChunks);
const dimensions = await imageMetadata(accumulatedChunks, url);

if (dimensions) {
await reader.cancel(); // stop stream as we have size now

return dimensions;
}
} catch (error) {
// This catch block is specifically for `sizeOf` failures,
// This catch block is specifically for `imageMetadata` errors
// which might occur if the accumulated data isn't yet sufficient.
}
}
Expand Down
Loading