1
1
import { Buffer } from 'node:buffer'
2
- import { parse as urlParse } from 'node:url'
3
2
import { match } from 'path-to-regexp'
4
3
import type { Create , CreateText } from 'sharp'
5
4
import sharp from 'sharp'
@@ -12,7 +11,7 @@ import type {
12
11
ImagePlaceholderQuery ,
13
12
ImageType ,
14
13
} from './types'
15
- import { formatColor , formatText , getBackground } from './utils'
14
+ import { formatColor , formatText , getBackground , urlParse } from './utils'
16
15
17
16
export type CreateOptions = Create
18
17
@@ -23,14 +22,16 @@ export async function pathToImage(
23
22
findPathRule : FindPathRule ,
24
23
options : Required < ImagePlaceholderOptions > ,
25
24
) : Promise < ImageCacheItem | undefined > {
26
- if ( bufferCache . has ( url ) ) {
25
+ if ( bufferCache . has ( url ) )
27
26
return bufferCache . get ( url )
28
- }
29
- const { query : urlQuery , pathname } = urlParse ( url , true )
27
+
28
+ const { query : urlQuery , pathname } = urlParse ( url )
30
29
31
30
const rule = findPathRule ( pathname ! )
32
31
33
- if ( ! rule ) return
32
+ if ( ! rule )
33
+ return
34
+
34
35
const urlMatch = match ( rule , { decode : decodeURIComponent } ) ( pathname ! ) || {
35
36
params : {
36
37
width : options . width ,
@@ -83,7 +84,8 @@ export async function pathToImage(
83
84
}
84
85
bufferCache . set ( url , result )
85
86
return result
86
- } catch ( e ) {
87
+ }
88
+ catch ( e ) {
87
89
console . error ( e )
88
90
}
89
91
}
@@ -103,9 +105,9 @@ export async function createImage({
103
105
compressionLevel : number
104
106
textColor : string
105
107
} ) {
106
- if ( type === 'svg' ) {
108
+ if ( type === 'svg' )
107
109
return createSVG ( create , text . text , textColor )
108
- }
110
+
109
111
let image = sharp ( { create } )
110
112
111
113
text . text = formatText ( text . text , textColor )
@@ -121,7 +123,7 @@ export async function createImage({
121
123
image = image . webp ( { quality } )
122
124
break
123
125
case 'heif' :
124
- image = image . heif ( { quality } )
126
+ image = image . heif ( { quality, compression : 'av1' } )
125
127
break
126
128
case 'avif' :
127
129
image = image . avif ( { quality } )
0 commit comments