1.18.0 performance items: reference 404 placeholder and heading font without preload, lazy form and Turnstile code (eager entry /client/eager), Turnstile space kept, background video paused for reduced motion, card hover on opacity, metadata stripped on upload, MongoDB TTL indexes for the consent log and statistics, unused-media notice, version limit for the plugin's entities

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 15:38:38 +01:00
co-authored by Claude Opus 5.5
parent 076b3ef707
commit d628839235
47 changed files with 2546 additions and 78 deletions
+89
View File
@@ -0,0 +1,89 @@
import sharp from 'sharp'
import { describe, expect, test } from 'vitest'
import { buildOptimizeUploadHook } from '../src/modules/media/optimizeUpload.js'
import { exifOrientation, stripImageMetadata } from '../src/modules/media/stripMetadata.js'
/** A small JPEG with EXIF (camera, GPS-like comment, orientation) and an ICC profile. */
const jpegWith = (orientation = 1) =>
sharp({ create: { background: { b: 40, g: 120, r: 200 }, channels: 3, height: 24, width: 40 } })
.withMetadata({ exif: { IFD0: { Copyright: 'Example', Make: 'PhoneCo', Model: 'X1' } }, orientation })
.withIccProfile('srgb')
.jpeg({ quality: 90 })
.toBuffer()
const pngWithText = async () => {
const png = await sharp({ create: { background: '#336699', channels: 4, height: 8, width: 8 } }).png().toBuffer()
// a tEXt chunk after IHDR: "Comment\0secret place"
const data = Buffer.from('Comment\0secret place', 'latin1')
const chunk = Buffer.alloc(12 + data.length)
chunk.writeUInt32BE(data.length, 0)
chunk.write('tEXt', 4, 'latin1')
data.copy(chunk, 8)
chunk.writeUInt32BE(0, 8 + data.length) // CRC is not checked by the stripper (the chunk is dropped)
const ihdrEnd = 8 + 12 + png.readUInt32BE(8)
return Buffer.concat([png.subarray(0, ihdrEnd), chunk, png.subarray(ihdrEnd)])
}
describe('stripImageMetadata', () => {
test('JPEG: EXIF goes, the image data and the colour profile stay byte for byte', async () => {
const input = await jpegWith()
expect(input.includes(Buffer.from('PhoneCo'))).toBe(true)
const { buffer, changed, orientation } = stripImageMetadata(input, 'image/jpeg')
expect(changed).toBe(true)
expect(orientation).toBe(1)
expect(buffer.includes(Buffer.from('PhoneCo'))).toBe(false)
const before = await sharp(input).metadata()
const after = await sharp(buffer).metadata()
expect(after.exif).toBeUndefined()
expect(after.icc).toBeDefined() // the colour profile is kept
expect([after.width, after.height]).toEqual([before.width, before.height])
// same pixels: no re-encode
expect(await sharp(buffer).raw().toBuffer()).toEqual(await sharp(input).raw().toBuffer())
const sos = (b: Buffer) => b.subarray(b.indexOf(Buffer.from([0xff, 0xda])))
expect(sos(buffer).equals(sos(input))).toBe(true)
})
test('JPEG: the EXIF orientation is reported, so the caller can turn the pixels first', async () => {
expect(stripImageMetadata(await jpegWith(6), 'image/jpeg').orientation).toBe(6)
expect(exifOrientation(Buffer.from('not exif'))).toBe(1)
})
test('PNG: text chunks go, the pixels stay', async () => {
const input = await pngWithText()
expect(input.includes(Buffer.from('secret place'))).toBe(true)
const { buffer, changed } = stripImageMetadata(input, 'image/png')
expect(changed).toBe(true)
expect(buffer.includes(Buffer.from('secret place'))).toBe(false)
expect(await sharp(buffer).raw().toBuffer()).toEqual(await sharp(input).raw().toBuffer())
})
test('nothing to strip, another format or a broken file: returned unchanged', async () => {
const clean = await sharp({ create: { background: '#000', channels: 3, height: 4, width: 4 } }).jpeg().toBuffer()
expect(stripImageMetadata(clean, 'image/jpeg').changed).toBe(false)
const webp = await sharp(clean).webp().toBuffer()
expect(stripImageMetadata(webp, 'image/webp')).toEqual({ buffer: webp, changed: false, orientation: 1 })
const broken = Buffer.from([0xff, 0xd8, 0x00, 0x01, 0x02])
expect(stripImageMetadata(broken, 'image/jpeg').buffer).toBe(broken)
})
})
describe('buildOptimizeUploadHook: metadata on uploads that pass the limits', () => {
const run = async (data: Buffer, options = {}) => {
const file = { data, mimetype: 'image/jpeg', name: 'photo.jpg', size: data.length }
await buildOptimizeUploadHook(options)({ args: { data: {} }, operation: 'create', req: { file, i18n: { language: 'en' }, payload: { config: { sharp } } } } as never)
return file
}
test('stripped by default; an EXIF rotation is applied to the pixels; `stripMetadata: false` keeps the file', async () => {
const plain = await run(await jpegWith())
expect(plain.data.includes(Buffer.from('PhoneCo'))).toBe(false)
expect(plain.name).toBe('photo.jpg') // same format and name: only the metadata went
const turned = await run(await jpegWith(6)) // 40×24 stored, "rotate 90°" in EXIF
const meta = await sharp(turned.data).metadata()
expect([meta.width, meta.height, meta.orientation]).toEqual([24, 40, undefined])
const original = await jpegWith()
expect((await run(original, { stripMetadata: false })).data).toBe(original)
})
})