Files
mtusi2026lab6/app/src/attachments/image/image-decoder.ts
T
ilya.tyunin 6889e77ab4 lab6: базовый коммит из lab5 (0790599)
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.

Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
  mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
  infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
  от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
  (OTEL_*) из skeleton.
2026-11-09 16:44:21 +03:00

120 lines
3.8 KiB
TypeScript

import {
Injectable,
InternalServerErrorException,
Logger,
ServiceUnavailableException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import sharp from 'sharp';
import type { AppConfig } from '../../config/app-config.schema.js';
import { MetricsService } from '../../metrics/metrics.service.js';
import { ImageRejectedError } from './image-inspector.js';
// One libvips worker per decode and no operation cache: decodes are one-off,
// and memory must stay predictable under the per-replica limit.
sharp.cache(false);
sharp.concurrency(1);
// Failures of the replica itself, not of the image: they must not turn into a
// client 415. libvips spills large images to a temp file unless
// VIPS_DISC_THRESHOLD (compose.yaml) keeps them in memory; a full /tmp or an
// allocation failure ends up here.
export const SERVER_FAULT =
/no space left|out of memory|unable to allocate|cannot allocate|write failed|unable to open|tmp/iu;
/**
* Full decode check. ImageInspector only proves the container structure
* (signature, chunk CRCs, markers, dimensions); a PNG with a garbage zlib stream
* or a JPEG without scan data passes it. Decoding every scanline with
* failOn=warning rejects those, including truncated or corrupted compressed
* data. A downscale to a thumbnail is the sink: it decodes all input but keeps
* far less in memory than a full-size pipeline (measured in
* evidence/lab4/decode-memory.txt). Decodes are bounded by
* IMAGE_DECODE_MAX_CONCURRENT; a waiter whose request was aborted leaves the
* queue and releases its buffer.
*/
@Injectable()
export class ImageDecoder {
private readonly logger = new Logger(ImageDecoder.name);
private readonly maxPixels: number;
private readonly maxConcurrent: number;
private active = 0;
private readonly waiting = new Set<() => void>();
constructor(
config: ConfigService<AppConfig>,
private readonly metrics: MetricsService,
) {
this.maxPixels = config.getOrThrow('MAX_IMAGE_PIXELS');
this.maxConcurrent = config.getOrThrow('IMAGE_DECODE_MAX_CONCURRENT');
}
async verify(data: Buffer, signal?: AbortSignal): Promise<void> {
await this.acquire(signal);
const started = performance.now();
try {
await sharp(data, {
failOn: 'warning',
sequentialRead: true,
limitInputPixels: this.maxPixels,
})
.resize(64, 64, { fit: 'inside' })
.toBuffer();
} catch (error) {
const message =
error instanceof Error ? error.message : String(error);
if (SERVER_FAULT.test(message)) {
this.metrics.increment('attachments.decode.server_error');
this.logger.error(
`Image decode failed on the server: ${message}`,
);
throw new InternalServerErrorException();
}
this.metrics.increment('attachments.decode.rejected');
throw new ImageRejectedError(
'unsupported_media_type',
'Image data cannot be decoded',
);
} finally {
this.metrics.add(
'attachments.decode.ms',
performance.now() - started,
);
this.release();
}
}
private acquire(signal?: AbortSignal): Promise<void> {
const abandoned = new ServiceUnavailableException(
'Image check was abandoned',
);
if (signal?.aborted) return Promise.reject(abandoned);
if (this.active < this.maxConcurrent) {
this.active += 1;
return Promise.resolve();
}
const { promise, resolve, reject } = Promise.withResolvers<void>();
const cancel = () => {
this.waiting.delete(grant);
reject(abandoned);
};
const grant = () => {
signal?.removeEventListener('abort', cancel);
resolve();
};
this.waiting.add(grant);
signal?.addEventListener('abort', cancel, { once: true });
return promise;
}
private release(): void {
const next = this.waiting.values().next();
if (next.done) {
this.active -= 1;
return;
}
this.waiting.delete(next.value);
next.value();
}
}