-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
inferSize
to getImage so width and height are optional for remo…
…te images (#9976) * add remote image dimension probing, and param for getImage options, and new astro error * add inferSize parameter to getImage, which probes remote image size, and respective types to picture and image component * add fixture and tests * add changeset * attempt to fix pnpm-lock.yaml * fix pnpm-lock.yaml again * pnpm-lock spacing * fix pnpm-lock AGAIN * better description of error for docgen * improve failed to probe error message and correct required dimensions error * increase timeout for mdx tests * increasing mdx timeout to 2min to see if it passes, will reduce if it does * setting mdx timeout to 70 seconds * Update packages/astro/src/assets/services/service.ts Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> * refactor to move logic to getImage instead of validateOptions and to not add a third type to image and picture * fix broken link created by docs PR * remove the probe-image-size library due to its use of node APIs * undo all changes to service.ts that were left after moving inferSize logic to getImage * update error message * remove probe-image-size library all together, update error message, add vendored version of complete image-size library instead of refactored version * Update .changeset/tame-cameras-change.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update .changeset/tame-cameras-change.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * reword the error message based on Sarah's previous review * remove probe-image-size from pnpmlock * fix lockfile? * update error message name * move image-size into vendor folder * add eslint ignore to a line in image-size * test if change to mdx test timeout was needed * Update .changeset/tame-cameras-change.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * update changset syntax * patch parse heif to account for filetype block being out of order --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
- Loading branch information
1 parent
787e6f5
commit 91f75af
Showing
40 changed files
with
1,414 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
"astro": minor | ||
--- | ||
|
||
Adds a new optional `astro:assets` image attribute `inferSize` for use with remote images. | ||
|
||
Remote images can now have their dimensions inferred just like local images. Setting `inferSize` to `true` allows you to use `getImage()` and the `<Image />` and `<Picture />` components without setting the `width` and `height` properties. | ||
|
||
```astro | ||
--- | ||
import { Image, Picture, getImage } from 'astro:assets'; | ||
const myPic = await getImage({ src: "https://example.com/example.png", inferSize: true }) | ||
--- | ||
<Image src="https://example.com/example.png" inferSize alt="" /> | ||
<Picture src="https://example.com/example.png" inferSize alt="" /> | ||
``` | ||
|
||
Read more about [using `inferSize` with remote images](https://docs.astro.build/en/guides/images/#infersize) in our documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import { lookup } from './vendor/image-size/lookup.js' | ||
import type { ISize } from './vendor/image-size/types/interface.ts'; | ||
|
||
export async function probe(url: string): Promise<ISize> { | ||
// Start fetching the image | ||
const response = await fetch(url); | ||
if (!response.body || !response.ok) { | ||
throw new Error('Failed to fetch image'); | ||
} | ||
|
||
const reader = response.body.getReader(); | ||
|
||
let done: boolean | undefined, value: Uint8Array; | ||
let accumulatedChunks = new Uint8Array(); | ||
|
||
// Process the stream chunk by chunk | ||
while (!done) { | ||
const readResult = await reader.read(); | ||
done = readResult.done; | ||
|
||
if (done) break; | ||
|
||
if (readResult.value) { | ||
value = readResult.value; | ||
|
||
// Accumulate chunks | ||
let tmp = new Uint8Array(accumulatedChunks.length + value.length); | ||
tmp.set(accumulatedChunks, 0); | ||
tmp.set(value, accumulatedChunks.length); | ||
accumulatedChunks = tmp; | ||
|
||
try { | ||
// Attempt to determine the size with each new chunk | ||
const dimensions = lookup(accumulatedChunks); | ||
if (dimensions) { | ||
await reader.cancel(); // stop stream as we have size now | ||
return dimensions; | ||
} | ||
} catch (error) { | ||
// This catch block is specifically for `sizeOf` failures, | ||
// which might occur if the accumulated data isn't yet sufficient. | ||
} | ||
} | ||
} | ||
|
||
throw new Error('Failed to parse the size'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2013-Present Aditya Yadav, http://netroy.in | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This code comes from https://github.com/image-size/image-size/pull/370, and is slightly modified (all import statements have file extensions added to them). | ||
|
||
The `fromFile` functionality has also been removed, as it was not being used. |
25 changes: 25 additions & 0 deletions
25
packages/astro/src/assets/utils/vendor/image-size/detector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { imageType } from './types/index.js' | ||
import { typeHandlers, types } from './types/index.js' | ||
|
||
// This map helps avoid validating for every single image type | ||
const firstBytes = new Map<number, imageType>([ | ||
[0x38, 'psd'], | ||
[0x42, 'bmp'], | ||
[0x44, 'dds'], | ||
[0x47, 'gif'], | ||
[0x49, 'tiff'], | ||
[0x4d, 'tiff'], | ||
[0x52, 'webp'], | ||
[0x69, 'icns'], | ||
[0x89, 'png'], | ||
[0xff, 'jpg'], | ||
]) | ||
|
||
export function detector(input: Uint8Array): imageType | undefined { | ||
const byte = input[0] | ||
const type = firstBytes.get(byte) | ||
if (type && typeHandlers.get(type)!.validate(input)) { | ||
return type | ||
} | ||
return types.find((fileType) => typeHandlers.get(fileType)!.validate(input)) | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/astro/src/assets/utils/vendor/image-size/lookup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { imageType } from './types/index.js' | ||
import { typeHandlers } from './types/index.js' | ||
import { detector } from './detector.js' | ||
import type { ISizeCalculationResult } from './types/interface.ts' | ||
|
||
type Options = { | ||
disabledTypes: imageType[] | ||
} | ||
|
||
const globalOptions: Options = { | ||
disabledTypes: [], | ||
} | ||
|
||
/** | ||
* Return size information based on an Uint8Array | ||
* | ||
* @param {Uint8Array} input | ||
* @returns {ISizeCalculationResult} | ||
*/ | ||
export function lookup(input: Uint8Array): ISizeCalculationResult { | ||
// detect the file type... don't rely on the extension | ||
const type = detector(input) | ||
|
||
if (typeof type !== 'undefined') { | ||
if (globalOptions.disabledTypes.indexOf(type) > -1) { | ||
throw new TypeError('disabled file type: ' + type) | ||
} | ||
|
||
// find an appropriate handler for this file type | ||
const size = typeHandlers.get(type)!.calculate(input) | ||
if (size !== undefined) { | ||
size.type = size.type ?? type | ||
return size | ||
} | ||
} | ||
|
||
// throw up, if we don't understand the file | ||
throw new TypeError('unsupported file type: ' + type) | ||
} | ||
|
||
export const disableTypes = (types: imageType[]): void => { | ||
globalOptions.disabledTypes = types | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/astro/src/assets/utils/vendor/image-size/types/bmp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { IImage } from './interface.ts' | ||
import { toUTF8String, readInt32LE, readUInt32LE } from './utils.js' | ||
|
||
export const BMP: IImage = { | ||
validate: (input) => toUTF8String(input, 0, 2) === 'BM', | ||
|
||
calculate: (input) => ({ | ||
height: Math.abs(readInt32LE(input, 22)), | ||
width: readUInt32LE(input, 18), | ||
}), | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/astro/src/assets/utils/vendor/image-size/types/cur.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { IImage } from './interface.ts' | ||
import { ICO } from './ico.js' | ||
import { readUInt16LE } from './utils.js' | ||
|
||
const TYPE_CURSOR = 2 | ||
export const CUR: IImage = { | ||
validate(input) { | ||
const reserved = readUInt16LE(input, 0) | ||
const imageCount = readUInt16LE(input, 4) | ||
if (reserved !== 0 || imageCount === 0) return false | ||
|
||
const imageType = readUInt16LE(input, 2) | ||
return imageType === TYPE_CURSOR | ||
}, | ||
|
||
calculate: (input) => ICO.calculate(input), | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/astro/src/assets/utils/vendor/image-size/types/dds.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { IImage } from './interface.ts' | ||
import { readUInt32LE } from './utils.js' | ||
|
||
export const DDS: IImage = { | ||
validate: (input) => readUInt32LE(input, 0) === 0x20534444, | ||
|
||
calculate: (input) => ({ | ||
height: readUInt32LE(input, 12), | ||
width: readUInt32LE(input, 16), | ||
}), | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/astro/src/assets/utils/vendor/image-size/types/gif.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { IImage } from './interface.ts' | ||
import { toUTF8String, readUInt16LE } from './utils.js' | ||
|
||
const gifRegexp = /^GIF8[79]a/ | ||
export const GIF: IImage = { | ||
validate: (input) => gifRegexp.test(toUTF8String(input, 0, 6)), | ||
|
||
calculate: (input) => ({ | ||
height: readUInt16LE(input, 8), | ||
width: readUInt16LE(input, 6), | ||
}), | ||
} |
Oops, something went wrong.