1
1
import fs from 'fs/promises'
2
2
3
+ import type { Response , NextFunction } from 'express'
3
4
import sharp from 'sharp'
4
5
5
- import { assetCacheControl , defaultCacheControl } from '#src/frame/middleware/cache-control.js'
6
+ import type { ExtendedRequest } from '@/types'
7
+ import { assetCacheControl , defaultCacheControl } from '@/frame/middleware/cache-control.js'
6
8
import {
7
9
setFastlySurrogateKey ,
8
10
SURROGATE_ENUMS ,
9
- } from '#src /frame/middleware/set-fastly-surrogate-key.js'
11
+ } from '@ /frame/middleware/set-fastly-surrogate-key.js'
10
12
11
13
/**
12
14
* This is the indicator that is a virtual part of the URL.
@@ -37,7 +39,11 @@ const maxWidthPathPartRegex = /\/mw-(\d+)\//
37
39
*/
38
40
const VALID_MAX_WIDTHS = [ 1440 , 1000 ]
39
41
40
- export default async function dynamicAssets ( req , res , next ) {
42
+ export default async function dynamicAssets (
43
+ req : ExtendedRequest ,
44
+ res : Response ,
45
+ next : NextFunction ,
46
+ ) {
41
47
if ( ! req . url . startsWith ( '/assets/' ) ) return next ( )
42
48
43
49
if ( ! ( req . method === 'GET' || req . method === 'HEAD' ) ) {
@@ -88,6 +94,7 @@ export default async function dynamicAssets(req, res, next) {
88
94
89
95
if ( maxWidth ) {
90
96
const { width } = await image . metadata ( )
97
+ if ( width === undefined ) throw new Error ( 'image metadata does not have a width' )
91
98
if ( width > maxWidth ) {
92
99
image . resize ( { width : maxWidth } )
93
100
}
@@ -140,7 +147,7 @@ export default async function dynamicAssets(req, res, next) {
140
147
assetCacheControl ( res )
141
148
return res . type ( 'image/webp' ) . send ( buffer )
142
149
} catch ( error ) {
143
- if ( error . code !== 'ENOENT' ) {
150
+ if ( error instanceof Error && ( error as any ) . code !== 'ENOENT' ) {
144
151
throw error
145
152
}
146
153
}
@@ -166,7 +173,7 @@ export default async function dynamicAssets(req, res, next) {
166
173
res . status ( 404 ) . type ( 'text/plain' ) . send ( 'Asset not found' )
167
174
}
168
175
169
- function deconstructImageURL ( url ) {
176
+ function deconstructImageURL ( url : string ) {
170
177
let error
171
178
let maxWidth
172
179
const match = url . match ( maxWidthPathPartRegex )
0 commit comments