Skip to content

Commit 53b5f97

Browse files
author
Peter Bengtsson
authored
Port dynamic-assets.js to TypeScript (#51436)
1 parent 0e2fe51 commit 53b5f97

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/assets/middleware/dynamic-assets.js renamed to src/assets/middleware/dynamic-assets.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import fs from 'fs/promises'
22

3+
import type { Response, NextFunction } from 'express'
34
import sharp from 'sharp'
45

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'
68
import {
79
setFastlySurrogateKey,
810
SURROGATE_ENUMS,
9-
} from '#src/frame/middleware/set-fastly-surrogate-key.js'
11+
} from '@/frame/middleware/set-fastly-surrogate-key.js'
1012

1113
/**
1214
* This is the indicator that is a virtual part of the URL.
@@ -37,7 +39,11 @@ const maxWidthPathPartRegex = /\/mw-(\d+)\//
3739
*/
3840
const VALID_MAX_WIDTHS = [1440, 1000]
3941

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+
) {
4147
if (!req.url.startsWith('/assets/')) return next()
4248

4349
if (!(req.method === 'GET' || req.method === 'HEAD')) {
@@ -88,6 +94,7 @@ export default async function dynamicAssets(req, res, next) {
8894

8995
if (maxWidth) {
9096
const { width } = await image.metadata()
97+
if (width === undefined) throw new Error('image metadata does not have a width')
9198
if (width > maxWidth) {
9299
image.resize({ width: maxWidth })
93100
}
@@ -140,7 +147,7 @@ export default async function dynamicAssets(req, res, next) {
140147
assetCacheControl(res)
141148
return res.type('image/webp').send(buffer)
142149
} catch (error) {
143-
if (error.code !== 'ENOENT') {
150+
if (error instanceof Error && (error as any).code !== 'ENOENT') {
144151
throw error
145152
}
146153
}
@@ -166,7 +173,7 @@ export default async function dynamicAssets(req, res, next) {
166173
res.status(404).type('text/plain').send('Asset not found')
167174
}
168175

169-
function deconstructImageURL(url) {
176+
function deconstructImageURL(url: string) {
170177
let error
171178
let maxWidth
172179
const match = url.match(maxWidthPathPartRegex)

src/assets/tests/dynamic-assets.js renamed to src/assets/tests/dynamic-assets.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { describe, expect, test, vi } from 'vitest'
33
import sharp from 'sharp'
44
import { fileTypeFromBuffer } from 'file-type'
55

6-
import { SURROGATE_ENUMS } from '#src/frame/middleware/set-fastly-surrogate-key.js'
7-
import { get, head } from '#src/tests/helpers/e2etest.js'
6+
import { SURROGATE_ENUMS } from '@/frame/middleware/set-fastly-surrogate-key.js'
7+
import { get, head } from '@/tests/helpers/e2etest.js'
88

99
describe('dynamic assets', () => {
1010
vi.setConfig({ testTimeout: 3 * 60 * 1000 })
@@ -15,7 +15,10 @@ describe('dynamic assets', () => {
1515
})
1616
expect(res.statusCode).toBe(200)
1717
expect(res.headers['content-type']).toBe('image/webp')
18-
const { mime } = await fileTypeFromBuffer(res.body)
18+
19+
const fileTypeResult = await fileTypeFromBuffer(res.body)
20+
if (!fileTypeResult) throw new Error('fileTypeFromBuffer failed')
21+
const { mime } = fileTypeResult
1922
expect(mime).toBe('image/webp')
2023
})
2124

src/frame/middleware/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import fastHead from './fast-head'
6060
import fastlyCacheTest from './fastly-cache-test'
6161
import trailingSlashes from './trailing-slashes'
6262
import mockVaPortal from './mock-va-portal.js'
63-
import dynamicAssets from '@/assets/middleware/dynamic-assets.js'
63+
import dynamicAssets from '@/assets/middleware/dynamic-assets'
6464
import contextualizeSearch from '@/search/middleware/contextualize.js'
6565
import shielding from '@/shielding/middleware'
6666
import tracking from '@/tracking/middleware'

0 commit comments

Comments
 (0)