11import { describe , expect , it } from 'vitest'
2- import Sharp from 'sharp'
32
43import satori from '../src/index.js'
54import { resolveImageData } from '../src/handler/image.js'
6- import { toImage } from './utils.js'
5+ import { toImageWithSharp } from './utils.js'
76
87const WEBP_CASES = [
98 [
@@ -30,30 +29,14 @@ function decodeBase64(data: string) {
3029 return Uint8Array . from ( atob ( data ) , ( char ) => char . charCodeAt ( 0 ) ) . buffer
3130}
3231
33- async function toImageWithWebP ( svg : string , width : number ) {
34- const webpDataUris = svg . match ( / d a t a : i m a g e \/ w e b p ; b a s e 6 4 , [ ^ " ' ] + / g) || [ ]
35-
36- for ( const dataUri of webpDataUris ) {
37- // Resvg cannot decode embedded WebP, so transcode only the image payload.
38- const webp = Buffer . from ( dataUri . slice ( dataUri . indexOf ( ',' ) + 1 ) , 'base64' )
39- const png = await Sharp ( webp ) . png ( ) . toBuffer ( )
40- svg = svg . replace (
41- dataUri ,
42- `data:image/png;base64,${ png . toString ( 'base64' ) } `
43- )
44- }
45-
46- return toImage ( svg , width )
47- }
48-
4932describe ( 'WebP images' , ( ) => {
5033 it . each ( WEBP_CASES ) ( 'renders %s images' , async ( _ , data , width , height ) => {
5134 const svg = await satori ( < img src = { `data:image/webp;base64,${ data } ` } /> , {
5235 width,
5336 height,
5437 fonts : [ ] ,
5538 } )
56- expect ( await toImageWithWebP ( svg , width * 10 ) ) . toMatchImageSnapshot ( )
39+ expect ( await toImageWithSharp ( svg , width * 10 ) ) . toMatchImageSnapshot ( )
5740 } )
5841
5942 it . each ( WEBP_CASES ) (
0 commit comments