lab4: исправления второго ревью (40 Мп в tmpfs, таймауты S3, параллельный complete, auth до Content-Type, лимит на пользователя, бюджет памяти декода)

This commit is contained in:
2026-10-23 17:09:49 +03:00
parent a06471a6c6
commit 4b53e03fd6
27 changed files with 642 additions and 141 deletions
+17 -6
View File
@@ -1,22 +1,33 @@
// Peak RSS of one full decode check (same sharp options as ImageDecoder) on
// worst-case inputs near MAX_IMAGE_PIXELS. Run from app/: node ../scripts/lab4/decode_memory.mjs <files...>
// Peak RSS of one full decode check with the same sharp pipeline as
// app/src/attachments/image/image-decoder.ts, on worst-case inputs near
// MAX_IMAGE_PIXELS. Runs where the API runs (same libvips, VIPS_DISC_THRESHOLD):
// docker compose exec -T api node - < scripts/lab4/decode_memory.mjs # reads files from IMAGES (paths in the container)
// or on the host from app/: IMAGES="a.png b.jpg" node ../scripts/lab4/decode_memory.mjs
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
const sharp = createRequire(`${process.cwd()}/`)('sharp');
sharp.cache(false);
sharp.concurrency(1);
for (const file of process.argv.slice(2)) {
for (const file of (process.env.IMAGES ?? '').split(/\s+/u).filter(Boolean)) {
const data = readFileSync(file);
const meta = await sharp(data).metadata();
const base = process.memoryUsage().rss;
let peak = base;
const timer = setInterval(() => {
peak = Math.max(peak, process.memoryUsage().rss);
}, 10);
}, 5);
const started = Date.now();
await sharp(data, { failOn: 'warning', sequentialRead: true, limitInputPixels: 40_000_000 }).stats();
let verdict = 'ok';
try {
await sharp(data, { failOn: 'warning', sequentialRead: true, limitInputPixels: 40_000_000 })
.resize(64, 64, { fit: 'inside' })
.toBuffer();
} catch (error) {
verdict = `rejected: ${String(error.message).split('\n')[0]}`;
}
clearInterval(timer);
console.log(
`${meta.format} ${meta.width}x${meta.height}${meta.isProgressive ? ' progressive' : ''} bytes=${data.length} decode_ms=${Date.now() - started} peak_delta_rss_mib=${((peak - base) / 1048576).toFixed(1)}`,
`${meta.format} ${meta.width}x${meta.height} ${meta.channels}ch ${meta.depth}${meta.isProgressive ? ' progressive' : ''}${meta.isPalette ? ' palette' : ''} bytes=${data.length} decode_ms=${Date.now() - started} peak_delta_rss_mib=${((peak - base) / 1048576).toFixed(1)} ${verdict}`,
);
}