diff --git a/.env.example b/.env.example index 119384b5..9447b322 100644 --- a/.env.example +++ b/.env.example @@ -1,25 +1,117 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. +# Полный список переменных, которые читают приложение и стенд; значения-образцы +# не являются секретами. Соответствие «переменная -> фактическая конфигурация» +# описано в REPORT.md. +# +# Приложение (проверяется zod-схемой app/src/config/app-config.schema.ts): APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url -API_REPLICAS=2 +DATABASE_URL=postgresql://messenger:change-me@postgres:5432/messenger +REDIS_URL=redis://redis:6379 +DATABASE_POOL_MAX=10 +DATABASE_CONNECTION_TIMEOUT_MS=5000 +DATABASE_STATEMENT_TIMEOUT_MS=5000 +DATABASE_IDLE_TX_TIMEOUT_MS=30000 +CACHE_TTL_SECONDS=30 +CACHE_MODE=protected +CACHE_LOCK_TTL_MS=1000 +CACHE_MISS_DELAY_MS=0 +# Бюджет проверки зависимостей в /health/ready: LB пробует ту же ручку +# (timeout check 500 ms), поэтому проба обязана уложиться заметно раньше. +HEALTH_TIMEOUT_MS=400 +# Токен доступа к /internal/metrics; сгенерируйте свой. +METRICS_TOKEN=change-me +# Дрейн перед выходом по SIGTERM: реплика перестаёт проходить пробы LB, +# ждёт DRAIN_DELAY_MS (длиннее обнаружения отказа ~1 s) и только потом закрывается. +DRAIN_DELAY_MS=3000 +# Переопределяет X-Instance-Id; по умолчанию это hostname контейнера +# (= короткий ID контейнера, по нему работает scripts/stop-one.sh). +# INSTANCE_ID=api-1 IDEMPOTENCY_TTL_SECONDS=86400 + +# Стенд (compose.yaml и scripts/): +API_REPLICAS=2 +API_CPUS_PER_REPLICA=1.5 +API_MEMORY_PER_REPLICA=1024m +PG_CPUS=1.5 +PG_MEMORY=512m +REDIS_CPUS=0.25 +REDIS_MEMORY=128m +LB_CPUS=0.25 +LB_MEMORY=128m +# Тег образа API; BUILD_ID отличает сборки при rolling update. +API_IMAGE=mtusi-messenger:lab5 +BUILD_ID=local +APP_PUBLIC_PORT=8080 +LB_STATS_PORT=8404 +# Loopback-порты PostgreSQL/Redis нужны только операторским проверкам с хоста +# (make test-e2e, psql). API-реплики портов не публикуют. +POSTGRES_PUBLISHED_PORT=5432 +REDIS_PUBLISHED_PORT=6379 + +# PostgreSQL (пароль подставьте свой; тот же, что в DATABASE_URL): +POSTGRES_DB=messenger +POSTGRES_USER=messenger +POSTGRES_PASSWORD=change-me + +# Объектное хранилище MinIO. Credentials — файлы ./secrets/* (scripts/init-env.sh), не здесь. S3_ENDPOINT=http://s3:9000 +S3_PUBLIC_ENDPOINT=http://localhost:9000 S3_REGION=us-east-1 S3_BUCKET=messenger-attachments -S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id -S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key +S3_PUBLIC_BIND=127.0.0.1 +S3_PUBLIC_PORT=9000 +S3_CPUS=1.0 +S3_MEMORY=512m +S3_CONNECT_TIMEOUT_MS=1000 +S3_REQUEST_TIMEOUT_MS=15000 MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 +# Каждая загрузка держит копию файла (≤ MAX_UPLOAD_BYTES) до полного декода; +# худший декод (40 Мп RGBA16 interlaced PNG) ≈ 340 MiB. Граница памяти реплики: +# базовый RSS (~250 MiB) + UPLOAD_MAX_IN_FLIGHT × 10 MiB + IMAGE_DECODE_MAX_CONCURRENT × 340 MiB +# = 250 + 160 + 340 ≈ 750 MiB при API_MEMORY_PER_REPLICA=1024m. +UPLOAD_MAX_IN_FLIGHT=16 +# Не больше стольких одновременных загрузок одного пользователя на реплику. +UPLOAD_MAX_IN_FLIGHT_PER_USER=4 +IMAGE_DECODE_MAX_CONCURRENT=1 +DIRECT_UPLOAD_MAX_PENDING_PER_USER=10 +# UPLOAD_TIMEOUT_MS, DIRECT_UPLOAD_URL_TTL_SECONDS, ATTACHMENT_SWEEP_GRACE_SECONDS, +# ATTACHMENT_SWEEP_INTERVAL_MS задаются в compose.yaml (environment) с умолчаниями +# и переопределяются из shell только для экспериментов. + +# lab5: очередь и асинхронная обработка изображений. URL брокера берётся из +# файла секрета (scripts/init-env.sh), байты и credentials в сообщения не +# попадают: только job_id/attachment_id/pipeline_version/attempt. BROKER_URL_FILE=/run/secrets/broker_url +BROKER_DELIVERY_LIMIT=10 +BROKER_PUBLISH_TIMEOUT_MS=5000 +# Сколько заданий worker обрабатывает параллельно; prefetch равен этому числу. WORKER_CONCURRENCY=2 JOB_MAX_ATTEMPTS=5 JOB_TIMEOUT_SECONDS=60 +JOB_LEASE_SECONDS=30 +JOB_RETRY_BASE_MS=1000 +JOB_RETRY_MAX_MS=30000 +PIPELINE_VERSION=1 +OUTBOX_POLL_INTERVAL_MS=500 +LEASE_REAPER_INTERVAL_MS=2000 +# 0 выключает фоновую уборку staging (ручной запуск остаётся). +STAGING_CLEANUP_INTERVAL_MS=60000 +# Сколько staging-объект неуспешной загрузки живёт до удаления. STAGING_RETENTION_SECONDS=86400 +WORKER_HEALTH_PORT=8081 +QUEUED_RECONCILE_GRACE_SECONDS=15 +OUTBOX_RETENTION_SECONDS=86400 +JOB_EVENTS_RETENTION_SECONDS=2592000 +# 1 включает чтение fault: из Redis; в обычном стенде — 0. +FAULT_INJECTION_ENABLED=0 +WORKER_REPLICAS=1 +WORKER_CPUS=1.5 +# Бюджет worker: база ≈250 MiB + WORKER_CONCURRENCY × (10 MiB исходник + +# ≈340 MiB худший декод 40 Мп) ≈ 950 MiB при concurrency 2; лимит 1280m. +WORKER_MEMORY=1280m +BROKER_CPUS=0.5 +BROKER_MEMORY=512m diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 7d1ff4a4..aa694fde 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,35 @@ ## Лабораторная 5 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Илья Тюнин, БВТ2303 +Целевая оценка (3/4/5): 5 +Трек 5, если заявлен: трек 2 «Доказанная идемпотентность pipeline» -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Что реализовано и что перенесено: upload переведён на 202 `queued`; байты +принимаются в S3 `staging/*`, задание и outbox коммитятся в PostgreSQL одной +транзакцией. Добавлены RabbitMQ 4.1 (quorum queue, DLX/DLQ), отдельный worker +с lease/heartbeat, retry с equal jitter, идемпотентными вариантами и DLQ, +операторский CLI (`status`, `failed`, `replay`, `reprocess`, `queues`, +`publish-raw`), версии pipeline v1/v2 и fault injection через Redis. Из lab4 +сохранены API, auth, чаты, сообщения, direct upload, кэш, drain, sweeper и +resilience hooks. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +Команды запуска из чистого clone: `make init-env`, `make stand-up`, +`make wait-ready`, `make test`, `make test-e2e`, `make test-dependencies`, +`make lab5-demo`; отказы трека 2 — стенд с +`FAULT_INJECTION_ENABLED=1 JOB_TIMEOUT_SECONDS=10 JOB_LEASE_SECONDS=8 JOB_RETRY_BASE_MS=500 JOB_RETRY_MAX_MS=4000` +и `make lab5-faults` / `make lab5-track2`. + +Результаты проверок и ссылка на REPORT.md: `REPORT.md`, +`evidence/lab5/summary.md`; smoke, e2e, dependency, persistence/failover, +worker-down demo и fault-сценарии. + +Ограничения / известные ошибки: single-node PostgreSQL/RabbitMQ/MinIO; +fault points только внутри процесса; S3-запись может повториться, но остаётся +побайтно детерминированной; track 1 не заявлен. + +- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] make check и make test выполнены; приложен реальный вывод. +- [x] Сценарии отказов/безопасности своего уровня воспроизведены. +- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. +- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..7f8f579d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +FROM node:25.7.0-slim AS build + +WORKDIR /usr/src/app + +RUN apt-get update \ + && apt-get install -y --no-install-recommends python3 make g++ \ + && rm -rf /var/lib/apt/lists/* + +COPY app/package.json app/package-lock.json ./ +RUN npm ci + +COPY app/ ./ +RUN npm run build && npm prune --omit=dev + +FROM node:25.7.0-slim AS runtime + +WORKDIR /usr/src/app + +# BUILD_ID доказывается через GET /internal/metrics и меняется при rolling +# update: он показывает, какая именно сборка отвечает на запрос. +ARG BUILD_ID=unknown +ENV BUILD_ID=$BUILD_ID + +ENV NODE_ENV=production +ENV APP_PORT=8080 +ENV DRAIN_DELAY_MS=5000 +COPY --from=build /usr/src/app/package.json /usr/src/app/package-lock.json ./ +COPY --from=build /usr/src/app/node_modules ./node_modules +COPY --from=build /usr/src/app/dist ./dist + +USER node + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \ + CMD node -e "const port=process.env.APP_PORT||'8080'; fetch('http://127.0.0.1:'+port+'/health/ready').then((response) => { if (!response.ok) process.exit(1); }).catch(() => process.exit(1))" + +CMD ["node", "dist/main.js"] diff --git a/Makefile b/Makefile index 5d86d171..beb83f74 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help check validate test load test-persistence test-failover test-dependencies stand-up stand-down stand-status wait-ready init-env test-e2e s3-security consistency bench lab5-demo lab5-faults lab5-track2 jobs help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes; init-env: .env + secrets; test-e2e: app e2e; s3-security; consistency; bench; lab5-demo; lab5-faults; lab5-track2; jobs ARGS=..." check: $(PYTHON) scripts/check_contract.py @@ -28,3 +28,54 @@ test-persistence: test-failover: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +COMPOSE_ENV_FILE ?= .env +COMPOSE = docker compose --env-file $(COMPOSE_ENV_FILE) + +test-dependencies: + cd app && E2E_COMPOSE_DIR="$(CURDIR)" E2E_COMPOSE_ENV_FILE="$(COMPOSE_ENV_FILE)" npm run test:dependencies + +stand-up: + $(COMPOSE) up -d --build --wait + +stand-down: + $(COMPOSE) down + +stand-status: + $(COMPOSE) ps + +wait-ready: + @for attempt in $$(seq 1 60); do \ + if curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1; then \ + echo "stand is ready ($(BASE_URL))"; exit 0; \ + fi; \ + sleep 1; \ + done; \ + echo "stand did not become ready within 60s" >&2; exit 1 + +init-env: + ./scripts/init-env.sh + +test-e2e: + scripts/app-e2e.sh + +s3-security: + scripts/lab4/check_s3_security.sh + +consistency: + scripts/lab4/consistency_check.sh + +bench: + scripts/lab4/run_matrix.sh + +lab5-demo: + $(PYTHON) scripts/lab5/worker_down_demo.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +lab5-faults: + $(PYTHON) scripts/lab5/fault_suite.py --base-url "$(BASE_URL)" --scenario level4 --output evidence/lab5/faults-level4.json $(TLS_ARGS) + +lab5-track2: + $(PYTHON) scripts/lab5/fault_suite.py --base-url "$(BASE_URL)" --scenario track2 --repeat 3 --output evidence/lab5/track2.json $(TLS_ARGS) + +jobs: + docker compose --env-file $(COMPOSE_ENV_FILE) exec -T api node dist/jobs/jobs.cli.js $(ARGS) diff --git a/REPORT.md b/REPORT.md index e98f75e9..d54465c8 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,384 @@ # Отчёт — лабораторная 5 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Илья Тюнин, БВТ2303 +- Целевая оценка: 5, трек 2 «Доказанная идемпотентность pipeline» +- Commit/PR: ветка `lab5`, коммиты `lab5: ...`; PR — после публикации +- Источники: [RabbitMQ reliability](https://www.rabbitmq.com/docs/reliability), [RabbitMQ confirms](https://www.rabbitmq.com/docs/confirms), `contracts/jobs.schema.json`, `contracts/attachment-states.md`, amqplib 2.2.1, sharp 0.35.5. ИИ использован для черновика плана, ревью кода и текста отчёта; решения и проверки выполнены и воспроизведены локально. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требования: Docker 29.x с Compose v2, Python 3.9+, Node 25.7.0 для локальных +юнит/e2e-прогонов. Образы приложения собираются из этого репозитория. + +```sh +make init-env # .env (если нет) + secrets/*; значения не печатаются +make check # offline-структура и OpenAPI +make stand-up # build + PostgreSQL, Redis, MinIO, RabbitMQ, API x2, worker, HAProxy +make wait-ready # /health/ready +make test # публичный smoke: 202 -> ready -> thumbnail 256x160 +make test-e2e # e2e приложения против стенда +make test-dependencies # отказы Redis/PostgreSQL/S3, пауза S3, saturation +make test-persistence ACTION_SCRIPT=scripts/restart.sh +scripts/start-replica.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +make lab5-demo # worker остановлен -> upload 202 -> рестарт broker/API -> ready +``` + +Проверка трека 2 требует включённого fault injection и укороченных таймаутов: + +```sh +FAULT_INJECTION_ENABLED=1 JOB_TIMEOUT_SECONDS=10 JOB_LEASE_SECONDS=8 \ +JOB_RETRY_BASE_MS=500 JOB_RETRY_MAX_MS=4000 make stand-up +make lab5-faults # level4 -> evidence/lab5/faults-level4.json +make lab5-track2 # track2 x3 -> evidence/lab5/track2.json +make stand-up # вернуть штатные значения +``` + +Остановка: `make stand-down`. Намеренное удаление учебных данных (необратимо): +`docker compose --env-file .env down -v` — удаляет PostgreSQL, Redis, MinIO и +RabbitMQ volumes. Секреты лежат в `secrets/` и в Git не попадают. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +```mermaid +flowchart LR + C[client] --> H[HAProxy] + H --> A1[API 1] + H --> A2[API 2] + A1 --> PG[(PostgreSQL)] + A2 --> PG + A1 --> S3S[(S3 staging/*)] + A1 --> O[(job_outbox)] + O --> R[Outbox relay] + R --> MQ[(RabbitMQ quorum queue)] + MQ --> W[worker] + W --> S3F[(S3 attachments/*)] + W --> PG + A1 --> RDS[(Redis: sessions, cache, faults)] +``` + +Источник истины — PostgreSQL. Брокер только доставляет сообщение; S3 хранит +байты. API и worker — разные процессы, worker не импортирует HTTP-модули. + +### Состояния + +| `attachments.status` | Значения | +| --- | --- | +| публичные | `queued`, `processing`, `ready`, `failed` | +| внутренние | `pending` (идёт приём), `deleting`, `rejected` (lab4) | + +| `attachment_jobs.status` | Значения | +| --- | --- | +| | `queued`, `processing`, `succeeded`, `failed`, `superseded` | + +Правила переходов выполняются в транзакции, меняющей job: + +- claim: `queued -> processing` только при `attachments.status='queued'`; +- success: `ready`, если `coalesce(ready_pipeline_version,0) <= pipeline_version`, + иначе job становится `superseded`, а строка вложения не меняется; +- retry: `processing -> queued` только при `attachments.status='processing'`; +- final failure: `failed` + `error_code`, только если вложение не `ready`; +- replay: `failed -> queued`; +- `ready` никогда не откатывается; старая job не перезаписывает новый результат. + +### Что уже сохранено в момент 202 + +До ответа 202 выполняются и фиксируются одним COMMIT: + +1. все байты приняты в `staging//original`; размер и SHA-256 сверены, + `ImageInspector` и полный decode проверили контейнер и пиксели; +2. `attachments` — строка `queued` с `size_bytes`, `sha256`, `width`, `height`; +3. `attachment_jobs` — логическая работа `(attachment_id, pipeline_version)`, + `attempt=1`, `max_attempts`; +4. `job_outbox` — намерение опубликовать сообщение. + +Только после COMMIT вызывается fault point `api.after_commit_before_publish` и +relay. Брокер не входит в `/health/ready`: outbox позволяет принимать загрузки +при недоступном RabbitMQ, а relay опубликует задание после восстановления. + +### Окна отказа + +| Окно из contract | Механизм | Сценарий | Доказательство | +| --- | --- | --- | --- | +| после staging до записи задания | строка `pending` + `expires_at`; sweeper удаляет staging и строку | lab4 sweep + `make test-dependencies` | `make-test-dependencies.txt` | +| после COMMIT до publish | transactional outbox, relay повторяет непомеченные строки | `crash_after_commit_before_publish` | `faults-level4.json` | +| после publish до confirm | ConfirmChannel; без confirm транзакция relay откатывается | `relay_crash_after_publish` | `track2.json` | +| после записи S3 до COMMIT результата | lease истекает, reaper возвращает job в `queued`; повторный рендер детерминирован | `crash_after_side_effect` | `track2.json` | +| после COMMIT результата до ack | redelivery видит `succeeded` и отвечает `duplicate_skipped` | `crash_after_commit_before_ack` | `track2.json` | + +### Publisher confirm и consumer ack + +- **Publisher confirm** — брокер принял persistent-сообщение. Outbox помечает + строку опубликованной только после confirm. +- **Consumer ack** — worker устойчиво зафиксировал эффект в PostgreSQL и S3. + Ack после commit; падение между commit и ack даёт redelivery и no-op. + +Это at-least-once доставка. Ключ эффекта — `(attachment_id, pipeline_version, +variant)` (PK `attachment_variants`); логическая работа уникальна по +`(attachment_id, pipeline_version)`. Повторная доставка не создаёт второй набор +вариантов. `attempt` увеличивается только управляемым retry/replay/истечением +lease, но не redelivery брокера. + +### Зависший worker и долгая работа + +Claim ставит `lease_owner`, `lease_token`, `lease_until`. Heartbeat продлевает +lease каждые `JOB_LEASE_SECONDS/3`; 0 обновлённых строк означает потерю lease и +аборт job. `JOB_TIMEOUT_SECONDS` — жёсткий дедлайн: `AbortController` плюс +`sharp(...).timeout({seconds})` дают transient `processing_timeout`. Reaper +возвращает job с истёкшим lease в очередь, поэтому убитый worker не оставляет +вечный `processing`. + +### DLQ, failed registry, replay + +- **DLQ** (`attachments.process.dlq`): невалидный JSON/схема и сообщение, + не совпадающее с job (`mismatch`). Это не очередь повторных попыток. +- **Failed registry**: `cli failed` — jobs в `failed` с `last_error_code`. +- **Replay** (`cli replay`) разрешён только для failed job, при отсутствии + активной job, и отказывает `already_ready`, `no_failed_job`, `job_active`, + `source_expired`. Новая попытка защищена тем же ключом эффекта; успешный + результат не исполняется повторно. + +### Staging retention + +Worker удаляет `staging//original` после успешного commit. Остатки убирает +`StagingCleaner`: для `ready` — через минуту, для `failed` — через +`STAGING_RETENTION_SECONDS` (24 ч). Pending/rejected загрузки удаляет lab4 +sweeper по `expires_at + grace`. Текущую работу cleaner не трогает. + +### Качество вариантов и метаданные + +| Вариант | v1 | v2 | +| --- | --- | --- | +| thumbnail | JPEG q80, fit 256×256, без апскейла, alpha на белом | JPEG q90 progressive mozjpeg | +| crop | JPEG q80, 128×128 cover centre (апскейл разрешён) | JPEG q90 progressive mozjpeg | +| optimized | WebP q75 effort 4, fit 1280×1280 | WebP q82 effort 5 smartSubsample | + +EXIF orientation применяется `.rotate()`; размеры берутся из +`metadata().autoOrient`. sharp по умолчанию удаляет EXIF/ICC (нет +`withMetadata`). `fitInside` использует целочисленную арифметику: 320×200 → +256×160, 1000×335 → 256×85, 10000×1 → 256×1. + +### Память worker + +База ≈250 MiB + `WORKER_CONCURRENCY × (10 MiB исходник + ≈340 MiB худший декод +40 Мп)` ≈ 950 MiB при concurrency 2. Лимит `WORKER_MEMORY=1280m`. +`big_image` загружает два `worst_decode_png` (8000×5000) параллельно: измеренный +пик — **992.2 MiB / 1.25 GiB** (в предыдущих прогонах 934–966 MiB), `OOMKilled=false`, оба вложения дошли до ready. +Запас ~300 MiB; при росте concurrency лимит нужно поднимать вместе с бюджетом. + +### Конфигурация + +| Переменная | Фактическое значение / роль | +| --- | --- | +| `BROKER_URL_FILE` | `/run/secrets/broker_url`, amqp URL с паролем из secret | +| `BROKER_DELIVERY_LIMIT` | 10, `x-delivery-limit` quorum-очереди | +| `WORKER_CONCURRENCY` | 2, prefetch и число параллельных job | +| `JOB_MAX_ATTEMPTS` | 5, ограниченные retry | +| `JOB_TIMEOUT_SECONDS` | 60, жёсткий дедлайн рендера | +| `JOB_LEASE_SECONDS` | 30, lease + heartbeat/3 | +| `JOB_RETRY_BASE_MS` / `JOB_RETRY_MAX_MS` | 1000 / 30000, equal jitter | +| `PIPELINE_VERSION` | 1, версия для новых upload | +| `OUTBOX_POLL_INTERVAL_MS` | 500, период relay | +| `LEASE_REAPER_INTERVAL_MS` | 2000 | +| `STAGING_CLEANUP_INTERVAL_MS` | 60000, 0 отключает | +| `STAGING_RETENTION_SECONDS` | 86400 | +| `WORKER_HEALTH_PORT` | 8081 | +| `FAULT_INJECTION_ENABLED` | 0 в штатном стенде, 1 для suite | +| `WORKER_REPLICAS` / `WORKER_CPUS` / `WORKER_MEMORY` | 1 / 1.5 / 1280m | +| `BROKER_CPUS` / `BROKER_MEMORY` | 0.5 / 512m | + +Остальные переменные lab4 (`DATABASE_URL`, `REDIS_URL`, S3, лимиты загрузки, +таймауты) сохранены; `S3_ACCESS_KEY_ID_FILE`/`S3_SECRET_ACCESS_KEY_FILE` +подменяются в worker на отдельную identity. + +### Роли инфраструктуры + +| Роль | Service | Образ | Готовность | Volume | Credentials | +| --- | --- | --- | --- | --- | --- | +| API | `api` | сборка `mtusi-messenger:lab5` | `/health/ready` (PG/Redis/S3) | — | `s3_api_*`, `broker_url` | +| Worker | `worker` | та же сборка, `dist/worker.main.js` | `127.0.0.1:8081/health` | — | `s3_worker_*`, `broker_url` | +| Broker | `broker` | `rabbitmq:4.1.8-alpine@sha256:153322e3974e6365c50e523e5eadd5a7fae231f0e8c58f67c03bb3e02056e34e` | `rabbitmq-diagnostics check_port_connectivity` | `broker-data` | `rabbitmq.conf` | +| PostgreSQL | `postgres` | `postgres:16.4-alpine` | `pg_isready` | `postgres-data` | `.env` / `DATABASE_URL` | +| Redis | `redis` | `redis:7.4.2-alpine` | `redis-cli ping` | `redis-data` | только compose-сеть | +| S3 | `s3` | `minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e` | `mc ready local` | `s3-data` | root secrets | +| S3 bootstrap | `s3-init` | `minio/mc:...@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727` | exit 0 | — | root + API/worker | +| LB | `haproxy` | `haproxy:3.2-alpine` | depends on api healthy | — | — | + +### Изменения относительно lab4 + +- upload 201 → 202 `queued`; metadata отдаёт `queued/processing/ready/failed`, + `error_code` и `variants`; +- байты пишутся в `staging/*`, worker переносит оригинал в `attachments/*`; +- добавлены `attachment_jobs`, `attachment_variants`, `job_outbox`, `job_events`; +- добавлены broker, worker, отдельные S3-identity и политики; +- `openContent` отдаёт `original/thumbnail/crop/optimized` и 409 до ready; +- сообщения можно привязывать к `queued/processing` вложениям, failed даёт 409; +- сохранены lab4-механизмы: idempotency, cache, drain, sweeper, direct upload, + failover/persistence hooks. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Сборка и lint | `npm run build && npm run lint` | без ошибок; `dist/worker.main.js` и `dist/jobs/jobs.cli.js` | `build-lint.txt` | +| Публичный smoke | `make test` | `PASS: lab5 public API scenario`; 202 → ready → thumbnail 256×160 JPEG, sha совпал | `evidence/lab5/make-test.txt` | +| «3»: API 202 только после устойчивого приёма | `make test`, `make lab5-demo` | staging + attachment + job + outbox в одном COMMIT; worker остановлен, upload принят | `make-test.txt`, `worker-down-demo.txt` | +| «3»: отдельные broker и worker | `make lab5-demo`, `docker compose ps` | broker/worker healthy; worker не в API-процессе | `compose-ps.txt`, `worker-down-demo.txt` | +| «3»: состояния и thumbnail | `make test`, `make test-e2e` | metadata `queued/processing/ready`; thumbnail 256×160; original неизменен | `make-test.txt`, `make-test-e2e.txt` | +| «3»: ограниченный retry и failed | `retry_and_replay` | 4 `retry_scheduled`, затем `failed`/`retry_exhausted`; replay → ready | `faults-level4.json` | +| «3»: принятое задание переживает рестарт broker | `make lab5-demo` | quorum-очередь сохранила сообщение после restart broker+API | `worker-down-demo.txt`, `queues.txt` | +| «4»: crop/optimized, качество, EXIF | `make test`, unit `pipelines.spec.ts` | 128×128 JPEG, WebP 320×200, EXIF 6 → 170×256, прозрачность на белом | `unit-tests.txt`, `make-test.txt` | +| «4»: outbox, crash COMMIT→publish | `crash_after_commit_before_publish` | attachment готов без ручного создания job; один `created` | `faults-level4.json` | +| «4»: backoff/jitter, permanent/transient, DLQ/failed/replay | `retry_and_replay`, `poison` | transient exhausted, DLQ +2, replay с audit | `faults-level4.json` | +| «4»: crash после S3 до ack | `crash_after_side_effect` | attempt 2, lease_expired, варианты байт-идентичны baseline | `faults-level4.json`, `track2.json` | +| «4»: timeout/lease, concurrency, очистка staging | `hung_worker`, `make lab5-demo`, CLI status | `processing_timeout` retry; `staging_present=false` после ready | `faults-level4.json`, `queues.txt` | +| «4»: память worker на 40 Мп | `big_image` | 2 × 40 Мп параллельно, пик 934–992 MiB, OOMKilled=false | `big-image-memory.txt`, `faults-level4.json` | +| «5» трек 2: версионирование, дубль, reorder, crash, stale | `make lab5-track2` | все инварианты I1–I6 истинны на 3 повторах | `track2.json` | +| «5» трек 2: отсутствие потери и перезаписи | `stale_version` | ready_pipeline_version=2, v1 `superseded`, v2 etag не изменился | `track2.json` | +| «5» трек 2: не exactly-once доставка | отчёт, `duplicate` | 5 duplicate publish дали 5 `duplicate_skipped`, S3 etag не изменился | `track2.json` | +| Регрессия ревью: потерянное сообщение | purge очереди после publish | reconciler вернул job, ready за 5.1 s, событие `reconciled` | `reconciler.txt` | +| Регрессия ревью: рестарт broker | restart broker при живом worker | consumer вернулся, следующий upload ready | `broker-resubscribe.txt` | +| Разделение RabbitMQ identities | `broker-init` + `rabbitmqctl list_permissions` | API не читает `attachments.process`; worker — отдельный пользователь | `broker-identities.txt` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +### Гипотеза трека 2 -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +At-least-once delivery + DB-fenced claim + versioned effect keys дают +**exactly-once effects** для `(attachment_id, pipeline_version, variant)` при +дубле, reorder, crash до/после side effect и устаревшей job после новой версии. +Доставка при этом остаётся at-least-once: дубли и повторные публикации +наблюдаются и поглощаются, а не исчезают. + +### Методика + +- Стенд: 8 vCPU (i7-11370H), 15 GiB RAM; worker 1 реплика, concurrency 2, + лимит 1280 MiB; broker 0.5 CPU / 512 MiB; JOB_TIMEOUT_SECONDS=10, + JOB_LEASE_SECONDS=8, retry 500–4000 ms; `track2` повторён 3 раза. +- Перед сценариями загружается reference и запоминаются SHA-256 вариантов v1. +- Каждый сценарий начинается и заканчивается очисткой `fault:*`; timeline + опрашивается каждые 200 мс. +- Инварианты: + - **I1** — ready (или ожидаемый failed) не позже 60 с; + - **I2** — ровно 3 варианта ready-версии и ровно одно `succeeded` на job; + - **I3** — ready не откатывается, `ready_pipeline_version` не убывает; + - **I4** — подтверждённая работа не потеряна (есть успешный эффект); + - **I5** — нет дублей `(pipeline_version, variant)`; + - **I6** — SHA-256 вариантов совпадает с baseline для той же версии. + +### Результаты + +Таблица заполняется из `evidence/lab5/track2.json` и +`evidence/lab5/faults-level4.json`; фактические значения и пик памяти worker +приведены в `evidence/lab5/summary.md`. + +| Сценарий | Группа | Наблюдение (3 повтора для track2) | +| --- | --- | --- | +| `duplicate` | track2 | 5 повторных publish → 5 `duplicate_skipped` (`already_succeeded`); S3 etag/last_modified не изменились; attempt 1 | +| `reorder` | track2 | attempt 99 и дубль attempt 1 поглощены; ровно 2 skip, 1 `succeeded`; attempt 1 | +| `crash_before_side_effect` | track2 | worker убит до S3; попытка 2, `lease_expired`, процесс worker перезапущен | +| `crash_after_side_effect` | track2 | попытка 2, `lease_expired`, SHA-256 вариантов совпал с baseline (I6) | +| `crash_after_commit_before_ack` | track2 | попытка 1, один `succeeded`, redelivery → `duplicate_skipped` (`already_succeeded`) | +| `relay_crash_after_publish` | track2 | API убит между publish и mark; relay опубликовал снова, дубль поглощён; все 3 повтора | +| `stale_version` | track2 | `ready_pipeline_version=2`, v1 `superseded`, остались только варианты v2, v2 etag не изменились, served thumbnail SHA ≠ v1 | +| `crash_after_commit_before_publish` | level4 | API убит после COMMIT; outbox relay опубликовал после рестарта; ровно один `created`, ready | +| `retry_and_replay` | level4 | 4 `retry_scheduled`, затем `failed`/`retry_exhausted`; replay → attempt 6 → ready; `replayed` с operator | +| `hung_worker` | level4 | abort по `JOB_TIMEOUT_SECONDS`, `retry_scheduled` с `processing_timeout`, затем ready | +| `poison` | level4 | два невалидных сообщения ушли в DLQ, jobs не созданы | +| `big_image` | level4 | оба 40 Мп вложения ready, `OOMKilled=false`; пик 934–992 MiB при лимите 1280 MiB | + +Итог: `level4` — **7/7**, `track2` — **21/21** (7 сценариев × 3), все +инварианты I1–I6 истинны; см. `evidence/lab5/faults-level4.json`, +`evidence/lab5/track2.json`. + +### Ограничения эксперимента + +Это не exactly-once **доставка**: redelivery и повторная публикация наблюдаются. +S3-запись может повториться (объект перезаписывается тем же детерминированным +байтом), PostgreSQL — одна нода, а fault points находятся внутри процесса. +Сценарии `relay_crash_after_publish` и `crash_after_commit_before_publish` +зависят от гонки relay и поэтому фиксируют наблюдение, если дубль не успел +возникнуть. Внешние эффекты за пределами `(attachment, version, variant)` не +защищены. + +## Исправления по независимому ревью + +После первого зелёного прогона ревью выявило латентные отказы; внесены правки: + +- **Потеря queued job после DLQ/исчезнувшего сообщения:** `LeaseReaper` теперь + периодически вызывает `reconcileQueuedJobs`; job без unpublished outbox + получает новую outbox-строку. `claim` при ошибке БД повторяется с задержкой, + nack отложен, поэтому delivery limit не сжигается в tight loop. +- **Потеря consumer после рестарта broker:** `BrokerClient` отслеживает + `subscribing`, повторяет `basic.consume` при закрытии канала, `basic.cancel` + и ошибке подписки; частично установленное соединение закрывается. +- **Publish без подтверждения/маршрута:** добавлены timeout подтверждения, + `mandatory: true` и обработка `basic.return`; outbox не помечает строку + published при возврате. +- **Clock skew «not_due»:** due-check выполняется в SQL (`next_attempt_at <= now()`). +- **Порядок блокировок:** claim/reap/replay/cleaner блокируют job, затем + attachment; это исключает 40P01 между claim и commit. +- **Staging cleaner vs replay:** удаление объекта выполняется под row-lock + вместе с `clearStaging`; replay ждёт и видит `queued`. +- **Relay в worker:** retry и lease-expiry публикуются даже при недоступном API; + при ошибке relay использует backoff. +- **Варианты и lease:** ключ варианта содержит attempt + (`.../-a.`); потерявший lease worker не перезапишет + объект, на который смотрит БД. +- **Долгий рендер:** общий дедлайн распределяется по операциям sharp, abort + проверяется между ними. +- **Терминальное событие reaper:** при `retry_exhausted` пишется `failed`. +- **Direct upload cap:** проверка лимита и insert сериализованы advisory-lock. +- **Retention:** опубликованный outbox и старые события чистятся по TTL, + добавлен индекс `job_outbox(job_id)`. +- **Legacy lab4 rows:** миграция 008 ставит им v1 job в outbox, чтобы появились + варианты, а не пустой `variants: []`. +- **Least privilege:** API теряет `DeleteObject` на варианты (только + `attachments/*/original`), S3 policies ограничены префиксами; RabbitMQ + разделён на `messenger` (публикация, без чтения очереди) и + `messenger-worker` (consumption), `broker-init` приводит существующий volume + к этой схеме. + +Осталось осознанным упрощением: финальный оригинал permanently failed job +сохраняется для операторского replay, staging удаляется по retention. + +## Повторное ревью и вторые правки + +Второй независимый проход не нашёл P0/P1, но выявил последствия первых правок; +они устранены: + +- `withTransaction` снимает `error`-события checked-out pg-клиента и + `release(error)`, поэтому `idle_in_transaction_session_timeout` не роняет + процесс; relay дополнительно ограничен бюджетом `< idle_timeout/2`. +- Reconciler срабатывает только при пустой очереди и отсчитывает grace от + `job_outbox.published_at`, поэтому нормальный backlog не размножается. +- Миграция 009 добавляет partial index по `queued` jobs, индексы по + `job_events(created_at)` и `job_outbox(published_at)`; pruning стал пакетным. +- Reconciliation пишется отдельным событием `reconciled`, а не + `retry_scheduled`, чтобы не искажать счётчик retry. +- После успеха/исчерпания попыток worker удаляет варианты старых attempt + (`-aN`), для чего worker получил `DeleteObject` только на `attachments/*/v*/*`. +- `replay` передаёт ожидаемый `staging_key` и под блокировкой отказывает + `source_changed`, если cleaner успел изменить источник. +- Publisher снимает метрику ровно один раз (`settled`), confirm-channel + `close`/`error` обрабатываются, а сбой consumer-подписки больше не рвёт + рабочее publish-соединение. +- RabbitMQ разделён на `messenger` (API: публикация и topology, без чтения + очереди) и `messenger-worker` (consumption); одноразовый `broker-init` + создаёт второго пользователя и сужает права первого. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Реализованы уровни 3–4 и трек 2; трек 1 (сравнение concurrency/prefetch) не + заявлен. +- Финальный original permanently failed job не удаляется автоматически: он + нужен для безопасного replay, пока staging ещё не истёк; ручная очистка + описана в отчёте. +- Брокер и S3 — single-node; production-варианты требуют кворумов и ретраев + между зонами. +- `job_outbox` публикуется одним relay-процессом на реплику; при росте нагрузки + нужен отдельный relay или партиционирование. +- Ошибки классифицируются по сообщению sharp/`SERVER_FAULT`; для новых + форматов список стоит расширять по мере появления. +- Следующий шаг: метрики очереди lag и p95 ready, отдельный DLQ-replay UI, + сравнение concurrency/prefetch (трек 1). diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 00000000..b266c548 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,59 @@ +# compiled output +/dist +/node_modules +/build + +# TypeScript build cache +*.tsbuildinfo + +# Logs +logs +*.log +npm-debug.log* +pnpm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# OS +.DS_Store + +# Tests +/coverage +/.nyc_output + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# temp directory +.temp +.tmp + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json diff --git a/app/.prettierrc b/app/.prettierrc new file mode 100644 index 00000000..b6528392 --- /dev/null +++ b/app/.prettierrc @@ -0,0 +1,6 @@ +{ + "singleQuote": true, + "trailingComma": "all", + "tabWidth": 4, + "useTabs": true +} diff --git a/app/README.md b/app/README.md new file mode 100644 index 00000000..b285bd3e --- /dev/null +++ b/app/README.md @@ -0,0 +1,314 @@ +# app — API мессенджера + +NestJS 12 + Fastify, ESM TypeScript. PostgreSQL — источник истины бизнес-данных, +Redis — серверные Bearer-сессии и производный кэш истории сообщений. + +Общая документация (в корне репозитория): + +| Документ | Что содержит | +|---|---| +| [`../REPORT.md`](../REPORT.md) | архитектура, схема данных, транзакции, политика кэша, команды, результаты проверок и трека 1 | +| [`../evidence/README.md`](../evidence/README.md) | индекс доказательств: что подтверждено и как это воспроизвести | +| [`../contracts/openapi.json`](../contracts/openapi.json) | нормативный контракт API | +| [`../tests/README.md`](../tests/README.md) | публичные проверки репозитория (`make check`, `make test`, persistence) | + +## Что внутри + +```text +src/ + main.ts bootstrap: Fastify, ValidationPipe, shutdown hooks + app.module.ts сборка модулей приложения + config/ Zod-схема env + глобальный ConfigModule + database/ pg.Pool, транзакции, миграции (001..004) + redis/ клиент Redis (AOF-совместимый, без offline queue) + auth/ Basic-вход, Redis-сессии, logout + users/ регистрация (Argon2id), профиль + chats/ создание чата в транзакции, список и чтение + messages/ отправка сообщения, keyset-история + cache/ версионированный кэш истории + lock на страницу + idempotency/ журнал Idempotency-Key (claim/read/store/forget/GC) + metrics/ /internal/metrics и /internal/metrics/reset + health/ liveness/readiness с проверкой зависимостей (readiness — проба LB) + common/ + http/ cursor (формат+календарь), page (конверт {items,next_cursor}), request-id + errors/ exception filter и тип тела ошибки ApiErrorBody + validation/ декоратор CodePointLength (длины в Unicode code points) + auth/ guard и декоратор текущего пользователя +test/ + app.e2e-spec.ts black-box набор против запущенного API (PG + Redis) +scripts/ + graph-modules.ts генератор графа зависимостей (nestjs-spelunker) +``` + +Принципы: raw `pg.Pool` без ORM, параметризованные запросы, никаких in-memory +хранилищ в боевых путях; кэш — производное состояние и всегда допускает +восстановление из PostgreSQL; отказ зависимости даёт ограниченную ошибку +(`503`), а не аварийное завершение процесса; длины строк проверяются в Unicode +code points (свой декоратор `@CodePointLength`), как в JSON Schema, а не в +UTF-16-единицах; состояние, от которого зависит корректность (сессии, журнал +идемпотентности), живёт только в общих PostgreSQL/Redis — локальных сессий и +файлов на диске реплики нет, поэтому `N` взаимозаменяемых реплик за одним LB +дают один и тот же результат. + +## Быстрый старт + +Рекомендуемый путь — весь стенд из корня репозитория (PostgreSQL, Redis, миграции, API): + +```sh +cd .. +cp .env.example .env +password=$(openssl rand -hex 32) +sed -i "s/^POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=$password/" .env +sed -i "s|^DATABASE_URL=.*|DATABASE_URL=postgresql://messenger:$password@postgres:5432/messenger|" .env +sed -i "s/^METRICS_TOKEN=.*/METRICS_TOKEN=$(openssl rand -hex 16)/" .env +make stand-up # сборка + up -d --wait: migrate -> api x API_REPLICAS -> haproxy +make wait-ready +curl -fsS http://localhost:8080/health/ready +for i in 1 2 3 4; do curl -sD- -o /dev/null localhost:8080/health/live | grep -i x-instance-id; done +``` + +Публичной точкой входа служит HAProxy (`compose.yaml`): реплики API портов не +публикуют. `API_REPLICAS` задаёт их число, `API_TOTAL_CPUS` / `API_TOTAL_MEMORY_MB` +делятся между ними — это бюджет серии трека 1 (`../REPORT.md`). + +Локальный запуск без Docker (нужны доступные PostgreSQL и Redis): + +```sh +cd app +npm ci +npm run build + +export DATABASE_URL=postgresql://messenger:@127.0.0.1:5432/messenger +export REDIS_URL=redis://127.0.0.1:6379 +export APP_PORT=8080 +node dist/database/migration.runner.js # применяет миграции из dist/database/migrations + +npm run start:dev # или npm run start:prod после build +``` + +## Переменные окружения + +Читаются только через конфигурационный слой (`src/config`), типы и диапазоны +проверяет Zod-схема на старте — при некорректном значении приложение падает +с понятной ошибкой. + +| Переменная | По умолчанию | Назначение | +|---|---|---| +| `APP_HOST` / `APP_PORT` | `0.0.0.0` / `8080` | адрес прослушивания | +| `APP_ENV` | `lab` | метка окружения | +| `DATABASE_URL` | — (обязательна) | строка подключения PostgreSQL | +| `REDIS_URL` | — (обязательна) | строка подключения Redis | +| `DATABASE_POOL_MAX` | `10` | размер пула соединений | +| `DATABASE_CONNECTION_TIMEOUT_MS` | `5000` | таймаут установки соединения | +| `DATABASE_STATEMENT_TIMEOUT_MS` | `5000` | таймаут SQL-запроса | +| `SESSION_TTL_SECONDS` | `3600` | TTL сессии (минимум 2) | +| `HEALTH_TIMEOUT_MS` | `400` | бюджет проверки PostgreSQL/Redis внутри `/health/ready`; меньше `timeout check` HAProxy (500 ms) и `DATABASE_STATEMENT_TIMEOUT_MS` | +| `CACHE_TTL_SECONDS` | `30` | TTL страницы истории в Redis | +| `CACHE_MODE` | `protected` | `protected` (lock на страницу) или `naive` (только для эксперимента) | +| `CACHE_LOCK_TTL_MS` | `1000` | TTL lock-а страницы | +| `CACHE_MISS_DELAY_MS` | `0` | искусственная пауза на промахе; влияет только на `naive` | +| `METRICS_TOKEN` | — (необязательна) | токен доступа к `/internal/metrics*`; без него endpoint закрыт | +| `DRAIN_DELAY_MS` | `5000` (в стенде `3000`) | окно дрейна при `SIGTERM`: реплика сразу перестаёт проходить пробу LB, ждёт этот интервал и только затем закрывает сервер и пулы | +| `INSTANCE_ID` | hostname контейнера | значение `X-Instance-Id`; в Docker hostname = короткий ID контейнера, поэтому `scripts/stop-one.sh` находит реплику по нему | +| `IDEMPOTENCY_TTL_SECONDS` | `86400` | срок хранения журнала Idempotency-Key (минимум 60) | + +Поддерживаются также `DATABASE_URL_FILE` / `REDIS_URL_FILE`: если заданы, значение +читается из файла (docker secrets / Swarm). Подробности и компромиссы — +[`../REPORT.md`](../REPORT.md), раздел «Конфигурация и секреты». + +## Эндпоинты + +Публичный контракт — [`../contracts/openapi.json`](../contracts/openapi.json). + +| Метод и путь | Аутентификация | Назначение | +|---|---|---| +| `GET /health/live` | нет | процесс жив; не обращается к зависимостям, поэтому отвечает `200` и при их отказе | +| `GET /health/ready` | нет | готовность реплики: флаг дреина + PostgreSQL и Redis (бюджет `HEALTH_TIMEOUT_MS`), иначе `503`; эту же ручку пробует HAProxy | +| `POST /api/v1/users` | нет | регистрация с паролем (Argon2id) | +| `POST /api/v1/auth/sessions` | `Basic` | вход, выдаёт opaque Bearer-токен | +| `DELETE /api/v1/auth/sessions/current` | `Bearer` | отзыв текущей сессии, `204` | +| `GET /api/v1/users/me` | `Bearer` | текущий пользователь | +| `POST /api/v1/chats` | `Bearer` | создание чата (транзакция) | +| `GET /api/v1/chats` | `Bearer` | список доступных чатов | +| `GET /api/v1/chats/:chatId` | `Bearer` | чат по id (чужой — `404`) | +| `POST /api/v1/chats/:chatId/messages` | `Bearer` + обязательный `Idempotency-Key` | отправка сообщения (см. ниже) | +| `GET /api/v1/chats/:chatId/messages` | `Bearer` | история, keyset-пагинация `(created_at, id)` | +| `GET /internal/metrics` | `Bearer METRICS_TOKEN` | счётчики и агрегаты латентности | +| `POST /internal/metrics/reset` | `Bearer METRICS_TOKEN` | сброс счётчиков (`200`) | + +Правила: `Basic` принимается только на создании сессии, бизнес-запросы — только +`Bearer`; заголовок `X-User-Id` не аутентифицирует и не подменяет владельца +сессии; истёкший, отозванный или неизвестный токен — `401` с соответствующим +`WWW-Authenticate`. + +## npm-скрипты + +| Команда | Что делает | +|---|---| +| `npm run build` | сборка в `dist/` | +| `npm run start:dev` | watch-режим | +| `npm run start:prod` | запуск собранного приложения (`node dist/main`) | +| `npm test` / `npm run test:unit` | unit-тесты (vitest) | +| `npm run test:e2e` | black-box набор против запущенного API (см. ниже) | +| `npm run test:dependencies` | автоматическая проверка отказов Redis/PostgreSQL: останавливает и поднимает контейнеры Compose-стенда | +| `npm run idempotency:gc` | ручной прогон удаления просроченных записей Idempotency-Key | +| `npm run lint` | oxlint по `src/` и `test/` | +| `npm run format` | prettier | +| `npm run graph` | граф модулей и провайдеров через nestjs-spelunker (пишет `../docs/architecture/`) | + +## Тесты + +Unit-тесты не требуют инфраструктуры: + +```sh +npm test +``` + +Интеграционный набор работает по HTTP против запущенного стенда и дополнительно +подключается к PostgreSQL (очистка тестовых данных) и Redis: + +```sh +export API_BASE_URL=http://localhost:8080 +export DATABASE_URL=postgresql://messenger:@:5432/messenger +export REDIS_URL=redis://:6379 +npm run test:e2e +``` + +Название `API_BASE_URL` (а не `BASE_URL`) выбрано потому, что Vitest сам +устанавливает `process.env.BASE_URL` в значение Vite base. Кейс истечения сессии +включается переменной `SESSION_TTL_SECONDS=2` и у запущенного API, и у набора. + +## Отказы зависимостей + +Отдельный набор останавливает и поднимает контейнеры Compose-стенда и проверяет +ограниченные ошибки вместо 500: + +```sh +cd .. # из корня репозитория, стенд поднят через docker compose +make test-dependencies +``` + +Он включён по флагу `E2E_DEPENDENCY_FAULTS=1` (им же управляет цель Makefile), +потому что требует прав на управление Docker Compose-проектом; остальные +переменные — `E2E_COMPOSE_DIR`, `E2E_COMPOSE_ENV_FILE`, `E2E_REDIS_SERVICE`, +`E2E_POSTGRES_SERVICE`, `E2E_API_SERVICE`, `E2E_LB_STATS_URL`, +`E2E_POSTGRES_USER`, `E2E_POSTGRES_DB`, `E2E_PG_HBA_PATH`. + +Проверяются три топологии отказа: + +1. **Отказ Redis** (общая зависимость): `/health/live` реплики остаётся `200`, + `/health/ready` и бизнес-запросы — `503 unavailable` с `request_id` из + `X-Request-Id` и без деталей драйвера, запрос без сессии — по-прежнему `401`. + Неготовыми становятся все реплики, HAProxy исключает их из ротации и + отвечает собственным `503`; после старта Redis сессия и подтверждённое + сообщение живы. +2. **Отказ PostgreSQL** (общая зависимость): то же плюс проверка, что после + возврата зависимости данные на месте и новая запись проходит. +3. **Отказ PostgreSQL у одной реплики** (локальный путь к БД): для адреса + выбранной реплики временно ставится `reject`-строка в `pg_hba.conf` (первое + совпадение) и обрываются её соединения. Выбранная реплика отвечает `503` и её + слот уходит в `DOWN`, здоровая продолжает `200`/`UP` и обслуживает все чтения + через LB; `pg_hba.conf` восстанавливается в `finally`, после чего реплика + возвращается в ротацию и снова получает запросы. + +Реплики API портов не публикуют, поэтому отдельная реплика опрашивается изнутри +своего контейнера (loopback), а состояние слотов — из `stats;csv` HAProxy +(`127.0.0.1:8404`). Каждый запрос к остановленной БД ждёт +`DATABASE_CONNECTION_TIMEOUT_MS`, поэтому набор идёт около минуты. + +## Реплики, балансировка и остановка реплики + +Все реплики запускаются из одного образа и одного `compose.yaml`; API портов не +публикует, трафик идёт только через HAProxy. Состояние, от которого зависит +корректность, лежит в общих PostgreSQL и Redis, поэтому клиент с одним +Bearer-токеном обслуживается любой репликой: + +```sh +make test BASE_URL=http://localhost:8080 # сквозной сценарий через LB +python3 scripts/lab3/lab3.py check spread # распределение X-Instance-Id +make test-failover ACTION_SCRIPT=scripts/stop-one.sh # отказ одной реплики +scripts/start-replica.sh # вернуть остановленную реплику +``` + +- `X-Instance-Id` = `INSTANCE_ID` или hostname процесса, стабилен для процесса; + в Docker hostname контейнера — короткий ID, поэтому `scripts/stop-one.sh` + останавливает ровно ту реплику, которую обслужил запрос теста. Тот же заголовок + добавляется к ответам об ошибках, которые формируются фильтром до интерсептора. +- **Остановка (`SIGTERM`)** идёт в порядке «сначала дрейн, потом разбор ресурсов»: + реплика помечается not-ready (проба LB и `/health/ready` начинают отвечать + `503`), `DRAIN_DELAY_MS` ждёт, пока LB её исключит (обнаружение ≈1 s), и лишь + затем закрываются HTTP-сервер, пул PostgreSQL и соединение Redis. Если + полагаться на стандартный порядок Nest (`onModuleDestroy` вызывает закрытие + пулов раньше `beforeApplicationShutdown`), реплика какое-то время остаётся в + ротации уже без ресурсов — это и было причиной явной последовательности в + `src/main.ts`. +- **Проба LB — тот же `/health/ready`**, что и healthcheck контейнера: реплика, + потерявшая обязательную зависимость (свой путь к PostgreSQL или Redis), + выходит из ротации, и трафик уходит на здоровые реплики. Проба с зависимостями + ограничена общим дедлайном `HEALTH_TIMEOUT_MS=400` на реплику; проверка + PostgreSQL (`SELECT 1`) идёт по отдельному соединению, а не из общего пула + бизнес-запросов, а для `PING` Redis стоит `AbortSignal.timeout`, поэтому проба + укладывается в `timeout check 500ms` HAProxy и не реагирует на перегрузку. + Когда недоступна общая для всех зависимость, + неготовыми становятся все реплики, и клиент получает `503` от самого LB: это + честный ответ «готовых узлов нет» вместо маскировки отказа. +- `/health/live` зависимостей не касается: он отвечает `200`, пока жив процесс, + и по нему проверяют, что реплика не упала (в том числе при отказе PG/Redis). + +## Идемпотентность отправки сообщения + +`POST /api/v1/chats/{chatId}/messages` требует заголовок `Idempotency-Key` +(ASCII `[A-Za-z0-9._:-]{1,128}`). Область уникальности — пользователь + метод + +путь чата + ключ, поэтому одинаковый ключ у разных пользователей или в разных +чатах не конфликтует. Журнал — таблица `idempotency_keys` (миграция 003), то +есть общая для всех реплик. + +| Ситуация | Ответ | +|---|---| +| Заголовок отсутствует, пуст или не подходит под шаблон | `400 invalid_request` (до обращения к БД) | +| Первый запрос с ключом | `201` и сообщение; заявка, вставка сообщения, инкремент версии чата и тело ответа записываются **одним оператором SQL** (атомарность обеспечивает сам оператор) | +| Повтор того же тела с тем же ключом | тот же `201` и тот же JSON (включая `id` и `created_at`), новое сообщение не создаётся | +| Другое тело с тем же ключом | `409 conflict` | +| Повтор после `404` (нет доступа к чату) | `404`, ключ не занят: оператор вставляет заявку только при наличии членства, иначе не создаётся ничего | +| Ключ старше `IDEMPOTENCY_TTL_SECONDS` | запись удаляется, ключ считается свободным | +| Одновременные повторы через разные реплики | ровно одно сообщение: заявка берётся `INSERT ... ON CONFLICT DO NOTHING`, повтор читает сохранённый ответ | + +Путь записи — один round trip: `messages.createIdempotent` проверяет членство, +занимает ключ (`INSERT ... ON CONFLICT DO NOTHING`), вставляет сообщение, поднимает +`chats.message_version` и сохраняет тело ответа, а повтор читает сохранённый ответ +вместе с повторной проверкой доступа (`idempotency.read`). До оптимизации это были +8 обращений к БД в явной транзакции (см. REPORT.md, раздел «A/B улучшения»). + +Отпечаток тела считается по валидированному DTO с фиксированным порядком полей, +поэтому порядок ключей в JSON не влияет на результат. Транзиентные сбои +PostgreSQL (`57014`, `40001`, `40P01`) и обрывы соединения отдаются как +`503 unavailable` и не фиксируются как окончательный результат: клиент может +безопасно повторить запрос с тем же ключом. Просроченные записи удаляются +фоновым проходом (`idempotency.purged` в метриках) под +`pg_try_advisory_xact_lock`, а также вручную: `npm run idempotency:gc`. + +## Харнесс экспериментов + +Уровни 3/4 и трек 1 обслуживает `../scripts/lab3/lab3.py` (только stdlib) и +профили k6 в `../scripts/load/`: `run`, `sweep`, `track1-extras`, `failover`, +`rolling-update`, `ab`, `analyze`, `watch`, `check`, `quiesce`, `track1`. +Сырые результаты прогонов, манифесты условий измерения и графики складываются в +`../evidence/track1/`. Подробности — в `../REPORT.md` и +`../evidence/README.md`. + +## Метрики + +`GET /internal/metrics` возвращает `{counters, latency_ms, instance_id, build_id}`: именованные SQL-метки, +операции Redis, `cache_hit`/`cache_miss`/`cache_lock_wait`, `session_get/set/del`, +`request_count` и процентили латентности (буфер ограничен). Деградации +наблюдаемы отдельными счётчиками: `cache_fallback`, `cache_write_error`, +`cache_unlock_error`, `cache_lock_timeout`, `cache_corrupt_entry`, +`database.pool_error`, `database.rollback_error`, `redis.error`, а также +идемпотентность: `idempotency.claim`, `idempotency.replay`, `idempotency.conflict`, +`idempotency.claim_lost`, `idempotency.released`, `idempotency.purged`, +`idempotency.purge_error`, `messages.write`. + +Счётчики локальны процессу: при нескольких репликах их нельзя суммировать как +распределённый снимок, а рестарт API обнуляет их (данные PostgreSQL и Redis при +этом не затрагиваются). `instance_id` и `build_id` позволяют сопоставить ответ +конкретной реплике и сборке — этим пользуются rolling update и эксперименты. diff --git a/app/nest-cli.json b/app/nest-cli.json new file mode 100644 index 00000000..5a21707a --- /dev/null +++ b/app/nest-cli.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://json.schemastore.org/nest-cli", + "collection": "@nestjs/schematics", + "sourceRoot": "src", + "compilerOptions": { + "deleteOutDir": true, + "assets": [ + { + "include": "database/migrations/*.sql", + "outDir": "dist" + } + ], + "watchAssets": true + } +} diff --git a/app/oxlint.json b/app/oxlint.json new file mode 100644 index 00000000..07ec3bd5 --- /dev/null +++ b/app/oxlint.json @@ -0,0 +1,10 @@ +{ + "$schema": "https://raw.githubusercontent.com/oxc-project/oxc/main/crates/oxc_linter/src/rules.rs", + "rules": { + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-floating-promises": "warn" + }, + "env": { + "node": true + } +} diff --git a/app/package-lock.json b/app/package-lock.json new file mode 100644 index 00000000..34fa6904 --- /dev/null +++ b/app/package-lock.json @@ -0,0 +1,8385 @@ +{ + "name": "app", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "app", + "version": "0.0.1", + "license": "UNLICENSED", + "dependencies": { + "@aws-sdk/client-s3": "^3.1156.0", + "@aws-sdk/s3-request-presigner": "^3.1156.0", + "@nestjs/common": "^12.0.1", + "@nestjs/config": "^12.0.0", + "@nestjs/core": "^12.0.1", + "@nestjs/platform-express": "^12.0.1", + "@nestjs/platform-fastify": "^12.0.3", + "@smithy/node-http-handler": "^4.12.1", + "amqplib": "^2.2.1", + "argon2": "^0.45.1", + "class-transformer": "^0.5.1", + "class-validator": "^0.15.1", + "date-fns": "^4.4.0", + "fastify": "^5.12.5", + "pg": "^8.23.1", + "redis": "^6.3.0", + "reflect-metadata": "^0.2.2", + "rxjs": "^7.8.1", + "sharp": "^0.35.5", + "zod": "^4.6.5" + }, + "devDependencies": { + "@nestjs/cli": "^12.0.0", + "@nestjs/mau": "^0.2.6", + "@nestjs/schematics": "^12.0.0", + "@nestjs/testing": "^12.0.1", + "@types/express": "^5.0.0", + "@types/node": "^24.0.0", + "@types/pg": "^8.23.1", + "@types/supertest": "^7.0.0", + "@vitest/coverage-v8": "^4.1.2", + "dotenv": "^18.0.5", + "nestjs-spelunker": "^1.3.2", + "oxlint": "^1.58.0", + "prettier": "^3.4.2", + "source-map-support": "^0.5.21", + "supertest": "^7.2.2", + "tsx": "^4.23.15", + "typescript": "^6.0.3", + "vite-tsconfig-paths": "^5.1.4", + "vitest": "^4.1.2" + } + }, + "node_modules/@angular-devkit/core": { + "version": "22.1.8", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-22.1.8.tgz", + "integrity": "sha512-34lsgg2FwMVBX7nR/ZqzRUcdvdYPvSB8XAEr2GudXAyZwLI9ehzfQlagAWR/gEb8p4uCFZW0r7uU0toxydq4Rw==", + "dev": true, + "license": "MIT", + "dependencies": { + "ajv": "8.20.0", + "ajv-formats": "3.0.1", + "jsonc-parser": "3.3.1", + "picomatch": "4.0.5", + "rxjs": "7.8.2", + "source-map": "0.7.6" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + }, + "peerDependencies": { + "chokidar": "^5.0.0" + }, + "peerDependenciesMeta": { + "chokidar": { + "optional": true + } + } + }, + "node_modules/@angular-devkit/schematics": { + "version": "22.1.8", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-22.1.8.tgz", + "integrity": "sha512-Pv3cPa/44kvEwYcqwx4Ns5dhIDmcFNSHhbpheqiEG1Z/u4e2t4zHKDtE3eHZB+8o+IcC7xJj+d+AqGR44RoZDA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.1.8", + "jsonc-parser": "3.3.1", + "magic-string": "1.0.0", + "ora": "9.4.1", + "rxjs": "7.8.2" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@angular-devkit/schematics-cli": { + "version": "22.1.8", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics-cli/-/schematics-cli-22.1.8.tgz", + "integrity": "sha512-SsGG8ompVzprmYE9sHi2L7DYZ+i1sD+YDfun6Rg2vkTCusejtD4SSGVxwuR6++dT22ZpN16OqUTmii/0bhE4xA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.1.8", + "@angular-devkit/schematics": "22.1.8", + "@inquirer/prompts": "8.5.2" + }, + "bin": { + "schematics": "bin/schematics.js" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@angular-devkit/schematics-cli/node_modules/@inquirer/prompts": { + "version": "8.5.2", + "resolved": "https://registry.npmjs.org/@inquirer/prompts/-/prompts-8.5.2.tgz", + "integrity": "sha512-IYR/3C/paEVVQYQvdDlFZVjRCJVYHHON0XXMH91KO9GSxs0TdKYWlUdvfQl2EfAHDxUaN3IBffkE/BDTh5nJ6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/checkbox": "^5.2.1", + "@inquirer/confirm": "^6.1.1", + "@inquirer/editor": "^5.2.2", + "@inquirer/expand": "^5.1.1", + "@inquirer/input": "^5.1.2", + "@inquirer/number": "^4.1.1", + "@inquirer/password": "^5.1.1", + "@inquirer/rawlist": "^5.3.1", + "@inquirer/search": "^4.2.1", + "@inquirer/select": "^5.2.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@aws-sdk/checksums": { + "version": "3.1001.5", + "resolved": "https://registry.npmjs.org/@aws-sdk/checksums/-/checksums-3.1001.5.tgz", + "integrity": "sha512-6erto9nK2OZO/woEgmFIaPh5gChgdNd7nL6jgAf8TAXz3/QHC8Pan2TFe9GYp3IxOIKCCGWPylSjtOHKfNzQlw==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/client-s3": { + "version": "3.1156.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.1156.0.tgz", + "integrity": "sha512-Z2PqQqKMDVX56tNZ6ggmjoI5o/8+4+8ArmjLwE5ktYsEzs1BrvklBT1ZzI+s0Vcf3wfPguoEMD7EkMswmEta/A==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/checksums": "^3.1001.5", + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/credential-provider-node": "^3.972.88", + "@aws-sdk/middleware-sdk-s3": "^3.972.81", + "@aws-sdk/signature-v4-multi-region": "^3.996.49", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/fetch-http-handler": "^5.8.0", + "@smithy/node-http-handler": "^4.12.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/core": { + "version": "3.978.5", + "resolved": "https://registry.npmjs.org/@aws-sdk/core/-/core-3.978.5.tgz", + "integrity": "sha512-ruI0AWqcrfL20XDS6SwZIJFPOBZ6N0ADa9nLzWlBhxk1aoUjbUX3JkTR9vEmk7o35XpAHi68ahZ4T6RE2U9XyA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.975.0", + "@aws-sdk/xml-builder": "^3.972.41", + "@aws/lambda-invoke-store": "^0.3.0", + "@smithy/core": "^3.36.1", + "@smithy/signature-v4": "^5.7.4", + "@smithy/types": "^4.19.0", + "bowser": "^2.11.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-env": { + "version": "3.972.76", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-env/-/credential-provider-env-3.972.76.tgz", + "integrity": "sha512-YxmL1Ar7bV9ZkJvtv+lj+MF6le76NnZnbmgySyZuxpZaCv0/1lZ3KxKy6RIj1h9ui2hWnVjZxbmTMeO0y/NrQA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-http": { + "version": "3.972.78", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-http/-/credential-provider-http-3.972.78.tgz", + "integrity": "sha512-3VHknDRngoebsaqyT1iR75b5cBT2zfEtODK6yYmYltljkK1HEAQLs2FEd1IpWqAeOYdMGaG9wikngWLTufY7RA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/fetch-http-handler": "^5.8.0", + "@smithy/node-http-handler": "^4.12.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-ini": { + "version": "3.973.21", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-ini/-/credential-provider-ini-3.973.21.tgz", + "integrity": "sha512-aLx9hbCX6mGAyv4ZZd8UagJ4RASHVJw6BFDSIyuKmLkqNL6H47EjBPK6qaJ4B5TDV6mc+vMa0SJFYyo4mkvVQg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/credential-provider-env": "^3.972.76", + "@aws-sdk/credential-provider-http": "^3.972.78", + "@aws-sdk/credential-provider-login": "^3.972.83", + "@aws-sdk/credential-provider-process": "^3.972.76", + "@aws-sdk/credential-provider-sso": "^3.973.20", + "@aws-sdk/credential-provider-web-identity": "^3.972.82", + "@aws-sdk/nested-clients": "^3.997.50", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/credential-provider-imds": "^4.5.2", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-login": { + "version": "3.972.83", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-login/-/credential-provider-login-3.972.83.tgz", + "integrity": "sha512-9bJU6FjRacF4FK/QHf8Dyxhur7B1grRj8C1Yh4IkpoFW2exQflPdCv0VrbemLW9ZctLNle+AE7TETs9KNvuLYg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/nested-clients": "^3.997.50", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-node": { + "version": "3.972.88", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-node/-/credential-provider-node-3.972.88.tgz", + "integrity": "sha512-AG0KID5qhQPzrFkh5b0Cws1Amytvzo0L0Xj6hMgJXEwD6ZXAqninDhSNAWJiN5zU4YVVZnuidnqwPwYJ04IamA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/credential-provider-env": "^3.972.76", + "@aws-sdk/credential-provider-http": "^3.972.78", + "@aws-sdk/credential-provider-ini": "^3.973.21", + "@aws-sdk/credential-provider-process": "^3.972.76", + "@aws-sdk/credential-provider-sso": "^3.973.20", + "@aws-sdk/credential-provider-web-identity": "^3.972.82", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/credential-provider-imds": "^4.5.2", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-process": { + "version": "3.972.76", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-process/-/credential-provider-process-3.972.76.tgz", + "integrity": "sha512-pdhKFTXv1zPYhh92MTlp0jVbLWvUEV9v+rziuEkoskGGQ2iJskxHTO38nA8UAbT/N4IM3n8OZJ9rIY5IBO15/A==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-sso": { + "version": "3.973.20", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-sso/-/credential-provider-sso-3.973.20.tgz", + "integrity": "sha512-DlxrAB3MnjNLx/da7b89W+3DyJQYso/q7Ns15MsST2IV2nkAYQOYTUCaffNSjgrpMP99Dm2mV2jxzcbyF94ZfA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/nested-clients": "^3.997.50", + "@aws-sdk/token-providers": "3.1155.0", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-web-identity": { + "version": "3.972.82", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-web-identity/-/credential-provider-web-identity-3.972.82.tgz", + "integrity": "sha512-LUo46IqPTJ4xKBsrLuoaxiv/D2GTBCcFDMD1y3jKIqPR/p7Rw14RJFZ0iRWftqZtyLPQX9JMGnSi3mBu4y/pNA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/nested-clients": "^3.997.50", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/middleware-sdk-s3": { + "version": "3.972.81", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-sdk-s3/-/middleware-sdk-s3-3.972.81.tgz", + "integrity": "sha512-jfanv1pV0sJFZw6/DPiNjvK7B478god0zOum5yIWo+/+1Gy+CSXWpGBWx+3nBJyDvjcu/2kUMr4ADUp9BHH34Q==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/signature-v4-multi-region": "^3.996.49", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/nested-clients": { + "version": "3.997.50", + "resolved": "https://registry.npmjs.org/@aws-sdk/nested-clients/-/nested-clients-3.997.50.tgz", + "integrity": "sha512-nKb8qu4QsnpZTq/6JIXc72mzuzfh7BK4sUQkpQznPec2z4W7Ob2xYkcM4i5e1yGTBnbJVeFv/0sf3jcz85hclA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/signature-v4-multi-region": "^3.996.49", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/fetch-http-handler": "^5.8.0", + "@smithy/node-http-handler": "^4.12.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/s3-request-presigner": { + "version": "3.1156.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/s3-request-presigner/-/s3-request-presigner-3.1156.0.tgz", + "integrity": "sha512-M4yj4HmAPydhIMxKK90eTcnHr5FHBl000d2oFMAWpt+XPX86c2kmNJpiYbKrPLIe8jyK9sweRt5CDdWrhq/QTg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/signature-v4-multi-region": "^3.996.49", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/signature-v4-multi-region": { + "version": "3.996.49", + "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.996.49.tgz", + "integrity": "sha512-nvuAZNRrRRVCiARRKsYlQ0VnXiEQiq7vXPH+UA+LSo+kN3Djl9eo/V2r0udkw85PnsNshmv2Em1clld5M3j67w==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.975.0", + "@smithy/signature-v4": "^5.7.4", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/token-providers": { + "version": "3.1155.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/token-providers/-/token-providers-3.1155.0.tgz", + "integrity": "sha512-iIcQnYLhWuq9qOLQbIWFrxF3PF0svVeZvHnUS4DUnO4zUFmFHIyGyjqrntXG+e/5PUgfavV0pYfDIKHxTzN2LA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.978.5", + "@aws-sdk/nested-clients": "^3.997.50", + "@aws-sdk/types": "^3.975.0", + "@smithy/core": "^3.36.1", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/types": { + "version": "3.975.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/types/-/types-3.975.0.tgz", + "integrity": "sha512-WQlhs6juByzWOzj5fwxOx0mPJaEqMcG/sDpl3Id61B3XhKE63xY+0QWcnG+sWR/sCfprmreyUo1lM7IKZmHMzw==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/xml-builder": { + "version": "3.972.41", + "resolved": "https://registry.npmjs.org/@aws-sdk/xml-builder/-/xml-builder-3.972.41.tgz", + "integrity": "sha512-ctjVSyCMegrWfXlx6VqzSBFI6UqmQ5ZlnfMhdLIiWmhoH8UAQxSCP5N3OpG7X3k4LnS7ou74C4mt20+bfTW2aQ==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws/lambda-invoke-store": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@aws/lambda-invoke-store/-/lambda-invoke-store-0.3.0.tgz", + "integrity": "sha512-sl4Bm6yiMNYrZKkqqDFWN0UfnWhlS8ivKxrYl+6t0gCLrqr8y3B2IqZZbFRkfaVVp7C/baApyh71P+LeE1A2sQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@babel/helper-string-parser": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.29.7.tgz", + "integrity": "sha512-Pb5ijPrZ89GDH8223L4UP8i6QApWxs04RbPQJTeWDV0/keR2E36MeKnyr6LYmUUvqRRI+Iv87SuF1W6ErINzYw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/helper-validator-identifier": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz", + "integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/parser": { + "version": "7.29.8", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.29.8.tgz", + "integrity": "sha512-E8lTAYNB1KW+FH+VGJuZM1ioAx2E6oVlvQFRrf5P8ZZmsiJXYAD9vTFV7yyEURNzgh1dFqMZuO6tUwcARbqFCA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/types": "^7.29.8" + }, + "bin": { + "parser": "bin/babel-parser.js" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@babel/types": { + "version": "7.29.8", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.29.8.tgz", + "integrity": "sha512-Vj1jF3cPfxg7OAfoI7QnVKLoILlm2JF9pnVHrX8qx7AHMiYWT+NDAA7jChlNgRS4WTLc/fD1lXLmPixluj+3Gg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-string-parser": "^7.29.7", + "@babel/helper-validator-identifier": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@bcoe/v8-coverage": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-1.0.2.tgz", + "integrity": "sha512-6zABk/ECA/QYSCQ1NGiVwwbQerUCZ+TQbp64Q3AgmfNvurHH0j8TtXa1qbShXA6qqkpAj4V5W8pP6mLe1mcMqA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@borewit/text-codec": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@borewit/text-codec/-/text-codec-0.2.2.tgz", + "integrity": "sha512-DDaRehssg1aNrH4+2hnj1B7vnUGEjU6OIlyRdkMd0aUdIUvKXrJfXsy8LVtXAy7DRvYVluWbMspsRhz2lcW0mQ==", + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, + "node_modules/@colors/colors": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", + "integrity": "sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==", + "dev": true, + "license": "MIT", + "optional": true, + "engines": { + "node": ">=0.1.90" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.11.3", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.3.tgz", + "integrity": "sha512-Xz4Tpyki7XyrpbUK1jR1AhdAdaXyhhY4lZ3neLodmhpuWfy2PAQN5B46sAiU4liOXGLkHypn/qU+jvfWSCYYLA==", + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@epic-web/invariant": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@epic-web/invariant/-/invariant-1.0.0.tgz", + "integrity": "sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==", + "license": "MIT" + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz", + "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@fastify/ajv-compiler": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@fastify/ajv-compiler/-/ajv-compiler-4.0.6.tgz", + "integrity": "sha512-NtuzM0SfaMJbGlnjr9LWQUN5LzgSrbB8tf/wRZNas+4E1O/Nmzl53e7ruT61HDZyRCJGC6FxIogmNZO1c5ETBA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "ajv": "^8.12.0", + "ajv-formats": "^3.0.1", + "fast-uri": "^4.0.0" + } + }, + "node_modules/@fastify/ajv-compiler/node_modules/fast-uri": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-4.2.1.tgz", + "integrity": "sha512-TmHQgewjHtMq1E5QKA0tOE0yeYGQs25KZC/ziJpubRtWI15W92e6vPFydWOeZBVROSHBYw/QhD9d5OefdD6LDg==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/@fastify/cors": { + "version": "11.3.0", + "resolved": "https://registry.npmjs.org/@fastify/cors/-/cors-11.3.0.tgz", + "integrity": "sha512-ggQGua+xHv1MvePbPr0v//xLYEsCXbWspquXCJS9Ot5YoRXq8J8ZWzHnxDBVnbtXosvistXo6LtNzOJswf64Fw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "fastify-plugin": "^6.0.0", + "toad-cache": "^3.7.0" + } + }, + "node_modules/@fastify/error": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@fastify/error/-/error-4.2.0.tgz", + "integrity": "sha512-RSo3sVDXfHskiBZKBPRgnQTtIqpi/7zhJOEmAxCiBcM7d0uwdGdxLlsCaLzGs8v8NnxIRlfG0N51p5yFaOentQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT" + }, + "node_modules/@fastify/fast-json-stringify-compiler": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@fastify/fast-json-stringify-compiler/-/fast-json-stringify-compiler-5.1.0.tgz", + "integrity": "sha512-PxcYtKLbQ8Z+yApiqjK8FwxIwvEj38k2OiLc17u8dkJSlmfi2wHHPaSnaoqBPQqtvF8YVsDgDpP2snDCfFrpfw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "fast-json-stringify": "^7.0.0" + } + }, + "node_modules/@fastify/formbody": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/@fastify/formbody/-/formbody-9.0.0.tgz", + "integrity": "sha512-T/af26CSrUARBCvsEmv+DJLPfZlrRKESzqironxP1j7qzuLyKcoZtj6MuTGShuKx1THXugoie2oFbUJxXfGFzA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "fast-querystring": "^1.1.2", + "fastify-plugin": "^6.0.0" + } + }, + "node_modules/@fastify/forwarded": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@fastify/forwarded/-/forwarded-3.0.2.tgz", + "integrity": "sha512-NE8HgKLgYejV9lDpqkEFaDKMLYelJBVfHekhB0UKvX0ghagXRJqg68feg8er1NPXxG4N9i6vPxzt8E+3wHfcmA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT" + }, + "node_modules/@fastify/merge-json-schemas": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@fastify/merge-json-schemas/-/merge-json-schemas-0.2.1.tgz", + "integrity": "sha512-OA3KGBCy6KtIvLf8DINC5880o5iBlDX4SxzLQS8HorJAbqluzLRn80UXU0bxZn7UOFhFgpRJDasfwn9nG4FG4A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "dequal": "^2.0.3" + } + }, + "node_modules/@fastify/middie": { + "version": "9.3.4", + "resolved": "https://registry.npmjs.org/@fastify/middie/-/middie-9.3.4.tgz", + "integrity": "sha512-dd8phTnVWzx3iKPBKSvtRUT8+Igvy/MP+P5HcY2tjejEyeVbyjkD4HgzKessUObue5RRM5J6PZdxtAqgbn84yw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "@fastify/error": "^4.0.0", + "fastify-plugin": "^6.0.0", + "find-my-way": "^9.5.0", + "path-to-regexp": "^8.1.0", + "reusify": "^1.0.4" + } + }, + "node_modules/@fastify/proxy-addr": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@fastify/proxy-addr/-/proxy-addr-5.1.1.tgz", + "integrity": "sha512-zv07Y9GEuDsJPegZoDFd4SDWaZOW8N2pa0GSrYmKpId/tjt1Hgo3BjZBVjdVpfVrHaA+Qv5jawtS2O50J5xM9g==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "@fastify/forwarded": "^3.0.0", + "ipaddr.js": "^2.1.0" + } + }, + "node_modules/@fastify/proxy-addr/node_modules/ipaddr.js": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.5.0.tgz", + "integrity": "sha512-aq+t5NAc+cS6rZQQVWC2x98CPqGtKKTMDd4Gaodv0wShnItdKg/51djkGJ1hqH+Oy0ivDftCbSLCQob8zso01w==", + "license": "MIT", + "engines": { + "node": ">= 10" + } + }, + "node_modules/@img/colour": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", + "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/sharp-darwin-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.35.5.tgz", + "integrity": "sha512-QRUlFQ0WxvdWyqqG/WtI3iupfD5rBzmCHXSdPsY91sAtVtTo7Q4cb6zOccZ3gqEqkr0f1As1ehLqmEpDsRf+lg==", + "cpu": [ + "arm64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-darwin-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.35.5.tgz", + "integrity": "sha512-+BR255RhDlpygUpOc/Jdt1nT6DQ3XG/ERo5wbcdOf5Q320dKtPCKPLR1LJs9VGXRaMa8l1uUa0tkCNOXiAxZUw==", + "cpu": [ + "x64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-freebsd-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-freebsd-wasm32/-/sharp-freebsd-wasm32-0.35.5.tgz", + "integrity": "sha512-Y/z91nEZ4uIBX5X3nfTovjU9lHNKFYbL2lpHCLVNmXQK03VIZvXBBt0KxbPGp2SdGSF+2mQU4e+hQaWOt86iAw==", + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "dependencies": { + "@img/sharp-wasm32": "0.35.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.3.4.tgz", + "integrity": "sha512-5R89nBYiRdUlSWJxPhO+GVtaXzXSxKnRu/xqMn3KTA3L9EB9Oy/P+Nn2f2vlhPuUdy/Zusb2DarbyTpGCfEDuw==", + "cpu": [ + "arm64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.3.4.tgz", + "integrity": "sha512-iR2OKH80yi0U+dUplyh3/xdpFvps6YkCwsXenIJxqxR1v9o+xtKTGbS9H7cps+2Vxjc8B1j96p75NmTGjIhtpQ==", + "cpu": [ + "x64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.3.4.tgz", + "integrity": "sha512-LmRtTsOHuvM2+wlO2Db37dx5MiZhB0FvSunciw48YjdOkZz9KAiRbm8ujeMOA1INqmei5NapFxYEK1D1ZSidmw==", + "cpu": [ + "arm" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.3.4.tgz", + "integrity": "sha512-Y3dgX/6lE2QhQb+Gxy0WZxfg9MEm/JBjamZpS2IklP7xIQoKN4hzAm7KcMVGtaVDt3neE9OKBC7vAfonA/Lr1A==", + "cpu": [ + "arm64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-ppc64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.3.4.tgz", + "integrity": "sha512-Le6boB8Tai0Nis+gIxIpKx68UDVVIqdR8Tin5Yf1z2LJJQLDJvCDRqRu+jC2qCoD+eIomonmOwB4smBRxfVpYQ==", + "cpu": [ + "ppc64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-riscv64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.3.4.tgz", + "integrity": "sha512-aHkkIEHPRdQEegJN20MLmGtxYD9R2wQr3Cwpddnu5+YKMt6Uzax7S9h5gpZTo8wyrGuZSlfQ63OevL5mTyOC7Q==", + "cpu": [ + "riscv64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-s390x": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.3.4.tgz", + "integrity": "sha512-ra/mB6MikESDUO7Yg+Mi95bFBb9GsObURuhnOv3OqknjGe9sZrG8tCe9q0xSIGrtLgvgw0gKnFWcK4blSgQOuQ==", + "cpu": [ + "s390x" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.3.4.tgz", + "integrity": "sha512-GJ//SSXbnwSDes02umB3nDJLFcQzw8a18V8fyhqr6tV515tOEMdImjjxj1AoafMRz56F3PHgftnj1QEKSU1zkw==", + "cpu": [ + "x64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-arm64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.3.4.tgz", + "integrity": "sha512-hvulFwtjUcagsis6BBxHwGFwWoNZjgYmULGVrZcyfNbjA8hKILbRxGg15/7w5HDyXHXUos/j6baAWqnCyQ2DWA==", + "cpu": [ + "arm64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-x64": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.3.4.tgz", + "integrity": "sha512-6zXKeE/p39I1AmA3cJG35eyBGNqNddLnUXjhwBnsGjFPWqf5VKkDBEqaEkPDoTEtkxwi2vv8Tcr2mDyP4So7Fg==", + "cpu": [ + "x64" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-linux-arm": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.35.5.tgz", + "integrity": "sha512-LEaXK2WdXVK5ykcw0buWyPMsmLLL2vpHLD6yrNSW+JGEL3BZPA4tpKN6iaMc4AxTTAoaX/sU1rOL51lcIz48ZQ==", + "cpu": [ + "arm" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.35.5.tgz", + "integrity": "sha512-LYVx5JTsOM2CBzmxreh+nl64/3H6Xb09iSLknqH47z2T2DFFxDeFLP5y4dJwe6H7uGQlHPyEEtIqyo3DYsRwdQ==", + "cpu": [ + "arm64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-ppc64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.35.5.tgz", + "integrity": "sha512-QVxAAq8evVRI9ia2vqgwrmWucn5Dfv+JdWzj75pD8omHLPSP7f8p20O8jxzjCcuCEQEOtYOZUmX1hkiZ0kdevA==", + "cpu": [ + "ppc64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-riscv64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.35.5.tgz", + "integrity": "sha512-LtdreXguaavKODPIfzJ4kffx7UNt1omwtK0rch4EBbbSTXPnxWmYSayXdLJw0fJzQ97kHt1gL/yh4tvU+nCyRQ==", + "cpu": [ + "riscv64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-s390x": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.35.5.tgz", + "integrity": "sha512-UZasTOFiYzotTsGOCu42BfUzP6Tu6Do/947iRm1RsLKvlllxwGcn4RN27LibGWceix4Y+Pmw3jsnTcCQIgWjqA==", + "cpu": [ + "s390x" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.3.4" + } + }, + "node_modules/@img/sharp-linux-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.35.5.tgz", + "integrity": "sha512-SxFtLTeJInhAA9Q836kux2vZNeOBQEx658qvbboZScr0wIARym3IcGmW7KpVD5sbVg0Ojy+udFQdayYIZyoNog==", + "cpu": [ + "x64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-linuxmusl-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.35.5.tgz", + "integrity": "sha512-9HbMclmI1zlNkFRs3z9/eBtDjfD0sGlrX1z6b1qwmiFY5ElDLh4BC0LPBdVp7z1DXFiKlIcznf+ZlsuZzLxQqg==", + "cpu": [ + "arm64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.3.4" + } + }, + "node_modules/@img/sharp-linuxmusl-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.35.5.tgz", + "integrity": "sha512-4KOphqB035HrVdqLZfCgMzzERrQkkzOwRhl4OAkRO1YCldbaFjySXMaK534Mo0V+LndnlJk+sbUyLeU0ULyD1A==", + "cpu": [ + "x64" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.3.4" + } + }, + "node_modules/@img/sharp-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.35.5.tgz", + "integrity": "sha512-Ptsga1su4tQx+LLF1ECS9U6nz5kmrXKo6XVbtR48Ke3ZRxxgaWBu7IDtEe1quo8hiupwm6WFqxVlXaSf7IINGQ==", + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "optional": true, + "dependencies": { + "@emnapi/runtime": "^1.11.3" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-webcontainers-wasm32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-webcontainers-wasm32/-/sharp-webcontainers-wasm32-0.35.5.tgz", + "integrity": "sha512-hfhF/FmoQyTUkA0bIKFOtw536BQSeBMe6BF6QyWlrPxT754+TFLaZ7sKKTfvvM0yJgKgaYTwnFCIZ/GuDw5SUA==", + "cpu": [ + "wasm32" + ], + "license": "Apache-2.0", + "optional": true, + "dependencies": { + "@img/sharp-wasm32": "0.35.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-arm64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.35.5.tgz", + "integrity": "sha512-X4t7g+7ZA5DKblCBEXGjUqqemj4vczING/5viFwAL8h4N3qYeyjwdCvRLHi4EdOUI+2Z7UFlp1VM+p/AuEtm6Q==", + "cpu": [ + "arm64" + ], + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-ia32": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.35.5.tgz", + "integrity": "sha512-5Zm82LoBc43nhwNybZlG7Y1KO//Zhsn306fQl29ZOuStHLGTo3BWL83q3cznX0poxSAMuYL1On/BHBxkBeKr6A==", + "cpu": [ + "ia32" + ], + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-x64": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.35.5.tgz", + "integrity": "sha512-x76eH0vEiHlcMQu8Y8IenntaACtddpT6W0wmXtWrnKcnKI7ME5DdgqhAD6SEWOEl1v2zDvkZDhFA9KnURwpfqg==", + "cpu": [ + "x64" + ], + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@inquirer/ansi": { + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/@inquirer/ansi/-/ansi-2.0.8.tgz", + "integrity": "sha512-WpQM+Ti6Z40EFwwt+uL2p4UabT+W179zHp6HhLVOzfbwnVn05IPO/eXIZXGNqcT1jbQ15SujNLzQ39k4QPPxBQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + } + }, + "node_modules/@inquirer/checkbox": { + "version": "5.2.5", + "resolved": "https://registry.npmjs.org/@inquirer/checkbox/-/checkbox-5.2.5.tgz", + "integrity": "sha512-bRt8J8m+Fot9CXv+zNQGXUq2ET0MggR1fPz7v6edN6MFYmsbfGnMmkmWZJEegMKqrAC8ej/o1sqisHZXZJMAfQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.8", + "@inquirer/core": "^12.0.3", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/confirm": { + "version": "6.3.2", + "resolved": "https://registry.npmjs.org/@inquirer/confirm/-/confirm-6.3.2.tgz", + "integrity": "sha512-Xvr/0HggjddPtGppuqVmxhTw+Hr8PvsZ/k0HmOEaAqQEt80OITNkFWnsdNmyT0/eM4Ab+iJLx2R8rctlEyfSVg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/core": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@inquirer/core/-/core-12.0.3.tgz", + "integrity": "sha512-wsSy0sznmXwkty+2PzZwx00Cazc/E0r0B7mAzdGROz2Ct+DFZXaK7WDjGZvgjRldxH5ZhFVfF2lgkYrqgOw2KA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.8", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.1.1", + "cli-width": "^4.1.0", + "fast-wrap-ansi": "^0.2.0", + "mute-stream": "^3.0.0", + "signal-exit": "^4.1.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/editor": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/@inquirer/editor/-/editor-5.3.3.tgz", + "integrity": "sha512-YsKkS2q63IiLtaDK/9nqzdComN97SDQrmKiyNggN+ceP4ty+Z6VwyTz3FpjeUWeW1Efss2xHFKCC9sx7hnrsxg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/external-editor": "^3.0.5", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/expand": { + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/@inquirer/expand/-/expand-5.1.5.tgz", + "integrity": "sha512-uHuXLmXW+TtIfT/9vSBotypAkqn1n34Ul+CLGPos/xANyO4Ff5xZzkYhbKR4NEcfVK4a9mHQOpwVZzluSHFRGw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/external-editor": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/@inquirer/external-editor/-/external-editor-3.0.5.tgz", + "integrity": "sha512-f3QQJRIX5ZEneBHNUIuPjmbdzHnmRFJA8r2dkcb8q+OM5Uv5KtnuAttQumnrjcBVBM3mcTX1CkmtAkU58VRZxg==", + "dev": true, + "license": "MIT", + "dependencies": { + "chardet": "^2.1.1", + "iconv-lite": "^0.7.2" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/figures": { + "version": "2.0.9", + "resolved": "https://registry.npmjs.org/@inquirer/figures/-/figures-2.0.9.tgz", + "integrity": "sha512-EAWgUTGQ/Umgga51dE3B2PUHbufuXarDfg86uVgoSgNHNNQnyFKcOrQLWVqYMghuSyHh8+2HUH0Js9cTC1WAdg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + } + }, + "node_modules/@inquirer/input": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/@inquirer/input/-/input-5.1.6.tgz", + "integrity": "sha512-HtcJhB2QFVXbLuJ5S3syhNbTUVxYvwqV4VRBDkQceBloC9bmTViUoRFP5PbSaDZb3HzfPmpuU/gG4ybVBz4FHA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/number": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/@inquirer/number/-/number-4.2.3.tgz", + "integrity": "sha512-6Yuwh1NGSbu1Lo4N1EWjXs1jKRntLg/ZCwhmeorEHde90v1XxAozdbd4Iu30eOQLW+6h1hp2O9ujNfLSbTPJnA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/password": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/@inquirer/password/-/password-5.2.2.tgz", + "integrity": "sha512-W9zYdyzogK+6110mqwaSJWCBu2yA5Q/OfnGSjjZB1bNpHlmUozXxTl0+QOZBNeVd6Qo81/qT75gW05gLAtITxw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.8", + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/prompts": { + "version": "8.7.2", + "resolved": "https://registry.npmjs.org/@inquirer/prompts/-/prompts-8.7.2.tgz", + "integrity": "sha512-QoRB4wFIjgH5iOhSjoIKMkTvSHDuV+O3OITlIqAYO0oK5x364GJILXiMBvlPiE+klg7Xx9tq5XVqQHcGUDYYPA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/checkbox": "^5.2.5", + "@inquirer/confirm": "^6.3.2", + "@inquirer/editor": "^5.3.3", + "@inquirer/expand": "^5.1.5", + "@inquirer/input": "^5.1.6", + "@inquirer/number": "^4.2.3", + "@inquirer/password": "^5.2.2", + "@inquirer/rawlist": "^5.3.5", + "@inquirer/search": "^4.3.3", + "@inquirer/select": "^5.2.5" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/rawlist": { + "version": "5.3.5", + "resolved": "https://registry.npmjs.org/@inquirer/rawlist/-/rawlist-5.3.5.tgz", + "integrity": "sha512-1oHky1ONfCOwNrnkQGDE1oaSij/3fI6HFMSf2H/WsGO2lEyDX9My82iggITSy9ddSZ8yk8j9v41OI0fVoSIoaA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/search": { + "version": "4.3.3", + "resolved": "https://registry.npmjs.org/@inquirer/search/-/search-4.3.3.tgz", + "integrity": "sha512-fyuIU1Nbpvwlikjg3gXwJFDI11+EFjqQ7P+iByfmivIKQ1vmaykNrD/vy5unHuUqUpsOsnvJ25//tPF7E/RBRA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/select": { + "version": "5.2.5", + "resolved": "https://registry.npmjs.org/@inquirer/select/-/select-5.2.5.tgz", + "integrity": "sha512-9kc15hr8r/kI+3DO/xLog5nOzTz1jqsHXa6JBFzmQKhkoJ8Slda1I1L/uD8ZSZ9tF1yp79wwXe7mclvX1rqR2Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.8", + "@inquirer/core": "^12.0.3", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/type": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.1.1.tgz", + "integrity": "sha512-yJoHYrMnxIsJZCY+0Vb66Dy3he3kL3e2wOBKhoSwWWAzZAY82emlxwgprCtp6yRixvNRNq9ztfRWQYPNr3Go7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz", + "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/@lukeed/csprng": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@lukeed/csprng/-/csprng-1.1.0.tgz", + "integrity": "sha512-Z7C/xXCiGWsg0KuKsHTKJxbWhpI3Vs5GwLfOean7MGyVFGqdRgBbAjOCh6u4bbjPc/8MJ2pZmK/0DLdCbivLDA==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/@nestjs/cli": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/cli/-/cli-12.0.3.tgz", + "integrity": "sha512-HofsPoiWsx6gZ7/IivaZJrgsq/3CTCb4FkCQzFu/Q3J2V+4xC7GYnit/HNB66EfuY8j3gWJCzasMKmF5VugmOA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.1.8", + "@angular-devkit/schematics": "22.1.8", + "@angular-devkit/schematics-cli": "22.1.8", + "@inquirer/prompts": "8.7.2", + "@nestjs/schematics": "^12.0.0", + "ansis": "4.4.0", + "chokidar": "5.0.0", + "cli-table3": "0.6.5", + "commander": "15.0.0", + "minimatch": "10.2.6", + "node-emoji": "2.2.0", + "ora": "9.4.1", + "tsconfig-paths": "4.2.0", + "typescript": "~6.0.2" + }, + "bin": { + "nest": "bin/nest.js" + }, + "engines": { + "node": ">= 20.11" + }, + "peerDependencies": { + "@rspack/core": "^1.7.7 || ^2.1.10", + "@swc/cli": "^0.8.0", + "@swc/core": "^1.15.18", + "fork-ts-checker-webpack-plugin": "^9.1.0", + "ts-loader": "^9.5.4", + "tsconfig-paths-webpack-plugin": "^4.2.0", + "webpack": "^5.105.4", + "webpack-node-externals": "^3.0.0" + }, + "peerDependenciesMeta": { + "@rspack/core": { + "optional": true + }, + "@swc/cli": { + "optional": true + }, + "@swc/core": { + "optional": true + }, + "fork-ts-checker-webpack-plugin": { + "optional": true + }, + "ts-loader": { + "optional": true + }, + "tsconfig-paths-webpack-plugin": { + "optional": true + }, + "webpack": { + "optional": true + }, + "webpack-node-externals": { + "optional": true + } + } + }, + "node_modules/@nestjs/common": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-12.0.3.tgz", + "integrity": "sha512-Fosz6lHc9OagZNcF0LTrVHyJS8W7mtuNFBOo9RRccGwyeICBd0hiutjAi9zxnzBcP45RkYXeNB+idA/52jZhBQ==", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "1.1.0", + "file-type": "22.1.0", + "iterare": "1.2.1", + "load-esm": "1.0.3", + "tslib": "2.8.1", + "uid": "2.0.2" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nest" + }, + "peerDependencies": { + "class-transformer": ">=0.4.1", + "class-validator": ">=0.13.2", + "reflect-metadata": "^0.1.12 || ^0.2.0", + "rxjs": "^7.1.0" + }, + "peerDependenciesMeta": { + "class-transformer": { + "optional": true + }, + "class-validator": { + "optional": true + } + } + }, + "node_modules/@nestjs/config": { + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-12.0.0.tgz", + "integrity": "sha512-pG5+fFfCo3cdgVkLJLvvhUGfxEjwzv9hIAQ1aJVHvHlHVniDWfwX5pR/utgoAkJzSikv2oTR1BmIF03FXdi42Q==", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "1.1.0", + "dotenv": "17.4.2", + "dotenv-expand": "13.0.0", + "es-toolkit": "1.51.0" + }, + "peerDependencies": { + "@nestjs/common": "^11.0.0 || ^12.0.0", + "rxjs": "^7.1.0" + } + }, + "node_modules/@nestjs/config/node_modules/dotenv": { + "version": "17.4.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.4.2.tgz", + "integrity": "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/@nestjs/core": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-12.0.3.tgz", + "integrity": "sha512-Ouv++gAXOL50S8K8n6AN19QGmw51rnIo6v7y9AqUYoaf0NcZlufTu5XTDeC2RtpMz8oXRr9nykBngRjflcoQKA==", + "license": "MIT", + "dependencies": { + "fast-safe-stringify": "2.1.1", + "iterare": "1.2.1", + "path-to-regexp": "8.4.2", + "tslib": "2.8.1", + "uid": "2.0.2" + }, + "engines": { + "node": ">= 20" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nest" + }, + "peerDependencies": { + "@nestjs/common": "^12.0.0", + "@nestjs/microservices": "^12.0.0", + "@nestjs/platform-express": "^12.0.0", + "@nestjs/websockets": "^12.0.0", + "reflect-metadata": "^0.1.12 || ^0.2.0", + "rxjs": "^7.1.0" + }, + "peerDependenciesMeta": { + "@nestjs/microservices": { + "optional": true + }, + "@nestjs/platform-express": { + "optional": true + }, + "@nestjs/websockets": { + "optional": true + } + } + }, + "node_modules/@nestjs/mau": { + "version": "0.2.8", + "resolved": "https://registry.npmjs.org/@nestjs/mau/-/mau-0.2.8.tgz", + "integrity": "sha512-nIb0i3c/KhKEbxZfYMEKv9Mcvk5miBdZcexnvkgnAUhoMO2BUjOGT+Khf2cZzu7B51Y7v01NG2JVl/HdQEUQ6w==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "4.1.2", + "cli-progress": "3.12.0", + "commander": "4.1.1", + "inquirer": "8.2.6", + "node-emoji": "2.1.3", + "ora": "5.4.1", + "undici": "6.20.1" + }, + "bin": { + "mau": "bin/mau.js" + }, + "engines": { + "node": ">= 20.0.0" + } + }, + "node_modules/@nestjs/mau/node_modules/cli-spinners": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.9.2.tgz", + "integrity": "sha512-ywqV+5MmyL4E7ybXgKys4DugZbX0FC6LnwrhjuykIjnK9k8OQacQ7axGKnjDXWNhns0xot3bZI5h55H8yo9cJg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@nestjs/mau/node_modules/commander": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", + "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 6" + } + }, + "node_modules/@nestjs/mau/node_modules/is-interactive": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-1.0.0.tgz", + "integrity": "sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/@nestjs/mau/node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@nestjs/mau/node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@nestjs/mau/node_modules/node-emoji": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-2.1.3.tgz", + "integrity": "sha512-E2WEOVsgs7O16zsURJ/eH8BqhF029wGpEOnv7Urwdo2wmQanOACwJQh0devF9D9RhoZru0+9JXIS0dBXIAz+lA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@sindresorhus/is": "^4.6.0", + "char-regex": "^1.0.2", + "emojilib": "^2.4.0", + "skin-tone": "^2.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/@nestjs/mau/node_modules/ora": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/ora/-/ora-5.4.1.tgz", + "integrity": "sha512-5b6Y85tPxZZ7QytO+BQzysW31HJku27cRIlkbAXaNx+BdcVi+LlRFmVXzeF6a7JCwJpyw5c4b+YSVImQIrBpuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "bl": "^4.1.0", + "chalk": "^4.1.0", + "cli-cursor": "^3.1.0", + "cli-spinners": "^2.5.0", + "is-interactive": "^1.0.0", + "is-unicode-supported": "^0.1.0", + "log-symbols": "^4.1.0", + "strip-ansi": "^6.0.0", + "wcwidth": "^1.0.1" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@nestjs/platform-express": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-12.0.3.tgz", + "integrity": "sha512-q9ImjCMjbYYgqvRHHro6sAypNwhjspIAHad0RTL2ww5wYd9gwUGWMss7Q+ACCw5Sksu7Q8S/3rjSLhB+W6B6bg==", + "license": "MIT", + "dependencies": { + "cors": "2.8.6", + "express": "5.2.1", + "multer": "2.4.0", + "path-to-regexp": "8.4.2", + "tslib": "2.8.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nest" + }, + "peerDependencies": { + "@nestjs/common": "^12.0.0", + "@nestjs/core": "^12.0.0" + } + }, + "node_modules/@nestjs/platform-fastify": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/platform-fastify/-/platform-fastify-12.0.3.tgz", + "integrity": "sha512-Cn8jR8jwPJBk7ftkzunplz0LHDahnq7urAO9VjOV0OWkk1YCwaFMm4CElSk3BwBiDwP177anJjJUbVxTlASk4A==", + "license": "MIT", + "dependencies": { + "@fastify/cors": "11.3.0", + "@fastify/formbody": "9.0.0", + "@fastify/middie": "9.3.4", + "fast-querystring": "1.1.2", + "fastify": "5.12.4", + "find-my-way": "9.9.0", + "light-my-request": "6.6.0", + "path-to-regexp": "8.4.2", + "tslib": "2.8.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nest" + }, + "peerDependencies": { + "@fastify/static": "^10.1.2", + "@fastify/view": "^10.0.0 || ^11.0.0 || ^12.0.0", + "@nestjs/common": "^12.0.0", + "@nestjs/core": "^12.0.0" + }, + "peerDependenciesMeta": { + "@fastify/static": { + "optional": true + }, + "@fastify/view": { + "optional": true + } + } + }, + "node_modules/@nestjs/platform-fastify/node_modules/fastify": { + "version": "5.12.4", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-5.12.4.tgz", + "integrity": "sha512-zATD676fP6tBMsn+jQ7Q5/cO/Qo+149TXH0dUqUKel+1/hcZxwIbnflqtPa9EjlrvpY8eCZcL3M1UYrMc31Y7g==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "@fastify/ajv-compiler": "^4.0.5", + "@fastify/error": "^4.0.0", + "@fastify/fast-json-stringify-compiler": "^5.0.0", + "@fastify/proxy-addr": "^5.0.0", + "abstract-logging": "^2.0.1", + "avvio": "^9.0.0", + "fast-json-stringify": "^7.0.0", + "find-my-way": "^9.6.0", + "light-my-request": "^6.0.0", + "pino": "^9.14.0 || ^10.1.0", + "process-warning": "^5.1.0", + "rfdc": "^1.3.1", + "secure-json-parse": "^4.0.0", + "semver": "^7.6.0", + "toad-cache": "^3.7.0" + } + }, + "node_modules/@nestjs/schematics": { + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-12.0.0.tgz", + "integrity": "sha512-ilsxam7i+SWd2psr12faLVSk86FYKnxRUGO6jAGNi1P9+O0kRaYVOScyrKeIj97W9T9UJoJF38fg+7KgjY/nOg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.1.5", + "@angular-devkit/schematics": "22.1.5", + "comment-json": "5.0.0", + "jsonc-parser": "3.3.1", + "pluralize": "8.0.0" + }, + "engines": { + "node": ">=22.12.0" + }, + "peerDependencies": { + "prettier": "^3.0.0", + "typescript": ">=6.0.0" + }, + "peerDependenciesMeta": { + "prettier": { + "optional": true + } + } + }, + "node_modules/@nestjs/schematics/node_modules/@angular-devkit/core": { + "version": "22.1.5", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-22.1.5.tgz", + "integrity": "sha512-HiY6d5dkIdJs5grP9OHvgkf14QOcDIo+hbuT7YKuLItQ++ZxCwUabJMLCCJ5R0KrstE5NqED0dNcyk5WD01t5Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "ajv": "8.20.0", + "ajv-formats": "3.0.1", + "jsonc-parser": "3.3.1", + "picomatch": "4.0.5", + "rxjs": "7.8.2", + "source-map": "0.7.6" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + }, + "peerDependencies": { + "chokidar": "^5.0.0" + }, + "peerDependenciesMeta": { + "chokidar": { + "optional": true + } + } + }, + "node_modules/@nestjs/schematics/node_modules/@angular-devkit/schematics": { + "version": "22.1.5", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-22.1.5.tgz", + "integrity": "sha512-HTmo9y8wjXKtJGVTYomTjZsjWzhirpu1pPRAzsVob3eiYOuxv1qDRAgWiHXNGp5CpnbHunZweXY/WffNGOFRyA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.1.5", + "jsonc-parser": "3.3.1", + "magic-string": "1.0.0", + "ora": "9.4.1", + "rxjs": "7.8.2" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@nestjs/testing": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-12.0.3.tgz", + "integrity": "sha512-TYDvBI8HvYeldNCmsuSaE4XqSs1Mg7IekSPlJf+zbrVt3+s5sqt4ySMUhbTs8JpMTFMyqCwtPTGVHIcFVuTBbQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "tslib": "2.8.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/nest" + }, + "peerDependencies": { + "@nestjs/common": "^12.0.0", + "@nestjs/core": "^12.0.0", + "@nestjs/microservices": "^12.0.0", + "@nestjs/platform-express": "^12.0.0" + }, + "peerDependenciesMeta": { + "@nestjs/microservices": { + "optional": true + }, + "@nestjs/platform-express": { + "optional": true + } + } + }, + "node_modules/@noble/hashes": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.8.0.tgz", + "integrity": "sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^14.21.3 || >=16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@ogma/common": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@ogma/common/-/common-1.2.0.tgz", + "integrity": "sha512-y2GHkT4t3B9CoI7ELfgZXeMHiiOC/X4XvdeqYLbP5A3keop7fANzjd5pQdtyYQvdQ0lPzcHJiMvOGkIINZHZHQ==", + "dev": true + }, + "node_modules/@ogma/styler": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@ogma/styler/-/styler-1.1.0.tgz", + "integrity": "sha512-OtwJ8Ump3sKEAv2DIUi+R2G2Y5v3cEzqRO/sH11MTWikAhac31GZR/xzU+3BoPRT0u8oIob7N5dtOKTdPhqq3w==", + "dev": true, + "dependencies": { + "@ogma/common": "^1.1.1" + } + }, + "node_modules/@oxc-project/types": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.150.0.tgz", + "integrity": "sha512-rDS5/31E9HfPl/CIzGrn0DOlvBbXFseQ5URJ9sYMfstbKLD/c6Gm9vmRzRGDdAXyOIL4zmO37lc9RIwYqVruZw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/@oxlint/binding-android-arm-eabi": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-android-arm-eabi/-/binding-android-arm-eabi-1.83.0.tgz", + "integrity": "sha512-0yGY24EwsLk5YDe6F+VkmZyRHSwJDALa3nIrPpq7FXmp2lV2d0TzvBCGeZk+wgiULRGr5blhyr4QMp5KCXJUqA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-android-arm64": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-android-arm64/-/binding-android-arm64-1.83.0.tgz", + "integrity": "sha512-hHfJ0vc17A4iUjH5p9BsTUPYbYRNxGpvD2lbu1aBRk54bzNIx9o5TtYF39QPZcV95DagZd+4DEAw2RH3G2ZsMg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-darwin-arm64": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-darwin-arm64/-/binding-darwin-arm64-1.83.0.tgz", + "integrity": "sha512-hsOjYjszLb/3zym/TkzUMPAoQlTJcuzSyEPOAyA+skXJIX9M0o+4JfOtqopX/Vf4hSLrJ98j0nvFo23gzk8auQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-darwin-x64": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-darwin-x64/-/binding-darwin-x64-1.83.0.tgz", + "integrity": "sha512-mjh5oH2EA+wl5yRJYT9K9G61O2zFlpuv+yf2JwZOi0+dq2FnTUtm1h8i+5Ik0fXPWIu/k84I1psZR9aQsLAnyA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-freebsd-x64": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-freebsd-x64/-/binding-freebsd-x64-1.83.0.tgz", + "integrity": "sha512-fNHr64/YaO8YssuoDVC8+F4Uk5enR86q5uxfHkQrjAPs1dbAILOrD2uaud+J7MO8Fx774g44ERLD0IGIvZE48w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-arm-gnueabihf": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.83.0.tgz", + "integrity": "sha512-Qpwy3zzAwMj+8/lyYItHmkSMwbkprFNWTK7jPYDOxSyxEhaSLOWYUTCMkjF334J8/WD0nznCCsoBbIH6hpsuIw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-arm-musleabihf": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-arm-musleabihf/-/binding-linux-arm-musleabihf-1.83.0.tgz", + "integrity": "sha512-s+BirYLFq7JL2k9sP0XI3ZXJ9dYvJ8sX3jLCLoag7tt+zrSHpZxP0jqznfL+Gdgwu7ay0dYgGYJXrQvq3iWloA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-arm64-gnu": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.83.0.tgz", + "integrity": "sha512-7lihXt3vKr+GIyapNbHrnFHm/biiW30le6Zv/DExbAFPF6YwCQXVFlONPFehxs0CpGO4CBfYPM9rdDT+XMoIlg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-arm64-musl": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.83.0.tgz", + "integrity": "sha512-q63JalLYVkZiZvls1z3PPUnpmQluOMXp0khqQMznCeAPLGydfNY8JhvuA4WlK57JfrvikU8wB5lPVveqpIXvew==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-ppc64-gnu": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.83.0.tgz", + "integrity": "sha512-krQmDF+dRbxvdqVPV88ZuOoPPu8X5BuqDA8Hd+qcS4YMRQCb+nexA57DazgGsc/rGdKBe3QmV0mnv0bdpW/p5g==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-riscv64-gnu": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-riscv64-gnu/-/binding-linux-riscv64-gnu-1.83.0.tgz", + "integrity": "sha512-MmOl8Y6txEAXZU1RG8Rr264jQ6D7VPmqFsU/45x/FeWsGe32hklTqGrLE6UxHzp5Rjt0wP+20tY8YXKgSFB3mw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-riscv64-musl": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-riscv64-musl/-/binding-linux-riscv64-musl-1.83.0.tgz", + "integrity": "sha512-u1rMymh0W3JZkq370kzQsYPULGWqhE09pZRqnZvUSoYaI9pVO5yVX+iYIslmWuEgwuzH9YAaOsScJiobWCHoOw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-s390x-gnu": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.83.0.tgz", + "integrity": "sha512-y0zK3HNwGysu7rqtE+BQG/d0bx5gh/KwlOtghN8oWeK1KcWzeaLqtZrbm8owqdma1lFyrce/hTO5ismuNu+INQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-x64-gnu": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.83.0.tgz", + "integrity": "sha512-rS5gM0NgD7ngmuJmbIehsidtrOwKkLFwCQbKEeb9KuyQrrWNq5Zkn0uV6AYdXOMJ0grrWEiLwBuvMxt8w5vsNw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-linux-x64-musl": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-linux-x64-musl/-/binding-linux-x64-musl-1.83.0.tgz", + "integrity": "sha512-W2IH4EtpcPaWcvNGCA95YoDg4vxqE/ZiPCi3arrxEEpsK7+JQN9WYwrlYFx9pcdP6KPXqRqkv3zdQPHcx7b6YQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-openharmony-arm64": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-openharmony-arm64/-/binding-openharmony-arm64-1.83.0.tgz", + "integrity": "sha512-6LyKkUyoajssTPLlZmDbZIbu4IZ5B4bGuRUnBgCGpEvHP3FQMaYITncHA/unPUo7q+Z+pIu2HhdkQ+8d1SG7iA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-win32-arm64-msvc": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.83.0.tgz", + "integrity": "sha512-Uz/fObEtF0jmNJQJ8CGRBKfefYstS0/wjD3s6IGzP8nUwsJykHQJBiN3npHwKiGRGn/vvBEgNr4B3cCzmmatvg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-win32-ia32-msvc": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-win32-ia32-msvc/-/binding-win32-ia32-msvc-1.83.0.tgz", + "integrity": "sha512-u7XcvPW6Bk58tY5iWs2ESb0vJjoE/kuSpHxopbwp/p3ZtWVQXZ6wor5w3ssVTHOqd/v8b+QdhSFWQ4grEUNWpA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxlint/binding-win32-x64-msvc": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/@oxlint/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.83.0.tgz", + "integrity": "sha512-LZRubd7ph13QmAg4fFecTYVZkiYbROR2Htaxh/ufWRkDhPOm2wrwaEYR89e0YpPFD3dqBrPoxS7myBw5hmYA7Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@paralleldrive/cuid2": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/@paralleldrive/cuid2/-/cuid2-2.3.1.tgz", + "integrity": "sha512-XO7cAxhnTZl0Yggq6jOgjiOHhbgcO4NqFqwSmQpjK3b6TEE6Uj/jfSk6wzYyemh3+I0sHirKSetjQwn5cZktFw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@noble/hashes": "^1.1.5" + } + }, + "node_modules/@phc/format": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@phc/format/-/format-1.0.0.tgz", + "integrity": "sha512-m7X9U6BG2+J+R1lSOdCiITLLrxm+cWlNI3HUFA92oLO77ObGNzaKdh8pMLqdZcshtkKuV84olNNXDfMc4FezBQ==", + "license": "MIT", + "engines": { + "node": ">=10" + } + }, + "node_modules/@pinojs/redact": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@pinojs/redact/-/redact-0.4.0.tgz", + "integrity": "sha512-k2ENnmBugE/rzQfEcdWHcCY+/FM3VLzH9cYEsbdsoqrvzAKRhUZeRNhAZvB8OitQJ1TBed3yqWtdjzS6wJKBwg==", + "license": "MIT" + }, + "node_modules/@redis/bloom": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-6.3.0.tgz", + "integrity": "sha512-NQ5poYpZr0jv6zazN1cy6JY5kIipKFZ3R9GTL4kOUTX3a9YAfY29U8bsE4lWhVQEM+G4V0BXVC57mubIdDijeA==", + "license": "MIT", + "engines": { + "node": ">= 20.0.0" + }, + "peerDependencies": { + "@redis/client": "^6.3.0" + } + }, + "node_modules/@redis/client": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-6.3.0.tgz", + "integrity": "sha512-fHrjwGCBvAANGi+TuRuwKi83sIOzgHr8eh+Uf48w3fSX7Hhrz174NrWqfJhAC2vnN0d/tfaZpdr6+o6JC3QHwg==", + "license": "MIT", + "engines": { + "node": ">= 20.0.0" + }, + "peerDependencies": { + "@node-rs/xxhash": "^1.1.0", + "@opentelemetry/api": ">=1 <2" + }, + "peerDependenciesMeta": { + "@node-rs/xxhash": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + } + } + }, + "node_modules/@redis/json": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/@redis/json/-/json-6.3.0.tgz", + "integrity": "sha512-yt6vRfPBXtW/3qm53cF81TSQpLMjWdW4/2E9WQ2GgbvsKkuCKCFQIcMiKlil1ZMnezK+IQuS795xKU5psbaegw==", + "license": "MIT", + "engines": { + "node": ">= 20.0.0" + }, + "peerDependencies": { + "@redis/client": "^6.3.0" + } + }, + "node_modules/@redis/search": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/@redis/search/-/search-6.3.0.tgz", + "integrity": "sha512-RPXAZmKjZZG7Tm7m1w9keFdXzbL91PiSHQBfQRe9xwW40jNsTRbG0FwhIz4QlHPU4gKDAsCy8IQHj4yZl4H9rw==", + "license": "MIT", + "engines": { + "node": ">= 20.0.0" + }, + "peerDependencies": { + "@redis/client": "^6.3.0" + } + }, + "node_modules/@redis/time-series": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-6.3.0.tgz", + "integrity": "sha512-+fJOB8mN1z5WRakCJNh5lVdz1KqvHGSOistsKeitD24QsCtPwyRQXWC0iq6T/i/ubLE/YcguZqPm8F598MKk4A==", + "license": "MIT", + "engines": { + "node": ">= 20.0.0" + }, + "peerDependencies": { + "@redis/client": "^6.3.0" + } + }, + "node_modules/@rolldown/binding-android-arm-eabi": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.9.tgz", + "integrity": "sha512-tNISae1QEf/vkb3xkRcjV5SEdzPE97We5IVaa2Z8jSszQPZ8U60B/YCYpw4QI7VidYsBtKavczXf+DyDs9WGxw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.9.tgz", + "integrity": "sha512-YC8YsI30o606GTZi0VyzYlsDKFP8W61i/QzayHDkLbNEz/IShqAmTa+hsJRj13xTHA0H+6fk4b2UmGn+Q/cMlg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.2.9.tgz", + "integrity": "sha512-IwhlH3qK5urrY8hZiEgGkHKEFN901p/p2bjxCxJlr4GyNnF7wYpUvK+Y43uaRYuC4hpfjzbR3SJC3arX1jGvmw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.2.9.tgz", + "integrity": "sha512-XxpJfVzFh+jilRxIXUqcfYAYcunIc/XEzIizsOL1fcJee5Sf7H3mH8WlLmfHfluz5amqR88QQo9izKtmMlavAw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.2.9.tgz", + "integrity": "sha512-kSfvhmgeWyfkbT3p/1s5vSgboogoah2zkm9fX2zjg2hHxSV7T4KhMWRUUaRk4OXNqoD3QAUeRqLcs1aZOK4U1g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.2.9.tgz", + "integrity": "sha512-1RVzG17pxqbTfYLC352JlLt6kKLG+6Hr30n8DlIJqsnV5luUDd2Qdx9Ayw1Cabfyb1K9k0jXEZ7evxkRoT+uiw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.2.9.tgz", + "integrity": "sha512-BXqPvZ2drqVD+/Z8UpKwcs4Mp7grM+eGFku4CAEKrEtcbAsUpzREphK1sogCRZGreVPiMkiiBtw0n3TPteuqvw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.2.9.tgz", + "integrity": "sha512-11vWvo8YDwLzukt27J3aYDWU+gg2P7J+ZOmiJ0hkF5BXZDW7pVya7r40MXDy6ya0i9KamoENSVKIugvJNgFXIA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.2.9.tgz", + "integrity": "sha512-a1tijMkdwsIARtc0F39ApURROkf3NwqinI6TOiSSWCTR7dT96dffNvMUtDHnq64wKNTIZOIlzKrFvvFUznJiyw==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.2.9.tgz", + "integrity": "sha512-x6SQNdAvv4c3hWqTMaWuawzMX9myaCs/yEmlGsxJzkdClnHW7FbrjQuSiRDhuSYzEYoEMhsaJy9qHG/XNemJPQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.2.9.tgz", + "integrity": "sha512-9s0AZ8BFK5/n7B/TBoa2yJE3gI3KURrbXcPBlsAsvjU4VeJKgE90y1YtNxyEUIcHPQkg6/yfF3qihUrcM/Kf0Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.2.9.tgz", + "integrity": "sha512-P7VWAmV+WdJluH7ovnRGoiv2i8To7GAZ+kGzfGup635cyL7SyYl3lSUaA3Gp5THf0n/Co5EyEqb2zbqq+nMOHQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.2.9.tgz", + "integrity": "sha512-1qixtsE4BK8h+yS3BfmZ09UhA7O/N4IACva6YBr7EBvCJraByTuRcgOTaiA62Tm0vey3UcKXLOaoGHtYmNGEVg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.2.9.tgz", + "integrity": "sha512-ok8IQjcEPs1AKZfuEUznVBrJw+gK4soq+bx8b1X2XoMqVClarc1q5JDmVtWXY1xfr6ZuHTAsPXHTgTrqKTZeww==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.2.9.tgz", + "integrity": "sha512-Ip2mXoU0hM0boq3Rf+ekuT653OROSo6aSYcPT1VHE4q52KvyxgFkQgrgb/IEsxOuvQ2fZZbs8khJAyCEPM24/g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.1.tgz", + "integrity": "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@sindresorhus/is": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz", + "integrity": "sha512-t09vSN3MdfsyCHoFcTRCH/iUtG7OJ0CsjzB8cjAmKc/va/kIgeDI/TxsigdncE/4be734m0cvIYwNaV4i2XqAw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sindresorhus/is?sponsor=1" + } + }, + "node_modules/@smithy/core": { + "version": "3.36.2", + "resolved": "https://registry.npmjs.org/@smithy/core/-/core-3.36.2.tgz", + "integrity": "sha512-oRw0rB9QSUiCZfEs23G5F4Af+b2rQt1lItIICWu5IejFNd+uPM8SWqiKLMpQ6bX1Q7slsLbjuoE2niErFtkVPQ==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/credential-provider-imds": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/@smithy/credential-provider-imds/-/credential-provider-imds-4.5.2.tgz", + "integrity": "sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.2", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/fetch-http-handler": { + "version": "5.8.0", + "resolved": "https://registry.npmjs.org/@smithy/fetch-http-handler/-/fetch-http-handler-5.8.0.tgz", + "integrity": "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.18.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/node-http-handler": { + "version": "4.12.1", + "resolved": "https://registry.npmjs.org/@smithy/node-http-handler/-/node-http-handler-4.12.1.tgz", + "integrity": "sha512-ThMkboGeONWXAelq9FvGsuJC4rOi+qyC4/zhUF58xYpxUg5sQKx2VXZYJmtNjr4dSuBJ1HeJXETQILCz3wOHvw==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.18.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/signature-v4": { + "version": "5.7.4", + "resolved": "https://registry.npmjs.org/@smithy/signature-v4/-/signature-v4-5.7.4.tgz", + "integrity": "sha512-tHy0K0VtqNd5Y7Y41h0a0Lhh0L1GzC08dTWg0F7vRJWFtTENg7IZikf3wQkanYIRdb7ngoIPMTmqgUi401fEeQ==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.35.0", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/types": { + "version": "4.19.0", + "resolved": "https://registry.npmjs.org/@smithy/types/-/types-4.19.0.tgz", + "integrity": "sha512-r7jh49VJxGerfAcTQA6gXcKc+98zOp/tqRwzYjgOE+iSQsP6cEU1hq2QzbuipmP68QtYdY9wKEhiCQZIzHgZ4Q==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "license": "MIT" + }, + "node_modules/@tokenizer/inflate": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@tokenizer/inflate/-/inflate-0.4.1.tgz", + "integrity": "sha512-2mAv+8pkG6GIZiF1kNg1jAjh27IDxEPKwdGul3snfztFerfPGI1LjDezZp3i7BElXompqEtPmoPx6c2wgtWsOA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "token-types": "^6.1.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, + "node_modules/@tokenizer/token": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz", + "integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==", + "license": "MIT" + }, + "node_modules/@types/body-parser": { + "version": "1.19.6", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz", + "integrity": "sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/cookiejar": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.5.tgz", + "integrity": "sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/estree": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz", + "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/express": { + "version": "5.0.6", + "resolved": "https://registry.npmjs.org/@types/express/-/express-5.0.6.tgz", + "integrity": "sha512-sKYVuV7Sv9fbPIt/442koC7+IIwK5olP1KWeD88e/idgoJqDm3JV/YUiPwkoKK92ylff2MGxSz1CSjsXelx0YA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^5.0.0", + "@types/serve-static": "^2" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-5.1.3.tgz", + "integrity": "sha512-dPfW8NFiOF4wOHc7+N/QSxlY9cfSsenewGbAz8C8U/MULPd/YZ27LvJUIlzaXie7e6Ove9YunJGgC9tbHD2cKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, + "node_modules/@types/http-errors": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.5.tgz", + "integrity": "sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/methods": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz", + "integrity": "sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "24.13.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.13.5.tgz", + "integrity": "sha512-TXyindR+lBr22aJIdMQzCFHPHR6cR4js838mRDCSz5hOKWZvZwsXSSiXDmjRj4iJmgl+sR9O+1mkoVBSMadNug==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~7.18.0" + } + }, + "node_modules/@types/pg": { + "version": "8.23.1", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.23.1.tgz", + "integrity": "sha512-fKVHpikPdg4GKks3JuLEhvwSyvwzF23hnabPy6DD8ljVbC7+6J5dQzdv4arV6jqq57djnMgs1HKBxX4P8aBI3A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^2.2.0" + } + }, + "node_modules/@types/qs": { + "version": "6.15.1", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.15.1.tgz", + "integrity": "sha512-GZHUBZR9hckSUhrxmp1nG6NwdpM9fCunJwyThLW1X3AyHgd9IlHb6VANpQQqDr2o/qQp6McZ3y/IA2rVzKzSbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/send": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@types/send/-/send-1.2.1.tgz", + "integrity": "sha512-arsCikDvlU99zl1g69TcAB3mzZPpxgw0UQnaHeC1Nwb015xp8bknZv5rIfri9xTOcMuaVgvabfIRA7PSZVuZIQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-2.2.0.tgz", + "integrity": "sha512-8mam4H1NHLtu7nmtalF7eyBH14QyOASmcxHhSfEoRyr0nP/YdoesEtU+uSRvMe96TW/HPTtkoKqQLl53N7UXMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/http-errors": "*", + "@types/node": "*" + } + }, + "node_modules/@types/superagent": { + "version": "8.1.11", + "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-8.1.11.tgz", + "integrity": "sha512-KA7srSW/HENDtOw9DOqaFLgWuMqN9WgjEw62lh9dpvRaZDkhdOkazASd7X7i2eMUYLHa1U37ZttnePsH5zTDHw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/cookiejar": "^2.1.5", + "@types/methods": "^1.1.4", + "@types/node": "*", + "form-data": "^4.0.0" + } + }, + "node_modules/@types/supertest": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/@types/supertest/-/supertest-7.2.1.tgz", + "integrity": "sha512-4CbBvoYVLHL7+yhbYrZET0vsvuyXTC05aRe7dNQkwMzm56auceoy6Yu3K50uZmwfHna1os3CMSgM/3QVkUtPTw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/methods": "^1.1.4", + "@types/superagent": "^8.1.0" + } + }, + "node_modules/@types/validator": { + "version": "13.15.10", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.15.10.tgz", + "integrity": "sha512-T8L6i7wCuyoK8A/ZeLYt1+q0ty3Zb9+qbSSvrIVitzT3YjZqkTZ40IbRsPanlB4h1QB3JVL1SYCdR6ngtFYcuA==", + "license": "MIT" + }, + "node_modules/@vitest/coverage-v8": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/coverage-v8/-/coverage-v8-4.1.11.tgz", + "integrity": "sha512-8MVGEFnJIcdGjcbfKmeq8z0pZHH0JlVtoVZH9Q/qwUp6wyFnEJUBMrw9DCaj+ra3vShGmhavjalMIhPNxZAUcw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@bcoe/v8-coverage": "^1.0.2", + "@vitest/utils": "4.1.11", + "ast-v8-to-istanbul": "^1.0.0", + "istanbul-lib-coverage": "^3.2.2", + "istanbul-lib-report": "^3.0.1", + "istanbul-reports": "^3.2.0", + "magicast": "^0.5.2", + "obug": "^2.1.1", + "std-env": "^4.0.0-rc.1", + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@vitest/browser": "4.1.11", + "vitest": "4.1.11" + }, + "peerDependenciesMeta": { + "@vitest/browser": { + "optional": true + } + } + }, + "node_modules/@vitest/expect": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-4.1.11.tgz", + "integrity": "sha512-VX2x5vNJXET47KAFzwERI+KRMtTTCSWTfSMKsW7JsUsXV4psq++e3DvZpuTDOpHcxytiDs6p2nhVb2tVDiiUYw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.1.0", + "@types/chai": "^5.2.2", + "@vitest/spy": "4.1.11", + "@vitest/utils": "4.1.11", + "chai": "^6.2.2", + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/mocker": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-4.1.11.tgz", + "integrity": "sha512-2XJVD55d1o5AZous5CCGKS74g/riOj9odEt2bQpCVZeblHyHdnMeFl4jl0XjU21stf4mbjUkew2eXQZt65g5CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "4.1.11", + "estree-walker": "^3.0.3", + "magic-string": "^0.30.21" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/mocker/node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, + "node_modules/@vitest/pretty-format": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-4.1.11.tgz", + "integrity": "sha512-yiZzPbGTS9Sr/JpFl8zHrcIkAofNbFV6k21vIgQN/cY/oxZeXhJv5sc/MBJ5jFKWmWs+oJHw0UXLZjmf931+Vw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-4.1.11.tgz", + "integrity": "sha512-LztvUgdwMNJMIkj3hQnnxiC2Xy1zNxq928W/xhjCLaNCzqTZOudjwbQf6v9IntZGPw132i2Lq2rgTRZHD3JHNw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "4.1.11", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/snapshot": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-4.1.11.tgz", + "integrity": "sha512-pN7ikn1ON7h8ee4gIAp4AzyK+zBtJPzVbqOgu5LCEh4VaJVbPQcgYQYJIMGQPXVeJJq1fnfazis7a5pFNPahog==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "4.1.11", + "@vitest/utils": "4.1.11", + "magic-string": "^0.30.21", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/snapshot/node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, + "node_modules/@vitest/spy": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-4.1.11.tgz", + "integrity": "sha512-apNa/prQy2qCeywhnixOHPRCgGNhvg7T4Dapfl1GahLp/R+uhBm5cPyFoNVyqsNd2h1nJxL6BqqdIjiABL60YA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/utils": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.1.11.tgz", + "integrity": "sha512-zTCVGpyFsGWBhllOyKlTw/vnr6D9qxsfSDyfbyZmTyjHw5N/VuvzHpHoQjm2ZJzn4RJgx5w4r7V0er69CmLgPQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "4.1.11", + "convert-source-map": "^2.0.0", + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/abstract-logging": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/abstract-logging/-/abstract-logging-2.0.1.tgz", + "integrity": "sha512-2BjRTZxTPvheOvGbBslFSYOUkr+SjPtOnrLP33f+VIWLzezQpZcqVg7ja3L4dBXmzzgwT+a029jRx5PCi3JuiA==", + "license": "MIT" + }, + "node_modules/accepts": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", + "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==", + "license": "MIT", + "dependencies": { + "mime-types": "^3.0.0", + "negotiator": "^1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/ajv": { + "version": "8.20.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.20.0.tgz", + "integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==", + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "fast-uri": "^3.0.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/ajv-formats": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz", + "integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==", + "license": "MIT", + "dependencies": { + "ajv": "^8.0.0" + }, + "peerDependencies": { + "ajv": "^8.0.0" + }, + "peerDependenciesMeta": { + "ajv": { + "optional": true + } + } + }, + "node_modules/amqplib": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-2.2.1.tgz", + "integrity": "sha512-J3JjAydC2vJ/AgkgVAE0kVH5M4RNfm51zUaspHpwSai3W35lD3hKryvYY/xqMnYcNfR66NYL06Eik+Adn293dQ==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/ansi-escapes": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz", + "integrity": "sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "type-fest": "^0.21.3" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/ansis": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/ansis/-/ansis-4.4.0.tgz", + "integrity": "sha512-9k3v7xcHwgdO/DruxGIg4HtjvlAZlcnsX/mzqUb1t3NkYnl9kK2UJ+Gq0io+vQf7iT//BD/HB/NBkUR1LWxoeA==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=14" + } + }, + "node_modules/append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==", + "license": "MIT" + }, + "node_modules/argon2": { + "version": "0.45.1", + "resolved": "https://registry.npmjs.org/argon2/-/argon2-0.45.1.tgz", + "integrity": "sha512-skm+/WCjkGqCQxF7FG1LuZXM5yvbFjgbfiCGsud2oLgaDhh6b6dbH0b1EkghbM+xx4Bj8Ape+KKgixoIlWZicQ==", + "hasInstallScript": true, + "license": "MIT", + "dependencies": { + "@phc/format": "^1.0.0", + "cross-env": "^10.1.0", + "node-addon-api": "^8.9.0", + "node-gyp-build": "^4.8.4" + }, + "engines": { + "node": ">=16.17.0" + } + }, + "node_modules/array-timsort": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/array-timsort/-/array-timsort-1.0.3.tgz", + "integrity": "sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==", + "dev": true, + "license": "MIT" + }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, + "node_modules/ast-v8-to-istanbul": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/ast-v8-to-istanbul/-/ast-v8-to-istanbul-1.0.6.tgz", + "integrity": "sha512-fvpl29helSO2w/z7utIbrkNXILdrLwDwAMH2I/zPKlGf5244+gf+B4cyS1sANcrPY2h+hWCGSgC8N61s/+AF9A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "^0.3.31", + "estree-walker": "^3.0.3", + "js-tokens": "^10.0.0" + } + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/atomic-sleep": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", + "integrity": "sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==", + "license": "MIT", + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/avvio": { + "version": "9.3.0", + "resolved": "https://registry.npmjs.org/avvio/-/avvio-9.3.0.tgz", + "integrity": "sha512-g2tQ7LE7oOSqDfwEm3M+ZCMTJc7KiZCdJ4UwyZJb5ckTKyYu50OYmvv0mCFXPuYXoM4zkSt8zM9XQ9KCvxA74A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "@fastify/error": "^4.0.0", + "fastq": "^1.17.1" + } + }, + "node_modules/balanced-match": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", + "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "18 || 20 || >=22" + } + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "node_modules/body-parser": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz", + "integrity": "sha512-2cGmJupaNgg+QUwVLAucDuWuoMZ6EX9iHDRswZ5lsNYEmwPaRknMPCLZz07yTzVq/83p4o/wzbDZbBrTvGGTIw==", + "license": "MIT", + "dependencies": { + "bytes": "^3.1.2", + "content-type": "^2.0.0", + "debug": "^4.4.3", + "http-errors": "^2.0.1", + "iconv-lite": "^0.7.2", + "on-finished": "^2.4.1", + "qs": "^6.15.2", + "raw-body": "^3.0.2", + "type-is": "^2.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/body-parser/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/bowser": { + "version": "2.14.1", + "resolved": "https://registry.npmjs.org/bowser/-/bowser-2.14.1.tgz", + "integrity": "sha512-tzPjzCxygAKWFOJP011oxFHs57HzIhOEracIgAePE4pqB3LikALKnSzUyU4MGs9/iCEUuHlAJTjTc5M+u7YEGg==", + "license": "MIT" + }, + "node_modules/brace-expansion": { + "version": "5.0.12", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.12.tgz", + "integrity": "sha512-YovQ3rzhaLMIrDjNDMkNS01tea93qhEhG5xy8f6+R0l+dw3Ki+5sCoIoI942iuLZTHWogWktgwVDhU09iNEimQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^4.0.2" + }, + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/chai": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.2.2.tgz", + "integrity": "sha512-NUPRluOfOiTKBKvWPtSD4PhFvWCqOi0BGStNWs57X9js7XGTprSmFoz5F0tWhR4WPjNeR9jXqdC7/UpSJTnlRg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/char-regex": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/char-regex/-/char-regex-1.0.2.tgz", + "integrity": "sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + } + }, + "node_modules/chardet": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/chardet/-/chardet-2.2.0.tgz", + "integrity": "sha512-rddelWYNPRrXq6PtNEN2S3f6t9ILzvqaN5pVgi4kqt9jHQaXIial9PznB5iSPVlQSLNaaH22ItWz3EJtQ10+OA==", + "dev": true, + "license": "MIT" + }, + "node_modules/chokidar": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-5.0.0.tgz", + "integrity": "sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw==", + "dev": true, + "license": "MIT", + "dependencies": { + "readdirp": "^5.0.0" + }, + "engines": { + "node": ">= 20.19.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/class-transformer": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz", + "integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==", + "license": "MIT" + }, + "node_modules/class-validator": { + "version": "0.15.1", + "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.15.1.tgz", + "integrity": "sha512-LqoS80HBBSCVhz/3KloUly0ovokxpdOLR++Al3J3+dHXWt9sTKlKd4eYtoxhxyUjoe5+UcIM+5k9MIxyBWnRTw==", + "license": "MIT", + "dependencies": { + "@types/validator": "^13.15.3", + "libphonenumber-js": "^1.11.1", + "validator": "^13.15.22" + } + }, + "node_modules/cli-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", + "integrity": "sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==", + "dev": true, + "license": "MIT", + "dependencies": { + "restore-cursor": "^3.1.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/cli-progress": { + "version": "3.12.0", + "resolved": "https://registry.npmjs.org/cli-progress/-/cli-progress-3.12.0.tgz", + "integrity": "sha512-tRkV3HJ1ASwm19THiiLIXLO7Im7wlTuKnvkYaTkyoAPefqjNg7W7DHKUlGRxy9vxDvbyCYQkQozvptuMkGCg8A==", + "dev": true, + "license": "MIT", + "dependencies": { + "string-width": "^4.2.3" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/cli-spinners": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-3.4.0.tgz", + "integrity": "sha512-bXfOC4QcT1tKXGorxL3wbJm6XJPDqEnij2gQ2m7ESQuE+/z9YFIWnl/5RpTiKWbMq3EVKR4fRLJGn6DVfu0mpw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18.20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-table3": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/cli-table3/-/cli-table3-0.6.5.tgz", + "integrity": "sha512-+W/5efTR7y5HRD7gACw9yQjqMVvEMLBHmboM/kPWam+H+Hmyrgjh6YncVKK122YZkXrLudzTuAukUw9FnMf7IQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "string-width": "^4.2.0" + }, + "engines": { + "node": "10.* || >= 12.*" + }, + "optionalDependencies": { + "@colors/colors": "1.5.0" + } + }, + "node_modules/cli-width": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-4.1.0.tgz", + "integrity": "sha512-ouuZd4/dm2Sw5Gmqy6bGyNNNe1qt9RpmxveLSO7KcgsTnU7RXfsw+/bukWGo1abgBiMAic068rclZsO4IWmmxQ==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">= 12" + } + }, + "node_modules/clone": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", + "integrity": "sha512-JQHZ2QMW6l3aH/j6xCqQThY/9OH4D/9ls34cgkUBiEeocRTU04tHfKPBsUK1PqZCUQM7GiA0IIXJSuXHI64Kbg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.8" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true, + "license": "MIT" + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/commander": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz", + "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + }, + "node_modules/comment-json": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/comment-json/-/comment-json-5.0.0.tgz", + "integrity": "sha512-uiqLcOiVDJtBP8WGkZHEP+FZIhTzP1dxvn59EfoYUi9gqupjrBWVQkO2atDrbnKPwLeotFYDsuNb26uBMqB+hw==", + "dev": true, + "license": "MIT", + "dependencies": { + "array-timsort": "^1.0.3", + "esprima": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/component-emitter": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.1.tgz", + "integrity": "sha512-T0+barUSQRTUQASh8bx02dl+DhF54GtIDY13Y3m9oWTklKbb3Wv974meRpeZ3lp1JpLVECWWNHC4vaG2XHXouQ==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/content-disposition": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.1.0.tgz", + "integrity": "sha512-5jRCH9Z/+DRP7rkvY83B+yGIGX96OYdJmzngqnw2SBSxqCFPd0w2km3s5iawpGX8krnwSGmF0FW5Nhr0Hfai3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/convert-source-map": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", + "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", + "dev": true, + "license": "MIT" + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz", + "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==", + "license": "MIT", + "engines": { + "node": ">=6.6.0" + } + }, + "node_modules/cookiejar": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz", + "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", + "dev": true, + "license": "MIT" + }, + "node_modules/cors": { + "version": "2.8.6", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz", + "integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/cross-env": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.1.0.tgz", + "integrity": "sha512-GsYosgnACZTADcmEyJctkJIoqAhHjttw7RsFrVoJNXbsWWqaq6Ym+7kZjq6mS45O0jij6vtiReppKQEtqWy6Dw==", + "license": "MIT", + "dependencies": { + "@epic-web/invariant": "^1.0.0", + "cross-spawn": "^7.0.6" + }, + "bin": { + "cross-env": "dist/bin/cross-env.js", + "cross-env-shell": "dist/bin/cross-env-shell.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/cross-spawn": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", + "license": "MIT", + "dependencies": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/date-fns": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.4.0.tgz", + "integrity": "sha512-+1UMbeh68lH1SegH83CGWwpb6OHHbpSgr3+s5Eww5M4CAgswBpoWS0AjTOfEJ33HiYKz1hdj/KTFprzXHmq/6w==", + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/kossnocorp" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/defaults": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.4.tgz", + "integrity": "sha512-eFuaLoy/Rxalv2kr+lqMlUnrDWV+3j4pljOIJgLIhI058IQfWJ7vXhyEIHu+HtC738klGALYxOKDO0bQP3tg8A==", + "dev": true, + "license": "MIT", + "dependencies": { + "clone": "^1.0.2" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/dequal": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", + "integrity": "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/dezalgo": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz", + "integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==", + "dev": true, + "license": "ISC", + "dependencies": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, + "node_modules/dotenv": { + "version": "18.0.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-18.0.5.tgz", + "integrity": "sha512-aBrGvt6KhjxbEnatqMWOMidftwsrCiCvqh1yNIGB9QyF0J+VcWkKDMsvYOEm7ZS82oiV+BT1sTOtIlAmUlTpwQ==", + "dev": true, + "license": "BSD-2-Clause", + "bin": { + "dotenv": "dist/index.cjs" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/dotenv-expand": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-13.0.0.tgz", + "integrity": "sha512-aBfBS8eYIeXmpHI9ThIlA7/WLq+SLt18iXUZhb52rW89QLKQFoIpPG1bPeewoPZsTyjSSO3T7234FBVUM1V2rA==", + "license": "BSD-2-Clause", + "dependencies": { + "dotenv": "^17.4.2" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/dotenv-expand/node_modules/dotenv": { + "version": "17.4.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.4.2.tgz", + "integrity": "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true, + "license": "MIT" + }, + "node_modules/emojilib": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/emojilib/-/emojilib-2.4.0.tgz", + "integrity": "sha512-5U0rVMU5Y2n2+ykNLQqMoqklN9ICBT/KsvC1Gz6vqHbz2AXXGkG+Pm5rMWk/8Vjrr/mY9985Hi8DYzn1F09Nyw==", + "dev": true, + "license": "MIT" + }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-module-lexer": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.3.2.tgz", + "integrity": "sha512-poHGpORABojJJucnV9KbOavETW8lBVnphkW77ER5/BQ5Fz7oXSoCNek7IH3vR5nRjdsEz926ibFYX8KtLQmdyw==", + "dev": true, + "license": "MIT" + }, + "node_modules/es-object-atoms": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz", + "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-set-tostringtag": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", + "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", + "dev": true, + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.6", + "has-tostringtag": "^1.0.2", + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-toolkit": { + "version": "1.51.0", + "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.51.0.tgz", + "integrity": "sha512-zC2lQGkM7QX+Gm6iM3+WIdZJzthsEd14LvRNJneSO2hzyz/zNBENR8+YXWo1cKxgPBtV6ksPYHELbcwBRzmdCw==", + "license": "MIT", + "workspaces": [ + "docs", + "benchmarks", + "tests/types", + "tests/browser-compat" + ] + }, + "node_modules/esbuild": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.2", + "@esbuild/android-arm": "0.28.2", + "@esbuild/android-arm64": "0.28.2", + "@esbuild/android-x64": "0.28.2", + "@esbuild/darwin-arm64": "0.28.2", + "@esbuild/darwin-x64": "0.28.2", + "@esbuild/freebsd-arm64": "0.28.2", + "@esbuild/freebsd-x64": "0.28.2", + "@esbuild/linux-arm": "0.28.2", + "@esbuild/linux-arm64": "0.28.2", + "@esbuild/linux-ia32": "0.28.2", + "@esbuild/linux-loong64": "0.28.2", + "@esbuild/linux-mips64el": "0.28.2", + "@esbuild/linux-ppc64": "0.28.2", + "@esbuild/linux-riscv64": "0.28.2", + "@esbuild/linux-s390x": "0.28.2", + "@esbuild/linux-x64": "0.28.2", + "@esbuild/netbsd-arm64": "0.28.2", + "@esbuild/netbsd-x64": "0.28.2", + "@esbuild/openbsd-arm64": "0.28.2", + "@esbuild/openbsd-x64": "0.28.2", + "@esbuild/openharmony-arm64": "0.28.2", + "@esbuild/sunos-x64": "0.28.2", + "@esbuild/win32-arm64": "0.28.2", + "@esbuild/win32-ia32": "0.28.2", + "@esbuild/win32-x64": "0.28.2" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, + "node_modules/escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "dev": true, + "license": "BSD-2-Clause", + "bin": { + "esparse": "bin/esparse.js", + "esvalidate": "bin/esvalidate.js" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/expect-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", + "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/express": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz", + "integrity": "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw==", + "license": "MIT", + "dependencies": { + "accepts": "^2.0.0", + "body-parser": "^2.2.1", + "content-disposition": "^1.0.0", + "content-type": "^1.0.5", + "cookie": "^0.7.1", + "cookie-signature": "^1.2.1", + "debug": "^4.4.0", + "depd": "^2.0.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "finalhandler": "^2.1.0", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "merge-descriptors": "^2.0.0", + "mime-types": "^3.0.0", + "on-finished": "^2.4.1", + "once": "^1.4.0", + "parseurl": "^1.3.3", + "proxy-addr": "^2.0.7", + "qs": "^6.14.0", + "range-parser": "^1.2.1", + "router": "^2.2.0", + "send": "^1.1.0", + "serve-static": "^2.2.0", + "statuses": "^2.0.1", + "type-is": "^2.0.1", + "vary": "^1.1.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/external-editor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", + "integrity": "sha512-hMQ4CX1p1izmuLYyZqLMO/qGNw10wSv9QDCPfzXfyFrOaCSSoRfqE1Kf1s5an66J5JZC62NewG+mK49jOCtQew==", + "dev": true, + "license": "MIT", + "dependencies": { + "chardet": "^0.7.0", + "iconv-lite": "^0.4.24", + "tmp": "^0.0.33" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/external-editor/node_modules/chardet": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz", + "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", + "dev": true, + "license": "MIT" + }, + "node_modules/external-editor/node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dev": true, + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/fast-decode-uri-component": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/fast-decode-uri-component/-/fast-decode-uri-component-1.0.1.tgz", + "integrity": "sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg==", + "license": "MIT" + }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "license": "MIT" + }, + "node_modules/fast-json-stringify": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fast-json-stringify/-/fast-json-stringify-7.0.1.tgz", + "integrity": "sha512-eRSayARSbbwlBjpP4vnTTIRD5QPcIrmihPxDeN1DtKnHPg66UuJLx+8hlK1kaFdjvzyQ/dzALoi4vwAQ+T+iZA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "@fastify/merge-json-schemas": "^0.2.0", + "ajv": "^8.12.0", + "ajv-formats": "^3.0.1", + "fast-uri": "^4.0.0", + "json-schema-ref-resolver": "^3.0.0", + "rfdc": "^1.2.0" + } + }, + "node_modules/fast-json-stringify/node_modules/fast-uri": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-4.2.1.tgz", + "integrity": "sha512-TmHQgewjHtMq1E5QKA0tOE0yeYGQs25KZC/ziJpubRtWI15W92e6vPFydWOeZBVROSHBYw/QhD9d5OefdD6LDg==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/fast-querystring": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/fast-querystring/-/fast-querystring-1.1.2.tgz", + "integrity": "sha512-g6KuKWmFXc0fID8WWH0jit4g0AGBoJhCkJMb1RmbsSEUNvQ+ZC8D6CUZ+GtF8nMzSPXnhiePyyqqipzNNEnHjg==", + "license": "MIT", + "dependencies": { + "fast-decode-uri-component": "^1.0.1" + } + }, + "node_modules/fast-safe-stringify": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", + "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", + "license": "MIT" + }, + "node_modules/fast-string-truncated-width": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/fast-string-truncated-width/-/fast-string-truncated-width-3.0.3.tgz", + "integrity": "sha512-0jjjIEL6+0jag3l2XWWizO64/aZVtpiGE3t0Zgqxv0DPuxiMjvB3M24fCyhZUO4KomJQPj3LTSUnDP3GpdwC0g==", + "dev": true, + "license": "MIT" + }, + "node_modules/fast-string-width": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/fast-string-width/-/fast-string-width-3.0.2.tgz", + "integrity": "sha512-gX8LrtNEI5hq8DVUfRQMbr5lpaS4nMIWV+7XEbXk2b8kiQIizgnlr12B4dA3ZEx3308ze0O4Q1R+cHts8kyUJg==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-string-truncated-width": "^3.0.2" + } + }, + "node_modules/fast-uri": { + "version": "3.1.8", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz", + "integrity": "sha512-GZMtZUTNRpOVIECoXwLNZS5xUGE+mVNbTB8h/7Rwh2TFWcBQiPzTgyZi05BF9UMZKkLJv8XBRJTlU7zg8+ZfMg==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/fast-wrap-ansi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/fast-wrap-ansi/-/fast-wrap-ansi-0.2.2.tgz", + "integrity": "sha512-7F2Fl+TjRSenLqlU3UjSH0iyqopqoZIu7eZVpEirP2g1GtWa2G/ecEmBdgz31+Mxr+ELclgg6sokpSFIQiZ02Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-string-width": "^3.0.2" + } + }, + "node_modules/fastify": { + "version": "5.12.5", + "resolved": "https://registry.npmjs.org/fastify/-/fastify-5.12.5.tgz", + "integrity": "sha512-OB2k1dlxs5/NAABqeKV2FUHkSD2BbENsCak8yULVcymn3fHIPDVa9TI3SDnJSWYSllZmSYuZXy2gTnsT+Sut1A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "@fastify/ajv-compiler": "^4.0.5", + "@fastify/error": "^4.0.0", + "@fastify/fast-json-stringify-compiler": "^5.0.0", + "@fastify/proxy-addr": "^5.0.0", + "abstract-logging": "^2.0.1", + "avvio": "^9.0.0", + "fast-json-stringify": "^7.0.0", + "find-my-way": "^9.6.0", + "light-my-request": "^6.0.0", + "pino": "^9.14.0 || ^10.1.0", + "process-warning": "^5.1.0", + "rfdc": "^1.3.1", + "secure-json-parse": "^4.0.0", + "semver": "^7.6.0", + "toad-cache": "^3.7.0" + } + }, + "node_modules/fastify-plugin": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-6.0.0.tgz", + "integrity": "sha512-fZOty7z3O7vOliF6d8bHE3wiEh1KcNnKEQensSgTk9C1DvN6nRLS++XVd86v33Hw/8u9Un8A1zDrQ8ujcQDHEg==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT" + }, + "node_modules/fastq": { + "version": "1.20.3", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.3.tgz", + "integrity": "sha512-XKv5nnLs6nLF71NgiKJLIZFLkPyIEuOselLG7ujZnGrRfQK8HpvY+WqKhAJUAdLomwVHErVS4LfxFlPq0/FTAw==", + "license": "ISC", + "dependencies": { + "reusify": "^1.0.4" + } + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/figures": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", + "integrity": "sha512-yaduQFRKLXYOGgEn6AZau90j3ggSOyiqXU0F9JZfeXYhNa+Jk4X+s45A2zg5jns87GAFa34BBm2kXw4XpNcbdg==", + "dev": true, + "license": "MIT", + "dependencies": { + "escape-string-regexp": "^1.0.5" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/file-type": { + "version": "22.1.0", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-22.1.0.tgz", + "integrity": "sha512-fbvf9u6jnHWr1EP6V8R99E9iLMhNLXgZkUzWBiPpJ5/89UJ/Atuxjn5YXR+XaZWo2hc2/8mGfTez+NDAgOcpLA==", + "license": "MIT", + "dependencies": { + "@tokenizer/inflate": "^0.4.1", + "strtok3": "^10.3.5", + "token-types": "^6.1.2", + "uint8array-extras": "^1.5.0" + }, + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/sindresorhus/file-type?sponsor=1" + } + }, + "node_modules/finalhandler": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.1.tgz", + "integrity": "sha512-S8KoZgRZN+a5rNwqTxlZZePjT/4cnm0ROV70LedRHZ0p8u9fRID0hJUZQpkKLzro8LfmC8sx23bY6tVNxv8pQA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/find-my-way": { + "version": "9.9.0", + "resolved": "https://registry.npmjs.org/find-my-way/-/find-my-way-9.9.0.tgz", + "integrity": "sha512-sJsgZ1sQH2UDuowPuMKg8az7Qc8F0jnj+SKkFWU/+T0xcFlgV5skgXOGUqmQzOdmW6ALA7AhJINWx3qFBkbLHA==", + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "fast-querystring": "^1.0.0", + "safe-regex2": "^5.0.0" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/form-data": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.6.tgz", + "integrity": "sha512-vKatAh4SlVfgbv+YtmhiRjhEMJsYpsG1Y2rMQtR+SVSbytsSD1YGzDIcrAJmdFec88u/+VoGmxnl+80gL1tRCQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.4", + "mime-types": "^2.1.35" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/form-data/node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/form-data/node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dev": true, + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/formidable": { + "version": "3.5.4", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.4.tgz", + "integrity": "sha512-YikH+7CUTOtP44ZTnUhR7Ic2UASBPOqmaRkRKxRbywPTe5VxF7RRCck4af9wutiZ/QKM5nME9Bie2fFaPz5Gug==", + "dev": true, + "license": "MIT", + "dependencies": { + "@paralleldrive/cuid2": "^2.2.2", + "dezalgo": "^1.0.4", + "once": "^1.4.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "funding": { + "url": "https://ko-fi.com/tunnckoCore/commissions" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz", + "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-east-asian-width": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.7.0.tgz", + "integrity": "sha512-XjH1AECxf0giL2V1aU8vKyRR2ppRUb5c0EvT7zuJTokQ74bNo52zOtghqdWIqrhUD79fo3x0WfKZdOqxF6LG1Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/globrex": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/globrex/-/globrex-0.1.2.tgz", + "integrity": "sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==", + "dev": true, + "license": "MIT" + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "dev": true, + "license": "MIT", + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz", + "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/html-escaper": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", + "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", + "dev": true, + "license": "MIT" + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.3.tgz", + "integrity": "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/inquirer": { + "version": "8.2.6", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-8.2.6.tgz", + "integrity": "sha512-M1WuAmb7pn9zdFRtQYk26ZBoY043Sse0wVDdk4Bppr+JOXyQYybdtvK+l9wUibhtjdjvtoiNy8tk+EgsYIUqKg==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-escapes": "^4.2.1", + "chalk": "^4.1.1", + "cli-cursor": "^3.1.0", + "cli-width": "^3.0.0", + "external-editor": "^3.0.3", + "figures": "^3.0.0", + "lodash": "^4.17.21", + "mute-stream": "0.0.8", + "ora": "^5.4.1", + "run-async": "^2.4.0", + "rxjs": "^7.5.5", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0", + "through": "^2.3.6", + "wrap-ansi": "^6.0.1" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/inquirer/node_modules/cli-spinners": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.9.2.tgz", + "integrity": "sha512-ywqV+5MmyL4E7ybXgKys4DugZbX0FC6LnwrhjuykIjnK9k8OQacQ7axGKnjDXWNhns0xot3bZI5h55H8yo9cJg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/inquirer/node_modules/cli-width": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-3.0.0.tgz", + "integrity": "sha512-FxqpkPPwu1HjuN93Omfm4h8uIanXofW0RxVEW3k5RKx+mJJYSthzNhp32Kzxxy3YAEZ/Dc/EWN1vZRY0+kOhbw==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">= 10" + } + }, + "node_modules/inquirer/node_modules/is-interactive": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-1.0.0.tgz", + "integrity": "sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/inquirer/node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/inquirer/node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/inquirer/node_modules/mute-stream": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", + "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==", + "dev": true, + "license": "ISC" + }, + "node_modules/inquirer/node_modules/ora": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/ora/-/ora-5.4.1.tgz", + "integrity": "sha512-5b6Y85tPxZZ7QytO+BQzysW31HJku27cRIlkbAXaNx+BdcVi+LlRFmVXzeF6a7JCwJpyw5c4b+YSVImQIrBpuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "bl": "^4.1.0", + "chalk": "^4.1.0", + "cli-cursor": "^3.1.0", + "cli-spinners": "^2.5.0", + "is-interactive": "^1.0.0", + "is-unicode-supported": "^0.1.0", + "log-symbols": "^4.1.0", + "strip-ansi": "^6.0.0", + "wcwidth": "^1.0.1" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/is-interactive": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-2.0.0.tgz", + "integrity": "sha512-qP1vozQRI+BMOPcjFzrjXuQvdak2pHNUMZoeG2eRbiSqyvbEf/wQtEOTOX1guk6E3t36RkaqiSt8A/6YElNxLQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "license": "MIT" + }, + "node_modules/is-unicode-supported": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-2.1.0.tgz", + "integrity": "sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "license": "ISC" + }, + "node_modules/istanbul-lib-coverage": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", + "integrity": "sha512-O8dpsF+r0WV/8MNRKfnmrtCWhuKjxrq2w+jpzBL5UZKTi2LeVWnWOmWRxFlesJONmc+wLAGvKQZEOanko0LFTg==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=8" + } + }, + "node_modules/istanbul-lib-report": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/istanbul-lib-report/-/istanbul-lib-report-3.0.1.tgz", + "integrity": "sha512-GCfE1mtsHGOELCU8e/Z7YWzpmybrx/+dSTfLrvY8qRmaY6zXTKWn6WQIjaAFw069icm6GVMNkgu0NzI4iPZUNw==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "istanbul-lib-coverage": "^3.0.0", + "make-dir": "^4.0.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/istanbul-reports": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.2.0.tgz", + "integrity": "sha512-HGYWWS/ehqTV3xN10i23tkPkpH46MLCIMFNCaaKNavAXTF1RkqxawEPtnjnGZ6XKSInBKkiOA5BKS+aZiY3AvA==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "html-escaper": "^2.0.0", + "istanbul-lib-report": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/iterare": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/iterare/-/iterare-1.2.1.tgz", + "integrity": "sha512-RKYVTCjAnRthyJes037NX/IiqeidgN1xc3j1RjFfECFp28A1GVwK9nA+i0rJPaHqSZwygLzRnFlzUuHFoWWy+Q==", + "license": "ISC", + "engines": { + "node": ">=6" + } + }, + "node_modules/js-tokens": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-10.0.0.tgz", + "integrity": "sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/json-schema-ref-resolver": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/json-schema-ref-resolver/-/json-schema-ref-resolver-3.0.0.tgz", + "integrity": "sha512-hOrZIVL5jyYFjzk7+y7n5JDzGlU8rfWDuYyHwGa2WA8/pcmMHezp2xsVwxrebD/Q9t8Nc5DboieySDpCp4WG4A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "dequal": "^2.0.3" + } + }, + "node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "license": "MIT" + }, + "node_modules/json5": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", + "dev": true, + "license": "MIT", + "bin": { + "json5": "lib/cli.js" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/jsonc-parser": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.3.1.tgz", + "integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/libphonenumber-js": { + "version": "1.13.13", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.13.13.tgz", + "integrity": "sha512-hV6SmWnyQ8U1QXhhqbqYBHIStNnscG7A5XbGodgdHhf/8N5PwhYpJA5U/yHDjYwPYTd8uNGXqq416XctuvK2PA==", + "license": "MIT" + }, + "node_modules/light-my-request": { + "version": "6.6.0", + "resolved": "https://registry.npmjs.org/light-my-request/-/light-my-request-6.6.0.tgz", + "integrity": "sha512-CHYbu8RtboSIoVsHZ6Ye4cj4Aw/yg2oAFimlF7mNvfDV192LR7nDiKtSIfCuLT7KokPSTn/9kfVLm5OGN0A28A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause", + "dependencies": { + "cookie": "^1.0.1", + "process-warning": "^4.0.0", + "set-cookie-parser": "^2.6.0" + } + }, + "node_modules/light-my-request/node_modules/cookie": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz", + "integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/light-my-request/node_modules/process-warning": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-4.0.1.tgz", + "integrity": "sha512-3c2LzQ3rY9d0hc1emcsHhfT9Jwz0cChib/QN89oME2R451w5fy3f0afAhERFZAwrbDU43wk12d0ORBpDVME50Q==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT" + }, + "node_modules/lightningcss": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz", + "integrity": "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==", + "dev": true, + "license": "MPL-2.0", + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.33.0", + "lightningcss-darwin-arm64": "1.33.0", + "lightningcss-darwin-x64": "1.33.0", + "lightningcss-freebsd-x64": "1.33.0", + "lightningcss-linux-arm-gnueabihf": "1.33.0", + "lightningcss-linux-arm64-gnu": "1.33.0", + "lightningcss-linux-arm64-musl": "1.33.0", + "lightningcss-linux-x64-gnu": "1.33.0", + "lightningcss-linux-x64-musl": "1.33.0", + "lightningcss-win32-arm64-msvc": "1.33.0", + "lightningcss-win32-x64-msvc": "1.33.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.33.0.tgz", + "integrity": "sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.33.0.tgz", + "integrity": "sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.33.0.tgz", + "integrity": "sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.33.0.tgz", + "integrity": "sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.33.0.tgz", + "integrity": "sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.33.0.tgz", + "integrity": "sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.33.0.tgz", + "integrity": "sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.33.0.tgz", + "integrity": "sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.33.0.tgz", + "integrity": "sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.33.0.tgz", + "integrity": "sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.33.0.tgz", + "integrity": "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/load-esm": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/load-esm/-/load-esm-1.0.3.tgz", + "integrity": "sha512-v5xlu8eHD1+6r8EHTg6hfmO97LN8ugKtiXcy5e6oN72iD2r6u0RPfLl6fxM+7Wnh2ZRq15o0russMst44WauPA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + }, + { + "type": "buymeacoffee", + "url": "https://buymeacoffee.com/borewit" + } + ], + "license": "MIT", + "engines": { + "node": ">=13.2.0" + } + }, + "node_modules/lodash": { + "version": "4.18.1", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz", + "integrity": "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/log-symbols": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-7.0.1.tgz", + "integrity": "sha512-ja1E3yCr9i/0hmBVaM0bfwDjnGy8I/s6PP4DFp+yP+a+mrHO4Rm7DtmnqROTUkHIkqffC84YY7AeqX6oFk0WFg==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-unicode-supported": "^2.0.0", + "yoctocolors": "^2.1.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/magic-string": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.0.0.tgz", + "integrity": "sha512-CGvjzMN08iv6w1mm4/x3Gh1hLb4VnyRUA15FFpl6CsCIGGoe36k7kY5KNz9QDbSBN5I/fWHM6ZlIkUTa5xdUEA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, + "node_modules/magicast": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/magicast/-/magicast-0.5.5.tgz", + "integrity": "sha512-UicdXN8zQ3JHlxVq+28afMXPr1z7WNY6+7EJnzTdQWkTAlMLF5fNCCKxJHBQwGaNGR11581EiQmQzx73+MvszA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/parser": "^7.29.7", + "@babel/types": "^7.29.7", + "source-map-js": "^1.2.1" + } + }, + "node_modules/make-dir": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-4.0.0.tgz", + "integrity": "sha512-hXdUTZYIVOt1Ex//jAQi+wTZZpUpwBj/0QsOzqegb3rGMMeJiSEu5xLHnYfBrRV4RH2+OCSOO95Is/7x1WJ4bw==", + "dev": true, + "license": "MIT", + "dependencies": { + "semver": "^7.5.3" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.1.tgz", + "integrity": "sha512-yz3xRaG20c6/BOzvYoDaGtPmGscs7YivItZEEqe6GbwNfHuxu9YNmvnEkMzKldAGY4/80pRcQRZSEnhquk9XuQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/merge-descriptors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", + "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.6.0.tgz", + "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==", + "dev": true, + "license": "MIT", + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mime-db": { + "version": "1.54.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", + "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz", + "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", + "license": "MIT", + "dependencies": { + "mime-db": "^1.54.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/mimic-function": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/mimic-function/-/mimic-function-5.0.1.tgz", + "integrity": "sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/minimatch": { + "version": "10.2.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.6.tgz", + "integrity": "sha512-vpLQEs+VLCr1nU0BXS07maYoFwlDAH0gngQuuttxIwutDFEMHq2blX+8vpgxDdK3J1PwjCJiep77OitTZ4Ll1A==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "brace-expansion": "^5.0.8" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/multer": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/multer/-/multer-2.4.0.tgz", + "integrity": "sha512-7dqa0ZcFfzbefdTuIkzOSMvZWC0J7FLqBOjJUZvDCXShIURWKxAyTT1wHhnE5q19c7jOJf43IYYKjBmZVZmvhg==", + "license": "MIT", + "dependencies": { + "append-field": "^1.0.0", + "busboy": "^1.6.0", + "type-is": "^1.6.18" + }, + "engines": { + "node": ">= 10.16.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/multer/node_modules/media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/multer/node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/multer/node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/multer/node_modules/type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "license": "MIT", + "dependencies": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mute-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-3.0.0.tgz", + "integrity": "sha512-dkEJPVvun4FryqBmZ5KhDo0K9iDXAwn08tMLDinNdRBNPcYEDiWYysLcc6k3mjTMlbP9KyylvRpd4wFtwrT9rw==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^20.17.0 || >=22.9.0" + } + }, + "node_modules/nanoid": { + "version": "3.3.19", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.19.tgz", + "integrity": "sha512-Y2tUNy4ouw6tq5oDSKeQYGOyhkUBhNOcGV/02KC+6kd9eDGqdZd++mjMiIDilrBYvjEnCYvVtsuHCuP+okSfug==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/negotiator": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.1.0.tgz", + "integrity": "sha512-NMPBRMJgiQHjbd8phG3Vebdx4kZ1H121rbl5IkMqeOsahptB9BKo/d7oJ3zTXqTgagn2bWlNSXkh0QUGM31RYg==", + "license": "MIT", + "dependencies": { + "content-type": "^2.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/negotiator/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/nestjs-spelunker": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/nestjs-spelunker/-/nestjs-spelunker-1.3.2.tgz", + "integrity": "sha512-tVQZvPvlYNRW9PFkl2dj/hCEzUjC/NbJdayhnr/0dBuebFu4fqJNICPQF1BWJgK8GxJARe1kvDAw/GoyvINvOA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ogma/styler": "^1.0.0" + }, + "peerDependencies": { + "@nestjs/common": ">6.11.0", + "@nestjs/core": ">6.11.0" + } + }, + "node_modules/node-addon-api": { + "version": "8.9.2", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.9.2.tgz", + "integrity": "sha512-VijLXbi3UACN69I0JVXJsX4tjACjNoQDgv2gTF6sx2wWEi8tkSg2eX8p5gSIFi8z2+DL3oHmY6OyKce38SDolg==", + "license": "MIT", + "engines": { + "node": "^18 || ^20 || >= 21" + } + }, + "node_modules/node-emoji": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-2.2.0.tgz", + "integrity": "sha512-Z3lTE9pLaJF47NyMhd4ww1yFTAP8YhYI8SleJiHzM46Fgpm5cnNzSl9XfzFNqbaz+VlJrIj3fXQ4DeN1Rjm6cw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@sindresorhus/is": "^4.6.0", + "char-regex": "^1.0.2", + "emojilib": "^2.4.0", + "skin-tone": "^2.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/node-gyp-build": { + "version": "4.8.4", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz", + "integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==", + "license": "MIT", + "bin": { + "node-gyp-build": "bin.js", + "node-gyp-build-optional": "optional.js", + "node-gyp-build-test": "build-test.js" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/obug": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/obug/-/obug-2.2.1.tgz", + "integrity": "sha512-XrsrhT5sybtKI6wakr2SPOlGZWWYbUXZ7a0jT8/QOeAPau+1X/bSegNe5YR75oJmEZQbKningirmGOEJCIk61Q==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/on-exit-leak-free": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz", + "integrity": "sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==", + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/onetime": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", + "integrity": "sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg==", + "dev": true, + "license": "MIT", + "dependencies": { + "mimic-fn": "^2.1.0" + }, + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora": { + "version": "9.4.1", + "resolved": "https://registry.npmjs.org/ora/-/ora-9.4.1.tgz", + "integrity": "sha512-6VlU9MLXbjVQD04AZCMX28hVtA5bUoadvUqO76MUCVA0ilwJbMiHsITRPfyVm6p/BC0Av/BXMujx39WCe1LEqw==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "^5.6.2", + "cli-cursor": "^5.0.0", + "cli-spinners": "^3.2.0", + "is-interactive": "^2.0.0", + "is-unicode-supported": "^2.1.0", + "log-symbols": "^7.0.1", + "stdin-discarder": "^0.3.2", + "string-width": "^8.1.0" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora/node_modules/ansi-regex": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.3.0.tgz", + "integrity": "sha512-WpDfL7NO6j7tH88IDBNVdUJxDh9nmCteAVW9dsep846XdwF4naCBK+/tGLX3KJgcpgMRXCFlTM2hKGoK9FsdrQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-regex?sponsor=1" + } + }, + "node_modules/ora/node_modules/chalk": { + "version": "5.6.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", + "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/ora/node_modules/cli-cursor": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-5.0.0.tgz", + "integrity": "sha512-aCj4O5wKyszjMmDT4tZj93kxyydN/K5zPWSCe6/0AV/AA1pqe5ZBIw0a2ZfPQV7lL5/yb5HsUreJ6UFAF1tEQw==", + "dev": true, + "license": "MIT", + "dependencies": { + "restore-cursor": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora/node_modules/onetime": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-7.0.0.tgz", + "integrity": "sha512-VXJjc87FScF88uafS3JllDgvAm+c/Slfz06lorj2uAY34rlUu0Nt+v8wreiImcrgAjjIHp1rXpTDlLOGw29WwQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "mimic-function": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora/node_modules/restore-cursor": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-5.1.0.tgz", + "integrity": "sha512-oMA2dcrw6u0YfxJQXm342bFKX/E4sG9rbTzO9ptUcR/e8A33cHuvStiYOwH7fszkZlZ1z/ta9AAoPk2F4qIOHA==", + "dev": true, + "license": "MIT", + "dependencies": { + "onetime": "^7.0.0", + "signal-exit": "^4.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora/node_modules/string-width": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-8.2.2.tgz", + "integrity": "sha512-GaPUh5gfdrYzqeVNZvUfT23vYYxXzKYidUcnMtJg/3rxRV63EFZy3k6xfKlmfeJD0176lnUV/Usr3XcwSvFzpg==", + "dev": true, + "license": "MIT", + "dependencies": { + "get-east-asian-width": "^1.5.0", + "strip-ansi": "^7.1.2" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora/node_modules/strip-ansi": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.2.0.tgz", + "integrity": "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^6.2.2" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/strip-ansi?sponsor=1" + } + }, + "node_modules/os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/oxlint": { + "version": "1.83.0", + "resolved": "https://registry.npmjs.org/oxlint/-/oxlint-1.83.0.tgz", + "integrity": "sha512-cyDzSzaw3uzP0TeCeq3lLRPPoaUxkbB4ZOXj+kn+5r+BX9V+4bNVGk9lxer+WrgcpebH4JxLlJ3KQjveVztOLQ==", + "dev": true, + "license": "MIT", + "bin": { + "oxlint": "bin/oxlint" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/sponsors/oxc-project" + }, + "optionalDependencies": { + "@oxlint/binding-android-arm-eabi": "1.83.0", + "@oxlint/binding-android-arm64": "1.83.0", + "@oxlint/binding-darwin-arm64": "1.83.0", + "@oxlint/binding-darwin-x64": "1.83.0", + "@oxlint/binding-freebsd-x64": "1.83.0", + "@oxlint/binding-linux-arm-gnueabihf": "1.83.0", + "@oxlint/binding-linux-arm-musleabihf": "1.83.0", + "@oxlint/binding-linux-arm64-gnu": "1.83.0", + "@oxlint/binding-linux-arm64-musl": "1.83.0", + "@oxlint/binding-linux-ppc64-gnu": "1.83.0", + "@oxlint/binding-linux-riscv64-gnu": "1.83.0", + "@oxlint/binding-linux-riscv64-musl": "1.83.0", + "@oxlint/binding-linux-s390x-gnu": "1.83.0", + "@oxlint/binding-linux-x64-gnu": "1.83.0", + "@oxlint/binding-linux-x64-musl": "1.83.0", + "@oxlint/binding-openharmony-arm64": "1.83.0", + "@oxlint/binding-win32-arm64-msvc": "1.83.0", + "@oxlint/binding-win32-ia32-msvc": "1.83.0", + "@oxlint/binding-win32-x64-msvc": "1.83.0" + }, + "peerDependencies": { + "oxlint-tsgolint": ">=7.0.2001", + "vite-plus": "*" + }, + "peerDependenciesMeta": { + "oxlint-tsgolint": { + "optional": true + }, + "vite-plus": { + "optional": true + } + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/path-to-regexp": { + "version": "8.4.2", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.4.2.tgz", + "integrity": "sha512-qRcuIdP69NPm4qbACK+aDogI5CBDMi1jKe0ry5rSQJz8JVLsC7jV8XpiJjGRLLol3N+R5ihGYcrPLTno6pAdBA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, + "node_modules/pg": { + "version": "8.23.1", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.23.1.tgz", + "integrity": "sha512-aL96AHANtWjPLDOLqnhx+ngp9+UK7ETEU8VJrDCGvsSSi/mGLcWYsS6Herg7lmaBJe4uwrfqsa7gTEFaSizDoQ==", + "license": "MIT", + "dependencies": { + "pg-connection-string": "^2.14.1", + "pg-pool": "^3.14.0", + "pg-protocol": "^1.16.1", + "pg-types": "2.2.0", + "pgpass": "1.0.5" + }, + "engines": { + "node": ">= 16.0.0" + }, + "optionalDependencies": { + "pg-cloudflare": "^1.4.1" + }, + "peerDependencies": { + "pg-native": ">=3.0.1" + }, + "peerDependenciesMeta": { + "pg-native": { + "optional": true + } + } + }, + "node_modules/pg-cloudflare": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.4.1.tgz", + "integrity": "sha512-6PQbsFWZcp9EmJEwy5cGQ2La+AMWpP46lgbb8X+U/XsHIUweYDNCpeuKck5RxL2MdVFi7krbbEi5nX4Zh7JhrQ==", + "license": "MIT", + "optional": true + }, + "node_modules/pg-connection-string": { + "version": "2.14.1", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.14.1.tgz", + "integrity": "sha512-qR3kGNPBLpCNtz0evbKA0Y/MRFXwSSdT+pTJvYp/bXTcReZbvX1kzF0IyTc1QnxqF7AZbOeBhNL8R5mYQZV/MA==", + "license": "MIT" + }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "license": "ISC", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-pool": { + "version": "3.14.0", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.14.0.tgz", + "integrity": "sha512-gKtPkFdQPU3DksooVLi9LsjZxrsBUZIpa+7aVx+LV5pNh0KzP4Zleud2po+ConrxbuXGBJ6Hfer6hdgpIBpBaw==", + "license": "MIT", + "peerDependencies": { + "pg": ">=8.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.16.1", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.16.1.tgz", + "integrity": "sha512-p9VOFMiHB/ZbJATetbg+99PxssTVSQRnyuPSQ67mN1+1KBOjZaZ83ZQzltnxPhJwSsC3nwVjJ10DVJlerbFzLg==", + "license": "MIT" + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "license": "MIT", + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/pgpass": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", + "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", + "license": "MIT", + "dependencies": { + "split2": "^4.1.0" + } + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.5.tgz", + "integrity": "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/pino": { + "version": "10.3.1", + "resolved": "https://registry.npmjs.org/pino/-/pino-10.3.1.tgz", + "integrity": "sha512-r34yH/GlQpKZbU1BvFFqOjhISRo1MNx1tWYsYvmj6KIRHSPMT2+yHOEb1SG6NMvRoHRF0a07kCOox/9yakl1vg==", + "license": "MIT", + "dependencies": { + "@pinojs/redact": "^0.4.0", + "atomic-sleep": "^1.0.0", + "on-exit-leak-free": "^2.1.0", + "pino-abstract-transport": "^3.0.0", + "pino-std-serializers": "^7.0.0", + "process-warning": "^5.0.0", + "quick-format-unescaped": "^4.0.3", + "real-require": "^0.2.0", + "safe-stable-stringify": "^2.3.1", + "sonic-boom": "^4.0.1", + "thread-stream": "^4.0.0" + }, + "bin": { + "pino": "bin.js" + } + }, + "node_modules/pino-abstract-transport": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pino-abstract-transport/-/pino-abstract-transport-3.0.0.tgz", + "integrity": "sha512-wlfUczU+n7Hy/Ha5j9a/gZNy7We5+cXp8YL+X+PG8S0KXxw7n/JXA3c46Y0zQznIJ83URJiwy7Lh56WLokNuxg==", + "license": "MIT", + "dependencies": { + "split2": "^4.0.0" + } + }, + "node_modules/pino-std-serializers": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-7.1.0.tgz", + "integrity": "sha512-BndPH67/JxGExRgiX1dX0w1FvZck5Wa4aal9198SrRhZjH3GxKQUKIBnYJTdj2HDN3UQAS06HlfcSbQj2OHmaw==", + "license": "MIT" + }, + "node_modules/pluralize": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", + "integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/postcss": { + "version": "8.5.28", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.28.tgz", + "integrity": "sha512-RRuzqDtt5Y9h3quz5hWhK+TPnsmVs6WwSU6LkJMeY4HstUEDuYTG8UJSdawMRzmzAtV+KEoG8N3Qg2qLy5vM/A==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.18", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.1" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz", + "integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "license": "MIT", + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/prettier": { + "version": "3.9.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.8.tgz", + "integrity": "sha512-WRFq3Wn3WId7LLROfMLdH7xaFr2jR62wU8nLO6rQUOLOxNZUviyJQs1M0iIhLexSFy+L+w0ch66wtoO2jRjG0A==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/process-warning": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-5.1.0.tgz", + "integrity": "sha512-jQSaVHsPgtyw60e1rQ/A+/ArPEj/S8pS/vFnyGa/gYFXrKk/6RuDkoqVDQ5NI5MmS01698ltlAk0NoDBNLujRw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT" + }, + "node_modules/proxy-addr": { + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.8.tgz", + "integrity": "sha512-5nnx0yGyVUcY6t9RnWcARWtwT9F1D8O9rt08htPvnd49W1IgZtmLkhu9WfMzQj1cFxjHIO6connUNVW5k7AVyQ==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/qs": { + "version": "6.16.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.16.0.tgz", + "integrity": "sha512-h6fhOIaRrID2CbEY2fqs+7t+UXZo+MLAnU5gRIq85uFtdiUPCdsApMlHhXogKVM4HM2DVbIjGNTTYH2OcmP1vA==", + "license": "BSD-3-Clause", + "dependencies": { + "es-define-property": "^1.0.1", + "side-channel": "^1.1.1" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/quick-format-unescaped": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-4.0.4.tgz", + "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", + "license": "MIT" + }, + "node_modules/range-parser": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.3.0.tgz", + "integrity": "sha512-hek2mFQpPuI4E1BBKrSto+BU3e3x4xuarsbiwr3+lf7p44juvFMV0XFWQAP3xUyqXA4RrXLIoaSUGbSt056ZMw==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/raw-body": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz", + "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.7.0", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dev": true, + "license": "MIT", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/readdirp": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-5.1.1.tgz", + "integrity": "sha512-Kko+Y5XQ6fM+Ce3dq3m9YGxnacYZYl9cA1wZjaF3Vbry2L3i1qVg8+CAgNPsXRArPMUMCaOR7oa9Nqntc43JKA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 20.19.0" + }, + "funding": { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/real-require": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz", + "integrity": "sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==", + "license": "MIT", + "engines": { + "node": ">= 12.13.0" + } + }, + "node_modules/redis": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/redis/-/redis-6.3.0.tgz", + "integrity": "sha512-XFQbPie1lGpKeUZ8ySYY43yLnQy/iIQtHNvIr/1XdaABpb96rUSdm3Mfl+98FEVbv9ZJIzINMno5s0Nh8E5LEw==", + "license": "MIT", + "dependencies": { + "@redis/bloom": "6.3.0", + "@redis/client": "6.3.0", + "@redis/json": "6.3.0", + "@redis/search": "6.3.0", + "@redis/time-series": "6.3.0" + }, + "engines": { + "node": ">= 20.0.0" + } + }, + "node_modules/reflect-metadata": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.2.tgz", + "integrity": "sha512-urBwgfrvVP/eAyXx4hluJivBKzuEbSQs9rKWCrCkbSxNv8mxPcUZKeuoF3Uy4mJl3Lwprp6yy5/39VWigZ4K6Q==", + "license": "Apache-2.0" + }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/restore-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", + "integrity": "sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==", + "dev": true, + "license": "MIT", + "dependencies": { + "onetime": "^5.1.0", + "signal-exit": "^3.0.2" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/restore-cursor/node_modules/signal-exit": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", + "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", + "dev": true, + "license": "ISC" + }, + "node_modules/ret": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/ret/-/ret-0.5.0.tgz", + "integrity": "sha512-I1XxrZSQ+oErkRR4jYbAyEEu2I0avBvvMM5JN+6EBprOGRCs63ENqZ3vjavq8fBw2+62G5LF5XelKwuJpcvcxw==", + "license": "MIT", + "engines": { + "node": ">=10" + } + }, + "node_modules/reusify": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.1.0.tgz", + "integrity": "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw==", + "license": "MIT", + "engines": { + "iojs": ">=1.0.0", + "node": ">=0.10.0" + } + }, + "node_modules/rfdc": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz", + "integrity": "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA==", + "license": "MIT" + }, + "node_modules/rolldown": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.9.tgz", + "integrity": "sha512-hx/Pv0N1haXRb11qkfnK5MXB/iqr7i0yjWQqmO9uHqZpBgQSqzc8UsSnEpalsh+j1I8qQ2CkXAkJC8Br3dKSlg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@oxc-project/types": "=0.150.0", + "@rolldown/pluginutils": "^1.0.0" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm-eabi": "1.2.9", + "@rolldown/binding-android-arm64": "1.2.9", + "@rolldown/binding-darwin-arm64": "1.2.9", + "@rolldown/binding-darwin-x64": "1.2.9", + "@rolldown/binding-freebsd-x64": "1.2.9", + "@rolldown/binding-linux-arm-gnueabihf": "1.2.9", + "@rolldown/binding-linux-arm64-gnu": "1.2.9", + "@rolldown/binding-linux-arm64-musl": "1.2.9", + "@rolldown/binding-linux-ppc64-gnu": "1.2.9", + "@rolldown/binding-linux-s390x-gnu": "1.2.9", + "@rolldown/binding-linux-x64-gnu": "1.2.9", + "@rolldown/binding-linux-x64-musl": "1.2.9", + "@rolldown/binding-openharmony-arm64": "1.2.9", + "@rolldown/binding-win32-arm64-msvc": "1.2.9", + "@rolldown/binding-win32-x64-msvc": "1.2.9" + } + }, + "node_modules/router": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz", + "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "depd": "^2.0.0", + "is-promise": "^4.0.0", + "parseurl": "^1.3.3", + "path-to-regexp": "^8.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/run-async": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/run-async/-/run-async-2.4.1.tgz", + "integrity": "sha512-tvVnVv01b8c1RrA6Ep7JkStj85Guv/YrMcwqYQnwjsAS2cTmmPGBBjAjpCW7RrSodNSoE2/qg9O4bceNvUuDgQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/rxjs": { + "version": "7.8.2", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.2.tgz", + "integrity": "sha512-dhKf903U/PQZY6boNNtAGdWbG85WAbjT/1xYoZIC7FAY0yWapOBQVsVrDl58W86//e1VpMNBtRV4MaXfdMySFA==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.1.0" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/safe-regex2": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/safe-regex2/-/safe-regex2-5.1.1.tgz", + "integrity": "sha512-mOSBvHGDZMuIEZMdOz/aCEYDCv0E7nfcNsIhUF+/P+xC7Hyf3FkvymqgPbg9D1EdSGu+uKbJgy09K/RKKc7kJA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "MIT", + "dependencies": { + "ret": "~0.5.0" + }, + "bin": { + "safe-regex2": "bin/safe-regex2.js" + } + }, + "node_modules/safe-stable-stringify": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.5.0.tgz", + "integrity": "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA==", + "license": "MIT", + "engines": { + "node": ">=10" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/secure-json-parse": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-4.1.0.tgz", + "integrity": "sha512-l4KnYfEyqYJxDwlNVyRfO2E4NTHfMKAWdUuA8J0yve2Dz/E/PdBepY03RvyJpssIpRFwJoCD55wA+mEDs6ByWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/send": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/send/-/send-1.2.1.tgz", + "integrity": "sha512-1gnZf7DFcoIcajTjTwjwuDjzuz4PPcY2StKPlsGAQ1+YH20IRVrBaXSWmdjowTJ6u8Rc01PoYOGHXfP1mYcZNQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "fresh": "^2.0.0", + "http-errors": "^2.0.1", + "mime-types": "^3.0.2", + "ms": "^2.1.3", + "on-finished": "^2.4.1", + "range-parser": "^1.2.1", + "statuses": "^2.0.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/serve-static": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz", + "integrity": "sha512-xRXBn0pPqQTVQiC8wyQrKs2MOlX24zQ0POGaj0kultvoOCstBQM5yvOhAVSUwOMjQtTvsPWoNCHfPGwaaQJhTw==", + "license": "MIT", + "dependencies": { + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "parseurl": "^1.3.3", + "send": "^1.2.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/set-cookie-parser": { + "version": "2.7.2", + "resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.2.tgz", + "integrity": "sha512-oeM1lpU/UvhTxw+g3cIfxXHyJRc/uidd3yK1P242gzHds0udQBYzs3y8j4gCCW+ZJ7ad0yctld8RYO+bdurlvw==", + "license": "MIT" + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, + "node_modules/sharp": { + "version": "0.35.5", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.35.5.tgz", + "integrity": "sha512-Ywn4OnzGukp7CDMrp08RQ50YKmuwG47brZgIVPTvBaaAfQlRlygrRqSrxdCiL9M+LlzLBiJ68IR1QqvzHyjC7g==", + "license": "Apache-2.0", + "dependencies": { + "@img/colour": "^1.1.0", + "detect-libc": "^2.1.2", + "semver": "^7.8.5" + }, + "engines": { + "node": ">=20.9.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.35.5", + "@img/sharp-darwin-x64": "0.35.5", + "@img/sharp-freebsd-wasm32": "0.35.5", + "@img/sharp-libvips-darwin-arm64": "1.3.4", + "@img/sharp-libvips-darwin-x64": "1.3.4", + "@img/sharp-libvips-linux-arm": "1.3.4", + "@img/sharp-libvips-linux-arm64": "1.3.4", + "@img/sharp-libvips-linux-ppc64": "1.3.4", + "@img/sharp-libvips-linux-riscv64": "1.3.4", + "@img/sharp-libvips-linux-s390x": "1.3.4", + "@img/sharp-libvips-linux-x64": "1.3.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.3.4", + "@img/sharp-libvips-linuxmusl-x64": "1.3.4", + "@img/sharp-linux-arm": "0.35.5", + "@img/sharp-linux-arm64": "0.35.5", + "@img/sharp-linux-ppc64": "0.35.5", + "@img/sharp-linux-riscv64": "0.35.5", + "@img/sharp-linux-s390x": "0.35.5", + "@img/sharp-linux-x64": "0.35.5", + "@img/sharp-linuxmusl-arm64": "0.35.5", + "@img/sharp-linuxmusl-x64": "0.35.5", + "@img/sharp-webcontainers-wasm32": "0.35.5", + "@img/sharp-win32-arm64": "0.35.5", + "@img/sharp-win32-ia32": "0.35.5", + "@img/sharp-win32-x64": "0.35.5" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "license": "MIT", + "dependencies": { + "shebang-regex": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/side-channel": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.1.tgz", + "integrity": "sha512-6x6dK6zJdpTzF4sQeNYxwtvBzf6Eg4GtlesS94HOvTudUeyK2WXAaIfmDgsyslYrRBeFIlsi54AYsFGUuhmvrQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4", + "side-channel-list": "^1.0.1", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz", + "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/siginfo": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz", + "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==", + "dev": true, + "license": "ISC" + }, + "node_modules/signal-exit": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/skin-tone": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/skin-tone/-/skin-tone-2.0.0.tgz", + "integrity": "sha512-kUMbT1oBJCpgrnKoSr0o6wPtvRWT9W9UKvGLwfJYO2WuahZRHOpEyL1ckyMGgMWh0UdpmaoFqKKD29WTomNEGA==", + "dev": true, + "license": "MIT", + "dependencies": { + "unicode-emoji-modifier-base": "^1.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/sonic-boom": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-4.2.1.tgz", + "integrity": "sha512-w6AxtubXa2wTXAUsZMMWERrsIRAdrK0Sc+FUytWvYAhBJLyuI4llrMIC1DtlNSdI99EI86KZum2MMq3EAZlF9Q==", + "license": "MIT", + "dependencies": { + "atomic-sleep": "^1.0.0" + } + }, + "node_modules/source-map": { + "version": "0.7.6", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.6.tgz", + "integrity": "sha512-i5uvt8C3ikiWeNZSVZNWcfZPItFQOsYTUAOkcUPGd8DqDy1uOUikjt5dG+uRlwyvR108Fb9DOd4GvXfT0N2/uQ==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">= 12" + } + }, + "node_modules/source-map-js": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", + "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "node_modules/source-map-support/node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/split2": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", + "license": "ISC", + "engines": { + "node": ">= 10.x" + } + }, + "node_modules/stackback": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz", + "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/std-env": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.2.0.tgz", + "integrity": "sha512-oCUKSupKTHX53EyjDtuZQ64pjLJ6yYCtpmEw0goYxtjG9KpbRe8KAsl2tBUGU9DyMcJ0RwJ8GqJAFzMXcXW1Rw==", + "dev": true, + "license": "MIT" + }, + "node_modules/stdin-discarder": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/stdin-discarder/-/stdin-discarder-0.3.2.tgz", + "integrity": "sha512-eCPu1qRxPVkl5605OTWF8Wz40b4Mf45NY5LQmVPQ599knfs5QhASUm9GbJ5BDMDOXgrnh0wyEdvzmL//YMlw0A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dev": true, + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dev": true, + "license": "MIT", + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/strtok3": { + "version": "10.3.5", + "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-10.3.5.tgz", + "integrity": "sha512-ki4hZQfh5rX0QDLLkOCj+h+CVNkqmp/CMf8v8kZpkNVK6jGQooMytqzLZYUVYIZcFZ6yDB70EfD8POcFXiF5oA==", + "license": "MIT", + "dependencies": { + "@tokenizer/token": "^0.3.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, + "node_modules/superagent": { + "version": "10.3.0", + "resolved": "https://registry.npmjs.org/superagent/-/superagent-10.3.0.tgz", + "integrity": "sha512-B+4Ik7ROgVKrQsXTV0Jwp2u+PXYLSlqtDAhYnkkD+zn3yg8s/zjA2MeGayPoY/KICrbitwneDHrjSotxKL+0XQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "component-emitter": "^1.3.1", + "cookiejar": "^2.1.4", + "debug": "^4.3.7", + "fast-safe-stringify": "^2.1.1", + "form-data": "^4.0.5", + "formidable": "^3.5.4", + "methods": "^1.1.2", + "mime": "2.6.0", + "qs": "^6.14.1" + }, + "engines": { + "node": ">=14.18.0" + } + }, + "node_modules/supertest": { + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-7.2.2.tgz", + "integrity": "sha512-oK8WG9diS3DlhdUkcFn4tkNIiIbBx9lI2ClF8K+b2/m8Eyv47LSawxUzZQSNKUrVb2KsqeTDCcjAAVPYaSLVTA==", + "dev": true, + "license": "MIT", + "dependencies": { + "cookie-signature": "^1.2.2", + "methods": "^1.1.2", + "superagent": "^10.3.0" + }, + "engines": { + "node": ">=14.18.0" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/thread-stream": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-4.2.0.tgz", + "integrity": "sha512-e2zZ96wSChazBsbENf/Pcm/4swHt2cEKQ92rhUjkL9GCKiTDJIaTBenjE/m9DXi0QBmTMDkFDdOomUy20A1tDQ==", + "license": "MIT", + "dependencies": { + "real-require": "^1.0.0" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/thread-stream/node_modules/real-require": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/real-require/-/real-require-1.0.0.tgz", + "integrity": "sha512-P4nbQYQfePJxRSmY+v/KINxVucm4NF3p3s7pJveMTtom52FR4YGltUQLB8idDXwDDWW+eYrWDFbuzUnjoWHF7g==", + "license": "MIT" + }, + "node_modules/through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinybench": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", + "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinyexec": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.1.tgz", + "integrity": "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", + "integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/tinyrainbow": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.1.1.tgz", + "integrity": "sha512-yau8yJdTt989Mm0Bd/236QnzEiPf2xLLTqUZRUJOo/3CB078LSwzei343DgtJVmfJKJE3TMINY1u42SQsP6mXw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/tmp": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", + "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==", + "dev": true, + "license": "MIT", + "dependencies": { + "os-tmpdir": "~1.0.2" + }, + "engines": { + "node": ">=0.6.0" + } + }, + "node_modules/toad-cache": { + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/toad-cache/-/toad-cache-3.7.4.tgz", + "integrity": "sha512-m1TdR/rvT7kgGJZhspNtXdsdYk0fddFpJJFlG5s+UkPFo6lkLoZ3YLOaovPYjq1R75NP5JfeTlSHaOsE09peCg==", + "license": "MIT", + "engines": { + "node": ">=20" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/token-types": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-6.1.2.tgz", + "integrity": "sha512-dRXchy+C0IgK8WPC6xvCHFRIWYUbqqdEIKPaKo/AcTUNzwLTK6AH7RjdLWsEZcAN/TBdtfUw3PYEgPr5VPr6ww==", + "license": "MIT", + "dependencies": { + "@borewit/text-codec": "^0.2.1", + "@tokenizer/token": "^0.3.0", + "ieee754": "^1.2.1" + }, + "engines": { + "node": ">=14.16" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + } + }, + "node_modules/tsconfig-paths": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-4.2.0.tgz", + "integrity": "sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==", + "dev": true, + "license": "MIT", + "dependencies": { + "json5": "^2.2.2", + "minimist": "^1.2.6", + "strip-bom": "^3.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "license": "0BSD" + }, + "node_modules/tsx": { + "version": "4.23.15", + "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.23.15.tgz", + "integrity": "sha512-Yiex1Ovn8z2xPpOWckIiysV1SSyRMY9BkLF++q0yKiDxCqRhosKfMg3janKkiLBwZ5c/YryloKwGZcrEmtwxKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "~0.28.0" + }, + "bin": { + "tsx": "dist/cli.mjs" + }, + "engines": { + "node": ">=18.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + } + }, + "node_modules/type-fest": { + "version": "0.21.3", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.21.3.tgz", + "integrity": "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==", + "dev": true, + "license": "(MIT OR CC0-1.0)", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/type-is": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz", + "integrity": "sha512-faYHw0anBbc/kWF3zFTEnxSFOAGUX9GFbOBthvDdLsIlEoWOFOtS0zgCiQYwIskL9iGXZL3kAXD8OoZ4GmMATA==", + "license": "MIT", + "dependencies": { + "content-type": "^2.0.0", + "media-typer": "^1.1.0", + "mime-types": "^3.0.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/type-is/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/typescript": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/uid": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/uid/-/uid-2.0.2.tgz", + "integrity": "sha512-u3xV3X7uzvi5b1MncmZo3i2Aw222Zk1keqLA1YkHldREkAhAqi65wuPfe7lHx8H/Wzy+8CE7S7uS3jekIM5s8g==", + "license": "MIT", + "dependencies": { + "@lukeed/csprng": "^1.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/uint8array-extras": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/uint8array-extras/-/uint8array-extras-1.5.0.tgz", + "integrity": "sha512-rvKSBiC5zqCCiDZ9kAOszZcDvdAHwwIKJG33Ykj43OKcWsnmcBRL09YTU4nOeHZ8Y2a7l1MgTd08SBe9A8Qj6A==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/undici": { + "version": "6.20.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.20.1.tgz", + "integrity": "sha512-AjQF1QsmqfJys+LXfGTNum+qw4S88CojRInG/6t31W/1fk6G59s92bnAvGz5Cmur+kQv2SURXEvvudLmbrE8QA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18.17" + } + }, + "node_modules/undici-types": { + "version": "7.18.2", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz", + "integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==", + "dev": true, + "license": "MIT" + }, + "node_modules/unicode-emoji-modifier-base": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unicode-emoji-modifier-base/-/unicode-emoji-modifier-base-1.0.0.tgz", + "integrity": "sha512-yLSH4py7oFH3oG/9K+XWrz1pSi3dfUrWEnInbxMfArOfc1+33BlGPQtLsOYwvdMy11AwUBetYuaRxSPqgkq+8g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "dev": true, + "license": "MIT" + }, + "node_modules/validator": { + "version": "13.15.35", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.15.35.tgz", + "integrity": "sha512-TQ5pAGhd5whStmqWvYF4OjQROlmv9SMFVt37qoCBdqRffuuklWYQlCNnEs2ZaIBD1kZRNnikiZOS1eqgkar0iw==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/vite": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.0.tgz", + "integrity": "sha512-lhZBVvEHefgE+HQZC9O7EBJgCU/nVzFNl7vkS4RE0APtWLP02/8QVIkQtzBxPquh7lq5/78NHipTj7ODQ6XuyQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "lightningcss": "^1.33.0", + "picomatch": "^4.0.7", + "postcss": "^8.5.28", + "rolldown": "~1.2.6", + "tinyglobby": "^0.2.17" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.7.1", + "esbuild": "^0.27.0 || ^0.28.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vite-tsconfig-paths": { + "version": "5.1.4", + "resolved": "https://registry.npmjs.org/vite-tsconfig-paths/-/vite-tsconfig-paths-5.1.4.tgz", + "integrity": "sha512-cYj0LRuLV2c2sMqhqhGpaO3LretdtMn/BVX4cPLanIZuwwrkVl+lK84E/miEXkCHWXuq65rhNN4rXsBcOB3S4w==", + "dev": true, + "license": "MIT", + "dependencies": { + "debug": "^4.1.1", + "globrex": "^0.1.2", + "tsconfck": "^3.0.3" + }, + "peerDependencies": { + "vite": "*" + }, + "peerDependenciesMeta": { + "vite": { + "optional": true + } + } + }, + "node_modules/vite-tsconfig-paths/node_modules/tsconfck": { + "version": "3.1.6", + "resolved": "https://registry.npmjs.org/tsconfck/-/tsconfck-3.1.6.tgz", + "integrity": "sha512-ks6Vjr/jEw0P1gmOVwutM3B7fWxoWBL2KRDb1JfqGVawBmO5UsvmWOQFGHBPl5yxYz4eERr19E6L7NMv+Fej4w==", + "deprecated": "unmaintained", + "dev": true, + "license": "MIT", + "bin": { + "tsconfck": "bin/tsconfck.js" + }, + "engines": { + "node": "^18 || >=20" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/vite/node_modules/picomatch": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz", + "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/vitest": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-4.1.11.tgz", + "integrity": "sha512-fhACrNXUidIbGSBr5FlbuBkO7VWC1ZyLl0DO4CU2DrQoAPxX84Ysxs+HeGQpii5lZWV1Q4gBZTTu49mF+A6Edw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/expect": "4.1.11", + "@vitest/mocker": "4.1.11", + "@vitest/pretty-format": "4.1.11", + "@vitest/runner": "4.1.11", + "@vitest/snapshot": "4.1.11", + "@vitest/spy": "4.1.11", + "@vitest/utils": "4.1.11", + "es-module-lexer": "^2.0.0", + "expect-type": "^1.3.0", + "magic-string": "^0.30.21", + "obug": "^2.1.1", + "pathe": "^2.0.3", + "picomatch": "^4.0.3", + "std-env": "^4.0.0-rc.1", + "tinybench": "^2.9.0", + "tinyexec": "^1.0.2", + "tinyglobby": "^0.2.15", + "tinyrainbow": "^3.1.0", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0", + "why-is-node-running": "^2.3.0" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^20.0.0 || ^22.0.0 || >=24.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^20.0.0 || ^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "4.1.11", + "@vitest/browser-preview": "4.1.11", + "@vitest/browser-webdriverio": "4.1.11", + "@vitest/coverage-istanbul": "4.1.11", + "@vitest/coverage-v8": "4.1.11", + "@vitest/ui": "4.1.11", + "happy-dom": "*", + "jsdom": "*", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/coverage-istanbul": { + "optional": true + }, + "@vitest/coverage-v8": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + }, + "vite": { + "optional": false + } + } + }, + "node_modules/vitest/node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, + "node_modules/wcwidth": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", + "integrity": "sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==", + "dev": true, + "license": "MIT", + "dependencies": { + "defaults": "^1.0.3" + } + }, + "node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "license": "ISC", + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/why-is-node-running": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", + "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "siginfo": "^2.0.0", + "stackback": "0.0.2" + }, + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "license": "MIT", + "engines": { + "node": ">=0.4" + } + }, + "node_modules/yoctocolors": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/yoctocolors/-/yoctocolors-2.2.0.tgz", + "integrity": "sha512-xYqdZFUK/VYazNl/oCDYN+3WloWQwMfZxBoiNt6qNyk+xfOdi598muWE42rNZFp1kNOiqW936q5RhUdnpqElSg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/zod": { + "version": "4.6.5", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.5.tgz", + "integrity": "sha512-v5l/aFXZQeai4awLbOpSoHecE9UiMrnfx75tEXLjNonXVARxQ5mOeipTjROUchszUNCqnE+hqAMujRsRHsut2Q==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + } + } +} diff --git a/app/package.json b/app/package.json new file mode 100644 index 00000000..e9277cd1 --- /dev/null +++ b/app/package.json @@ -0,0 +1,73 @@ +{ + "name": "app", + "version": "0.0.1", + "description": "", + "author": "", + "private": true, + "license": "UNLICENSED", + "type": "module", + "scripts": { + "build": "nest build", + "deploy": "nest deploy", + "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", + "start": "nest start", + "start:dev": "nest start --watch", + "start:debug": "nest start --debug --watch", + "start:prod": "node dist/main", + "lint": "oxlint src/ test/", + "test": "vitest run", + "test:unit": "vitest run src/", + "test:watch": "vitest", + "test:cov": "vitest run --coverage", + "test:debug": "vitest --inspect-brk --no-file-parallelism", + "graph": "npm run build && DATABASE_URL=postgresql://graph-only.invalid/messenger REDIS_URL=redis://graph-only.invalid:6379 tsx scripts/graph-modules.ts", + "test:e2e": "vitest run --config ./vitest.config.e2e.ts", + "test:dependencies": "E2E_DEPENDENCY_FAULTS=1 vitest run --config ./vitest.config.e2e.ts dependency-failures", + "attachments:sweep": "tsx src/attachments/attachments-sweep.cli.ts", + "idempotency:gc": "tsx src/messages/idempotency/idempotency-gc.cli.ts", + "jobs": "node dist/jobs/jobs.cli.js" + }, + "dependencies": { + "@aws-sdk/client-s3": "^3.1156.0", + "@aws-sdk/s3-request-presigner": "^3.1156.0", + "@nestjs/common": "^12.0.1", + "@nestjs/config": "^12.0.0", + "@nestjs/core": "^12.0.1", + "@nestjs/platform-express": "^12.0.1", + "@nestjs/platform-fastify": "^12.0.3", + "@smithy/node-http-handler": "^4.12.1", + "amqplib": "^2.2.1", + "argon2": "^0.45.1", + "class-transformer": "^0.5.1", + "class-validator": "^0.15.1", + "date-fns": "^4.4.0", + "fastify": "^5.12.5", + "pg": "^8.23.1", + "redis": "^6.3.0", + "reflect-metadata": "^0.2.2", + "rxjs": "^7.8.1", + "sharp": "^0.35.5", + "zod": "^4.6.5" + }, + "devDependencies": { + "@nestjs/cli": "^12.0.0", + "@nestjs/mau": "^0.2.6", + "@nestjs/schematics": "^12.0.0", + "@nestjs/testing": "^12.0.1", + "@types/express": "^5.0.0", + "@types/node": "^24.0.0", + "@types/pg": "^8.23.1", + "@types/supertest": "^7.0.0", + "@vitest/coverage-v8": "^4.1.2", + "dotenv": "^18.0.5", + "nestjs-spelunker": "^1.3.2", + "oxlint": "^1.58.0", + "prettier": "^3.4.2", + "source-map-support": "^0.5.21", + "supertest": "^7.2.2", + "tsx": "^4.23.15", + "typescript": "^6.0.3", + "vite-tsconfig-paths": "^5.1.4", + "vitest": "^4.1.2" + } +} diff --git a/app/scripts/graph-modules.ts b/app/scripts/graph-modules.ts new file mode 100644 index 00000000..5e8e6d86 --- /dev/null +++ b/app/scripts/graph-modules.ts @@ -0,0 +1,123 @@ +import { mkdir, writeFile } from 'node:fs/promises'; +import 'reflect-metadata'; +import { SpelunkerModule, type DebuggedTree } from 'nestjs-spelunker'; +import { AppModule } from '../dist/app.module.js'; + +function mermaidId(name: string): string { + return name.replace(/[^a-zA-Z0-9_]/gu, '_'); +} + +function findDependencyModule( + tree: DebuggedTree[], + dependency: string, +): DebuggedTree | undefined { + return tree.find( + (candidate) => + candidate.providers.some((item) => item.name === dependency) || + candidate.controllers.some((item) => item.name === dependency), + ); +} + +function renderMermaid(tree: DebuggedTree[]): string { + const lines = [ + 'flowchart LR', + ' %% Generated by scripts/graph-modules.ts', + ' %% Solid arrows: module imports; dotted arrows: constructor dependencies', + ]; + const moduleNames = tree.map((module) => module.name); + + for (const module of tree) { + const moduleId = mermaidId(module.name); + lines.push(` subgraph ${moduleId}["${module.name}"]`); + for (const provider of module.providers) { + lines.push( + ` ${moduleId}_${mermaidId(provider.name)}["provider: ${provider.name}"]`, + ); + } + for (const controller of module.controllers) { + lines.push( + ` ${moduleId}_${mermaidId(controller.name)}["controller: ${controller.name}"]`, + ); + } + lines.push(' end'); + } + + for (const module of tree) { + for (const imported of module.imports) { + if (moduleNames.includes(imported)) { + lines.push( + ` ${mermaidId(module.name)} -->|imports| ${mermaidId(imported)}`, + ); + } + } + for (const item of [...module.providers, ...module.controllers]) { + for (const dependency of item.dependencies) { + const targetModule = findDependencyModule(tree, dependency); + if (targetModule) { + const source = `${mermaidId(module.name)}_${mermaidId(item.name)}`; + const target = `${mermaidId(targetModule.name)}_${mermaidId(dependency)}`; + lines.push(` ${source} -.->|injects| ${target}`); + } + } + } + } + + return `${lines.join('\n')}\n`; +} + +function renderMarkdown(tree: DebuggedTree[], mermaid: string): string { + const lines = [ + '# NestJS dependency graph', + '', + 'Generated from `AppModule` with `nestjs-spelunker` static module debugging.', + '', + '- Solid arrows: module imports.', + '- Dotted arrows: constructor dependencies.', + '- The generator does not boot PostgreSQL or Redis.', + '', + '```mermaid', + mermaid.trim(), + '```', + '', + '## Module details', + '', + ]; + + for (const module of tree) { + lines.push( + `### ${module.name}`, + '', + `- Imports: ${module.imports.join(', ') || 'none'}`, + ); + lines.push( + `- Exports: ${module.exports.map((item) => `${item.name} (${item.type})`).join(', ') || 'none'}`, + ); + lines.push('', '| Kind | Name | Dependencies |', '| --- | --- | --- |'); + for (const provider of module.providers) { + lines.push( + `| provider | ${provider.name} | ${provider.dependencies.join(', ') || 'none'} |`, + ); + } + for (const controller of module.controllers) { + lines.push( + `| controller | ${controller.name} | ${controller.dependencies.join(', ') || 'none'} |`, + ); + } + lines.push(''); + } + + return lines.join('\n'); +} + +const tree = await SpelunkerModule.debug(AppModule); +const mermaid = renderMermaid(tree); +await mkdir('../docs/architecture', { recursive: true }); +await writeFile( + '../docs/architecture/nest-module-details.json', + JSON.stringify(tree, null, 2), +); +await writeFile('../docs/architecture/nest-module-graph.mmd', mermaid); +await writeFile( + '../docs/architecture/nest-dependency-graph.md', + renderMarkdown(tree, mermaid), +); diff --git a/app/src/app.module.ts b/app/src/app.module.ts new file mode 100644 index 00000000..220c60aa --- /dev/null +++ b/app/src/app.module.ts @@ -0,0 +1,35 @@ +import { Module } from '@nestjs/common'; +import { AppConfigModule } from './config/app-config.module.js'; +import { DatabaseModule } from './database/database.module.js'; +import { RedisModule } from './redis/redis.module.js'; +import { AuthModule } from './auth/auth.module.js'; +import { ChatsModule } from './chats/chats.module.js'; +import { HealthModule } from './health/health.module.js'; +import { AttachmentsModule } from './attachments/attachments.module.js'; +import { MessagesModule } from './messages/messages.module.js'; +import { MetricsModule } from './metrics/metrics.module.js'; +import { UsersModule } from './users/users.module.js'; +import { APP_FILTER } from '@nestjs/core'; +import { ApiExceptionFilter } from './common/errors/api-exception.filter.js'; + +@Module({ + imports: [ + AppConfigModule, + DatabaseModule, + RedisModule, + UsersModule, + AuthModule, + HealthModule, + ChatsModule, + MessagesModule, + AttachmentsModule, + MetricsModule, + ], + providers: [ + { + provide: APP_FILTER, + useClass: ApiExceptionFilter, + }, + ], +}) +export class AppModule {} diff --git a/app/src/attachments/attachment-sweeper.service.ts b/app/src/attachments/attachment-sweeper.service.ts new file mode 100644 index 00000000..ba17ae33 --- /dev/null +++ b/app/src/attachments/attachment-sweeper.service.ts @@ -0,0 +1,80 @@ +import { + Injectable, + Logger, + OnModuleDestroy, + OnModuleInit, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { ObjectStorage } from '../storage/object-storage.js'; +import { AttachmentsRepository } from './attachments.repository.js'; + +const SWEEP_BATCH = 100; + +/** + * Removes unfinished uploads (pending past expiry + grace) and rows whose + * deletion was claimed but never finished. A live upload is never touched: + * its expiry is a hard request deadline and the grace period is far longer. + */ +@Injectable() +export class AttachmentSweeper implements OnModuleInit, OnModuleDestroy { + private readonly logger = new Logger(AttachmentSweeper.name); + private timer: NodeJS.Timeout | undefined; + private running = false; + + constructor( + private readonly config: ConfigService, + private readonly repository: AttachmentsRepository, + private readonly storage: ObjectStorage, + private readonly metrics: MetricsService, + ) {} + + onModuleInit(): void { + const interval = this.config.getOrThrow('ATTACHMENT_SWEEP_INTERVAL_MS'); + if (interval <= 0) return; + this.timer = setInterval(() => { + if (this.running) return; + this.running = true; + this.sweepOnce() + .catch((error: unknown) => { + this.logger.warn( + `Sweep failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + }) + .finally(() => { + this.running = false; + }); + }, interval); + this.timer.unref(); + } + + onModuleDestroy(): void { + if (this.timer) clearInterval(this.timer); + this.timer = undefined; + } + + async sweepOnce(): Promise<{ claimed: number; deleted: number }> { + const claimed = await this.repository.claimExpired( + this.config.getOrThrow('ATTACHMENT_SWEEP_GRACE_SECONDS'), + SWEEP_BATCH, + ); + let deleted = 0; + for (const item of claimed) { + try { + // New uploads live in staging; lab4 rows have no staging key and + // their final object is the only copy. + await this.storage.delete(item.staging_key ?? item.object_key); + await this.repository.deleteClaimed(item.id); + deleted += 1; + this.metrics.increment('attachments.sweep.deleted'); + } catch (error) { + this.metrics.increment('attachments.sweep.error'); + this.logger.warn( + `Sweep of attachment ${item.id} failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + } + return { claimed: claimed.length, deleted }; + } +} diff --git a/app/src/attachments/attachment.ts b/app/src/attachments/attachment.ts new file mode 100644 index 00000000..999c97d0 --- /dev/null +++ b/app/src/attachments/attachment.ts @@ -0,0 +1,109 @@ +export type AttachmentStatus = 'queued' | 'processing' | 'ready' | 'failed'; +export type VariantName = 'thumbnail' | 'crop' | 'optimized'; + +export const VARIANT_ORDER: VariantName[] = ['thumbnail', 'crop', 'optimized']; + +export type Variant = { + name: VariantName; + content_type: 'image/jpeg' | 'image/png' | 'image/webp'; + size_bytes: number; + width: number; + height: number; + sha256: string; +}; + +export type Attachment = { + id: string; + chat_id: string; + owner_id: string; + filename: string; + content_type: 'image/png' | 'image/jpeg'; + size_bytes: number; + sha256: string; + status: AttachmentStatus; + variants: Variant[]; + error_code: string | null; + created_at: string; + updated_at: string; +}; + +export type AttachmentRow = { + id: string; + chat_id: string; + owner_id: string; + filename: string; + content_type: 'image/png' | 'image/jpeg'; + upload_mode: 'proxy' | 'direct'; + status: + | 'pending' + | 'queued' + | 'processing' + | 'ready' + | 'failed' + | 'deleting' + | 'rejected'; + object_key: string; + staging_key: string | null; + size_bytes: string | number | null; + sha256: string | null; + width: number | null; + height: number | null; + error_code: string | null; + ready_pipeline_version: number | null; + expires_at: Date | null; + created_at: Date; + updated_at: Date; +}; + +export type VariantRow = { + attachment_id: string; + pipeline_version: number; + variant: VariantName; + object_key: string; + content_type: 'image/jpeg' | 'image/png' | 'image/webp'; + size_bytes: string | number; + width: number; + height: number; + sha256: string; + job_id: string; + created_at: Date; +}; + +function toVariant(row: VariantRow): Variant { + return { + name: row.variant, + content_type: row.content_type, + size_bytes: Number(row.size_bytes), + width: row.width, + height: row.height, + sha256: row.sha256, + }; +} + +export function toAttachment( + row: AttachmentRow, + variants: VariantRow[] = [], +): Attachment { + const ordered = + row.status === 'ready' + ? [...variants].sort( + (left, right) => + VARIANT_ORDER.indexOf(left.variant) - + VARIANT_ORDER.indexOf(right.variant), + ) + : []; + return { + id: row.id, + chat_id: row.chat_id, + owner_id: row.owner_id, + filename: row.filename, + content_type: row.content_type, + size_bytes: Number(row.size_bytes), + sha256: row.sha256 as string, + status: row.status as AttachmentStatus, + variants: ordered.map(toVariant), + error_code: row.status === 'failed' ? row.error_code : null, + created_at: row.created_at.toISOString(), + updated_at: row.updated_at.toISOString(), + }; +} diff --git a/app/src/attachments/attachments-sweep.cli.ts b/app/src/attachments/attachments-sweep.cli.ts new file mode 100644 index 00000000..91d0c36f --- /dev/null +++ b/app/src/attachments/attachments-sweep.cli.ts @@ -0,0 +1,42 @@ +import { fileURLToPath } from 'node:url'; +import { parseAppConfig } from '../config/app-config.schema.js'; +import { DatabaseClient } from '../database/database.client.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { ObjectStorage } from '../storage/object-storage.js'; +import { AttachmentSweeper } from './attachment-sweeper.service.js'; +import { AttachmentsRepository } from './attachments.repository.js'; + +// Manual operator pass over unfinished uploads (the API also sweeps in the +// background). Never touches rows before expiry + ATTACHMENT_SWEEP_GRACE_SECONDS. +// in the stand: docker compose exec api node dist/attachments/attachments-sweep.cli.js +// on the host: npm run attachments:sweep (needs reachable DATABASE_URL and S3) +async function main(): Promise { + const config = parseAppConfig(process.env as Record); + const configService = { + getOrThrow: (key: keyof typeof config) => config[key], + } as never; + const metrics = new MetricsService(); + const database = new DatabaseClient(configService, metrics); + try { + const sweeper = new AttachmentSweeper( + configService, + new AttachmentsRepository(database), + new ObjectStorage(configService), + metrics, + ); + let total = 0; + for (;;) { + const { claimed, deleted } = await sweeper.sweepOnce(); + total += deleted; + // A pass that claimed rows but could not delete any would loop forever. + if (claimed === 0 || deleted === 0) break; + } + process.stdout.write( + `Swept ${total} attachment(s) older than ${config.ATTACHMENT_SWEEP_GRACE_SECONDS} seconds past expiry\n`, + ); + } finally { + await database.onModuleDestroy(); + } +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) await main(); diff --git a/app/src/attachments/attachments.controller.ts b/app/src/attachments/attachments.controller.ts new file mode 100644 index 00000000..6335d6e2 --- /dev/null +++ b/app/src/attachments/attachments.controller.ts @@ -0,0 +1,109 @@ +import { + Body, + Controller, + Get, + Headers, + HttpCode, + Param, + ParseUUIDPipe, + Post, + Query, + Req, + Res, + StreamableFile, + UseGuards, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { FastifyReply, FastifyRequest } from 'fastify'; +import type { Readable } from 'node:stream'; +import { CurrentUser } from '../common/auth/current-user.decorator.js'; +import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; +import type { AppConfig } from '../config/app-config.schema.js'; +import type { User } from '../users/user.js'; +import { AttachmentsService } from './attachments.service.js'; +import { CreateDirectUploadDto } from './dto/create-direct-upload.dto.js'; +import { DirectUploadsService } from './direct-uploads.service.js'; +import type { UploadRequest } from './upload-request.js'; +import { contentDisposition } from './filename.js'; + +@Controller('api/v1') +@UseGuards(CurrentUserGuard) +export class AttachmentsController { + private readonly maxUploadBytes: number; + + constructor( + private readonly attachments: AttachmentsService, + private readonly directUploads: DirectUploadsService, + config: ConfigService, + ) { + this.maxUploadBytes = config.getOrThrow('MAX_UPLOAD_BYTES'); + } + + @Post('chats/:chatId/attachments') + @HttpCode(202) + upload( + @Param('chatId', new ParseUUIDPipe()) chatId: string, + @CurrentUser() user: User, + @Query('filename') filename: unknown, + @Headers('content-length') contentLength: string | undefined, + @Req() request: FastifyRequest & UploadRequest, + @Res({ passthrough: true }) reply: FastifyReply, + ) { + // A gigantic body is not worth draining: close the connection after the answer. + if (Number(contentLength) > 2 * this.maxUploadBytes) + reply.header('Connection', 'close'); + return this.attachments.uploadProxy( + chatId, + user, + filename, + request.uploadContentType, + contentLength, + request.body as Readable | undefined, + ); + } + + @Post('chats/:chatId/attachments/direct-uploads') + @HttpCode(201) + initDirectUpload( + @Param('chatId', new ParseUUIDPipe()) chatId: string, + @CurrentUser() user: User, + @Body() body: CreateDirectUploadDto, + ) { + return this.directUploads.init(chatId, user, body); + } + + @Post('attachments/:attachmentId/complete') + @HttpCode(200) + completeDirectUpload( + @Param('attachmentId', new ParseUUIDPipe()) attachmentId: string, + @CurrentUser() user: User, + ) { + return this.directUploads.complete(attachmentId, user); + } + + @Get('attachments/:attachmentId') + metadata( + @Param('attachmentId', new ParseUUIDPipe()) attachmentId: string, + @CurrentUser() user: User, + ) { + return this.attachments.getMetadata(attachmentId, user); + } + + @Get('attachments/:attachmentId/content') + async content( + @Param('attachmentId', new ParseUUIDPipe()) attachmentId: string, + @CurrentUser() user: User, + @Res({ passthrough: true }) reply: FastifyReply, + @Query('variant') variant?: string, + ) { + const { row, contentType, sizeBytes, stream } = + await this.attachments.openContent(attachmentId, user, variant); + reply.header('X-Content-Type-Options', 'nosniff'); + reply.header('Cache-Control', 'private, no-store'); + return new StreamableFile(stream, { + type: contentType, + length: sizeBytes, + disposition: contentDisposition(row.filename), + }); + } +} diff --git a/app/src/attachments/attachments.module.ts b/app/src/attachments/attachments.module.ts new file mode 100644 index 00000000..7a8fbdc1 --- /dev/null +++ b/app/src/attachments/attachments.module.ts @@ -0,0 +1,42 @@ +import { Module } from '@nestjs/common'; +import { DatabaseModule } from '../database/database.module.js'; +import { ChatsModule } from '../chats/chats.module.js'; +import { UsersModule } from '../users/users.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { StorageModule } from '../storage/storage.module.js'; +import { JobsModule } from '../jobs/jobs.module.js'; +import { OutboxRelay } from '../jobs/outbox-relay.service.js'; +import { AttachmentSweeper } from './attachment-sweeper.service.js'; +import { AttachmentsController } from './attachments.controller.js'; +import { AttachmentsRepository } from './attachments.repository.js'; +import { ImageDecoder } from './image/image-decoder.js'; +import { UploadSlots } from './upload-slots.js'; +import { DirectUploadsService } from './direct-uploads.service.js'; +import { AttachmentsService } from './attachments.service.js'; + +@Module({ + imports: [ + DatabaseModule, + ChatsModule, + UsersModule, + RedisModule, + MetricsModule, + StorageModule, + JobsModule, + ], + controllers: [AttachmentsController], + providers: [ + AttachmentsRepository, + AttachmentsService, + DirectUploadsService, + ImageDecoder, + UploadSlots, + AttachmentSweeper, + // The relay runs only in the API process; the worker never imports + // AttachmentsModule. + OutboxRelay, + ], + exports: [AttachmentsRepository], +}) +export class AttachmentsModule {} diff --git a/app/src/attachments/attachments.repository.ts b/app/src/attachments/attachments.repository.ts new file mode 100644 index 00000000..dc4b9021 --- /dev/null +++ b/app/src/attachments/attachments.repository.ts @@ -0,0 +1,216 @@ +import { Injectable } from '@nestjs/common'; +import { DatabaseClient } from '../database/database.client.js'; +import type { AttachmentRow } from './attachment.js'; + +export type InsertPending = { + id: string; + chatId: string; + ownerId: string; + filename: string; + contentType: string; + uploadMode: 'proxy' | 'direct'; + objectKey: string; + stagingKey: string | null; + sizeBytes: number | null; + sha256: string | null; + ttlMs: number; +}; + +const VISIBLE_MEMBER = `JOIN chat_members m ON m.chat_id = a.chat_id AND m.user_id = $2`; + +const INSERT_PENDING_SQL = `INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, upload_mode, status, + object_key, staging_key, size_bytes, sha256, expires_at) +VALUES ($1, $2, $3, $4, $5, $6, 'pending', $7, $8, $9, $10, + now() + make_interval(secs => $11::double precision / 1000.0))`; + +@Injectable() +export class AttachmentsRepository { + constructor(private readonly database: DatabaseClient) {} + + private values(input: InsertPending): unknown[] { + return [ + input.id, + input.chatId, + input.ownerId, + input.filename, + input.contentType, + input.uploadMode, + input.objectKey, + input.stagingKey, + input.sizeBytes, + input.sha256, + input.ttlMs, + ]; + } + + async insertPending(input: InsertPending): Promise { + await this.database.query( + 'attachments.insert_pending', + INSERT_PENDING_SQL, + this.values(input), + ); + } + + /** + * Serializes the per-owner pending check with the insert. Two parallel + * `init` calls cannot both pass the limit. + */ + async insertPendingLimited( + input: InsertPending, + maxPending: number, + ): Promise { + return this.database.withTransaction(async (client) => { + await client.query( + `SELECT pg_advisory_xact_lock(hashtext($1)::bigint)`, + [input.ownerId], + ); + const count = await client.query<{ count: string }>( + `SELECT count(*) AS count FROM attachments + WHERE owner_id = $1 AND status IN ('pending', 'rejected') + AND upload_mode = 'direct' AND expires_at > now()`, + [input.ownerId], + ); + if (Number(count.rows[0].count) >= maxPending) return false; + await client.query(INSERT_PENDING_SQL, this.values(input)); + return true; + }); + } + + async deletePending(id: string): Promise { + await this.database.query( + 'attachments.delete_pending', + `DELETE FROM attachments WHERE id = $1 AND status = 'pending'`, + [id], + ); + } + + async rejectPending(id: string): Promise { + const result = await this.database.query( + 'attachments.reject_pending', + `UPDATE attachments SET status = 'rejected', updated_at = now() + WHERE id = $1 AND status = 'pending'`, + [id], + ); + return result.rowCount === 1; + } + + /** + * Takes a pending row out of play before its object is deleted. The object + * is deleted only when this returns true: a row that is already `ready` + * (its commit succeeded even if the reply was lost) keeps its object. + */ + async claimForCompensation(id: string): Promise { + const result = await this.database.query( + 'attachments.claim_for_compensation', + `UPDATE attachments SET status = 'deleting', updated_at = now() + WHERE id = $1 AND status = 'pending'`, + [id], + ); + return result.rowCount === 1; + } + + async findById(id: string): Promise { + const result = await this.database.query( + 'attachments.find_by_id', + 'SELECT * FROM attachments WHERE id = $1', + [id], + ); + return result.rows[0]; + } + + async findVisible( + id: string, + userId: string, + ): Promise { + const result = await this.database.query( + 'attachments.find_visible', + `SELECT a.* FROM attachments a ${VISIBLE_MEMBER} + WHERE a.id = $1 AND a.status IN ('queued','processing','ready','failed')`, + [id, userId], + ); + return result.rows[0]; + } + + async findForComplete( + id: string, + userId: string, + ): Promise { + const result = await this.database.query( + 'attachments.find_for_complete', + `SELECT a.* FROM attachments a ${VISIBLE_MEMBER} + WHERE a.id = $1 AND a.upload_mode = 'direct' AND a.owner_id = $2 + AND a.status IN ('pending','queued','processing','ready','failed')`, + [id, userId], + ); + return result.rows[0]; + } + + async countPendingDirect(ownerId: string): Promise { + const result = await this.database.query<{ count: string }>( + 'attachments.count_pending_direct', + `SELECT count(*) AS count FROM attachments + WHERE owner_id = $1 AND status IN ('pending', 'rejected') + AND upload_mode = 'direct' AND expires_at > now()`, + [ownerId], + ); + return Number(result.rows[0].count); + } + + async linkState( + chatId: string, + ids: string[], + ): Promise<'ok' | 'failed' | 'missing'> { + const result = await this.database.query<{ + linkable: string; + failed: string; + }>( + 'attachments.link_state', + `SELECT count(*) FILTER (WHERE status IN ('queued','processing','ready')) AS linkable, + count(*) FILTER (WHERE status = 'failed') AS failed + FROM attachments + WHERE chat_id = $1 AND id = ANY($2::uuid[])`, + [chatId, ids], + ); + const linkable = Number(result.rows[0]?.linkable ?? 0); + const failed = Number(result.rows[0]?.failed ?? 0); + if (linkable === ids.length) return 'ok'; + if (linkable + failed === ids.length) return 'failed'; + return 'missing'; + } + + async claimExpired( + graceSeconds: number, + limit: number, + ): Promise< + { id: string; object_key: string; staging_key: string | null }[] + > { + const result = await this.database.query<{ + id: string; + object_key: string; + staging_key: string | null; + }>( + 'attachments.claim_expired', + `UPDATE attachments SET status = 'deleting', updated_at = now() + WHERE id IN ( + SELECT id FROM attachments + WHERE (status IN ('pending', 'rejected') + AND expires_at < now() - make_interval(secs => $1::double precision)) + OR (status = 'deleting' AND updated_at < now() - interval '1 minute') + ORDER BY expires_at NULLS FIRST + LIMIT $2 + FOR UPDATE SKIP LOCKED) + RETURNING id, object_key, staging_key`, + [graceSeconds, limit], + ); + return result.rows; + } + + async deleteClaimed(id: string): Promise { + await this.database.query( + 'attachments.delete_claimed', + `DELETE FROM attachments WHERE id = $1 AND status = 'deleting'`, + [id], + ); + } +} diff --git a/app/src/attachments/attachments.service.spec.ts b/app/src/attachments/attachments.service.spec.ts new file mode 100644 index 00000000..2aac0ae3 --- /dev/null +++ b/app/src/attachments/attachments.service.spec.ts @@ -0,0 +1,133 @@ +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { Readable } from 'node:stream'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it, vi } from 'vitest'; +import type { AttachmentRow } from './attachment.js'; +import { AttachmentsService } from './attachments.service.js'; + +const here = dirname(fileURLToPath(import.meta.url)); +const png = readFileSync(join(here, '../../../tests/fixtures/sample.png')); +const lostReply = () => + Object.assign(new Error('Connection terminated unexpectedly'), {}); + +const settings: Record = { + MAX_UPLOAD_BYTES: 10_485_760, + MAX_IMAGE_PIXELS: 40_000_000, + UPLOAD_TIMEOUT_MS: 10_000, + PIPELINE_VERSION: 1, + JOB_MAX_ATTEMPTS: 5, +}; + +function setup( + repository: Record, + jobs: Record = {}, +) { + const storage = { + objectKey: (id: string) => `attachments/${id}/original`, + stagingKey: (id: string) => `staging/${id}/original`, + putStream: vi.fn(async (_key: string, body: Readable) => { + for await (const _chunk of body) { + // drain like S3 does + } + }), + delete: vi.fn().mockResolvedValue(undefined), + }; + const service = new AttachmentsService( + { getOrThrow: (key: string) => settings[key] } as never, + { + insertPending: vi.fn(), + deleteClaimed: vi.fn(), + ...repository, + } as never, + { acceptUpload: vi.fn(), ...jobs } as never, + { kick: vi.fn() } as never, + { point: vi.fn() } as never, + storage as never, + { getForUser: vi.fn() } as never, + { increment: vi.fn(), add: vi.fn() } as never, + { verify: vi.fn() } as never, + { + run: (_user: string, work: () => Promise) => work(), + } as never, + ); + const upload = () => + service.uploadProxy( + 'chat', + { id: 'user' } as never, + 'a.png', + 'image/png', + String(png.length), + Readable.from([png]), + ); + return { storage, upload }; +} + +const readyRow = (id: string): AttachmentRow => ({ + id, + chat_id: 'chat', + owner_id: 'user', + filename: 'a.png', + content_type: 'image/png', + upload_mode: 'proxy', + status: 'ready', + object_key: `attachments/${id}/original`, + staging_key: `staging/${id}/original`, + size_bytes: png.length, + sha256: 'a'.repeat(64), + width: 320, + height: 200, + error_code: null, + ready_pipeline_version: 1, + expires_at: null, + created_at: new Date(), + updated_at: new Date(), +}); + +describe('AttachmentsService compensation', () => { + it('keeps the object and answers 202 when the job commit reply was lost', async () => { + const { storage, upload } = setup( + { + claimForCompensation: vi.fn().mockResolvedValue(false), + findById: vi.fn((id: string) => Promise.resolve(readyRow(id))), + }, + { acceptUpload: vi.fn().mockRejectedValue(lostReply()) }, + ); + const attachment = await upload(); + expect(attachment.status).toBe('ready'); + expect(storage.delete).not.toHaveBeenCalled(); + }); + + it('deletes nothing while PostgreSQL is unreachable', async () => { + const { storage, upload } = setup( + { + claimForCompensation: vi.fn().mockRejectedValue(lostReply()), + findById: vi.fn().mockRejectedValue(lostReply()), + }, + { acceptUpload: vi.fn().mockRejectedValue(lostReply()) }, + ); + await expect(upload()).rejects.toThrow(); + expect(storage.delete).not.toHaveBeenCalled(); + }); + + it('deletes the object only after claiming the pending row', async () => { + const order: string[] = []; + const { storage, upload } = setup( + { + claimForCompensation: vi.fn(async () => { + order.push('claim'); + return true; + }), + deleteClaimed: vi.fn(async () => { + order.push('delete-row'); + }), + }, + { acceptUpload: vi.fn().mockRejectedValue(lostReply()) }, + ); + storage.delete.mockImplementation(async () => { + order.push('delete-object'); + }); + await expect(upload()).rejects.toThrow(); + expect(order).toEqual(['claim', 'delete-object', 'delete-row']); + }); +}); diff --git a/app/src/attachments/attachments.service.ts b/app/src/attachments/attachments.service.ts new file mode 100644 index 00000000..72a9b569 --- /dev/null +++ b/app/src/attachments/attachments.service.ts @@ -0,0 +1,435 @@ +import { + BadRequestException, + ConflictException, + HttpException, + Injectable, + InternalServerErrorException, + Logger, + NotFoundException, + PayloadTooLargeException, + ServiceUnavailableException, + UnsupportedMediaTypeException, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { createHash, randomUUID } from 'node:crypto'; +import { pipeline, Readable, Transform } from 'node:stream'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { ChatsService } from '../chats/chats.service.js'; +import { FaultInjector } from '../jobs/fault-injector.js'; +import { JobsRepository } from '../jobs/jobs.repository.js'; +import { OutboxRelay } from '../jobs/outbox-relay.service.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { ObjectStorage } from '../storage/object-storage.js'; +import type { User } from '../users/user.js'; +import { + toAttachment, + type Attachment, + type AttachmentRow, +} from './attachment.js'; +import { validateFilename } from './filename.js'; +import { + ImageHeader, + ImageInspector, + ImageRejectedError, +} from './image/image-inspector.js'; +import { AttachmentsRepository } from './attachments.repository.js'; +import { ImageDecoder } from './image/image-decoder.js'; +import { UploadSlots } from './upload-slots.js'; + +const MEDIA_TYPES = new Set(['image/png', 'image/jpeg']); + +export function mediaType(header: unknown): string | undefined { + if (typeof header !== 'string') return undefined; + return header.split(';', 1)[0].trim().toLowerCase(); +} + +export function imageRejection(error: ImageRejectedError): HttpException { + return error.reason === 'too_many_pixels' + ? new PayloadTooLargeException(error.message) + : new UnsupportedMediaTypeException(error.message); +} + +class UploadInterruptedError extends Error {} + +@Injectable() +export class AttachmentsService { + private readonly logger = new Logger(AttachmentsService.name); + private readonly maxUploadBytes: number; + private readonly maxPixels: number; + private readonly uploadTimeoutMs: number; + private readonly pipelineVersion: number; + private readonly maxAttempts: number; + + constructor( + config: ConfigService, + private readonly repository: AttachmentsRepository, + private readonly jobs: JobsRepository, + private readonly relay: OutboxRelay, + private readonly faults: FaultInjector, + private readonly storage: ObjectStorage, + private readonly chats: ChatsService, + private readonly metrics: MetricsService, + private readonly decoder: ImageDecoder, + private readonly slots: UploadSlots, + ) { + this.maxUploadBytes = config.getOrThrow('MAX_UPLOAD_BYTES'); + this.maxPixels = config.getOrThrow('MAX_IMAGE_PIXELS'); + this.uploadTimeoutMs = config.getOrThrow('UPLOAD_TIMEOUT_MS'); + this.pipelineVersion = config.getOrThrow('PIPELINE_VERSION'); + this.maxAttempts = config.getOrThrow('JOB_MAX_ATTEMPTS'); + } + + async uploadProxy( + chatId: string, + user: User, + rawFilename: unknown, + contentTypeHeader: unknown, + contentLengthHeader: unknown, + body: Readable | undefined, + ): Promise { + const filename = validateFilename(rawFilename); + const declared = mediaType(contentTypeHeader); + if (declared === undefined || !MEDIA_TYPES.has(declared)) + throw new UnsupportedMediaTypeException( + 'Content-Type must be image/png or image/jpeg', + ); + if ( + typeof contentLengthHeader !== 'string' || + !/^\d{1,15}$/u.test(contentLengthHeader) + ) + throw new BadRequestException('Content-Length is required'); + const contentLength = Number(contentLengthHeader); + if (contentLength === 0 || body === undefined) + throw new UnsupportedMediaTypeException('Image body is empty'); + if (contentLength > this.maxUploadBytes) + throw new PayloadTooLargeException('Image is too large'); + + return this.slots.run(user.id, async () => { + await this.chats.getForUser(chatId, user); + return this.receive( + chatId, + user, + filename, + declared, + contentLength, + body, + ); + }); + } + + private async receive( + chatId: string, + user: User, + filename: string, + declared: string, + contentLength: number, + body: Readable, + ): Promise { + const controller = new AbortController(); + let timedOut = false; + const timer = setTimeout(() => { + timedOut = true; + body.destroy(new Error('upload timeout')); + controller.abort(); + }, this.uploadTimeoutMs); + const iterator = body[Symbol.asyncIterator]() as AsyncIterator; + const next = async (): Promise> => { + try { + return await iterator.next(); + } catch { + throw new UploadInterruptedError('Upload was interrupted'); + } + }; + + const inspector = new ImageInspector(this.maxPixels); + const head: Buffer[] = []; + let id: string | undefined; + let key = ''; + let rejection: unknown; + try { + // Header phase: nothing exists in the database or S3 yet. It is + // bounded by Content-Length (<= MAX_UPLOAD_BYTES): a JPEG may carry + // megabytes of APPn metadata before its frame header. + try { + while (inspector.header === undefined) { + const step = await next(); + if (step.done) { + inspector.finish(); + throw new ImageRejectedError( + 'unsupported_media_type', + 'Image header is missing', + ); + } + head.push(step.value); + inspector.push(step.value); + } + } catch (error) { + throw this.mapUploadError(error, timedOut); + } + const header = inspector.header as ImageHeader; + if (header.contentType !== declared) + throw new UnsupportedMediaTypeException( + 'Image content does not match Content-Type', + ); + + id = randomUUID(); + key = this.storage.stagingKey(id); + await this.repository.insertPending({ + id, + chatId, + ownerId: user.id, + filename, + contentType: declared, + uploadMode: 'proxy', + objectKey: this.storage.objectKey(id), + stagingKey: key, + sizeBytes: null, + sha256: null, + ttlMs: this.uploadTimeoutMs, + }); + + const sha256 = createHash('sha256'); + const decoder = this.decoder; + // Copy for the full decode check; bounded by MAX_UPLOAD_BYTES and + // UPLOAD_MAX_IN_FLIGHT, released as soon as the decode starts. + const copy: Buffer[] = []; + let total = 0; + let allSent = false; + const account = (chunk: Buffer, inspect: boolean): boolean => { + total += chunk.length; + if (total > contentLength) + throw new UploadInterruptedError( + 'Body exceeds Content-Length', + ); + if (inspect) inspector.push(chunk); + sha256.update(chunk); + this.metrics.add('attachments.proxy.bytes_in', chunk.length); + copy.push(chunk); + return total === contentLength; + }; + // Validate before the last bytes reach S3: it cannot commit an object + // whose body is incomplete, so an invalid image never becomes an object. + const finalize = async (): Promise => { + inspector.finish(); + const data = Buffer.concat(copy, total); + copy.length = 0; + await decoder.verify(data, controller.signal); + // From here S3 may receive the whole body and commit the object + // even if this request fails or times out afterwards. + allSent = true; + }; + async function* generate(): AsyncGenerator { + for (const chunk of head) { + if (account(chunk, false)) await finalize(); + yield chunk; + } + for (;;) { + const step = await next(); + if (step.done) break; + if (account(step.value, true)) await finalize(); + yield step.value; + } + if (total < contentLength) + throw new UploadInterruptedError('Upload was interrupted'); + } + const readable = Readable.from( + (async function* () { + try { + yield* generate(); + } catch (error) { + rejection = error; + throw error; + } + })(), + ); + + // A failing body must abort the PUT at once: the SDK does not end a + // request whose Content-Length can no longer be satisfied, it would + // hang until the request timeout. The error itself is reported via + // `rejection`; the listener also keeps a late error from crashing us. + readable.on('error', () => controller.abort()); + let claimed = false; + let stored = false; + try { + await this.storage.putStream( + key, + readable, + contentLength, + declared, + controller.signal, + ); + this.metrics.add( + 'attachments.proxy.bytes_to_s3', + contentLength, + ); + stored = true; + const row = await this.jobs.acceptUpload( + id, + { + sizeBytes: total, + sha256: sha256.digest('hex'), + width: header.width, + height: header.height, + }, + this.pipelineVersion, + this.maxAttempts, + ); + if (row === undefined) { + claimed = true; + throw new ServiceUnavailableException(); + } + // Durable: staging object + attachment + job + outbox are committed. + // The fault point deliberately sits between COMMIT and publish; the + // relay recovers the outbox row after a crash. + await this.faults.point('api.after_commit_before_publish'); + this.relay.kick(); + this.metrics.increment('attachments.upload.queued'); + return toAttachment(row); + } catch (error) { + readable.destroy(); + if (!claimed) { + // After the last byte was handed to S3, a PUT aborted on our + // side may still be committed by S3 after our DELETE: keep the + // row so the sweeper deletes the key again later. + const outcome = await this.compensate(id, key, { + keepRow: allSent && !stored, + }); + // acceptUpload failed on our side, but the row is no longer + // pending: the commit may have succeeded with its reply lost. + // If so, the upload is accepted and 202 is the truth. + if (outcome === 'not_pending' && stored) { + const row = await this.repository + .findById(id) + .catch(() => undefined); + if ( + row && + [ + 'queued', + 'processing', + 'ready', + 'failed', + ].includes(row.status) + ) { + this.metrics.increment( + 'attachments.upload.recovered', + ); + return toAttachment(row); + } + } + } + throw this.mapUploadError(rejection ?? error, timedOut); + } + } finally { + clearTimeout(timer); + } + } + + private mapUploadError(error: unknown, timedOut: boolean): unknown { + if (error instanceof ImageRejectedError) return imageRejection(error); + if (error instanceof UploadInterruptedError || timedOut) + return new BadRequestException('Upload was interrupted'); + return error; + } + + /** + * Undoes an upload that did not reach `ready`. The row is claimed first + * (pending -> deleting) and the object is deleted only after a successful + * claim: if PostgreSQL committed `ready` but the reply was lost, the claim + * matches nothing and the object stays. If PostgreSQL is unreachable, + * nothing is deleted; the sweeper finishes the job after expiry + grace + * (a row stuck in `deleting` is re-claimed after a minute). + */ + async compensate( + id: string, + key: string, + options: { keepRow?: boolean } = {}, + ): Promise<'compensated' | 'not_pending' | 'deferred'> { + try { + if (!(await this.repository.claimForCompensation(id))) { + this.metrics.increment('attachments.compensation_skipped'); + return 'not_pending'; + } + await this.storage.delete(key); + // A kept `deleting` row is re-claimed by the sweeper after a minute, + // which deletes the key once more and only then the row. + if (!options.keepRow) await this.repository.deleteClaimed(id); + this.metrics.increment('attachments.compensated'); + return 'compensated'; + } catch (error) { + this.metrics.increment('attachments.compensation_deferred'); + this.logger.warn( + `Compensation of attachment ${id} deferred: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + return 'deferred'; + } + } + + async getMetadata(id: string, user: User): Promise { + const row = await this.repository.findVisible(id, user.id); + if (!row) throw new NotFoundException(); + const variants = + row.status === 'ready' && row.ready_pipeline_version !== null + ? await this.jobs.variantsFor( + row.id, + row.ready_pipeline_version, + ) + : []; + return toAttachment(row, variants); + } + + async openContent( + id: string, + user: User, + variant: string | undefined, + ): Promise<{ + row: AttachmentRow; + contentType: string; + sizeBytes: number; + stream: Readable; + }> { + if ( + variant !== undefined && + !['original', 'thumbnail', 'crop', 'optimized'].includes(variant) + ) + throw new BadRequestException('variant is invalid'); + const row = await this.repository.findVisible(id, user.id); + if (!row) throw new NotFoundException(); + if (row.status !== 'ready') + throw new ConflictException('Attachment is not ready'); + + let key = row.object_key; + let contentType: string = row.content_type; + let sizeBytes = Number(row.size_bytes); + if (variant !== undefined && variant !== 'original') { + if (row.ready_pipeline_version === null) + throw new NotFoundException(); + const variants = await this.jobs.variantsFor( + row.id, + row.ready_pipeline_version, + ); + const match = variants.find((item) => item.variant === variant); + if (!match) throw new NotFoundException(); + key = match.object_key; + contentType = match.content_type; + sizeBytes = Number(match.size_bytes); + } + + const source = await this.storage.getStream(key); + if (source === null) { + this.metrics.increment('attachments.object_missing'); + this.logger.error( + `Object for ready attachment ${row.id} is missing in storage`, + ); + throw new InternalServerErrorException(); + } + const metrics = this.metrics; + const counter = new Transform({ + transform(chunk: Buffer, _encoding, callback) { + metrics.add('attachments.download.bytes_out', chunk.length); + callback(null, chunk); + }, + }); + pipeline(source, counter, () => undefined); + return { row, contentType, sizeBytes, stream: counter }; + } +} diff --git a/app/src/attachments/direct-uploads.service.ts b/app/src/attachments/direct-uploads.service.ts new file mode 100644 index 00000000..b431ef8f --- /dev/null +++ b/app/src/attachments/direct-uploads.service.ts @@ -0,0 +1,293 @@ +import { + ConflictException, + HttpException, + Injectable, + NotFoundException, + PayloadTooLargeException, + UnsupportedMediaTypeException, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { randomUUID } from 'node:crypto'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { ChatsService } from '../chats/chats.service.js'; +import { JobsRepository } from '../jobs/jobs.repository.js'; +import { OutboxRelay } from '../jobs/outbox-relay.service.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { + ObjectStorage, + ObjectStorageUnavailableError, +} from '../storage/object-storage.js'; +import type { User } from '../users/user.js'; +import { + toAttachment, + type Attachment, + type AttachmentRow, +} from './attachment.js'; +import { AttachmentsRepository } from './attachments.repository.js'; +import { imageRejection } from './attachments.service.js'; +import type { CreateDirectUploadDto } from './dto/create-direct-upload.dto.js'; +import { validateFilename } from './filename.js'; +import { ImageDecoder } from './image/image-decoder.js'; +import { UploadSlots } from './upload-slots.js'; +import { + ImageInspector, + ImageRejectedError, + type ImageHeader, +} from './image/image-inspector.js'; + +export type DirectUpload = { + attachment_id: string; + method: 'PUT'; + url: string; + headers: Record; + expires_at: string; +}; + +const MEDIA_TYPES = new Set(['image/png', 'image/jpeg']); + +@Injectable() +export class DirectUploadsService { + private readonly maxUploadBytes: number; + private readonly maxPixels: number; + private readonly uploadTimeoutMs: number; + private readonly ttlSeconds: number; + private readonly maxPending: number; + private readonly pipelineVersion: number; + private readonly maxAttempts: number; + + constructor( + config: ConfigService, + private readonly repository: AttachmentsRepository, + private readonly jobs: JobsRepository, + private readonly relay: OutboxRelay, + private readonly storage: ObjectStorage, + private readonly chats: ChatsService, + private readonly metrics: MetricsService, + private readonly decoder: ImageDecoder, + private readonly slots: UploadSlots, + ) { + this.maxUploadBytes = config.getOrThrow('MAX_UPLOAD_BYTES'); + this.maxPixels = config.getOrThrow('MAX_IMAGE_PIXELS'); + this.uploadTimeoutMs = config.getOrThrow('UPLOAD_TIMEOUT_MS'); + this.ttlSeconds = config.getOrThrow('DIRECT_UPLOAD_URL_TTL_SECONDS'); + this.maxPending = config.getOrThrow( + 'DIRECT_UPLOAD_MAX_PENDING_PER_USER', + ); + this.pipelineVersion = config.getOrThrow('PIPELINE_VERSION'); + this.maxAttempts = config.getOrThrow('JOB_MAX_ATTEMPTS'); + } + + async init( + chatId: string, + user: User, + dto: CreateDirectUploadDto, + ): Promise { + const filename = validateFilename(dto.filename); + if (!MEDIA_TYPES.has(dto.content_type)) + throw new UnsupportedMediaTypeException( + 'content_type must be image/png or image/jpeg', + ); + if (dto.size_bytes > this.maxUploadBytes) + throw new PayloadTooLargeException('Image is too large'); + await this.chats.getForUser(chatId, user); + const id = randomUUID(); + const stagingKey = this.storage.stagingKey(id); + const inserted = await this.repository.insertPendingLimited( + { + id, + chatId, + ownerId: user.id, + filename, + contentType: dto.content_type, + uploadMode: 'direct', + objectKey: this.storage.objectKey(id), + stagingKey, + sizeBytes: dto.size_bytes, + sha256: dto.sha256, + ttlMs: this.ttlSeconds * 1000, + }, + this.maxPending, + ); + if (!inserted) + throw new HttpException( + { + message: 'Too many unfinished direct uploads', + retryAfter: this.ttlSeconds, + }, + 429, + ); + const checksum = Buffer.from(dto.sha256, 'hex').toString('base64'); + let url: string; + try { + url = await this.storage.presignPut(stagingKey, { + contentType: dto.content_type, + contentLength: dto.size_bytes, + checksumSha256Base64: checksum, + expiresInSeconds: this.ttlSeconds, + }); + } catch (error) { + await this.repository.deletePending(id); + throw error; + } + this.metrics.increment('attachments.direct.initiated'); + return { + attachment_id: id, + method: 'PUT', + url, + headers: { + 'Content-Type': dto.content_type, + 'x-amz-checksum-sha256': checksum, + }, + expires_at: new Date( + Date.now() + this.ttlSeconds * 1000, + ).toISOString(), + }; + } + + async complete(attachmentId: string, user: User): Promise { + const row = await this.repository.findForComplete( + attachmentId, + user.id, + ); + if (!row) throw new NotFoundException(); + if (row.status !== 'pending') return this.describe(row); + return this.slots.run(user.id, () => this.verifyAndAccept(row)); + } + + private async describe(row: AttachmentRow): Promise { + const variants = + row.status === 'ready' && row.ready_pipeline_version !== null + ? await this.jobs.variantsFor( + row.id, + row.ready_pipeline_version, + ) + : []; + return toAttachment(row, variants); + } + + private async verifyAndAccept(row: AttachmentRow): Promise { + const size = Number(row.size_bytes); + const stagingKey = row.staging_key; + if (stagingKey === null) + throw new ConflictException('Upload has not been received'); + const expectedChecksum = Buffer.from( + row.sha256 as string, + 'hex', + ).toString('base64'); + const head = await this.storage.head(stagingKey); + if (head === null) + throw new ConflictException('Upload has not been received'); + if (head.size !== size || head.checksumSha256 !== expectedChecksum) { + await this.reject(row, stagingKey); + throw new ConflictException( + 'Uploaded object does not match the declaration', + ); + } + + let header: ImageHeader; + try { + header = await this.verifyObject(stagingKey, size); + } catch (error) { + if (error instanceof ImageRejectedError) { + await this.reject(row, stagingKey); + throw imageRejection(error); + } + throw error; + } + if (header.contentType !== row.content_type) { + await this.reject(row, stagingKey); + throw new UnsupportedMediaTypeException( + 'Image content does not match content_type', + ); + } + const accepted = await this.jobs.acceptUpload( + row.id, + { + sizeBytes: size, + sha256: row.sha256 as string, + width: header.width, + height: header.height, + }, + this.pipelineVersion, + this.maxAttempts, + ); + if (!accepted) { + // A parallel complete of the same upload won: answer like a repeat. + const current = await this.repository.findForComplete( + row.id, + row.owner_id, + ); + if (current && current.status !== 'pending') + return this.describe(current); + throw new ConflictException('Upload is no longer pending'); + } + this.relay.kick(); + this.metrics.increment('attachments.direct.completed'); + return toAttachment(accepted); + } + + /** + * Reads the whole object: a correct signed checksum only proves that the + * bytes are the ones the client declared, not that they form an image. The + * stream goes through ImageInspector (structure, CRCs, completeness), then + * the copy is fully decoded. Memory is bounded like a proxy upload. + */ + private async verifyObject( + key: string, + size: number, + ): Promise { + // Same deadline as a proxy upload: a slow or hung read releases its slot. + const signal = AbortSignal.timeout(this.uploadTimeoutMs); + const stream = await this.storage.getStream(key, signal); + if (stream === null) + throw new ConflictException('Upload has not been received'); + const inspector = new ImageInspector(this.maxPixels); + const chunks: Buffer[] = []; + let read = 0; + try { + for await (const chunk of stream as AsyncIterable) { + read += chunk.length; + if (read > size) + throw new ImageRejectedError( + 'unsupported_media_type', + 'Object is larger than declared', + ); + inspector.push(chunk); + chunks.push(chunk); + } + } catch (error) { + if (error instanceof ImageRejectedError) throw error; + // The object stream broke mid-read (S3 outage, timeout): the image + // was not judged, so this is an outage, not a client error. + throw new ObjectStorageUnavailableError(); + } finally { + stream.destroy(); + this.metrics.add('attachments.direct.verify_bytes', read); + } + const header = inspector.finish(); + const data = Buffer.concat(chunks, read); + chunks.length = 0; + await this.decoder.verify(data, signal); + return header; + } + + /** + * A rejected claim keeps its row (status `rejected`, original expiry) while + * the signed URL may still be used: a later re-PUT would otherwise leave an + * object that no row refers to. The sweeper removes both after expiry + grace. + */ + private async reject( + row: AttachmentRow, + stagingKey: string, + ): Promise { + // Row first: a row that is not pending any more (accepted after a parallel + // complete, or claimed by the sweeper) keeps its object. + if (!(await this.repository.rejectPending(row.id))) return; + try { + await this.storage.delete(stagingKey); + } catch { + // The sweeper deletes the object again after expiry. + } + this.metrics.increment('attachments.direct.rejected'); + } +} diff --git a/app/src/attachments/dto/create-direct-upload.dto.ts b/app/src/attachments/dto/create-direct-upload.dto.ts new file mode 100644 index 00000000..f883a126 --- /dev/null +++ b/app/src/attachments/dto/create-direct-upload.dto.ts @@ -0,0 +1,16 @@ +import { IsInt, IsString, Matches, Min } from 'class-validator'; + +export class CreateDirectUploadDto { + @IsString() + filename!: string; + + @IsString() + content_type!: string; + + @IsInt() + @Min(1) + size_bytes!: number; + + @Matches(/^[a-f0-9]{64}$/u) + sha256!: string; +} diff --git a/app/src/attachments/filename.ts b/app/src/attachments/filename.ts new file mode 100644 index 00000000..b4cb3b9b --- /dev/null +++ b/app/src/attachments/filename.ts @@ -0,0 +1,34 @@ +import { BadRequestException } from '@nestjs/common'; + +const FORBIDDEN = + // eslint-disable-next-line no-control-regex + /[/\\\u0000-\u001f\u007f-\u009f\u200e\u200f\u202a-\u202e\u2066-\u2069]/u; + +/** The name is stored as-is and used for display only, never in paths or keys. */ +export function validateFilename(value: unknown): string { + if (typeof value !== 'string') throw invalid(); + const length = [...value].length; + if ( + length < 1 || + length > 255 || + /^\s*$/u.test(value) || + value === '.' || + value === '..' || + FORBIDDEN.test(value) + ) + throw invalid(); + return value; +} + +function invalid(): BadRequestException { + return new BadRequestException('filename is invalid'); +} + +export function contentDisposition(filename: string): string { + const fallback = filename.replace(/[^\x20-\x7e]|["\\]/gu, '_'); + const encoded = encodeURIComponent(filename).replace( + /['()*]/gu, + (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`, + ); + return `inline; filename="${fallback}"; filename*=UTF-8''${encoded}`; +} diff --git a/app/src/attachments/image/image-decoder.spec.ts b/app/src/attachments/image/image-decoder.spec.ts new file mode 100644 index 00000000..cc98444f --- /dev/null +++ b/app/src/attachments/image/image-decoder.spec.ts @@ -0,0 +1,70 @@ +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from 'vitest'; +import { jpegWithoutScanData } from '../../../test/support/jpeg.js'; +import { + maxPixelsRgba16Png, + noisePng, + pngWithGarbageIdat, +} from '../../../test/support/png.js'; +import { ImageDecoder } from './image-decoder.js'; +import { ImageInspector, ImageRejectedError } from './image-inspector.js'; + +const here = dirname(fileURLToPath(import.meta.url)); +const png = readFileSync(join(here, '../../../../tests/fixtures/sample.png')); +const jpeg = readFileSync(join(here, '../../../test/fixtures/sample.jpg')); +const progressive = readFileSync( + join(here, '../../../test/fixtures/sample-progressive.jpg'), +); + +const settings: Record = { + MAX_IMAGE_PIXELS: 40_000_000, + IMAGE_DECODE_MAX_CONCURRENT: 1, +}; +const decoder = new ImageDecoder( + { getOrThrow: (key: string) => settings[key] } as never, + { increment: () => undefined, add: () => undefined } as never, +); + +describe('ImageDecoder', () => { + it('accepts valid PNG and JPEG files, also concurrently', async () => { + await Promise.all( + [png, jpeg, progressive, noisePng(1_048_576)].map((data) => + decoder.verify(data), + ), + ); + }); + + // These files pass the structural inspector; only a decode catches them. + it.each([ + ['PNG with a garbage zlib stream', pngWithGarbageIdat()], + ['JPEG without scan data', jpegWithoutScanData(jpeg)], + [ + 'progressive JPEG without scan data', + jpegWithoutScanData(progressive), + ], + ])('rejects a %s', async (_name, data) => { + const inspector = new ImageInspector(40_000_000); + inspector.push(data); + expect(inspector.finish().width).toBe(320); + await expect(decoder.verify(data)).rejects.toBeInstanceOf( + ImageRejectedError, + ); + }); + + it('accepts the heaviest valid input: 40 MP RGBA 16-bit', async () => { + await decoder.verify(await maxPixelsRgba16Png()); + }, 30_000); + + it('drops an aborted waiter from the queue without running its decode', async () => { + const busy = decoder.verify(noisePng(4_000_000)); + const controller = new AbortController(); + const waiting = decoder.verify(png, controller.signal); + controller.abort(); + await expect(waiting).rejects.toMatchObject({ status: 503 }); + await busy; + // The slot was not leaked: the next decode runs. + await decoder.verify(png); + }); +}); diff --git a/app/src/attachments/image/image-decoder.ts b/app/src/attachments/image/image-decoder.ts new file mode 100644 index 00000000..c3831012 --- /dev/null +++ b/app/src/attachments/image/image-decoder.ts @@ -0,0 +1,119 @@ +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, + 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 { + 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 { + 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(); + 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(); + } +} diff --git a/app/src/attachments/image/image-inspector.spec.ts b/app/src/attachments/image/image-inspector.spec.ts new file mode 100644 index 00000000..9e749592 --- /dev/null +++ b/app/src/attachments/image/image-inspector.spec.ts @@ -0,0 +1,109 @@ +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from 'vitest'; +import { noisePng, pixelBombPng } from '../../../test/support/png.js'; +import { ImageInspector, ImageRejectedError } from './image-inspector.js'; + +const here = dirname(fileURLToPath(import.meta.url)); +const png = readFileSync(join(here, '../../../../tests/fixtures/sample.png')); +const jpeg = readFileSync(join(here, '../../../test/fixtures/sample.jpg')); +const progressive = readFileSync( + join(here, '../../../test/fixtures/sample-progressive.jpg'), +); +const MAX = 40_000_000; + +function inspect(data: Buffer, step = data.length, max = MAX) { + const inspector = new ImageInspector(max); + for (let offset = 0; offset < data.length; offset += step) + inspector.push(data.subarray(offset, offset + step)); + return inspector.finish(); +} + +function rejection(action: () => unknown): ImageRejectedError { + try { + action(); + } catch (error) { + expect(error).toBeInstanceOf(ImageRejectedError); + return error as ImageRejectedError; + } + throw new Error('expected rejection'); +} + +describe('ImageInspector', () => { + it.each([ + ['png', png, 'image/png'], + ['jpeg', jpeg, 'image/jpeg'], + ['progressive jpeg', progressive, 'image/jpeg'], + ])('accepts %s and reports its size', (_name, data, contentType) => { + const expected = { contentType, width: 320, height: 200 }; + expect(inspect(data)).toEqual(expected); + expect(inspect(data, 1)).toEqual(expected); + expect(inspect(data, 7)).toEqual(expected); + }); + + it('rejects truncated files on finish', () => { + for (const data of [png, jpeg, progressive]) + for (const cut of [10, 40, data.length - 100, data.length - 1]) { + const reason = rejection(() => + inspect(data.subarray(0, cut)), + ).reason; + expect(reason).toBe('unsupported_media_type'); + } + }); + + it('rejects a flipped byte inside IDAT by checksum', () => { + const damaged = Buffer.from(png); + damaged[Math.floor(png.length / 2)] ^= 0xff; + expect(rejection(() => inspect(damaged)).reason).toBe( + 'unsupported_media_type', + ); + }); + + it('rejects a pixel bomb right after IHDR, before any IDAT', () => { + const bomb = pixelBombPng(); + const inspector = new ImageInspector(MAX); + // signature + IHDR chunk = 8 + 25 bytes + const reason = rejection(() => + inspector.push(bomb.subarray(0, 33)), + ).reason; + expect(reason).toBe('too_many_pixels'); + expect(inspector.header).toBeUndefined(); + }); + + it('applies the pixel limit to JPEG frame headers', () => { + expect(rejection(() => inspect(jpeg, jpeg.length, 63_999)).reason).toBe( + 'too_many_pixels', + ); + expect(inspect(jpeg, jpeg.length, 64_000).width).toBe(320); + }); + + it('rejects foreign formats and trailing data after IEND', () => { + for (const data of [ + Buffer.from('GIF89a' + 'x'.repeat(40)), + Buffer.from('plain text, not an image'), + Buffer.alloc(0), + ]) + expect(rejection(() => inspect(data)).reason).toBe( + 'unsupported_media_type', + ); + expect( + rejection(() => inspect(Buffer.concat([png, Buffer.from([0])]))) + .reason, + ).toBe('unsupported_media_type'); + }); + + it('accepts padded and noise PNGs of exact sizes', () => { + for (const size of [131_072, 1_048_576]) { + const data = noisePng(size); + expect(data.length).toBe(size); + expect(inspect(data).width).toBe(1024); + } + }); + + it('tolerates trailing data after JPEG EOI', () => { + expect( + inspect(Buffer.concat([jpeg, Buffer.from('MPF trailer')])).height, + ).toBe(200); + }); +}); diff --git a/app/src/attachments/image/image-inspector.ts b/app/src/attachments/image/image-inspector.ts new file mode 100644 index 00000000..aeb88feb --- /dev/null +++ b/app/src/attachments/image/image-inspector.ts @@ -0,0 +1,387 @@ +import { crc32 } from 'node:zlib'; + +export type ImageHeader = { + contentType: 'image/png' | 'image/jpeg'; + width: number; + height: number; +}; + +export class ImageRejectedError extends Error { + constructor( + readonly reason: 'unsupported_media_type' | 'too_many_pixels', + message: string, + ) { + super(message); + this.name = 'ImageRejectedError'; + } +} + +const PNG_SIGNATURE = Buffer.from([ + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, +]); +const PNG_MAX_DIMENSION = 2 ** 31 - 1; +const PNG_BIT_DEPTHS: Record = { + 0: [1, 2, 4, 8, 16], + 2: [8, 16], + 3: [1, 2, 4, 8], + 4: [8, 16], + 6: [8, 16], +}; +const SOF_MARKERS = new Set([ + 0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, + 0xcf, +]); + +type State = + | 'start' + // PNG + | 'png_chunk_head' + | 'png_ihdr' + | 'png_skip' + | 'png_crc' + | 'png_done' + // JPEG + | 'jpeg_marker' + | 'jpeg_code' + | 'jpeg_length' + | 'jpeg_sof' + | 'jpeg_skip' + | 'jpeg_entropy' + | 'jpeg_entropy_ff' + | 'jpeg_trailer'; + +function unsupported(message: string): ImageRejectedError { + return new ImageRejectedError('unsupported_media_type', message); +} + +/** + * Streaming structural validator for PNG and JPEG. It never buffers the body: + * only fixed-size fields (chunk headers, IHDR, SOF) are accumulated, and + * nothing is decompressed. It is the early gate (type, dimensions, pixel limit + * before anything is stored), not proof of a valid image: compressed pixel + * data is checked by ImageDecoder. + */ +export class ImageInspector { + private state: State = 'start'; + private imageHeader: ImageHeader | undefined; + + // Accumulator for fixed-length fields. + private field = Buffer.alloc(16); + private fieldNeed = 0; + private fieldLength = 0; + + // Remaining bytes to skip in png_skip / jpeg_skip. + private skip = 0; + + // PNG + private chunkType = ''; + private chunkLength = 0; + private crc = 0; + private firstChunk = true; + private seenIdat = false; + + // JPEG + private marker = 0; + private scans = 0; + private afterSkip: State = 'jpeg_marker'; + + constructor(private readonly maxPixels: number) {} + + get header(): ImageHeader | undefined { + return this.imageHeader; + } + + push(chunk: Buffer): void { + let offset = 0; + while (offset < chunk.length) offset = this.step(chunk, offset); + } + + finish(): ImageHeader { + const done = this.state === 'png_done' || this.state === 'jpeg_trailer'; + if (!done || this.imageHeader === undefined) + throw unsupported('Image data is incomplete'); + return this.imageHeader; + } + + private collect(need: number): void { + this.fieldNeed = need; + this.fieldLength = 0; + } + + /** Copies up to fieldNeed bytes; returns new offset and whether complete. */ + private fill(chunk: Buffer, offset: number): [number, boolean] { + const take = Math.min( + this.fieldNeed - this.fieldLength, + chunk.length - offset, + ); + chunk.copy(this.field, this.fieldLength, offset, offset + take); + this.fieldLength += take; + return [offset + take, this.fieldLength === this.fieldNeed]; + } + + private step(chunk: Buffer, offset: number): number { + switch (this.state) { + case 'start': + return this.stepStart(chunk, offset); + case 'png_chunk_head': + case 'png_ihdr': + case 'png_crc': + case 'jpeg_length': + case 'jpeg_sof': { + const [next, complete] = this.fill(chunk, offset); + if (complete) this.fieldDone(); + return next; + } + case 'png_skip': + case 'jpeg_skip': { + const take = Math.min(this.skip, chunk.length - offset); + if (this.state === 'png_skip') + this.crc = crc32( + chunk.subarray(offset, offset + take), + this.crc, + ); + this.skip -= take; + if (this.skip === 0) this.skipDone(); + return offset + take; + } + case 'png_done': + throw unsupported('Data after the PNG end chunk'); + case 'jpeg_marker': + if (chunk[offset] !== 0xff) + throw unsupported('Unexpected byte between JPEG segments'); + this.state = 'jpeg_code'; + return offset + 1; + case 'jpeg_code': + this.onMarkerCode(chunk[offset]); + return offset + 1; + case 'jpeg_entropy': { + const next = chunk.indexOf(0xff, offset); + if (next === -1) return chunk.length; + this.state = 'jpeg_entropy_ff'; + return next + 1; + } + case 'jpeg_entropy_ff': { + const byte = chunk[offset]; + if (byte === 0xff) return offset + 1; + if (byte === 0x00 || (byte >= 0xd0 && byte <= 0xd7)) { + this.state = 'jpeg_entropy'; + return offset + 1; + } + this.onMarkerCode(byte); + return offset + 1; + } + case 'jpeg_trailer': + return chunk.length; + } + } + + private stepStart(chunk: Buffer, offset: number): number { + // Accumulate until the type is certain: 3 bytes for JPEG, 8 for PNG. + if (this.fieldLength === 0 && this.fieldNeed === 0) this.collect(8); + const have = this.fieldLength; + const take = Math.min(this.fieldNeed - have, chunk.length - offset); + chunk.copy(this.field, have, offset, offset + take); + this.fieldLength += take; + const first = this.field[0]; + if (first === 0xff) { + if (this.fieldLength >= 3) { + if (this.field[1] !== 0xd8 || this.field[2] !== 0xff) + throw unsupported('Not a JPEG file'); + // The third byte opens the next marker. + this.state = 'jpeg_code'; + this.fieldNeed = 0; + this.fieldLength = 0; + return offset + (3 - have); + } + return offset + take; + } + if (first === PNG_SIGNATURE[0]) { + for (let index = 0; index < this.fieldLength; index += 1) + if (this.field[index] !== PNG_SIGNATURE[index]) + throw unsupported('Not a PNG file'); + if (this.fieldLength === 8) { + this.state = 'png_chunk_head'; + this.collect(8); + } + return offset + take; + } + throw unsupported('Unsupported image format'); + } + + private fieldDone(): void { + switch (this.state) { + case 'png_chunk_head': + this.onPngChunkHead(); + return; + case 'png_ihdr': + this.crc = crc32(this.field.subarray(0, 13), this.crc); + this.parseIhdr(); + this.state = 'png_crc'; + this.collect(4); + return; + case 'png_crc': { + const expected = this.field.readUInt32BE(0); + if (this.crc >>> 0 !== expected) + throw unsupported('PNG chunk checksum mismatch'); + this.onPngChunkEnd(); + return; + } + case 'jpeg_length': + this.onJpegLength(); + return; + case 'jpeg_sof': + this.onJpegSof(); + return; + default: + return; + } + } + + private skipDone(): void { + if (this.state === 'png_skip') { + this.state = 'png_crc'; + this.collect(4); + } else { + this.state = this.afterSkip; + } + } + + // ---- PNG ---- + + private onPngChunkHead(): void { + const length = this.field.readUInt32BE(0); + const type = this.field.toString('latin1', 4, 8); + if (length > PNG_MAX_DIMENSION) + throw unsupported('PNG chunk is too long'); + if (!/^[A-Za-z]{4}$/u.test(type)) + throw unsupported('PNG chunk type is invalid'); + if (this.firstChunk) { + if (type !== 'IHDR' || length !== 13) + throw unsupported('PNG must start with IHDR'); + this.firstChunk = false; + } else if (type === 'IHDR') { + throw unsupported('Duplicate PNG IHDR'); + } + if (type === 'IEND') { + if (length !== 0 || !this.seenIdat) + throw unsupported('PNG IEND is invalid'); + } + if (type === 'IDAT') this.seenIdat = true; + this.chunkType = type; + this.chunkLength = length; + this.crc = crc32(this.field.subarray(4, 8), 0); + if (type === 'IHDR') { + this.state = 'png_ihdr'; + this.collect(13); + } else if (length === 0) { + this.state = 'png_crc'; + this.collect(4); + } else { + this.state = 'png_skip'; + this.skip = length; + } + } + + private parseIhdr(): void { + const width = this.field.readUInt32BE(0); + const height = this.field.readUInt32BE(4); + const bitDepth = this.field[8]; + const colorType = this.field[9]; + if (width < 1 || width > PNG_MAX_DIMENSION) + throw unsupported('PNG width is invalid'); + if (height < 1 || height > PNG_MAX_DIMENSION) + throw unsupported('PNG height is invalid'); + if (!PNG_BIT_DEPTHS[colorType]?.includes(bitDepth)) + throw unsupported('PNG color type / bit depth is invalid'); + if (this.field[10] !== 0 || this.field[11] !== 0) + throw unsupported('PNG compression/filter method is invalid'); + if (this.field[12] > 1) + throw unsupported('PNG interlace method is invalid'); + this.pendingHeader = { contentType: 'image/png', width, height }; + } + + private pendingHeader: ImageHeader | undefined; + + private onPngChunkEnd(): void { + if (this.chunkType === 'IHDR' && this.pendingHeader) { + const { width, height } = this.pendingHeader; + if (width * height > this.maxPixels) + throw new ImageRejectedError( + 'too_many_pixels', + 'Image has too many pixels', + ); + this.imageHeader = this.pendingHeader; + } + if (this.chunkType === 'IEND') { + this.state = 'png_done'; + return; + } + this.state = 'png_chunk_head'; + this.collect(8); + } + + // ---- JPEG ---- + + private onMarkerCode(code: number): void { + if (code === 0xff) return; // fill byte + this.marker = code; + if (code === 0x00 || code === 0xd8) + throw unsupported('Unexpected JPEG marker'); + if (code === 0x01 || (code >= 0xd0 && code <= 0xd7)) { + this.state = 'jpeg_marker'; + return; + } + if (code === 0xd9) { + if (this.imageHeader === undefined || this.scans === 0) + throw unsupported('JPEG ended before image data'); + this.state = 'jpeg_trailer'; + return; + } + if (code === 0xda && this.imageHeader === undefined) + throw unsupported('JPEG scan before frame header'); + this.state = 'jpeg_length'; + this.collect(2); + } + + private onJpegLength(): void { + const length = this.field.readUInt16BE(0); + if (length < 2) throw unsupported('JPEG segment length is invalid'); + const dataLength = length - 2; + if (SOF_MARKERS.has(this.marker)) { + if (this.imageHeader !== undefined || this.pendingHeader) + throw unsupported('Duplicate JPEG frame header'); + if (dataLength < 6) throw unsupported('JPEG frame header is short'); + this.skip = dataLength - 6; + this.afterSkip = 'jpeg_marker'; + this.state = 'jpeg_sof'; + this.collect(6); + return; + } + if (this.marker === 0xda) { + this.scans += 1; + this.afterSkip = 'jpeg_entropy'; + } else { + this.afterSkip = 'jpeg_marker'; + } + if (dataLength === 0) this.state = this.afterSkip; + else { + this.skip = dataLength; + this.state = 'jpeg_skip'; + } + } + + private onJpegSof(): void { + const height = this.field.readUInt16BE(1); + const width = this.field.readUInt16BE(3); + if (width === 0 || height === 0 || this.field[5] === 0) + throw unsupported('JPEG dimensions are invalid'); + if (width * height > this.maxPixels) + throw new ImageRejectedError( + 'too_many_pixels', + 'Image has too many pixels', + ); + this.imageHeader = { contentType: 'image/jpeg', width, height }; + if (this.skip === 0) this.state = 'jpeg_marker'; + else this.state = 'jpeg_skip'; + } +} diff --git a/app/src/attachments/upload-request.ts b/app/src/attachments/upload-request.ts new file mode 100644 index 00000000..3479cc34 --- /dev/null +++ b/app/src/attachments/upload-request.ts @@ -0,0 +1,9 @@ +/** Request of the upload route; see the onRequest hook in main.ts. */ +export type UploadRequest = { + method: string; + routeOptions?: { url?: string }; + headers: Record; + /** Content-Type as sent by the client (the header itself is rewritten). */ + uploadContentType?: string | string[]; + body?: unknown; +}; diff --git a/app/src/attachments/upload-slots.spec.ts b/app/src/attachments/upload-slots.spec.ts new file mode 100644 index 00000000..9910b9a7 --- /dev/null +++ b/app/src/attachments/upload-slots.spec.ts @@ -0,0 +1,54 @@ +import { describe, expect, it } from 'vitest'; +import { UploadSlots } from './upload-slots.js'; + +const slots = (max: number, perUser: number) => + new UploadSlots({ + getOrThrow: (key: string) => + key === 'UPLOAD_MAX_IN_FLIGHT' ? max : perUser, + } as never); + +function hold() { + const { promise, resolve } = Promise.withResolvers(); + return { work: () => promise, release: resolve }; +} + +describe('UploadSlots', () => { + it('caps one user without starving others, and frees slots after work', async () => { + const limiter = slots(10, 2); + const held = [hold(), hold()]; + const running = held.map((h) => limiter.run('a', h.work)); + await expect(limiter.run('a', async () => 'x')).rejects.toMatchObject({ + status: 429, + }); + await expect(limiter.run('b', async () => 'ok')).resolves.toBe('ok'); + held.forEach((h) => h.release()); + await Promise.all(running); + await expect(limiter.run('a', async () => 'again')).resolves.toBe( + 'again', + ); + }); + + it('applies the replica-wide cap across users', async () => { + const limiter = slots(2, 2); + const held = [hold(), hold()]; + const running = [ + limiter.run('a', held[0].work), + limiter.run('b', held[1].work), + ]; + await expect(limiter.run('c', async () => 'x')).rejects.toMatchObject({ + status: 429, + }); + held.forEach((h) => h.release()); + await Promise.all(running); + }); + + it('releases a slot when the work fails', async () => { + const limiter = slots(1, 1); + await expect( + limiter.run('a', async () => { + throw new Error('boom'); + }), + ).rejects.toThrow('boom'); + await expect(limiter.run('a', async () => 'ok')).resolves.toBe('ok'); + }); +}); diff --git a/app/src/attachments/upload-slots.ts b/app/src/attachments/upload-slots.ts new file mode 100644 index 00000000..bc4129f8 --- /dev/null +++ b/app/src/attachments/upload-slots.ts @@ -0,0 +1,41 @@ +import { HttpException, Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; + +/** + * Per-replica caps on requests that hold an image in memory (proxy upload, + * direct complete): each may keep up to MAX_UPLOAD_BYTES for the decode check. + * The per-user cap keeps one client (e.g. slow senders holding slots until + * UPLOAD_TIMEOUT_MS) from starving everyone else. + */ +@Injectable() +export class UploadSlots { + private inFlight = 0; + private readonly perUser = new Map(); + private readonly max: number; + private readonly maxPerUser: number; + + constructor(config: ConfigService) { + this.max = config.getOrThrow('UPLOAD_MAX_IN_FLIGHT'); + this.maxPerUser = config.getOrThrow('UPLOAD_MAX_IN_FLIGHT_PER_USER'); + } + + async run(userId: string, work: () => Promise): Promise { + const mine = this.perUser.get(userId) ?? 0; + if (this.inFlight >= this.max || mine >= this.maxPerUser) + throw new HttpException( + { message: 'Too many concurrent uploads', retryAfter: 1 }, + 429, + ); + this.inFlight += 1; + this.perUser.set(userId, mine + 1); + try { + return await work(); + } finally { + this.inFlight -= 1; + const left = (this.perUser.get(userId) ?? 1) - 1; + if (left === 0) this.perUser.delete(userId); + else this.perUser.set(userId, left); + } + } +} diff --git a/app/src/auth/auth.controller.ts b/app/src/auth/auth.controller.ts new file mode 100644 index 00000000..265696d9 --- /dev/null +++ b/app/src/auth/auth.controller.ts @@ -0,0 +1,79 @@ +import { + BadRequestException, + Body, + Controller, + Delete, + Headers, + HttpCode, + HttpStatus, + Post, + UnauthorizedException, + UseGuards, +} from '@nestjs/common'; +import { TextDecoder } from 'node:util'; +import { AuthService } from './auth.service.js'; +import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; + +const BASE64 = + /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u; +const BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'; + +function basicUnauthorized(): UnauthorizedException { + return new UnauthorizedException({ + message: 'Unauthorized', + challenge: BASIC_CHALLENGE, + }); +} + +function bearerUnauthorized(): UnauthorizedException { + return new UnauthorizedException({ + message: 'Unauthorized', + challenge: 'Bearer realm="messenger"', + }); +} + +function parseBasic(value: string | undefined): [string, string] { + if (typeof value !== 'string') throw basicUnauthorized(); + const match = /^Basic[ \t]+([^ \t]+)$/iu.exec(value); + if (!match || !BASE64.test(match[1])) throw basicUnauthorized(); + const bytes = Buffer.from(match[1], 'base64'); + if (bytes.toString('base64') !== match[1]) throw basicUnauthorized(); + let decoded: string; + try { + decoded = new TextDecoder('utf-8', { fatal: true }).decode(bytes); + } catch { + throw basicUnauthorized(); + } + const separator = decoded.indexOf(':'); + if (separator < 1) throw basicUnauthorized(); + return [decoded.slice(0, separator), decoded.slice(separator + 1)]; +} + +@Controller('api/v1/auth/sessions') +export class AuthController { + constructor(private readonly auth: AuthService) {} + + @Post() + create( + @Headers('authorization') authorization: string | undefined, + @Body() body: unknown, + ) { + const [username, password] = parseBasic(authorization); + if (body !== undefined) + throw new BadRequestException( + 'Session creation does not accept a body', + ); + return this.auth.createSession(username, password); + } + + @Delete('current') + @UseGuards(CurrentUserGuard) + @HttpCode(HttpStatus.NO_CONTENT) + async delete( + @Headers('authorization') authorization: string | undefined, + ): Promise { + if (!authorization || !/^Bearer\s+[^\s]+$/iu.test(authorization)) + throw bearerUnauthorized(); + await this.auth.deleteSession(authorization.slice(7).trim()); + } +} diff --git a/app/src/auth/auth.module.ts b/app/src/auth/auth.module.ts new file mode 100644 index 00000000..c29483bd --- /dev/null +++ b/app/src/auth/auth.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { DatabaseModule } from '../database/database.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { UsersModule } from '../users/users.module.js'; +import { AuthController } from './auth.controller.js'; +import { AuthService } from './auth.service.js'; +import { SessionRepository } from './session.repository.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; + +@Module({ + imports: [UsersModule, DatabaseModule, RedisModule, MetricsModule], + controllers: [AuthController], + providers: [AuthService, SessionRepository], + exports: [AuthService, SessionRepository], +}) +export class AuthModule {} diff --git a/app/src/auth/auth.service.ts b/app/src/auth/auth.service.ts new file mode 100644 index 00000000..f9833586 --- /dev/null +++ b/app/src/auth/auth.service.ts @@ -0,0 +1,63 @@ +import { + Injectable, + ServiceUnavailableException, + UnauthorizedException, +} from '@nestjs/common'; +import argon2 from 'argon2'; +import type { User } from '../users/user.js'; +import { UsersService } from '../users/users.service.js'; +import { SessionRepository } from './session.repository.js'; + +export type SessionResponse = { + token: string; + token_type: 'Bearer'; + expires_at: string; + user: User; +}; + +@Injectable() +export class AuthService { + constructor( + private readonly users: UsersService, + private readonly sessions: SessionRepository, + ) {} + + async createSession( + username: string, + password: string, + ): Promise { + const storedUser = await this.users.findByUsername(username); + if ( + !storedUser || + !(await argon2.verify(storedUser.password_hash, password)) + ) { + throw new UnauthorizedException(); + } + const user: User = { + id: storedUser.id, + username: storedUser.username, + display_name: storedUser.display_name, + created_at: storedUser.created_at, + }; + let session: { token: string; expiresAt: string }; + try { + session = await this.sessions.create(user.id); + } catch { + throw new ServiceUnavailableException('Session store unavailable'); + } + return { + token: session.token, + token_type: 'Bearer', + expires_at: session.expiresAt, + user, + }; + } + + async deleteSession(token: string): Promise { + try { + await this.sessions.delete(token); + } catch { + throw new ServiceUnavailableException('Session store unavailable'); + } + } +} diff --git a/app/src/auth/session.repository.ts b/app/src/auth/session.repository.ts new file mode 100644 index 00000000..57eb2827 --- /dev/null +++ b/app/src/auth/session.repository.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { randomBytes } from 'node:crypto'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { RedisClient } from '../redis/redis.client.js'; +import { MetricsService } from '../metrics/metrics.service.js'; + +@Injectable() +export class SessionRepository { + constructor( + private readonly redis: RedisClient, + private readonly config: ConfigService, + private readonly metrics: MetricsService, + ) {} + + async create( + userId: string, + ): Promise<{ token: string; expiresAt: string }> { + const token = randomBytes(32).toString('base64url'); + const ttl = this.config.getOrThrow('SESSION_TTL_SECONDS'); + const expiresAt = new Date(Date.now() + ttl * 1000).toISOString(); + this.metrics.increment('session_set'); + await this.redis.set( + `session:${token}`, + JSON.stringify({ user_id: userId, expires_at: expiresAt }), + { EX: ttl }, + ); + return { token, expiresAt }; + } + + delete(token: string): Promise { + this.metrics.increment('session_del'); + return this.redis.del(`session:${token}`); + } +} diff --git a/app/src/chats/chat.ts b/app/src/chats/chat.ts new file mode 100644 index 00000000..fb2e262e --- /dev/null +++ b/app/src/chats/chat.ts @@ -0,0 +1,6 @@ +export type Chat = { + id: string; + title: string; + member_ids: string[]; + created_at: string; +}; diff --git a/app/src/chats/chats.controller.ts b/app/src/chats/chats.controller.ts new file mode 100644 index 00000000..53dcd865 --- /dev/null +++ b/app/src/chats/chats.controller.ts @@ -0,0 +1,49 @@ +import { + BadRequestException, + Body, + Controller, + DefaultValuePipe, + Get, + Param, + ParseIntPipe, + ParseUUIDPipe, + Post, + Query, + UseGuards, +} from '@nestjs/common'; +import { CurrentUser } from '../common/auth/current-user.decorator.js'; +import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; +import { ChatsService } from './chats.service.js'; +import { CreateChatDto } from './dto/create-chat.dto.js'; +import type { User } from '../users/user.js'; + +@Controller('api/v1/chats') +@UseGuards(CurrentUserGuard) +export class ChatsController { + constructor(private readonly chatsService: ChatsService) {} + + @Post() + create(@Body() body: CreateChatDto, @CurrentUser() currentUser: User) { + return this.chatsService.create(body, currentUser); + } + + @Get() + list( + @CurrentUser() currentUser: User, + @Query('limit', new DefaultValuePipe(50), new ParseIntPipe()) + limit: number, + @Query('cursor') cursor?: string, + ) { + if (limit < 1 || limit > 100) + throw new BadRequestException('limit must be between 1 and 100'); + return this.chatsService.listForUser(currentUser, limit, cursor); + } + + @Get(':chatId') + get( + @Param('chatId', new ParseUUIDPipe()) chatId: string, + @CurrentUser() currentUser: User, + ) { + return this.chatsService.getForUser(chatId, currentUser); + } +} diff --git a/app/src/chats/chats.module.ts b/app/src/chats/chats.module.ts new file mode 100644 index 00000000..5bc65a34 --- /dev/null +++ b/app/src/chats/chats.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { DatabaseModule } from '../database/database.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { UsersModule } from '../users/users.module.js'; +import { ChatsController } from './chats.controller.js'; +import { ChatsRepository } from './chats.repository.js'; +import { ChatsService } from './chats.service.js'; + +@Module({ + imports: [UsersModule, DatabaseModule, RedisModule, MetricsModule], + controllers: [ChatsController], + providers: [ChatsRepository, ChatsService], + exports: [ChatsService], +}) +export class ChatsModule {} diff --git a/app/src/chats/chats.repository.ts b/app/src/chats/chats.repository.ts new file mode 100644 index 00000000..7f724bcb --- /dev/null +++ b/app/src/chats/chats.repository.ts @@ -0,0 +1,109 @@ +import { BadRequestException, Injectable } from '@nestjs/common'; +import { randomUUID } from 'node:crypto'; +import { DatabaseClient } from '../database/database.client.js'; +import type { Cursor } from '../common/http/cursor.js'; +import type { User } from '../users/user.js'; +import type { Chat } from './chat.js'; + +type ChatRow = { + id: string; + title: string; + created_at: Date | string; + member_ids: string[]; +}; + +function toChat(row: ChatRow): Chat { + return { + id: row.id, + title: row.title, + created_at: new Date(row.created_at).toISOString(), + member_ids: row.member_ids, + }; +} + +@Injectable() +export class ChatsRepository { + constructor(private readonly database: DatabaseClient) {} + + async create(title: string, memberIds: string[]): Promise { + const id = randomUUID(); + return this.database.withTransaction(async (client) => { + const users = await client.query<{ id: string }>({ + name: 'chats.validateMembers', + text: 'SELECT id FROM users WHERE id = ANY($1::uuid[])', + values: [memberIds], + }); + if (users.rowCount !== memberIds.length) + throw new BadRequestException('Chat member does not exist'); + const inserted = await client.query({ + name: 'chats.insert', + text: 'INSERT INTO chats (id, title) VALUES ($1, $2) RETURNING id, title, created_at', + values: [id, title], + }); + for (const memberId of memberIds) { + await client.query({ + name: 'chats.insertMember', + text: 'INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)', + values: [id, memberId], + }); + } + return { + ...toChat({ ...inserted.rows[0], member_ids: memberIds }), + member_ids: memberIds, + }; + }); + } + + async findForUser(id: string, user: User): Promise { + const result = await this.database.query( + 'chats.findForUser', + `SELECT c.id, c.title, c.created_at, + COALESCE(array_agg(cm.user_id ORDER BY cm.user_id) + FILTER (WHERE cm.user_id IS NOT NULL), '{}') AS member_ids + FROM chats c + LEFT JOIN chat_members cm ON cm.chat_id = c.id + WHERE c.id = $1 + AND EXISTS ( + SELECT 1 FROM chat_members own + WHERE own.chat_id = c.id AND own.user_id = $2 + ) + GROUP BY c.id`, + [id, user.id], + ); + return result.rows[0] ? toChat(result.rows[0]) : undefined; + } + + async findPageForUser( + user: User, + limit: number, + cursor?: Cursor, + ): Promise<{ items: Chat[]; hasMore: boolean }> { + const values: unknown[] = [user.id]; + let predicate = ''; + if (cursor) { + values.push(cursor.created_at, cursor.id); + predicate = + 'AND (c.created_at, c.id) > ($2::timestamptz, $3::uuid)'; + } + values.push(limit + 1); + const result = await this.database.query( + 'chats.listForUser', + `SELECT c.id, c.title, c.created_at, + COALESCE(array_agg(cm.user_id ORDER BY cm.user_id) + FILTER (WHERE cm.user_id IS NOT NULL), '{}') AS member_ids + FROM chats c + LEFT JOIN chat_members cm ON cm.chat_id = c.id + WHERE EXISTS ( + SELECT 1 FROM chat_members own + WHERE own.chat_id = c.id AND own.user_id = $1 + ) + ${predicate} + GROUP BY c.id ORDER BY c.created_at, c.id LIMIT $${values.length}`, + values, + ); + return { + items: result.rows.slice(0, limit).map(toChat), + hasMore: result.rows.length > limit, + }; + } +} diff --git a/app/src/chats/chats.service.ts b/app/src/chats/chats.service.ts new file mode 100644 index 00000000..2a78c65e --- /dev/null +++ b/app/src/chats/chats.service.ts @@ -0,0 +1,61 @@ +import { + BadRequestException, + Injectable, + NotFoundException, +} from '@nestjs/common'; +import { encodeCursor, decodeCursor } from '../common/http/cursor.js'; +import type { Page } from '../common/http/page.js'; +import type { User } from '../users/user.js'; +import type { CreateChatDto } from './dto/create-chat.dto.js'; +import type { Chat } from './chat.js'; +import { ChatsRepository } from './chats.repository.js'; + +@Injectable() +export class ChatsService { + constructor(private readonly chatsRepository: ChatsRepository) {} + + create(input: CreateChatDto, currentUser: User): Promise { + if (!input.member_ids.includes(currentUser.id)) + throw new BadRequestException('Current user must be a chat member'); + if (new Set(input.member_ids).size !== input.member_ids.length) + throw new BadRequestException('Duplicate chat member'); + return this.chatsRepository.create(input.title, input.member_ids); + } + + async getForUser(chatId: string, currentUser: User): Promise { + const chat = await this.chatsRepository.findForUser( + chatId, + currentUser, + ); + if (!chat) throw new NotFoundException(); + return chat; + } + + async listForUser( + currentUser: User, + limit: number, + cursorValue?: string, + ): Promise> { + const scope = `chats:${currentUser.id}`; + const cursor = cursorValue + ? decodeCursor(cursorValue, scope) + : undefined; + const page = await this.chatsRepository.findPageForUser( + currentUser, + limit, + cursor, + ); + const last = page.items.at(-1); + return { + items: page.items, + next_cursor: + page.hasMore && last + ? encodeCursor({ + scope, + created_at: last.created_at, + id: last.id, + }) + : null, + }; + } +} diff --git a/app/src/chats/dto/create-chat.dto.ts b/app/src/chats/dto/create-chat.dto.ts new file mode 100644 index 00000000..37ff853d --- /dev/null +++ b/app/src/chats/dto/create-chat.dto.ts @@ -0,0 +1,26 @@ +import { + ArrayMaxSize, + ArrayMinSize, + ArrayUnique, + IsArray, + IsString, + IsUUID, + Matches, +} from 'class-validator'; +import { CodePointLength } from '../../common/validation/code-point-length.js'; + +export class CreateChatDto { + @IsString() + @CodePointLength(1, 100) + @Matches(/\S/, { + message: 'title must contain a non-whitespace character', + }) + title: string; + + @IsArray() + @ArrayMinSize(1) + @ArrayMaxSize(100) + @ArrayUnique() + @IsUUID('4', { each: true }) + member_ids: string[]; +} diff --git a/app/src/common/auth/current-user.decorator.ts b/app/src/common/auth/current-user.decorator.ts new file mode 100644 index 00000000..dc054212 --- /dev/null +++ b/app/src/common/auth/current-user.decorator.ts @@ -0,0 +1,10 @@ +import { createParamDecorator, ExecutionContext } from '@nestjs/common'; +import type { User } from '../../users/user.js'; +import { FastifyRequest } from 'fastify'; + +export const CurrentUser = createParamDecorator( + (_data: unknown, context: ExecutionContext): User => { + const request = context.switchToHttp().getRequest(); + return request.currentUser!; + }, +); diff --git a/app/src/common/auth/current-user.guard.ts b/app/src/common/auth/current-user.guard.ts new file mode 100644 index 00000000..ae400b1f --- /dev/null +++ b/app/src/common/auth/current-user.guard.ts @@ -0,0 +1,85 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + ServiceUnavailableException, + UnauthorizedException, +} from '@nestjs/common'; +import type { FastifyRequest } from 'fastify'; +import type { User } from '../../users/user.js'; +import { RedisClient } from '../../redis/redis.client.js'; +import { UsersService } from '../../users/users.service.js'; +import { MetricsService } from '../../metrics/metrics.service.js'; + +@Injectable() +export class CurrentUserGuard implements CanActivate { + constructor( + private readonly redis: RedisClient, + private readonly users: UsersService, + private readonly metrics: MetricsService, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const request = context.switchToHttp().getRequest(); + const authorization = request.headers.authorization; + if ( + typeof authorization !== 'string' || + !/^Bearer\s+[^\s]+$/u.test(authorization) + ) { + throw new UnauthorizedException({ + message: 'Unauthorized', + challenge: 'Bearer realm="messenger"', + }); + } + let session: string | null; + try { + this.metrics.increment('session_get'); + session = await this.redis.get( + `session:${authorization.slice(7).trim()}`, + ); + } catch { + throw new ServiceUnavailableException('Session store unavailable'); + } + if (!session) + throw new UnauthorizedException({ + message: 'Unauthorized', + challenge: 'Bearer realm="messenger"', + }); + let userId: string; + try { + const parsed: unknown = JSON.parse(session); + if ( + typeof parsed !== 'object' || + parsed === null || + !('user_id' in parsed) || + typeof parsed.user_id !== 'string' || + !('expires_at' in parsed) || + typeof parsed.expires_at !== 'string' || + !/^(?:\d{4})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/u.test( + parsed.expires_at, + ) + ) + throw new Error('invalid session'); + const expiresAt = Date.parse(parsed.expires_at); + if (!Number.isFinite(expiresAt) || Date.now() >= expiresAt) + throw new Error('expired session'); + userId = parsed.user_id; + } catch { + throw new UnauthorizedException({ + message: 'Unauthorized', + challenge: 'Bearer realm="messenger"', + }); + } + let user: User | undefined; + try { + user = await this.users.findById(userId); + } catch { + throw new ServiceUnavailableException( + 'Authentication service unavailable', + ); + } + if (!user) throw new UnauthorizedException(); + request.currentUser = user; + return true; + } +} diff --git a/app/src/common/errors/api-error.ts b/app/src/common/errors/api-error.ts new file mode 100644 index 00000000..b26ece9b --- /dev/null +++ b/app/src/common/errors/api-error.ts @@ -0,0 +1,7 @@ +export type ApiErrorBody = { + error: { + code: string; + message: string; + request_id: string; + }; +}; diff --git a/app/src/common/errors/api-exception.filter.ts b/app/src/common/errors/api-exception.filter.ts new file mode 100644 index 00000000..e8e01b76 --- /dev/null +++ b/app/src/common/errors/api-exception.filter.ts @@ -0,0 +1,181 @@ +import { + ArgumentsHost, + Catch, + ExceptionFilter, + HttpException, + HttpStatus, + Logger, +} from '@nestjs/common'; +import { HttpAdapterHost } from '@nestjs/core'; +import { ConfigService } from '@nestjs/config'; +import { randomUUID } from 'node:crypto'; +import type { AppConfig } from '../../config/app-config.schema.js'; +import { resolveInstanceId } from '../../config/instance-id.js'; +import type { ApiErrorBody } from './api-error.js'; + +type RequestWithId = { requestId?: string; method?: string; url?: string }; +type ExceptionBody = { + message?: unknown; + challenge?: unknown; + retryAfter?: unknown; +}; + +// Connection/session loss codes reported by node-postgres and the network layer. +const UNAVAILABLE_ERROR_CODES: Record = { + ECONNREFUSED: true, + ECONNRESET: true, + ENOTFOUND: true, + ETIMEDOUT: true, + EPIPE: true, + '08000': true, + '08001': true, + '08003': true, + '08004': true, + '08006': true, + '57P01': true, + '57P02': true, + '57P03': true, + '53300': true, + // Transient aborts: the statement was cancelled by statement_timeout, the + // transaction was rolled back by a serialization failure or a deadlock. The + // work was never confirmed, so callers see a retryable 503 instead of an + // internal error. Without this, such an abort would be recorded as the final + // result of an idempotent operation. + '57014': true, + '40001': true, + '40P01': true, +}; + +// Some driver failures carry no `code` at all: node-postgres raises plain errors +// when a connection dies or its pool attempt times out, and node-redis reports a +// closed socket the same way. Without these patterns a stopped database would +// surface as an internal fault instead of a bounded outage. +const UNAVAILABLE_ERROR_MESSAGES = [ + 'Connection terminated unexpectedly', + 'Connection terminated due to connection timeout', + 'timeout exceeded when trying to connect', + 'Client has encountered a connection error and is not queryable', + 'Socket closed unexpectedly', + 'Connection is closed', +]; + +function isDependencyOutage(exception: unknown): boolean { + if (typeof exception !== 'object' || exception === null) return false; + const code = (exception as { code?: unknown }).code; + if (typeof code === 'string' && UNAVAILABLE_ERROR_CODES[code] === true) + return true; + const message = (exception as { message?: unknown }).message; + return ( + typeof message === 'string' && + UNAVAILABLE_ERROR_MESSAGES.some((pattern) => message.includes(pattern)) + ); +} + +function errorCodeForStatus(status: number): string { + const codes: Record = { + [HttpStatus.BAD_REQUEST]: 'invalid_request', + [HttpStatus.UNAUTHORIZED]: 'unauthorized', + [HttpStatus.NOT_FOUND]: 'not_found', + [HttpStatus.CONFLICT]: 'conflict', + [HttpStatus.PAYLOAD_TOO_LARGE]: 'payload_too_large', + [HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'unsupported_media_type', + [HttpStatus.TOO_MANY_REQUESTS]: 'rate_limited', + [HttpStatus.SERVICE_UNAVAILABLE]: 'unavailable', + }; + return codes[status] ?? 'internal_error'; +} + +@Catch() +export class ApiExceptionFilter implements ExceptionFilter { + private readonly logger = new Logger(ApiExceptionFilter.name); + // Guards and pipes run before interceptors, so an error response may be the + // first thing the filter writes: it must carry the replica identity too. + private readonly instanceId: string; + + constructor( + private readonly adapterHost: HttpAdapterHost, + config: ConfigService, + ) { + this.instanceId = resolveInstanceId(config); + } + + catch(exception: unknown, host: ArgumentsHost): void { + const http = host.switchToHttp(); + const request = http.getRequest(); + const response = http.getResponse(); + const adapter = this.adapterHost.httpAdapter; + const requestId = request.requestId ?? randomUUID(); + request.requestId = requestId; + + let status = HttpStatus.INTERNAL_SERVER_ERROR; + let message = 'Internal server error'; + let challenge: string | undefined; + let retryAfter = 1; + if (exception instanceof HttpException) { + status = exception.getStatus(); + const body = exception.getResponse(); + const details: ExceptionBody = + typeof body === 'object' && body !== null + ? (body as ExceptionBody) + : {}; + const rawMessage = + 'message' in details ? details.message : undefined; + challenge = + typeof details.challenge === 'string' + ? details.challenge + : undefined; + if ( + typeof details.retryAfter === 'number' && + Number.isFinite(details.retryAfter) && + details.retryAfter >= 1 + ) + retryAfter = Math.ceil(details.retryAfter); + message = Array.isArray(rawMessage) + ? rawMessage.join('; ') + : typeof rawMessage === 'string' + ? rawMessage + : 'Request failed'; + if (status === HttpStatus.NOT_FOUND) message = 'Resource not found'; + if (status >= HttpStatus.INTERNAL_SERVER_ERROR) + message = + status === HttpStatus.SERVICE_UNAVAILABLE + ? 'Service unavailable' + : 'Internal server error'; + } else if (isDependencyOutage(exception)) { + // A dropped dependency is a bounded outage, not an internal fault. + status = HttpStatus.SERVICE_UNAVAILABLE; + message = 'Service unavailable'; + this.logger.warn( + `Dependency outage on ${request.method ?? ''} ${request.url ?? ''}: ${exception instanceof Error ? exception.message : 'unknown error'}`, + ); + } else { + this.logger.error( + `Unhandled exception on ${request.method ?? ''} ${request.url ?? ''}`, + exception instanceof Error + ? exception.stack + : String(exception), + ); + } + if (status === HttpStatus.UNAUTHORIZED) { + const authChallenge = + challenge ?? + (request.method === 'POST' && + request.url?.startsWith('/api/v1/auth/sessions') + ? 'Basic realm="messenger", charset="UTF-8"' + : 'Bearer realm="messenger"'); + adapter.setHeader(response, 'WWW-Authenticate', authChallenge); + } + if (status === HttpStatus.TOO_MANY_REQUESTS) + adapter.setHeader(response, 'Retry-After', String(retryAfter)); + adapter.setHeader(response, 'X-Request-Id', requestId); + adapter.setHeader(response, 'X-Instance-Id', this.instanceId); + const payload: ApiErrorBody = { + error: { + code: errorCodeForStatus(status), + message, + request_id: requestId, + }, + }; + adapter.reply(response, payload, status); + } +} diff --git a/app/src/common/http/cursor.spec.ts b/app/src/common/http/cursor.spec.ts new file mode 100644 index 00000000..f4e7823b --- /dev/null +++ b/app/src/common/http/cursor.spec.ts @@ -0,0 +1,85 @@ +import { BadRequestException } from '@nestjs/common'; +import { randomUUID } from 'node:crypto'; +import { describe, expect, it } from 'vitest'; +import { decodeCursor, encodeCursor } from './cursor.js'; + +const scope = 'messages:6f1c2a3e-1111-4222-8333-444455556666'; + +function encode(createdAt: string, cursorScope = scope): string { + return encodeCursor({ + scope: cursorScope, + created_at: createdAt, + id: randomUUID(), + }); +} + +describe('decodeCursor', () => { + it('accepts valid instants and normalizes them to canonical milliseconds', () => { + const accepted: Array<[string, string]> = [ + ['2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z'], + ['2026-01-01T00:00:00Z', '2026-01-01T00:00:00.000Z'], + ['2026-01-01T00:00:00.999999Z', '2026-01-01T00:00:00.999Z'], + ['2024-02-29T23:59:59.000Z', '2024-02-29T23:59:59.000Z'], + ['2026-12-31T23:59:59.999Z', '2026-12-31T23:59:59.999Z'], + ]; + for (const [createdAt, expected] of accepted) { + const decoded = decodeCursor(encode(createdAt), scope); + expect(decoded.created_at).toBe(expected); + expect(decoded.scope).toBe(scope); + } + }); + + it('rejects calendar dates that the platform parser rolls over', () => { + // 2026-02-31 would silently become 2026-03-03 and then fail inside SQL. + for (const createdAt of [ + '2026-02-31T00:00:00.000Z', + '2025-02-29T00:00:00.000Z', + '2026-04-31T00:00:00.000Z', + '2026-06-31T12:00:00.000Z', + ]) { + expect(() => decodeCursor(encode(createdAt), scope)).toThrow( + BadRequestException, + ); + } + }); + + it('rejects out-of-range and malformed timestamps', () => { + for (const createdAt of [ + '2026-01-01T24:00:00.000Z', + '2026-01-01T00:00:60.000Z', + '2026-13-01T00:00:00.000Z', + '2026-00-01T00:00:00.000Z', + '2026-01-01T00:00:00', + '2026-01-01T00:00:00+02:00', + '2026-1-1T00:00:00.000Z', + '2026-01-01', + 'not-a-timestamp', + ]) { + expect(() => decodeCursor(encode(createdAt), scope)).toThrow( + BadRequestException, + ); + } + }); + + it('rejects a cursor from another scope, a malformed id, and non-cursor input', () => { + expect(() => + decodeCursor( + encode('2026-01-01T00:00:00.000Z', 'messages:other'), + scope, + ), + ).toThrow(BadRequestException); + expect(() => + decodeCursor( + encodeCursor({ + scope, + created_at: '2026-01-01T00:00:00.000Z', + id: 'not-a-uuid', + }), + scope, + ), + ).toThrow(BadRequestException); + expect(() => decodeCursor('not-a-cursor', scope)).toThrow( + BadRequestException, + ); + }); +}); diff --git a/app/src/common/http/cursor.ts b/app/src/common/http/cursor.ts new file mode 100644 index 00000000..037d4a4d --- /dev/null +++ b/app/src/common/http/cursor.ts @@ -0,0 +1,54 @@ +import { BadRequestException } from '@nestjs/common'; +import { isValid, parseISO } from 'date-fns'; + +export type Cursor = { + scope: string; + created_at: string; + id: string; +}; + +// Format check for the canonical API shape: `Date.prototype.toISOString()` with +// optional fractional seconds. Explicit ranges reject values such as 24:00:00. +// Calendar validity (2026-02-31, 2025-02-29, 2026-04-31) is checked by +// date-fns `parseISO`, which refuses impossible dates that the platform parser +// would silently roll over into another day. +const CURSOR_TIMESTAMP = + /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z$/u; + +export function encodeCursor(cursor: Cursor): string { + return Buffer.from(JSON.stringify(cursor), 'utf8').toString('base64url'); +} + +export function decodeCursor(value: string, expectedScope: string): Cursor { + try { + const parsed: unknown = JSON.parse( + Buffer.from(value, 'base64url').toString('utf8'), + ); + if ( + typeof parsed !== 'object' || + parsed === null || + !('scope' in parsed) || + !('created_at' in parsed) || + !('id' in parsed) || + typeof parsed.scope !== 'string' || + typeof parsed.created_at !== 'string' || + typeof parsed.id !== 'string' || + parsed.scope !== expectedScope || + !/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu.test( + parsed.id, + ) || + !CURSOR_TIMESTAMP.test(parsed.created_at) + ) + throw new Error('invalid cursor'); + const instant = parseISO(parsed.created_at); + if (!isValid(instant)) throw new Error('invalid cursor'); + + return { + scope: parsed.scope, + created_at: instant.toISOString(), + id: parsed.id, + }; + } catch { + throw new BadRequestException('Invalid cursor'); + } +} diff --git a/app/src/common/http/page.ts b/app/src/common/http/page.ts new file mode 100644 index 00000000..55c9ff8a --- /dev/null +++ b/app/src/common/http/page.ts @@ -0,0 +1,4 @@ +export type Page = { + items: T[]; + next_cursor: string | null; +}; diff --git a/app/src/common/http/request-id.interceptor.ts b/app/src/common/http/request-id.interceptor.ts new file mode 100644 index 00000000..e1ed4d83 --- /dev/null +++ b/app/src/common/http/request-id.interceptor.ts @@ -0,0 +1,59 @@ +import { + CallHandler, + ExecutionContext, + Injectable, + NestInterceptor, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { HttpAdapterHost } from '@nestjs/core'; +import { randomUUID } from 'node:crypto'; +import { finalize, Observable } from 'rxjs'; +import type { AppConfig } from '../../config/app-config.schema.js'; +import { resolveInstanceId } from '../../config/instance-id.js'; +import { MetricsService } from '../../metrics/metrics.service.js'; + +type RequestWithId = { requestId?: string }; + +@Injectable() +export class RequestIdInterceptor implements NestInterceptor { + // Resolved once: the identity must be stable for the whole process lifetime, + // otherwise a client cannot tell whether it moved to another replica. + private readonly instanceId: string; + + constructor( + private readonly adapterHost: HttpAdapterHost, + private readonly metrics: MetricsService, + config: ConfigService, + ) { + this.instanceId = resolveInstanceId(config); + } + + intercept( + context: ExecutionContext, + next: CallHandler, + ): Observable { + const request = context.switchToHttp().getRequest(); + const response = context.switchToHttp().getResponse(); + const requestId = randomUUID(); + const startedAt = performance.now(); + request.requestId = requestId; + this.adapterHost.httpAdapter.setHeader( + response, + 'X-Request-Id', + requestId, + ); + this.adapterHost.httpAdapter.setHeader( + response, + 'X-Instance-Id', + this.instanceId, + ); + this.metrics.increment('request_count'); + return next + .handle() + .pipe( + finalize(() => + this.metrics.observeLatency(performance.now() - startedAt), + ), + ); + } +} diff --git a/app/src/common/validation/code-point-length.spec.ts b/app/src/common/validation/code-point-length.spec.ts new file mode 100644 index 00000000..2daa8306 --- /dev/null +++ b/app/src/common/validation/code-point-length.spec.ts @@ -0,0 +1,43 @@ +import { validate } from 'class-validator'; +import { describe, expect, it } from 'vitest'; +import { CodePointLength } from './code-point-length.js'; + +class Sample { + @CodePointLength(3, 5) + value!: string; +} + +async function fails(value: unknown): Promise { + const sample = new Sample(); + (sample as { value: unknown }).value = value; + const errors = await validate(sample); + return errors.length > 0; +} + +describe('CodePointLength', () => { + it('counts Unicode code points instead of UTF-16 units', async () => { + // '🔐' is one code point but two UTF-16 units. + expect(await fails('🔐🔐')).toBe(true); + expect(await fails('🔐🔐🔐')).toBe(false); + expect(await fails('🔐🔐🔐🔐🔐')).toBe(false); + expect(await fails('🔐🔐🔐🔐🔐🔐')).toBe(true); + // Combining marks are separate code points, not grapheme clusters: + // three 'é' sequences are six code points and exceed the maximum. + expect(await fails('e\u0301e\u0301')).toBe(false); + expect(await fails('e\u0301e\u0301e\u0301')).toBe(true); + }); + + it('applies to BMP text boundaries', async () => { + expect(await fails('ab')).toBe(true); + expect(await fails('abc')).toBe(false); + expect(await fails('abcde')).toBe(false); + expect(await fails('abcdef')).toBe(true); + }); + + it('rejects empty and non-string values', async () => { + expect(await fails('')).toBe(true); + expect(await fails(undefined)).toBe(true); + expect(await fails(12345)).toBe(true); + expect(await fails(['a', 'b', 'c'])).toBe(true); + }); +}); diff --git a/app/src/common/validation/code-point-length.ts b/app/src/common/validation/code-point-length.ts new file mode 100644 index 00000000..5b3fa3ae --- /dev/null +++ b/app/src/common/validation/code-point-length.ts @@ -0,0 +1,38 @@ +import { + registerDecorator, + ValidatorConstraint, + type ValidationArguments, + type ValidatorConstraintInterface, +} from 'class-validator'; + +// JSON Schema (and the course contract) measure string lengths in Unicode code +// points, while String.prototype.length counts UTF-16 units: one emoji outside +// the BMP is two units. `@Length` therefore accepted an 11-emoji password +// (22 units) and rejected 4000 emoji (8000 units), so these fields use an +// explicit code-point validator instead. +@ValidatorConstraint({ name: 'codePointLength', async: false }) +class CodePointLengthConstraint implements ValidatorConstraintInterface { + validate(value: unknown, args: ValidationArguments): boolean { + const [min, max] = args.constraints as [number, number]; + if (typeof value !== 'string') return false; + const length = [...value].length; + return length >= min && length <= max; + } + + defaultMessage(args: ValidationArguments): string { + const [min, max] = args.constraints as [number, number]; + return `${args.property} must be between ${min} and ${max} code points`; + } +} + +export function CodePointLength(min: number, max: number): PropertyDecorator { + return (target, propertyKey) => { + registerDecorator({ + name: 'codePointLength', + target: target.constructor, + propertyName: String(propertyKey), + constraints: [min, max], + validator: CodePointLengthConstraint, + }); + }; +} diff --git a/app/src/config/app-config.module.ts b/app/src/config/app-config.module.ts new file mode 100644 index 00000000..567ea6a7 --- /dev/null +++ b/app/src/config/app-config.module.ts @@ -0,0 +1,16 @@ +import { Global, Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { parseAppConfig } from './app-config.schema.js'; + +@Global() +@Module({ + imports: [ + ConfigModule.forRoot({ + isGlobal: true, + cache: true, + validate: parseAppConfig, + }), + ], + exports: [ConfigModule], +}) +export class AppConfigModule {} diff --git a/app/src/config/app-config.schema.ts b/app/src/config/app-config.schema.ts new file mode 100644 index 00000000..c5b4cdaf --- /dev/null +++ b/app/src/config/app-config.schema.ts @@ -0,0 +1,255 @@ +import { readFileSync } from 'node:fs'; +import { z } from 'zod'; + +function numeric( + name: string, + minimum: number, + maximum: number | undefined, + fallback: number, +) { + let schema = z + .number({ message: `${name} must be a number` }) + .int() + .min(minimum); + if (maximum !== undefined) schema = schema.max(maximum); + return z.preprocess( + (value) => + typeof value === 'string' && value.trim() !== '' + ? Number(value) + : value, + schema.default(fallback), + ); +} + +function requiredString(name: string) { + return z + .string({ message: `${name} is required` }) + .min(1, `${name} is required`); +} + +export const appConfigSchema = z + .object({ + APP_HOST: z.string().min(1).default('0.0.0.0'), + APP_PORT: numeric('APP_PORT', 1, 65535, 8080), + APP_ENV: z.string().min(1).default('lab'), + SESSION_TTL_SECONDS: numeric('SESSION_TTL_SECONDS', 2, undefined, 3600), + DATABASE_URL: requiredString('DATABASE_URL'), + REDIS_URL: requiredString('REDIS_URL'), + BROKER_URL: requiredString('BROKER_URL'), + BROKER_PUBLISH_TIMEOUT_MS: numeric( + 'BROKER_PUBLISH_TIMEOUT_MS', + 100, + 60000, + 5000, + ), + DATABASE_POOL_MAX: numeric('DATABASE_POOL_MAX', 1, 100, 10), + DATABASE_CONNECTION_TIMEOUT_MS: numeric( + 'DATABASE_CONNECTION_TIMEOUT_MS', + 1, + undefined, + 5000, + ), + DATABASE_STATEMENT_TIMEOUT_MS: numeric( + 'DATABASE_STATEMENT_TIMEOUT_MS', + 1, + undefined, + 5000, + ), + DATABASE_IDLE_TX_TIMEOUT_MS: numeric( + 'DATABASE_IDLE_TX_TIMEOUT_MS', + 1000, + undefined, + 30000, + ), + // Бюджет зависимостей в /health/ready: LB проверяет ту же ручку, поэтому + // проба готовности обязана уложиться в timeout check HAProxy (500 ms). + HEALTH_TIMEOUT_MS: numeric('HEALTH_TIMEOUT_MS', 1, undefined, 400), + CACHE_TTL_SECONDS: numeric('CACHE_TTL_SECONDS', 1, undefined, 30), + CACHE_MODE: z.enum(['protected', 'naive']).default('protected'), + CACHE_LOCK_TTL_MS: numeric('CACHE_LOCK_TTL_MS', 1, undefined, 1000), + CACHE_MISS_DELAY_MS: numeric('CACHE_MISS_DELAY_MS', 0, undefined, 0), + METRICS_TOKEN: z.string().min(1).optional(), + DRAIN_DELAY_MS: numeric('DRAIN_DELAY_MS', 0, undefined, 5000), + INSTANCE_ID: z.string().min(1).optional(), + IDEMPOTENCY_TTL_SECONDS: numeric( + 'IDEMPOTENCY_TTL_SECONDS', + 60, + undefined, + 86400, + ), + S3_ENDPOINT: requiredString('S3_ENDPOINT'), + S3_PUBLIC_ENDPOINT: requiredString('S3_PUBLIC_ENDPOINT'), + S3_REGION: z.string().min(1).default('us-east-1'), + S3_BUCKET: requiredString('S3_BUCKET'), + S3_ACCESS_KEY_ID: requiredString('S3_ACCESS_KEY_ID'), + S3_SECRET_ACCESS_KEY: requiredString('S3_SECRET_ACCESS_KEY'), + S3_CONNECT_TIMEOUT_MS: numeric( + 'S3_CONNECT_TIMEOUT_MS', + 1, + undefined, + 1000, + ), + S3_REQUEST_TIMEOUT_MS: numeric( + 'S3_REQUEST_TIMEOUT_MS', + 1, + undefined, + 15000, + ), + MAX_UPLOAD_BYTES: numeric('MAX_UPLOAD_BYTES', 1, 10485760, 10485760), + MAX_IMAGE_PIXELS: numeric('MAX_IMAGE_PIXELS', 1, 40000000, 40000000), + UPLOAD_TIMEOUT_MS: numeric('UPLOAD_TIMEOUT_MS', 1000, undefined, 25000), + UPLOAD_MAX_IN_FLIGHT: numeric('UPLOAD_MAX_IN_FLIGHT', 1, 1024, 16), + UPLOAD_MAX_IN_FLIGHT_PER_USER: numeric( + 'UPLOAD_MAX_IN_FLIGHT_PER_USER', + 1, + 1024, + 4, + ), + IMAGE_DECODE_MAX_CONCURRENT: numeric( + 'IMAGE_DECODE_MAX_CONCURRENT', + 1, + 64, + 1, + ), + // 0 отключает фоновый проход уборщика (остаётся ручной CLI). + ATTACHMENT_SWEEP_INTERVAL_MS: numeric( + 'ATTACHMENT_SWEEP_INTERVAL_MS', + 0, + undefined, + 60000, + ), + ATTACHMENT_SWEEP_GRACE_SECONDS: numeric( + 'ATTACHMENT_SWEEP_GRACE_SECONDS', + 0, + undefined, + 900, + ), + DIRECT_UPLOAD_URL_TTL_SECONDS: numeric( + 'DIRECT_UPLOAD_URL_TTL_SECONDS', + 1, + 3600, + 300, + ), + DIRECT_UPLOAD_MAX_PENDING_PER_USER: numeric( + 'DIRECT_UPLOAD_MAX_PENDING_PER_USER', + 1, + 1000, + 10, + ), + WORKER_CONCURRENCY: numeric('WORKER_CONCURRENCY', 1, 16, 2), + JOB_MAX_ATTEMPTS: numeric('JOB_MAX_ATTEMPTS', 1, 20, 5), + JOB_TIMEOUT_SECONDS: numeric('JOB_TIMEOUT_SECONDS', 1, 600, 60), + JOB_LEASE_SECONDS: numeric('JOB_LEASE_SECONDS', 3, 600, 30), + JOB_RETRY_BASE_MS: numeric('JOB_RETRY_BASE_MS', 10, undefined, 1000), + JOB_RETRY_MAX_MS: numeric('JOB_RETRY_MAX_MS', 10, undefined, 30000), + PIPELINE_VERSION: numeric('PIPELINE_VERSION', 1, 2, 1), + BROKER_DELIVERY_LIMIT: numeric('BROKER_DELIVERY_LIMIT', 1, 100, 10), + OUTBOX_POLL_INTERVAL_MS: numeric( + 'OUTBOX_POLL_INTERVAL_MS', + 50, + undefined, + 500, + ), + LEASE_REAPER_INTERVAL_MS: numeric( + 'LEASE_REAPER_INTERVAL_MS', + 100, + undefined, + 2000, + ), + // 0 отключает фоновую уборку staging (ручной запуск остаётся). + STAGING_CLEANUP_INTERVAL_MS: numeric( + 'STAGING_CLEANUP_INTERVAL_MS', + 0, + undefined, + 60000, + ), + STAGING_RETENTION_SECONDS: numeric( + 'STAGING_RETENTION_SECONDS', + 60, + undefined, + 86400, + ), + WORKER_HEALTH_PORT: numeric('WORKER_HEALTH_PORT', 1, 65535, 8081), + FAULT_INJECTION_ENABLED: numeric('FAULT_INJECTION_ENABLED', 0, 1, 0), + QUEUED_RECONCILE_GRACE_SECONDS: numeric( + 'QUEUED_RECONCILE_GRACE_SECONDS', + 5, + 3600, + 15, + ), + OUTBOX_RETENTION_SECONDS: numeric( + 'OUTBOX_RETENTION_SECONDS', + 60, + undefined, + 86400, + ), + JOB_EVENTS_RETENTION_SECONDS: numeric( + 'JOB_EVENTS_RETENTION_SECONDS', + 3600, + undefined, + 2592000, + ), + }) + .strip(); + +export const databaseConfigSchema = appConfigSchema.pick({ + DATABASE_URL: true, + DATABASE_POOL_MAX: true, + DATABASE_CONNECTION_TIMEOUT_MS: true, + DATABASE_STATEMENT_TIMEOUT_MS: true, + DATABASE_IDLE_TX_TIMEOUT_MS: true, + HEALTH_TIMEOUT_MS: true, + IDEMPOTENCY_TTL_SECONDS: true, +}); +export type DatabaseConfig = z.infer; + +export type AppConfig = z.infer; +export type AppConfigKey = keyof AppConfig; + +function resolveSecret( + raw: Record, + key: + | 'DATABASE_URL' + | 'REDIS_URL' + | 'BROKER_URL' + | 'S3_ACCESS_KEY_ID' + | 'S3_SECRET_ACCESS_KEY', +): unknown { + const fileKey = `${key}_FILE`; + const path = raw[fileKey]; + if (typeof path !== 'string' || path.length === 0) return raw[key]; + try { + const value = readFileSync(path, 'utf8').trim(); + if (value.length === 0) + throw new Error(`${fileKey} contains an empty value`); + return value; + } catch (error) { + if ( + error instanceof Error && + error.message.includes('contains an empty value') + ) + throw error; + throw new Error(`${fileKey} could not be read`); + } +} + +export function parseAppConfig(raw: Record): AppConfig { + return appConfigSchema.parse({ + ...raw, + DATABASE_URL: resolveSecret(raw, 'DATABASE_URL'), + REDIS_URL: resolveSecret(raw, 'REDIS_URL'), + BROKER_URL: resolveSecret(raw, 'BROKER_URL'), + S3_ACCESS_KEY_ID: resolveSecret(raw, 'S3_ACCESS_KEY_ID'), + S3_SECRET_ACCESS_KEY: resolveSecret(raw, 'S3_SECRET_ACCESS_KEY'), + }); +} + +// Для процессов, которым нужна только БД (миграции, GC идемпотентности). +export function parseDatabaseConfig( + raw: Record, +): DatabaseConfig { + return databaseConfigSchema.parse({ + ...raw, + DATABASE_URL: resolveSecret(raw, 'DATABASE_URL'), + }); +} diff --git a/app/src/config/instance-id.ts b/app/src/config/instance-id.ts new file mode 100644 index 00000000..e42f2f48 --- /dev/null +++ b/app/src/config/instance-id.ts @@ -0,0 +1,14 @@ +import { randomUUID } from 'node:crypto'; +import { hostname } from 'node:os'; +import type { ConfigService } from '@nestjs/config'; +import type { AppConfig } from './app-config.schema.js'; + +// A replica must be identifiable from the outside. Inside Docker the hostname is +// the short container ID, so X-Instance-Id can be mapped back to exactly one +// container; INSTANCE_ID exists for platforms where that mapping is not useful. +export function resolveInstanceId(config: ConfigService): string { + const configured = config.get('INSTANCE_ID'); + if (configured) return configured; + const host = hostname(); + return host.length > 0 ? host : randomUUID(); +} diff --git a/app/src/database/database.client.ts b/app/src/database/database.client.ts new file mode 100644 index 00000000..d992c8a6 --- /dev/null +++ b/app/src/database/database.client.ts @@ -0,0 +1,105 @@ +import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import pg from 'pg'; +import { MetricsService } from '../metrics/metrics.service.js'; +import type { AppConfig } from '../config/app-config.schema.js'; + +@Injectable() +export class DatabaseClient implements OnModuleDestroy { + readonly pool: pg.Pool; + private readonly healthPool: pg.Pool; + private readonly logger = new Logger(DatabaseClient.name); + + constructor( + private readonly config: ConfigService, + private readonly metrics: MetricsService, + ) { + this.pool = new pg.Pool({ + connectionString: this.config.getOrThrow('DATABASE_URL'), + max: this.config.getOrThrow('DATABASE_POOL_MAX'), + connectionTimeoutMillis: this.config.getOrThrow( + 'DATABASE_CONNECTION_TIMEOUT_MS', + ), + statement_timeout: this.config.getOrThrow( + 'DATABASE_STATEMENT_TIMEOUT_MS', + ), + // A relay that waits on the broker must not leave the transaction + // idle forever if confirm handling stalls. + options: `-c idle_in_transaction_session_timeout=${this.config.getOrThrow('DATABASE_IDLE_TX_TIMEOUT_MS')}`, + }); + // Idle clients emit 'error' on the pool when the database drops; without a + // listener Node treats it as uncaught and terminates the process. + this.pool.on('error', (error: Error) => { + this.metrics.increment('database.pool_error'); + this.logger.warn(`PostgreSQL pool error: ${error.message}`); + }); + // Проба готовности не должна стоять в очереди за бизнес-запросами: занятый + // общий пул — это перегрузка, а не потеря зависимости. + this.healthPool = new pg.Pool({ + connectionString: this.config.getOrThrow('DATABASE_URL'), + max: 1, + idleTimeoutMillis: 0, + connectionTimeoutMillis: + this.config.getOrThrow('HEALTH_TIMEOUT_MS'), + query_timeout: this.config.getOrThrow('HEALTH_TIMEOUT_MS'), + }); + this.healthPool.on('error', (error: Error) => { + this.metrics.increment('database.health_pool_error'); + this.logger.warn( + `PostgreSQL health connection error: ${error.message}`, + ); + }); + } + + query( + label: string, + text: string, + values: readonly unknown[] = [], + ): Promise> { + this.metrics.increment(label); + return this.pool.query({ text, values: [...values] }); + } + + async health(): Promise { + this.metrics.increment('health.database'); + await this.healthPool.query('SELECT 1'); + } + + async withTransaction( + work: (client: pg.PoolClient) => Promise, + ): Promise { + const client = await this.pool.connect(); + // A server-side idle_in_transaction_session_timeout or a network drop + // can emit 'error' on a checked-out client after pg-pool removed its + // idle listener. Without this listener Node treats it as uncaught. + let fatal: Error | undefined; + const onError = (error: Error): void => { + fatal = error; + }; + client.on('error', onError); + try { + await client.query('BEGIN'); + const result = await work(client); + await client.query('COMMIT'); + return result; + } catch (error) { + try { + await client.query('ROLLBACK'); + } catch (rollbackError) { + this.metrics.increment('database.rollback_error'); + this.logger.warn( + `Rollback failed: ${rollbackError instanceof Error ? rollbackError.message : 'unknown error'}`, + ); + } + // Callers must see the operation error, not the rollback error. + throw error; + } finally { + client.removeListener('error', onError); + client.release(fatal); + } + } + + async onModuleDestroy(): Promise { + await Promise.all([this.pool.end(), this.healthPool.end()]); + } +} diff --git a/app/src/database/database.module.ts b/app/src/database/database.module.ts new file mode 100644 index 00000000..fe9f0b76 --- /dev/null +++ b/app/src/database/database.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { DatabaseClient } from './database.client.js'; + +@Module({ + imports: [MetricsModule], + providers: [DatabaseClient], + exports: [DatabaseClient], +}) +export class DatabaseModule {} diff --git a/app/src/database/migration.runner.ts b/app/src/database/migration.runner.ts new file mode 100644 index 00000000..5e101c35 --- /dev/null +++ b/app/src/database/migration.runner.ts @@ -0,0 +1,63 @@ +import { readdir, readFile } from 'node:fs/promises'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { DatabaseClient } from './database.client.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { parseDatabaseConfig } from '../config/app-config.schema.js'; + +const migrationsDirectory = join( + dirname(fileURLToPath(import.meta.url)), + 'migrations', +); + +async function migrationFiles(): Promise { + const files = await readdir(migrationsDirectory); + return files.filter((file) => /^\d+_.+\.sql$/u.test(file)).sort(); +} + +export async function runMigrations(database: DatabaseClient): Promise { + await database.query( + 'migrations.ensure_table', + `CREATE TABLE IF NOT EXISTS schema_migrations ( + version text PRIMARY KEY, + applied_at timestamptz NOT NULL DEFAULT now() + )`, + ); + + for (const version of await migrationFiles()) { + await database.withTransaction(async (client) => { + const applied = await client.query({ + text: 'SELECT version FROM schema_migrations WHERE version = $1', + values: [version], + }); + if (applied.rowCount) return; + + const sql = await readFile( + join(migrationsDirectory, version), + 'utf8', + ); + await client.query(sql); + await client.query({ + text: 'INSERT INTO schema_migrations (version) VALUES ($1)', + values: [version], + }); + }); + } +} + +async function main(): Promise { + const config = parseDatabaseConfig(process.env as Record); + const database = new DatabaseClient( + { + getOrThrow: (key: keyof typeof config) => config[key], + } as never, + new MetricsService(), + ); + try { + await runMigrations(database); + } finally { + await database.onModuleDestroy(); + } +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) await main(); diff --git a/app/src/database/migrations/001_initial.sql b/app/src/database/migrations/001_initial.sql new file mode 100644 index 00000000..19225298 --- /dev/null +++ b/app/src/database/migrations/001_initial.sql @@ -0,0 +1,41 @@ +CREATE TABLE IF NOT EXISTS schema_migrations ( + version text PRIMARY KEY, + applied_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE, + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + message_version bigint NOT NULL DEFAULT 0 +); + +CREATE TABLE IF NOT EXISTS chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE TABLE IF NOT EXISTS messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + text text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + CONSTRAINT messages_sender_member_fk + FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id) + ON DELETE RESTRICT +); + +CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx + ON messages (chat_id, created_at, id); +CREATE INDEX IF NOT EXISTS chat_members_user_chat_idx + ON chat_members (user_id, chat_id); diff --git a/app/src/database/migrations/002_timestamp_precision.sql b/app/src/database/migrations/002_timestamp_precision.sql new file mode 100644 index 00000000..e084dc68 --- /dev/null +++ b/app/src/database/migrations/002_timestamp_precision.sql @@ -0,0 +1,5 @@ +-- HTTP timestamps and keyset cursors use JavaScript millisecond precision. +-- Store the same precision so a returned cursor cannot point before its own row. +ALTER TABLE users ALTER COLUMN created_at TYPE timestamptz(3); +ALTER TABLE chats ALTER COLUMN created_at TYPE timestamptz(3); +ALTER TABLE messages ALTER COLUMN created_at TYPE timestamptz(3); diff --git a/app/src/database/migrations/003_idempotency_keys.sql b/app/src/database/migrations/003_idempotency_keys.sql new file mode 100644 index 00000000..aad214df --- /dev/null +++ b/app/src/database/migrations/003_idempotency_keys.sql @@ -0,0 +1,20 @@ +-- Shared idempotency ledger for POST /api/v1/chats/{id}/messages. +-- The key scope is (user, method, path, key): the same key from another user or +-- another chat is independent, as the contract requires. +-- claim + response are committed in one transaction, so a committed row always +-- carries status_code and response_body (there is no "poisoned" intermediate state). +CREATE TABLE IF NOT EXISTS idempotency_keys ( + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method text NOT NULL, + path text NOT NULL, + key text NOT NULL, + request_fingerprint text NOT NULL, + status_code integer, + response_body jsonb, + message_id uuid, + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, method, path, key) +); + +CREATE INDEX IF NOT EXISTS idempotency_keys_created_at_idx + ON idempotency_keys (created_at); diff --git a/app/src/database/migrations/004_pg_stat_statements.sql b/app/src/database/migrations/004_pg_stat_statements.sql new file mode 100644 index 00000000..c70da499 --- /dev/null +++ b/app/src/database/migrations/004_pg_stat_statements.sql @@ -0,0 +1,9 @@ +-- Instrumentation for the scaling experiments. pg_stat_statements only works +-- when the server is started with shared_preload_libraries=pg_stat_statements +-- (compose.yaml does that); without the preload the CREATE fails with a plain +-- notice, so a database started without the preload still migrates cleanly. +DO $$ BEGIN + CREATE EXTENSION IF NOT EXISTS pg_stat_statements; +EXCEPTION WHEN OTHERS THEN + RAISE NOTICE 'pg_stat_statements unavailable: %', SQLERRM; +END $$; diff --git a/app/src/database/migrations/005_attachments.sql b/app/src/database/migrations/005_attachments.sql new file mode 100644 index 00000000..a6c9d811 --- /dev/null +++ b/app/src/database/migrations/005_attachments.sql @@ -0,0 +1,42 @@ +ALTER TABLE messages ADD CONSTRAINT messages_id_chat_key UNIQUE (id, chat_id); + +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255), + content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + upload_mode text NOT NULL CHECK (upload_mode IN ('proxy', 'direct')), + status text NOT NULL CHECK (status IN ('pending', 'ready', 'deleting')), + object_key text NOT NULL UNIQUE, + size_bytes bigint CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 text CHECK (sha256 ~ '^[a-f0-9]{64}$'), + width integer CHECK (width > 0), + height integer CHECK (height > 0), + expires_at timestamptz(3), + created_at timestamptz(3) NOT NULL DEFAULT now(), + updated_at timestamptz(3) NOT NULL DEFAULT now(), + CONSTRAINT attachments_id_chat_key UNIQUE (id, chat_id), + CONSTRAINT attachments_owner_member_fk FOREIGN KEY (chat_id, owner_id) + REFERENCES chat_members (chat_id, user_id), + CONSTRAINT attachments_ready_complete CHECK (status <> 'ready' OR + (size_bytes IS NOT NULL AND sha256 IS NOT NULL AND width IS NOT NULL AND height IS NOT NULL)), + CONSTRAINT attachments_unfinished_expiry CHECK (status = 'ready' OR expires_at IS NOT NULL) +); +CREATE INDEX attachments_unfinished_idx ON attachments (expires_at) WHERE status <> 'ready'; +CREATE INDEX attachments_owner_direct_pending_idx ON attachments (owner_id) + WHERE status = 'pending' AND upload_mode = 'direct'; + +CREATE TABLE message_attachments ( + message_id uuid NOT NULL, + chat_id uuid NOT NULL, + attachment_id uuid NOT NULL, + ordinal smallint NOT NULL CHECK (ordinal BETWEEN 1 AND 10), + PRIMARY KEY (message_id, attachment_id), + CONSTRAINT message_attachments_ordinal_key UNIQUE (message_id, ordinal), + CONSTRAINT message_attachments_message_fk FOREIGN KEY (message_id, chat_id) + REFERENCES messages (id, chat_id) ON DELETE CASCADE, + CONSTRAINT message_attachments_attachment_fk FOREIGN KEY (attachment_id, chat_id) + REFERENCES attachments (id, chat_id) +); +CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id); diff --git a/app/src/database/migrations/006_attachment_rejected.sql b/app/src/database/migrations/006_attachment_rejected.sql new file mode 100644 index 00000000..6040217f --- /dev/null +++ b/app/src/database/migrations/006_attachment_rejected.sql @@ -0,0 +1,8 @@ +-- Rejected direct uploads keep their row until the signed URL can no longer be +-- used, so an object re-PUT after rejection is still found by the sweeper. +ALTER TABLE attachments DROP CONSTRAINT attachments_status_check; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_check + CHECK (status IN ('pending', 'ready', 'deleting', 'rejected')); +DROP INDEX attachments_owner_direct_pending_idx; +CREATE INDEX attachments_owner_direct_pending_idx ON attachments (owner_id) + WHERE status IN ('pending', 'rejected') AND upload_mode = 'direct'; diff --git a/app/src/database/migrations/007_attachment_pipeline.sql b/app/src/database/migrations/007_attachment_pipeline.sql new file mode 100644 index 00000000..c2244aab --- /dev/null +++ b/app/src/database/migrations/007_attachment_pipeline.sql @@ -0,0 +1,81 @@ +ALTER TABLE attachments DROP CONSTRAINT attachments_status_check; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN + ('pending','queued','processing','ready','failed','deleting','rejected')); +ALTER TABLE attachments DROP CONSTRAINT attachments_ready_complete; +ALTER TABLE attachments ADD CONSTRAINT attachments_accepted_complete CHECK ( + status NOT IN ('queued','processing','ready','failed') OR + (size_bytes IS NOT NULL AND sha256 IS NOT NULL AND width IS NOT NULL AND height IS NOT NULL)); +ALTER TABLE attachments DROP CONSTRAINT attachments_unfinished_expiry; +ALTER TABLE attachments ADD CONSTRAINT attachments_unfinished_expiry CHECK ( + status NOT IN ('pending','rejected','deleting') OR expires_at IS NOT NULL); +ALTER TABLE attachments + ADD COLUMN staging_key text UNIQUE, + ADD COLUMN error_code text CHECK (error_code ~ '^[a-z_]{1,64}$'), + ADD COLUMN ready_pipeline_version integer CHECK (ready_pipeline_version >= 1); +ALTER TABLE attachments ADD CONSTRAINT attachments_failed_code + CHECK (status <> 'failed' OR error_code IS NOT NULL); +DROP INDEX attachments_unfinished_idx; +CREATE INDEX attachments_unfinished_idx ON attachments (expires_at) + WHERE status IN ('pending','rejected','deleting'); +CREATE INDEX attachments_staging_idx ON attachments (updated_at) + WHERE staging_key IS NOT NULL AND status IN ('ready','failed'); + +-- Rows accepted by lab4 already have their final original and complete metadata. +UPDATE attachments SET ready_pipeline_version = 1 + WHERE status = 'ready' AND ready_pipeline_version IS NULL; + +CREATE TABLE attachment_jobs ( + job_id uuid PRIMARY KEY, + attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + pipeline_version integer NOT NULL CHECK (pipeline_version >= 1), + status text NOT NULL CHECK (status IN ('queued','processing','succeeded','failed','superseded')), + attempt integer NOT NULL DEFAULT 1 CHECK (attempt >= 1), + max_attempts integer NOT NULL CHECK (max_attempts >= 1), + next_attempt_at timestamptz(3) NOT NULL DEFAULT now(), + lease_owner text, lease_token uuid, lease_until timestamptz(3), + last_error_code text, + created_at timestamptz(3) NOT NULL DEFAULT now(), + updated_at timestamptz(3) NOT NULL DEFAULT now(), + finished_at timestamptz(3), + CONSTRAINT attachment_jobs_effect_key UNIQUE (attachment_id, pipeline_version), + CONSTRAINT attachment_jobs_lease CHECK ((status = 'processing') = + (lease_owner IS NOT NULL AND lease_token IS NOT NULL AND lease_until IS NOT NULL)) +); +CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until) WHERE status = 'processing'; +CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at) WHERE status = 'failed'; + +CREATE TABLE attachment_variants ( + attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + pipeline_version integer NOT NULL CHECK (pipeline_version >= 1), + variant text NOT NULL CHECK (variant IN ('thumbnail','crop','optimized')), + object_key text NOT NULL UNIQUE, + content_type text NOT NULL CHECK (content_type IN ('image/jpeg','image/png','image/webp')), + size_bytes bigint NOT NULL CHECK (size_bytes >= 1), + width integer NOT NULL CHECK (width >= 1), + height integer NOT NULL CHECK (height >= 1), + sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), + job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE, + created_at timestamptz(3) NOT NULL DEFAULT now(), + PRIMARY KEY (attachment_id, pipeline_version, variant) +); + +CREATE TABLE job_outbox ( + id bigserial PRIMARY KEY, + job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE, + payload jsonb NOT NULL, + available_at timestamptz(3) NOT NULL DEFAULT now(), + published_at timestamptz(3), + created_at timestamptz(3) NOT NULL DEFAULT now() +); +CREATE INDEX job_outbox_pending_idx ON job_outbox (available_at, id) WHERE published_at IS NULL; + +CREATE TABLE job_events ( + id bigserial PRIMARY KEY, + job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE, + event text NOT NULL CHECK (event IN ('created','claimed','retry_scheduled','lease_expired', + 'succeeded','failed','superseded','replayed','duplicate_skipped','staging_deleted')), + attempt integer NOT NULL, + detail jsonb NOT NULL DEFAULT '{}'::jsonb, + created_at timestamptz(3) NOT NULL DEFAULT now() +); +CREATE INDEX job_events_job_idx ON job_events (job_id, id); diff --git a/app/src/database/migrations/008_job_reconcile.sql b/app/src/database/migrations/008_job_reconcile.sql new file mode 100644 index 00000000..90cc697b --- /dev/null +++ b/app/src/database/migrations/008_job_reconcile.sql @@ -0,0 +1,37 @@ +ALTER TABLE attachment_jobs ADD COLUMN last_reconciled_at timestamptz(3); +CREATE INDEX job_outbox_job_idx ON job_outbox (job_id); + +-- lab4 rows are ready but predate variants. Requeue them once so the worker +-- materializes the same v1 variants every other ready attachment has. The +-- INSERT ... RETURNING CTE keeps the job, outbox and audit event atomic. +WITH legacy AS ( + INSERT INTO attachment_jobs + (job_id, attachment_id, pipeline_version, status, attempt, max_attempts, + next_attempt_at, created_at) + SELECT gen_random_uuid(), a.id, 1, 'queued', 1, 5, now(), now() + FROM attachments a + WHERE a.status = 'ready' + AND NOT EXISTS ( + SELECT 1 FROM attachment_variants v WHERE v.attachment_id = a.id) + AND NOT EXISTS ( + SELECT 1 FROM attachment_jobs j + WHERE j.attachment_id = a.id AND j.pipeline_version = 1) + RETURNING job_id, attachment_id, pipeline_version, attempt, created_at +), queued AS ( + INSERT INTO job_outbox (job_id, payload, available_at) + SELECT job_id, + jsonb_build_object( + 'schema_version', 1, + 'job_id', job_id, + 'type', 'attachment.process.v1', + 'attachment_id', attachment_id, + 'pipeline_version', pipeline_version, + 'attempt', attempt, + 'created_at', to_char(created_at AT TIME ZONE 'UTC', + 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')), + now() + FROM legacy + RETURNING job_id +) +INSERT INTO job_events (job_id, event, attempt, detail) +SELECT job_id, 'created', 1, '{"backfill":true}'::jsonb FROM legacy; diff --git a/app/src/database/migrations/009_queue_reconcile_indexes.sql b/app/src/database/migrations/009_queue_reconcile_indexes.sql new file mode 100644 index 00000000..73a5a863 --- /dev/null +++ b/app/src/database/migrations/009_queue_reconcile_indexes.sql @@ -0,0 +1,10 @@ +CREATE INDEX attachment_jobs_queued_idx + ON attachment_jobs (next_attempt_at) WHERE status = 'queued'; +CREATE INDEX job_events_created_idx ON job_events (created_at); +CREATE INDEX job_outbox_published_idx + ON job_outbox (published_at) WHERE published_at IS NOT NULL; + +ALTER TABLE job_events DROP CONSTRAINT job_events_event_check; +ALTER TABLE job_events ADD CONSTRAINT job_events_event_check CHECK (event IN + ('created','claimed','retry_scheduled','lease_expired','succeeded','failed', + 'superseded','replayed','duplicate_skipped','staging_deleted','reconciled')); diff --git a/app/src/health/health.controller.ts b/app/src/health/health.controller.ts new file mode 100644 index 00000000..f297b64c --- /dev/null +++ b/app/src/health/health.controller.ts @@ -0,0 +1,17 @@ +import { Controller, Get } from '@nestjs/common'; +import { HealthService } from './health.service.js'; + +@Controller('health') +export class HealthController { + constructor(private readonly healthService: HealthService) {} + + @Get('live') + getLive() { + return this.healthService.getLiveness(); + } + + @Get('ready') + async getReady() { + return this.healthService.getReadiness(); + } +} diff --git a/app/src/health/health.module.ts b/app/src/health/health.module.ts new file mode 100644 index 00000000..f5bbe7a6 --- /dev/null +++ b/app/src/health/health.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { HealthController } from './health.controller.js'; +import { HealthService } from './health.service.js'; +import { DatabaseModule } from '../database/database.module.js'; +import { RedisModule } from '../redis/redis.module.js'; + +@Module({ + imports: [DatabaseModule, RedisModule], + controllers: [HealthController], + providers: [HealthService], +}) +export class HealthModule {} diff --git a/app/src/health/health.service.spec.ts b/app/src/health/health.service.spec.ts new file mode 100644 index 00000000..91f3e435 --- /dev/null +++ b/app/src/health/health.service.spec.ts @@ -0,0 +1,91 @@ +import { describe, expect, it, vi } from 'vitest'; +import { ServiceUnavailableException } from '@nestjs/common'; +import { HealthService } from './health.service.js'; + +describe('HealthService', () => { + it('reports liveness and readiness when dependencies are healthy', async () => { + const database = { health: vi.fn().mockResolvedValue(undefined) }; + const redis = { health: vi.fn().mockResolvedValue(undefined) }; + const service = new HealthService( + database as never, + redis as never, + { getOrThrow: () => 50 } as never, + ); + expect(service.getLiveness()).toEqual({ status: 'ok' }); + await expect(service.getReadiness()).resolves.toEqual({ status: 'ok' }); + }); + + it('rejects readiness when a dependency is unavailable', async () => { + const database = { + health: vi.fn().mockRejectedValue(new Error('down')), + }; + const redis = { health: vi.fn().mockResolvedValue(undefined) }; + const service = new HealthService( + database as never, + redis as never, + { getOrThrow: () => 50 } as never, + ); + await expect(service.getReadiness()).rejects.toBeInstanceOf( + ServiceUnavailableException, + ); + }); + + it('rejects readiness when Redis alone is unavailable', async () => { + const database = { health: vi.fn().mockResolvedValue(undefined) }; + const redis = { + health: vi.fn().mockRejectedValue(new Error('down')), + }; + const service = new HealthService( + database as never, + redis as never, + { getOrThrow: () => 50 } as never, + ); + await expect(service.getReadiness()).rejects.toBeInstanceOf( + ServiceUnavailableException, + ); + }); + + it('keeps liveness healthy while draining', async () => { + const service = new HealthService( + { health: vi.fn() } as never, + { health: vi.fn() } as never, + { getOrThrow: () => 50 } as never, + ); + service.setDraining(); + expect(service.getLiveness()).toEqual({ status: 'ok' }); + await expect(service.getReadiness()).rejects.toThrow( + 'Service is shutting down', + ); + }); + + it('answers not-ready during drain without probing dependencies', async () => { + const database = { health: vi.fn().mockResolvedValue(undefined) }; + const redis = { health: vi.fn().mockResolvedValue(undefined) }; + const service = new HealthService( + database as never, + redis as never, + { getOrThrow: () => 50 } as never, + ); + service.setDraining(); + await expect(service.getReadiness()).rejects.toThrow( + 'Service is shutting down', + ); + // Дрейн обязан отвечать сразу: проба LB не должна ждать зависимости, + // когда реплика уже выходит из ротации. + expect(database.health).not.toHaveBeenCalled(); + expect(redis.health).not.toHaveBeenCalled(); + }); + + it('fails readiness within the budget when a dependency hangs', async () => { + const database = { health: vi.fn(() => new Promise(() => {})) }; + const redis = { health: vi.fn().mockResolvedValue(undefined) }; + const service = new HealthService( + database as never, + redis as never, + { getOrThrow: () => 20 } as never, + ); + await expect(service.getReadiness()).rejects.toBeInstanceOf( + ServiceUnavailableException, + ); + }); +}); diff --git a/app/src/health/health.service.ts b/app/src/health/health.service.ts new file mode 100644 index 00000000..b2e56402 --- /dev/null +++ b/app/src/health/health.service.ts @@ -0,0 +1,60 @@ +import { + Global, + Injectable, + Module, + ServiceUnavailableException, +} from '@nestjs/common'; +import { DatabaseClient } from '../database/database.client.js'; +import { RedisClient } from '../redis/redis.client.js'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; + +@Injectable() +export class HealthService { + private ready = true; + constructor( + private readonly database: DatabaseClient, + private readonly redis: RedisClient, + private readonly config: ConfigService, + ) {} + getLiveness() { + return { status: 'ok' as const }; + } + async getReadiness() { + if (!this.ready) + throw new ServiceUnavailableException('Service is shutting down'); + const dependencies = Promise.all([ + this.database.health(), + this.redis.health(), + ]); + // Поздний отказ после дедлайна не должен стать unhandled rejection. + dependencies.catch(() => undefined); + let timer: NodeJS.Timeout | undefined; + const deadline = new Promise((_, reject) => { + timer = setTimeout( + () => reject(new Error('readiness budget exceeded')), + this.config.getOrThrow('HEALTH_TIMEOUT_MS'), + ); + }); + try { + await Promise.race([dependencies, deadline]); + } catch { + throw new ServiceUnavailableException('Dependencies unavailable'); + } finally { + clearTimeout(timer); + } + return { status: 'ok' as const }; + } + // Called on SIGTERM before the drain delay: the replica turns not-ready while + // it finishes in-flight requests, so the load balancer stops probing it + // successfully and removes it from rotation. The delay itself lives in + // main.ts, because Nest destroys providers (closing the pools) before this + // class could serve anyone. + setDraining() { + this.ready = false; + } +} + +@Global() +@Module({ providers: [HealthService], exports: [HealthService] }) +export class HealthServiceModule {} diff --git a/app/src/jobs/backoff.spec.ts b/app/src/jobs/backoff.spec.ts new file mode 100644 index 00000000..1255ae3d --- /dev/null +++ b/app/src/jobs/backoff.spec.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest'; +import { retryDelayMs } from './backoff.js'; + +describe('retryDelayMs', () => { + it('uses equal jitter', () => { + expect(retryDelayMs(1, 1000, 30000, () => 0)).toBe(500); + expect(retryDelayMs(1, 1000, 30000, () => 1)).toBe(1000); + expect(retryDelayMs(10, 1000, 30000, () => 1)).toBe(30000); + }); + + it('never exceeds the cap and grows monotonically at random 0.5', () => { + const values = Array.from({ length: 6 }, (_, index) => + retryDelayMs(index + 1, 1000, 30000, () => 0.5), + ); + for (const value of values) expect(value).toBeLessThanOrEqual(30000); + for (let index = 1; index < values.length; index += 1) + expect(values[index]).toBeGreaterThanOrEqual(values[index - 1]); + }); +}); diff --git a/app/src/jobs/backoff.ts b/app/src/jobs/backoff.ts new file mode 100644 index 00000000..33645ff1 --- /dev/null +++ b/app/src/jobs/backoff.ts @@ -0,0 +1,14 @@ +/** + * Equal jitter: half of the exponential delay is fixed, half is random. The + * result never exceeds `capMs`, so `max_attempts` bounds both attempts and + * total wait time. + */ +export function retryDelayMs( + attempt: number, + baseMs: number, + capMs: number, + random: () => number = Math.random, +): number { + const exponential = Math.min(capMs, baseMs * 2 ** (attempt - 1)); + return Math.floor(exponential * (0.5 + 0.5 * random())); +} diff --git a/app/src/jobs/broker.client.ts b/app/src/jobs/broker.client.ts new file mode 100644 index 00000000..a749e90f --- /dev/null +++ b/app/src/jobs/broker.client.ts @@ -0,0 +1,419 @@ +import { + connect as amqpConnect, + type Channel, + type ChannelModel, + type ConfirmChannel, + type ConsumeMessage, +} from 'amqplib'; +import { + Injectable, + Logger, + OnModuleDestroy, + ServiceUnavailableException, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { randomUUID } from 'node:crypto'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { + assertTopology, + JOB_EXCHANGE, + JOB_QUEUE, + JOB_ROUTING_KEY, +} from './broker.topology.js'; +import type { JobMessage } from './job-message.js'; + +export class BrokerUnavailableError extends ServiceUnavailableException { + constructor() { + super('Broker unavailable'); + } +} + +export type ConsumeHandler = ( + message: ConsumeMessage, + channel: Channel, +) => Promise; + +const MAX_RECONNECT_DELAY_MS = 10_000; + +@Injectable() +export class BrokerClient implements OnModuleDestroy { + private readonly logger = new Logger(BrokerClient.name); + private readonly url: string; + private readonly deliveryLimit: number; + private readonly publishTimeoutMs: number; + private connection: ChannelModel | undefined; + private confirmChannel: ConfirmChannel | undefined; + private consumeChannel: Channel | undefined; + private consumerTag: string | undefined; + private prefetch = 0; + private handler: ConsumeHandler | undefined; + private connecting: Promise | undefined; + private subscribing: Promise | undefined; + private reconnectTimer: NodeJS.Timeout | undefined; + private subscribeTimer: NodeJS.Timeout | undefined; + private reconnectDelayMs = 1000; + private subscribeDelayMs = 1000; + private closing = false; + // Unroutable messages are returned before their confirm; the map lets the + // return listener reject the exact publish that produced it. + private readonly returned = new Map void>(); + + constructor( + config: ConfigService, + private readonly metrics: MetricsService, + ) { + this.url = config.getOrThrow('BROKER_URL'); + this.deliveryLimit = config.getOrThrow('BROKER_DELIVERY_LIMIT'); + this.publishTimeoutMs = config.getOrThrow( + 'BROKER_PUBLISH_TIMEOUT_MS', + ); + } + + async publish(message: JobMessage | Buffer): Promise { + const channel = await this.withTimeout( + this.ensureConnected(), + this.publishTimeoutMs, + ); + const content = Buffer.isBuffer(message) + ? message + : Buffer.from(JSON.stringify(message)); + const correlationId = randomUUID(); + const options = { + persistent: true, + contentType: 'application/json', + mandatory: true, + correlationId, + ...(Buffer.isBuffer(message) + ? {} + : { + messageId: message.job_id, + type: message.type, + headers: { 'x-attempt': message.attempt }, + }), + }; + const { promise, resolve, reject } = Promise.withResolvers(); + let settled = false; + const fail = (): void => { + if (settled) return; + settled = true; + clearTimeout(timer); + this.returned.delete(correlationId); + this.metrics.increment('broker.publish_failed'); + reject(new BrokerUnavailableError()); + }; + const timer = setTimeout(fail, this.publishTimeoutMs); + this.returned.set(correlationId, fail); + try { + channel.publish( + JOB_EXCHANGE, + JOB_ROUTING_KEY, + content, + options, + (error) => { + if (settled) return; + settled = true; + clearTimeout(timer); + this.returned.delete(correlationId); + if (error) { + this.metrics.increment('broker.publish_failed'); + reject(new BrokerUnavailableError()); + return; + } + this.metrics.increment('broker.published'); + resolve(); + }, + ); + } catch { + fail(); + } + return promise; + } + + consume(prefetch: number, handler: ConsumeHandler): void { + this.prefetch = prefetch; + this.handler = handler; + this.ensureConnected() + .then(() => this.subscribe()) + .catch(() => this.scheduleSubscribe()); + } + + isConsuming(): boolean { + return this.consumerTag !== undefined; + } + + async queueDepth( + queue: string, + ): Promise<{ messages: number; consumers: number }> { + const channel = await this.withTimeout( + this.ensureConnected(), + this.publishTimeoutMs, + ); + const result = await channel.checkQueue(queue); + return { + messages: result.messageCount, + consumers: result.consumerCount, + }; + } + + async stopConsuming(): Promise { + clearTimeout(this.subscribeTimer); + this.subscribeTimer = undefined; + const channel = this.consumeChannel; + const tag = this.consumerTag; + this.consumerTag = undefined; + this.consumeChannel = undefined; + this.handler = undefined; + if (channel && tag) { + try { + await channel.cancel(tag); + } catch { + // The channel is already closing; there is nothing to cancel. + } + } + if (channel) { + try { + await channel.close(); + } catch { + // Closing an already closed channel is harmless. + } + } + } + + async onModuleDestroy(): Promise { + this.closing = true; + clearTimeout(this.reconnectTimer); + clearTimeout(this.subscribeTimer); + this.reconnectTimer = undefined; + this.subscribeTimer = undefined; + for (const reject of this.returned.values()) reject(); + this.returned.clear(); + await this.stopConsuming(); + const connection = this.connection; + this.connection = undefined; + this.confirmChannel = undefined; + if (connection) { + try { + await connection.close(); + } catch { + // Shutdown is best effort; channels are already unusable. + } + } + } + + private async ensureConnected(): Promise { + if (this.closing) throw new BrokerUnavailableError(); + if (this.confirmChannel) return this.confirmChannel; + if (!this.connecting) { + this.connecting = this.connect() + .then(() => undefined) + .finally(() => { + this.connecting = undefined; + }); + } + await this.connecting; + if (!this.confirmChannel) throw new BrokerUnavailableError(); + return this.confirmChannel; + } + + private async connect(): Promise { + const connection = await amqpConnect(this.url); + connection.on('error', (error: Error) => { + this.metrics.increment('broker.error'); + this.logger.warn(`Broker connection error: ${error.message}`); + }); + connection.on('close', () => { + if (this.connection !== connection) return; + this.handleClose(); + }); + try { + const confirm = await connection.createConfirmChannel(); + confirm.on('return', (message) => { + const correlationId = message.properties?.correlationId; + if (!correlationId) return; + const reject = this.returned.get(correlationId); + if (!reject) return; + this.returned.delete(correlationId); + this.logger.warn('Broker returned an unroutable job message'); + reject(); + }); + await assertTopology(confirm, this.deliveryLimit); + confirm.on('error', (error: Error) => { + this.metrics.increment('broker.confirm_error'); + this.logger.warn(`Broker confirm channel error: ${error.message}`); + }); + confirm.on('close', () => this.handleConfirmClose(confirm)); + this.connection = connection; + this.confirmChannel = confirm; + this.reconnectDelayMs = 1000; + clearTimeout(this.reconnectTimer); + this.reconnectTimer = undefined; + this.metrics.increment('broker.connected'); + } catch (error) { + if (this.connection === connection) { + this.connection = undefined; + this.confirmChannel = undefined; + } + try { + await connection.close(); + } catch { + // The connection is already unusable. + } + throw error; + } + if (this.handler) + void this.subscribe().catch(() => this.scheduleSubscribe()); + } + + private handleConfirmClose(confirm: ConfirmChannel): void { + if (this.confirmChannel !== confirm) return; + this.metrics.increment('broker.confirm_closed'); + for (const reject of this.returned.values()) reject(); + this.returned.clear(); + this.confirmChannel = undefined; + const connection = this.connection; + if (connection) { + void connection.close().catch(() => this.handleClose()); + } else { + this.handleClose(); + } + } + + private subscribe(): Promise { + if (this.subscribing) return this.subscribing; + const connection = this.connection; + const handler = this.handler; + if (!connection || !handler || this.consumerTag) + return Promise.resolve(); + this.subscribing = this.doSubscribe(connection, handler).finally( + () => { + this.subscribing = undefined; + }, + ); + return this.subscribing; + } + + private async doSubscribe( + connection: ChannelModel, + handler: ConsumeHandler, + ): Promise { + const channel = await connection.createChannel(); + try { + channel.on('error', (error: Error) => { + this.metrics.increment('broker.consumer_error'); + this.logger.warn( + `Broker consumer channel error: ${error.message}`, + ); + this.handleConsumerClose(channel); + }); + channel.on('close', () => this.handleConsumerClose(channel)); + await channel.prefetch(this.prefetch); + await assertTopology(channel, this.deliveryLimit); + if (this.handler !== handler) { + await channel.close(); + return; + } + const consumer = await channel.consume( + JOB_QUEUE, + (message) => { + if (!message) { + // basic.cancel from the broker: the worker must not stay + // healthy while deaf. + this.handleConsumerClose(channel); + void channel.close().catch(() => undefined); + return; + } + handler(message, channel).catch((error: unknown) => { + this.metrics.increment('broker.handler_error'); + this.logger.error( + `Job handler crashed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + // Hold the message briefly instead of burning the + // quorum delivery limit in a tight nack loop. + setTimeout(() => { + try { + channel.nack(message, false, true); + } catch { + // The channel is gone; the broker redelivers after the lease. + } + }, 1000); + }); + }, + { noAck: false }, + ); + this.consumeChannel = channel; + this.consumerTag = consumer.consumerTag; + this.subscribeDelayMs = 1000; + } catch (error) { + try { + await channel.close(); + } catch { + // The channel is already unusable. + } + throw error; + } + } + + private handleConsumerClose(channel: Channel): void { + if (this.consumeChannel && this.consumeChannel !== channel) return; + this.consumeChannel = undefined; + this.consumerTag = undefined; + if (!this.closing && this.handler) this.scheduleSubscribe(); + } + + private handleClose(): void { + this.metrics.increment('broker.closed'); + clearTimeout(this.subscribeTimer); + this.subscribeTimer = undefined; + this.connection = undefined; + this.confirmChannel = undefined; + this.consumeChannel = undefined; + this.consumerTag = undefined; + for (const reject of this.returned.values()) reject(); + this.returned.clear(); + if (this.closing) return; + this.scheduleReconnect(); + } + + private scheduleReconnect(): void { + if (this.closing || this.reconnectTimer) return; + const delay = this.reconnectDelayMs; + this.reconnectDelayMs = Math.min( + this.reconnectDelayMs * 2, + MAX_RECONNECT_DELAY_MS, + ); + this.metrics.increment('broker.reconnect_scheduled'); + this.reconnectTimer = setTimeout(() => { + this.reconnectTimer = undefined; + this.ensureConnected() + .then(() => this.subscribe()) + .catch(() => this.scheduleReconnect()); + }, delay); + } + + private scheduleSubscribe(): void { + if (this.closing || !this.handler || this.subscribeTimer) return; + const delay = this.subscribeDelayMs; + this.subscribeDelayMs = Math.min( + this.subscribeDelayMs * 2, + MAX_RECONNECT_DELAY_MS, + ); + this.metrics.increment('broker.subscribe_scheduled'); + this.subscribeTimer = setTimeout(() => { + this.subscribeTimer = undefined; + this.ensureConnected() + .then(() => this.subscribe()) + .catch(() => this.scheduleSubscribe()); + }, delay); + } + + private async withTimeout(promise: Promise, ms: number): Promise { + const { promise: timeout, reject } = Promise.withResolvers(); + const timer = setTimeout(() => reject(new BrokerUnavailableError()), ms); + try { + return await Promise.race([promise, timeout]); + } finally { + clearTimeout(timer); + } + } +} diff --git a/app/src/jobs/broker.topology.ts b/app/src/jobs/broker.topology.ts new file mode 100644 index 00000000..6921d74e --- /dev/null +++ b/app/src/jobs/broker.topology.ts @@ -0,0 +1,37 @@ +import type { Channel } from 'amqplib'; + +export const JOB_EXCHANGE = 'attachments.jobs'; +export const JOB_ROUTING_KEY = 'attachment.process.v1'; +export const JOB_QUEUE = 'attachments.process'; +export const DEAD_LETTER_EXCHANGE = 'attachments.dlx'; +export const DEAD_LETTER_QUEUE = 'attachments.process.dlq'; + +/** + * Idempotent declaration used by both the producer and the consumer on every + * (re)connect: a stand started from an empty volume and a reconnect after a + * broker restart converge to the same durable topology. + */ +export async function assertTopology( + channel: Channel, + deliveryLimit: number, +): Promise { + await channel.assertExchange(DEAD_LETTER_EXCHANGE, 'fanout', { + durable: true, + }); + await channel.assertQueue(DEAD_LETTER_QUEUE, { + durable: true, + arguments: { 'x-queue-type': 'quorum' }, + }); + await channel.bindQueue(DEAD_LETTER_QUEUE, DEAD_LETTER_EXCHANGE, ''); + + await channel.assertExchange(JOB_EXCHANGE, 'direct', { durable: true }); + await channel.assertQueue(JOB_QUEUE, { + durable: true, + arguments: { + 'x-queue-type': 'quorum', + 'x-dead-letter-exchange': DEAD_LETTER_EXCHANGE, + 'x-delivery-limit': deliveryLimit, + }, + }); + await channel.bindQueue(JOB_QUEUE, JOB_EXCHANGE, JOB_ROUTING_KEY); +} diff --git a/app/src/jobs/errors.spec.ts b/app/src/jobs/errors.spec.ts new file mode 100644 index 00000000..6c37c3f2 --- /dev/null +++ b/app/src/jobs/errors.spec.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from 'vitest'; +import { ObjectStorageUnavailableError } from '../storage/object-storage.js'; +import { classifyError, JobTimeoutError } from './errors.js'; + +describe('classifyError', () => { + it('classifies unsupported sharp input as permanent invalid_image', () => { + expect( + classifyError( + new Error('Input buffer contains unsupported image format'), + ), + ).toEqual({ kind: 'permanent', code: 'invalid_image' }); + }); + + it('classifies the pixel limit as permanent image_too_large', () => { + expect( + classifyError(new Error('Input image exceeds pixel limit')), + ).toEqual({ kind: 'permanent', code: 'image_too_large' }); + }); + + it('classifies object storage outages as transient', () => { + expect(classifyError(new ObjectStorageUnavailableError())).toEqual({ + kind: 'transient', + code: 'processing_failed', + }); + }); + + it('classifies a job timeout as transient processing_timeout', () => { + expect(classifyError(new JobTimeoutError(10))).toEqual({ + kind: 'transient', + code: 'processing_timeout', + }); + }); +}); diff --git a/app/src/jobs/errors.ts b/app/src/jobs/errors.ts new file mode 100644 index 00000000..9c121e12 --- /dev/null +++ b/app/src/jobs/errors.ts @@ -0,0 +1,65 @@ +import { SERVER_FAULT } from '../attachments/image/image-decoder.js'; +import { InjectedFaultError } from './fault-injector.js'; + +export class SourceMissingError extends Error { + constructor(message = 'Source object is missing') { + super(message); + this.name = 'SourceMissingError'; + } +} + +export class SourceMismatchError extends Error { + constructor(message = 'Source object does not match its metadata') { + super(message); + this.name = 'SourceMismatchError'; + } +} + +export class UnsupportedPipelineError extends Error { + constructor(version: number) { + super(`Unsupported pipeline version: ${version}`); + this.name = 'UnsupportedPipelineError'; + } +} + +export class JobTimeoutError extends Error { + constructor(seconds: number) { + super(`Job exceeded ${seconds}s deadline`); + this.name = 'JobTimeoutError'; + } +} + +export type ErrorClass = { + kind: 'permanent' | 'transient'; + code: string; +}; + +const SHARP_INPUT = + /unsupported image format|input buffer|input file|bad seek|premature end|vipsjpeg|vippng|corrupt|truncated|invalid (?:image|pixel)|not a valid image|exceeded.*limit/iu; + +/** + * Maps a processing failure to the contract's safe error class. Permanent + * errors are never retried; transient ones are retried until max_attempts, + * after which the job is failed as `retry_exhausted`. + */ +export function classifyError(error: unknown): ErrorClass { + if (error instanceof InjectedFaultError) + return { kind: error.kind, code: 'injected_fault' }; + if (error instanceof SourceMissingError) + return { kind: 'permanent', code: 'source_missing' }; + if (error instanceof SourceMismatchError) + return { kind: 'permanent', code: 'source_mismatch' }; + if (error instanceof UnsupportedPipelineError) + return { kind: 'permanent', code: 'unsupported_pipeline' }; + if (error instanceof JobTimeoutError) + return { kind: 'transient', code: 'processing_timeout' }; + + const message = error instanceof Error ? error.message : String(error); + if (/pixel limit/iu.test(message)) + return { kind: 'permanent', code: 'image_too_large' }; + if (SERVER_FAULT.test(message)) + return { kind: 'transient', code: 'processing_failed' }; + if (SHARP_INPUT.test(message)) + return { kind: 'permanent', code: 'invalid_image' }; + return { kind: 'transient', code: 'processing_failed' }; +} diff --git a/app/src/jobs/fault-injector.ts b/app/src/jobs/fault-injector.ts new file mode 100644 index 00000000..bcc41204 --- /dev/null +++ b/app/src/jobs/fault-injector.ts @@ -0,0 +1,134 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { RedisClient } from '../redis/redis.client.js'; + +export type FaultPoint = + | 'api.after_commit_before_publish' + | 'relay.after_publish_before_mark' + | 'worker.before_side_effect' + | 'worker.after_side_effect' + | 'worker.after_commit_before_ack'; + +export type FaultTarget = { + attachmentId: string; + pipelineVersion: number; +}; + +export type FaultKind = 'permanent' | 'transient'; + +// Node timers overflow above 2^31-1 ms and fire immediately. +const MAX_TIMER_MS = 2_147_483_647; + +export class InjectedFaultError extends Error { + constructor(readonly kind: FaultKind) { + super(`Injected ${kind} fault`); + this.name = 'InjectedFaultError'; + } +} + +/** + * Active only with FAULT_INJECTION_ENABLED=1. The switch is deliberately + * checked before every Redis call so a normal stand never depends on Redis for + * job correctness. + * + * `transient` stays until the key is deleted; every other value is consumed by + * the first point that observes it. Worker points are addressed to one + * attachment and pipeline version; the untargeted key is a fallback. + */ +@Injectable() +export class FaultInjector { + private readonly logger = new Logger(FaultInjector.name); + private readonly enabled: boolean; + + constructor( + config: ConfigService, + private readonly redis: RedisClient, + private readonly metrics: MetricsService, + ) { + this.enabled = config.getOrThrow('FAULT_INJECTION_ENABLED') === 1; + } + + async point( + name: FaultPoint, + target?: FaultTarget, + signal?: AbortSignal, + ): Promise { + if (!this.enabled) return; + const keys = target + ? [ + `fault:${name}:${target.attachmentId}:${target.pipelineVersion}`, + `fault:${name}`, + ] + : [`fault:${name}`]; + for (const key of keys) { + let value: string | null; + let deleted: number; + try { + value = await this.redis.get(key); + if (value === null) continue; + if (value === 'transient') { + this.metrics.increment('faults.triggered'); + throw new InjectedFaultError('transient'); + } + deleted = await this.redis.del(key); + } catch (error) { + if (error instanceof InjectedFaultError) throw error; + this.logger.warn( + `Fault point ${name} could not read Redis: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + return; + } + if (deleted !== 1) continue; + // The action is outside the Redis try/catch: an abort or an injected + // fault must reach the job processor, not be logged as an outage. + await this.act(value, signal); + return; + } + } + + private async act(value: string, signal?: AbortSignal): Promise { + if (value === 'crash') { + this.metrics.increment('faults.triggered'); + // PID 1 in a container namespace may not be killable by SIGKILL; + // exit 137 gives the same observable crash (no graceful shutdown). + process.kill(process.pid, 'SIGKILL'); + process.exit(137); + } + if (value === 'permanent') { + this.metrics.increment('faults.triggered'); + throw new InjectedFaultError('permanent'); + } + if (value.startsWith('delay:')) { + const milliseconds = Number(value.slice('delay:'.length)); + if (!Number.isFinite(milliseconds) || milliseconds < 0) return; + this.metrics.increment('faults.triggered'); + await this.sleep(Math.min(milliseconds, MAX_TIMER_MS), signal); + } + } + + private sleep(milliseconds: number, signal?: AbortSignal): Promise { + if (signal?.aborted) + return Promise.reject( + signal.reason instanceof Error + ? signal.reason + : new Error('Aborted'), + ); + const { promise, resolve, reject } = Promise.withResolvers(); + const timer = setTimeout(() => { + signal?.removeEventListener('abort', cancel); + resolve(); + }, milliseconds); + const cancel = (): void => { + clearTimeout(timer); + reject( + signal?.reason instanceof Error + ? signal.reason + : new Error('Aborted'), + ); + }; + signal?.addEventListener('abort', cancel, { once: true }); + return promise; + } +} diff --git a/app/src/jobs/fit-inside.spec.ts b/app/src/jobs/fit-inside.spec.ts new file mode 100644 index 00000000..a2cbb07b --- /dev/null +++ b/app/src/jobs/fit-inside.spec.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from 'vitest'; +import { fitInside } from './fit-inside.js'; + +describe('fitInside', () => { + it.each([ + [320, 200, 256, [256, 160]], + [1000, 335, 256, [256, 85]], + [100, 50, 256, [100, 50]], + [10000, 1, 256, [256, 1]], + [1, 10000, 256, [1, 256]], + ])('fits %ix%i into %i as %j', (width, height, box, expected) => { + expect(fitInside(width, height, box)).toEqual(expected); + }); +}); diff --git a/app/src/jobs/fit-inside.ts b/app/src/jobs/fit-inside.ts new file mode 100644 index 00000000..cff80c6c --- /dev/null +++ b/app/src/jobs/fit-inside.ts @@ -0,0 +1,15 @@ +/** + * Fits `width`x`height` into a `box`x`box` square without upscaling. + * Integer arithmetic keeps the result exact for the contract's fixtures: + * 320x200 -> 256x160, 1000x335 -> 256x85, 10000x1 -> 256x1. + */ +export function fitInside( + width: number, + height: number, + box: number, +): [number, number] { + if (width <= box && height <= box) return [width, height]; + if (width >= height) + return [box, Math.max(1, Math.floor((height * box) / width))]; + return [Math.max(1, Math.floor((width * box) / height)), box]; +} diff --git a/app/src/jobs/job-message.spec.ts b/app/src/jobs/job-message.spec.ts new file mode 100644 index 00000000..4016bd06 --- /dev/null +++ b/app/src/jobs/job-message.spec.ts @@ -0,0 +1,47 @@ +import { readFileSync } from 'node:fs'; +import { describe, expect, it } from 'vitest'; +import { parseJobMessage } from './job-message.js'; + +const contract = JSON.parse( + readFileSync( + new URL('../../../contracts/jobs.schema.json', import.meta.url), + 'utf8', + ), +) as { examples: Record[] }; +const example = contract.examples[0]; + +describe('parseJobMessage', () => { + it('accepts the contract example', () => { + expect(parseJobMessage(Buffer.from(JSON.stringify(example)))).toEqual( + example, + ); + }); + + it('rejects an extra property', () => { + expect( + parseJobMessage( + Buffer.from(JSON.stringify({ ...example, extra: true })), + ), + ).toBeUndefined(); + }); + + it('rejects attempt 0', () => { + expect( + parseJobMessage( + Buffer.from(JSON.stringify({ ...example, attempt: 0 })), + ), + ).toBeUndefined(); + }); + + it('rejects a wrong type', () => { + expect( + parseJobMessage( + Buffer.from(JSON.stringify({ ...example, type: 'other' })), + ), + ).toBeUndefined(); + }); + + it('rejects non-JSON', () => { + expect(parseJobMessage(Buffer.from('not-json'))).toBeUndefined(); + }); +}); diff --git a/app/src/jobs/job-message.ts b/app/src/jobs/job-message.ts new file mode 100644 index 00000000..2e9c724d --- /dev/null +++ b/app/src/jobs/job-message.ts @@ -0,0 +1,53 @@ +import { z } from 'zod'; + +export const JOB_TYPE = 'attachment.process.v1'; + +/** + * Mirrors contracts/jobs.schema.json. The broker message carries identifiers + * and scheduling data only: no bytes, URLs, file names or credentials. + */ +export const jobMessageSchema = z.strictObject({ + schema_version: z.literal(1), + job_id: z.uuid(), + type: z.literal(JOB_TYPE), + attachment_id: z.uuid(), + pipeline_version: z.number().int().min(1), + attempt: z.number().int().min(1), + created_at: z.iso.datetime(), + traceparent: z + .string() + .regex(/^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$/u) + .optional(), + tracestate: z.string().max(512).optional(), +}); + +export type JobMessage = z.infer; + +export function buildJobMessage(job: { + job_id: string; + attachment_id: string; + pipeline_version: number; + attempt: number; + created_at: Date; +}): JobMessage { + return { + schema_version: 1, + job_id: job.job_id, + type: JOB_TYPE, + attachment_id: job.attachment_id, + pipeline_version: job.pipeline_version, + attempt: job.attempt, + created_at: job.created_at.toISOString(), + }; +} + +export function parseJobMessage(content: Buffer): JobMessage | undefined { + let value: unknown; + try { + value = JSON.parse(content.toString('utf8')); + } catch { + return undefined; + } + const parsed = jobMessageSchema.safeParse(value); + return parsed.success ? parsed.data : undefined; +} diff --git a/app/src/jobs/jobs.cli.ts b/app/src/jobs/jobs.cli.ts new file mode 100644 index 00000000..c931582f --- /dev/null +++ b/app/src/jobs/jobs.cli.ts @@ -0,0 +1,234 @@ +import { Module } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { NestFactory } from '@nestjs/core'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { AppConfigModule } from '../config/app-config.module.js'; +import { DatabaseModule } from '../database/database.module.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { ObjectStorage } from '../storage/object-storage.js'; +import { StorageModule } from '../storage/storage.module.js'; +import { DEAD_LETTER_QUEUE, JOB_QUEUE } from './broker.topology.js'; +import { BrokerClient } from './broker.client.js'; +import { JobsModule } from './jobs.module.js'; +import { JobsRepository } from './jobs.repository.js'; + +@Module({ + imports: [ + AppConfigModule, + DatabaseModule, + RedisModule, + MetricsModule, + StorageModule, + JobsModule, + ], +}) +class JobsCliModule {} + +const SUPPORTED_PIPELINES = [1, 2]; + +function option(args: string[], name: string): string | undefined { + const index = args.indexOf(name); + if (index === -1) return undefined; + return args[index + 1]; +} + +async function readStdin(): Promise { + const chunks: Buffer[] = []; + for await (const chunk of process.stdin) + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); + return Buffer.concat(chunks).toString('utf8'); +} + +async function s3State( + storage: ObjectStorage, + key: string, +): Promise<{ + exists: boolean; + etag: string | null; + last_modified: string | null; +}> { + const head = await storage.head(key); + return head + ? { + exists: true, + etag: head.etag ?? null, + last_modified: head.lastModified?.toISOString() ?? null, + } + : { exists: false, etag: null, last_modified: null }; +} + +async function main(): Promise { + const args = process.argv.slice(2); + const command = args[0]; + const app = await NestFactory.createApplicationContext(JobsCliModule, { + logger: false, + }); + try { + const repository = app.get(JobsRepository); + const storage = app.get(ObjectStorage); + const broker = app.get(BrokerClient); + const config = app.get(ConfigService); + + if (command === 'status') { + const attachmentId = args[1]; + if (!attachmentId) return usage(); + const status = await repository.statusOf(attachmentId); + if (!status) { + console.log(JSON.stringify({ error: 'attachment_not_found' })); + return 1; + } + const variants = []; + for (const variant of status.variants) { + variants.push({ + pipeline_version: variant.pipeline_version, + variant: variant.variant, + sha256: variant.sha256, + size_bytes: Number(variant.size_bytes), + width: variant.width, + height: variant.height, + object_key: variant.object_key, + s3: await s3State(storage, variant.object_key), + }); + } + console.log( + JSON.stringify({ + attachment: { + id: status.attachment.id, + status: status.attachment.status, + error_code: status.attachment.error_code, + ready_pipeline_version: + status.attachment.ready_pipeline_version, + staging_present: status.attachment.staging_key !== null, + }, + jobs: status.jobs.map((job) => ({ + job_id: job.job_id, + pipeline_version: job.pipeline_version, + status: job.status, + attempt: job.attempt, + max_attempts: job.max_attempts, + last_error_code: job.last_error_code, + })), + variants, + original: await s3State( + storage, + status.attachment.object_key, + ), + events: status.events.map((event) => ({ + job_id: event.job_id, + event: event.event, + attempt: event.attempt, + detail: event.detail, + created_at: event.created_at.toISOString(), + })), + outbox: status.outbox, + }), + ); + return 0; + } + + if (command === 'find-by-chat') { + const chatId = args[1]; + if (!chatId) return usage(); + console.log(JSON.stringify(await repository.findByChat(chatId))); + return 0; + } + + if (command === 'failed') { + console.log(JSON.stringify(await repository.failedJobs())); + return 0; + } + + if (command === 'replay') { + const attachmentId = args[1]; + const reason = option(args, '--reason') ?? 'operator replay'; + const operator = + option(args, '--operator') ?? process.env.USER ?? 'operator'; + if (!attachmentId) return usage(); + const candidate = await repository.replayCandidate(attachmentId); + let sourceAvailable = false; + if (candidate) { + sourceAvailable = + candidate.staging_key !== null || + (await storage.head(storage.objectKey(attachmentId))) !== null; + } + const result = await repository.replay(attachmentId, { + reason, + operator, + maxAttempts: config.getOrThrow('JOB_MAX_ATTEMPTS'), + sourceAvailable, + expectedStagingKey: candidate?.staging_key ?? null, + }); + console.log(JSON.stringify(result)); + return result.kind === 'replayed' ? 0 : 3; + } + + if (command === 'reprocess') { + const attachmentId = args[1]; + const rawVersion = option(args, '--pipeline-version'); + const version = Number(rawVersion); + if ( + !attachmentId || + rawVersion === undefined || + !SUPPORTED_PIPELINES.includes(version) + ) + return usage(); + const result = await repository.reprocess(attachmentId, version, { + reason: option(args, '--reason') ?? 'reprocess', + operator: + option(args, '--operator') ?? + process.env.USER ?? + 'operator', + maxAttempts: config.getOrThrow('JOB_MAX_ATTEMPTS'), + }); + console.log(JSON.stringify(result)); + return result.kind === 'created' ? 0 : 3; + } + + if (command === 'publish-raw') { + if (config.getOrThrow('FAULT_INJECTION_ENABLED') !== 1) { + console.log( + JSON.stringify({ refused: 'fault_injection_disabled' }), + ); + return 3; + } + const text = await readStdin(); + const lines = text.split(/\r?\n/u).filter((line) => line !== ''); + for (const line of lines) await broker.publish(Buffer.from(line)); + console.log(JSON.stringify({ published: lines.length })); + return 0; + } + + if (command === 'queues') { + console.log( + JSON.stringify({ + process: await broker.queueDepth(JOB_QUEUE), + dlq: await broker.queueDepth(DEAD_LETTER_QUEUE), + }), + ); + return 0; + } + + return usage(); + } finally { + await app.close(); + } +} + +function usage(): number { + console.error( + 'usage: jobs.cli.js status | find-by-chat | failed | replay --reason [--operator ] | reprocess --pipeline-version [--reason ] | publish-raw | queues', + ); + return 2; +} + +main() + .then((code) => process.exit(code)) + .catch((error: unknown) => { + console.log( + JSON.stringify({ + error: error instanceof Error ? error.message : 'unknown error', + }), + ); + process.exit(1); + }); diff --git a/app/src/jobs/jobs.module.ts b/app/src/jobs/jobs.module.ts new file mode 100644 index 00000000..26e251d8 --- /dev/null +++ b/app/src/jobs/jobs.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { DatabaseModule } from '../database/database.module.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { BrokerClient } from './broker.client.js'; +import { FaultInjector } from './fault-injector.js'; +import { JobsRepository } from './jobs.repository.js'; + +@Module({ + imports: [DatabaseModule, RedisModule, MetricsModule], + providers: [BrokerClient, JobsRepository, FaultInjector], + exports: [BrokerClient, JobsRepository, FaultInjector], +}) +export class JobsModule {} diff --git a/app/src/jobs/jobs.repository.ts b/app/src/jobs/jobs.repository.ts new file mode 100644 index 00000000..6307ef50 --- /dev/null +++ b/app/src/jobs/jobs.repository.ts @@ -0,0 +1,1043 @@ +import { Injectable } from '@nestjs/common'; +import { randomUUID } from 'node:crypto'; +import type { PoolClient } from 'pg'; +import type { AttachmentRow, VariantRow } from '../attachments/attachment.js'; +import { DatabaseClient } from '../database/database.client.js'; +import { buildJobMessage, type JobMessage } from './job-message.js'; + +export type JobStatus = + 'queued' | 'processing' | 'succeeded' | 'failed' | 'superseded'; + +export type AttachmentJobRow = { + job_id: string; + attachment_id: string; + pipeline_version: number; + status: JobStatus; + attempt: number; + max_attempts: number; + next_attempt_at: Date; + lease_owner: string | null; + lease_token: string | null; + lease_until: Date | null; + last_error_code: string | null; + created_at: Date; + updated_at: Date; + finished_at: Date | null; +}; + +export type VariantWrite = { + name: 'thumbnail' | 'crop' | 'optimized'; + objectKey: string; + contentType: 'image/jpeg' | 'image/png' | 'image/webp'; + sizeBytes: number; + width: number; + height: number; + sha256: string; +}; + +export type ClaimResult = + | { kind: 'skip'; reason: string } + | { + kind: 'claimed'; + job: AttachmentJobRow; + attachment: AttachmentRow; + leaseToken: string; + }; + +export type CommitFailureResult = 'retry' | 'failed' | 'lease_lost'; +export type CommitSuccessResult = 'succeeded' | 'superseded' | 'lease_lost'; + +export type ReplayResult = + | { kind: 'replayed'; job_id: string; attempt: number } + | { kind: 'refused'; reason: string; job_id?: string }; + +export type ReprocessResult = + | { kind: 'created'; job_id: string } + | { kind: 'refused'; reason: string; job_id?: string }; + +@Injectable() +export class JobsRepository { + constructor(private readonly database: DatabaseClient) {} + + async acceptUpload( + attachmentId: string, + fields: { + sizeBytes: number; + sha256: string; + width: number; + height: number; + }, + pipelineVersion: number, + maxAttempts: number, + ): Promise { + return this.database.withTransaction(async (client) => { + const accepted = await client.query( + `UPDATE attachments + SET status = 'queued', size_bytes = $2, sha256 = $3, + width = $4, height = $5, expires_at = NULL, updated_at = now() + WHERE id = $1 AND status = 'pending' + RETURNING *`, + [ + attachmentId, + fields.sizeBytes, + fields.sha256, + fields.width, + fields.height, + ], + ); + const attachment = accepted.rows[0]; + if (!attachment) return undefined; + + const jobId = randomUUID(); + const created = await client.query( + `INSERT INTO attachment_jobs + (job_id, attachment_id, pipeline_version, status, attempt, + max_attempts, next_attempt_at) + VALUES ($1, $2, $3, 'queued', 1, $4, now()) + RETURNING *`, + [jobId, attachmentId, pipelineVersion, maxAttempts], + ); + const job = created.rows[0]; + await client.query( + `INSERT INTO job_outbox (job_id, payload, available_at) + VALUES ($1, $2::jsonb, now())`, + [ + job.job_id, + JSON.stringify( + buildJobMessage({ + ...job, + created_at: job.created_at, + }), + ), + ], + ); + await this.event(client, job.job_id, 'created', 1, {}); + return attachment; + }); + } + + async claim( + message: JobMessage, + workerId: string, + leaseSeconds: number, + ): Promise { + return this.database.withTransaction(async (client) => { + // Lock order is always job -> attachment, matching commitSuccess and + // commitFailure. A join FOR UPDATE would let the planner choose the + // opposite order under a concurrent duplicate delivery. + const foundJob = await client.query< + AttachmentJobRow & { due: boolean } + >( + `SELECT *, (next_attempt_at <= now()) AS due + FROM attachment_jobs + WHERE job_id = $1 + FOR UPDATE`, + [message.job_id], + ); + const job = foundJob.rows[0]; + if (!job) return { kind: 'skip', reason: 'unknown_job' }; + + const foundAttachment = await client.query( + `SELECT * FROM attachments WHERE id = $1 FOR UPDATE`, + [job.attachment_id], + ); + const attachment = foundAttachment.rows[0]; + + if ( + job.attachment_id !== message.attachment_id || + job.pipeline_version !== message.pipeline_version + ) + return this.skip(client, message, 'mismatch'); + + if (job.status === 'succeeded') + return this.skip(client, message, 'already_succeeded'); + if (job.status === 'failed') + return this.skip(client, message, 'already_failed'); + if (job.status === 'superseded') + return this.skip(client, message, 'superseded'); + if (job.status === 'processing') + return this.skip(client, message, 'in_progress'); + if (job.attempt !== message.attempt) + return this.skip(client, message, 'stale_attempt'); + // Both `next_attempt_at` and `now()` come from PostgreSQL; a worker + // clock skew must not turn a future retry into a lost job. + if (!job.due) + return this.skip(client, message, 'not_due'); + + if ( + attachment.ready_pipeline_version !== null && + attachment.ready_pipeline_version > job.pipeline_version + ) { + await client.query( + `UPDATE attachment_jobs + SET status = 'superseded', updated_at = now(), finished_at = now() + WHERE job_id = $1`, + [job.job_id], + ); + await this.event( + client, + job.job_id, + 'superseded', + job.attempt, + { + ready_pipeline_version: + attachment.ready_pipeline_version, + }, + ); + return this.skip(client, message, 'superseded'); + } + + const leaseToken = randomUUID(); + const claimed = await client.query( + `UPDATE attachment_jobs + SET status = 'processing', lease_owner = $2, lease_token = $3, + lease_until = now() + make_interval(secs => $4::double precision), + last_reconciled_at = NULL, + updated_at = now() + WHERE job_id = $1 + RETURNING *`, + [job.job_id, workerId, leaseToken, leaseSeconds], + ); + await client.query( + `UPDATE attachments SET status = 'processing', updated_at = now() + WHERE id = $1 AND status = 'queued'`, + [job.attachment_id], + ); + await this.event( + client, + job.job_id, + 'claimed', + claimed.rows[0].attempt, + { lease_owner: workerId }, + ); + return { + kind: 'claimed', + job: claimed.rows[0], + attachment, + leaseToken, + }; + }); + } + + async extendLease( + jobId: string, + token: string, + leaseSeconds: number, + ): Promise { + const result = await this.database.query( + 'jobs.extend_lease', + `UPDATE attachment_jobs + SET lease_until = now() + make_interval(secs => $3::double precision), + updated_at = now() + WHERE job_id = $1 AND lease_token = $2 AND status = 'processing'`, + [jobId, token, leaseSeconds], + ); + return result.rowCount === 1; + } + + async commitSuccess( + jobId: string, + token: string, + variants: VariantWrite[], + ): Promise { + return this.database.withTransaction(async (client) => { + const succeeded = await client.query( + `UPDATE attachment_jobs + SET status = 'succeeded', lease_owner = NULL, lease_token = NULL, + lease_until = NULL, updated_at = now(), finished_at = now() + WHERE job_id = $1 AND lease_token = $2 AND status = 'processing' + RETURNING *`, + [jobId, token], + ); + const job = succeeded.rows[0]; + if (!job) return 'lease_lost'; + + const attachment = await client.query( + `SELECT * FROM attachments WHERE id = $1 FOR UPDATE`, + [job.attachment_id], + ); + const row = attachment.rows[0]; + if ( + row.ready_pipeline_version !== null && + row.ready_pipeline_version > job.pipeline_version + ) { + await client.query( + `UPDATE attachment_jobs + SET status = 'superseded', updated_at = now() + WHERE job_id = $1`, + [job.job_id], + ); + await this.event( + client, + job.job_id, + 'superseded', + job.attempt, + { + ready_pipeline_version: row.ready_pipeline_version, + }, + ); + return 'superseded'; + } + + for (const variant of variants) { + await client.query( + `INSERT INTO attachment_variants + (attachment_id, pipeline_version, variant, object_key, + content_type, size_bytes, width, height, sha256, job_id) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + ON CONFLICT (attachment_id, pipeline_version, variant) + DO UPDATE SET object_key = EXCLUDED.object_key, + content_type = EXCLUDED.content_type, + size_bytes = EXCLUDED.size_bytes, + width = EXCLUDED.width, + height = EXCLUDED.height, + sha256 = EXCLUDED.sha256, + job_id = EXCLUDED.job_id`, + [ + job.attachment_id, + job.pipeline_version, + variant.name, + variant.objectKey, + variant.contentType, + variant.sizeBytes, + variant.width, + variant.height, + variant.sha256, + job.job_id, + ], + ); + } + await client.query( + `UPDATE attachments + SET status = 'ready', ready_pipeline_version = $2, + error_code = NULL, updated_at = now() + WHERE id = $1`, + [job.attachment_id, job.pipeline_version], + ); + await this.event(client, job.job_id, 'succeeded', job.attempt, {}); + return 'succeeded'; + }); + } + + async commitFailure( + jobId: string, + token: string, + failure: { kind: 'permanent' | 'transient'; code: string }, + delayMs: number, + ): Promise { + return this.database.withTransaction(async (client) => { + const found = await client.query( + `SELECT * FROM attachment_jobs + WHERE job_id = $1 AND lease_token = $2 AND status = 'processing' + FOR UPDATE`, + [jobId, token], + ); + const job = found.rows[0]; + if (!job) return 'lease_lost'; + const finalCode = + failure.kind === 'permanent' || job.attempt >= job.max_attempts + ? failure.kind === 'transient' + ? 'retry_exhausted' + : failure.code + : failure.code; + + if ( + failure.kind === 'permanent' || + job.attempt >= job.max_attempts + ) { + await client.query( + `UPDATE attachment_jobs + SET status = 'failed', last_error_code = $2, + lease_owner = NULL, lease_token = NULL, lease_until = NULL, + updated_at = now(), finished_at = now() + WHERE job_id = $1`, + [job.job_id, finalCode], + ); + await client.query( + `UPDATE attachments + SET status = 'failed', error_code = $2, updated_at = now() + WHERE id = $1 AND status <> 'ready'`, + [job.attachment_id, finalCode], + ); + await this.event(client, job.job_id, 'failed', job.attempt, { + code: finalCode, + cause: failure.code, + }); + return 'failed'; + } + + const nextAttempt = job.attempt + 1; + await client.query( + `UPDATE attachment_jobs + SET status = 'queued', attempt = $2, + next_attempt_at = now() + make_interval(secs => $3::double precision), + lease_owner = NULL, lease_token = NULL, lease_until = NULL, + last_error_code = $4, last_reconciled_at = NULL, + updated_at = now() + WHERE job_id = $1`, + [job.job_id, nextAttempt, delayMs / 1000, failure.code], + ); + await client.query( + `UPDATE attachments SET status = 'queued', updated_at = now() + WHERE id = $1 AND status = 'processing'`, + [job.attachment_id], + ); + await client.query( + `INSERT INTO job_outbox (job_id, payload, available_at) + VALUES ($1, $2::jsonb, + now() + make_interval(secs => $3::double precision))`, + [ + job.job_id, + JSON.stringify( + buildJobMessage({ ...job, attempt: nextAttempt }), + ), + delayMs / 1000, + ], + ); + await this.event( + client, + job.job_id, + 'retry_scheduled', + nextAttempt, + { + code: failure.code, + delay_ms: delayMs, + }, + ); + return 'retry'; + }); + } + + async reapExpiredLeases( + limit: number, + delayFor: (attempt: number) => number, + ): Promise { + return this.database.withTransaction(async (client) => { + const expired = await client.query( + `SELECT * FROM attachment_jobs + WHERE status = 'processing' AND lease_until < now() + ORDER BY lease_until + LIMIT $1 + FOR UPDATE SKIP LOCKED`, + [limit], + ); + for (const job of expired.rows) { + // Keep the same lock order as claim/commit: job, then attachment. + await client.query( + `SELECT 1 FROM attachments WHERE id = $1 FOR UPDATE`, + [job.attachment_id], + ); + if (job.attempt >= job.max_attempts) { + await client.query( + `UPDATE attachment_jobs + SET status = 'failed', last_error_code = 'retry_exhausted', + lease_owner = NULL, lease_token = NULL, + lease_until = NULL, updated_at = now(), + finished_at = now() + WHERE job_id = $1`, + [job.job_id], + ); + await client.query( + `UPDATE attachments + SET status = 'failed', error_code = 'retry_exhausted', + updated_at = now() + WHERE id = $1 AND status <> 'ready'`, + [job.attachment_id], + ); + await this.event( + client, + job.job_id, + 'lease_expired', + job.attempt, + { + lease_owner: job.lease_owner, + code: 'retry_exhausted', + }, + ); + await this.event( + client, + job.job_id, + 'failed', + job.attempt, + { + code: 'retry_exhausted', + cause: 'lease_expired', + }, + ); + continue; + } + + const nextAttempt = job.attempt + 1; + const delayMs = delayFor(job.attempt); + await client.query( + `UPDATE attachment_jobs + SET status = 'queued', attempt = $2, + next_attempt_at = now() + make_interval(secs => $3::double precision), + lease_owner = NULL, lease_token = NULL, + lease_until = NULL, last_error_code = 'lease_expired', + last_reconciled_at = NULL, + updated_at = now() + WHERE job_id = $1`, + [job.job_id, nextAttempt, delayMs / 1000], + ); + await client.query( + `UPDATE attachments SET status = 'queued', updated_at = now() + WHERE id = $1 AND status = 'processing'`, + [job.attachment_id], + ); + await client.query( + `INSERT INTO job_outbox (job_id, payload, available_at) + VALUES ($1, $2::jsonb, + now() + make_interval(secs => $3::double precision))`, + [ + job.job_id, + JSON.stringify( + buildJobMessage({ ...job, attempt: nextAttempt }), + ), + delayMs / 1000, + ], + ); + await this.event( + client, + job.job_id, + 'lease_expired', + job.attempt, + { lease_owner: job.lease_owner }, + ); + } + return expired.rows.length; + }); + } + + /** + * Backstop for a queued job whose broker message was lost or dead-lettered + * while the outbox row was already marked published. Re-emitting is safe: + * claim fences duplicates by job status and attempt. + */ + async reconcileQueuedJobs( + limit: number, + graceSeconds: number, + ): Promise { + return this.database.withTransaction(async (client) => { + const stuck = await client.query( + `SELECT j.* FROM attachment_jobs j + WHERE j.status = 'queued' + AND j.next_attempt_at <= now() + AND COALESCE( + (SELECT max(o.published_at) FROM job_outbox o + WHERE o.job_id = j.job_id AND o.published_at IS NOT NULL), + j.updated_at) <= now() - make_interval(secs => $2::double precision) + AND (j.last_reconciled_at IS NULL + OR j.last_reconciled_at <= now() - make_interval(secs => $2::double precision)) + AND NOT EXISTS ( + SELECT 1 FROM job_outbox o + WHERE o.job_id = j.job_id AND o.published_at IS NULL) + ORDER BY j.next_attempt_at + LIMIT $1 + FOR UPDATE SKIP LOCKED`, + [limit, graceSeconds], + ); + for (const job of stuck.rows) { + await client.query( + `UPDATE attachment_jobs SET last_reconciled_at = now(), updated_at = now() + WHERE job_id = $1`, + [job.job_id], + ); + await client.query( + `INSERT INTO job_outbox (job_id, payload, available_at) + VALUES ($1, $2::jsonb, now())`, + [job.job_id, JSON.stringify(buildJobMessage(job))], + ); + await this.event( + client, + job.job_id, + 'reconciled', + job.attempt, + { code: 'queue_message_missing' }, + ); + } + return stuck.rows.length; + }); + } + + async publishPending( + limit: number, + publish: (message: JobMessage) => Promise, + afterPublish: () => Promise, + ): Promise { + return this.database.withTransaction(async (client) => { + const pending = await client.query<{ + id: string; + payload: JobMessage; + }>( + `SELECT id, payload FROM job_outbox + WHERE published_at IS NULL AND available_at <= now() + ORDER BY available_at, id + LIMIT $1 + FOR UPDATE SKIP LOCKED`, + [limit], + ); + const published: string[] = []; + let failure: unknown; + for (const row of pending.rows) { + try { + await publish(row.payload); + published.push(row.id); + } catch (error) { + failure = error; + break; + } + } + if (published.length === 0) { + if (failure) throw failure; + return 0; + } + await afterPublish(); + await client.query( + `UPDATE job_outbox SET published_at = now() WHERE id = ANY($1::bigint[])`, + [published], + ); + if (failure) throw failure; + return published.length; + }); + } + + async variantsFor( + attachmentId: string, + pipelineVersion: number, + ): Promise { + const result = await this.database.query( + 'jobs.variants', + `SELECT * FROM attachment_variants + WHERE attachment_id = $1 AND pipeline_version = $2 + ORDER BY array_position(ARRAY['thumbnail','crop','optimized']::text[], variant)`, + [attachmentId, pipelineVersion], + ); + return result.rows; + } + + async stagingCleanupCandidates( + retentionSeconds: number, + limit: number, + ): Promise<{ id: string; staging_key: string }[]> { + const result = await this.database.query<{ + id: string; + staging_key: string; + }>( + 'jobs.staging_candidates', + `SELECT id, staging_key FROM attachments + WHERE staging_key IS NOT NULL + AND ((status = 'ready' AND updated_at < now() - interval '1 minute') + OR (status = 'failed' AND updated_at < now() - make_interval(secs => $1::double precision))) + ORDER BY updated_at + LIMIT $2`, + [retentionSeconds, limit], + ); + return result.rows; + } + + async clearStaging( + id: string, + key: string, + removeObject: (key: string) => Promise, + ): Promise { + return this.database.withTransaction(async (client) => { + // Keep the same job -> attachment lock order as claim/replay, then + // serialize cleanup with replay on the attachment row. + await client.query( + `SELECT job_id FROM attachment_jobs + WHERE attachment_id = $1 + ORDER BY created_at DESC + LIMIT 1 + FOR UPDATE`, + [id], + ); + const locked = await client.query( + `SELECT * FROM attachments WHERE id = $1 FOR UPDATE`, + [id], + ); + const row = locked.rows[0]; + if ( + !row || + row.staging_key !== key || + !['ready', 'failed'].includes(row.status) + ) + return false; + await removeObject(key); + await client.query( + `UPDATE attachments SET staging_key = NULL, updated_at = now() + WHERE id = $1`, + [id], + ); + await client.query( + `INSERT INTO job_events (job_id, event, attempt, detail) + SELECT job_id, 'staging_deleted', attempt, $2::jsonb + FROM attachment_jobs + WHERE attachment_id = $1 + ORDER BY created_at DESC + LIMIT 1`, + [id, JSON.stringify({ staging_key: key })], + ); + return true; + }); + } + + async pruneOutbox( + retentionSeconds: number, + limit = 5000, + ): Promise { + const result = await this.database.query( + 'jobs.outbox_prune', + `DELETE FROM job_outbox + WHERE id IN ( + SELECT id FROM job_outbox + WHERE published_at IS NOT NULL + AND published_at < now() - make_interval(secs => $1::double precision) + ORDER BY published_at + LIMIT $2)`, + [retentionSeconds, limit], + ); + return result.rowCount ?? 0; + } + + async pruneEvents( + retentionSeconds: number, + limit = 5000, + ): Promise { + const result = await this.database.query( + 'jobs.events_prune', + `DELETE FROM job_events + WHERE id IN ( + SELECT id FROM job_events + WHERE created_at < now() - make_interval(secs => $1::double precision) + ORDER BY created_at + LIMIT $2)`, + [retentionSeconds, limit], + ); + return result.rowCount ?? 0; + } + + async statusOf(attachmentId: string): Promise< + | { + attachment: AttachmentRow; + jobs: AttachmentJobRow[]; + variants: VariantRow[]; + events: { + job_id: string; + event: string; + attempt: number; + detail: unknown; + created_at: Date; + }[]; + outbox: { pending: number; published: number }; + } + | undefined + > { + const attachment = await this.database.query( + 'jobs.status_attachment', + `SELECT * FROM attachments WHERE id = $1`, + [attachmentId], + ); + if (!attachment.rows[0]) return undefined; + const jobs = await this.database.query( + 'jobs.status_jobs', + `SELECT * FROM attachment_jobs WHERE attachment_id = $1 ORDER BY created_at`, + [attachmentId], + ); + const variants = await this.database.query( + 'jobs.status_variants', + `SELECT * FROM attachment_variants WHERE attachment_id = $1 + ORDER BY pipeline_version, + array_position(ARRAY['thumbnail','crop','optimized']::text[], variant)`, + [attachmentId], + ); + const events = await this.database.query<{ + job_id: string; + event: string; + attempt: number; + detail: unknown; + created_at: Date; + }>( + 'jobs.status_events', + `SELECT e.job_id, e.event, e.attempt, e.detail, e.created_at + FROM job_events e + JOIN attachment_jobs j ON j.job_id = e.job_id + WHERE j.attachment_id = $1 + ORDER BY e.id`, + [attachmentId], + ); + const outbox = await this.database.query<{ + pending: string; + published: string; + }>( + 'jobs.status_outbox', + `SELECT count(*) FILTER (WHERE o.published_at IS NULL) AS pending, + count(*) FILTER (WHERE o.published_at IS NOT NULL) AS published + FROM job_outbox o + JOIN attachment_jobs j ON j.job_id = o.job_id + WHERE j.attachment_id = $1`, + [attachmentId], + ); + return { + attachment: attachment.rows[0], + jobs: jobs.rows, + variants: variants.rows, + events: events.rows, + outbox: { + pending: Number(outbox.rows[0]?.pending ?? 0), + published: Number(outbox.rows[0]?.published ?? 0), + }, + }; + } + + async findByChat( + chatId: string, + ): Promise<{ id: string; status: string; created_at: Date }[]> { + const result = await this.database.query<{ + id: string; + status: string; + created_at: Date; + }>( + 'jobs.find_by_chat', + `SELECT id, status, created_at FROM attachments + WHERE chat_id = $1 ORDER BY created_at`, + [chatId], + ); + return result.rows; + } + + async failedJobs(): Promise< + { + attachment_id: string; + job_id: string; + pipeline_version: number; + attempt: number; + last_error_code: string | null; + updated_at: Date; + }[] + > { + const result = await this.database.query<{ + attachment_id: string; + job_id: string; + pipeline_version: number; + attempt: number; + last_error_code: string | null; + updated_at: Date; + }>( + 'jobs.failed', + `SELECT attachment_id, job_id, pipeline_version, attempt, + last_error_code, updated_at + FROM attachment_jobs WHERE status = 'failed' + ORDER BY updated_at`, + ); + return result.rows; + } + + async replayCandidate(attachmentId: string): Promise< + | { + job_id: string; + pipeline_version: number; + staging_key: string | null; + } + | undefined + > { + const result = await this.database.query<{ + job_id: string; + pipeline_version: number; + staging_key: string | null; + }>( + 'jobs.replay_candidate', + `SELECT j.job_id, j.pipeline_version, a.staging_key + FROM attachment_jobs j + JOIN attachments a ON a.id = j.attachment_id + WHERE j.attachment_id = $1 AND j.status = 'failed' + ORDER BY j.updated_at DESC + LIMIT 1`, + [attachmentId], + ); + return result.rows[0]; + } + + async replay( + attachmentId: string, + options: { + reason: string; + operator: string; + maxAttempts: number; + sourceAvailable: boolean; + expectedStagingKey: string | null; + }, + ): Promise { + return this.database.withTransaction(async (client) => { + const found = await client.query( + `SELECT * FROM attachment_jobs + WHERE attachment_id = $1 AND status = 'failed' + ORDER BY updated_at DESC + LIMIT 1 + FOR UPDATE`, + [attachmentId], + ); + const job = found.rows[0]; + if (!job) return { kind: 'refused', reason: 'no_failed_job' }; + + const attachment = await client.query( + `SELECT * FROM attachments WHERE id = $1 FOR UPDATE`, + [attachmentId], + ); + const row = attachment.rows[0]; + if ( + row.status === 'ready' && + row.ready_pipeline_version !== null && + row.ready_pipeline_version >= job.pipeline_version + ) + return { + kind: 'refused', + reason: 'already_ready', + job_id: job.job_id, + }; + + const active = await client.query( + `SELECT 1 FROM attachment_jobs + WHERE attachment_id = $1 AND status IN ('queued','processing') + LIMIT 1`, + [attachmentId], + ); + if (active.rowCount !== 0) + return { + kind: 'refused', + reason: 'job_active', + job_id: job.job_id, + }; + + if (row.staging_key !== options.expectedStagingKey) + return { + kind: 'refused', + reason: 'source_changed', + job_id: job.job_id, + }; + if (row.staging_key === null && !options.sourceAvailable) + return { + kind: 'refused', + reason: 'source_expired', + job_id: job.job_id, + }; + + const attempt = job.attempt + 1; + await client.query( + `UPDATE attachment_jobs + SET status = 'queued', attempt = $2, + max_attempts = $2::integer + $3::integer - 1, + next_attempt_at = now(), lease_owner = NULL, + lease_token = NULL, lease_until = NULL, + last_reconciled_at = NULL, + updated_at = now(), finished_at = NULL + WHERE job_id = $1`, + [job.job_id, attempt, options.maxAttempts], + ); + await client.query( + `UPDATE attachments SET status = 'queued', error_code = NULL, + updated_at = now() + WHERE id = $1 AND status = 'failed'`, + [attachmentId], + ); + await client.query( + `INSERT INTO job_outbox (job_id, payload, available_at) + VALUES ($1, $2::jsonb, now())`, + [ + job.job_id, + JSON.stringify(buildJobMessage({ ...job, attempt })), + ], + ); + await this.event(client, job.job_id, 'replayed', attempt, { + operator: options.operator, + reason: options.reason, + previous_error: job.last_error_code, + }); + return { kind: 'replayed', job_id: job.job_id, attempt }; + }); + } + + async reprocess( + attachmentId: string, + pipelineVersion: number, + options: { reason: string; operator: string; maxAttempts: number }, + ): Promise { + return this.database.withTransaction(async (client) => { + const attachment = await client.query( + `SELECT * FROM attachments WHERE id = $1 FOR UPDATE`, + [attachmentId], + ); + const row = attachment.rows[0]; + if ( + !row || + !['queued', 'processing', 'ready', 'failed'].includes( + row.status, + ) + ) + return { kind: 'refused', reason: 'not_accepted' }; + + const jobId = randomUUID(); + const created = await client.query( + `INSERT INTO attachment_jobs + (job_id, attachment_id, pipeline_version, status, attempt, + max_attempts, next_attempt_at) + VALUES ($1, $2, $3, 'queued', 1, $4, now()) + ON CONFLICT (attachment_id, pipeline_version) DO NOTHING + RETURNING *`, + [jobId, attachmentId, pipelineVersion, options.maxAttempts], + ); + const job = created.rows[0]; + if (!job) { + const existing = await client.query<{ job_id: string }>( + `SELECT job_id FROM attachment_jobs + WHERE attachment_id = $1 AND pipeline_version = $2`, + [attachmentId, pipelineVersion], + ); + return { + kind: 'refused', + reason: 'job_exists', + job_id: existing.rows[0]?.job_id, + }; + } + await client.query( + `INSERT INTO job_outbox (job_id, payload, available_at) + VALUES ($1, $2::jsonb, now())`, + [job.job_id, JSON.stringify(buildJobMessage(job))], + ); + await this.event(client, job.job_id, 'created', 1, { + reason: options.reason, + operator: options.operator, + }); + return { kind: 'created', job_id: job.job_id }; + }); + } + + private async skip( + client: PoolClient, + message: JobMessage, + reason: string, + ): Promise { + await this.event( + client, + message.job_id, + 'duplicate_skipped', + message.attempt, + { reason, message_attempt: message.attempt }, + ); + return { kind: 'skip', reason }; + } + + private async event( + client: PoolClient, + jobId: string, + event: string, + attempt: number, + detail: Record, + ): Promise { + await client.query( + `INSERT INTO job_events (job_id, event, attempt, detail) + VALUES ($1, $2, $3, $4::jsonb)`, + [jobId, event, attempt, JSON.stringify(detail)], + ); + } +} diff --git a/app/src/jobs/outbox-relay.service.ts b/app/src/jobs/outbox-relay.service.ts new file mode 100644 index 00000000..9fd99f17 --- /dev/null +++ b/app/src/jobs/outbox-relay.service.ts @@ -0,0 +1,110 @@ +import { + Injectable, + Logger, + OnApplicationBootstrap, + OnModuleDestroy, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { BrokerClient } from './broker.client.js'; +import { FaultInjector } from './fault-injector.js'; +import { JobsRepository } from './jobs.repository.js'; + +const BATCH_SIZE = 20; + +/** + * Runs in the API and worker processes. `publishPending` holds one transaction around + * the broker confirms and the `published_at` update; a crash before the mark + * rolls the transaction back and republishes the same message after restart. + * That duplicate is expected: the worker fences it on job status. + */ +@Injectable() +export class OutboxRelay implements OnApplicationBootstrap, OnModuleDestroy { + private readonly logger = new Logger(OutboxRelay.name); + private timer: NodeJS.Timeout | undefined; + private running = false; + private again = false; + private current: Promise | undefined; + private failing = false; + private retryAt = 0; + private retryDelayMs = 1000; + + constructor( + private readonly config: ConfigService, + private readonly repository: JobsRepository, + private readonly broker: BrokerClient, + private readonly faults: FaultInjector, + private readonly metrics: MetricsService, + ) {} + + onApplicationBootstrap(): void { + const interval = this.config.getOrThrow('OUTBOX_POLL_INTERVAL_MS'); + this.timer = setInterval(() => this.kick(), interval); + this.timer.unref(); + this.kick(); + } + + async onModuleDestroy(): Promise { + clearInterval(this.timer); + this.timer = undefined; + await this.current; + } + + kick(): void { + if (Date.now() < this.retryAt) return; + if (this.running) { + this.again = true; + return; + } + this.running = true; + this.current = this.drain() + .catch((error: unknown) => { + this.metrics.increment('outbox.publish_failed'); + this.retryAt = Date.now() + this.retryDelayMs; + this.retryDelayMs = Math.min(this.retryDelayMs * 2, 10_000); + if (!this.failing) { + this.failing = true; + this.logger.warn( + `Outbox publish failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + }) + .finally(() => { + this.running = false; + this.current = undefined; + if (this.again) { + this.again = false; + this.kick(); + } + }); + } + + private async drain(): Promise { + const budgetMs = Math.max( + 1000, + Math.floor( + this.config.getOrThrow('DATABASE_IDLE_TX_TIMEOUT_MS') / 2, + ), + ); + const started = Date.now(); + const published = await this.repository.publishPending( + BATCH_SIZE, + async (message) => { + if (Date.now() - started > budgetMs) + throw new Error('outbox batch deadline exceeded'); + await this.broker.publish(message); + }, + () => this.faults.point('relay.after_publish_before_mark'), + ); + if (published > 0) { + this.metrics.add('outbox.published', published); + this.retryAt = 0; + this.retryDelayMs = 1000; + if (this.failing) { + this.failing = false; + this.logger.log('Outbox publishing recovered'); + } + } + } +} diff --git a/app/src/main.ts b/app/src/main.ts new file mode 100644 index 00000000..8c4a615c --- /dev/null +++ b/app/src/main.ts @@ -0,0 +1,96 @@ +import { NestFactory } from '@nestjs/core'; +import { HttpAdapterHost } from '@nestjs/core'; +import { ConfigService } from '@nestjs/config'; +import { ValidationPipe } from '@nestjs/common'; +import type { UploadRequest } from './attachments/upload-request.js'; +import { + FastifyAdapter, + NestFastifyApplication, +} from '@nestjs/platform-fastify'; +import { AppModule } from './app.module.js'; +import { HealthService } from './health/health.service.js'; +import { MetricsService } from './metrics/metrics.service.js'; +import { RequestIdInterceptor } from './common/http/request-id.interceptor.js'; + +const UPLOAD_ROUTE = '/api/v1/chats/:chatId/attachments'; +const UPLOAD_STREAM_TYPE = 'application/x-messenger-upload-stream'; + +async function bootstrap(): Promise { + const app = await NestFactory.create( + AppModule, + new FastifyAdapter({ logger: true }), + ); + // The upload route receives its body as an unread stream whatever the + // declared Content-Type: the service checks the type itself, after the + // auth guard (401 before 415, contracts/README.md), and bounds the body by + // its own limits, not Fastify's bodyLimit. The declared type is kept on the + // request; all other routes keep Fastify's parsers untouched. + const fastify = app.getHttpAdapter().getInstance(); + fastify.addHook( + 'onRequest', + (request: UploadRequest, _reply: unknown, done: () => void) => { + if ( + request.method === 'POST' && + request.routeOptions?.url === UPLOAD_ROUTE + ) { + request.uploadContentType = request.headers['content-type']; + request.headers['content-type'] = UPLOAD_STREAM_TYPE; + } + done(); + }, + ); + fastify.addContentTypeParser( + UPLOAD_STREAM_TYPE, + ( + _request: unknown, + payload: unknown, + done: (error: Error | null, body?: unknown) => void, + ) => done(null, payload), + ); + app.useGlobalInterceptors( + new RequestIdInterceptor( + app.get(HttpAdapterHost), + app.get(MetricsService), + app.get(ConfigService), + ), + ); + app.useGlobalPipes( + new ValidationPipe({ + whitelist: true, + forbidNonWhitelisted: true, + transform: true, + }), + ); + + const config = app.get(ConfigService); + const port = config.getOrThrow('APP_PORT'); + const host = config.getOrThrow('APP_HOST'); + await app.listen(port, host); + + // Drain before teardown. Nest's own shutdown sequence destroys providers + // (closing the database pool and Redis) before `beforeApplicationShutdown` + // runs, so relying on it would keep the replica listed in the load balancer + // while it can no longer serve: SIGTERM must first turn the replica + // not-ready for the LB probe, then wait DRAIN_DELAY_MS, and only then close + // the server and the pools. + const health = app.get(HealthService); + const drainDelayMs = config.getOrThrow('DRAIN_DELAY_MS'); + let shuttingDown = false; + const shutdown = async (): Promise => { + if (shuttingDown) return; + shuttingDown = true; + health.setDraining(); + if (drainDelayMs > 0) + await new Promise((resolve) => + setTimeout(resolve, drainDelayMs), + ); + await app.close(); + process.exit(0); + }; + for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP'] as const) + process.once(signal, () => { + void shutdown(); + }); +} + +await bootstrap(); diff --git a/app/src/messages/cache/messages-cache.metrics.spec.ts b/app/src/messages/cache/messages-cache.metrics.spec.ts new file mode 100644 index 00000000..13da19e1 --- /dev/null +++ b/app/src/messages/cache/messages-cache.metrics.spec.ts @@ -0,0 +1,37 @@ +import { ConfigService } from '@nestjs/config'; +import { describe, expect, it, vi } from 'vitest'; +import { MetricsService } from '../../metrics/metrics.service.js'; +import { MessagesCacheService } from './messages-cache.service.js'; + +describe('MessagesCacheService metrics', () => { + it('records a miss and a hit for a protected page fill', async () => { + const metrics = new MetricsService(); + const page = { items: [], next_cursor: null }; + const redis = { + get: vi + .fn() + .mockResolvedValueOnce(null) + .mockResolvedValueOnce(null) + .mockResolvedValue(JSON.stringify(page)), + setNxPx: vi.fn().mockResolvedValue(true), + set: vi.fn().mockResolvedValue('OK'), + unlock: vi.fn().mockResolvedValue(1), + }; + const config = new ConfigService({ + CACHE_MODE: 'protected', + CACHE_LOCK_TTL_MS: 1000, + CACHE_TTL_SECONDS: 30, + }); + const service = new MessagesCacheService( + redis as never, + config, + metrics, + ); + await service.getOrLoad('chat', 1n, undefined, 50, async () => page); + await service.getOrLoad('chat', 1n, undefined, 50, async () => page); + expect(metrics.snapshot().counters).toMatchObject({ + cache_miss: 1, + cache_hit: 1, + }); + }); +}); diff --git a/app/src/messages/cache/messages-cache.module.ts b/app/src/messages/cache/messages-cache.module.ts new file mode 100644 index 00000000..f1cfddfe --- /dev/null +++ b/app/src/messages/cache/messages-cache.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { MessagesCacheService } from './messages-cache.service.js'; +import { MetricsModule } from '../../metrics/metrics.module.js'; +import { RedisModule } from '../../redis/redis.module.js'; + +@Module({ + imports: [RedisModule, MetricsModule], + providers: [MessagesCacheService], + exports: [MessagesCacheService], +}) +export class MessagesCacheModule {} diff --git a/app/src/messages/cache/messages-cache.service.spec.ts b/app/src/messages/cache/messages-cache.service.spec.ts new file mode 100644 index 00000000..c4053491 --- /dev/null +++ b/app/src/messages/cache/messages-cache.service.spec.ts @@ -0,0 +1,72 @@ +import { ConfigService } from '@nestjs/config'; +import { describe, expect, it, vi } from 'vitest'; +import type { AppConfig } from '../../config/app-config.schema.js'; +import type { RedisClient } from '../../redis/redis.client.js'; +import { MessagesCacheService } from './messages-cache.service.js'; +import { MetricsService } from '../../metrics/metrics.service.js'; + +function serviceWithRedis(redis: Partial): MessagesCacheService { + const config = new ConfigService({ + CACHE_MODE: 'protected', + CACHE_LOCK_TTL_MS: 1000, + CACHE_TTL_SECONDS: 30, + CACHE_MISS_DELAY_MS: 0, + }); + return new MessagesCacheService( + redis as RedisClient, + config, + new MetricsService(), + ); +} + +describe('MessagesCacheService fallback', () => { + it('loads from the authoritative source when Redis read fails', async () => { + const load = vi + .fn() + .mockResolvedValue({ items: [{ id: 'db' }], next_cursor: null }); + const service = serviceWithRedis({ + get: vi.fn().mockRejectedValue(new Error('redis down')), + }); + + await expect( + service.getOrLoad('chat', 1n, undefined, 50, load), + ).resolves.toEqual({ + items: [{ id: 'db' }], + next_cursor: null, + }); + expect(load).toHaveBeenCalledOnce(); + }); + + it('falls back after bounded lock contention', async () => { + const load = vi + .fn() + .mockResolvedValue({ items: [{ id: 'db' }], next_cursor: null }); + const service = serviceWithRedis({ + get: vi.fn().mockResolvedValue(null), + setNxPx: vi.fn().mockResolvedValue(false), + }); + + await expect( + service.getOrLoad('chat', 1n, undefined, 50, load), + ).resolves.toEqual({ + items: [{ id: 'db' }], + next_cursor: null, + }); + expect(load).toHaveBeenCalledOnce(); + }); + + it('returns the authoritative page when cache write fails', async () => { + const page = { items: [{ id: 'db' }], next_cursor: null }; + const load = vi.fn().mockResolvedValue(page); + const service = serviceWithRedis({ + get: vi.fn().mockResolvedValue(null), + setNxPx: vi.fn().mockResolvedValue(true), + set: vi.fn().mockRejectedValue(new Error('redis down')), + unlock: vi.fn().mockResolvedValue(1), + }); + + await expect( + service.getOrLoad('chat', 1n, undefined, 50, load), + ).resolves.toEqual(page); + }); +}); diff --git a/app/src/messages/cache/messages-cache.service.ts b/app/src/messages/cache/messages-cache.service.ts new file mode 100644 index 00000000..565c17c4 --- /dev/null +++ b/app/src/messages/cache/messages-cache.service.ts @@ -0,0 +1,205 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { setTimeout as delay } from 'node:timers/promises'; +import type { AppConfig } from '../../config/app-config.schema.js'; +import { RedisClient } from '../../redis/redis.client.js'; +import type { Page } from '../../common/http/page.js'; +import { MetricsService } from '../../metrics/metrics.service.js'; + +const LOCK_RETRY_COUNT = 4; +const LOCK_RETRY_DELAY_MS = 25; + +export type CachePage = Page; + +@Injectable() +export class MessagesCacheService { + private readonly logger = new Logger(MessagesCacheService.name); + + constructor( + private readonly redis: RedisClient, + private readonly config: ConfigService, + private readonly metrics: MetricsService, + ) {} + + async getOrLoad( + chatId: string, + version: bigint, + cursor: string | undefined, + limit: number, + loader: () => Promise, + ): Promise { + const key = this.cacheKey(chatId, version, cursor, limit); + if (this.config.getOrThrow('CACHE_MODE') === 'naive') { + return this.loadNaively(key, loader); + } + + let cached: string | null; + try { + cached = await this.redis.get(key); + } catch (error) { + return this.fallbackToDatabase('read', error, loader); + } + const cachedPage = this.parsePage(cached); + if (cachedPage) { + this.metrics.increment('cache_hit'); + return cachedPage; + } + this.metrics.increment('cache_miss'); + + const lockKey = `lock:${key}`; + const owner = `${process.pid}:${Math.random().toString(36).slice(2)}`; + for (let attempt = 0; attempt < LOCK_RETRY_COUNT; attempt += 1) { + let locked: boolean; + try { + locked = await this.redis.setNxPx( + lockKey, + owner, + this.config.getOrThrow('CACHE_LOCK_TTL_MS'), + ); + } catch (error) { + return this.fallbackToDatabase('lock', error, loader); + } + if (locked) return this.fillLocked(key, lockKey, owner, loader); + + if (attempt === 0) this.metrics.increment('cache_lock_wait'); + try { + const waitedPage = this.parsePage(await this.redis.get(key)); + if (waitedPage) { + this.metrics.increment('cache_hit'); + return waitedPage; + } + } catch (error) { + return this.fallbackToDatabase('waiting read', error, loader); + } + await delay(LOCK_RETRY_DELAY_MS); + } + + // The owner did not publish the page within the bounded wait: serve the + // authoritative result instead of blocking the request further. + this.metrics.increment('cache_lock_timeout'); + return loader(); + } + + private async fillLocked( + key: string, + lockKey: string, + owner: string, + loader: () => Promise, + ): Promise { + try { + let secondCheck: string | null; + try { + secondCheck = await this.redis.get(key); + } catch (error) { + return this.fallbackToDatabase('double-check', error, loader); + } + const secondPage = this.parsePage(secondCheck); + if (secondPage) { + this.metrics.increment('cache_hit'); + return secondPage; + } + + const page = await loader(); + try { + await this.redis.set(key, JSON.stringify(page), { + EX: this.config.getOrThrow('CACHE_TTL_SECONDS'), + }); + } catch (error) { + // Redis is derived state: the page is still valid for this response, + // but the failure must stay visible in metrics and logs. + this.metrics.increment('cache_write_error'); + this.logger.warn( + `Cache fill failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + return page; + } finally { + try { + await this.redis.unlock(lockKey, owner); + } catch (error) { + // The lock has a TTL, so a stale lock expires on its own; a successful + // response must not be replaced by an unlock failure. + this.metrics.increment('cache_unlock_error'); + this.logger.warn( + `Cache unlock failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + } + } + + private cacheKey( + chatId: string, + version: bigint, + cursor: string | undefined, + limit: number, + ): string { + return `messages:${chatId}:v${version.toString()}:c${cursor ?? 'start'}:l${limit}`; + } + + private async loadNaively( + key: string, + loader: () => Promise, + ): Promise { + let cached: string | null; + try { + cached = await this.redis.get(key); + } catch (error) { + return this.fallbackToDatabase('naive read', error, loader); + } + const cachedPage = this.parsePage(cached); + if (cachedPage) { + this.metrics.increment('cache_hit'); + return cachedPage; + } + this.metrics.increment('cache_miss'); + + const cacheMissDelay = this.config.getOrThrow('CACHE_MISS_DELAY_MS'); + if (cacheMissDelay > 0) await delay(cacheMissDelay); + const page = await loader(); + try { + await this.redis.set(key, JSON.stringify(page), { + EX: this.config.getOrThrow('CACHE_TTL_SECONDS'), + }); + } catch (error) { + this.metrics.increment('cache_write_error'); + this.logger.warn( + `Cache fill failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + return page; + } + + private async fallbackToDatabase( + stage: string, + error: unknown, + loader: () => Promise, + ): Promise { + this.metrics.increment('cache_fallback'); + this.logger.warn( + `Cache ${stage} failed, serving PostgreSQL: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + return loader(); + } + + private parsePage(value: string | null): CachePage | undefined { + if (!value) return undefined; + try { + const parsed: unknown = JSON.parse(value); + if ( + typeof parsed !== 'object' || + parsed === null || + !('items' in parsed) || + !('next_cursor' in parsed) || + !Array.isArray(parsed.items) || + (parsed.next_cursor !== null && + typeof parsed.next_cursor !== 'string') + ) + return undefined; + return { items: parsed.items, next_cursor: parsed.next_cursor }; + } catch { + // A malformed entry is treated as a miss and overwritten by the owner fill. + this.metrics.increment('cache_corrupt_entry'); + return undefined; + } + } +} diff --git a/app/src/messages/dto/create-message.dto.ts b/app/src/messages/dto/create-message.dto.ts new file mode 100644 index 00000000..0a8b8d2a --- /dev/null +++ b/app/src/messages/dto/create-message.dto.ts @@ -0,0 +1,23 @@ +import { + ArrayMaxSize, + ArrayUnique, + IsArray, + IsString, + IsUUID, + ValidateIf, +} from 'class-validator'; +import { CodePointLength } from '../../common/validation/code-point-length.js'; + +export class CreateMessageDto { + @IsString() + @CodePointLength(0, 4000) + text!: string; + + // Absent means []; null is a client error. + @ValidateIf((_, value) => value !== undefined) + @IsArray() + @ArrayMaxSize(10) + @ArrayUnique() + @IsUUID('all', { each: true }) + attachment_ids: string[] = []; +} diff --git a/app/src/messages/idempotency/idempotency-gc.cli.ts b/app/src/messages/idempotency/idempotency-gc.cli.ts new file mode 100644 index 00000000..4e473bcc --- /dev/null +++ b/app/src/messages/idempotency/idempotency-gc.cli.ts @@ -0,0 +1,33 @@ +import { fileURLToPath } from 'node:url'; +import { parseDatabaseConfig } from '../../config/app-config.schema.js'; +import { DatabaseClient } from '../../database/database.client.js'; +import { MetricsService } from '../../metrics/metrics.service.js'; +import { IdempotencyRepository } from './idempotency.repository.js'; + +// Manual operator pass over the idempotency ledger: the API also purges expired +// claims in the background, but an operator must be able to reclaim space +// without waiting for traffic. It never touches unexpired claims. +// in the stand: docker compose exec api node dist/messages/idempotency/idempotency-gc.cli.js +// on the host: npm run idempotency:gc (needs a reachable DATABASE_URL) +async function main(): Promise { + const config = parseDatabaseConfig(process.env as Record); + const database = new DatabaseClient( + { + getOrThrow: (key: keyof typeof config) => config[key], + } as never, + new MetricsService(), + ); + try { + const repository = new IdempotencyRepository(database); + const purged = await repository.purgeExpired( + config.IDEMPOTENCY_TTL_SECONDS, + ); + process.stdout.write( + `Purged ${purged} idempotency key(s) older than ${config.IDEMPOTENCY_TTL_SECONDS} seconds\n`, + ); + } finally { + await database.onModuleDestroy(); + } +} + +if (process.argv[1] === fileURLToPath(import.meta.url)) await main(); diff --git a/app/src/messages/idempotency/idempotency.module.ts b/app/src/messages/idempotency/idempotency.module.ts new file mode 100644 index 00000000..33c8a329 --- /dev/null +++ b/app/src/messages/idempotency/idempotency.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { DatabaseModule } from '../../database/database.module.js'; +import { IdempotencyRepository } from './idempotency.repository.js'; + +@Module({ + imports: [DatabaseModule], + providers: [IdempotencyRepository], + exports: [IdempotencyRepository], +}) +export class IdempotencyModule {} diff --git a/app/src/messages/idempotency/idempotency.repository.spec.ts b/app/src/messages/idempotency/idempotency.repository.spec.ts new file mode 100644 index 00000000..056876a4 --- /dev/null +++ b/app/src/messages/idempotency/idempotency.repository.spec.ts @@ -0,0 +1,24 @@ +import { describe, expect, it, vi } from 'vitest'; +import { IdempotencyRepository } from './idempotency.repository.js'; + +describe('IdempotencyRepository expired-key cleanup', () => { + it('deletes only rows older than the configured TTL', async () => { + const query = vi.fn().mockResolvedValue({ rowCount: 0, rows: [] }); + const repository = new IdempotencyRepository({ query } as never); + + await repository.forget( + 'user', + '/api/v1/chats/chat/messages', + 'key', + 86400, + ); + + expect(query).toHaveBeenCalledWith( + 'idempotency.forget', + expect.stringContaining( + 'created_at < now() - make_interval(secs => $4::double precision)', + ), + ['user', '/api/v1/chats/chat/messages', 'key', 86400], + ); + }); +}); diff --git a/app/src/messages/idempotency/idempotency.repository.ts b/app/src/messages/idempotency/idempotency.repository.ts new file mode 100644 index 00000000..badd7364 --- /dev/null +++ b/app/src/messages/idempotency/idempotency.repository.ts @@ -0,0 +1,119 @@ +import { Injectable } from '@nestjs/common'; +import pg from 'pg'; +import { DatabaseClient } from '../../database/database.client.js'; +import type { Message } from '../message.js'; + +// A committed row always carries the response: the claim and the stored response +// are written by one statement, so there is no poisoned intermediate state to +// re-check at replay time. +export type IdempotencyRecord = { + request_fingerprint: string; + status_code: number; + response_body: Message; + created_at: Date | string; + expired: boolean; + is_member: boolean; +}; + +const PURGE_BATCH_LIMIT = 1000; +const PURGE_BATCHES = 10; + +@Injectable() +export class IdempotencyRepository { + constructor(private readonly database: DatabaseClient) {} + + /** + * Reads the stored result and re-checks access in the same round trip: a + * replay must still prove that the caller may read this chat. + */ + async read( + userId: string, + path: string, + key: string, + chatId: string, + ttlSeconds: number, + ): Promise { + const result = await this.database.query( + 'idempotency.read', + `SELECT k.request_fingerprint, k.status_code, k.response_body, k.created_at, + k.created_at < now() - make_interval(secs => $4::double precision) AS expired, + EXISTS ( + SELECT 1 FROM chat_members WHERE chat_id = $5 AND user_id = $1 + ) AS is_member + FROM idempotency_keys k + WHERE k.user_id = $1 AND k.method = 'POST' AND k.path = $2 AND k.key = $3`, + [userId, path, key, ttlSeconds, chatId], + ); + return result.rows[0]; + } + + async forget( + userId: string, + path: string, + key: string, + ttlSeconds: number, + ): Promise { + await this.database.query( + 'idempotency.forget', + `DELETE FROM idempotency_keys + WHERE user_id = $1 AND method = 'POST' AND path = $2 AND key = $3 + AND created_at < now() - make_interval(secs => $4::double precision)`, + [userId, path, key, ttlSeconds], + ); + } + + async isMember(chatId: string, userId: string): Promise { + const result = await this.database.query<{ is_member: boolean }>( + 'messages.checkMembership', + `SELECT EXISTS ( + SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2 + ) AS is_member`, + [chatId, userId], + ); + return result.rows[0]?.is_member === true; + } + + /** + * Deletes expired claims in bounded batches. The transaction-scoped advisory + * lock makes the pass single-flight across replicas without risking a lock + * leaking back into the pool with a released connection. Returns the number + * of deleted rows (0 when another replica is purging). + */ + async purgeExpired(ttlSeconds: number): Promise { + const client: pg.PoolClient = await this.database.pool.connect(); + try { + await client.query('BEGIN'); + try { + const lock = await client.query<{ locked: boolean }>( + "SELECT pg_try_advisory_xact_lock(hashtext('idempotency_gc')) AS locked", + ); + if (lock.rows[0]?.locked !== true) { + await client.query('ROLLBACK'); + return 0; + } + let purged = 0; + for (let batch = 0; batch < PURGE_BATCHES; batch += 1) { + const result = await client.query( + `DELETE FROM idempotency_keys + WHERE ctid IN ( + SELECT ctid FROM idempotency_keys + WHERE created_at < now() - make_interval(secs => $1::double precision) + LIMIT ${PURGE_BATCH_LIMIT} + )`, + [ttlSeconds], + ); + const deleted = result.rowCount ?? 0; + purged += deleted; + if (deleted < PURGE_BATCH_LIMIT) break; + } + await client.query('COMMIT'); + return purged; + } catch (error) { + await client.query('ROLLBACK').catch(() => undefined); + throw error; + } + } finally { + client.release(); + } + } +} diff --git a/app/src/messages/idempotency/idempotency.ts b/app/src/messages/idempotency/idempotency.ts new file mode 100644 index 00000000..7c4f922f --- /dev/null +++ b/app/src/messages/idempotency/idempotency.ts @@ -0,0 +1,26 @@ +import { createHash } from 'node:crypto'; + +// Contract: ASCII [A-Za-z0-9._:-]{1,128}. Anything else (including a duplicated +// header, which Fastify joins with ", ") is rejected as a client error. +export const IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/; + +export function messagesPath(chatId: string): string { + return `/api/v1/chats/${chatId}/messages`; +} + +// The fingerprint is derived from the validated payload with a fixed field +// order, so neither JSON key order nor an absent optional field can change it. +// Text-only messages keep the lab3 fingerprint; the order of attachment ids +// is significant because it is the order of attachments in the message. +export function idempotencyFingerprint( + text: string, + attachmentIds: string[], +): string { + return createHash('sha256') + .update( + attachmentIds.length === 0 + ? JSON.stringify({ text }) + : JSON.stringify({ text, attachment_ids: attachmentIds }), + ) + .digest('hex'); +} diff --git a/app/src/messages/message.ts b/app/src/messages/message.ts new file mode 100644 index 00000000..cd6c1788 --- /dev/null +++ b/app/src/messages/message.ts @@ -0,0 +1,8 @@ +export type Message = { + id: string; + chat_id: string; + sender_id: string; + text: string; + created_at: string; + attachment_ids: string[]; +}; diff --git a/app/src/messages/messages.controller.ts b/app/src/messages/messages.controller.ts new file mode 100644 index 00000000..a4fcdb14 --- /dev/null +++ b/app/src/messages/messages.controller.ts @@ -0,0 +1,53 @@ +import { + BadRequestException, + Body, + Controller, + DefaultValuePipe, + Get, + Headers, + Param, + ParseIntPipe, + ParseUUIDPipe, + Post, + Query, + UseGuards, +} from '@nestjs/common'; +import { CurrentUser } from '../common/auth/current-user.decorator.js'; +import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; +import type { User } from '../users/user.js'; +import { CreateMessageDto } from './dto/create-message.dto.js'; +import { MessagesService } from './messages.service.js'; + +@Controller('api/v1/chats/:chatId/messages') +@UseGuards(CurrentUserGuard) +export class MessagesController { + constructor(private readonly messagesService: MessagesService) {} + + @Post() + create( + @Param('chatId', new ParseUUIDPipe()) chatId: string, + @Body() body: CreateMessageDto, + @CurrentUser() currentUser: User, + @Headers('idempotency-key') idempotencyKey?: string | string[], + ) { + return this.messagesService.create( + chatId, + body, + currentUser, + idempotencyKey, + ); + } + + @Get() + list( + @Param('chatId', new ParseUUIDPipe()) chatId: string, + @CurrentUser() currentUser: User, + @Query('limit', new DefaultValuePipe(50), new ParseIntPipe()) + limit: number, + @Query('cursor') cursor?: string, + ) { + if (limit < 1 || limit > 100) + throw new BadRequestException('limit must be between 1 and 100'); + return this.messagesService.list(chatId, currentUser, limit, cursor); + } +} diff --git a/app/src/messages/messages.module.ts b/app/src/messages/messages.module.ts new file mode 100644 index 00000000..895c16b1 --- /dev/null +++ b/app/src/messages/messages.module.ts @@ -0,0 +1,28 @@ +import { Module } from '@nestjs/common'; +import { DatabaseModule } from '../database/database.module.js'; +import { ChatsModule } from '../chats/chats.module.js'; +import { MessagesController } from './messages.controller.js'; +import { MessagesRepository } from './messages.repository.js'; +import { MessagesService } from './messages.service.js'; +import { MessagesCacheModule } from './cache/messages-cache.module.js'; +import { IdempotencyModule } from './idempotency/idempotency.module.js'; +import { UsersModule } from '../users/users.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { AttachmentsModule } from '../attachments/attachments.module.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; + +@Module({ + imports: [ + ChatsModule, + MessagesCacheModule, + IdempotencyModule, + DatabaseModule, + UsersModule, + RedisModule, + MetricsModule, + AttachmentsModule, + ], + controllers: [MessagesController], + providers: [MessagesRepository, MessagesService], +}) +export class MessagesModule {} diff --git a/app/src/messages/messages.repository.spec.ts b/app/src/messages/messages.repository.spec.ts new file mode 100644 index 00000000..10fc6d3d --- /dev/null +++ b/app/src/messages/messages.repository.spec.ts @@ -0,0 +1,26 @@ +import { describe, expect, it, vi } from 'vitest'; +import type { Message } from './message.js'; +import { MessagesRepository } from './messages.repository.js'; + +const message = (id: string, created_at: string): Message => ({ + id, + chat_id: 'chat', + sender_id: 'sender', + text: id, + created_at, + attachment_ids: [], +}); + +describe('MessagesRepository', () => { + it('returns rows in response shape', async () => { + const query = vi.fn().mockResolvedValue({ + rows: [message('1', '2026-01-01T00:00:00.000Z')], + rowCount: 1, + }); + const repository = new MessagesRepository({ query } as never); + await expect(repository.findPage('chat', 1)).resolves.toEqual({ + items: [message('1', '2026-01-01T00:00:00.000Z')], + hasMore: false, + }); + }); +}); diff --git a/app/src/messages/messages.repository.ts b/app/src/messages/messages.repository.ts new file mode 100644 index 00000000..a3f17ce3 --- /dev/null +++ b/app/src/messages/messages.repository.ts @@ -0,0 +1,145 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { randomUUID } from 'node:crypto'; +import { DatabaseClient } from '../database/database.client.js'; +import type { Cursor } from '../common/http/cursor.js'; +import type { Message } from './message.js'; + +type MessageRow = { + id: string; + chat_id: string; + sender_id: string; + text: string; + created_at: Date | string; + attachment_ids?: string[] | null; +}; + +export type CreateOnceRequest = { + userId: string; + chatId: string; + path: string; + key: string; + fingerprint: string; + text: string; + attachmentIds: string[]; +}; + +function toMessage(row: MessageRow): Message { + return { + id: row.id, + chat_id: row.chat_id, + sender_id: row.sender_id, + text: row.text, + created_at: new Date(row.created_at).toISOString(), + attachment_ids: row.attachment_ids ?? [], + }; +} + +// Один оператор делает всё, что нужно пути записи: проверяет членство, занимает +// ключ идемпотентности, вставляет сообщение, увеличивает версию чата (якорь +// инвалидации кэша истории) и сохраняет тело ответа. Атомарность даёт сам +// оператор, поэтому явная транзакция и отдельные round trip'ы не нужны; тело +// ответа строится в SQL и служит единственным источником истины и для первого +// ответа, и для повторов. +// +// Зависимости CTE задают порядок выполнения: claim -> inserted -> bumped. +const CREATE_ONCE_SQL = ` +WITH claim AS ( + INSERT INTO idempotency_keys (user_id, method, path, key, request_fingerprint, + status_code, response_body, message_id) + SELECT $1, 'POST', $2, $3, $4, 201, + jsonb_build_object( + 'id', $5::uuid, + 'chat_id', $7::uuid, + 'sender_id', $1::uuid, + 'text', $6::text, + 'created_at', to_char(date_trunc('milliseconds', now()) AT TIME ZONE 'UTC', + 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'), + 'attachment_ids', to_jsonb($8::uuid[])), + $5::uuid + WHERE EXISTS ( + SELECT 1 FROM chat_members WHERE chat_id = $7::uuid AND user_id = $1 + ) + AND (SELECT count(*) FROM attachments a + WHERE a.chat_id = $7::uuid + AND a.status IN ('queued','processing','ready') + AND a.id = ANY($8::uuid[])) = cardinality($8::uuid[]) + ON CONFLICT DO NOTHING + RETURNING response_body +), inserted AS ( + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + SELECT $5::uuid, $7::uuid, $1::uuid, $6::text, date_trunc('milliseconds', now()) + FROM claim + RETURNING id +), linked AS ( + INSERT INTO message_attachments (message_id, chat_id, attachment_id, ordinal) + SELECT $5::uuid, $7::uuid, t.id, t.ord + FROM inserted, unnest($8::uuid[]) WITH ORDINALITY AS t(id, ord) +), bumped AS ( + UPDATE chats SET message_version = message_version + 1 + WHERE id = $7::uuid AND EXISTS (SELECT 1 FROM inserted) +) +SELECT response_body FROM claim`; + +@Injectable() +export class MessagesRepository { + constructor(private readonly database: DatabaseClient) {} + + /** + * Returns the created message, or undefined when the key is already taken or + * the caller is not a member of the chat (both leave no side effect). + */ + async createOnce(request: CreateOnceRequest): Promise { + const result = await this.database.query<{ response_body: Message }>( + 'messages.createIdempotent', + CREATE_ONCE_SQL, + [ + request.userId, + request.path, + request.key, + request.fingerprint, + randomUUID(), + request.text, + request.chatId, + request.attachmentIds, + ], + ); + return result.rows[0]?.response_body; + } + + async getVersion(chatId: string, userId: string): Promise { + const result = await this.database.query<{ message_version: string }>( + 'messages.version', + 'SELECT c.message_version FROM chats c WHERE c.id = $1 AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id = c.id AND user_id = $2)', + [chatId, userId], + ); + if (!result.rows[0]) throw new NotFoundException(); + return BigInt(result.rows[0].message_version); + } + + async findPage( + chatId: string, + limit: number, + cursor?: Cursor, + ): Promise<{ items: Message[]; hasMore: boolean }> { + const values: unknown[] = [chatId]; + let predicate = ''; + if (cursor) { + values.push(cursor.created_at, cursor.id); + predicate = + 'AND (m.created_at, m.id) > ($2::timestamptz, $3::uuid)'; + } + values.push(limit + 1); + const result = await this.database.query( + 'messages.history', + `SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, + COALESCE((SELECT json_agg(ma.attachment_id ORDER BY ma.ordinal) + FROM message_attachments ma WHERE ma.message_id = m.id), '[]'::json) AS attachment_ids + FROM messages m WHERE m.chat_id = $1 ${predicate} ORDER BY m.created_at, m.id LIMIT $${values.length}`, + values, + ); + return { + items: result.rows.slice(0, limit).map(toMessage), + hasMore: result.rows.length > limit, + }; + } +} diff --git a/app/src/messages/messages.service.spec.ts b/app/src/messages/messages.service.spec.ts new file mode 100644 index 00000000..8618dea2 --- /dev/null +++ b/app/src/messages/messages.service.spec.ts @@ -0,0 +1,121 @@ +import { describe, expect, it, vi } from 'vitest'; +import { MessagesService } from './messages.service.js'; +import { idempotencyFingerprint } from './idempotency/idempotency.js'; +import type { Message } from './message.js'; + +type StoredRow = { + request_fingerprint: string; + response_body: Message; + created_at: Date; + expired: boolean; + is_member: boolean; +}; + +function deferred(): { promise: Promise; resolve: () => void } { + let resolve!: () => void; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +} + +describe('MessagesService idempotency expiry race', () => { + it('does not let a stale expiry decision delete a replacement claim', async () => { + const oldCreatedAt = new Date('2020-01-01T00:00:00.000Z'); + const newMessage: Message = { + id: 'message-new', + chat_id: 'chat', + sender_id: 'user', + text: 'hello', + created_at: '2026-01-01T00:00:00.000Z', + attachment_ids: [], + }; + let row: StoredRow | undefined = { + request_fingerprint: idempotencyFingerprint('hello', []), + response_body: { + ...newMessage, + id: 'message-old', + text: 'old', + }, + created_at: oldCreatedAt, + expired: true, + is_member: true, + }; + let createCalls = 0; + let readCalls = 0; + let forgetCalls = 0; + let messageWrites = 0; + const bothInitialClaims = deferred(); + const bothInitialReads = deferred(); + const replacementCreated = deferred(); + + const messagesRepository = { + createOnce: vi.fn(async () => { + createCalls += 1; + if (createCalls === 2) bothInitialClaims.resolve(); + if (createCalls <= 2) { + await bothInitialClaims.promise; + return undefined; + } + if (row) return undefined; + messageWrites += 1; + row = { + request_fingerprint: idempotencyFingerprint('hello', []), + response_body: newMessage, + created_at: new Date('2026-01-01T00:00:00.000Z'), + expired: false, + is_member: true, + }; + replacementCreated.resolve(); + return newMessage; + }), + }; + const idempotency = { + read: vi.fn(async () => { + readCalls += 1; + if (readCalls === 2) bothInitialReads.resolve(); + if (readCalls <= 2) await bothInitialReads.promise; + return row; + }), + forget: vi.fn(async (...args: unknown[]) => { + forgetCalls += 1; + if (args.length !== 4) { + row = undefined; + return; + } + if (forgetCalls === 2) await replacementCreated.promise; + if ( + !row || + row.created_at >= new Date('2026-01-01T00:00:00.000Z') + ) + return; + row = undefined; + }), + isMember: vi.fn().mockResolvedValue(true), + }; + const makeService = () => + new MessagesService( + messagesRepository as never, + {} as never, + {} as never, + idempotency as never, + { getOrThrow: vi.fn().mockReturnValue(60) } as never, + { increment: vi.fn() } as never, + {} as never, + ); + const input = { text: 'hello', attachment_ids: [] } as never; + const user = { id: 'user' } as never; + + const results = await Promise.all([ + makeService().create('chat', input, user, 'same-key'), + makeService().create('chat', input, user, 'same-key'), + ]); + + expect(results).toEqual([newMessage, newMessage]); + expect(new Set(results.map((message) => message.id))).toEqual( + new Set(['message-new']), + ); + expect(messageWrites).toBe(1); + expect(forgetCalls).toBe(2); + }); +}); diff --git a/app/src/messages/messages.service.ts b/app/src/messages/messages.service.ts new file mode 100644 index 00000000..1ab2f960 --- /dev/null +++ b/app/src/messages/messages.service.ts @@ -0,0 +1,206 @@ +import { + BadRequestException, + ConflictException, + Injectable, + Logger, + NotFoundException, + ServiceUnavailableException, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { decodeCursor, encodeCursor } from '../common/http/cursor.js'; +import type { Page } from '../common/http/page.js'; +import type { AppConfig } from '../config/app-config.schema.js'; +import type { User } from '../users/user.js'; +import { ChatsService } from '../chats/chats.service.js'; +import { AttachmentsRepository } from '../attachments/attachments.repository.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import type { CreateMessageDto } from './dto/create-message.dto.js'; +import type { Message } from './message.js'; +import { MessagesRepository } from './messages.repository.js'; +import { MessagesCacheService } from './cache/messages-cache.service.js'; +import { + IDEMPOTENCY_KEY_PATTERN, + idempotencyFingerprint, + messagesPath, +} from './idempotency/idempotency.js'; +import { IdempotencyRepository } from './idempotency/idempotency.repository.js'; + +// Один повтор нужен только для просроченного ключа: запись удаляется, и тот же +// оператор занимает ключ заново. Больше трёх кругов означает, что ключ +// перехватывают параллельные запросы, и честнее вернуть 503, чем крутиться. +const MAX_CLAIM_ATTEMPTS = 3; +const PURGE_EVERY_REQUESTS = 512; + +@Injectable() +export class MessagesService { + private readonly logger = new Logger(MessagesService.name); + private requestsSincePurge = 0; + + constructor( + private readonly messagesRepository: MessagesRepository, + private readonly chatsService: ChatsService, + private readonly cache: MessagesCacheService, + private readonly idempotency: IdempotencyRepository, + private readonly config: ConfigService, + private readonly metrics: MetricsService, + private readonly attachments: AttachmentsRepository, + ) {} + + async create( + chatId: string, + input: CreateMessageDto, + currentUser: User, + rawKey: string | string[] | undefined, + ): Promise { + // Ключ и тело проверяются до обращения к БД: клиентская ошибка не должна + // создавать заявку и не должна зависеть от состояния хранилища. + if (typeof rawKey !== 'string' || !IDEMPOTENCY_KEY_PATTERN.test(rawKey)) + throw new BadRequestException('Idempotency-Key header is required'); + const ids = input.attachment_ids.map((id) => id.toLowerCase()); + if (new Set(ids).size !== ids.length) + throw new BadRequestException('attachment_ids must be unique'); + if ( + ids.length === 0 && + [...input.text].every((character) => /\s/u.test(character)) + ) + throw new BadRequestException('Message text cannot be blank'); + + const path = messagesPath(chatId); + const fingerprint = idempotencyFingerprint(input.text, ids); + const ttl = this.config.getOrThrow('IDEMPOTENCY_TTL_SECONDS'); + for (let attempt = 1; attempt <= MAX_CLAIM_ATTEMPTS; attempt += 1) { + const created = await this.messagesRepository.createOnce({ + userId: currentUser.id, + chatId, + path, + key: rawKey, + fingerprint, + text: input.text, + attachmentIds: ids, + }); + if (created) { + this.metrics.increment('idempotency.claim'); + this.metrics.increment('messages.write'); + this.scheduleExpiredPurge(); + return created; + } + + const stored = await this.idempotency.read( + currentUser.id, + path, + rawKey, + chatId, + ttl, + ); + if (!stored) { + // Заявки нет: либо запрос не имеет доступа к чату (404), либо + // чужую заявку сняли между нашими двумя запросами (503). + if (!(await this.idempotency.isMember(chatId, currentUser.id))) + throw new NotFoundException(); + // Unknown or foreign-chat ids are 404; an attachment whose + // processing failed is a conflict, not a missing object. + const linkState = + ids.length > 0 + ? await this.attachments.linkState(chatId, ids) + : 'ok'; + if (linkState === 'failed') + throw new ConflictException('Attachment processing failed'); + if (linkState === 'missing') throw new NotFoundException(); + this.metrics.increment('idempotency.claim_lost'); + throw new ServiceUnavailableException( + 'Idempotency key could not be claimed', + ); + } + if (stored.expired) { + this.metrics.increment('idempotency.released'); + await this.idempotency.forget( + currentUser.id, + path, + rawKey, + ttl, + ); + continue; + } + if (stored.request_fingerprint !== fingerprint) { + this.metrics.increment('idempotency.conflict'); + throw new ConflictException( + 'Idempotency key reused with a different body', + ); + } + if (!stored.is_member) throw new NotFoundException(); + this.metrics.increment('idempotency.replay'); + // Bodies stored before attachments existed carry no attachment_ids. + return { + ...stored.response_body, + attachment_ids: stored.response_body.attachment_ids ?? [], + }; + } + throw new ServiceUnavailableException( + 'Idempotency key could not be claimed', + ); + } + + // Expired claims are storage hygiene, not part of the request: the pass runs + // outside the request path, at most once per PURGE_EVERY_REQUESTS successful + // writes, and its failure never changes the client's result. + private scheduleExpiredPurge(): void { + this.requestsSincePurge += 1; + if (this.requestsSincePurge < PURGE_EVERY_REQUESTS) return; + this.requestsSincePurge = 0; + const ttl = this.config.getOrThrow('IDEMPOTENCY_TTL_SECONDS'); + void this.idempotency + .purgeExpired(ttl) + .then((purged) => { + if (purged > 0) this.metrics.increment('idempotency.purged'); + }) + .catch((error: unknown) => { + this.metrics.increment('idempotency.purge_error'); + this.logger.warn( + `Idempotency purge failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + }); + } + + async list( + chatId: string, + currentUser: User, + limit: number, + cursorValue?: string, + ): Promise> { + await this.chatsService.getForUser(chatId, currentUser); + const scope = `messages:${chatId}`; + const cursor = cursorValue + ? decodeCursor(cursorValue, scope) + : undefined; + const version = await this.messagesRepository.getVersion( + chatId, + currentUser.id, + ); + const page = await this.cache.getOrLoad( + chatId, + version, + cursorValue, + limit, + async () => { + const loaded = await this.messagesRepository.findPage( + chatId, + limit, + cursor, + ); + const lastLoaded = loaded.items.at(-1); + return { + items: loaded.items, + next_cursor: + loaded.hasMore && lastLoaded + ? encodeCursor({ + scope, + created_at: lastLoaded.created_at, + id: lastLoaded.id, + }) + : null, + }; + }, + ); + return page as Page; + } +} diff --git a/app/src/metrics/metrics.controller.ts b/app/src/metrics/metrics.controller.ts new file mode 100644 index 00000000..9c2100d5 --- /dev/null +++ b/app/src/metrics/metrics.controller.ts @@ -0,0 +1,54 @@ +import { + Controller, + Get, + Headers, + HttpCode, + HttpStatus, + Post, + UnauthorizedException, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { resolveInstanceId } from '../config/instance-id.js'; +import { MetricsService } from './metrics.service.js'; + +@Controller('internal') +export class MetricsController { + private readonly instanceId: string; + private readonly buildId: string; + + constructor( + private readonly config: ConfigService, + private readonly metrics: MetricsService, + ) { + this.instanceId = resolveInstanceId(config); + this.buildId = process.env.BUILD_ID ?? 'unknown'; + } + + @Get('metrics') + get(@Headers('authorization') authorization?: string) { + this.requireToken(authorization); + return { + ...this.metrics.snapshot(), + instance_id: this.instanceId, + build_id: this.buildId, + }; + } + + @Post('metrics/reset') + @HttpCode(HttpStatus.OK) + reset(@Headers('authorization') authorization?: string): { status: 'ok' } { + this.requireToken(authorization); + this.metrics.reset(); + return { status: 'ok' }; + } + + private requireToken(authorization?: string): void { + const token = this.config.get('METRICS_TOKEN'); + if (!token || authorization !== `Bearer ${token}`) + throw new UnauthorizedException({ + message: 'Unauthorized', + challenge: 'Bearer realm="metrics"', + }); + } +} diff --git a/app/src/metrics/metrics.module.ts b/app/src/metrics/metrics.module.ts new file mode 100644 index 00000000..3e945a56 --- /dev/null +++ b/app/src/metrics/metrics.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { MetricsController } from './metrics.controller.js'; +import { MetricsService } from './metrics.service.js'; + +@Module({ + controllers: [MetricsController], + providers: [MetricsService], + exports: [MetricsService], +}) +export class MetricsModule {} diff --git a/app/src/metrics/metrics.service.spec.ts b/app/src/metrics/metrics.service.spec.ts new file mode 100644 index 00000000..0316e946 --- /dev/null +++ b/app/src/metrics/metrics.service.spec.ts @@ -0,0 +1,77 @@ +import { describe, expect, it } from 'vitest'; +import { MetricsService } from './metrics.service.js'; + +describe('MetricsService', () => { + it('computes nearest-rank percentiles from observed latencies', () => { + const metrics = new MetricsService(); + metrics.increment('cache_hit'); + metrics.increment('cache_hit'); + metrics.observeLatency(30); + metrics.observeLatency(10); + metrics.observeLatency(20); + const snapshot = metrics.snapshot(); + expect(snapshot.counters).toEqual({ cache_hit: 2 }); + expect(snapshot.latency_ms).toEqual({ + count: 3, + sample_count: 3, + p50: 20, + p95: 30, + p99: 30, + max: 30, + }); + }); + + it('keeps snapshots isolated from callers and later observations', () => { + const metrics = new MetricsService(); + metrics.increment('cache_hit'); + const snapshot = metrics.snapshot(); + snapshot.counters.cache_hit = 99; + metrics.increment('cache_hit'); + expect(metrics.snapshot().counters.cache_hit).toBe(2); + expect(snapshot.latency_ms.count).toBe(0); + }); + + it('bounds retained samples but keeps cumulative count and maximum', () => { + const metrics = new MetricsService(); + metrics.observeLatency(9000); + for (let index = 0; index < 4096; index += 1) + metrics.observeLatency(10); + expect(metrics.snapshot().latency_ms).toEqual({ + count: 4097, + sample_count: 4096, + p50: 10, + p95: 10, + p99: 10, + max: 9000, + }); + }); + + it('ignores invalid latency observations', () => { + const metrics = new MetricsService(); + for (const value of [-1, NaN, Infinity]) metrics.observeLatency(value); + expect(metrics.snapshot().latency_ms).toEqual({ + count: 0, + sample_count: 0, + p50: 0, + p95: 0, + p99: 0, + max: 0, + }); + }); + + it('adds byte counters and ignores invalid amounts', () => { + const metrics = new MetricsService(); + metrics.add('bytes', 100); + metrics.add('bytes', 28); + for (const value of [-1, NaN, Infinity]) metrics.add('bytes', value); + expect(metrics.snapshot().counters.bytes).toBe(128); + }); + + it('tracks memory peaks until reset', () => { + const metrics = new MetricsService(); + const ballast = Buffer.alloc(32 * 1024 * 1024, 1); + const during = metrics.snapshot().memory_peak_bytes.array_buffers; + expect(during).toBeGreaterThanOrEqual(ballast.length); + ballast.fill(0); + }); +}); diff --git a/app/src/metrics/metrics.service.ts b/app/src/metrics/metrics.service.ts new file mode 100644 index 00000000..9087b09a --- /dev/null +++ b/app/src/metrics/metrics.service.ts @@ -0,0 +1,123 @@ +import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; + +const LATENCY_SAMPLE_CAPACITY = 4096; + +type LatencySnapshot = { + count: number; + sample_count: number; + p50: number; + p95: number; + p99: number; + max: number; +}; + +const MEMORY_SAMPLE_INTERVAL_MS = 50; + +type MemoryUsage = { + rss: number; + heap_used: number; + external: number; + array_buffers: number; +}; + +function currentMemory(): MemoryUsage { + const usage = process.memoryUsage(); + return { + rss: usage.rss, + heap_used: usage.heapUsed, + external: usage.external, + array_buffers: usage.arrayBuffers, + }; +} + +@Injectable() +export class MetricsService implements OnModuleInit, OnModuleDestroy { + private memoryPeak: MemoryUsage = currentMemory(); + private memoryTimer: NodeJS.Timeout | undefined; + private readonly counters: Record = {}; + private readonly latencies: number[] = []; + private latencyCount = 0; + private maximumLatency = 0; + private nextSample = 0; + + onModuleInit(): void { + this.memoryTimer = setInterval( + () => this.sampleMemory(), + MEMORY_SAMPLE_INTERVAL_MS, + ); + this.memoryTimer.unref(); + } + + onModuleDestroy(): void { + if (this.memoryTimer) clearInterval(this.memoryTimer); + this.memoryTimer = undefined; + } + + add(name: string, amount: number): void { + if (!Number.isFinite(amount) || amount < 0) return; + this.counters[name] = (this.counters[name] ?? 0) + amount; + } + + increment(name: string): void { + this.counters[name] = (this.counters[name] ?? 0) + 1; + } + + observeLatency(milliseconds: number): void { + if (!Number.isFinite(milliseconds) || milliseconds < 0) return; + this.latencyCount += 1; + this.maximumLatency = Math.max(this.maximumLatency, milliseconds); + this.latencies[this.nextSample] = milliseconds; + this.nextSample = (this.nextSample + 1) % LATENCY_SAMPLE_CAPACITY; + } + + snapshot(): { + counters: Record; + latency_ms: LatencySnapshot; + memory_bytes: MemoryUsage; + memory_peak_bytes: MemoryUsage; + } { + this.sampleMemory(); + const sorted = [...this.latencies].sort((left, right) => left - right); + return { + counters: { ...this.counters }, + latency_ms: { + count: this.latencyCount, + sample_count: sorted.length, + p50: this.percentile(sorted, 0.5), + p95: this.percentile(sorted, 0.95), + p99: this.percentile(sorted, 0.99), + max: this.maximumLatency, + }, + memory_bytes: currentMemory(), + memory_peak_bytes: { ...this.memoryPeak }, + }; + } + + reset(): void { + for (const name of Object.keys(this.counters)) + delete this.counters[name]; + this.latencies.length = 0; + this.latencyCount = 0; + this.maximumLatency = 0; + this.nextSample = 0; + this.memoryPeak = currentMemory(); + } + + private sampleMemory(): void { + const now = currentMemory(); + const peak = this.memoryPeak; + peak.rss = Math.max(peak.rss, now.rss); + peak.heap_used = Math.max(peak.heap_used, now.heap_used); + peak.external = Math.max(peak.external, now.external); + peak.array_buffers = Math.max(peak.array_buffers, now.array_buffers); + } + + private percentile(sorted: number[], percentile: number): number { + if (sorted.length === 0) return 0; + const index = Math.min( + sorted.length - 1, + Math.ceil(sorted.length * percentile) - 1, + ); + return sorted[index]; + } +} diff --git a/app/src/redis/redis.client.ts b/app/src/redis/redis.client.ts new file mode 100644 index 00000000..220a6fbc --- /dev/null +++ b/app/src/redis/redis.client.ts @@ -0,0 +1,145 @@ +import { + Injectable, + Logger, + OnModuleDestroy, + OnModuleInit, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { createClient, type RedisClientType } from 'redis'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { MetricsService } from '../metrics/metrics.service.js'; + +const UNLOCK_SCRIPT = + 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end'; +type Client = RedisClientType; + +@Injectable() +export class RedisClient implements OnModuleInit, OnModuleDestroy { + readonly client: Client; + private readonly logger = new Logger(RedisClient.name); + + constructor( + private readonly config: ConfigService, + private readonly metrics: MetricsService, + ) { + this.client = createClient({ + url: this.config.getOrThrow('REDIS_URL'), + disableOfflineQueue: true, + socket: { + reconnectStrategy: (attempt) => + Math.min(1000 + attempt * 250, 5000), + connectTimeout: 5000, + }, + }) as Client; + // Reconnect failures must be visible; the readiness probe pings Redis. + this.client.on('error', (error: Error) => { + this.metrics.increment('redis.error'); + this.logger.warn(`Redis error: ${error.message}`); + }); + } + + async onModuleInit(): Promise { + await this.client.connect(); + } + + async onModuleDestroy(): Promise { + if (this.client.isOpen) await this.client.quit(); + } + + async health(): Promise { + this.metrics.increment('redis.ping'); + // AbortSignal.timeout ограничивает именно пробу: обычные GET/SET остаются + // без командного таймаута, а замороженный Redis проваливает готовность + // в бюджет, который короче timeout check балансировщика. + const timeout = AbortSignal.timeout( + this.config.getOrThrow('HEALTH_TIMEOUT_MS'), + ); + await this.client.withAbortSignal(timeout).ping(); + } + + async get(key: string): Promise { + this.metrics.increment('redis.get'); + return this.client.get(key); + } + + async set( + key: string, + value: string, + options?: { EX?: number; PX?: number; NX?: boolean }, + ): Promise { + this.metrics.increment('redis.set'); + return this.client.set(key, value, options); + } + + async setNxEx( + key: string, + value: string, + seconds: number, + ): Promise { + this.metrics.increment('redis.set'); + return ( + (await this.client.set(key, value, { NX: true, EX: seconds })) === + 'OK' + ); + } + + async setNxPx( + key: string, + value: string, + milliseconds: number, + ): Promise { + this.metrics.increment('redis.set'); + return ( + (await this.client.set(key, value, { + NX: true, + PX: milliseconds, + })) === 'OK' + ); + } + + async del(key: string): Promise { + this.metrics.increment('redis.del'); + return this.client.del(key); + } + + async incr(key: string): Promise { + this.metrics.increment('redis.incr'); + return this.client.incr(key); + } + + async expire(key: string, seconds: number): Promise { + this.metrics.increment('redis.expire'); + return (await this.client.expire(key, seconds)) === 1; + } + + async scan( + cursor: number, + pattern?: string, + count = 100, + ): Promise<{ cursor: number; keys: string[] }> { + this.metrics.increment('redis.scan'); + const result = await this.client.scan(String(cursor), { + MATCH: pattern, + COUNT: count, + }); + return { cursor: Number(result.cursor), keys: result.keys }; + } + + async unlock(key: string, owner: string): Promise { + this.metrics.increment('redis.unlock'); + return Number( + await this.client.eval(UNLOCK_SCRIPT, { + keys: [key], + arguments: [owner], + }), + ); + } + + pipeline(commands: Array<[string, ...string[]]>): Promise { + this.metrics.increment('redis.pipeline'); + const multi = this.client.multi(); + for (const [command, ...args] of commands) + multi.addCommand([command, ...args]); + return multi.exec(); + } +} diff --git a/app/src/redis/redis.module.ts b/app/src/redis/redis.module.ts new file mode 100644 index 00000000..74464f56 --- /dev/null +++ b/app/src/redis/redis.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { RedisClient } from './redis.client.js'; + +@Module({ + imports: [MetricsModule], + providers: [RedisClient], + exports: [RedisClient], +}) +export class RedisModule {} diff --git a/app/src/storage/object-storage.ts b/app/src/storage/object-storage.ts new file mode 100644 index 00000000..b531a0d4 --- /dev/null +++ b/app/src/storage/object-storage.ts @@ -0,0 +1,233 @@ +import { + DeleteObjectCommand, + GetObjectCommand, + HeadObjectCommand, + PutObjectCommand, + S3Client, +} from '@aws-sdk/client-s3'; +import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; +import { NodeHttpHandler } from '@smithy/node-http-handler'; +import { + Injectable, + Logger, + ServiceUnavailableException, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import http from 'node:http'; +import { Readable } from 'node:stream'; +import type { AppConfig } from '../config/app-config.schema.js'; + +export class ObjectStorageUnavailableError extends ServiceUnavailableException { + constructor() { + super('Object storage unavailable'); + } +} + +export type PresignPutOptions = { + contentType: string; + contentLength: number; + checksumSha256Base64: string; + expiresInSeconds: number; +}; + +function isMissingKey(error: unknown): boolean { + const candidate = error as { + name?: string; + $metadata?: { httpStatusCode?: number }; + }; + return ( + candidate?.name === 'NoSuchKey' || + candidate?.name === 'NotFound' || + candidate?.$metadata?.httpStatusCode === 404 + ); +} + +@Injectable() +export class ObjectStorage { + private readonly logger = new Logger(ObjectStorage.name); + private readonly bucket: string; + private readonly internal: S3Client; + private readonly presign: S3Client; + private readonly requestTimeoutMs: number; + + constructor(config: ConfigService) { + this.bucket = config.getOrThrow('S3_BUCKET'); + this.requestTimeoutMs = config.getOrThrow('S3_REQUEST_TIMEOUT_MS'); + const common = { + region: config.getOrThrow('S3_REGION'), + forcePathStyle: true, + credentials: { + accessKeyId: config.getOrThrow('S3_ACCESS_KEY_ID'), + secretAccessKey: config.getOrThrow('S3_SECRET_ACCESS_KEY'), + }, + // Поток нельзя повторить: повтор — решение клиента API. + maxAttempts: 1, + requestChecksumCalculation: 'WHEN_REQUIRED' as const, + responseChecksumValidation: 'WHEN_REQUIRED' as const, + // requestTimeout bounds a request until its response arrives and, + // with throwOnRequestTimeout, actually fails it (otherwise smithy + // only logs a warning); socketTimeout bounds a stalled body read. + // A hung S3 therefore costs at most S3_REQUEST_TIMEOUT_MS per call. + requestHandler: new NodeHttpHandler({ + connectionTimeout: config.getOrThrow('S3_CONNECT_TIMEOUT_MS'), + requestTimeout: config.getOrThrow('S3_REQUEST_TIMEOUT_MS'), + throwOnRequestTimeout: true, + socketTimeout: config.getOrThrow('S3_REQUEST_TIMEOUT_MS'), + httpAgent: new http.Agent({ keepAlive: true, maxSockets: 64 }), + }), + }; + this.internal = new S3Client({ + ...common, + endpoint: config.getOrThrow('S3_ENDPOINT'), + }); + // Сеть не использует: только подпись URL для клиента. + this.presign = new S3Client({ + ...common, + endpoint: config.getOrThrow('S3_PUBLIC_ENDPOINT'), + }); + } + + objectKey(attachmentId: string): string { + return `attachments/${attachmentId}/original`; + } + + stagingKey(attachmentId: string): string { + return `staging/${attachmentId}/original`; + } + + variantKey( + attachmentId: string, + pipelineVersion: number, + variant: 'thumbnail' | 'crop' | 'optimized', + extension: 'jpg' | 'webp', + attempt = 1, + ): string { + // The attempt suffix makes a stale worker's write harmless: the DB + // points at the key of the attempt that won the lease fence. + return `attachments/${attachmentId}/v${pipelineVersion}/${variant}-a${attempt}.${extension}`; + } + + async putStream( + key: string, + body: Readable, + contentLength: number, + contentType: string, + signal?: AbortSignal, + ): Promise { + try { + await this.internal.send( + new PutObjectCommand({ + Bucket: this.bucket, + Key: key, + Body: body, + ContentLength: contentLength, + ContentType: contentType, + }), + // The upload deadline (UPLOAD_TIMEOUT_MS, via `signal`) bounds the + // PUT: it lasts as long as the client sends, not S3_REQUEST_TIMEOUT_MS. + // A worker buffer has no client deadline, so it uses the SDK timeout. + signal + ? { abortSignal: signal, requestTimeout: 0 } + : { requestTimeout: this.requestTimeoutMs }, + ); + } catch (error) { + throw this.fail('put', error); + } + } + + async putBuffer( + key: string, + data: Buffer, + contentType: string, + signal?: AbortSignal, + ): Promise { + await this.putStream( + key, + Readable.from([data]), + data.length, + contentType, + signal, + ); + } + + async getStream( + key: string, + signal?: AbortSignal, + ): Promise { + try { + const result = await this.internal.send( + new GetObjectCommand({ Bucket: this.bucket, Key: key }), + { abortSignal: signal }, + ); + return result.Body as Readable; + } catch (error) { + if (isMissingKey(error)) return null; + throw this.fail('get', error); + } + } + + async head(key: string): Promise<{ + size: number; + checksumSha256?: string; + etag?: string; + lastModified?: Date; + } | null> { + try { + const result = await this.internal.send( + new HeadObjectCommand({ + Bucket: this.bucket, + Key: key, + ChecksumMode: 'ENABLED', + }), + ); + return { + size: Number(result.ContentLength ?? 0), + checksumSha256: result.ChecksumSHA256, + etag: result.ETag, + lastModified: result.LastModified, + }; + } catch (error) { + if (isMissingKey(error)) return null; + throw this.fail('head', error); + } + } + + async delete(key: string): Promise { + try { + await this.internal.send( + new DeleteObjectCommand({ Bucket: this.bucket, Key: key }), + ); + } catch (error) { + if (isMissingKey(error)) return; + throw this.fail('delete', error); + } + } + + async presignPut(key: string, options: PresignPutOptions): Promise { + return getSignedUrl( + this.presign, + new PutObjectCommand({ + Bucket: this.bucket, + Key: key, + ContentType: options.contentType, + ContentLength: options.contentLength, + ChecksumSHA256: options.checksumSha256Base64, + }), + { + expiresIn: options.expiresInSeconds, + signableHeaders: new Set([ + 'content-type', + 'content-length', + 'x-amz-checksum-sha256', + ]), + unhoistableHeaders: new Set(['x-amz-checksum-sha256']), + }, + ); + } + + private fail(operation: string, error: unknown): Error { + const name = (error as { name?: string })?.name ?? 'UnknownError'; + this.logger.warn(`S3 ${operation} failed: ${name}`); + return new ObjectStorageUnavailableError(); + } +} diff --git a/app/src/storage/storage.module.ts b/app/src/storage/storage.module.ts new file mode 100644 index 00000000..6cdffb6e --- /dev/null +++ b/app/src/storage/storage.module.ts @@ -0,0 +1,5 @@ +import { Module } from '@nestjs/common'; +import { ObjectStorage } from './object-storage.js'; + +@Module({ providers: [ObjectStorage], exports: [ObjectStorage] }) +export class StorageModule {} diff --git a/app/src/types.d.ts b/app/src/types.d.ts new file mode 100644 index 00000000..6a215a66 --- /dev/null +++ b/app/src/types.d.ts @@ -0,0 +1,7 @@ +import { User } from './users/user.ts'; + +declare module 'fastify' { + interface FastifyRequest { + currentUser?: User; + } +} diff --git a/app/src/users/dto/create-user.dto.ts b/app/src/users/dto/create-user.dto.ts new file mode 100644 index 00000000..8e82f58e --- /dev/null +++ b/app/src/users/dto/create-user.dto.ts @@ -0,0 +1,22 @@ +import { IsString, Matches } from 'class-validator'; +import { CodePointLength } from '../../common/validation/code-point-length.js'; + +export class CreateUserDTO { + @IsString() + @Matches(/^[a-z0-9_]{3,32}$/) + username!: string; + + @IsString() + @CodePointLength(1, 100) + @Matches(/\S/u, { + message: 'display_name must contain a non-whitespace character', + }) + display_name!: string; + + @IsString() + @CodePointLength(12, 128) + @Matches(/\S/u, { + message: 'password must contain a non-whitespace character', + }) + password!: string; +} diff --git a/app/src/users/user.ts b/app/src/users/user.ts new file mode 100644 index 00000000..9c91d9e7 --- /dev/null +++ b/app/src/users/user.ts @@ -0,0 +1,6 @@ +export type User = { + id: string; + username: string; + display_name: string; + created_at: string; +}; diff --git a/app/src/users/users.controller.ts b/app/src/users/users.controller.ts new file mode 100644 index 00000000..1ab8c777 --- /dev/null +++ b/app/src/users/users.controller.ts @@ -0,0 +1,22 @@ +import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'; +import { CurrentUser } from '../common/auth/current-user.decorator.js'; +import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; +import type { User } from './user.js'; +import { CreateUserDTO } from './dto/create-user.dto.js'; +import { UsersService } from './users.service.js'; + +@Controller('api/v1/users') +export class UsersController { + constructor(private readonly usersService: UsersService) {} + + @Post() + create(@Body() body: CreateUserDTO) { + return this.usersService.create(body); + } + + @Get('/me') + @UseGuards(CurrentUserGuard) + getMe(@CurrentUser() currentUser: User) { + return currentUser; + } +} diff --git a/app/src/users/users.module.ts b/app/src/users/users.module.ts new file mode 100644 index 00000000..55a73289 --- /dev/null +++ b/app/src/users/users.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { UsersController } from './users.controller.js'; +import { UsersRepository } from './users.repository.js'; +import { UsersService } from './users.service.js'; +import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { DatabaseModule } from '../database/database.module.js'; + +@Module({ + imports: [DatabaseModule, RedisModule, MetricsModule], + controllers: [UsersController], + providers: [UsersRepository, UsersService, CurrentUserGuard], + exports: [UsersService, CurrentUserGuard, RedisModule, MetricsModule], +}) +export class UsersModule {} diff --git a/app/src/users/users.repository.ts b/app/src/users/users.repository.ts new file mode 100644 index 00000000..4fbbe865 --- /dev/null +++ b/app/src/users/users.repository.ts @@ -0,0 +1,71 @@ +import { ConflictException, Injectable } from '@nestjs/common'; +import { DatabaseClient } from '../database/database.client.js'; +import type { User } from './user.js'; + +type UserRow = { + id: string; + username: string; + display_name: string; + password_hash: string; + created_at: Date | string; +}; + +function toUser(row: UserRow): User { + return { + id: row.id, + username: row.username, + display_name: row.display_name, + created_at: new Date(row.created_at).toISOString(), + }; +} + +@Injectable() +export class UsersRepository { + constructor(private readonly database: DatabaseClient) {} + + async findById(id: string): Promise { + const result = await this.database.query( + 'users.findById', + 'SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = $1', + [id], + ); + const row = result.rows[0]; + return row ? toUser(row) : undefined; + } + + async findByUsername( + username: string, + ): Promise<(User & { password_hash: string }) | undefined> { + const result = await this.database.query( + 'users.findByUsername', + 'SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1', + [username], + ); + const row = result.rows[0]; + return row + ? { ...toUser(row), password_hash: row.password_hash } + : undefined; + } + + async create(user: { + id: string; + username: string; + display_name: string; + password_hash: string; + }): Promise { + try { + const result = await this.database.query( + 'users.insert', + `INSERT INTO users (id, username, display_name, password_hash) + VALUES ($1, $2, $3, $4) + RETURNING id, username, display_name, password_hash, created_at`, + [user.id, user.username, user.display_name, user.password_hash], + ); + return toUser(result.rows[0]); + } catch (error) { + if ((error as { code?: string }).code === '23505') + throw new ConflictException('Username already exists'); + throw error; + } + } +} diff --git a/app/src/users/users.service.ts b/app/src/users/users.service.ts new file mode 100644 index 00000000..e4ac35df --- /dev/null +++ b/app/src/users/users.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@nestjs/common'; +import argon2 from 'argon2'; +import { randomUUID } from 'node:crypto'; +import { CreateUserDTO } from './dto/create-user.dto.js'; +import { User } from './user.js'; +import { UsersRepository } from './users.repository.js'; + +@Injectable() +export class UsersService { + constructor(private readonly usersRepository: UsersRepository) {} + + async create(input: CreateUserDTO): Promise { + const passwordHash = await argon2.hash(input.password, { + type: argon2.argon2id, + }); + return this.usersRepository.create({ + id: randomUUID(), + username: input.username, + display_name: input.display_name, + password_hash: passwordHash, + }); + } + + findById(id: string): Promise { + return this.usersRepository.findById(id); + } + + findByUsername(username: string) { + return this.usersRepository.findByUsername(username); + } +} diff --git a/app/src/worker.main.ts b/app/src/worker.main.ts new file mode 100644 index 00000000..77dfea6e --- /dev/null +++ b/app/src/worker.main.ts @@ -0,0 +1,67 @@ +import { Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { NestFactory } from '@nestjs/core'; +import http from 'node:http'; +import type { AppConfig } from './config/app-config.schema.js'; +import { BrokerClient } from './jobs/broker.client.js'; +import { JobProcessor } from './worker/job-processor.service.js'; +import { WorkerModule } from './worker/worker.module.js'; + +const DRAIN_MS = 30_000; + +async function bootstrap(): Promise { + const app = await NestFactory.createApplicationContext(WorkerModule, { + logger: ['error', 'warn', 'log'], + }); + const config = app.get(ConfigService); + const broker = app.get(BrokerClient); + const processor = app.get(JobProcessor); + const logger = new Logger('worker'); + + const server = http.createServer((request, response) => { + if (request.method === 'GET' && request.url === '/health') { + const consuming = broker.isConsuming(); + response.writeHead(consuming ? 200 : 503, { + 'content-type': 'application/json', + }); + response.end( + JSON.stringify({ + status: consuming ? 'ok' : 'not_consuming', + }), + ); + return; + } + response.writeHead(404, { 'content-type': 'application/json' }); + response.end(JSON.stringify({ status: 'not_found' })); + }); + await new Promise((resolve) => + server.listen( + config.getOrThrow('WORKER_HEALTH_PORT'), + '127.0.0.1', + resolve, + ), + ); + logger.log( + `worker ready: id=${process.env.INSTANCE_ID ?? process.env.HOSTNAME ?? 'worker'}`, + ); + + let shuttingDown = false; + const shutdown = async (signal: string): Promise => { + if (shuttingDown) return; + shuttingDown = true; + logger.log( + `${signal}: stopping consumption and draining in-flight jobs`, + ); + await broker.stopConsuming(); + await processor.drain(DRAIN_MS); + await new Promise((resolve) => server.close(() => resolve())); + await app.close(); + process.exit(0); + }; + for (const signal of ['SIGTERM', 'SIGINT'] as const) + process.once(signal, () => { + void shutdown(signal); + }); +} + +await bootstrap(); diff --git a/app/src/worker/job-processor.service.ts b/app/src/worker/job-processor.service.ts new file mode 100644 index 00000000..da69e65e --- /dev/null +++ b/app/src/worker/job-processor.service.ts @@ -0,0 +1,499 @@ +import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { Channel, ConsumeMessage } from 'amqplib'; +import { createHash } from 'node:crypto'; +import os from 'node:os'; +import type { AttachmentRow } from '../attachments/attachment.js'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { ObjectStorage } from '../storage/object-storage.js'; +import { retryDelayMs } from '../jobs/backoff.js'; +import { BrokerClient } from '../jobs/broker.client.js'; +import { + classifyError, + JobTimeoutError, + SourceMismatchError, + SourceMissingError, + type ErrorClass, +} from '../jobs/errors.js'; +import { FaultInjector } from '../jobs/fault-injector.js'; +import { parseJobMessage, type JobMessage } from '../jobs/job-message.js'; +import { OutboxRelay } from '../jobs/outbox-relay.service.js'; +import { + JobsRepository, + type ClaimResult, + type VariantWrite, +} from '../jobs/jobs.repository.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { renderVariants, type RenderedVariant } from './pipelines.js'; + +const SHUTDOWN_DRAIN_MS = 30_000; +const VARIANT_EXTENSIONS = [ + ['thumbnail', 'jpg'], + ['crop', 'jpg'], + ['optimized', 'webp'], +] as const; + +function throwIfAborted(signal: AbortSignal): void { + if (!signal.aborted) return; + throw signal.reason instanceof Error ? signal.reason : new Error('Aborted'); +} + +function sha256(data: Buffer): string { + return createHash('sha256').update(data).digest('hex'); +} + +@Injectable() +export class JobProcessor implements OnApplicationBootstrap { + private readonly logger = new Logger(JobProcessor.name); + private readonly workerId = process.env.INSTANCE_ID ?? os.hostname(); + private readonly maxPixels: number; + private readonly timeoutSeconds: number; + private readonly leaseSeconds: number; + private readonly retryBaseMs: number; + private readonly retryMaxMs: number; + private readonly maxUploadBytes: number; + private active = 0; + private readonly idleWaiters: (() => void)[] = []; + + constructor( + private readonly config: ConfigService, + private readonly broker: BrokerClient, + private readonly jobs: JobsRepository, + private readonly relay: OutboxRelay, + private readonly faults: FaultInjector, + private readonly storage: ObjectStorage, + private readonly metrics: MetricsService, + ) { + this.maxPixels = config.getOrThrow('MAX_IMAGE_PIXELS'); + this.timeoutSeconds = config.getOrThrow('JOB_TIMEOUT_SECONDS'); + this.leaseSeconds = config.getOrThrow('JOB_LEASE_SECONDS'); + this.retryBaseMs = config.getOrThrow('JOB_RETRY_BASE_MS'); + this.retryMaxMs = config.getOrThrow('JOB_RETRY_MAX_MS'); + this.maxUploadBytes = config.getOrThrow('MAX_UPLOAD_BYTES'); + } + + onApplicationBootstrap(): void { + this.broker.consume( + this.config.getOrThrow('WORKER_CONCURRENCY'), + (message, channel) => this.handle(message, channel), + ); + } + + async drain(timeoutMs = SHUTDOWN_DRAIN_MS): Promise { + if (this.active === 0) return; + const { promise, resolve } = Promise.withResolvers(); + this.idleWaiters.push(resolve); + const timer = setTimeout(resolve, timeoutMs); + await promise; + clearTimeout(timer); + } + + private async handle( + message: ConsumeMessage, + channel: Channel, + ): Promise { + const parsed = parseJobMessage(message.content); + if (!parsed) { + this.metrics.increment('worker.messages.poison'); + this.safeNack(channel, message, false); + return; + } + + const claim = await this.claimWithRetry(parsed); + if (!claim) { + this.metrics.increment('worker.jobs.claim_failed'); + // Do not burn the delivery limit in a tight nack loop while the + // database is briefly unavailable. + this.safeNackAfter(channel, message, true, 5000); + return; + } + if (claim.kind === 'skip') { + if (claim.reason === 'mismatch') { + this.safeNack(channel, message, false); + return; + } + this.metrics.increment(`worker.jobs.skipped.${claim.reason}`); + this.safeAck(channel, message); + return; + } + + this.begin(); + try { + await this.process( + claim.job, + claim.attachment, + claim.leaseToken, + message, + channel, + ); + } finally { + this.end(); + } + } + + private async process( + job: { + job_id: string; + attachment_id: string; + pipeline_version: number; + attempt: number; + }, + attachment: AttachmentRow, + leaseToken: string, + message: ConsumeMessage, + channel: Channel, + ): Promise { + this.metrics.increment('worker.jobs.claimed'); + const controller = new AbortController(); + const target = { + attachmentId: job.attachment_id, + pipelineVersion: job.pipeline_version, + }; + const timeout = setTimeout( + () => controller.abort(new JobTimeoutError(this.timeoutSeconds)), + this.timeoutSeconds * 1000, + ); + const heartbeat = setInterval( + () => { + this.jobs + .extendLease(job.job_id, leaseToken, this.leaseSeconds) + .then((extended) => { + if (!extended && !controller.signal.aborted) + controller.abort(new Error('Lease lost')); + }) + .catch((error: unknown) => { + this.logger.warn( + `Lease heartbeat failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + }); + }, + Math.max(1000, (this.leaseSeconds * 1000) / 3), + ); + let committed = false; + + try { + await this.faults.point( + 'worker.before_side_effect', + target, + controller.signal, + ); + throwIfAborted(controller.signal); + const source = await this.ensureFinalOriginal( + attachment, + controller.signal, + ); + throwIfAborted(controller.signal); + const variants = await renderVariants( + source, + job.pipeline_version, + { + maxPixels: this.maxPixels, + timeoutSeconds: this.timeoutSeconds, + signal: controller.signal, + }, + ); + throwIfAborted(controller.signal); + for (const variant of variants) { + if ( + !(await this.jobs.extendLease( + job.job_id, + leaseToken, + this.leaseSeconds, + )) + ) + throw new Error('Lease lost'); + await this.storage.putBuffer( + this.variantKey(job, variant), + variant.data, + variant.contentType, + controller.signal, + ); + } + await this.faults.point( + 'worker.after_side_effect', + target, + controller.signal, + ); + throwIfAborted(controller.signal); + + const result = await this.jobs.commitSuccess( + job.job_id, + leaseToken, + variants.map((variant) => this.variantWrite(job, variant)), + ); + committed = true; + this.metrics.increment(`worker.jobs.${result}`); + await this.faults.point( + 'worker.after_commit_before_ack', + target, + controller.signal, + ); + this.safeAck(channel, message); + if (result === 'succeeded') { + await this.cleanupStaging(attachment); + await this.cleanupAttemptObjects(job, job.attempt); + } else if (result === 'superseded') { + await this.cleanupAttemptObjects(job, null); + } + } catch (error) { + if (committed) { + // The durable effect is already committed; a failure at the ack + // fault point or a lost reply must not turn into a retry. + this.safeAck(channel, message); + return; + } + const failure = this.classify(error, controller.signal); + const delay = retryDelayMs( + job.attempt, + this.retryBaseMs, + this.retryMaxMs, + ); + try { + const outcome = await this.jobs.commitFailure( + job.job_id, + leaseToken, + failure, + delay, + ); + this.metrics.increment(`worker.jobs.${outcome}`); + if (outcome === 'retry') this.relay.kick(); + this.safeAck(channel, message); + if (outcome === 'retry' || outcome === 'failed') + await this.cleanupAttemptObjects(job, null); + } catch (commitError) { + this.metrics.increment('worker.jobs.commit_failed'); + this.logger.warn( + `Failure commit failed: ${commitError instanceof Error ? commitError.message : 'unknown error'}`, + ); + setTimeout(() => this.safeNack(channel, message, true), 1000); + } + } finally { + clearTimeout(timeout); + clearInterval(heartbeat); + } + } + + private classify(error: unknown, signal: AbortSignal): ErrorClass { + if (signal.aborted && signal.reason instanceof JobTimeoutError) + return classifyError(signal.reason); + return classifyError(error); + } + + private async ensureFinalOriginal( + attachment: AttachmentRow, + signal: AbortSignal, + ): Promise { + const expectedSize = Number(attachment.size_bytes); + const head = await this.storage.head(attachment.object_key); + if (head !== null && head.size === expectedSize) { + const data = await this.readObject(attachment.object_key, signal); + this.verifySource(attachment, data); + return data; + } + + const stagingKey = attachment.staging_key; + if (stagingKey === null) throw new SourceMissingError(); + const data = await this.readObject(stagingKey, signal); + this.verifySource(attachment, data); + await this.storage.putBuffer( + attachment.object_key, + data, + attachment.content_type, + signal, + ); + this.metrics.increment('worker.original.published'); + return data; + } + + private async readObject( + key: string, + signal: AbortSignal, + ): Promise { + const stream = await this.storage.getStream(key, signal); + if (stream === null) throw new SourceMissingError(); + const chunks: Buffer[] = []; + let total = 0; + const destroy = (): void => { + stream.destroy( + signal.reason instanceof Error + ? signal.reason + : new Error('Aborted'), + ); + }; + signal.addEventListener('abort', destroy, { once: true }); + try { + for await (const chunk of stream as AsyncIterable) { + total += chunk.length; + if (total > this.maxUploadBytes) + throw new SourceMismatchError( + 'Source exceeds MAX_UPLOAD_BYTES', + ); + chunks.push(chunk); + } + } catch (error) { + if (signal.aborted) + throw signal.reason instanceof Error ? signal.reason : error; + throw error; + } finally { + signal.removeEventListener('abort', destroy); + stream.destroy(); + } + return Buffer.concat(chunks, total); + } + + private verifySource(attachment: AttachmentRow, data: Buffer): void { + if ( + data.length !== Number(attachment.size_bytes) || + sha256(data) !== attachment.sha256 + ) + throw new SourceMismatchError(); + } + + private variantKey( + job: { + attachment_id: string; + pipeline_version: number; + attempt: number; + }, + variant: RenderedVariant, + ): string { + return this.storage.variantKey( + job.attachment_id, + job.pipeline_version, + variant.name, + variant.ext, + job.attempt, + ); + } + + private variantWrite( + job: { + attachment_id: string; + pipeline_version: number; + attempt: number; + }, + variant: RenderedVariant, + ): VariantWrite { + return { + name: variant.name, + objectKey: this.variantKey(job, variant), + contentType: variant.contentType, + sizeBytes: variant.data.length, + width: variant.width, + height: variant.height, + sha256: variant.sha256, + }; + } + + private async cleanupAttemptObjects( + job: { attachment_id: string; pipeline_version: number; attempt: number }, + keepAttempt: number | null, + ): Promise { + for (let attempt = 1; attempt <= job.attempt; attempt += 1) { + if (attempt === keepAttempt) continue; + for (const [name, extension] of VARIANT_EXTENSIONS) { + try { + await this.storage.delete( + this.storage.variantKey( + job.attachment_id, + job.pipeline_version, + name, + extension, + attempt, + ), + ); + } catch (error) { + this.logger.warn( + `Variant cleanup failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + } + } + } + + private async cleanupStaging(attachment: AttachmentRow): Promise { + const key = attachment.staging_key; + if (key === null) return; + try { + const cleared = await this.jobs.clearStaging( + attachment.id, + key, + (stagingKey) => this.storage.delete(stagingKey), + ); + if (cleared) this.metrics.increment('worker.staging.deleted'); + } catch (error) { + this.metrics.increment('worker.staging.error'); + this.logger.warn( + `Staging cleanup for ${attachment.id} deferred: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + } + + private async claimWithRetry( + message: JobMessage, + ): Promise { + for (let attempt = 1; attempt <= 3; attempt += 1) { + try { + return await this.jobs.claim( + message, + this.workerId, + this.leaseSeconds, + ); + } catch (error) { + if (attempt === 3) { + this.logger.warn( + `Claim failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + return undefined; + } + await this.sleep(300 * attempt); + } + } + return undefined; + } + + private sleep(milliseconds: number): Promise { + const { promise, resolve } = Promise.withResolvers(); + setTimeout(resolve, milliseconds); + return promise; + } + + private safeNackAfter( + channel: Channel, + message: ConsumeMessage, + requeue: boolean, + delayMs: number, + ): void { + setTimeout(() => this.safeNack(channel, message, requeue), delayMs); + } + + private safeAck(channel: Channel, message: ConsumeMessage): void { + try { + channel.ack(message); + } catch { + // A closed channel redelivers; the DB fence makes the duplicate a no-op. + } + } + + private safeNack( + channel: Channel, + message: ConsumeMessage, + requeue: boolean, + ): void { + try { + channel.nack(message, false, requeue); + } catch { + // The broker redelivers after the delivery limit or lease recovery. + } + } + + private begin(): void { + this.active += 1; + } + + private end(): void { + this.active -= 1; + if (this.active !== 0) return; + for (const resolve of this.idleWaiters.splice(0)) resolve(); + } +} diff --git a/app/src/worker/lease-reaper.service.ts b/app/src/worker/lease-reaper.service.ts new file mode 100644 index 00000000..55311885 --- /dev/null +++ b/app/src/worker/lease-reaper.service.ts @@ -0,0 +1,100 @@ +import { + Injectable, + Logger, + OnModuleDestroy, + OnModuleInit, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { retryDelayMs } from '../jobs/backoff.js'; +import { BrokerClient } from '../jobs/broker.client.js'; +import { JOB_QUEUE } from '../jobs/broker.topology.js'; +import { JobsRepository } from '../jobs/jobs.repository.js'; +import { OutboxRelay } from '../jobs/outbox-relay.service.js'; +import { MetricsService } from '../metrics/metrics.service.js'; + +const REAP_BATCH = 20; + +/** + * The reaper recovers a worker killed mid-job (expired lease) and re-emits + * queued jobs whose broker message disappeared, for example after a DLQ + * dead-letter or a lost publish. Both paths only insert an outbox row; the + * claim fence absorbs duplicates. + */ +@Injectable() +export class LeaseReaper implements OnModuleInit, OnModuleDestroy { + private readonly logger = new Logger(LeaseReaper.name); + private timer: NodeJS.Timeout | undefined; + private running = false; + private readonly retryBaseMs: number; + private readonly retryMaxMs: number; + private readonly reconcileGraceSeconds: number; + + constructor( + private readonly config: ConfigService, + private readonly jobs: JobsRepository, + private readonly broker: BrokerClient, + private readonly relay: OutboxRelay, + private readonly metrics: MetricsService, + ) { + this.retryBaseMs = config.getOrThrow('JOB_RETRY_BASE_MS'); + this.retryMaxMs = config.getOrThrow('JOB_RETRY_MAX_MS'); + this.reconcileGraceSeconds = config.getOrThrow( + 'QUEUED_RECONCILE_GRACE_SECONDS', + ); + } + + onModuleInit(): void { + const interval = this.config.getOrThrow('LEASE_REAPER_INTERVAL_MS'); + this.timer = setInterval(() => { + if (this.running) return; + this.running = true; + this.tick() + .catch((error: unknown) => { + this.metrics.increment('worker.leases.reap_error'); + this.logger.warn( + `Lease reaper failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + }) + .finally(() => { + this.running = false; + }); + }, interval); + this.timer.unref(); + } + + onModuleDestroy(): void { + clearInterval(this.timer); + this.timer = undefined; + } + + private async tick(): Promise { + const reaped = await this.jobs.reapExpiredLeases( + REAP_BATCH, + (attempt) => + retryDelayMs(attempt, this.retryBaseMs, this.retryMaxMs), + ); + if (reaped > 0) { + this.metrics.add('worker.leases.reaped', reaped); + this.relay.kick(); + } + // A normal backlog also has queued jobs with a published outbox row. + // Re-emitting them would multiply broker messages; only reconcile when + // the queue looks empty. + let queueEmpty = false; + try { + queueEmpty = (await this.broker.queueDepth(JOB_QUEUE)).messages === 0; + } catch { + queueEmpty = false; + } + if (!queueEmpty) return; + const reconciled = await this.jobs.reconcileQueuedJobs( + REAP_BATCH, + this.reconcileGraceSeconds, + ); + if (reconciled > 0) { + this.metrics.add('worker.jobs.reconciled', reconciled); + this.relay.kick(); + } + } +} diff --git a/app/src/worker/pipelines.spec.ts b/app/src/worker/pipelines.spec.ts new file mode 100644 index 00000000..91de436b --- /dev/null +++ b/app/src/worker/pipelines.spec.ts @@ -0,0 +1,102 @@ +import { readFileSync } from 'node:fs'; +import { describe, expect, it } from 'vitest'; +import sharp from 'sharp'; +import { JobTimeoutError } from '../jobs/errors.js'; +import { renderVariants, type RenderedVariant } from './pipelines.js'; + +const sample = readFileSync( + new URL('../../../tests/fixtures/sample.png', import.meta.url), +); +const limits = { maxPixels: 40_000_000, timeoutSeconds: 30 }; + +function byName( + variants: RenderedVariant[], + name: 'thumbnail' | 'crop' | 'optimized', +): RenderedVariant { + const variant = variants.find((item) => item.name === name); + if (!variant) throw new Error(`missing ${name}`); + return variant; +} + +describe('renderVariants', () => { + it('renders the v1 contract geometry and formats', async () => { + const variants = await renderVariants(sample, 1, limits); + const thumbnail = byName(variants, 'thumbnail'); + const crop = byName(variants, 'crop'); + const optimized = byName(variants, 'optimized'); + + expect([thumbnail.width, thumbnail.height]).toEqual([256, 160]); + expect(thumbnail.contentType).toBe('image/jpeg'); + expect((await sharp(thumbnail.data).metadata()).format).toBe('jpeg'); + + expect([crop.width, crop.height]).toEqual([128, 128]); + expect((await sharp(crop.data).metadata()).format).toBe('jpeg'); + + expect([optimized.width, optimized.height]).toEqual([320, 200]); + expect(optimized.contentType).toBe('image/webp'); + expect((await sharp(optimized.data).metadata()).format).toBe('webp'); + }); + + it('applies EXIF orientation before fitting', async () => { + const oriented = await sharp({ + create: { + width: 300, + height: 200, + channels: 3, + background: { r: 20, g: 80, b: 140 }, + }, + }) + .jpeg() + .withMetadata({ orientation: 6 }) + .toBuffer(); + const variants = await renderVariants(oriented, 1, limits); + const thumbnail = byName(variants, 'thumbnail'); + expect([thumbnail.width, thumbnail.height]).toEqual([170, 256]); + }); + + it('composites transparent pixels onto white', async () => { + const transparent = await sharp({ + create: { + width: 10, + height: 10, + channels: 4, + background: { r: 0, g: 0, b: 0, alpha: 0 }, + }, + }) + .png() + .toBuffer(); + const variants = await renderVariants(transparent, 1, limits); + const thumbnail = byName(variants, 'thumbnail'); + const { data } = await sharp(thumbnail.data) + .raw() + .toBuffer({ resolveWithObject: true }); + expect([...data.subarray(0, 3)]).toEqual([255, 255, 255]); + }); + + it('is deterministic for the same input and version', async () => { + const first = await renderVariants(sample, 1, limits); + const second = await renderVariants(sample, 1, limits); + for (const name of ['thumbnail', 'crop', 'optimized'] as const) + expect(byName(first, name).sha256).toBe( + byName(second, name).sha256, + ); + }); + + it('honours an aborted job deadline before starting a render', async () => { + const controller = new AbortController(); + controller.abort(new JobTimeoutError(1)); + await expect( + renderVariants(sample, 1, { + ...limits, + signal: controller.signal, + }), + ).rejects.toBeInstanceOf(JobTimeoutError); + }); + + it('produces a different v2 thumbnail', async () => { + const v1 = byName(await renderVariants(sample, 1, limits), 'thumbnail'); + const v2 = byName(await renderVariants(sample, 2, limits), 'thumbnail'); + expect(v1.sha256).not.toBe(v2.sha256); + expect([v2.width, v2.height]).toEqual([256, 160]); + }); +}); diff --git a/app/src/worker/pipelines.ts b/app/src/worker/pipelines.ts new file mode 100644 index 00000000..0a0b80cd --- /dev/null +++ b/app/src/worker/pipelines.ts @@ -0,0 +1,175 @@ +import { createHash } from 'node:crypto'; +import sharp from 'sharp'; +import { + JobTimeoutError, + UnsupportedPipelineError, +} from '../jobs/errors.js'; +import { fitInside } from '../jobs/fit-inside.js'; + +// Same policy as the API decoder: one libvips worker, no operation cache. +sharp.cache(false); +sharp.concurrency(1); + +type SharpImage = ReturnType; + +export const SUPPORTED_PIPELINE_VERSIONS = [1, 2] as const; + +export type RenderedVariant = { + name: 'thumbnail' | 'crop' | 'optimized'; + ext: 'jpg' | 'webp'; + contentType: 'image/jpeg' | 'image/webp'; + data: Buffer; + width: number; + height: number; + sha256: string; +}; + +export type RenderLimits = { + maxPixels: number; + timeoutSeconds: number; + signal?: AbortSignal; +}; + +function throwIfAborted(signal?: AbortSignal): void { + if (!signal?.aborted) return; + throw signal.reason instanceof Error + ? signal.reason + : new Error('Aborted'); +} + +function remainingSeconds(deadline: number, total: number): number { + const remaining = Math.ceil((deadline - Date.now()) / 1000); + if (remaining <= 0) throw new JobTimeoutError(total); + return Math.min(total, remaining); +} + +function input( + data: Buffer, + limits: RenderLimits, + deadline: number, +): SharpImage { + return sharp(data, { + failOn: 'warning', + limitInputPixels: limits.maxPixels, + sequentialRead: true, + }) + .timeout({ seconds: remainingSeconds(deadline, limits.timeoutSeconds) }) + .rotate(); +} + +function jpegV1(image: SharpImage): SharpImage { + return image.jpeg({ quality: 80 }); +} + +function jpegV2(image: SharpImage): SharpImage { + return image.jpeg({ quality: 90, progressive: true, mozjpeg: true }); +} + +function rendered( + name: RenderedVariant['name'], + ext: RenderedVariant['ext'], + contentType: RenderedVariant['contentType'], + data: Buffer, + width: number, + height: number, +): RenderedVariant { + return { + name, + ext, + contentType, + data, + width, + height, + sha256: createHash('sha256').update(data).digest('hex'), + }; +} + +async function output( + image: SharpImage, + name: RenderedVariant['name'], + ext: RenderedVariant['ext'], + contentType: RenderedVariant['contentType'], +): Promise { + const { data, info } = await image.toBuffer({ resolveWithObject: true }); + return rendered(name, ext, contentType, data, info.width, info.height); +} + +/** + * Renders the thumbnail, crop and optimized variants from one decoded source. + * The input dimensions are EXIF-oriented: `.rotate()` applies the orientation + * and `metadata().autoOrient` describes the result, so 300x200 orientation 6 + * becomes 200x300 before fitting. + */ +export async function renderVariants( + data: Buffer, + pipelineVersion: number, + limits: RenderLimits, +): Promise { + if ( + !(SUPPORTED_PIPELINE_VERSIONS as readonly number[]).includes( + pipelineVersion, + ) + ) + throw new UnsupportedPipelineError(pipelineVersion); + + const deadline = Date.now() + limits.timeoutSeconds * 1000; + throwIfAborted(limits.signal); + const metadata = await sharp(data, { + failOn: 'warning', + limitInputPixels: limits.maxPixels, + sequentialRead: true, + }) + .timeout({ seconds: remainingSeconds(deadline, limits.timeoutSeconds) }) + .metadata(); + throwIfAborted(limits.signal); + const width = metadata.autoOrient?.width ?? metadata.width; + const height = metadata.autoOrient?.height ?? metadata.height; + const [thumbnailWidth, thumbnailHeight] = fitInside(width, height, 256); + const [optimizedWidth, optimizedHeight] = fitInside(width, height, 1280); + const v2 = pipelineVersion >= 2; + + const thumbnail = v2 ? jpegV2 : jpegV1; + throwIfAborted(limits.signal); + remainingSeconds(deadline, limits.timeoutSeconds); + const thumbnailOutput = await output( + thumbnail( + input(data, limits, deadline) + .flatten({ background: '#ffffff' }) + .resize(thumbnailWidth, thumbnailHeight, { fit: 'fill' }), + ), + 'thumbnail', + 'jpg', + 'image/jpeg', + ); + + throwIfAborted(limits.signal); + remainingSeconds(deadline, limits.timeoutSeconds); + const cropOutput = await output( + thumbnail( + input(data, limits, deadline) + .flatten({ background: '#ffffff' }) + .resize(128, 128, { fit: 'cover', position: 'centre' }), + ), + 'crop', + 'jpg', + 'image/jpeg', + ); + + throwIfAborted(limits.signal); + remainingSeconds(deadline, limits.timeoutSeconds); + const optimizedOutput = await output( + input(data, limits, deadline) + .resize(optimizedWidth, optimizedHeight, { fit: 'fill' }) + .webp( + v2 + ? { quality: 82, effort: 5, smartSubsample: true } + : { quality: 75, effort: 4 }, + ), + 'optimized', + 'webp', + 'image/webp', + ); + + throwIfAborted(limits.signal); + return [thumbnailOutput, cropOutput, optimizedOutput]; +} diff --git a/app/src/worker/staging-cleaner.service.ts b/app/src/worker/staging-cleaner.service.ts new file mode 100644 index 00000000..49eeb8b2 --- /dev/null +++ b/app/src/worker/staging-cleaner.service.ts @@ -0,0 +1,92 @@ +import { + Injectable, + Logger, + OnModuleDestroy, + OnModuleInit, +} from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import type { AppConfig } from '../config/app-config.schema.js'; +import { JobsRepository } from '../jobs/jobs.repository.js'; +import { MetricsService } from '../metrics/metrics.service.js'; +import { ObjectStorage } from '../storage/object-storage.js'; + +const CLEANUP_BATCH = 100; + +/** + * Removes staging objects after the worker committed the final copy, and after + * the documented retention for failed jobs. A ready row waits one minute so a + * just-acknowledged job is never raced by the cleaner. + */ +@Injectable() +export class StagingCleaner implements OnModuleInit, OnModuleDestroy { + private readonly logger = new Logger(StagingCleaner.name); + private timer: NodeJS.Timeout | undefined; + private running = false; + + constructor( + private readonly config: ConfigService, + private readonly jobs: JobsRepository, + private readonly storage: ObjectStorage, + private readonly metrics: MetricsService, + ) {} + + onModuleInit(): void { + const interval = this.config.getOrThrow('STAGING_CLEANUP_INTERVAL_MS'); + if (interval <= 0) return; + this.timer = setInterval(() => { + if (this.running) return; + this.running = true; + this.cleanupOnce() + .catch((error: unknown) => { + this.metrics.increment('worker.staging.cleanup_error'); + this.logger.warn( + `Staging cleanup failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + }) + .finally(() => { + this.running = false; + }); + }, interval); + this.timer.unref(); + } + + onModuleDestroy(): void { + clearInterval(this.timer); + this.timer = undefined; + } + + async cleanupOnce(): Promise { + const candidates = await this.jobs.stagingCleanupCandidates( + this.config.getOrThrow('STAGING_RETENTION_SECONDS'), + CLEANUP_BATCH, + ); + let deleted = 0; + for (const candidate of candidates) { + try { + if ( + await this.jobs.clearStaging( + candidate.id, + candidate.staging_key, + (key) => this.storage.delete(key), + ) + ) + deleted += 1; + } catch (error) { + this.metrics.increment('worker.staging.cleanup_error'); + this.logger.warn( + `Staging cleanup of ${candidate.id} failed: ${error instanceof Error ? error.message : 'unknown error'}`, + ); + } + } + if (deleted > 0) this.metrics.add('worker.staging.deleted', deleted); + const outbox = await this.jobs.pruneOutbox( + this.config.getOrThrow('OUTBOX_RETENTION_SECONDS'), + ); + if (outbox > 0) this.metrics.add('jobs.outbox.pruned', outbox); + const events = await this.jobs.pruneEvents( + this.config.getOrThrow('JOB_EVENTS_RETENTION_SECONDS'), + ); + if (events > 0) this.metrics.add('jobs.events.pruned', events); + return deleted; + } +} diff --git a/app/src/worker/worker.module.ts b/app/src/worker/worker.module.ts new file mode 100644 index 00000000..8f409fd7 --- /dev/null +++ b/app/src/worker/worker.module.ts @@ -0,0 +1,26 @@ +import { Module } from '@nestjs/common'; +import { AppConfigModule } from '../config/app-config.module.js'; +import { DatabaseModule } from '../database/database.module.js'; +import { JobsModule } from '../jobs/jobs.module.js'; +import { OutboxRelay } from '../jobs/outbox-relay.service.js'; +import { MetricsModule } from '../metrics/metrics.module.js'; +import { RedisModule } from '../redis/redis.module.js'; +import { StorageModule } from '../storage/storage.module.js'; +import { JobProcessor } from './job-processor.service.js'; +import { LeaseReaper } from './lease-reaper.service.js'; +import { StagingCleaner } from './staging-cleaner.service.js'; + +@Module({ + imports: [ + AppConfigModule, + DatabaseModule, + RedisModule, + MetricsModule, + StorageModule, + JobsModule, + ], + // The worker publishes retry and lease-expiry outbox rows itself; the same + // relay also runs in the API process. SKIP LOCKED makes that safe. + providers: [JobProcessor, LeaseReaper, StagingCleaner, OutboxRelay], +}) +export class WorkerModule {} diff --git a/app/test/app.e2e-spec.ts b/app/test/app.e2e-spec.ts new file mode 100644 index 00000000..5d7cedca --- /dev/null +++ b/app/test/app.e2e-spec.ts @@ -0,0 +1,674 @@ +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { randomUUID } from 'node:crypto'; +import { setTimeout as sleep } from 'node:timers/promises'; +import { Pool } from 'pg'; +import { createClient } from 'redis'; +import type { SessionResponse } from '../src/auth/auth.service.js'; +import type { Chat } from '../src/chats/chat.js'; +import type { ApiErrorBody } from '../src/common/errors/api-error.js'; +import type { Page } from '../src/common/http/page.js'; +import type { Message } from '../src/messages/message.js'; +import type { User } from '../src/users/user.js'; + +// The suite is black-box: run it against the compiled application process. +// Vitest sets process.env.BASE_URL itself (Vite base), so the API URL uses its own name. +const baseUrl = (process.env.API_BASE_URL ?? 'http://localhost:8080').replace( + /\/+$/u, + '', +); +const basicChallenge = 'Basic realm="messenger", charset="UTF-8"'; +const bearerChallenge = 'Bearer realm="messenger"'; +const password = 'integration:пароль🔐'; +const runId = randomUUID().replaceAll('-', '').slice(0, 12); +const usernames: string[] = []; +const tokens: string[] = []; +let sequence = 0; +let database: Pool; +let redis: ReturnType; + +type Actor = { user: User; password: string; token: string }; +type HttpResult = { body: T; response: Response }; + +async function http( + path: string, + init: RequestInit = {}, +): Promise> { + const response = await fetch(`${baseUrl}${path}`, init); + const text = await response.text(); + let body: unknown = undefined; + if (text) { + try { + body = JSON.parse(text); + } catch { + body = text; + } + } + return { body: body as T, response }; +} + +function json( + body: unknown, + headers: Record = {}, +): RequestInit { + return { + method: 'POST', + headers: { 'content-type': 'application/json', ...headers }, + body: JSON.stringify(body), + }; +} + +function basic(username: string, secret: string): string { + return `Basic ${Buffer.from(`${username}:${secret}`, 'utf8').toString('base64')}`; +} + +function bearer(token: string): Record { + return { authorization: `Bearer ${token}` }; +} + +function idempotencyKey(): Record { + return { 'idempotency-key': randomUUID().replaceAll('-', '') }; +} + +function messageHeaders(token: string): Record { + return { ...bearer(token), ...idempotencyKey() }; +} + +async function register( + displayName = 'Интеграционный пользователь', + secret = password, +): Promise { + const username = `e2e_${runId}_${sequence++}`.slice(0, 32); + usernames.push(username); + const result = await http( + '/api/v1/users', + json({ username, display_name: displayName, password: secret }), + ); + expect(result.response.status).toBe(201); + return result.body; +} + +async function login(user: User, secret = password): Promise { + const result = await http('/api/v1/auth/sessions', { + method: 'POST', + headers: { authorization: basic(user.username, secret) }, + }); + expect(result.response.status).toBe(201); + tokens.push(result.body.token); + return result.body; +} + +async function actor(displayName?: string): Promise { + const user = await register(displayName); + const session = await login(user); + return { user, password, token: session.token }; +} + +function expectError( + result: HttpResult, + status: number, + code: string, +): void { + expect(result.response.status).toBe(status); + expect(result.body.error.code).toBe(code); + expect(result.body.error.request_id).toBe( + result.response.headers.get('x-request-id'), + ); +} + +describe('HTTP integration contract', () => { + beforeAll(async () => { + database = new Pool({ + connectionString: process.env.DATABASE_URL, + connectionTimeoutMillis: 5000, + }); + redis = createClient({ + url: process.env.REDIS_URL, + socket: { reconnectStrategy: false }, + }); + redis.on('error', () => {}); + await redis.connect(); + }); + + afterAll(async () => { + if (usernames.length) { + await database.query('BEGIN'); + try { + await database.query( + `DELETE FROM messages WHERE sender_id IN (SELECT id FROM users WHERE username = ANY($1::text[]))`, + [usernames], + ); + await database.query( + `DELETE FROM chats WHERE id IN (SELECT chat_id FROM chat_members WHERE user_id IN (SELECT id FROM users WHERE username = ANY($1::text[])))`, + [usernames], + ); + await database.query( + 'DELETE FROM users WHERE username = ANY($1::text[])', + [usernames], + ); + await database.query('COMMIT'); + } catch (error) { + await database.query('ROLLBACK'); + throw error; + } + } + if (redis.isOpen) redis.destroy(); + await database.end(); + }); + + it('serves liveness and requires real session authentication', async () => { + const live = await http<{ status: string }>('/health/live'); + expect(live.response.status).toBe(200); + expect(live.body).toEqual({ status: 'ok' }); + const denied = await http('/api/v1/users/me'); + expectError(denied, 401, 'unauthorized'); + expect(denied.response.headers.get('www-authenticate')).toBe( + bearerChallenge, + ); + }); + + it('measures string lengths in Unicode code points', async () => { + const emoji = '🔐'; + const username = (suffix: string) => + `e2e_${runId}_${suffix}`.slice(0, 32); + const rejected = await http( + '/api/v1/users', + json({ + username: username('cp_short'), + display_name: 'Code points', + password: emoji.repeat(11), + }), + ); + expectError(rejected, 400, 'invalid_request'); + const tooLongPassword = await http( + '/api/v1/users', + json({ + username: username('cp_long'), + display_name: 'Code points', + password: emoji.repeat(129), + }), + ); + expectError(tooLongPassword, 400, 'invalid_request'); + const tooLongName = await http( + '/api/v1/users', + json({ + username: username('cp_name'), + display_name: emoji.repeat(101), + password, + }), + ); + expectError(tooLongName, 400, 'invalid_request'); + + const shortUsername = username('cp_ok'); + usernames.push(shortUsername); + const boundaryPassword = emoji.repeat(12); + const created = await http( + '/api/v1/users', + json({ + username: shortUsername, + display_name: emoji.repeat(100), + password: boundaryPassword, + }), + ); + expect(created.response.status).toBe(201); + expect([...created.body.display_name].length).toBe(100); + // The stored password keeps all 12 code points: authentication works. + const session = await http('/api/v1/auth/sessions', { + method: 'POST', + headers: { + authorization: basic(shortUsername, boundaryPassword), + }, + }); + expect(session.response.status).toBe(201); + tokens.push(session.body.token); + + const longTitleRejected = await http( + '/api/v1/chats', + json( + { title: emoji.repeat(101), member_ids: [created.body.id] }, + bearer(session.body.token), + ), + ); + expectError(longTitleRejected, 400, 'invalid_request'); + const longTitle = await http( + '/api/v1/chats', + json( + { title: emoji.repeat(100), member_ids: [created.body.id] }, + bearer(session.body.token), + ), + ); + expect(longTitle.response.status).toBe(201); + const tooLongText = await http( + `/api/v1/chats/${longTitle.body.id}/messages`, + json({ text: emoji.repeat(4001) }, messageHeaders(session.body.token)), + ); + expectError(tooLongText, 400, 'invalid_request'); + const boundaryText = await http( + `/api/v1/chats/${longTitle.body.id}/messages`, + json({ text: emoji.repeat(4000) }, messageHeaders(session.body.token)), + ); + expect(boundaryText.response.status).toBe(201); + expect([...boundaryText.body.text].length).toBe(4000); + }); + + it('rejects whitespace-only and too-short credentials', async () => { + const username = `e2e_${runId}_${sequence++}`.slice(0, 32); + usernames.push(username); + const cases = [ + { password: ' '.repeat(12), display_name: 'Пользователь' }, + { password: '\t'.repeat(16), display_name: 'Пользователь' }, + { password: 'short-pass', display_name: 'Пользователь' }, + { password, display_name: ' ' }, + ]; + for (const body of cases) { + const result = await http( + '/api/v1/users', + json({ username, ...body }), + ); + expectError(result, 400, 'invalid_request'); + } + const accepted = await http( + '/api/v1/users', + json({ username, display_name: 'Пользователь', password }), + ); + expect(accepted.response.status).toBe(201); + }); + + it('registers, logs in with Unicode/colon password, and challenges malformed Basic', async () => { + const user = await register(); + const session = await login(user); + expect(session.user.id).toBe(user.id); + expect(session.token_type).toBe('Bearer'); + const me = await http('/api/v1/users/me', { + headers: bearer(session.token), + }); + expect(me.response.status).toBe(200); + expect(me.body.id).toBe(user.id); + for (const authorization of [ + 'Basic !!!', + 'Basic YWJj', + 'Bearer nope', + ]) { + const result = await http('/api/v1/auth/sessions', { + method: 'POST', + headers: { authorization }, + }); + expectError(result, 401, 'unauthorized'); + expect(result.response.headers.get('www-authenticate')).toBe( + basicChallenge, + ); + } + const bodyResult = await http('/api/v1/auth/sessions', { + ...json({ unexpected: true }), + headers: { authorization: basic(user.username, password) }, + }); + expectError(bodyResult, 400, 'invalid_request'); + }); + + it('returns conflict for concurrent duplicate registration', async () => { + const username = `e2e_${runId}_race`.slice(0, 32); + usernames.push(username); + const results = await Promise.all([ + http( + '/api/v1/users', + json({ username, display_name: 'Race A', password }), + ), + http( + '/api/v1/users', + json({ username, display_name: 'Race B', password }), + ), + ]); + expect( + results.filter((item) => item.response.status === 201), + 'duplicate registration must create exactly one user', + ).toHaveLength(1); + expect( + results.filter((item) => item.response.status === 409), + 'loser of the race must receive 409, not 500', + ).toHaveLength(1); + // The database unique index is the race-proof guard, so verify its effect. + const persisted = await database.query<{ count: string }>( + 'SELECT count(*)::text AS count FROM users WHERE username = $1', + [username], + ); + expect(persisted.rows[0]?.count).toBe('1'); + }); + + it('uses Bearer only, ignores X-User-Id spoofing, and prevents logout replay', async () => { + const alice = await actor('Алиса'); + const bob = await actor('Боб'); + const spoof = await http('/api/v1/users/me', { + headers: { ...bearer(alice.token), 'x-user-id': bob.user.id }, + }); + expect(spoof.response.status).toBe(200); + expect((spoof.body as unknown as User).id).toBe(alice.user.id); + const basicBusiness = await http('/api/v1/users/me', { + headers: { authorization: basic(alice.user.username, password) }, + }); + expectError(basicBusiness, 401, 'unauthorized'); + expect(basicBusiness.response.headers.get('www-authenticate')).toBe( + bearerChallenge, + ); + const logout = await http('/api/v1/auth/sessions/current', { + method: 'DELETE', + headers: bearer(alice.token), + }); + expect(logout.response.status).toBe(204); + const replay = await http('/api/v1/users/me', { + headers: bearer(alice.token), + }); + expectError(replay, 401, 'unauthorized'); + }); + + it('rejects unknown and duplicate chat members and hides chats from outsiders', async () => { + const alice = await actor(); + const bob = await actor(); + const outsider = await actor(); + const unknown = await http( + '/api/v1/chats', + json( + { title: 'unknown', member_ids: [alice.user.id, randomUUID()] }, + bearer(alice.token), + ), + ); + expectError(unknown, 400, 'invalid_request'); + const leaked = await http(`/api/v1/chats?limit=100`, { + headers: bearer(alice.token), + }); + expect(leaked.response.status).toBe(200); + expect( + (leaked.body as unknown as Page).items.some( + (item) => item.title === 'unknown', + ), + ).toBe(false); + const duplicate = await http( + '/api/v1/chats', + json( + { + title: 'duplicate', + member_ids: [alice.user.id, alice.user.id], + }, + bearer(alice.token), + ), + ); + expectError(duplicate, 400, 'invalid_request'); + const created = await http( + '/api/v1/chats', + json( + { + title: `chat-${runId}`, + member_ids: [alice.user.id, bob.user.id], + }, + bearer(alice.token), + ), + ); + expect(created.response.status).toBe(201); + const hidden = await http( + `/api/v1/chats/${created.body.id}`, + { headers: bearer(outsider.token) }, + ); + expectError(hidden, 404, 'not_found'); + }); + + it('keeps sender server-owned and makes writes visible to a second session', async () => { + const alice = await actor(); + const secondSession = (await login(alice.user)).token; + const chat = await http( + '/api/v1/chats', + json( + { title: `messages-${runId}`, member_ids: [alice.user.id] }, + bearer(alice.token), + ), + ); + expect(chat.response.status).toBe(201); + const blank = await http( + `/api/v1/chats/${chat.body.id}/messages`, + json( + { text: ' \n\t', sender_id: randomUUID() }, + messageHeaders(alice.token), + ), + ); + expectError(blank, 400, 'invalid_request'); + const spoof = await http( + `/api/v1/chats/${chat.body.id}/messages`, + json( + { text: 'hello', sender_id: randomUUID() }, + messageHeaders(alice.token), + ), + ); + expectError(spoof, 400, 'invalid_request'); + await http(`/api/v1/chats/${chat.body.id}/messages?limit=50`, { + headers: bearer(secondSession), + }); + const sent = await http( + `/api/v1/chats/${chat.body.id}/messages`, + json({ text: 'hello' }, messageHeaders(alice.token)), + ); + expect(sent.response.status).toBe(201); + expect(sent.body.sender_id).toBe(alice.user.id); + const page = await http>( + `/api/v1/chats/${chat.body.id}/messages?limit=50`, + { headers: bearer(secondSession) }, + ); + expect(page.response.status).toBe(200); + expect(page.body.items.map((item) => item.id)).toContain(sent.body.id); + }); + + it('validates cursor, limits, malformed JSON, and paginates without repeats', async () => { + const alice = await actor(); + const chat = await http( + '/api/v1/chats', + json( + { title: `pages-${runId}`, member_ids: [alice.user.id] }, + bearer(alice.token), + ), + ); + for (const text of ['one', 'two', 'three']) { + const sent = await http( + `/api/v1/chats/${chat.body.id}/messages`, + json({ text }, messageHeaders(alice.token)), + ); + expect(sent.response.status).toBe(201); + } + const invalidLimit = await http( + `/api/v1/chats/${chat.body.id}/messages?limit=101`, + { headers: bearer(alice.token) }, + ); + expectError(invalidLimit, 400, 'invalid_request'); + const invalidCursor = await http( + `/api/v1/chats/${chat.body.id}/messages?cursor=not-a-cursor`, + { headers: bearer(alice.token) }, + ); + expectError(invalidCursor, 400, 'invalid_request'); + const rolledOver = http( + `/api/v1/chats/${chat.body.id}/messages?cursor=${encodeURIComponent( + Buffer.from( + JSON.stringify({ + scope: `messages:${chat.body.id}`, + created_at: '2026-02-31T00:00:00.000Z', + id: randomUUID(), + }), + 'utf8', + ).toString('base64url'), + )}`, + { headers: bearer(alice.token) }, + ); + expectError(await rolledOver, 400, 'invalid_request'); + const malformed = await http('/api/v1/users', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: '{', + }); + expectError(malformed, 400, 'invalid_request'); + const first = await http>( + `/api/v1/chats/${chat.body.id}/messages?limit=2`, + { headers: bearer(alice.token) }, + ); + expect(first.body.items).toHaveLength(2); + const second = await http>( + `/api/v1/chats/${chat.body.id}/messages?limit=2&cursor=${encodeURIComponent(first.body.next_cursor!)}`, + { headers: bearer(alice.token) }, + ); + expect(second.body.items).toHaveLength(1); + expect( + new Set( + [...first.body.items, ...second.body.items].map( + (item) => item.id, + ), + ).size, + ).toBe(3); + }); + + it.skipIf(process.env.SESSION_TTL_SECONDS !== '2')( + 'expires sessions with TTL=2', + async () => { + const user = await register(); + const session = await login(user); + await sleep(2300); + const expired = await http('/api/v1/users/me', { + headers: bearer(session.token), + }); + expectError(expired, 401, 'unauthorized'); + }, + ); +}); + +// lab3: the Idempotency-Key contract of POST /chats/{id}/messages. The suite is +// black-box, so these assertions hold for one replica and for a load-balanced +// fleet alike. +describe('message idempotency', () => { + async function chatFor(token: string, memberIds: string[]): Promise { + const created = await http( + '/api/v1/chats', + json({ title: `idempotency-${runId}`, member_ids: memberIds }, bearer(token)), + ); + expect(created.response.status).toBe(201); + return created.body.id; + } + + async function textsIn(chatId: string, token: string): Promise { + const page = await http>( + `/api/v1/chats/${chatId}/messages?limit=100`, + { headers: bearer(token) }, + ); + expect(page.response.status).toBe(200); + return page.body.items.map((item) => item.text); + } + + it('requires a well-formed Idempotency-Key', async () => { + const alice = await actor(); + const chatId = await chatFor(alice.token, [alice.user.id]); + const path = `/api/v1/chats/${chatId}/messages`; + const missing = await http( + path, + json({ text: 'без ключа' }, bearer(alice.token)), + ); + expectError(missing, 400, 'invalid_request'); + for (const key of ['has space', 'comma,separated', 'x'.repeat(129)]) { + const malformed = await http( + path, + json({ text: 'плохой ключ' }, { ...bearer(alice.token), 'idempotency-key': key }), + ); + expectError(malformed, 400, 'invalid_request'); + } + }); + + it('replays the stored response and conflicts on a different body', async () => { + const alice = await actor(); + const chatId = await chatFor(alice.token, [alice.user.id]); + const path = `/api/v1/chats/${chatId}/messages`; + const key = randomUUID().replaceAll('-', ''); + const headers = { ...bearer(alice.token), 'idempotency-key': key }; + const text = `повтор ${runId}`; + + const first = await http(path, json({ text }, headers)); + expect(first.response.status).toBe(201); + const replayed = await http(path, json({ text }, headers)); + expect(replayed.response.status).toBe(201); + expect(replayed.body).toEqual(first.body); + + const conflict = await http( + path, + json({ text: 'другой body' }, headers), + ); + expectError(conflict, 409, 'conflict'); + + expect(await textsIn(chatId, alice.token)).toEqual([text]); + }); + + it('creates exactly one message for concurrent requests with one key', async () => { + const alice = await actor(); + const chatId = await chatFor(alice.token, [alice.user.id]); + const path = `/api/v1/chats/${chatId}/messages`; + const headers = { ...bearer(alice.token), ...idempotencyKey() }; + const text = `конкурентный повтор ${runId}`; + + const results = await Promise.all( + Array.from({ length: 8 }, () => http(path, json({ text }, headers))), + ); + for (const result of results) expect(result.response.status).toBe(201); + const ids = new Set(results.map((result) => result.body.id)); + expect( + ids.size, + 'one key must resolve to exactly one message across replicas', + ).toBe(1); + expect(await textsIn(chatId, alice.token)).toEqual([text]); + }); + + it('replays a key created by another replica', async () => { + // lab3: повтор с тем же ключом может прийти на любую реплику, поэтому + // проверяем не только сам повтор, но и то, что его обслужила другая реплика + // (иначе тест не отличал бы общий журнал от локального состояния процесса). + const alice = await actor(); + const chatId = await chatFor(alice.token, [alice.user.id]); + const path = `/api/v1/chats/${chatId}/messages`; + const key = randomUUID().replaceAll('-', ''); + const headers = { ...bearer(alice.token), 'idempotency-key': key }; + + const first = await http(path, json({ text: 'переключение реплики' }, headers)); + expect(first.response.status).toBe(201); + const firstInstance = first.response.headers.get('x-instance-id'); + expect(firstInstance).toBeTruthy(); + + let replay = first; + let replayInstance = firstInstance; + for (let attempt = 0; attempt < 12 && replayInstance === firstInstance; attempt += 1) { + replay = await http(path, json({ text: 'переключение реплики' }, headers)); + expect(replay.response.status).toBe(201); + replayInstance = replay.response.headers.get('x-instance-id'); + } + expect( + replayInstance, + 'повтор должен был прийти на другую реплику (round-robin за LB)', + ).not.toBe(firstInstance); + expect(replay.body).toEqual(first.body); + expect(await textsIn(chatId, alice.token)).toEqual(['переключение реплики']); + }); + + it('scopes the key per user and per chat path', async () => { + const alice = await actor(); + const bob = await actor(); + const outsider = await actor(); + const chatId = await chatFor(alice.token, [alice.user.id, bob.user.id]); + const path = `/api/v1/chats/${chatId}/messages`; + const key = randomUUID().replaceAll('-', ''); + const foreignChatId = await chatFor(outsider.token, [outsider.user.id]); + + const denied = await http( + `/api/v1/chats/${foreignChatId}/messages`, + json({ text: 'чужое' }, { ...bearer(alice.token), 'idempotency-key': key }), + ); + expectError(denied, 404, 'not_found'); + + const mine = await http( + path, + json({ text: 'своё' }, { ...bearer(alice.token), 'idempotency-key': key }), + ); + expect(mine.response.status).toBe(201); + const other = await http( + path, + json({ text: 'своё' }, { ...bearer(bob.token), 'idempotency-key': key }), + ); + expect(other.response.status).toBe(201); + expect(other.body.id).not.toBe(mine.body.id); + }); +}); diff --git a/app/test/attachments.e2e-spec.ts b/app/test/attachments.e2e-spec.ts new file mode 100644 index 00000000..e7bbcf89 --- /dev/null +++ b/app/test/attachments.e2e-spec.ts @@ -0,0 +1,542 @@ +import { createHash, randomBytes, randomUUID } from 'node:crypto'; +import { readFileSync } from 'node:fs'; +import net from 'node:net'; +import { setTimeout as sleep } from 'node:timers/promises'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import type { Pool } from 'pg'; +import type { ApiErrorBody } from '../src/common/errors/api-error.js'; +import type { Page } from '../src/common/http/page.js'; +import type { Message } from '../src/messages/message.js'; +import { + Actor, + actor, + baseUrl, + bearer, + createChat, + database, + download, + expectError, + http, + postMessage, + upload, + uploadRaw, + waitReady, +} from './support/e2e.js'; +import sharp from 'sharp'; +import { jpegWithMetadata, jpegWithoutScanData } from './support/jpeg.js'; +import { + maxPixelsRgba16Png, + noisePng, + padPngTo, + pixelBombPng, + pngWithGarbageIdat, +} from './support/png.js'; + +const png = readFileSync( + new URL('../../tests/fixtures/sample.png', import.meta.url), +); +const jpeg = readFileSync(new URL('./fixtures/sample.jpg', import.meta.url)); +const progressive = readFileSync( + new URL('./fixtures/sample-progressive.jpg', import.meta.url), +); +const MAX = 10_485_760; + +let db: Pool; +let alice: Actor; +let bob: Actor; +let eve: Actor; +let chatId: string; +let eveChatId: string; +let leftoverChatId: string; + +const sha256 = (data: Uint8Array) => + createHash('sha256').update(data).digest('hex'); + +async function ready(data: Buffer, type = 'image/png', chat = chatId) { + const result = await upload(alice.token, chat, data, { + contentType: type, + }); + expect(result.response.status).toBe(202); + expect(result.body.status).toBe('queued'); + return waitReady(alice.token, result.body.id); +} + +describe('attachments (proxy upload)', () => { + beforeAll(async () => { + db = database(); + alice = await actor(); + bob = await actor(); + eve = await actor(); + chatId = (await createChat(alice, [bob])).id; + eveChatId = (await createChat(eve, [])).id; + // Chat for rejected uploads: it must end up without any row. + leftoverChatId = (await createChat(alice, [])).id; + }); + + afterAll(async () => { + await db.end(); + }); + + it('stores a PNG and serves it only to chat members', async () => { + const uploaded = await upload(alice.token, chatId, png, { + filename: 'Снимок.png', + }); + expect(uploaded.response.status).toBe(202); + expect(uploaded.body).toMatchObject({ + chat_id: chatId, + owner_id: alice.user.id, + filename: 'Снимок.png', + content_type: 'image/png', + size_bytes: png.length, + sha256: sha256(png), + status: 'queued', + variants: [], + error_code: null, + }); + const id = uploaded.body.id; + + const ready = await waitReady(bob.token, id); + expect(ready).toMatchObject({ + filename: 'Снимок.png', + size_bytes: png.length, + sha256: sha256(png), + status: 'ready', + error_code: null, + }); + expect(ready.variants.map((variant) => variant.name)).toEqual([ + 'thumbnail', + 'crop', + 'optimized', + ]); + + const metadata = await http(`/api/v1/attachments/${id}`, { + headers: bearer(bob.token), + }); + expect(metadata.response.status).toBe(200); + expect(metadata.body.sha256).toBe(sha256(png)); + + const content = await download(bob.token, id); + expect(content.response.status).toBe(200); + expect(content.response.headers.get('content-type')).toBe('image/png'); + expect(content.response.headers.get('x-content-type-options')).toBe( + 'nosniff', + ); + expect(content.response.headers.get('content-length')).toBe( + String(png.length), + ); + expect(content.response.headers.get('content-disposition')).toContain( + "filename*=UTF-8''%D0%A1", + ); + expect(content.bytes.equals(png)).toBe(true); + + for (const path of [`/${id}`, `/${id}/content`]) { + expectError( + await http(`/api/v1/attachments${path}`, { + headers: bearer(eve.token), + }), + 404, + 'not_found', + ); + } + expectError( + await http(`/api/v1/attachments/${id}/content`), + 401, + 'unauthorized', + ); + expectError(await upload(eve.token, chatId, png), 404, 'not_found'); + expectError( + await http(`/api/v1/attachments/${randomUUID()}`, { + headers: bearer(bob.token), + }), + 404, + 'not_found', + ); + }); + + it.each([ + ['JPEG', jpeg], + ['progressive JPEG', progressive], + ])('accepts a %s', async (_name, data) => { + const created = await ready(data, 'image/jpeg'); + expect(created.content_type).toBe('image/jpeg'); + const content = await download(alice.token, created.id); + expect(content.bytes.equals(data)).toBe(true); + }); + + it('accepts camera-size images up to the pixel limit', async () => { + // 7728x5152 = 39.8 MP, like a 40 MP camera; decoded size far above the + // replica's 16 MiB /tmp, so libvips must not spill to a temp file. + const camera = sharp({ + create: { + width: 7728, + height: 5152, + channels: 3, + background: { r: 120, g: 80, b: 40 }, + }, + }); + const baseline = await camera.clone().jpeg({ quality: 85 }).toBuffer(); + const progressiveCamera = await camera + .clone() + .jpeg({ quality: 85, progressive: true }) + .toBuffer(); + for (const data of [baseline, progressiveCamera]) + expect((await ready(data, 'image/jpeg')).size_bytes).toBe( + data.length, + ); + const rgba16 = await maxPixelsRgba16Png(); + expect((await ready(rgba16)).size_bytes).toBe(rgba16.length); + }, 120_000); + + it('accepts a JPEG with megabytes of metadata before the frame header', async () => { + const data = jpegWithMetadata(jpeg, 20); + expect(data.length).toBeGreaterThan(1_048_576); + expect((await ready(data, 'image/jpeg')).sha256).toBe(sha256(data)); + }); + + it('authenticates before judging the content type', async () => { + const zip = await http( + `/api/v1/chats/${chatId}/attachments?filename=a.zip`, + { + method: 'POST', + headers: { 'content-type': 'application/zip' }, + body: png as unknown as BodyInit, + }, + ); + expectError(zip, 401, 'unauthorized'); + expectError( + await upload(alice.token, leftoverChatId, png, { + contentType: 'application/json', + }), + 415, + 'unsupported_media_type', + ); + }); + + it('answers 413 with the error envelope to an oversized text body on JSON routes', async () => { + const result = await http( + `/api/v1/chats/${chatId}/messages`, + { + method: 'POST', + headers: { + ...bearer(alice.token), + 'content-type': 'text/plain', + 'idempotency-key': randomUUID().replaceAll('-', ''), + }, + body: 'x'.repeat(2_100_000), + }, + ); + expectError(result, 413, 'payload_too_large'); + }); + + it('keeps one slow client from taking every upload slot', async () => { + const slow = await actor(); + const slowChat = (await createChat(slow, [])).id; + const url = new URL(baseUrl); + const sockets: net.Socket[] = []; + try { + for (let index = 0; index < 12; index += 1) { + const socket = net.connect( + Number(url.port || 80), + url.hostname, + ); + socket.on('error', () => {}); + socket.write( + `POST /api/v1/chats/${slowChat}/attachments?filename=slow.png HTTP/1.1\r\n` + + `Host: ${url.host}\r\nAuthorization: Bearer ${slow.token}\r\n` + + `Content-Type: image/png\r\nContent-Length: ${png.length}\r\n\r\n`, + ); + socket.write(png.subarray(0, 4)); + sockets.push(socket); + } + await sleep(1000); + const extra = await upload(slow.token, slowChat, png); + expectError(extra, 429, 'rate_limited'); + expect(extra.response.headers.get('retry-after')).toBe('1'); + // Another user is not affected by the slow client. + for (let index = 0; index < 4; index += 1) + expect( + (await upload(bob.token, chatId, png)).response.status, + ).toBe(202); + } finally { + for (const socket of sockets) socket.destroy(); + } + await settled(slowChat); + }); + + it('enforces the 10 MiB limit exactly', async () => { + const exact = padPngTo(png, MAX); + const created = await ready(exact); + expect(created.size_bytes).toBe(MAX); + expectError( + await upload(alice.token, leftoverChatId, padPngTo(png, MAX + 1)), + 413, + 'payload_too_large', + ); + }); + + it('rejects a pixel bomb before storing anything', async () => { + expectError( + await upload(alice.token, leftoverChatId, pixelBombPng()), + 413, + 'payload_too_large', + ); + }); + + it('rejects damaged images', async () => { + const flipped = Buffer.from(png); + flipped[Math.floor(png.length / 2)] ^= 0xff; + const cases: Buffer[] = [ + png.subarray(0, png.length - 100), + flipped, + Buffer.concat([png.subarray(0, 8), randomBytes(2048)]), + Buffer.alloc(0), + // Structure and CRCs are fine; the pixel data does not decode. + pngWithGarbageIdat(), + ]; + for (const data of cases) + expectError( + await upload(alice.token, leftoverChatId, data), + 415, + 'unsupported_media_type', + ); + }); + + it.each([ + ['baseline', jpeg], + ['progressive', progressive], + ])('rejects a %s JPEG without scan data', async (_name, data) => { + expectError( + await upload( + alice.token, + leftoverChatId, + jpegWithoutScanData(data), + { + contentType: 'image/jpeg', + }, + ), + 415, + 'unsupported_media_type', + ); + }); + + it('rejects a content type that does not match the bytes', async () => { + const gif = Buffer.from('GIF89a' + 'x'.repeat(64)); + const cases: [Buffer, string][] = [ + [png, 'image/jpeg'], + [jpeg, 'image/png'], + [gif, 'image/png'], + [png, 'text/plain'], + [png, 'application/octet-stream'], + ]; + for (const [data, type] of cases) + expectError( + await upload(alice.token, leftoverChatId, data, { + contentType: type, + }), + 415, + 'unsupported_media_type', + ); + }); + + it('validates filenames by code points and forbidden characters', async () => { + const bad = [ + '?filename=a%2Fb.png', + '?filename=a%5Cb.png', + '?filename=a%00b.png', + '?filename=a%07b.png', + '?filename=%E2%80%AEgnp.exe', + '?filename=%20', + '?filename=', + `?filename=${'a'.repeat(256)}`, + '', + '?filename=a.png&filename=b.png', + ]; + for (const query of bad) + expectError( + await uploadRaw(alice.token, leftoverChatId, query, png), + 400, + 'invalid_request', + ); + const long = '😀'.repeat(255); + const created = await upload(alice.token, chatId, png, { + filename: long, + }); + expect(created.response.status).toBe(202); + expect(created.body.filename).toBe(long); + }); + + it('links attachments to messages in the given order', async () => { + const first = await ready(png); + const second = await ready(jpeg, 'image/jpeg'); + const order = [second.id, first.id]; + const key = randomUUID().replaceAll('-', ''); + const sent = await postMessage( + alice.token, + chatId, + { text: '', attachment_ids: order }, + key, + ); + expect(sent.response.status).toBe(201); + expect(sent.body.attachment_ids).toEqual(order); + + const history = await http>( + `/api/v1/chats/${chatId}/messages?limit=100`, + { headers: bearer(bob.token) }, + ); + const stored = history.body.items.find((m) => m.id === sent.body.id); + expect(stored?.attachment_ids).toEqual(order); + + const replay = await postMessage( + alice.token, + chatId, + { text: '', attachment_ids: order }, + key, + ); + expect(replay.response.status).toBe(201); + expect(replay.body).toEqual(sent.body); + expectError( + await postMessage( + alice.token, + chatId, + { text: '', attachment_ids: [first.id] }, + key, + ), + 409, + 'conflict', + ); + + const textOnly = await postMessage(alice.token, chatId, { + text: 'hello', + }); + expect(textOnly.response.status).toBe(201); + expect(textOnly.body.attachment_ids).toEqual([]); + }); + + it('rejects invalid attachment references', async () => { + const mine = await ready(png); + const foreign = await upload(eve.token, eveChatId, png); + expect(foreign.response.status).toBe(202); + expectError( + await postMessage(alice.token, chatId, { text: '' }), + 400, + 'invalid_request', + ); + expectError( + await postMessage(alice.token, chatId, { + text: '', + attachment_ids: [foreign.body.id], + }), + 404, + 'not_found', + ); + expectError( + await postMessage(alice.token, chatId, { + text: 'x', + attachment_ids: [randomUUID()], + }), + 404, + 'not_found', + ); + for (const ids of [ + [mine.id, mine.id], + [mine.id, mine.id.toUpperCase()], + Array.from({ length: 11 }, () => randomUUID()), + null, + ['not-a-uuid'], + ]) + expectError( + await postMessage(alice.token, chatId, { + text: 'x', + attachment_ids: ids, + }), + 400, + 'invalid_request', + ); + }); + + it('serves the promised variants only after ready', async () => { + const uploaded = await upload(alice.token, chatId, png); + expect(uploaded.response.status).toBe(202); + expectError( + await http( + `/api/v1/attachments/${uploaded.body.id}/content`, + { headers: bearer(alice.token) }, + ), + 409, + 'conflict', + ); + const created = await waitReady(alice.token, uploaded.body.id); + const variant = (name: string) => + http( + `/api/v1/attachments/${created.id}/content?variant=${name}`, + { + headers: bearer(alice.token), + }, + ); + expect((await variant('original')).response.status).toBe(200); + for (const [name, type] of [ + ['thumbnail', 'image/jpeg'], + ['crop', 'image/jpeg'], + ['optimized', 'image/webp'], + ] as const) { + const response = await variant(name); + expect(response.response.status).toBe(200); + expect(response.response.headers.get('content-type')).toBe(type); + } + expectError(await variant('bogus'), 400, 'invalid_request'); + }); + + it('answers 4xx, not 500, for raw image or text bodies on JSON routes', async () => { + for (const [type, status, code] of [ + ['image/png', 415, 'unsupported_media_type'], + ['image/jpeg', 415, 'unsupported_media_type'], + ['text/plain', 400, 'invalid_request'], + ] as const) { + const result = await http('/api/v1/users', { + method: 'POST', + headers: { 'content-type': type }, + body: 'x', + }); + expectError(result, status, code); + } + }); + + it('removes the pending row when the client drops the connection', async () => { + const data = noisePng(2 * 1024 * 1024); + const url = new URL(baseUrl); + const socket = net.connect(Number(url.port || 80), url.hostname); + await new Promise((resolve, reject) => { + socket.once('connect', resolve); + socket.once('error', reject); + }); + socket.on('error', () => {}); + socket.write( + `POST /api/v1/chats/${leftoverChatId}/attachments?filename=cut.png HTTP/1.1\r\n` + + `Host: ${url.host}\r\nAuthorization: Bearer ${alice.token}\r\n` + + `Content-Type: image/png\r\nContent-Length: ${data.length}\r\n\r\n`, + ); + socket.write(data.subarray(0, 512 * 1024)); + await sleep(500); + socket.destroy(); + await settled(leftoverChatId); + }); + + it('leaves no rows behind after rejected uploads', async () => { + await settled(leftoverChatId); + }); +}); + +async function settled(chat: string): Promise { + const deadline = Date.now() + 10_000; + let count = -1; + while (Date.now() < deadline) { + const result = await db.query<{ count: string }>( + 'SELECT count(*) FROM attachments WHERE chat_id = $1', + [chat], + ); + count = Number(result.rows[0].count); + if (count === 0) return; + await sleep(250); + } + expect(count).toBe(0); +} diff --git a/app/test/dependency-failures.e2e-spec.ts b/app/test/dependency-failures.e2e-spec.ts new file mode 100644 index 00000000..9b274f5b --- /dev/null +++ b/app/test/dependency-failures.e2e-spec.ts @@ -0,0 +1,1107 @@ +import { execFile } from 'node:child_process'; +import { randomUUID } from 'node:crypto'; +import { setTimeout as sleep } from 'node:timers/promises'; +import { readFileSync } from 'node:fs'; +import net from 'node:net'; +import { fileURLToPath } from 'node:url'; +import { promisify } from 'node:util'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import type { SessionResponse } from '../src/auth/auth.service.js'; +import type { Chat } from '../src/chats/chat.js'; +import type { ApiErrorBody } from '../src/common/errors/api-error.js'; +import type { Page } from '../src/common/http/page.js'; +import type { Message } from '../src/messages/message.js'; +import type { User } from '../src/users/user.js'; +import { noisePng } from './support/png.js'; +import { waitReady } from './support/e2e.js'; + +// Automated counterpart of the manual Redis/PostgreSQL outage checks. Four +// topologies, because the load balancer probes /health/ready (the same route as +// the container healthcheck): +// 1. Redis outage — shared dependency: every replica turns not-ready, HAProxy +// removes all backends and answers 503 itself, while the replica's own +// readiness stays a bounded API 503 with request_id. +// 2. PostgreSQL outage — same shared-dependency topology, confirmed data and +// the session survive the restart. +// 3. Per-replica PostgreSQL fault — only the selected replica loses its own +// path to the database (temporary pg_hba reject entry for its address plus +// connection termination): that slot leaves rotation, the healthy replica +// keeps serving, and the fault is always undone. +// 4. Pool saturation — a busy shared pool is overload, not a lost dependency: +// the replicas stay in rotation because the probe uses its own connection. +// +// It is opt-in because it needs control over the Docker Compose project: +// make test-dependencies +// cd app && E2E_DEPENDENCY_FAULTS=1 npm run test:dependencies +const enabled = process.env.E2E_DEPENDENCY_FAULTS === '1'; +const baseUrl = (process.env.API_BASE_URL ?? 'http://localhost:8080').replace( + /\/+$/u, + '', +); +const composeDir = + process.env.E2E_COMPOSE_DIR ?? + fileURLToPath(new URL('../..', import.meta.url)); +const composeEnvFile = process.env.E2E_COMPOSE_ENV_FILE ?? '.env'; +const redisService = process.env.E2E_REDIS_SERVICE ?? 'redis'; +const postgresService = process.env.E2E_POSTGRES_SERVICE ?? 'postgres'; +const apiService = process.env.E2E_API_SERVICE ?? 'api'; +const s3Service = process.env.E2E_S3_SERVICE ?? 's3'; +const s3Bucket = process.env.E2E_S3_BUCKET ?? 'messenger-attachments'; +const lbStatsUrl = + process.env.E2E_LB_STATS_URL ?? 'http://127.0.0.1:8404/stats;csv'; +// PostgreSQL superuser of the stand (compose.yaml, .env); psql runs over the +// container's local socket, so no password is needed here. +const postgresUser = process.env.E2E_POSTGRES_USER ?? 'messenger'; +const postgresDatabase = process.env.E2E_POSTGRES_DB ?? 'messenger'; +const pgHbaPath = + process.env.E2E_PG_HBA_PATH ?? '/var/lib/postgresql/data/pg_hba.conf'; +const pgHbaBackup = `${pgHbaPath}.lab3-partial-fault.bak`; +const password = 'outage-check:пароль🔐'; +const runId = randomUUID().replaceAll('-', '').slice(0, 12); +// A request that reaches a stopped database waits for the pool connection +// timeout, so every outage step costs up to DATABASE_CONNECTION_TIMEOUT_MS. +const outagedRequestBudgetMs = 180_000; +const slotBudgetMs = 20_000; +const readyReads = 16; +const saturatingWrites = 60; // больше, чем replicas × DATABASE_POOL_MAX (2 × 10) + +const execFileAsync = promisify(execFile); + +type Result = { body: T; text: string; response: Response }; +type ReplicaProbe = { + instanceId: string | null; + requestId: string | null; + status: number; + text: string; +}; +type LbSlot = { addr: string; check: string; name: string; status: string }; + +async function compose(...args: string[]): Promise { + const result = await execFileAsync( + 'docker', + ['compose', '--env-file', composeEnvFile, ...args], + { cwd: composeDir, timeout: 120_000 }, + ); + return result.stdout.trim(); +} + +async function docker(...args: string[]): Promise { + const result = await execFileAsync('docker', args, { + cwd: composeDir, + timeout: 60_000, + }); + return result.stdout.trim(); +} + +async function request( + path: string, + init: RequestInit = {}, +): Promise> { + const response = await fetch(`${baseUrl}${path}`, init); + const text = await response.text(); + // With every backend out of rotation the answer is the LB's own HTML 503, + // so a body is not guaranteed to be JSON (it always is for API answers). + let body: T | undefined; + try { + body = text ? (JSON.parse(text) as T) : undefined; + } catch { + body = undefined; + } + return { body: body as T, text, response }; +} + +function json( + body: unknown, + headers: Record = {}, +): RequestInit { + return { + method: 'POST', + headers: { 'content-type': 'application/json', ...headers }, + body: JSON.stringify(body), + }; +} + +function bearer(token: string): Record { + return { authorization: `Bearer ${token}` }; +} + +// POST messages needs a unique Idempotency-Key in lab3+; outage checks only +// need the write path, not replay semantics. +function messageHeaders(token: string): Record { + return { + ...bearer(token), + 'idempotency-key': randomUUID().replaceAll('-', ''), + }; +} + +function basic(username: string, secret: string): string { + return `Basic ${Buffer.from(`${username}:${secret}`, 'utf8').toString('base64')}`; +} + +// The dependencies recover asynchronously, so readiness is polled instead of +// asserted right after docker compose returns. +async function waitForReadiness( + expected: number, + timeoutMs = 90_000, +): Promise { + const deadline = Date.now() + timeoutMs; + let last: number | string = 'no response'; + for (;;) { + try { + const probe = await request('/health/ready'); + last = probe.response.status; + if (probe.response.status === expected) return; + } catch (error) { + last = error instanceof Error ? error.message : 'request failed'; + } + if (Date.now() > deadline) + throw new Error( + `readiness never became ${expected} within ${timeoutMs}ms (last: ${String(last)})`, + ); + await sleep(250); + } +} + +async function waitFor( + condition: () => Promise, + describe: () => Promise, + timeoutMs: number, +): Promise { + const deadline = Date.now() + timeoutMs; + for (;;) { + if (await condition()) return; + if (Date.now() > deadline) + throw new Error( + `condition not reached within ${timeoutMs}ms: ${await describe()}`, + ); + await sleep(250); + } +} + +function expectBoundedError( + status: number, + text: string, + requestId: string | null, +): void { + expect(status).toBe(503); + const body = JSON.parse(text) as ApiErrorBody; + expect(body.error.code).toBe('unavailable'); + expect(body.error.message).toBe('Service unavailable'); + expect(body.error.request_id).toBe(requestId); + // A bounded error must not leak driver details, SQL text or a stack trace. + expect(text).not.toMatch( + /ECONNREFUSED|ECONNRESET|Connection terminated|pg\.|node_modules|at .*\(.+:\d+:\d+\)/u, + ); +} + +function expectBoundedOutage(result: Result): void { + expectBoundedError( + result.response.status, + result.text, + result.response.headers.get('x-request-id'), + ); +} + +// With every backend out of rotation the last hop is the load balancer itself: +// it answers with its own 503 page, so there is no API error body and no +// X-Instance-Id — the request never reached a replica. +function expectLbOutage(result: Result): void { + expect(result.response.status).toBe(503); + expect(result.response.headers.get('x-instance-id')).toBeNull(); +} + +// What a client sees while a shared dependency is down: either the bounded 503 +// of a replica that is still in rotation (identified by X-Instance-Id), or the +// LB's own 503 once every replica has left rotation. Never a success. +function expectUnavailableThroughLb(result: Result): void { + expect(result.response.status).toBe(503); + if (result.response.headers.get('x-instance-id') === null) return; + expectBoundedOutage(result); +} + +async function historyOf( + chatId: string, + token: string, +): Promise>> { + return request>( + `/api/v1/chats/${chatId}/messages?limit=100`, + { + headers: bearer(token), + }, + ); +} + +async function apiContainers(): Promise { + const stdout = await compose('ps', '-q', apiService); + return stdout + .split('\n') + .map((line) => line.trim()) + .filter((line) => line.length > 0) + .sort(); +} + +async function containerIp(container: string): Promise { + return docker( + 'inspect', + '-f', + '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}', + container, + ); +} + +// The replicas publish no ports, so a single replica is probed from inside its +// own network namespace (loopback). +const probeScript = ` +const params = JSON.parse(process.env.LAB3_PROBE); +const port = process.env.APP_PORT || 8080; +const response = await fetch('http://127.0.0.1:' + port + params.path, { + method: params.method || 'GET', + headers: params.headers || {}, + body: params.body, +}); +const text = await response.text(); +console.log(JSON.stringify({ + status: response.status, + text, + instanceId: response.headers.get('x-instance-id'), + requestId: response.headers.get('x-request-id'), +})); +`; + +type ProbeInit = { + body?: string; + headers?: Record; + method?: string; +}; + +async function probeReplica( + container: string, + path: string, + init: ProbeInit = {}, +): Promise { + const stdout = await docker( + 'exec', + '-e', + `LAB3_PROBE=${JSON.stringify({ path, ...init })}`, + container, + 'node', + '--input-type=module', + '-e', + probeScript, + ); + return JSON.parse(stdout) as ReplicaProbe; +} + +async function waitForReplicaReadiness( + container: string, + expected: number, + timeoutMs: number, +): Promise { + let last = 'no probe'; + await waitFor( + async () => { + try { + const probe = await probeReplica(container, '/health/ready'); + last = `${probe.status} ${probe.text}`; + return probe.status === expected; + } catch (error) { + last = error instanceof Error ? error.message : 'probe failed'; + return false; + } + }, + async () => last, + timeoutMs, + ); +} + +function parseCsvLine(line: string): string[] { + const values: string[] = []; + let current = ''; + let quoted = false; + for (const character of line) { + if (character === '"') quoted = !quoted; + else if (character === ',' && !quoted) { + values.push(current); + current = ''; + } else current += character; + } + values.push(current.replace(/\r$/u, '')); + return values; +} + +// Resolved backend slots of the api backend: the unused slots of the +// server-template have no address and are ignored. +async function lbSlots(): Promise { + const response = await fetch(lbStatsUrl); + const lines = (await response.text()).trim().split('\n'); + const header = parseCsvLine(lines[0].replace(/^#\s*/u, '')).map((name) => + name.trim(), + ); + const column = (name: string): number => header.indexOf(name); + return lines + .slice(1) + .map(parseCsvLine) + .filter( + (columns) => + columns[column('pxname')] === 'api' && + /^api\d+$/u.test(columns[column('svname')] ?? '') && + (columns[column('addr')] ?? '').length > 0, + ) + .map((columns) => ({ + addr: columns[column('addr')], + check: columns[column('check_status')], + name: columns[column('svname')], + status: columns[column('status')], + })); +} + +async function waitForSlot( + predicate: (slots: LbSlot[]) => boolean, + describe: string, + timeoutMs = slotBudgetMs, +): Promise { + let last = 'no stats'; + await waitFor( + async () => { + try { + const slots = await lbSlots(); + last = slots + .map( + (slot) => + `${slot.name}@${slot.addr} ${slot.status}/${slot.check}`, + ) + .join(', '); + return slots.length > 0 && predicate(slots); + } catch (error) { + last = error instanceof Error ? error.message : 'stats failed'; + return false; + } + }, + async () => `${describe} (last: ${last})`, + timeoutMs, + ); + return lbSlots(); +} + +function upSlots(slots: LbSlot[]): LbSlot[] { + return slots.filter((slot) => slot.status === 'UP'); +} + +async function psql(query: string): Promise { + return dockerExecService( + postgresService, + 'psql', + '-U', + postgresUser, + '-d', + postgresDatabase, + '-tA', + '-c', + query, + ); +} + +// docker exec needs a container, while the suite is configured with Compose +// service names, so the container of the service is resolved first. +async function dockerExecService( + service: string, + ...args: string[] +): Promise { + const container = (await compose('ps', '-q', service)) + .split('\n')[0] + ?.trim(); + if (!container) throw new Error(`no container for service ${service}`); + return docker('exec', container, ...args); +} + +// Takes away the replica's own path to PostgreSQL without touching the shared +// database: a reject entry for the replica's address becomes the first matching +// line of pg_hba.conf, so its new connections fail while the server stays up +// for everyone else. The file is rewritten through the existing inode (`cat > +// file`) to preserve the owner and mode the server needs to reload it. +async function applyReplicaDatabaseReject(address: string): Promise { + const script = [ + `test -f ${pgHbaBackup} || cat ${pgHbaPath} > ${pgHbaBackup}`, + `printf 'host all all ${address}/32 reject\\n' > /tmp/lab3-pg-hba`, + `cat ${pgHbaPath} >> /tmp/lab3-pg-hba`, + `cat /tmp/lab3-pg-hba > ${pgHbaPath}`, + 'rm -f /tmp/lab3-pg-hba', + ].join('; '); + await dockerExecService(postgresService, 'sh', '-c', script); + await psql('select pg_reload_conf()'); + await psql( + `select count(pg_terminate_backend(pid)) from pg_stat_activity where client_addr = '${address}'`, + ); +} + +// Idempotent restore: also runs from afterAll, so an assertion that failed +// mid-fault cannot leave the stand rejecting one replica. +async function restoreDatabaseAccess(): Promise { + await dockerExecService( + postgresService, + 'sh', + '-c', + `if [ -f ${pgHbaBackup} ]; then cat ${pgHbaBackup} > ${pgHbaPath}; rm -f ${pgHbaBackup}; fi`, + ); + await psql('select pg_reload_conf()'); +} + +const samplePng = readFileSync( + new URL('../../tests/fixtures/sample.png', import.meta.url), +); + +function secretFile(name: string): string { + return readFileSync(`${composeDir}/secrets/${name}`, 'utf8').trim(); +} + +// Root credentials stay inside the local process and the container's exec +// environment; they are never printed. +async function objectExists(key: string): Promise { + const host = `http://${secretFile('minio_root_user')}:${secretFile('minio_root_password')}@127.0.0.1:9000`; + try { + await dockerExecService( + s3Service, + 'env', + `MC_HOST_stand=${host}`, + 'mc', + 'stat', + `stand/${s3Bucket}/${key}`, + ); + return true; + } catch { + return false; + } +} + +async function waitForS3Healthy(): Promise { + const container = (await compose('ps', '-q', s3Service)).split('\n')[0]; + await waitFor( + async () => + (await docker( + 'inspect', + '-f', + '{{.State.Health.Status}}', + container, + ).catch(() => '')) === 'healthy', + async () => 's3 is not healthy', + 60_000, + ); +} + +function uploadPath(chatId: string, filename: string): string { + return `/api/v1/chats/${chatId}/attachments?filename=${encodeURIComponent(filename)}`; +} + +async function sweepNow(): Promise { + await compose( + 'exec', + '-T', + '-e', + 'ATTACHMENT_SWEEP_GRACE_SECONDS=0', + apiService, + 'node', + 'dist/attachments/attachments-sweep.cli.js', + ); +} + +async function waitUntilExpired(filter: string): Promise { + await waitFor( + async () => + Number( + await psql( + `select count(*) from attachments where ${filter} and expires_at < now()`, + ), + ) > 0, + async () => 'pending row did not expire', + 60_000, + ); +} + +async function rawUpload( + data: Buffer, + chatId: string, + filename: string, + token: string, + splitAt: number, + between: () => Promise, +): Promise { + const url = new URL(baseUrl); + const socket = net.connect(Number(url.port || 80), url.hostname); + await new Promise((resolve, reject) => { + socket.once('connect', resolve); + socket.once('error', reject); + }); + const chunks: Buffer[] = []; + socket.on('data', (chunk: Buffer) => chunks.push(chunk)); + const closed = new Promise((resolve) => socket.on('close', resolve)); + socket.write( + `POST ${uploadPath(chatId, filename)} HTTP/1.1\r\nHost: ${url.host}\r\n` + + `Authorization: Bearer ${token}\r\nContent-Type: image/png\r\n` + + `Content-Length: ${data.length}\r\nConnection: close\r\n\r\n`, + ); + socket.write(data.subarray(0, splitAt)); + await between(); + socket.write(data.subarray(splitAt)); + await closed; + return Buffer.concat(chunks).toString('latin1'); +} + +describe.skipIf(!enabled)('dependency outages', () => { + let user: User; + let token: string; + let chat: Chat; + let message: Message; + let replicas: string[]; + + beforeAll(async () => { + await compose('up', '-d'); + await waitForReadiness(200); + const username = `outage_${runId}`; + const registered = await request( + '/api/v1/users', + json({ username, display_name: 'Отказ зависимости', password }), + ); + expect(registered.response.status).toBe(201); + user = registered.body; + const session = await request( + '/api/v1/auth/sessions', + { + method: 'POST', + headers: { authorization: basic(username, password) }, + }, + ); + expect(session.response.status).toBe(201); + token = session.body.token; + const created = await request( + '/api/v1/chats', + json( + { title: `outage-${runId}`, member_ids: [user.id] }, + bearer(token), + ), + ); + expect(created.response.status).toBe(201); + chat = created.body; + const sent = await request( + `/api/v1/chats/${chat.id}/messages`, + json({ text: 'Сообщение до отказа' }, messageHeaders(token)), + ); + expect(sent.response.status).toBe(201); + message = sent.body; + replicas = await apiContainers(); + expect(replicas.length).toBeGreaterThanOrEqual(2); + await waitForSlot( + (slots) => upSlots(slots).length === replicas.length, + 'not every replica is UP', + 30_000, + ); + }, outagedRequestBudgetMs); + + afterAll(async () => { + // Always restore the stand, even when an assertion failed mid-outage: + // first the services (pg_hba is only reachable in a running container), + // then the temporary per-replica rule, then rotation. + await compose('start', redisService); + await compose('start', postgresService); + await compose('start', s3Service); + const s3Container = (await compose('ps', '-q', s3Service)).split( + '\n', + )[0]; + await docker('unpause', s3Container).catch(() => undefined); + await waitForReadiness(200); + await restoreDatabaseAccess(); + const expected = (await apiContainers()).length; + await waitForSlot( + (slots) => upSlots(slots).length === expected, + 'backends did not return to rotation', + 30_000, + ); + }, outagedRequestBudgetMs); + + it( + 'keeps every replica in rotation while the database pool is saturated', + async () => { + // A row lock on the chat parks every write in the shared pool; the + // readiness probe uses its own connection and must keep answering. + const lock = psql( + `begin; select id from chats where id = '${chat.id}' for update; select pg_sleep(4); commit;`, + ); + await sleep(300); + const statuses = new Set(); + let sampling = true; + const sampler = (async () => { + while (sampling) { + for (const slot of await lbSlots()) + statuses.add(slot.status); + await sleep(200); + } + })(); + const writes = await Promise.all( + Array.from({ length: saturatingWrites }, () => + request( + `/api/v1/chats/${chat.id}/messages`, + json({ text: 'saturation' }, messageHeaders(token)), + ), + ), + ); + await lock; + await sleep(1000); + sampling = false; + await sampler; + expect([...statuses]).toEqual(['UP']); + expect( + writes + .map((write) => write.response.status) + .filter((status) => status !== 201), + ).toEqual([]); + }, + outagedRequestBudgetMs, + ); + + it( + 'serves bounded 503 for the Redis outage and recovers the session', + async () => { + await compose('stop', redisService); + const replica = replicas[0]; + + expect((await probeReplica(replica, '/health/live')).status).toBe( + 200, + ); + await waitForReplicaReadiness(replica, 503, 30_000); + const readiness = await probeReplica(replica, '/health/ready'); + expectBoundedError( + readiness.status, + readiness.text, + readiness.requestId, + ); + + const me = await probeReplica(replica, '/api/v1/users/me', { + headers: bearer(token), + }); + expectBoundedError(me.status, me.text, me.requestId); + + // A request without a session must stay unauthorized during an outage: + // the outage may not turn an anonymous caller into a trusted one. + const anonymous = await probeReplica(replica, '/api/v1/chats', { + headers: { 'x-user-id': user.id }, + }); + expect(anonymous.status).toBe(401); + expect( + (JSON.parse(anonymous.text) as ApiErrorBody).error.code, + ).toBe('unauthorized'); + + expectUnavailableThroughLb( + await request('/api/v1/users/me', { + headers: bearer(token), + }), + ); + await waitForSlot( + // 'UP 1/2' is still in rotation: wait for DOWN itself. + (slots) => slots.every((slot) => slot.status === 'DOWN'), + 'not every slot left rotation', + ); + expectLbOutage( + await request('/api/v1/users/me', { headers: bearer(token) }), + ); + + await compose('start', redisService); + await waitForReadiness(200); + await waitForSlot( + (slots) => upSlots(slots).length === replicas.length, + 'backends did not return to rotation', + 30_000, + ); + + // Sessions live in Redis with persistence, so the old token still works. + const recovered = await request('/api/v1/users/me', { + headers: bearer(token), + }); + expect(recovered.response.status).toBe(200); + expect(recovered.body.id).toBe(user.id); + + const history = await historyOf(chat.id, token); + expect(history.response.status).toBe(200); + expect(history.body.items.map((item) => item.id)).toContain( + message.id, + ); + }, + outagedRequestBudgetMs, + ); + + it( + 'serves bounded 503 for the PostgreSQL outage and keeps confirmed data', + async () => { + await compose('stop', postgresService); + const replica = replicas[0]; + + expect((await probeReplica(replica, '/health/live')).status).toBe( + 200, + ); + await waitForReplicaReadiness(replica, 503, 30_000); + const readiness = await probeReplica(replica, '/health/ready'); + expectBoundedError( + readiness.status, + readiness.text, + readiness.requestId, + ); + + // Redis still answers, so these requests reach the database layer and + // must fail with the bounded 503, not an unhandled 500. + const me = await probeReplica(replica, '/api/v1/users/me', { + headers: bearer(token), + }); + expectBoundedError(me.status, me.text, me.requestId); + + const chats = await probeReplica(replica, '/api/v1/chats', { + headers: bearer(token), + }); + expectBoundedError(chats.status, chats.text, chats.requestId); + + const history = await probeReplica( + replica, + `/api/v1/chats/${chat.id}/messages?limit=100`, + { headers: bearer(token) }, + ); + expectBoundedError(history.status, history.text, history.requestId); + + const registration = await probeReplica(replica, '/api/v1/users', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ + username: `outage_${runId}_late`, + display_name: 'Отказ зависимости', + password, + }), + }); + expectBoundedError( + registration.status, + registration.text, + registration.requestId, + ); + + expectUnavailableThroughLb( + await request('/api/v1/users/me', { + headers: bearer(token), + }), + ); + await waitForSlot( + // 'UP 1/2' is still in rotation: wait for DOWN itself. + (slots) => slots.every((slot) => slot.status === 'DOWN'), + 'not every slot left rotation', + ); + expectLbOutage( + await request('/api/v1/users/me', { headers: bearer(token) }), + ); + + await compose('start', postgresService); + await waitForReadiness(200); + await waitForSlot( + (slots) => upSlots(slots).length === replicas.length, + 'backends did not return to rotation', + 30_000, + ); + + const recovered = await request('/api/v1/users/me', { + headers: bearer(token), + }); + expect(recovered.response.status).toBe(200); + expect(recovered.body.id).toBe(user.id); + + const after = await historyOf(chat.id, token); + expect(after.response.status).toBe(200); + expect(after.body.items.map((item) => item.id)).toContain( + message.id, + ); + + const sent = await request( + `/api/v1/chats/${chat.id}/messages`, + json({ text: 'Сообщение после отказа' }, messageHeaders(token)), + ); + expect(sent.response.status).toBe(201); + }, + outagedRequestBudgetMs, + ); + + it( + 'excludes only the replica that lost its own database path', + async () => { + const selected = replicas[0]; + const healthy = replicas[1]; + const selectedIp = await containerIp(selected); + const selectedAddress = `${selectedIp}:8080`; + const selectedInstance = ( + await probeReplica(selected, '/health/live') + ).instanceId; + expect(selectedInstance).not.toBeNull(); + const healthyInstance = ( + await probeReplica(healthy, '/health/live') + ).instanceId; + + try { + await applyReplicaDatabaseReject(selectedIp); + const faultAt = Date.now(); + + await waitForReplicaReadiness(selected, 503, 30_000); + expect( + (await probeReplica(healthy, '/health/ready')).status, + ).toBe(200); + + // Only the affected slot leaves rotation. While `fall 2` counts the + // failed checks the slot passes through "UP 1/2 (L7STS)", so the + // wait is for DOWN itself, not for "no longer plain UP". + const slots = await waitForSlot( + (candidates) => + upSlots(candidates).length === replicas.length - 1 && + candidates.some( + (slot) => + slot.addr === selectedAddress && + slot.status === 'DOWN', + ), + `selected slot ${selectedAddress} did not go down`, + 30_000, + ); + // inter 500 ms × fall 2 + timeout check 500 ms + polling step 250 ms. + expect(Date.now() - faultAt).toBeLessThan(3_000); + expect( + slots.find((slot) => slot.addr === selectedAddress)?.status, + ).toBe('DOWN'); + + const served = new Set(); + for (let index = 0; index < readyReads; index += 1) { + const read = await historyOf(chat.id, token); + expect(read.response.status).toBe(200); + served.add( + read.response.headers.get('x-instance-id') ?? 'unknown', + ); + } + expect(served.has(selectedInstance as string)).toBe(false); + expect(served.has(healthyInstance as string)).toBe(true); + } finally { + await restoreDatabaseAccess(); + } + + await waitForReplicaReadiness(selected, 200, 30_000); + await waitForSlot( + (slots) => + upSlots(slots).length === replicas.length && + slots.some( + (slot) => + slot.addr === selectedAddress && + slot.status === 'UP', + ), + 'restored replica did not return to rotation', + 30_000, + ); + + let servedAgain = false; + for ( + let index = 0; + index < readyReads * 2 && !servedAgain; + index += 1 + ) { + const read = await historyOf(chat.id, token); + expect(read.response.status).toBe(200); + servedAgain = + read.response.headers.get('x-instance-id') === + selectedInstance; + } + expect(servedAgain).toBe(true); + }, + outagedRequestBudgetMs, + ); + + it( + 'degrades attachments while S3 is down and sweeps the failed upload afterwards', + async () => { + const upload = (data: Buffer, name: string) => + request( + uploadPath(chat.id, name), + { + method: 'POST', + headers: { + ...bearer(token), + 'content-type': 'image/png', + }, + body: data as unknown as BodyInit, + }, + ); + const before = await upload(samplePng, `s3before-${runId}.png`); + expect(before.response.status).toBe(202); + await waitReady(token, before.body.id); + + await compose('stop', s3Service); + try { + const failed = await upload(samplePng, `s3down-${runId}.png`); + expectBoundedOutage(failed); + const metadata = await request( + `/api/v1/attachments/${before.body.id}`, + { headers: bearer(token) }, + ); + expect(metadata.response.status).toBe(200); + const content = await request( + `/api/v1/attachments/${before.body.id}/content`, + { headers: bearer(token) }, + ); + expectBoundedOutage(content); + const text = await request( + `/api/v1/chats/${chat.id}/messages`, + json({ text: 'S3 down' }, messageHeaders(token)), + ); + expect(text.response.status).toBe(201); + expect((await request('/health/ready')).response.status).toBe( + 200, + ); + // Compensation claimed the row but could not delete the object: + // the row stays in `deleting`, tracked for the sweeper. + expect( + await psql( + `select status from attachments where chat_id = '${chat.id}' and filename = 's3down-${runId}.png'`, + ), + ).toBe('deleting'); + } finally { + await compose('start', s3Service); + } + await waitForS3Healthy(); + + const bytes = await fetch( + `${baseUrl}/api/v1/attachments/${before.body.id}/content`, + { headers: bearer(token) }, + ); + expect(bytes.status).toBe(200); + expect( + Buffer.from(await bytes.arrayBuffer()).equals(samplePng), + ).toBe(true); + const after = await upload(samplePng, `s3after-${runId}.png`); + expect(after.response.status).toBe(202); + await waitReady(token, after.body.id); + + // A `deleting` row is re-claimed one minute after its last change + // (an in-flight compensation is never raced), then swept. + const filter = `chat_id = '${chat.id}' and filename = 's3down-${runId}.png'`; + await waitFor( + async () => { + await sweepNow(); + return ( + (await psql( + `select count(*) from attachments where ${filter}`, + )) === '0' + ); + }, + async () => 'deleting row was not swept', + 90_000, + ); + }, + outagedRequestBudgetMs, + ); + + it( + 'bounds requests by S3_REQUEST_TIMEOUT_MS when S3 hangs instead of refusing', + async () => { + const created = await request<{ id: string }>( + uploadPath(chat.id, `hang-${runId}.png`), + { + method: 'POST', + headers: { ...bearer(token), 'content-type': 'image/png' }, + body: samplePng as unknown as BodyInit, + }, + ); + expect(created.response.status).toBe(202); + await waitReady(token, created.body.id); + // A paused container keeps its TCP port open but never answers: the + // failure mode a stopped container (connection refused) does not show. + const container = (await compose('ps', '-q', s3Service)).split( + '\n', + )[0]; + await docker('pause', container); + try { + const started = Date.now(); + const content = await request( + `/api/v1/attachments/${created.body.id}/content`, + { headers: bearer(token) }, + ); + const elapsed = Date.now() - started; + expectBoundedOutage(content); + // S3_REQUEST_TIMEOUT_MS is 15 s in the stand; HAProxy gives up at 30 s. + expect(elapsed).toBeLessThan(20_000); + expect( + ( + await request( + `/api/v1/attachments/${created.body.id}`, + { + headers: bearer(token), + }, + ) + ).response.status, + ).toBe(200); + expect((await request('/health/ready')).response.status).toBe( + 200, + ); + } finally { + await docker('unpause', container); + } + const bytes = await fetch( + `${baseUrl}/api/v1/attachments/${created.body.id}/content`, + { headers: bearer(token) }, + ); + expect(bytes.status).toBe(200); + expect( + Buffer.from(await bytes.arrayBuffer()).equals(samplePng), + ).toBe(true); + }, + outagedRequestBudgetMs, + ); + + it( + 'keeps the object while PostgreSQL is down before ready and sweeps it afterwards', + async () => { + const data = noisePng(1024 * 1024); + const filename = `pgfault-${runId}.png`; + const filter = `chat_id = '${chat.id}' and status = 'pending' and filename = '${filename}'`; + let restarted = false; + let reply = ''; + try { + reply = await rawUpload( + data, + chat.id, + filename, + token, + 64 * 1024, + async () => { + await waitFor( + async () => + Number( + await psql( + `select count(*) from attachments where ${filter}`, + ), + ) === 1, + async () => 'pending row did not appear', + 10_000, + ); + await compose('stop', postgresService); + }, + ); + } finally { + await compose('start', postgresService); + restarted = true; + } + expect(restarted).toBe(true); + expect(reply.startsWith('HTTP/1.1 503')).toBe(true); + await waitForReadiness(200); + + const key = await psql( + `select staging_key from attachments where ${filter}`, + ); + expect(key).toMatch(/^staging\/[0-9a-f-]{36}\/original$/u); + // Without PostgreSQL the API cannot tell a lost commit from a failed + // one, so it deletes nothing; the pending row keeps the object tracked. + expect(await objectExists(key)).toBe(true); + + await waitUntilExpired(filter); + await sweepNow(); + expect( + await psql(`select count(*) from attachments where ${filter}`), + ).toBe('0'); + expect(await objectExists(key)).toBe(false); + }, + outagedRequestBudgetMs, + ); +}); diff --git a/app/test/direct-upload.e2e-spec.ts b/app/test/direct-upload.e2e-spec.ts new file mode 100644 index 00000000..04149a48 --- /dev/null +++ b/app/test/direct-upload.e2e-spec.ts @@ -0,0 +1,348 @@ +import { createHash } from 'node:crypto'; +import { readFileSync } from 'node:fs'; +import { setTimeout as sleep } from 'node:timers/promises'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import type { Pool } from 'pg'; +import type { Attachment } from '../src/attachments/attachment.js'; +import type { DirectUpload } from '../src/attachments/direct-uploads.service.js'; +import type { ApiErrorBody } from '../src/common/errors/api-error.js'; +import { + Actor, + actor, + bearer, + createChat, + database, + download, + expectError, + http, + json, + postMessage, + waitReady, +} from './support/e2e.js'; +import { noisePng, pixelBombPng } from './support/png.js'; + +const png = readFileSync( + new URL('../../tests/fixtures/sample.png', import.meta.url), +); +const jpeg = readFileSync(new URL('./fixtures/sample.jpg', import.meta.url)); +const MAX = 10_485_760; +const shortTtl = process.env.E2E_DIRECT_TTL_SHORT === '1'; + +let db: Pool; +let alice: Actor; +let bob: Actor; +let eve: Actor; +let chatId: string; + +const sha256 = (data: Uint8Array) => + createHash('sha256').update(data).digest('hex'); + +function init(token: string, chat: string, body: Record) { + return http( + `/api/v1/chats/${chat}/attachments/direct-uploads`, + json(body, bearer(token)), + ); +} + +function declaration(data: Buffer, contentType = 'image/png') { + return { + filename: 'direct.png', + content_type: contentType, + size_bytes: data.length, + sha256: sha256(data), + }; +} + +async function initOk(data: Buffer, contentType = 'image/png') { + const result = await init( + alice.token, + chatId, + declaration(data, contentType), + ); + expect(result.response.status).toBe(201); + return result.body; +} + +// The PUT goes straight to the public S3 endpoint, not through the API. +async function put(ticket: DirectUpload, data: Buffer): Promise { + return fetch(ticket.url, { + method: ticket.method, + headers: ticket.headers, + body: data as unknown as BodyInit, + }); +} + +function complete(token: string, id: string) { + return http( + `/api/v1/attachments/${id}/complete`, + { + method: 'POST', + headers: bearer(token), + }, + ); +} + +describe('direct upload (presigned PUT)', () => { + beforeAll(async () => { + db = database(); + alice = await actor(); + bob = await actor(); + eve = await actor(); + chatId = (await createChat(alice, [bob])).id; + }); + + afterAll(async () => { + await db.end(); + }); + + it('uploads around the API and exposes the attachment after complete', async () => { + const ticket = await initOk(png); + expect(ticket.method).toBe('PUT'); + expect(ticket.headers['x-amz-checksum-sha256']).toBe( + Buffer.from(sha256(png), 'hex').toString('base64'), + ); + expect(Date.parse(ticket.expires_at)).toBeGreaterThan(Date.now()); + expect(new URL(ticket.url).pathname).toBe( + `/messenger-attachments/staging/${ticket.attachment_id}/original`, + ); + + const hidden = await http( + `/api/v1/attachments/${ticket.attachment_id}`, + { headers: bearer(bob.token) }, + ); + expectError(hidden, 404, 'not_found'); + expectError( + await postMessage(alice.token, chatId, { + text: '', + attachment_ids: [ticket.attachment_id], + }), + 404, + 'not_found', + ); + expectError( + await complete(alice.token, ticket.attachment_id), + 409, + 'conflict', + ); + + expect((await put(ticket, png)).status).toBe(200); + expectError( + await complete(bob.token, ticket.attachment_id), + 404, + 'not_found', + ); + expectError( + await complete(eve.token, ticket.attachment_id), + 404, + 'not_found', + ); + const done = await complete(alice.token, ticket.attachment_id); + expect(done.response.status).toBe(200); + expect(done.body).toMatchObject({ + id: ticket.attachment_id, + owner_id: alice.user.id, + content_type: 'image/png', + size_bytes: png.length, + sha256: sha256(png), + status: 'queued', + }); + const ready = await waitReady(alice.token, ticket.attachment_id); + expect(ready.status).toBe('ready'); + expect(ready.variants).toHaveLength(3); + const again = await complete(alice.token, ticket.attachment_id); + expect(again.response.status).toBe(200); + expect(again.body.status).toBe('ready'); + + const content = await download(bob.token, ticket.attachment_id); + expect(content.response.status).toBe(200); + expect(content.bytes.equals(png)).toBe(true); + const sent = await postMessage(alice.token, chatId, { + text: '', + attachment_ids: [ticket.attachment_id], + }); + expect(sent.response.status).toBe(201); + expect(sent.body.attachment_ids).toEqual([ticket.attachment_id]); + }); + + it('answers 200 to every parallel complete of one upload', async () => { + const ticket = await initOk(png); + expect((await put(ticket, png)).status).toBe(200); + const results = await Promise.all( + Array.from({ length: 4 }, () => + complete(alice.token, ticket.attachment_id), + ), + ); + expect(results.map((result) => result.response.status)).toEqual([ + 200, 200, 200, 200, + ]); + for (const result of results) + expect(result.body.id).toBe(ticket.attachment_id); + }); + + it('rejects invalid declarations', async () => { + const base = declaration(png); + expectError(await init(eve.token, chatId, base), 404, 'not_found'); + expectError( + await init(alice.token, chatId, { ...base, size_bytes: MAX + 1 }), + 413, + 'payload_too_large', + ); + expectError( + await init(alice.token, chatId, { + ...base, + content_type: 'image/gif', + }), + 415, + 'unsupported_media_type', + ); + for (const bad of [ + { ...base, extra: true }, + { ...base, sha256: 'ABC' }, + { ...base, size_bytes: 0 }, + { ...base, filename: 'a/b.png' }, + ]) + expectError( + await init(alice.token, chatId, bad), + 400, + 'invalid_request', + ); + }); + + it('lets the storage refuse bytes that differ from the declaration', async () => { + const ticket = await initOk(png); + const tampered = Buffer.from(png); + tampered[1000] ^= 1; + const refused = await put(ticket, tampered); + expect(refused.status).toBe(400); + expect(await refused.text()).toContain('XAmzContentChecksumMismatch'); + // No object was created, so the upload can still be finished. + expectError( + await complete(alice.token, ticket.attachment_id), + 409, + 'conflict', + ); + expect((await put(ticket, png)).status).toBe(200); + expect( + (await complete(alice.token, ticket.attachment_id)).response.status, + ).toBe(200); + + const longer = await initOk(png); + const wrongLength = await put( + longer, + Buffer.concat([png, Buffer.from('x')]), + ); + expect(wrongLength.status).toBe(403); + }); + + it('rejects a declared PNG that is really a JPEG, then forgets the claim', async () => { + const ticket = await initOk(jpeg, 'image/png'); + expect((await put(ticket, jpeg)).status).toBe(200); + expectError( + await complete(alice.token, ticket.attachment_id), + 415, + 'unsupported_media_type', + ); + expectError( + await complete(alice.token, ticket.attachment_id), + 404, + 'not_found', + ); + // The URL is still valid: a re-PUT must stay tracked so the sweeper + // removes the new object too. + expect((await put(ticket, jpeg)).status).toBe(200); + const row = await db.query<{ status: string }>( + 'SELECT status FROM attachments WHERE id = $1', + [ticket.attachment_id], + ); + expect(row.rows[0]?.status).toBe('rejected'); + expectError( + await complete(alice.token, ticket.attachment_id), + 404, + 'not_found', + ); + }); + + it.each([ + ['truncated PNG', () => png.subarray(0, png.length - 100)], + [ + 'PNG damaged after the first 64 KiB', + () => { + const data = noisePng(1_048_576); + data[700_000] ^= 0xff; + return data; + }, + ], + ])('rejects a %s even with a matching checksum', async (_name, make) => { + // The declaration and the signed checksum match the bytes exactly: + // only a check of the whole image can tell they are not an image. + const data = make(); + const ticket = await initOk(data); + expect((await put(ticket, data)).status).toBe(200); + expectError( + await complete(alice.token, ticket.attachment_id), + 415, + 'unsupported_media_type', + ); + expectError( + await complete(alice.token, ticket.attachment_id), + 404, + 'not_found', + ); + }); + + it('rejects a pixel bomb on complete', async () => { + const bomb = pixelBombPng(); + const ticket = await initOk(bomb); + expect((await put(ticket, bomb)).status).toBe(200); + expectError( + await complete(alice.token, ticket.attachment_id), + 413, + 'payload_too_large', + ); + expectError( + await complete(alice.token, ticket.attachment_id), + 404, + 'not_found', + ); + }); + + it('limits unfinished direct uploads per user', async () => { + const spammer = await actor(); + const chat = (await createChat(spammer, [])).id; + for (let index = 0; index < 10; index += 1) { + const ok = await init(spammer.token, chat, declaration(png)); + expect(ok.response.status).toBe(201); + } + const limited = await init(spammer.token, chat, declaration(png)); + expectError(limited, 429, 'rate_limited'); + expect( + Number(limited.response.headers.get('retry-after')), + ).toBeGreaterThan(0); + }); + + it.runIf(shortTtl)( + 'refuses an expired URL and removes the claim', + async () => { + const ticket = await initOk(png); + const expiresAt = Date.parse(ticket.expires_at); + await sleep(Math.max(0, expiresAt - Date.now()) + 2000); + expect((await put(ticket, png)).status).toBe(403); + const deadline = Date.now() + 15_000; + for (;;) { + const rows = await db.query( + 'SELECT 1 FROM attachments WHERE id = $1', + [ticket.attachment_id], + ); + if (rows.rowCount === 0) break; + expect(Date.now()).toBeLessThan(deadline); + await sleep(250); + } + expectError( + await complete(alice.token, ticket.attachment_id), + 404, + 'not_found', + ); + }, + 60_000, + ); +}); diff --git a/app/test/fixtures/sample-progressive.jpg b/app/test/fixtures/sample-progressive.jpg new file mode 100644 index 00000000..eed80152 Binary files /dev/null and b/app/test/fixtures/sample-progressive.jpg differ diff --git a/app/test/fixtures/sample.jpg b/app/test/fixtures/sample.jpg new file mode 100644 index 00000000..708a5aee Binary files /dev/null and b/app/test/fixtures/sample.jpg differ diff --git a/app/test/support/e2e.ts b/app/test/support/e2e.ts new file mode 100644 index 00000000..7f0338a3 --- /dev/null +++ b/app/test/support/e2e.ts @@ -0,0 +1,187 @@ +import { randomUUID } from 'node:crypto'; +import { Pool } from 'pg'; +import { expect } from 'vitest'; +import type { Chat } from '../../src/chats/chat.js'; +import type { ApiErrorBody } from '../../src/common/errors/api-error.js'; +import type { Message } from '../../src/messages/message.js'; +import type { Attachment } from '../../src/attachments/attachment.js'; +import type { User } from '../../src/users/user.js'; + +export const baseUrl = ( + process.env.API_BASE_URL ?? 'http://localhost:8080' +).replace(/\/+$/u, ''); +export const password = 'integration:пароль🔐'; +const runId = randomUUID().replaceAll('-', '').slice(0, 10); +let sequence = 0; + +export type Actor = { user: User; token: string }; +export type HttpResult = { body: T; response: Response }; + +export function database(): Pool { + return new Pool({ + connectionString: process.env.DATABASE_URL, + connectionTimeoutMillis: 5000, + }); +} + +export async function http( + path: string, + init: RequestInit = {}, +): Promise> { + const response = await fetch(`${baseUrl}${path}`, init); + const text = await response.text(); + let body: unknown = undefined; + if (text) { + try { + body = JSON.parse(text); + } catch { + body = text; + } + } + return { body: body as T, response }; +} + +export function json( + body: unknown, + headers: Record = {}, +): RequestInit { + return { + method: 'POST', + headers: { 'content-type': 'application/json', ...headers }, + body: JSON.stringify(body), + }; +} + +export function bearer(token: string): Record { + return { authorization: `Bearer ${token}` }; +} + +export function expectError( + result: HttpResult, + status: number, + code: string, +): void { + expect(result.response.status).toBe(status); + expect(result.body.error.code).toBe(code); + expect(result.body.error.request_id).toBe( + result.response.headers.get('x-request-id'), + ); +} + +export async function actor(): Promise { + const username = `e2e_${runId}_${sequence++}`.slice(0, 32); + const created = await http( + '/api/v1/users', + json({ username, display_name: 'Attachments e2e', password }), + ); + expect(created.response.status).toBe(201); + const basic = Buffer.from(`${username}:${password}`, 'utf8').toString( + 'base64', + ); + const session = await http<{ token: string }>('/api/v1/auth/sessions', { + method: 'POST', + headers: { authorization: `Basic ${basic}` }, + }); + expect(session.response.status).toBe(201); + return { user: created.body, token: session.body.token }; +} + +export async function createChat( + owner: Actor, + members: Actor[], +): Promise { + const result = await http( + '/api/v1/chats', + json( + { + title: 'attachments', + member_ids: [owner.user.id, ...members.map((m) => m.user.id)], + }, + bearer(owner.token), + ), + ); + expect(result.response.status).toBe(201); + return result.body; +} + +/** `query` is sent verbatim so tests can craft malformed filename parameters. */ +export function uploadRaw( + token: string, + chatId: string, + query: string, + body: Buffer | undefined, + contentType = 'image/png', +): Promise> { + return http(`/api/v1/chats/${chatId}/attachments${query}`, { + method: 'POST', + headers: { ...bearer(token), 'content-type': contentType }, + body: body as BodyInit | undefined, + }); +} + +export function upload( + token: string, + chatId: string, + body: Buffer | undefined, + options: { filename?: string; contentType?: string } = {}, +) { + return uploadRaw( + token, + chatId, + `?filename=${encodeURIComponent(options.filename ?? 'picture.png')}`, + body, + options.contentType ?? 'image/png', + ); +} + +/** + * Lab5 uploads answer 202 before the worker runs. Poll metadata until ready; + * a failed attachment is a test failure, never a silent timeout. + */ +export async function waitReady( + token: string, + attachmentId: string, + timeoutMs = 60_000, +): Promise { + const deadline = Date.now() + timeoutMs; + for (;;) { + const result = await http( + `/api/v1/attachments/${attachmentId}`, + { headers: bearer(token) }, + ); + expect(result.response.status).toBe(200); + if (result.body.status === 'failed') + throw new Error( + `attachment ${attachmentId} failed: ${result.body.error_code ?? 'unknown'}`, + ); + if (result.body.status === 'ready') return result.body; + if (Date.now() >= deadline) + throw new Error( + `attachment ${attachmentId} did not become ready within ${timeoutMs}ms`, + ); + await new Promise((resolve) => setTimeout(resolve, 200)); + } +} + +export async function download( + token: string, + attachmentId: string, +): Promise<{ response: Response; bytes: Buffer }> { + const response = await fetch( + `${baseUrl}/api/v1/attachments/${attachmentId}/content`, + { headers: bearer(token) }, + ); + return { response, bytes: Buffer.from(await response.arrayBuffer()) }; +} + +export function postMessage( + token: string, + chatId: string, + body: unknown, + key = randomUUID().replaceAll('-', ''), +) { + return http( + `/api/v1/chats/${chatId}/messages`, + json(body, { ...bearer(token), 'idempotency-key': key }), + ); +} diff --git a/app/test/support/jpeg.ts b/app/test/support/jpeg.ts new file mode 100644 index 00000000..b2d1ef97 --- /dev/null +++ b/app/test/support/jpeg.ts @@ -0,0 +1,24 @@ +/** Keeps every segment through the SOS header and drops the scan data. */ +export function jpegWithoutScanData(jpeg: Buffer): Buffer { + let offset = 2; + for (;;) { + const marker = jpeg[offset + 1]; + offset += 2 + jpeg.readUInt16BE(offset + 2); + if (marker === 0xda) break; + } + return Buffer.concat([jpeg.subarray(0, offset), Buffer.from([0xff, 0xd9])]); +} + +/** Inserts `count` APP13 segments of ~64 KiB right after SOI (Photoshop-style metadata). */ +export function jpegWithMetadata(jpeg: Buffer, count: number): Buffer { + const payload = Buffer.alloc(65_533, 0x20); + const segment = Buffer.concat([ + Buffer.from([0xff, 0xed, 0xff, 0xff]), + payload, + ]); + return Buffer.concat([ + jpeg.subarray(0, 2), + ...Array.from({ length: count }, () => segment), + jpeg.subarray(2), + ]); +} diff --git a/app/test/support/png.ts b/app/test/support/png.ts new file mode 100644 index 00000000..0605b82f --- /dev/null +++ b/app/test/support/png.ts @@ -0,0 +1,140 @@ +import { randomBytes } from 'node:crypto'; +import { once } from 'node:events'; +import { crc32, createDeflate, deflateSync } from 'node:zlib'; + +const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); + +function chunk(type: string, data: Buffer): Buffer { + const head = Buffer.alloc(8); + head.writeUInt32BE(data.length, 0); + head.write(type, 4, 'latin1'); + const crc = Buffer.alloc(4); + crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), data])) >>> 0, 0); + return Buffer.concat([head, data, crc]); +} + +export function buildPng(options: { + width: number; + height: number; + colorType: number; + bitDepth: number; + idat: Buffer; +}): Buffer { + const ihdr = Buffer.alloc(13); + ihdr.writeUInt32BE(options.width, 0); + ihdr.writeUInt32BE(options.height, 4); + ihdr[8] = options.bitDepth; + ihdr[9] = options.colorType; + return Buffer.concat([ + SIGNATURE, + chunk('IHDR', ihdr), + chunk('IDAT', options.idat), + chunk('IEND', Buffer.alloc(0)), + ]); +} + +/** Inserts a private ancillary chunk before IEND to reach exactly `size` bytes. */ +export function padPngTo(png: Buffer, size: number): Buffer { + const extra = size - png.length; + if (extra === 0) return png; + if (extra < 12) throw new Error('PNG padding needs at least 12 bytes'); + const iend = png.length - 12; + return Buffer.concat([ + png.subarray(0, iend), + chunk('prVt', Buffer.alloc(extra - 12)), + png.subarray(iend), + ]); +} + +/** 8000x5001 grayscale 1-bit, all-zero rows: tiny file, 40 008 000 pixels. */ +export function pixelBombPng(): Buffer { + const width = 8000; + const height = 5001; + const row = Buffer.alloc(1 + Math.ceil(width / 8)); + const raw = Buffer.alloc(row.length * height); + return buildPng({ + width, + height, + colorType: 0, + bitDepth: 1, + idat: deflateSync(raw, { level: 9 }), + }); +} + +const NOISE_WIDTH = 1024; +const NOISE_ROW = 1 + NOISE_WIDTH * 3; + +function noiseOfHeight(height: number): Buffer { + const raw = randomBytes(NOISE_ROW * height); + for (let row = 0; row < height; row += 1) raw[row * NOISE_ROW] = 0; + return buildPng({ + width: NOISE_WIDTH, + height, + colorType: 2, + bitDepth: 8, + idat: deflateSync(raw, { level: 0 }), + }); +} + +/** Incompressible, structurally valid PNG of exactly `size` bytes. */ +export function noisePng(size: number): Buffer { + let height = Math.max(1, Math.floor(size / NOISE_ROW)); + for (;;) { + const png = noiseOfHeight(height); + if (png.length > size) { + height -= 1; + if (height < 1) throw new Error('size is too small for noisePng'); + continue; + } + const extra = size - png.length; + if (extra > 0 && extra < 12) { + height -= 1; + continue; + } + return padPngTo(png, size); + } +} + +/** Valid signature, IHDR, CRCs and IEND around a zlib stream of random bytes. */ +export function pngWithGarbageIdat(): Buffer { + return buildPng({ + width: 320, + height: 200, + colorType: 2, + bitDepth: 8, + idat: randomBytes(5000), + }); +} + +/** + * 8000x5000 RGBA 16-bit (8 bytes/pixel, 40 000 000 px — the pixel limit): + * the heaviest decode a valid upload can ask for. Rows are streamed into the + * compressor, so the 320 MB raw image is never allocated here. + */ +export async function maxPixelsRgba16Png(): Promise { + const width = 8000; + const height = 5000; + // First row: a gradient; every next row uses filter "Up" with zero + // deltas, i.e. repeats it — a valid image that compresses well. + const first = Buffer.alloc(1 + width * 8); + for (let x = 0; x < width; x += 1) + first.writeUInt16BE((x * 8) & 0xffff, 1 + x * 8); + const repeat = Buffer.alloc(1 + width * 8); + repeat[0] = 2; + const deflate = createDeflate({ level: 1 }); + const chunks: Buffer[] = []; + deflate.on('data', (chunk: Buffer) => chunks.push(chunk)); + const ended = once(deflate, 'end'); + for (let y = 0; y < height; y += 1) + if (!deflate.write(y === 0 ? first : repeat)) + await once(deflate, 'drain'); + deflate.end(); + await ended; + return buildPng({ + width, + height, + colorType: 6, + bitDepth: 16, + idat: Buffer.concat(chunks), + }); +} diff --git a/app/tsconfig.build.json b/app/tsconfig.build.json new file mode 100644 index 00000000..2eafee0e --- /dev/null +++ b/app/tsconfig.build.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "./src" + }, + "include": ["src"], + "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] +} diff --git a/app/tsconfig.json b/app/tsconfig.json new file mode 100644 index 00000000..6aa3ca79 --- /dev/null +++ b/app/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "nodenext", + "moduleResolution": "nodenext", + "resolvePackageJsonExports": true, + "esModuleInterop": true, + "isolatedModules": true, + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2024", + "sourceMap": true, + "outDir": "./dist", + "incremental": true, + "skipLibCheck": true, + "strict": true, + "strictPropertyInitialization": false, + "types": ["vitest/globals", "node"] + } +} diff --git a/app/vitest.config.e2e.ts b/app/vitest.config.e2e.ts new file mode 100644 index 00000000..a7d63173 --- /dev/null +++ b/app/vitest.config.e2e.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'vitest/config'; +import tsconfigPaths from 'vite-tsconfig-paths'; + +export default defineConfig({ + plugins: [tsconfigPaths()], + test: { + globals: true, + root: './', + include: ['test/*.e2e-spec.ts'], + testTimeout: 30_000, + hookTimeout: 30_000, + sequence: { concurrent: false }, + }, +}); diff --git a/app/vitest.config.ts b/app/vitest.config.ts new file mode 100644 index 00000000..9384f47b --- /dev/null +++ b/app/vitest.config.ts @@ -0,0 +1,13 @@ +import { defineConfig } from 'vitest/config'; +import tsconfigPaths from 'vite-tsconfig-paths'; + +export default defineConfig({ + // Resolves the path aliases declared in tsconfig.json, including the ones + // added by `nest g library`. + plugins: [tsconfigPaths()], + test: { + globals: true, + root: './', + include: ['**/*.spec.ts'], + }, +}); diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..ee22ece2 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,361 @@ +# Стенд lab5 (база lab4): клиент -> HAProxy -> API x N -> PostgreSQL + Redis + MinIO +# + RabbitMQ; отдельный worker читает задания из quorum-очереди и публикует +# оригинал и варианты в конечный S3-prefix. API отвечает 202 после COMMIT +# (staging-объект + attachment + job + outbox), поэтому брокер не входит в +# /health/ready и его недоступность не останавливает приём загрузок. +# Одна команда запуска: make stand-up (docker compose --env-file .env up -d --build --wait) +# +# Проект запинен именем, чтобы имена контейнеров и labels были предсказуемы для +# scripts/stop-one.sh; container_name нигде не задан, иначе масштабирование +# одного Compose-service стало бы невозможным. +name: mtusi-lab5 + +services: + postgres: + image: postgres:16.4-alpine + # pg_stat_statements включён для трека 1 (агрегаты SQL по репликам); + # max_connections поднят под N x DATABASE_POOL_MAX. + command: + [ + 'postgres', + '-c', + 'shared_preload_libraries=pg_stat_statements', + '-c', + 'max_connections=120', + '-c', + 'shared_buffers=128MB', + '-c', + 'effective_cache_size=384MB', + ] + environment: + POSTGRES_DB: ${POSTGRES_DB:-messenger} + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env} + # Только loopback: операторские проверки (host-side npm run test:e2e, + # psql в evidence). API-реплики портов не публикуют вовсе. + ports: + - '127.0.0.1:${POSTGRES_PUBLISHED_PORT:-5432}:5432' + volumes: + - postgres-data:/var/lib/postgresql/data + deploy: + resources: + limits: + cpus: '${PG_CPUS:-1.5}' + memory: ${PG_MEMORY:-512m} + healthcheck: + test: + [ + 'CMD-SHELL', + 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}', + ] + interval: 5s + timeout: 3s + retries: 20 + + redis: + image: redis:7.4.2-alpine + # appendonly: сессии и кэш переживают рестарт контейнера (проверка + # сохранности с неизменным токеном). volatile-lru вытесняет только + # ключи с TTL; в стенде у них ограниченный набор и объём. + command: + [ + 'redis-server', + '--appendonly', + 'yes', + '--appendfsync', + 'everysec', + '--maxmemory', + '96mb', + '--maxmemory-policy', + 'volatile-lru', + ] + ports: + - '127.0.0.1:${REDIS_PUBLISHED_PORT:-6379}:6379' + volumes: + - redis-data:/data + deploy: + resources: + limits: + cpus: '${REDIS_CPUS:-0.25}' + memory: ${REDIS_MEMORY:-128m} + healthcheck: + test: ['CMD', 'redis-cli', 'ping'] + interval: 5s + timeout: 3s + retries: 20 + + broker: + image: rabbitmq:4.1.8-alpine@sha256:153322e3974e6365c50e523e5eadd5a7fae231f0e8c58f67c03bb3e02056e34e + # Имя узла rabbit@broker должно оставаться стабильным: при смене + # hostname данные в broker-data превращаются в сирот. + hostname: broker + secrets: + - source: rabbitmq_conf + target: /etc/rabbitmq/conf.d/20-credentials.conf + volumes: + - broker-data:/var/lib/rabbitmq + # Портов нет: брокер доступен только сервисам compose-сети. + deploy: + resources: + limits: + cpus: '${BROKER_CPUS:-0.5}' + memory: ${BROKER_MEMORY:-512m} + healthcheck: + test: ['CMD', 'rabbitmq-diagnostics', '-q', 'check_port_connectivity'] + interval: 5s + timeout: 5s + retries: 30 + start_period: 20s + stop_grace_period: 30s + restart: unless-stopped + + # One-shot: creates the worker identity and tightens the API identity. + # rabbitmqctl talks to rabbit@broker using the shared Erlang cookie, so the + # data volume is mounted read-only. + broker-init: + image: rabbitmq:4.1.8-alpine@sha256:153322e3974e6365c50e523e5eadd5a7fae231f0e8c58f67c03bb3e02056e34e + entrypoint: ['/bin/sh', '/bootstrap/init-worker.sh'] + volumes: + - broker-data:/var/lib/rabbitmq:ro + - ./infra/rabbitmq:/bootstrap:ro + secrets: [broker_worker_password] + depends_on: + broker: { condition: service_healthy } + restart: 'no' + deploy: + resources: + limits: + cpus: '0.25' + memory: 128m + + s3: + image: minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + command: ['server', '/data', '--address', ':9000'] + environment: + MINIO_ROOT_USER_FILE: minio_root_user + MINIO_ROOT_PASSWORD_FILE: minio_root_password + MINIO_BROWSER: 'off' + secrets: [minio_root_user, minio_root_password] + # Консольный порт 9001 не публикуется, консоль отключена (MINIO_BROWSER=off). + ports: + - '${S3_PUBLIC_BIND:-127.0.0.1}:${S3_PUBLIC_PORT:-9000}:9000' + volumes: + - s3-data:/data + deploy: + resources: + limits: + cpus: '${S3_CPUS:-1.0}' + memory: ${S3_MEMORY:-512m} + healthcheck: + test: ['CMD', 'mc', 'ready', 'local'] + interval: 5s + timeout: 3s + retries: 20 + + # Идемпотентный bootstrap: bucket, политика, пользователь API. + s3-init: + image: minio/mc:RELEASE.2025-08-13T08-35-41Z@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727 + entrypoint: ['/bin/sh', '/bootstrap/bootstrap.sh'] + environment: + S3_BUCKET: '${S3_BUCKET:-messenger-attachments}' + volumes: + - ./infra/minio:/bootstrap:ro + secrets: + - minio_root_user + - minio_root_password + - s3_api_access_key_id + - s3_api_secret_access_key + - s3_worker_access_key_id + - s3_worker_secret_access_key + depends_on: + s3: { condition: service_healthy } + restart: 'no' + deploy: + resources: + limits: + cpus: '0.25' + memory: 128m + + # Миграции выполняются один раз отдельным сервисом, а не гонкой реплик: + # api стартует только после его успешного завершения. + migrate: + build: + context: . + dockerfile: Dockerfile + image: ${API_IMAGE:-mtusi-messenger:lab5} + command: ['node', 'dist/database/migration.runner.js'] + env_file: [.env] + deploy: + resources: + limits: + cpus: '0.5' + memory: 192m + depends_on: + postgres: { condition: service_healthy } + redis: { condition: service_healthy } + restart: 'no' + + api: + build: + context: . + dockerfile: Dockerfile + args: + BUILD_ID: ${BUILD_ID:-unknown} + image: ${API_IMAGE:-mtusi-messenger:lab5} + # Портов нет намеренно: единственная точка входа — HAProxy. + env_file: [.env] + environment: + DATABASE_POOL_MAX: ${DATABASE_POOL_MAX:-10} + CACHE_MODE: ${CACHE_MODE:-protected} + CACHE_MISS_DELAY_MS: ${CACHE_MISS_DELAY_MS:-0} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + # Бюджет пробы готовности; LB проверяет ту же ручку с timeout check 500 ms. + HEALTH_TIMEOUT_MS: ${HEALTH_TIMEOUT_MS:-400} + # Дрейн длиннее обнаружения отказа LB (500 ms x fall 2 ≈ 1 s). + DRAIN_DELAY_MS: ${DRAIN_DELAY_MS:-3000} + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret_access_key + UPLOAD_TIMEOUT_MS: ${UPLOAD_TIMEOUT_MS:-25000} + DIRECT_UPLOAD_URL_TTL_SECONDS: ${DIRECT_UPLOAD_URL_TTL_SECONDS:-300} + ATTACHMENT_SWEEP_GRACE_SECONDS: ${ATTACHMENT_SWEEP_GRACE_SECONDS:-900} + ATTACHMENT_SWEEP_INTERVAL_MS: ${ATTACHMENT_SWEEP_INTERVAL_MS:-60000} + # libvips держит декодируемое изображение в памяти, а не во временном + # файле: /tmp — tmpfs 16 MiB, и изображение ~40 Мп туда не помещается. + VIPS_DISC_THRESHOLD: ${VIPS_DISC_THRESHOLD:-1g} + FAULT_INJECTION_ENABLED: ${FAULT_INJECTION_ENABLED:-0} + JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} + JOB_RETRY_BASE_MS: ${JOB_RETRY_BASE_MS:-1000} + JOB_RETRY_MAX_MS: ${JOB_RETRY_MAX_MS:-30000} + BROKER_PUBLISH_TIMEOUT_MS: ${BROKER_PUBLISH_TIMEOUT_MS:-5000} + # Outbox закрывает недоступность брокера: API принимает загрузку и + # отвечает 202 без публикации, relay доставит задание после восстановления. + secrets: + - s3_api_access_key_id + - s3_api_secret_access_key + - broker_url + # Корневая ФС только для чтения: локального файла оригинала на API нет. + read_only: true + tmpfs: ['/tmp:size=16m'] + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: '${API_CPUS_PER_REPLICA:-1.5}' + memory: ${API_MEMORY_PER_REPLICA:-1024m} + stop_grace_period: 20s + restart: unless-stopped + depends_on: + migrate: { condition: service_completed_successfully } + postgres: { condition: service_healthy } + redis: { condition: service_healthy } + s3-init: { condition: service_completed_successfully } + healthcheck: + test: + [ + 'CMD', + 'node', + '-e', + "fetch('http://127.0.0.1:'+ (process.env.APP_PORT||8080) + '/health/ready').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))", + ] + interval: 5s + timeout: 3s + retries: 20 + + # Worker — отдельный процесс без HTTP API. Он читает задания из quorum- + # очереди, держит lease с heartbeat, пишет варианты и коммитит результат в + # PostgreSQL. Остановка worker не блокирует приём загрузок API. + worker: + build: + context: . + dockerfile: Dockerfile + args: + BUILD_ID: ${BUILD_ID:-unknown} + image: ${API_IMAGE:-mtusi-messenger:lab5} + command: ['node', 'dist/worker.main.js'] + env_file: [.env] + environment: + BROKER_URL_FILE: /run/secrets/broker_worker_url + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_worker_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_worker_secret_access_key + VIPS_DISC_THRESHOLD: ${VIPS_DISC_THRESHOLD:-1g} + FAULT_INJECTION_ENABLED: ${FAULT_INJECTION_ENABLED:-0} + WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2} + JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} + JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} + JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:-30} + JOB_RETRY_BASE_MS: ${JOB_RETRY_BASE_MS:-1000} + JOB_RETRY_MAX_MS: ${JOB_RETRY_MAX_MS:-30000} + STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} + WORKER_HEALTH_PORT: ${WORKER_HEALTH_PORT:-8081} + BROKER_PUBLISH_TIMEOUT_MS: ${BROKER_PUBLISH_TIMEOUT_MS:-5000} + QUEUED_RECONCILE_GRACE_SECONDS: ${QUEUED_RECONCILE_GRACE_SECONDS:-15} + OUTBOX_RETENTION_SECONDS: ${OUTBOX_RETENTION_SECONDS:-86400} + JOB_EVENTS_RETENTION_SECONDS: ${JOB_EVENTS_RETENTION_SECONDS:-2592000} + secrets: + - s3_worker_access_key_id + - s3_worker_secret_access_key + - broker_worker_url + read_only: true + tmpfs: ['/tmp:size=16m'] + deploy: + replicas: ${WORKER_REPLICAS:-1} + resources: + limits: + cpus: '${WORKER_CPUS:-1.5}' + memory: ${WORKER_MEMORY:-1280m} + stop_grace_period: 40s + restart: unless-stopped + depends_on: + migrate: { condition: service_completed_successfully } + postgres: { condition: service_healthy } + redis: { condition: service_healthy } + broker: { condition: service_healthy } + broker-init: { condition: service_completed_successfully } + s3-init: { condition: service_completed_successfully } + healthcheck: + test: + [ + 'CMD', + 'node', + '-e', + "fetch('http://127.0.0.1:'+ (process.env.WORKER_HEALTH_PORT||8081) + '/health').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))", + ] + interval: 5s + timeout: 3s + retries: 20 + start_period: 20s + + haproxy: + image: haproxy:3.2-alpine + ports: + - '${APP_PUBLIC_PORT:-8080}:8080' + # Страница статистики — интерфейс LB, публикуется только на loopback. + - '127.0.0.1:${LB_STATS_PORT:-8404}:8404' + volumes: + - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + deploy: + resources: + limits: + cpus: '${LB_CPUS:-0.25}' + memory: ${LB_MEMORY:-128m} + depends_on: + api: { condition: service_healthy } + + +secrets: + minio_root_user: { file: ./secrets/minio_root_user } + minio_root_password: { file: ./secrets/minio_root_password } + s3_api_access_key_id: { file: ./secrets/s3_api_access_key_id } + s3_api_secret_access_key: { file: ./secrets/s3_api_secret_access_key } + s3_worker_access_key_id: { file: ./secrets/s3_worker_access_key_id } + s3_worker_secret_access_key: { file: ./secrets/s3_worker_secret_access_key } + broker_url: { file: ./secrets/broker_url } + broker_worker_url: { file: ./secrets/broker_worker_url } + broker_worker_password: { file: ./secrets/broker_worker_password } + rabbitmq_conf: { file: ./secrets/rabbitmq.conf } + +volumes: + postgres-data: + redis-data: + s3-data: + broker-data: diff --git a/contracts/extensions.openapi.json b/contracts/extensions.openapi.json new file mode 100644 index 00000000..8dded17a --- /dev/null +++ b/contracts/extensions.openapi.json @@ -0,0 +1,618 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "MTUSI Messenger — расширение lab4: прямая загрузка", + "version": "1.4.0-ext.2", + "description": "Трек 1. Не меняет обязательный контракт openapi.json: POST /api/v1/chats/{chat_id}/attachments остаётся основным путём. Клиент получает presigned PUT URL и отправляет байты прямо в приватный bucket MinIO, минуя API. Подписаны заголовки content-type, content-length и x-amz-checksum-sha256: хранилище само отвергает тело с другим размером (403 SignatureDoesNotMatch) или другим SHA-256 (400 XAmzContentChecksumMismatch), объект при этом не создаётся. TTL URL — DIRECT_UPLOAD_URL_TTL_SECONDS (300 с по умолчанию). Ключ объекта attachments//original раскрывает только UUID вложения; bucket приватный, анонимный доступ запрещён. Незавершённые и отклонённые заявки и их объекты убирает уборщик через ATTACHMENT_SWEEP_GRACE_SECONDS после истечения. Вложение видно участникам чата только после успешного complete (status=ready)." + }, + "servers": [ + { + "url": "http://localhost:8080", + "description": "Локальный учебный стенд" + } + ], + "security": [ + { + "bearerAuth": [] + } + ], + "paths": { + "/api/v1/chats/{chat_id}/attachments/direct-uploads": { + "post": { + "operationId": "initDirectUpload", + "summary": "Заявка на прямую загрузку: вернуть presigned PUT URL", + "parameters": [ + { + "name": "chat_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateDirectUpload" + } + } + } + }, + "responses": { + "201": { + "description": "Заявка создана; вложение pending и не видно никому, кроме загрузки", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DirectUpload" + } + } + } + }, + "400": { + "description": "Некорректное тело, filename или sha256", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет или неверен токен", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Чат недоступен", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "413": { + "description": "size_bytes больше 10 MiB", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "415": { + "description": "content_type не image/png и не image/jpeg", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Слишком много незавершённых прямых загрузок пользователя; Retry-After — секунды до освобождения слота (TTL URL).", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Секунды до повтора.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Зависимость недоступна", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + } + } + } + }, + "/api/v1/attachments/{attachment_id}/complete": { + "post": { + "operationId": "completeDirectUpload", + "summary": "Подтвердить прямую загрузку: HEAD объекта, полное чтение и декод изображения", + "description": "Только владелец заявки. Сверяет размер и подписанный SHA-256 объекта (HEAD), затем читает объект целиком: структурная проверка PNG/JPEG и полный декод (повреждённые и усечённые изображения отвергаются, даже если checksum совпадает). При несоответствии объект удаляется, а заявка получает внутренний статус rejected до истечения URL и grace-периода уборщика (повторный PUT по ещё действующему URL не оставит неучтённый объект); повторный complete отклонённой заявки — 404. Если объект ещё не загружен — 409, заявка не меняется. Повторный и параллельный complete готового вложения идемпотентен (200).", + "parameters": [ + { + "name": "attachment_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ], + "responses": { + "200": { + "description": "Вложение готово; тело идентично Attachment из openapi.json", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Attachment" + } + } + } + }, + "400": { + "description": "Некорректный attachment_id", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "401": { + "description": "Нет или неверен токен", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "404": { + "description": "Заявка не найдена, не принадлежит пользователю или уже удалена", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "409": { + "description": "Объект не получен или не совпадает с заявленным размером/SHA-256", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "413": { + "description": "Слишком много пикселей", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "415": { + "description": "Содержимое не PNG/JPEG или не совпадает с content_type", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "503": { + "description": "Хранилище недоступно или не ответило за S3_REQUEST_TIMEOUT_MS / UPLOAD_TIMEOUT_MS", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + }, + "429": { + "description": "Реплика проверяет слишком много изображений одновременно (UPLOAD_MAX_IN_FLIGHT) или у пользователя слишком много одновременных проверок (UPLOAD_MAX_IN_FLIGHT_PER_USER); Retry-After — секунды до повтора.", + "headers": { + "X-Request-Id": { + "description": "Идентификатор запроса.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "X-Instance-Id": { + "description": "Идентификатор реплики.", + "schema": { + "type": "string", + "minLength": 1 + } + }, + "Retry-After": { + "description": "Секунды до повтора.", + "schema": { + "type": "integer", + "minimum": 1 + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "openapi.json#/components/schemas/Error" + } + } + } + } + } + } + } + }, + "components": { + "securitySchemes": { + "bearerAuth": { + "type": "http", + "scheme": "bearer", + "bearerFormat": "opaque session token" + } + }, + "schemas": { + "CreateDirectUpload": { + "type": "object", + "additionalProperties": false, + "required": [ + "filename", + "content_type", + "size_bytes", + "sha256" + ], + "properties": { + "filename": { + "type": "string", + "minLength": 1, + "maxLength": 255 + }, + "content_type": { + "type": "string", + "enum": [ + "image/png", + "image/jpeg" + ] + }, + "size_bytes": { + "type": "integer", + "minimum": 1, + "maximum": 10485760 + }, + "sha256": { + "type": "string", + "pattern": "^[a-f0-9]{64}$" + } + } + }, + "DirectUpload": { + "type": "object", + "additionalProperties": false, + "required": [ + "attachment_id", + "method", + "url", + "headers", + "expires_at" + ], + "properties": { + "attachment_id": { + "type": "string", + "format": "uuid" + }, + "method": { + "type": "string", + "enum": [ + "PUT" + ] + }, + "url": { + "type": "string", + "format": "uri", + "description": "Presigned URL; содержит подпись, не логировать." + }, + "headers": { + "type": "object", + "description": "Заголовки, которые клиент обязан отправить как есть. Content-Length клиент ставит сам; тело должно быть ровно size_bytes.", + "properties": { + "Content-Type": { + "type": "string" + }, + "x-amz-checksum-sha256": { + "type": "string" + } + }, + "required": [ + "Content-Type", + "x-amz-checksum-sha256" + ] + }, + "expires_at": { + "type": "string", + "format": "date-time" + } + } + } + } + } +} diff --git a/evidence/lab4/bench-oom-check.txt b/evidence/lab4/bench-oom-check.txt new file mode 100644 index 00000000..8fb74cc0 --- /dev/null +++ b/evidence/lab4/bench-oom-check.txt @@ -0,0 +1,2 @@ +/mtusi-lab4-api-1 OOMKilled=false restarts=0 +/mtusi-lab4-api-2 OOMKilled=false restarts=0 diff --git a/evidence/lab4/bench/direct-1048576-c10.json b/evidence/lab4/bench/direct-1048576-c10.json new file mode 100644 index 00000000..ab327e57 --- /dev/null +++ b/evidence/lab4/bench/direct-1048576-c10.json @@ -0,0 +1,224 @@ +{ + "date": "2026-10-23T14:19:18+00:00", + "git": "9caa031", + "params": { + "mode": "direct", + "size_bytes": 1048576, + "concurrency": 10, + "uploads_per_worker": 5, + "repeat": 3, + "users": 10, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.474, + "latency_ms": { + "p50": 71.2, + "p95": 97.4, + "p99": 114.6 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 26410731, + "delta_tx_bytes": 192869, + "baseline_rss_bytes": 255631360, + "peak_memory_bytes": { + "rss": 255631360, + "heap_used": 55130048, + "external": 30839902, + "array_buffers": 27031366 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 25, + "attachments.insert_pending": 25, + "attachments.direct.initiated": 25, + "attachments.find_for_complete": 25, + "attachments.direct.verify_bytes": 26214400, + "attachments.decode.ms": 149.66068200027803, + "attachments.mark_ready": 25, + "attachments.direct.completed": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 26409853, + "delta_tx_bytes": 199121, + "baseline_rss_bytes": 220901376, + "peak_memory_bytes": { + "rss": 220901376, + "heap_used": 49764944, + "external": 19106520, + "array_buffers": 15302577 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 25, + "attachments.insert_pending": 25, + "attachments.direct.initiated": 25, + "attachments.find_for_complete": 25, + "attachments.direct.verify_bytes": 26214400, + "attachments.decode.ms": 134.77708099997835, + "attachments.mark_ready": 25, + "attachments.direct.completed": 25 + } + } + ], + "total_delta_rx_bytes": 52820584, + "total_delta_tx_bytes": 391990, + "rx_bytes_per_upload": 1056412, + "tx_bytes_per_upload": 7840, + "max_delta_rss_bytes": 0 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.486, + "latency_ms": { + "p50": 67.0, + "p95": 107.3, + "p99": 117.3 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 26407608, + "delta_tx_bytes": 187247, + "baseline_rss_bytes": 255631360, + "peak_memory_bytes": { + "rss": 255631360, + "heap_used": 54714120, + "external": 29734772, + "array_buffers": 25926248 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 25, + "attachments.insert_pending": 25, + "attachments.direct.initiated": 25, + "attachments.find_for_complete": 25, + "attachments.direct.verify_bytes": 26214400, + "attachments.decode.ms": 129.6924299999373, + "attachments.mark_ready": 25, + "attachments.direct.completed": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 26411452, + "delta_tx_bytes": 197525, + "baseline_rss_bytes": 220901376, + "peak_memory_bytes": { + "rss": 220901376, + "heap_used": 50436872, + "external": 20154009, + "array_buffers": 16352314 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 25, + "attachments.insert_pending": 25, + "attachments.direct.initiated": 25, + "attachments.find_for_complete": 25, + "attachments.direct.verify_bytes": 26214400, + "attachments.decode.ms": 146.63997799990466, + "attachments.mark_ready": 25, + "attachments.direct.completed": 25 + } + } + ], + "total_delta_rx_bytes": 52819060, + "total_delta_tx_bytes": 384772, + "rx_bytes_per_upload": 1056381, + "tx_bytes_per_upload": 7695, + "max_delta_rss_bytes": 0 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.588, + "latency_ms": { + "p50": 79.7, + "p95": 122.1, + "p99": 146.3 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 24306748, + "delta_tx_bytes": 183976, + "baseline_rss_bytes": 255631360, + "peak_memory_bytes": { + "rss": 256024576, + "heap_used": 55222760, + "external": 27656537, + "array_buffers": 23848029 + }, + "delta_rss_bytes": 393216, + "counters": { + "attachments.count_pending_direct": 27, + "attachments.insert_pending": 27, + "attachments.direct.initiated": 27, + "attachments.find_for_complete": 23, + "attachments.direct.verify_bytes": 24117248, + "attachments.decode.ms": 164.5750180000323, + "attachments.mark_ready": 23, + "attachments.direct.completed": 23 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 28515559, + "delta_tx_bytes": 188283, + "baseline_rss_bytes": 220901376, + "peak_memory_bytes": { + "rss": 220901376, + "heap_used": 51242112, + "external": 23325182, + "array_buffers": 19516705 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 23, + "attachments.insert_pending": 23, + "attachments.direct.initiated": 23, + "attachments.find_for_complete": 27, + "attachments.direct.verify_bytes": 28311552, + "attachments.decode.ms": 181.22829800000181, + "attachments.mark_ready": 27, + "attachments.direct.completed": 27 + } + } + ], + "total_delta_rx_bytes": 52822307, + "total_delta_tx_bytes": 372259, + "rx_bytes_per_upload": 1056446, + "tx_bytes_per_upload": 7445, + "max_delta_rss_bytes": 393216 + } + ] +} diff --git a/evidence/lab4/bench/direct-131072-c10.json b/evidence/lab4/bench/direct-131072-c10.json new file mode 100644 index 00000000..719e33cb --- /dev/null +++ b/evidence/lab4/bench/direct-131072-c10.json @@ -0,0 +1,224 @@ +{ + "date": "2026-10-23T14:19:05+00:00", + "git": "9caa031", + "params": { + "mode": "direct", + "size_bytes": 131072, + "concurrency": 10, + "uploads_per_worker": 5, + "repeat": 3, + "users": 10, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.225, + "latency_ms": { + "p50": 39.1, + "p95": 67.1, + "p99": 70.2 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 3306246, + "delta_tx_bytes": 183847, + "baseline_rss_bytes": 255115264, + "peak_memory_bytes": { + "rss": 255115264, + "heap_used": 52168040, + "external": 11980507, + "array_buffers": 8188021 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 26, + "attachments.insert_pending": 26, + "attachments.direct.initiated": 26, + "attachments.find_for_complete": 24, + "attachments.direct.verify_bytes": 3145728, + "attachments.decode.ms": 76.04879400000209, + "attachments.mark_ready": 24, + "attachments.direct.completed": 24 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 3572465, + "delta_tx_bytes": 185987, + "baseline_rss_bytes": 410796032, + "peak_memory_bytes": { + "rss": 413024256, + "heap_used": 61028552, + "external": 11110575, + "array_buffers": 7314315 + }, + "delta_rss_bytes": 2228224, + "counters": { + "attachments.count_pending_direct": 24, + "attachments.insert_pending": 24, + "attachments.direct.initiated": 24, + "attachments.find_for_complete": 26, + "attachments.direct.verify_bytes": 3407872, + "attachments.decode.ms": 95.17834699997911, + "attachments.mark_ready": 26, + "attachments.direct.completed": 26 + } + } + ], + "total_delta_rx_bytes": 6878711, + "total_delta_tx_bytes": 369834, + "rx_bytes_per_upload": 137574, + "tx_bytes_per_upload": 7397, + "max_delta_rss_bytes": 2228224 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.237, + "latency_ms": { + "p50": 39.7, + "p95": 70.8, + "p99": 71.9 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 3305361, + "delta_tx_bytes": 182735, + "baseline_rss_bytes": 255115264, + "peak_memory_bytes": { + "rss": 255115264, + "heap_used": 52870848, + "external": 12917408, + "array_buffers": 8731525 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 26, + "attachments.insert_pending": 26, + "attachments.direct.initiated": 26, + "attachments.find_for_complete": 24, + "attachments.direct.verify_bytes": 3145728, + "attachments.decode.ms": 74.47586199996294, + "attachments.mark_ready": 24, + "attachments.direct.completed": 24 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 3572269, + "delta_tx_bytes": 183875, + "baseline_rss_bytes": 413024256, + "peak_memory_bytes": { + "rss": 413417472, + "heap_used": 59807944, + "external": 10703911, + "array_buffers": 6908083 + }, + "delta_rss_bytes": 393216, + "counters": { + "attachments.count_pending_direct": 24, + "attachments.insert_pending": 24, + "attachments.direct.initiated": 24, + "attachments.find_for_complete": 26, + "attachments.direct.verify_bytes": 3407872, + "attachments.decode.ms": 80.40512400015723, + "attachments.mark_ready": 26, + "attachments.direct.completed": 26 + } + } + ], + "total_delta_rx_bytes": 6877630, + "total_delta_tx_bytes": 366610, + "rx_bytes_per_upload": 137553, + "tx_bytes_per_upload": 7332, + "max_delta_rss_bytes": 393216 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.266, + "latency_ms": { + "p50": 45.7, + "p95": 73.8, + "p99": 80.1 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 3306421, + "delta_tx_bytes": 183391, + "baseline_rss_bytes": 255115264, + "peak_memory_bytes": { + "rss": 255377408, + "heap_used": 55403296, + "external": 12765653, + "array_buffers": 8973582 + }, + "delta_rss_bytes": 262144, + "counters": { + "attachments.count_pending_direct": 26, + "attachments.insert_pending": 26, + "attachments.direct.initiated": 26, + "attachments.find_for_complete": 24, + "attachments.direct.verify_bytes": 3145728, + "attachments.decode.ms": 113.08572699979413, + "attachments.mark_ready": 24, + "attachments.direct.completed": 24 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 3572203, + "delta_tx_bytes": 184215, + "baseline_rss_bytes": 413417472, + "peak_memory_bytes": { + "rss": 413548544, + "heap_used": 60296808, + "external": 10700258, + "array_buffers": 6904444 + }, + "delta_rss_bytes": 131072, + "counters": { + "attachments.count_pending_direct": 24, + "attachments.insert_pending": 24, + "attachments.direct.initiated": 24, + "attachments.find_for_complete": 26, + "attachments.direct.verify_bytes": 3407872, + "attachments.decode.ms": 102.82485899998574, + "attachments.mark_ready": 26, + "attachments.direct.completed": 26 + } + } + ], + "total_delta_rx_bytes": 6878624, + "total_delta_tx_bytes": 367606, + "rx_bytes_per_upload": 137572, + "tx_bytes_per_upload": 7352, + "max_delta_rss_bytes": 262144 + } + ] +} diff --git a/evidence/lab4/bench/direct-9961472-c10.json b/evidence/lab4/bench/direct-9961472-c10.json new file mode 100644 index 00000000..43a23c55 --- /dev/null +++ b/evidence/lab4/bench/direct-9961472-c10.json @@ -0,0 +1,224 @@ +{ + "date": "2026-10-23T14:19:48+00:00", + "git": "9caa031", + "params": { + "mode": "direct", + "size_bytes": 9961472, + "concurrency": 10, + "uploads_per_worker": 5, + "repeat": 3, + "users": 10, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 3.75, + "latency_ms": { + "p50": 448.3, + "p95": 674.2, + "p99": 702.9 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 259479426, + "delta_tx_bytes": 287642, + "baseline_rss_bytes": 280027136, + "peak_memory_bytes": { + "rss": 322101248, + "heap_used": 54779008, + "external": 104107482, + "array_buffers": 100313819 + }, + "delta_rss_bytes": 42074112, + "counters": { + "attachments.count_pending_direct": 24, + "attachments.insert_pending": 24, + "attachments.direct.initiated": 24, + "attachments.find_for_complete": 26, + "attachments.direct.verify_bytes": 258998272, + "attachments.decode.ms": 498.32417600008193, + "attachments.mark_ready": 26, + "attachments.direct.completed": 26 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 239524438, + "delta_tx_bytes": 268169, + "baseline_rss_bytes": 243490816, + "peak_memory_bytes": { + "rss": 326799360, + "heap_used": 54201104, + "external": 106161894, + "array_buffers": 102364928 + }, + "delta_rss_bytes": 83308544, + "counters": { + "attachments.count_pending_direct": 26, + "attachments.insert_pending": 26, + "attachments.direct.initiated": 26, + "attachments.find_for_complete": 24, + "attachments.direct.verify_bytes": 239075328, + "attachments.decode.ms": 373.651800000167, + "attachments.mark_ready": 24, + "attachments.direct.completed": 24 + } + } + ], + "total_delta_rx_bytes": 499003864, + "total_delta_tx_bytes": 555811, + "rx_bytes_per_upload": 9980077, + "tx_bytes_per_upload": 11116, + "max_delta_rss_bytes": 83308544 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 3.749, + "latency_ms": { + "p50": 471.3, + "p95": 672.0, + "p99": 739.0 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 229545332, + "delta_tx_bytes": 272949, + "baseline_rss_bytes": 322101248, + "peak_memory_bytes": { + "rss": 322101248, + "heap_used": 53139240, + "external": 120388652, + "array_buffers": 115495375 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.count_pending_direct": 27, + "attachments.insert_pending": 27, + "attachments.direct.initiated": 27, + "attachments.find_for_complete": 23, + "attachments.direct.verify_bytes": 229113856, + "attachments.decode.ms": 295.2854530001059, + "attachments.mark_ready": 23, + "attachments.direct.completed": 23 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 269451762, + "delta_tx_bytes": 274913, + "baseline_rss_bytes": 294375424, + "peak_memory_bytes": { + "rss": 360865792, + "heap_used": 53636296, + "external": 157544946, + "array_buffers": 153746128 + }, + "delta_rss_bytes": 66490368, + "counters": { + "attachments.count_pending_direct": 23, + "attachments.insert_pending": 23, + "attachments.direct.initiated": 23, + "attachments.find_for_complete": 27, + "attachments.direct.verify_bytes": 268959744, + "attachments.decode.ms": 467.34784599993145, + "attachments.mark_ready": 27, + "attachments.direct.completed": 27 + } + } + ], + "total_delta_rx_bytes": 498997094, + "total_delta_tx_bytes": 547862, + "rx_bytes_per_upload": 9979942, + "tx_bytes_per_upload": 10957, + "max_delta_rss_bytes": 66490368 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 3.501, + "latency_ms": { + "p50": 404.2, + "p95": 599.0, + "p99": 605.8 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 219577054, + "delta_tx_bytes": 272143, + "baseline_rss_bytes": 270405632, + "peak_memory_bytes": { + "rss": 290258944, + "heap_used": 52555024, + "external": 103954247, + "array_buffers": 100162235 + }, + "delta_rss_bytes": 19853312, + "counters": { + "attachments.count_pending_direct": 28, + "attachments.insert_pending": 28, + "attachments.direct.initiated": 28, + "attachments.find_for_complete": 22, + "attachments.direct.verify_bytes": 219152384, + "attachments.decode.ms": 273.8368729999056, + "attachments.mark_ready": 22, + "attachments.direct.completed": 22 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 279433430, + "delta_tx_bytes": 286105, + "baseline_rss_bytes": 223891456, + "peak_memory_bytes": { + "rss": 294211584, + "heap_used": 50865176, + "external": 84552392, + "array_buffers": 80757048 + }, + "delta_rss_bytes": 70320128, + "counters": { + "attachments.count_pending_direct": 22, + "attachments.insert_pending": 22, + "attachments.direct.initiated": 22, + "attachments.find_for_complete": 28, + "attachments.direct.verify_bytes": 278921216, + "attachments.decode.ms": 406.8028439999907, + "attachments.mark_ready": 28, + "attachments.direct.completed": 28 + } + } + ], + "total_delta_rx_bytes": 499010484, + "total_delta_tx_bytes": 558248, + "rx_bytes_per_upload": 9980210, + "tx_bytes_per_upload": 11165, + "max_delta_rss_bytes": 70320128 + } + ] +} diff --git a/evidence/lab4/bench/direct-worst-c32.json b/evidence/lab4/bench/direct-worst-c32.json new file mode 100644 index 00000000..07081154 --- /dev/null +++ b/evidence/lab4/bench/direct-worst-c32.json @@ -0,0 +1,229 @@ +{ + "date": "2026-10-23T14:23:13+00:00", + "git": "9caa031", + "params": { + "mode": "direct", + "size_bytes": 10485760, + "concurrency": 32, + "uploads_per_worker": 2, + "repeat": 3, + "users": 32, + "image": "worst-40mp-rgba16-adam7.png" + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 64, + "succeeded": 48, + "failed": 16, + "outcomes": { + "ok": 48, + "complete:429": 16 + }, + "duration_s": 25.881, + "latency_ms": { + "p50": 11067.1, + "p95": 13423.0, + "p99": 14220.4 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 168217561, + "delta_tx_bytes": 384114, + "baseline_rss_bytes": 428978176, + "peak_memory_bytes": { + "rss": 823418880, + "heap_used": 52305208, + "external": 243338213, + "array_buffers": 239547869 + }, + "delta_rss_bytes": 394440704, + "counters": { + "attachments.count_pending_direct": 48, + "attachments.insert_pending": 48, + "attachments.direct.initiated": 48, + "attachments.find_for_complete": 16, + "attachments.direct.verify_bytes": 167772160, + "attachments.decode.ms": 13205.84250300005, + "attachments.mark_ready": 16, + "attachments.direct.completed": 16 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 336236648, + "delta_tx_bytes": 458338, + "baseline_rss_bytes": 288284672, + "peak_memory_bytes": { + "rss": 803422208, + "heap_used": 51748952, + "external": 236179385, + "array_buffers": 232389041 + }, + "delta_rss_bytes": 515137536, + "counters": { + "attachments.count_pending_direct": 16, + "attachments.insert_pending": 16, + "attachments.direct.initiated": 16, + "attachments.find_for_complete": 48, + "attachments.direct.verify_bytes": 335544320, + "attachments.decode.ms": 24842.089320000494, + "attachments.mark_ready": 32, + "attachments.direct.completed": 32 + } + } + ], + "total_delta_rx_bytes": 504454209, + "total_delta_tx_bytes": 842452, + "rx_bytes_per_upload": 7882097, + "tx_bytes_per_upload": 13163, + "max_delta_rss_bytes": 515137536 + }, + { + "uploads": 64, + "succeeded": 60, + "failed": 4, + "outcomes": { + "ok": 60, + "complete:429": 4 + }, + "duration_s": 26.523, + "latency_ms": { + "p50": 11841.6, + "p95": 13541.3, + "p99": 14293.7 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 294214487, + "delta_tx_bytes": 425744, + "baseline_rss_bytes": 470261760, + "peak_memory_bytes": { + "rss": 828153856, + "heap_used": 51846456, + "external": 242086934, + "array_buffers": 238296517 + }, + "delta_rss_bytes": 357892096, + "counters": { + "attachments.count_pending_direct": 36, + "attachments.insert_pending": 36, + "attachments.direct.initiated": 36, + "attachments.find_for_complete": 28, + "attachments.direct.verify_bytes": 293601280, + "attachments.decode.ms": 22602.63425100036, + "attachments.mark_ready": 28, + "attachments.claim_expired": 1, + "attachments.direct.completed": 28 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 336212832, + "delta_tx_bytes": 461394, + "baseline_rss_bytes": 429502464, + "peak_memory_bytes": { + "rss": 814202880, + "heap_used": 51503296, + "external": 256575006, + "array_buffers": 212949888 + }, + "delta_rss_bytes": 384700416, + "counters": { + "attachments.count_pending_direct": 28, + "attachments.insert_pending": 28, + "attachments.direct.initiated": 28, + "attachments.find_for_complete": 36, + "attachments.direct.verify_bytes": 335544320, + "attachments.decode.ms": 25502.105729999603, + "attachments.mark_ready": 32, + "attachments.direct.completed": 32, + "attachments.claim_expired": 1 + } + } + ], + "total_delta_rx_bytes": 630427319, + "total_delta_tx_bytes": 887138, + "rx_bytes_per_upload": 9850427, + "tx_bytes_per_upload": 13862, + "max_delta_rss_bytes": 384700416 + }, + { + "uploads": 64, + "succeeded": 59, + "failed": 5, + "outcomes": { + "complete:429": 5, + "ok": 59 + }, + "duration_s": 26.673, + "latency_ms": { + "p50": 10563.3, + "p95": 13343.1, + "p99": 14265.7 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 283773903, + "delta_tx_bytes": 418163, + "baseline_rss_bytes": 474800128, + "peak_memory_bytes": { + "rss": 829358080, + "heap_used": 51810024, + "external": 242621863, + "array_buffers": 228344296 + }, + "delta_rss_bytes": 354557952, + "counters": { + "attachments.count_pending_direct": 37, + "attachments.insert_pending": 37, + "attachments.direct.initiated": 37, + "attachments.find_for_complete": 27, + "attachments.direct.verify_bytes": 283115520, + "attachments.decode.ms": 22160.10410899995, + "attachments.mark_ready": 27, + "attachments.direct.completed": 27 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 336214318, + "delta_tx_bytes": 426666, + "baseline_rss_bytes": 461180928, + "peak_memory_bytes": { + "rss": 814313472, + "heap_used": 54614560, + "external": 256831606, + "array_buffers": 229984560 + }, + "delta_rss_bytes": 353132544, + "counters": { + "attachments.count_pending_direct": 27, + "attachments.insert_pending": 27, + "attachments.direct.initiated": 27, + "attachments.find_for_complete": 37, + "attachments.direct.verify_bytes": 335544320, + "attachments.decode.ms": 25823.93422999978, + "attachments.mark_ready": 32, + "attachments.direct.completed": 32 + } + } + ], + "total_delta_rx_bytes": 619988221, + "total_delta_tx_bytes": 844829, + "rx_bytes_per_upload": 9687316, + "tx_bytes_per_upload": 13200, + "max_delta_rss_bytes": 354557952 + } + ] +} diff --git a/evidence/lab4/bench/proxy-1048576-c10.json b/evidence/lab4/bench/proxy-1048576-c10.json new file mode 100644 index 00000000..19e6321c --- /dev/null +++ b/evidence/lab4/bench/proxy-1048576-c10.json @@ -0,0 +1,212 @@ +{ + "date": "2026-10-23T14:19:11+00:00", + "git": "9caa031", + "params": { + "mode": "proxy", + "size_bytes": 1048576, + "concurrency": 10, + "uploads_per_worker": 5, + "repeat": 3, + "users": 10, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.402, + "latency_ms": { + "p50": 48.4, + "p95": 84.1, + "p99": 92.3 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 26376487, + "delta_tx_bytes": 26404723, + "baseline_rss_bytes": 255217664, + "peak_memory_bytes": { + "rss": 255610880, + "heap_used": 52221216, + "external": 23026820, + "array_buffers": 19216043 + }, + "delta_rss_bytes": 393216, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 26214400, + "attachments.decode.ms": 156.9926100000739, + "attachments.proxy.bytes_to_s3": 26214400, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 26386314, + "delta_tx_bytes": 26402341, + "baseline_rss_bytes": 413614080, + "peak_memory_bytes": { + "rss": 413876224, + "heap_used": 55746224, + "external": 28951897, + "array_buffers": 25131678 + }, + "delta_rss_bytes": 262144, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 26214400, + "attachments.decode.ms": 162.94164999981876, + "attachments.proxy.bytes_to_s3": 26214400, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 52762801, + "total_delta_tx_bytes": 52807064, + "rx_bytes_per_upload": 1055256, + "tx_bytes_per_upload": 1056141, + "max_delta_rss_bytes": 393216 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.38, + "latency_ms": { + "p50": 50.7, + "p95": 82.2, + "p99": 88.5 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 26388819, + "delta_tx_bytes": 26402419, + "baseline_rss_bytes": 255610880, + "peak_memory_bytes": { + "rss": 255741952, + "heap_used": 53542032, + "external": 30837329, + "array_buffers": 27022020 + }, + "delta_rss_bytes": 131072, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 26214400, + "attachments.decode.ms": 149.34638900024584, + "attachments.proxy.bytes_to_s3": 26214400, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 26377403, + "delta_tx_bytes": 26400247, + "baseline_rss_bytes": 220905472, + "peak_memory_bytes": { + "rss": 221036544, + "heap_used": 51357984, + "external": 22079805, + "array_buffers": 18271239 + }, + "delta_rss_bytes": 131072, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 26214400, + "attachments.decode.ms": 137.65105199965183, + "attachments.proxy.bytes_to_s3": 26214400, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 52766222, + "total_delta_tx_bytes": 52802666, + "rx_bytes_per_upload": 1055324, + "tx_bytes_per_upload": 1056053, + "max_delta_rss_bytes": 131072 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.385, + "latency_ms": { + "p50": 49.7, + "p95": 73.2, + "p99": 80.6 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 26383716, + "delta_tx_bytes": 26401715, + "baseline_rss_bytes": 255741952, + "peak_memory_bytes": { + "rss": 255873024, + "heap_used": 52957000, + "external": 26930642, + "array_buffers": 23117596 + }, + "delta_rss_bytes": 131072, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 26214400, + "attachments.decode.ms": 139.30256199993892, + "attachments.proxy.bytes_to_s3": 26214400, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 26386387, + "delta_tx_bytes": 26402657, + "baseline_rss_bytes": 221036544, + "peak_memory_bytes": { + "rss": 221036544, + "heap_used": 51459840, + "external": 22079805, + "array_buffers": 16097979 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 26214400, + "attachments.decode.ms": 120.35195299994666, + "attachments.proxy.bytes_to_s3": 26214400, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 52770103, + "total_delta_tx_bytes": 52804372, + "rx_bytes_per_upload": 1055402, + "tx_bytes_per_upload": 1056087, + "max_delta_rss_bytes": 131072 + } + ] +} diff --git a/evidence/lab4/bench/proxy-131072-c10.json b/evidence/lab4/bench/proxy-131072-c10.json new file mode 100644 index 00000000..39e183e8 --- /dev/null +++ b/evidence/lab4/bench/proxy-131072-c10.json @@ -0,0 +1,212 @@ +{ + "date": "2026-10-23T14:18:59+00:00", + "git": "9caa031", + "params": { + "mode": "proxy", + "size_bytes": 131072, + "concurrency": 10, + "uploads_per_worker": 5, + "repeat": 3, + "users": 10, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.169, + "latency_ms": { + "p50": 26.1, + "p95": 46.2, + "p99": 49.0 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 3382850, + "delta_tx_bytes": 3398303, + "baseline_rss_bytes": 468705280, + "peak_memory_bytes": { + "rss": 468705280, + "heap_used": 50851544, + "external": 134219881, + "array_buffers": 130427173 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 3276800, + "attachments.decode.ms": 100.31424599990714, + "attachments.proxy.bytes_to_s3": 3276800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 3382858, + "delta_tx_bytes": 3398701, + "baseline_rss_bytes": 449990656, + "peak_memory_bytes": { + "rss": 451563520, + "heap_used": 58537824, + "external": 181003027, + "array_buffers": 177201619 + }, + "delta_rss_bytes": 1572864, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 3276800, + "attachments.decode.ms": 102.65141499997117, + "attachments.proxy.bytes_to_s3": 3276800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 6765708, + "total_delta_tx_bytes": 6797004, + "rx_bytes_per_upload": 135314, + "tx_bytes_per_upload": 135940, + "max_delta_rss_bytes": 1572864 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.198, + "latency_ms": { + "p50": 24.7, + "p95": 79.6, + "p99": 87.8 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 3378497, + "delta_tx_bytes": 3395989, + "baseline_rss_bytes": 468705280, + "peak_memory_bytes": { + "rss": 468705280, + "heap_used": 51433088, + "external": 135937131, + "array_buffers": 132143156 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 3276800, + "attachments.decode.ms": 133.3207659999607, + "attachments.proxy.bytes_to_s3": 3276800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 3377682, + "delta_tx_bytes": 3396121, + "baseline_rss_bytes": 410624000, + "peak_memory_bytes": { + "rss": 410624000, + "heap_used": 57652400, + "external": 13565945, + "array_buffers": 9765488 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 3276800, + "attachments.decode.ms": 68.74197999993339, + "attachments.proxy.bytes_to_s3": 3276800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 6756179, + "total_delta_tx_bytes": 6792110, + "rx_bytes_per_upload": 135124, + "tx_bytes_per_upload": 135842, + "max_delta_rss_bytes": 0 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 0.161, + "latency_ms": { + "p50": 26.1, + "p95": 42.7, + "p99": 45.7 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 3374905, + "delta_tx_bytes": 3395785, + "baseline_rss_bytes": 254783488, + "peak_memory_bytes": { + "rss": 254783488, + "heap_used": 51574496, + "external": 12158693, + "array_buffers": 8364940 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 3276800, + "attachments.decode.ms": 77.51481299975421, + "attachments.proxy.bytes_to_s3": 3276800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 3376331, + "delta_tx_bytes": 3394989, + "baseline_rss_bytes": 410624000, + "peak_memory_bytes": { + "rss": 410624000, + "heap_used": 58715704, + "external": 14697744, + "array_buffers": 10896445 + }, + "delta_rss_bytes": 0, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 3276800, + "attachments.decode.ms": 79.76343099982478, + "attachments.proxy.bytes_to_s3": 3276800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 6751236, + "total_delta_tx_bytes": 6790774, + "rx_bytes_per_upload": 135025, + "tx_bytes_per_upload": 135815, + "max_delta_rss_bytes": 0 + } + ] +} diff --git a/evidence/lab4/bench/proxy-9961472-c10.json b/evidence/lab4/bench/proxy-9961472-c10.json new file mode 100644 index 00000000..cfe1f948 --- /dev/null +++ b/evidence/lab4/bench/proxy-9961472-c10.json @@ -0,0 +1,214 @@ +{ + "date": "2026-10-23T14:19:32+00:00", + "git": "9caa031", + "params": { + "mode": "proxy", + "size_bytes": 9961472, + "concurrency": 10, + "uploads_per_worker": 5, + "repeat": 3, + "users": 10, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 2.923, + "latency_ms": { + "p50": 245.8, + "p95": 426.3, + "p99": 473.7 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 249684975, + "delta_tx_bytes": 249854149, + "baseline_rss_bytes": 257658880, + "peak_memory_bytes": { + "rss": 258445312, + "heap_used": 50586328, + "external": 54821684, + "array_buffers": 51025026 + }, + "delta_rss_bytes": 786432, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 249036800, + "attachments.claim_expired": 1, + "attachments.decode.ms": 319.71605499990983, + "attachments.proxy.bytes_to_s3": 249036800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 249696173, + "delta_tx_bytes": 249850753, + "baseline_rss_bytes": 242159616, + "peak_memory_bytes": { + "rss": 263458816, + "heap_used": 50272920, + "external": 64509948, + "array_buffers": 60710825 + }, + "delta_rss_bytes": 21299200, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 249036800, + "attachments.decode.ms": 417.5356929998379, + "attachments.proxy.bytes_to_s3": 249036800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25, + "attachments.claim_expired": 1 + } + } + ], + "total_delta_rx_bytes": 499381148, + "total_delta_tx_bytes": 499704902, + "rx_bytes_per_upload": 9987623, + "tx_bytes_per_upload": 9994098, + "max_delta_rss_bytes": 21299200 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 2.962, + "latency_ms": { + "p50": 218.3, + "p95": 399.3, + "p99": 460.0 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 249696535, + "delta_tx_bytes": 249834513, + "baseline_rss_bytes": 258445312, + "peak_memory_bytes": { + "rss": 278368256, + "heap_used": 51479592, + "external": 64969358, + "array_buffers": 60432585 + }, + "delta_rss_bytes": 19922944, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 249036800, + "attachments.decode.ms": 418.99375499994494, + "attachments.proxy.bytes_to_s3": 249036800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 249668505, + "delta_tx_bytes": 249828657, + "baseline_rss_bytes": 263458816, + "peak_memory_bytes": { + "rss": 286236672, + "heap_used": 50811912, + "external": 98237420, + "array_buffers": 75492114 + }, + "delta_rss_bytes": 22777856, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 249036800, + "attachments.decode.ms": 377.89479400013806, + "attachments.proxy.bytes_to_s3": 249036800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 499365040, + "total_delta_tx_bytes": 499663170, + "rx_bytes_per_upload": 9987301, + "tx_bytes_per_upload": 9993263, + "max_delta_rss_bytes": 22777856 + }, + { + "uploads": 50, + "succeeded": 50, + "failed": 0, + "outcomes": { + "ok": 50 + }, + "duration_s": 2.985, + "latency_ms": { + "p50": 244.7, + "p95": 375.1, + "p99": 434.8 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 249713919, + "delta_tx_bytes": 249869668, + "baseline_rss_bytes": 278368256, + "peak_memory_bytes": { + "rss": 280072192, + "heap_used": 51096488, + "external": 54668040, + "array_buffers": 50874378 + }, + "delta_rss_bytes": 1703936, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 249036800, + "attachments.decode.ms": 335.8925800000434, + "attachments.proxy.bytes_to_s3": 249036800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 249686633, + "delta_tx_bytes": 249847874, + "baseline_rss_bytes": 243417088, + "peak_memory_bytes": { + "rss": 253378560, + "heap_used": 50078992, + "external": 54471131, + "array_buffers": 50675083 + }, + "delta_rss_bytes": 9961472, + "counters": { + "attachments.insert_pending": 25, + "attachments.proxy.bytes_in": 249036800, + "attachments.decode.ms": 318.2283639999805, + "attachments.proxy.bytes_to_s3": 249036800, + "attachments.mark_ready": 25, + "attachments.upload.created": 25 + } + } + ], + "total_delta_rx_bytes": 499400552, + "total_delta_tx_bytes": 499717542, + "rx_bytes_per_upload": 9988011, + "tx_bytes_per_upload": 9994351, + "max_delta_rss_bytes": 9961472 + } + ] +} diff --git a/evidence/lab4/bench/proxy-9961472-c20.json b/evidence/lab4/bench/proxy-9961472-c20.json new file mode 100644 index 00000000..7dc09983 --- /dev/null +++ b/evidence/lab4/bench/proxy-9961472-c20.json @@ -0,0 +1,212 @@ +{ + "date": "2026-10-23T14:20:13+00:00", + "git": "9caa031", + "params": { + "mode": "proxy", + "size_bytes": 9961472, + "concurrency": 20, + "uploads_per_worker": 5, + "repeat": 3, + "users": 20, + "image": null + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 100, + "succeeded": 100, + "failed": 0, + "outcomes": { + "ok": 100 + }, + "duration_s": 6.03, + "latency_ms": { + "p50": 446.3, + "p95": 781.2, + "p99": 875.5 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 499450818, + "delta_tx_bytes": 499682679, + "baseline_rss_bytes": 260186112, + "peak_memory_bytes": { + "rss": 270147584, + "heap_used": 50815808, + "external": 74823876, + "array_buffers": 71026869 + }, + "delta_rss_bytes": 9961472, + "counters": { + "attachments.insert_pending": 50, + "attachments.proxy.bytes_in": 498073600, + "attachments.decode.ms": 747.5779010000988, + "attachments.proxy.bytes_to_s3": 498073600, + "attachments.mark_ready": 50, + "attachments.upload.created": 50 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 499478040, + "delta_tx_bytes": 499706687, + "baseline_rss_bytes": 233414656, + "peak_memory_bytes": { + "rss": 272007168, + "heap_used": 51073272, + "external": 81928377, + "array_buffers": 78003733 + }, + "delta_rss_bytes": 38592512, + "counters": { + "attachments.insert_pending": 50, + "attachments.proxy.bytes_in": 498073600, + "attachments.decode.ms": 729.9557609999902, + "attachments.proxy.bytes_to_s3": 498073600, + "attachments.mark_ready": 50, + "attachments.upload.created": 50 + } + } + ], + "total_delta_rx_bytes": 998928858, + "total_delta_tx_bytes": 999389366, + "rx_bytes_per_upload": 9989289, + "tx_bytes_per_upload": 9993894, + "max_delta_rss_bytes": 38592512 + }, + { + "uploads": 100, + "succeeded": 100, + "failed": 0, + "outcomes": { + "ok": 100 + }, + "duration_s": 5.959, + "latency_ms": { + "p50": 433.3, + "p95": 826.3, + "p99": 1157.3 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 499374042, + "delta_tx_bytes": 499651950, + "baseline_rss_bytes": 270147584, + "peak_memory_bytes": { + "rss": 272506880, + "heap_used": 51829328, + "external": 83084989, + "array_buffers": 79288011 + }, + "delta_rss_bytes": 2359296, + "counters": { + "attachments.insert_pending": 50, + "attachments.proxy.bytes_in": 498073600, + "attachments.decode.ms": 779.4219730002806, + "attachments.proxy.bytes_to_s3": 498073600, + "attachments.mark_ready": 50, + "attachments.upload.created": 50 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 499493122, + "delta_tx_bytes": 499744106, + "baseline_rss_bytes": 223895552, + "peak_memory_bytes": { + "rss": 268492800, + "heap_used": 51545968, + "external": 74440258, + "array_buffers": 70643266 + }, + "delta_rss_bytes": 44597248, + "counters": { + "attachments.insert_pending": 50, + "attachments.proxy.bytes_in": 498073600, + "attachments.decode.ms": 863.9362580000306, + "attachments.proxy.bytes_to_s3": 498073600, + "attachments.mark_ready": 50, + "attachments.upload.created": 50 + } + } + ], + "total_delta_rx_bytes": 998867164, + "total_delta_tx_bytes": 999396056, + "rx_bytes_per_upload": 9988672, + "tx_bytes_per_upload": 9993961, + "max_delta_rss_bytes": 44597248 + }, + { + "uploads": 100, + "succeeded": 100, + "failed": 0, + "outcomes": { + "ok": 100 + }, + "duration_s": 5.867, + "latency_ms": { + "p50": 425.4, + "p95": 748.6, + "p99": 899.5 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 499413858, + "delta_tx_bytes": 499718735, + "baseline_rss_bytes": 272506880, + "peak_memory_bytes": { + "rss": 282468352, + "heap_used": 51214136, + "external": 94183394, + "array_buffers": 90388084 + }, + "delta_rss_bytes": 9961472, + "counters": { + "attachments.insert_pending": 50, + "attachments.proxy.bytes_in": 498073600, + "attachments.decode.ms": 737.3356790000107, + "attachments.proxy.bytes_to_s3": 498073600, + "attachments.mark_ready": 50, + "attachments.upload.created": 50 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 499436776, + "delta_tx_bytes": 499691520, + "baseline_rss_bytes": 224206848, + "peak_memory_bytes": { + "rss": 287236096, + "heap_used": 51412744, + "external": 94514510, + "array_buffers": 90719177 + }, + "delta_rss_bytes": 63029248, + "counters": { + "attachments.insert_pending": 50, + "attachments.proxy.bytes_in": 498073600, + "attachments.decode.ms": 632.1155839997809, + "attachments.proxy.bytes_to_s3": 498073600, + "attachments.mark_ready": 50, + "attachments.upload.created": 50 + } + } + ], + "total_delta_rx_bytes": 998850634, + "total_delta_tx_bytes": 999410255, + "rx_bytes_per_upload": 9988506, + "tx_bytes_per_upload": 9994103, + "max_delta_rss_bytes": 63029248 + } + ] +} diff --git a/evidence/lab4/bench/proxy-worst-c32.json b/evidence/lab4/bench/proxy-worst-c32.json new file mode 100644 index 00000000..58fac4ca --- /dev/null +++ b/evidence/lab4/bench/proxy-worst-c32.json @@ -0,0 +1,216 @@ +{ + "date": "2026-10-23T14:21:43+00:00", + "git": "9caa031", + "params": { + "mode": "proxy", + "size_bytes": 10485760, + "concurrency": 32, + "uploads_per_worker": 2, + "repeat": 3, + "users": 32, + "image": "worst-40mp-rgba16-adam7.png" + }, + "host": { + "nproc": 8, + "mem_total_bytes": 16538955776 + }, + "api_container_limits_nanocpus_memory": [ + "1500000000 1073741824", + "1500000000 1073741824" + ], + "repeats": [ + { + "uploads": 64, + "succeeded": 63, + "failed": 1, + "outcomes": { + "ok": 63, + "upload:429": 1 + }, + "duration_s": 26.314, + "latency_ms": { + "p50": 11982.1, + "p95": 13221.2, + "p99": 13881.3 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 337116624, + "delta_tx_bytes": 326709435, + "baseline_rss_bytes": 280637440, + "peak_memory_bytes": { + "rss": 794800128, + "heap_used": 52577168, + "external": 200629533, + "array_buffers": 196839189 + }, + "delta_rss_bytes": 514162688, + "counters": { + "attachments.insert_pending": 31, + "attachments.proxy.bytes_in": 325058560, + "attachments.decode.ms": 25555.52753299987, + "attachments.proxy.bytes_to_s3": 325058560, + "attachments.mark_ready": 31, + "attachments.upload.created": 31 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 337265287, + "delta_tx_bytes": 337358402, + "baseline_rss_bytes": 243548160, + "peak_memory_bytes": { + "rss": 810434560, + "heap_used": 53145984, + "external": 236705259, + "array_buffers": 220341467 + }, + "delta_rss_bytes": 566886400, + "counters": { + "attachments.insert_pending": 32, + "attachments.proxy.bytes_in": 335544320, + "attachments.decode.ms": 26139.059334000223, + "attachments.proxy.bytes_to_s3": 335544320, + "attachments.mark_ready": 32, + "attachments.upload.created": 32 + } + } + ], + "total_delta_rx_bytes": 674381911, + "total_delta_tx_bytes": 664067837, + "rx_bytes_per_upload": 10537217, + "tx_bytes_per_upload": 10376060, + "max_delta_rss_bytes": 566886400 + }, + { + "uploads": 64, + "succeeded": 63, + "failed": 1, + "outcomes": { + "ok": 63, + "upload:429": 1 + }, + "duration_s": 26.067, + "latency_ms": { + "p50": 11840.4, + "p95": 12960.2, + "p99": 13468.5 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 336715328, + "delta_tx_bytes": 326375446, + "baseline_rss_bytes": 440582144, + "peak_memory_bytes": { + "rss": 792059904, + "heap_used": 56538592, + "external": 222265987, + "array_buffers": 218475643 + }, + "delta_rss_bytes": 351477760, + "counters": { + "attachments.insert_pending": 31, + "attachments.proxy.bytes_in": 325058560, + "attachments.decode.ms": 25179.0629260001, + "attachments.proxy.bytes_to_s3": 325058560, + "attachments.mark_ready": 31, + "attachments.upload.created": 31 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 336711646, + "delta_tx_bytes": 336796533, + "baseline_rss_bytes": 456798208, + "peak_memory_bytes": { + "rss": 804114432, + "heap_used": 56361496, + "external": 227834931, + "array_buffers": 224044587 + }, + "delta_rss_bytes": 347316224, + "counters": { + "attachments.insert_pending": 32, + "attachments.proxy.bytes_in": 335544320, + "attachments.decode.ms": 25936.202234999975, + "attachments.proxy.bytes_to_s3": 335544320, + "attachments.mark_ready": 32, + "attachments.upload.created": 32 + } + } + ], + "total_delta_rx_bytes": 673426974, + "total_delta_tx_bytes": 663171979, + "rx_bytes_per_upload": 10522296, + "tx_bytes_per_upload": 10362062, + "max_delta_rss_bytes": 351477760 + }, + { + "uploads": 64, + "succeeded": 64, + "failed": 0, + "outcomes": { + "ok": 64 + }, + "duration_s": 26.191, + "latency_ms": { + "p50": 12369.3, + "p95": 13068.5, + "p99": 13838.5 + }, + "replicas": [ + { + "instance_id": "90e2e1d73b23", + "delta_rx_bytes": 336907021, + "delta_tx_bytes": 336990630, + "baseline_rss_bytes": 438906880, + "peak_memory_bytes": { + "rss": 812994560, + "heap_used": 53308256, + "external": 217363492, + "array_buffers": 213573148 + }, + "delta_rss_bytes": 374087680, + "counters": { + "attachments.insert_pending": 32, + "attachments.proxy.bytes_in": 335544320, + "attachments.decode.ms": 25964.192683000234, + "attachments.proxy.bytes_to_s3": 335544320, + "attachments.mark_ready": 32, + "attachments.upload.created": 32, + "attachments.claim_expired": 1 + } + }, + { + "instance_id": "9bb9a19b4693", + "delta_rx_bytes": 336764548, + "delta_tx_bytes": 336853328, + "baseline_rss_bytes": 451035136, + "peak_memory_bytes": { + "rss": 822910976, + "heap_used": 51718280, + "external": 238155032, + "array_buffers": 234364688 + }, + "delta_rss_bytes": 371875840, + "counters": { + "attachments.insert_pending": 32, + "attachments.proxy.bytes_in": 335544320, + "attachments.decode.ms": 25902.83660300006, + "attachments.proxy.bytes_to_s3": 335544320, + "attachments.mark_ready": 32, + "attachments.upload.created": 32, + "attachments.claim_expired": 1 + } + } + ], + "total_delta_rx_bytes": 673671569, + "total_delta_tx_bytes": 673843958, + "rx_bytes_per_upload": 10526118, + "tx_bytes_per_upload": 10528812, + "max_delta_rss_bytes": 374087680 + } + ] +} diff --git a/evidence/lab4/bootstrap-rerun.txt b/evidence/lab4/bootstrap-rerun.txt new file mode 100644 index 00000000..2fdd01c4 --- /dev/null +++ b/evidence/lab4/bootstrap-rerun.txt @@ -0,0 +1,12 @@ +Bucket created successfully `stand/messenger-attachments`. +Access permission for `stand/messenger-attachments` is set to `private` +Created policy `messenger-api` successfully. +Added user `api-` successfully. +Attached Policies: [messenger-api] +To User: api- +bootstrap ok: bucket=messenger-attachments policy=messenger-api +Bucket created successfully `stand/messenger-attachments`. +Access permission for `stand/messenger-attachments` is set to `private` +Created policy `messenger-api` successfully. +Added user `api-` successfully. +bootstrap ok: bucket=messenger-attachments policy=messenger-api diff --git a/evidence/lab4/consistency.txt b/evidence/lab4/consistency.txt new file mode 100644 index 00000000..cde6d533 --- /dev/null +++ b/evidence/lab4/consistency.txt @@ -0,0 +1,9 @@ +scripts/lab4/consistency_check.sh +bucket: messenger-attachments +objects: 8398 +rows by status: + 84 pending + 8324 ready + 4 rejected +orphans: 0 +missing: 0 diff --git a/evidence/lab4/decode-memory.txt b/evidence/lab4/decode-memory.txt new file mode 100644 index 00000000..9bf773dc --- /dev/null +++ b/evidence/lab4/decode-memory.txt @@ -0,0 +1,8 @@ +# container /mtusi-lab4-api-1, memory limit 1073741824 bytes, VIPS_DISC_THRESHOLD=1g, commit 4b53e03; same pipeline as ImageDecoder +jpeg 7728x5152 3ch uchar bytes=4921012 decode_ms=61 peak_delta_rss_mib=4.5 ok +jpeg 7728x5152 3ch uchar progressive bytes=4826968 decode_ms=273 peak_delta_rss_mib=118.1 ok +jpeg 7728x5152 4ch uchar bytes=10601868 decode_ms=159 peak_delta_rss_mib=7.5 ok +png 8000x5000 4ch ushort bytes=1420114 decode_ms=524 peak_delta_rss_mib=67.1 ok +png 8000x5000 4ch ushort progressive bytes=364096 decode_ms=1231 peak_delta_rss_mib=338.5 ok +png 8000x5000 3ch uchar progressive palette bytes=61042 decode_ms=356 peak_delta_rss_mib=126.0 ok +png 8000x5000 4ch ushort progressive bytes=10485760 decode_ms=707 peak_delta_rss_mib=338.4 ok diff --git a/evidence/lab4/direct-ttl-short.txt b/evidence/lab4/direct-ttl-short.txt new file mode 100644 index 00000000..f9028aea --- /dev/null +++ b/evidence/lab4/direct-ttl-short.txt @@ -0,0 +1,8 @@ +> app@0.0.1 test:e2e +> vitest run --config ./vitest.config.e2e.ts direct-upload +The plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the resolve.tsconfigPaths option. You can remove the plugin and set resolve.tsconfigPaths: true in your Vite config instead. + RUN v4.1.11 /home/tomato/Projects/VP/mtusi2026lab4/app + Test Files 1 passed (1) + Tests 10 passed (10) + Start at 17:11:07 + Duration 8.19s (transform 48ms, setup 0ms, import 72ms, tests 8.02s, environment 0ms) diff --git a/evidence/lab4/failover-attachment.txt b/evidence/lab4/failover-attachment.txt new file mode 100644 index 00000000..228475c3 --- /dev/null +++ b/evidence/lab4/failover-attachment.txt @@ -0,0 +1,11 @@ +# commit 9caa031; attachment download survives the stop of either replica (sha256 of sample.png: 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75) +stopped mtusi-lab4-api-1 + GET content -> HTTP/1.1 200 OK x-instance-id: 9bb9a19b4693 sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 + GET content -> HTTP/1.1 200 OK x-instance-id: 9bb9a19b4693 sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 + GET content -> HTTP/1.1 200 OK x-instance-id: 9bb9a19b4693 sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 +stopped mtusi-lab4-api-2 + GET content -> HTTP/1.1 200 OK x-instance-id: 90e2e1d73b23 sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 + GET content -> HTTP/1.1 200 OK x-instance-id: 90e2e1d73b23 sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 + GET content -> HTTP/1.1 200 OK x-instance-id: 90e2e1d73b23 sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 +mtusi-lab4-api-1 Up 14 seconds (healthy) +mtusi-lab4-api-2 Up 5 seconds (healthy) diff --git a/evidence/lab4/failover.txt b/evidence/lab4/failover.txt new file mode 100644 index 00000000..0478cc62 --- /dev/null +++ b/evidence/lab4/failover.txt @@ -0,0 +1,13 @@ +# commit 9caa031 +# failover round 1 +{"observed_instances_before": 2, "successful_writes": 133, "failed_attempts": 0, "first_recovery_seconds": 3.184} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. +mtusi-lab4-api-1 Exited (0) 12 seconds ago +mtusi-lab4-api-2 Up 31 seconds (healthy) +# failover round 2 +{"observed_instances_before": 2, "successful_writes": 141, "failed_attempts": 0, "first_recovery_seconds": 3.249} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. +mtusi-lab4-api-1 Exited (0) 12 seconds ago +mtusi-lab4-api-2 Up 53 seconds (healthy) +mtusi-lab4-api-1 Up 5 seconds (healthy) +mtusi-lab4-api-2 Up 58 seconds (healthy) diff --git a/evidence/lab4/make-check.txt b/evidence/lab4/make-check.txt new file mode 100644 index 00000000..9a553306 --- /dev/null +++ b/evidence/lab4/make-check.txt @@ -0,0 +1,2 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/lab4/make-test-dependencies.txt b/evidence/lab4/make-test-dependencies.txt new file mode 100644 index 00000000..54050306 --- /dev/null +++ b/evidence/lab4/make-test-dependencies.txt @@ -0,0 +1,9 @@ +cd app && E2E_COMPOSE_DIR="/home/tomato/Projects/VP/mtusi2026lab4" E2E_COMPOSE_ENV_FILE=".env" npm run test:dependencies +> app@0.0.1 test:dependencies +> E2E_DEPENDENCY_FAULTS=1 vitest run --config ./vitest.config.e2e.ts dependency-failures +The plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the resolve.tsconfigPaths option. You can remove the plugin and set resolve.tsconfigPaths: true in your Vite config instead. + RUN v4.1.11 /home/tomato/Projects/VP/mtusi2026lab4/app + Test Files 1 passed (1) + Tests 7 passed (7) + Start at 17:11:25 + Duration 140.04s (transform 65ms, setup 0ms, import 80ms, tests 139.87s, environment 0ms) diff --git a/evidence/lab4/make-test-e2e.txt b/evidence/lab4/make-test-e2e.txt new file mode 100644 index 00000000..d7c3b7f4 --- /dev/null +++ b/evidence/lab4/make-test-e2e.txt @@ -0,0 +1,8 @@ +> app@0.0.1 test:e2e +> vitest run --config ./vitest.config.e2e.ts +The plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the resolve.tsconfigPaths option. You can remove the plugin and set resolve.tsconfigPaths: true in your Vite config instead. + RUN v4.1.11 /home/tomato/Projects/VP/mtusi2026lab4/app + Test Files 3 passed | 1 skipped (4) + Tests 44 passed | 9 skipped (53) + Start at 17:10:37 + Duration 6.55s (transform 310ms, setup 0ms, import 591ms, tests 12.84s, environment 0ms) diff --git a/evidence/lab4/make-test.txt b/evidence/lab4/make-test.txt new file mode 100644 index 00000000..1e7b9ff4 --- /dev/null +++ b/evidence/lab4/make-test.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab4/persistence.txt b/evidence/lab4/persistence.txt new file mode 100644 index 00000000..c5b981e2 --- /dev/null +++ b/evidence/lab4/persistence.txt @@ -0,0 +1,18 @@ +# commit 9caa031 +# before +/mtusi-lab4-postgres-1 2026-10-23T14:13:29.263262079Z +/mtusi-lab4-redis-1 2026-10-23T14:11:34.042926489Z +/mtusi-lab4-s3-1 2026-10-23T14:12:05.200412031Z +/mtusi-lab4-api-1 2026-10-23T14:11:19.011665615Z +/mtusi-lab4-api-2 2026-10-23T14:11:19.118116521Z +/mtusi-lab4-haproxy-1 2026-10-22T19:41:00.434084255Z +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +# after +/mtusi-lab4-postgres-1 2026-10-23T14:23:26.500579665Z +/mtusi-lab4-redis-1 2026-10-23T14:23:26.233233745Z +/mtusi-lab4-s3-1 2026-10-23T14:23:26.420908434Z +/mtusi-lab4-api-1 2026-10-23T14:23:29.194759152Z +/mtusi-lab4-api-2 2026-10-23T14:23:29.224476065Z +/mtusi-lab4-haproxy-1 2026-10-23T14:23:26.32966444Z diff --git a/evidence/lab4/presign-spike.txt b/evidence/lab4/presign-spike.txt new file mode 100644 index 00000000..1ddb55cb --- /dev/null +++ b/evidence/lab4/presign-spike.txt @@ -0,0 +1,6 @@ +a) X-Amz-SignedHeaders=content-length;content-type;host;x-amz-checksum-sha256 +b) valid PUT -> {"status":200,"text":""} +e) HEAD size=5000 checksumSha256Matches=true +c) same size, one byte changed -> {"status":400,"text":"\nXAmzContentChecksumMismatchThe provided 'x-amz-checksum' header does not match what was computed.attachments/spike-1792697803765-c/originalmessenger-attachments"}; object created=false +d) longer body -> {"status":403,"text":"\nSignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.attachments/spike-1792697803775-d1/originalmessenger-attachments<"}; object created=false +d) shorter body -> {"status":403,"text":"\nSignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.attachments/spike-1792697803780-d2/originalmessenger-attachments<"}; object created=false diff --git a/evidence/lab4/readonly-api.txt b/evidence/lab4/readonly-api.txt new file mode 100644 index 00000000..982ea3b8 --- /dev/null +++ b/evidence/lab4/readonly-api.txt @@ -0,0 +1 @@ +touch: cannot touch '/usr/src/app/x': Read-only file system diff --git a/evidence/lab4/s3-security.txt b/evidence/lab4/s3-security.txt new file mode 100644 index 00000000..be087818 --- /dev/null +++ b/evidence/lab4/s3-security.txt @@ -0,0 +1,10 @@ +scripts/lab4/check_s3_security.sh +PASS anonymous GET of a ready object -> 403 +PASS anonymous list of the bucket -> 403 +PASS anonymous PUT -> 403 +PASS API credentials cannot create buckets +PASS API credentials cannot use admin API +PASS API credentials can read attachments/* +PASS API credentials cannot write outside attachments/ +PASS console port 9001 is not published +PASS console not reachable on localhost:9001 diff --git a/evidence/lab4/summary.md b/evidence/lab4/summary.md new file mode 100644 index 00000000..632d95f2 --- /dev/null +++ b/evidence/lab4/summary.md @@ -0,0 +1,11 @@ +| mode | size, MiB | C | ok | failures | API rx/upload, MiB | API tx/upload, MiB | max Δrss, MiB | peak RSS, MiB | p50, ms | p95, ms | +|---|---|---|---|---|---|---|---|---|---|---| +| direct | 0.12 | 10 | 150/150 | — | 0.13 | 0.01 | 0.38 | 394.39 | 40 | 71 | +| proxy | 0.12 | 10 | 150/150 | — | 0.13 | 0.13 | 0.00 | 446.99 | 26 | 46 | +| direct | 1.00 | 10 | 150/150 | — | 1.01 | 0.01 | 0.00 | 244.16 | 71 | 107 | +| proxy | 1.00 | 10 | 150/150 | — | 1.01 | 1.01 | 0.12 | 394.70 | 50 | 82 | +| direct | 9.50 | 10 | 150/150 | — | 9.52 | 0.01 | 67.06 | 344.15 | 448 | 672 | +| proxy | 9.50 | 10 | 150/150 | — | 9.52 | 9.53 | 20.31 | 272.98 | 245 | 399 | +| proxy | 9.50 | 20 | 300/300 | — | 9.53 | 9.53 | 42.53 | 273.93 | 433 | 781 | +| direct (worst-40mp-rgba16-adam7.png) | 10.00 | 32 | 167/192 | complete:429×25 | 9.24 | 0.01 | 366.88 | 790.94 | 11067 | 13423 | +| proxy (worst-40mp-rgba16-adam7.png) | 10.00 | 32 | 190/192 | upload:429×2 | 10.04 | 9.90 | 356.76 | 784.79 | 11982 | 13068 | diff --git a/evidence/lab4/unit-tests.txt b/evidence/lab4/unit-tests.txt new file mode 100644 index 00000000..348e65c9 --- /dev/null +++ b/evidence/lab4/unit-tests.txt @@ -0,0 +1,6 @@ +[Nest] 4013602 - 10/23/2026, 5:10:35 PM WARN [MessagesCacheService] Cache fill failed: redis down +[Nest] 4013620 - 10/23/2026, 5:10:37 PM WARN [AttachmentsService] Compensation of attachment 2dc5fe05-aac7-484f-935e-186a935e9224 deferred: Connection terminated unexpectedly + Test Files 13 passed (13) + Tests 48 passed (48) + Start at 17:10:34 + Duration 2.68s (transform 652ms, setup 0ms, import 8.56s, tests 1.95s, environment 2ms) diff --git a/evidence/lab5/big-image-memory.txt b/evidence/lab5/big-image-memory.txt new file mode 100644 index 00000000..ea8d1278 --- /dev/null +++ b/evidence/lab5/big-image-memory.txt @@ -0,0 +1,4 @@ +big_image invariants: {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} +final suite run: peak worker memory: 992.2 MiB / 1280 MiB +worker limit: WORKER_MEMORY=1280m, WORKER_CONCURRENCY=2 +input: two concurrent 8000x5000 PNG, 10 MiB each diff --git a/evidence/lab5/broker-identities.txt b/evidence/lab5/broker-identities.txt new file mode 100644 index 00000000..ca83e569 --- /dev/null +++ b/evidence/lab5/broker-identities.txt @@ -0,0 +1,10 @@ +api Up About a minute (healthy) +api Up About a minute (healthy) +broker Up 56 minutes (healthy) +worker Up About a minute (healthy) +user tags +messenger [administrator] +messenger-worker [] +user configure write read +messenger-worker ^(attachments\.jobs|attachments\.process|attachments\.dlx|attachments\.process\.dlq)$ ^attachments\.(jobs|process|dlx|process\.dlq)$ ^attachments\.(jobs|process|dlx|process\.dlq)$ +messenger ^(attachments\.jobs|attachments\.process|attachments\.dlx|attachments\.process\.dlq)$ ^attachments\.(jobs|process|dlx|process\.dlq)$ ^attachments\.(jobs|dlx)$ diff --git a/evidence/lab5/broker-resubscribe.txt b/evidence/lab5/broker-resubscribe.txt new file mode 100644 index 00000000..d8c002b2 --- /dev/null +++ b/evidence/lab5/broker-resubscribe.txt @@ -0,0 +1,2 @@ +consumers after broker restart 1 +upload after restart ready 3 diff --git a/evidence/lab5/build-lint.txt b/evidence/lab5/build-lint.txt new file mode 100644 index 00000000..b5d76a1f --- /dev/null +++ b/evidence/lab5/build-lint.txt @@ -0,0 +1,8 @@ + +> app@0.0.1 build +> nest build + + +> app@0.0.1 lint +> oxlint src/ test/ + diff --git a/evidence/lab5/compose-ps.txt b/evidence/lab5/compose-ps.txt new file mode 100644 index 00000000..8bfb9a17 --- /dev/null +++ b/evidence/lab5/compose-ps.txt @@ -0,0 +1,9 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi-lab5-api-1 mtusi-messenger:lab5 "docker-entrypoint.s…" api 8 minutes ago Up 2 minutes (healthy) 8080/tcp +mtusi-lab5-api-2 mtusi-messenger:lab5 "docker-entrypoint.s…" api 8 minutes ago Up 2 minutes (healthy) 8080/tcp +mtusi-lab5-broker-1 rabbitmq:4.1.8-alpine@sha256:153322e3974e6365c50e523e5eadd5a7fae231f0e8c58f67c03bb3e02056e34e "docker-entrypoint.s…" broker 3 hours ago Up 41 seconds (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp +mtusi-lab5-haproxy-1 haproxy:3.2-alpine "docker-entrypoint.s…" haproxy 3 hours ago Up 3 minutes 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp, 127.0.0.1:8404->8404/tcp +mtusi-lab5-postgres-1 postgres:16.4-alpine "docker-entrypoint.s…" postgres 3 hours ago Up 3 minutes (healthy) 127.0.0.1:5432->5432/tcp +mtusi-lab5-redis-1 redis:7.4.2-alpine "docker-entrypoint.s…" redis 3 hours ago Up 3 minutes (healthy) 127.0.0.1:6379->6379/tcp +mtusi-lab5-s3-1 minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e "/usr/bin/docker-ent…" s3 3 hours ago Up 3 minutes (healthy) 127.0.0.1:9000->9000/tcp +mtusi-lab5-worker-1 mtusi-messenger:lab5 "docker-entrypoint.s…" worker 8 minutes ago Up About a minute (healthy) 8080/tcp diff --git a/evidence/lab5/failover.txt b/evidence/lab5/failover.txt new file mode 100644 index 00000000..a647590e --- /dev/null +++ b/evidence/lab5/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 130, "failed_attempts": 0, "first_recovery_seconds": 3.568} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab5/faults-level4.json b/evidence/lab5/faults-level4.json new file mode 100644 index 00000000..1900550d --- /dev/null +++ b/evidence/lab5/faults-level4.json @@ -0,0 +1,138 @@ +[ + { + "name": "crash_after_commit_before_publish", + "repeat": 1, + "passed": true, + "duration_s": 29.638, + "attachment_id": "9c6d6896-0f76-4d4b-aa37-02b175a40fdc", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "outbox relay published the committed job after API restart" + }, + { + "name": "crash_after_side_effect", + "repeat": 1, + "passed": true, + "duration_s": 82.618, + "attachment_id": "5fb2f6c6-b84d-46b3-ad91-18358faec586", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_side_effect" + }, + { + "name": "crash_after_commit_before_ack", + "repeat": 1, + "passed": true, + "duration_s": 80.048, + "attachment_id": "168fb65f-2965-464a-a197-e62af48b25a8", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_commit_before_ack" + }, + { + "name": "retry_and_replay", + "repeat": 1, + "passed": true, + "duration_s": 84.119, + "attachment_id": "98f19c5e-42f2-4ba5-b74f-3fb1831be1ad", + "job_attempts": [ + 6 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "transient injected fault exhausted retries; replay recovered" + }, + { + "name": "hung_worker", + "repeat": 1, + "passed": true, + "duration_s": 88.126, + "attachment_id": "00a3eef9-5176-4848-b114-124291f68413", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "hard timeout aborted the hung render and retried" + }, + { + "name": "poison", + "repeat": 1, + "passed": true, + "duration_s": 36.723, + "attachment_id": null, + "job_attempts": [], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "malformed messages moved to the DLQ without touching jobs" + }, + { + "name": "big_image", + "repeat": 1, + "passed": true, + "duration_s": 34.708, + "attachment_id": "de5c5405-96d3-40c5-b1c6-6b40f34d7235", + "job_attempts": [], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "peak worker memory: 992.2 MiB / 1280 MiB" + } +] diff --git a/evidence/lab5/faults-level4.txt b/evidence/lab5/faults-level4.txt new file mode 100644 index 00000000..eeabaa00 --- /dev/null +++ b/evidence/lab5/faults-level4.txt @@ -0,0 +1,9 @@ +python3 scripts/lab5/fault_suite.py --base-url "http://localhost:8080" --scenario level4 --output evidence/lab5/faults-level4.json +PASS crash_after_commit_before_publish #1 29.638s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} outbox relay published the committed job after API restart +PASS crash_after_side_effect #1 82.618s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} crashed at worker.after_side_effect +PASS crash_after_commit_before_ack #1 80.048s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} crashed at worker.after_commit_before_ack +PASS retry_and_replay #1 84.119s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} transient injected fault exhausted retries; replay recovered +PASS hung_worker #1 88.126s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} hard timeout aborted the hung render and retried +PASS poison #1 36.723s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} malformed messages moved to the DLQ without touching jobs +PASS big_image #1 34.708s {"I1": true, "I2": true, "I3": true, "I4": true, "I5": true, "I6": true} peak worker memory: 992.2 MiB / 1280 MiB +7/7 scenarios passed diff --git a/evidence/lab5/make-check.txt b/evidence/lab5/make-check.txt new file mode 100644 index 00000000..9a553306 --- /dev/null +++ b/evidence/lab5/make-check.txt @@ -0,0 +1,2 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/lab5/make-test-dependencies.txt b/evidence/lab5/make-test-dependencies.txt new file mode 100644 index 00000000..eb32f88e --- /dev/null +++ b/evidence/lab5/make-test-dependencies.txt @@ -0,0 +1,15 @@ +cd app && E2E_COMPOSE_DIR="/home/tomato/Projects/VP/mtusi2026lab5" E2E_COMPOSE_ENV_FILE=".env" npm run test:dependencies + +> app@0.0.1 test:dependencies +> E2E_DEPENDENCY_FAULTS=1 vitest run --config ./vitest.config.e2e.ts dependency-failures + +The plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the resolve.tsconfigPaths option. You can remove the plugin and set resolve.tsconfigPaths: true in your Vite config instead. + + RUN v4.1.11 /home/tomato/Projects/VP/mtusi2026lab5/app + + + Test Files 1 passed (1) + Tests 7 passed (7) + Start at 03:12:54 + Duration 181.23s (transform 259ms, setup 0ms, import 351ms, tests 180.58s, environment 0ms) + diff --git a/evidence/lab5/make-test-e2e.txt b/evidence/lab5/make-test-e2e.txt new file mode 100644 index 00000000..2aecc7b1 --- /dev/null +++ b/evidence/lab5/make-test-e2e.txt @@ -0,0 +1,15 @@ +scripts/app-e2e.sh + +> app@0.0.1 test:e2e +> vitest run --config ./vitest.config.e2e.ts + +The plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the resolve.tsconfigPaths option. You can remove the plugin and set resolve.tsconfigPaths: true in your Vite config instead. + + RUN v4.1.11 /home/tomato/Projects/VP/mtusi2026lab5/app + + + Test Files 3 passed | 1 skipped (4) + Tests 44 passed | 9 skipped (53) + Start at 03:16:31 + Duration 23.23s (transform 2.53s, setup 0ms, import 5.36s, tests 34.68s, environment 1ms) + diff --git a/evidence/lab5/make-test.txt b/evidence/lab5/make-test.txt new file mode 100644 index 00000000..195a0a51 --- /dev/null +++ b/evidence/lab5/make-test.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab5 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/lab5/persistence.txt b/evidence/lab5/persistence.txt new file mode 100644 index 00000000..933c59c7 --- /dev/null +++ b/evidence/lab5/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab5/queues.txt b/evidence/lab5/queues.txt new file mode 100644 index 00000000..a1f0b2cc --- /dev/null +++ b/evidence/lab5/queues.txt @@ -0,0 +1,3 @@ +{"process":{"messages":0,"consumers":1},"dlq":{"messages":14,"consumers":0}} + +{"attachment":{"id":"b51554bd-cbfc-4e2a-8134-aa9bcc9247c8","status":"ready","error_code":null,"ready_pipeline_version":1,"staging_present":false},"jobs":[{"job_id":"55b4165c-1009-4338-8772-f866fbae2eb9","pipeline_version":1,"status":"succeeded","attempt":1,"max_attempts":5,"last_error_code":null}],"variants":[{"pipeline_version":1,"variant":"thumbnail","sha256":"a18bfc29cd9311807ce56afc7078d45db13b19ac2ac14a31d2b91f2640eddf29","size_bytes":4101,"width":256,"height":160,"object_key":"attachments/b51554bd-cbfc-4e2a-8134-aa9bcc9247c8/v1/thumbnail-a1.jpg","s3":{"exists":true,"etag":"\"1c094bdd7c6cf854d0c4429768fb8c49\"","last_modified":"2026-11-05T00:20:36.000Z"}},{"pipeline_version":1,"variant":"crop","sha256":"06c85b2c7a64a45c5e88d65ef492c3e54bc3a6dd1c4a2fad1dccd71b9aba8113","size_bytes":1657,"width":128,"height":128,"object_key":"attachments/b51554bd-cbfc-4e2a-8134-aa9bcc9247c8/v1/crop-a1.jpg","s3":{"exists":true,"etag":"\"f36ea628d9691b1d7fbad35499d3863f\"","last_modified":"2026-11-05T00:20:36.000Z"}},{"pipeline_version":1,"variant":"optimized","sha256":"0bb5b26524335872b2dfe977e2e364101870bfa4e223f4383ab00c34f6771735","size_bytes":2148,"width":320,"height":200,"object_key":"attachments/b51554bd-cbfc-4e2a-8134-aa9bcc9247c8/v1/optimized-a1.webp","s3":{"exists":true,"etag":"\"03817f0652c967e9a963d9b29a1152c1\"","last_modified":"2026-11-05T00:20:36.000Z"}}],"original":{"exists":true,"etag":"\"c2ab21400d8c13ea847e4d5d3c9832ad\"","last_modified":"2026-11-05T00:20:36.000Z"},"events":[{"job_id":"55b4165c-1009-4338-8772-f866fbae2eb9","event":"created","attempt":1,"detail":{},"created_at":"2026-11-05T00:20:36.743Z"},{"job_id":"55b4165c-1009-4338-8772-f866fbae2eb9","event":"claimed","attempt":1,"detail":{"lease_owner":"c434196d75a8"},"created_at":"2026-11-05T00:20:36.760Z"},{"job_id":"55b4165c-1009-4338-8772-f866fbae2eb9","event":"succeeded","attempt":1,"detail":{},"created_at":"2026-11-05T00:20:36.858Z"},{"job_id":"55b4165c-1009-4338-8772-f866fbae2eb9","event":"staging_deleted","attempt":1,"detail":{"staging_key":"staging/b51554bd-cbfc-4e2a-8134-aa9bcc9247c8/original"},"created_at":"2026-11-05T00:20:36.866Z"}],"outbox":{"pending":0,"published":1}} diff --git a/evidence/lab5/reconciler.txt b/evidence/lab5/reconciler.txt new file mode 100644 index 00000000..dbacdc24 --- /dev/null +++ b/evidence/lab5/reconciler.txt @@ -0,0 +1,3 @@ +attachment 431a2dbd-c2aa-4130-8866-6e1895f154d1 status ready ready_seconds 5.1 +events ['created', 'reconciled', 'claimed', 'succeeded', 'staging_deleted'] +reconciled True diff --git a/evidence/lab5/summary.md b/evidence/lab5/summary.md new file mode 100644 index 00000000..fcc51a4c --- /dev/null +++ b/evidence/lab5/summary.md @@ -0,0 +1,44 @@ +# lab5 — сводка доказательств + +Дата прогона: 2026-11-05. Хост: 8 vCPU i7-11370H, 15 GiB RAM; Docker 29.9.2, +Node 25.7.0, Python 3.14.8. Все команды выполнены из корня репозитория; секреты +и токены не печатались. + +| Критерий | Файл | Наблюдение | +| --- | --- | --- | +| Offline-структура и OpenAPI | `make-check.txt` | `PASS: offline skeleton structure` | +| Публичный smoke (уровень 3) | `make-test.txt` | `PASS: lab5 public API scenario`; 202 → ready → thumbnail 256×160 JPEG | +| Юнит-тесты | `unit-tests.txt` | 18 файлов, 70 тестов, все зелёные; включены fit-inside, backoff, job-message, classifyError, pipelines (EXIF, alpha, determinism) | +| E2E приложения | `make-test-e2e.txt` | 44 passed, 9 skipped (opt-in dependency сценарии) | +| Отказы зависимостей | `make-test-dependencies.txt` | 7/7: Redis, PostgreSQL, per-replica DB path, насыщение пула, S3 stop/pause, staging при PG down | +| Сохранность после рестарта stores | `persistence.txt` | `PASS: persistence preserved confirmed data and existing session` | +| Failover API-реплики | `failover.txt` | 130 успешных записей, 0 ошибок, первое восстановление 3.568 s, 2 инстанса до отказа | +| Демонстрация «worker остановлен» | `worker-down-demo.txt` | 202 queued, content 409, сообщение привязано, очередь пережила restart broker+API, после start worker — ready и thumbnail 256×160 | +| Уровень 4, fault injection | `faults-level4.json`, `faults-level4.txt` | 7/7 сценариев, все I1–I6; crash COMMIT→publish, retry/replay, timeout, DLQ, big image | +| Трек 2, 3 повтора | `track2.json`, `track2.txt` | 21/21 (7 сценариев × 3), все I1–I6; duplicate, reorder, crash до/после side effect, crash после COMMIT до ack, relay crash, stale version | +| Память worker на 40 Мп | `big-image-memory.txt` | 2 × 8000×5000 одновременно; пик 992.2 MiB в финальном прогоне (934–966 MiB в предыдущих) при лимите 1280 MiB; `OOMKilled=false` | +| Восстановление потерянного сообщения | `reconciler.txt` | сообщение удалено из очереди после publish; reconciler переотправил, attachment ready за 5.1 s, событие `reconciled` | +| Resubscribe после рестарта broker | `broker-resubscribe.txt` | после restart broker consumer вернулся, следующий upload дошёл до ready с 3 вариантами | +| Разделение RabbitMQ identities | `broker-identities.txt` | `messenger` и `messenger-worker`; API читает только обменники, worker — очередь; оба ограничены четырьмя ресурсами топологии | +| Состав стенда | `compose-ps.txt` | api ×2 healthy, worker healthy, broker healthy, postgres/redis/s3 healthy | +| Очереди и job после demo | `queues.txt` | process 0/1 consumer, DLQ 12 (накопленные poison-сообщения); job succeeded attempt 1, `staging_present=false` | + +## Параметры fault-стенда + +```sh +FAULT_INJECTION_ENABLED=1 JOB_TIMEOUT_SECONDS=10 JOB_LEASE_SECONDS=8 \ +JOB_RETRY_BASE_MS=500 JOB_RETRY_MAX_MS=4000 make stand-up +``` + +`WORKER_CONCURRENCY=2`, `JOB_MAX_ATTEMPTS=5`, `WORKER_MEMORY=1280m`. +После экспериментов стенд возвращён в штатный режим +(`FAULT_INJECTION_ENABLED=0`, timeout 60 s, lease 30 s) и `make test` повторён. + +## Итог трека 2 + +`track2.json`: 21 запуск, 21 passed. Средние длительности: duplicate 16.7 s, +reorder 36.7 s, crash_before 42.1 s, crash_after 43.9 s, crash_after_ack +33.2 s, relay_crash 42.1 s, stale_version 48.8 s. Ни один инвариант I1–I6 не +нарушен. Повторные публикации и redelivery наблюдались и поглощались +(`duplicate_skipped`), S3 etag вариантов не менялся; v1 после v2 получил +`superseded`, а v2-объекты остались нетронутыми. diff --git a/evidence/lab5/track2.json b/evidence/lab5/track2.json new file mode 100644 index 00000000..d8e6e817 --- /dev/null +++ b/evidence/lab5/track2.json @@ -0,0 +1,425 @@ +[ + { + "name": "duplicate", + "repeat": 1, + "passed": true, + "duration_s": 32.0, + "attachment_id": "5935c541-99c8-4870-ac4c-0b56fa0d1695", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "5 duplicate deliveries were absorbed by the DB fence" + }, + { + "name": "duplicate", + "repeat": 2, + "passed": true, + "duration_s": 29.828, + "attachment_id": "479b209f-da35-4f5f-9f63-e69687219640", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "5 duplicate deliveries were absorbed by the DB fence" + }, + { + "name": "duplicate", + "repeat": 3, + "passed": true, + "duration_s": 31.497, + "attachment_id": "ae5a273e-46b5-47b5-8b95-31bcba873522", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "5 duplicate deliveries were absorbed by the DB fence" + }, + { + "name": "reorder", + "repeat": 1, + "passed": true, + "duration_s": 74.003, + "attachment_id": "e6cb0d4f-d3b8-4cad-be1d-0b082f772d32", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "attempt 99 and a duplicate attempt 1 were acked as skips" + }, + { + "name": "reorder", + "repeat": 2, + "passed": true, + "duration_s": 81.88, + "attachment_id": "b8e17ee2-316c-4d47-852a-eb954438e951", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "attempt 99 and a duplicate attempt 1 were acked as skips" + }, + { + "name": "reorder", + "repeat": 3, + "passed": true, + "duration_s": 76.46, + "attachment_id": "3ecaef19-fca6-468e-ad6c-b6ec89f16fd2", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "attempt 99 and a duplicate attempt 1 were acked as skips" + }, + { + "name": "crash_before_side_effect", + "repeat": 1, + "passed": true, + "duration_s": 84.929, + "attachment_id": "f796e4ef-e4f7-4980-9969-2d7af7f8173f", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.before_side_effect" + }, + { + "name": "crash_before_side_effect", + "repeat": 2, + "passed": true, + "duration_s": 82.228, + "attachment_id": "dff97faf-a729-4c82-ac96-8580aff44606", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.before_side_effect" + }, + { + "name": "crash_before_side_effect", + "repeat": 3, + "passed": true, + "duration_s": 82.816, + "attachment_id": "2f187d8a-3d12-4f9e-af10-b94ad87bca31", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.before_side_effect" + }, + { + "name": "crash_after_side_effect", + "repeat": 1, + "passed": true, + "duration_s": 83.342, + "attachment_id": "20a45ae3-6f74-4547-bb76-f44c0c35fdd5", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_side_effect" + }, + { + "name": "crash_after_side_effect", + "repeat": 2, + "passed": true, + "duration_s": 82.109, + "attachment_id": "42b41254-417d-41a8-b8ea-5836dc4dbcff", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_side_effect" + }, + { + "name": "crash_after_side_effect", + "repeat": 3, + "passed": true, + "duration_s": 81.735, + "attachment_id": "beaad33b-5f44-452c-bf4e-d2577b248cfb", + "job_attempts": [ + 2 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_side_effect" + }, + { + "name": "crash_after_commit_before_ack", + "repeat": 1, + "passed": true, + "duration_s": 74.213, + "attachment_id": "c01dd235-7257-48f4-85c2-d6025d85a09e", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_commit_before_ack" + }, + { + "name": "crash_after_commit_before_ack", + "repeat": 2, + "passed": true, + "duration_s": 70.885, + "attachment_id": "d165ea80-ed1c-4d96-9246-0c637a19b191", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_commit_before_ack" + }, + { + "name": "crash_after_commit_before_ack", + "repeat": 3, + "passed": true, + "duration_s": 74.808, + "attachment_id": "15414a8c-0230-40be-97b4-8bcbee990147", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "crashed at worker.after_commit_before_ack" + }, + { + "name": "relay_crash_after_publish", + "repeat": 1, + "passed": true, + "duration_s": 84.973, + "attachment_id": "cbc4a105-9966-4ff4-906e-f8cafbb4c58b", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "relay republished the same message; duplicate absorbed" + }, + { + "name": "relay_crash_after_publish", + "repeat": 2, + "passed": true, + "duration_s": 83.561, + "attachment_id": "8607c972-a90b-45d4-b5ce-05ba3f201b31", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "relay republished the same message; duplicate absorbed" + }, + { + "name": "relay_crash_after_publish", + "repeat": 3, + "passed": true, + "duration_s": 83.425, + "attachment_id": "7119cbcd-cc68-4b5d-ae94-3ac9b2cd9869", + "job_attempts": [ + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "relay republished the same message; duplicate absorbed" + }, + { + "name": "stale_version", + "repeat": 1, + "passed": true, + "duration_s": 89.723, + "attachment_id": "495ff6af-c20d-485e-9294-fa0fe835d9df", + "job_attempts": [ + 1, + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "v1 finished after v2 and was recorded as superseded" + }, + { + "name": "stale_version", + "repeat": 2, + "passed": true, + "duration_s": 90.277, + "attachment_id": "d1a7eb56-5b9d-4631-b61e-45e642c9c247", + "job_attempts": [ + 1, + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "v1 finished after v2 and was recorded as superseded" + }, + { + "name": "stale_version", + "repeat": 3, + "passed": true, + "duration_s": 86.614, + "attachment_id": "4f3f0abe-86b0-43bb-a49e-8a9dd93015b4", + "job_attempts": [ + 1, + 1 + ], + "events_summary": {}, + "invariants": { + "I1": true, + "I2": true, + "I3": true, + "I4": true, + "I5": true, + "I6": true + }, + "notes": "v1 finished after v2 and was recorded as superseded" + } +] diff --git a/evidence/lab5/track2.txt b/evidence/lab5/track2.txt new file mode 100644 index 00000000..a9096514 --- /dev/null +++ b/evidence/lab5/track2.txt @@ -0,0 +1,22 @@ +track2: 21/21 scenarios passed +PASS duplicate #1 32.000s I1=true I2=true I3=true I4=true I5=true I6=true 5 duplicate deliveries were absorbed by the DB fence +PASS duplicate #2 29.828s I1=true I2=true I3=true I4=true I5=true I6=true 5 duplicate deliveries were absorbed by the DB fence +PASS duplicate #3 31.497s I1=true I2=true I3=true I4=true I5=true I6=true 5 duplicate deliveries were absorbed by the DB fence +PASS reorder #1 74.003s I1=true I2=true I3=true I4=true I5=true I6=true attempt 99 and a duplicate attempt 1 were acked as skips +PASS reorder #2 81.880s I1=true I2=true I3=true I4=true I5=true I6=true attempt 99 and a duplicate attempt 1 were acked as skips +PASS reorder #3 76.460s I1=true I2=true I3=true I4=true I5=true I6=true attempt 99 and a duplicate attempt 1 were acked as skips +PASS crash_before_side_effect #1 84.929s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.before_side_effect +PASS crash_before_side_effect #2 82.228s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.before_side_effect +PASS crash_before_side_effect #3 82.816s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.before_side_effect +PASS crash_after_side_effect #1 83.342s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.after_side_effect +PASS crash_after_side_effect #2 82.109s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.after_side_effect +PASS crash_after_side_effect #3 81.735s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.after_side_effect +PASS crash_after_commit_before_ack #1 74.213s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.after_commit_before_ack +PASS crash_after_commit_before_ack #2 70.885s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.after_commit_before_ack +PASS crash_after_commit_before_ack #3 74.808s I1=true I2=true I3=true I4=true I5=true I6=true crashed at worker.after_commit_before_ack +PASS relay_crash_after_publish #1 84.973s I1=true I2=true I3=true I4=true I5=true I6=true relay republished the same message; duplicate absorbed +PASS relay_crash_after_publish #2 83.561s I1=true I2=true I3=true I4=true I5=true I6=true relay republished the same message; duplicate absorbed +PASS relay_crash_after_publish #3 83.425s I1=true I2=true I3=true I4=true I5=true I6=true relay republished the same message; duplicate absorbed +PASS stale_version #1 89.723s I1=true I2=true I3=true I4=true I5=true I6=true v1 finished after v2 and was recorded as superseded +PASS stale_version #2 90.277s I1=true I2=true I3=true I4=true I5=true I6=true v1 finished after v2 and was recorded as superseded +PASS stale_version #3 86.614s I1=true I2=true I3=true I4=true I5=true I6=true v1 finished after v2 and was recorded as superseded diff --git a/evidence/lab5/unit-tests.txt b/evidence/lab5/unit-tests.txt new file mode 100644 index 00000000..3417df62 --- /dev/null +++ b/evidence/lab5/unit-tests.txt @@ -0,0 +1,17 @@ + +> app@0.0.1 test:unit +> vitest run src/ + +The plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the resolve.tsconfigPaths option. You can remove the plugin and set resolve.tsconfigPaths: true in your Vite config instead. + + RUN v4.1.11 /home/tomato/Projects/VP/mtusi2026lab5/app + +[Nest] 2892631 - 11/05/2026, 3:16:19 AM WARN [MessagesCacheService] Cache read failed, serving PostgreSQL: redis down +[Nest] 2892631 - 11/05/2026, 3:16:19 AM WARN [MessagesCacheService] Cache fill failed: redis down +[Nest] 2893013 - 11/05/2026, 3:16:27 AM WARN [AttachmentsService] Compensation of attachment 7f67f209-53ca-4786-b06e-23c31ac0b59b deferred: Connection terminated unexpectedly + + Test Files 18 passed (18) + Tests 70 passed (70) + Start at 03:16:17 + Duration 11.09s (transform 2.70s, setup 0ms, import 32.94s, tests 8.26s, environment 18ms) + diff --git a/evidence/lab5/worker-down-demo.txt b/evidence/lab5/worker-down-demo.txt new file mode 100644 index 00000000..ef210259 --- /dev/null +++ b/evidence/lab5/worker-down-demo.txt @@ -0,0 +1,36 @@ +python3 scripts/lab5/worker_down_demo.py --base-url "http://localhost:8080" +[1] compose ps +SERVICE STATUS +api Up 41 seconds (healthy) +api Up 2 seconds (health: starting) +broker Up 44 seconds (healthy) +haproxy Up 45 seconds +postgres Up 45 seconds (healthy) +redis Up 45 seconds (healthy) +s3 Up 46 seconds (healthy) +worker Up 45 seconds (healthy) +[2] stop worker +[3] upload while worker is down (expect 202 queued) +attachment=ddc95667-82f9-49ed-a7d9-031ae46fb4f8 status=queued +[4] content and thumbnail must answer 409 before ready +[5] link the queued attachment to messages +message=13caa29b-26bd-49e6-8534-1a078df9a901 +[6] wait for outbox publish and a message in the queue +queue={'messages': 1, 'type': 'quorum'} +[7] restart broker and API with volumes +[8] the quorum message survived the broker restart +queue={'messages': 1, 'type': 'quorum'} +[9] start worker +[10] wait ready and verify the thumbnail +ready attachment=ddc95667-82f9-49ed-a7d9-031ae46fb4f8 thumbnail=256x160 sha256=a18bfc29cd931180... +[11] compose ps +SERVICE STATUS +api Up 43 seconds (healthy) +api Up 43 seconds (healthy) +broker Up 45 seconds (healthy) +haproxy Up About a minute +postgres Up About a minute (healthy) +redis Up About a minute (healthy) +s3 Up About a minute (healthy) +worker Up 2 seconds (health: starting) +PASS: worker-down durability demo diff --git a/infra/README.md b/infra/README.md index 5a39e862..9a1e1586 100644 --- a/infra/README.md +++ b/infra/README.md @@ -2,6 +2,35 @@ Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. -В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. +В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. + +## Роли lab5 + +| Роль | Service | Образ | Готовность | Volume | Credentials | +| --- | --- | --- | --- | --- | --- | +| API | `api` | сборка `mtusi-messenger:lab5` | `/health/ready` (PostgreSQL, Redis, S3) | — | `secrets/s3_api_*`, `secrets/broker_url` | +| Worker | `worker` | та же сборка, `dist/worker.main.js` | `127.0.0.1:8081/health` (consumer активен) | — | `secrets/s3_worker_*`, `secrets/broker_worker_url` | +| Broker | `broker` | `rabbitmq:4.1.8-alpine@sha256:153322e3974e6365c50e523e5eadd5a7fae231f0e8c58f67c03bb3e02056e34e` | `rabbitmq-diagnostics check_port_connectivity` | `broker-data` | `rabbitmq.conf`; `broker-init` — `broker_worker_password` | +| PostgreSQL | `postgres` | `postgres:16.4-alpine` | `pg_isready` | `postgres-data` | пароль в `.env` / `DATABASE_URL` | +| Redis | `redis` | `redis:7.4.2-alpine` | `redis-cli ping` | `redis-data` | без пароля, только compose-сеть | +| Object storage | `s3` | `minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e` | `mc ready local` | `s3-data` | root — `secrets/minio_root_*` | +| S3 bootstrap | `s3-init` | `minio/mc:RELEASE.2025-08-13T08-35-41Z@sha256:a7fe349ef4bd8521fb8497f55c6042871b2ae640607cf99d9bede5e9bdf11727` | завершается 0 | — | root + identities API/worker | +| Broker bootstrap | `broker-init` | `rabbitmq:4.1.8-alpine@sha256:153322e3974e6365c50e523e5eadd5a7fae231f0e8c58f67c03bb3e02056e34e` | завершается 0 | `broker-data` (ro, cookie) | `broker_worker_password` | +| Load balancer | `haproxy` | `haproxy:3.2-alpine` | зависит от `api` healthy | — | — | + +Политики MinIO разделены: API пишет/удаляет только `staging/*`, читает +`attachments/*` и удаляет только legacy `attachments/*/original`; worker +читает/удаляет `staging/*` и читает/пишет `attachments/*` (без удаления). +Обе роли получают `s3:ListBucket`. Пароль брокера генерируется один раз в +`scripts/init-env.sh` и попадает и в `secrets/broker_url` (amqplib), и в +`secrets/rabbitmq.conf` (сам брокер), поэтому URL и конфигурация не расходятся. + +Bootstrap идемпотентен: `scripts/init-env.sh` создаёт `.env` и секреты, `migrate` +применяет SQL-миграции, `s3-init` создаёт bucket/политики/пользователей, а +producer и consumer объявляют обменники и quorum-очереди при каждом подключении. +Одноразовый `broker-init` (образ RabbitMQ, data volume только для cookie) +создаёт `messenger-worker` и сужает права `messenger`: API публикует и объявляет +топологию, но не читает основную очередь, worker потребляет её. Так схема +сходится и на существующем volume, где `default_permissions` уже не применяются. Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику. diff --git a/infra/haproxy/haproxy.cfg b/infra/haproxy/haproxy.cfg new file mode 100644 index 00000000..a31e1f7c --- /dev/null +++ b/infra/haproxy/haproxy.cfg @@ -0,0 +1,61 @@ +# HAProxy — единственная точка входа lab4. +# +# Проверка готовности backend: GET /health/ready — та же ручка, что у +# healthcheck контейнера. Реплика, потерявшая обязательную зависимость (свой +# путь к общей БД или Redis), исключается из ротации, и трафик уходит на +# здоровые; при отказе общей зависимости неготовыми становятся все реплики, и +# клиент получает 503 от балансировщика — это честный признак того, что +# готовых узлов нет. +# Проба с зависимостями ограничена на стороне API: бюджет пробы 400 ms (общий +# дедлайн /health/ready, проверка БД — по отдельному соединению, не из общего +# пула) меньше timeout check 500 ms, который равен интервалу inter. Обнаружение +# отказа (inter 500 ms × fall 2 ≈ 1 s) короче окна дреина API +# (DRAIN_DELAY_MS=3000): узел уходит из ротации раньше, чем перестаёт +# обслуживать. +# +# Реплики не перечислены статически: server-template + DNS Docker обновляет +# список адресов при масштабировании (init-addr none — стартовать без адресов +# и дождаться DNS, а не падать при пустом ответе). + +global + maxconn 8192 + log stdout format raw local0 + +defaults + mode http + option httplog + timeout connect 500ms + timeout client 35s + timeout server 30s + timeout queue 2s + retries 3 + option redispatch + retry-on conn-failure + +resolvers docker + nameserver dns1 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 500ms + hold nx 500ms + hold other 1s + +frontend http + bind *:8080 + default_backend api + +backend api + balance roundrobin + option httpchk GET /health/ready + http-check expect status 200 + timeout check 500ms + default-server check inter 500ms fastinter 250ms downinter 1s fall 2 rise 2 maxconn 200 + server-template api 1-8 api:8080 resolvers docker init-addr none + +listen stats + bind *:8404 + stats enable + stats uri /stats + stats refresh 1s + stats show-legends diff --git a/infra/minio/bootstrap.sh b/infra/minio/bootstrap.sh new file mode 100755 index 00000000..27e50247 --- /dev/null +++ b/infra/minio/bootstrap.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# Идемпотентная инициализация MinIO: приватный bucket, ограниченная политика и +# отдельный пользователь API. Выполняется в контейнере minio/mc. +set -eu + +IFS= read -r root_user < /run/secrets/minio_root_user || [ -n "$root_user" ] +IFS= read -r root_password < /run/secrets/minio_root_password || [ -n "$root_password" ] +IFS= read -r api_key < /run/secrets/s3_api_access_key_id || [ -n "$api_key" ] +IFS= read -r api_secret < /run/secrets/s3_api_secret_access_key || [ -n "$api_secret" ] +IFS= read -r worker_key < /run/secrets/s3_worker_access_key_id || [ -n "$worker_key" ] +IFS= read -r worker_secret < /run/secrets/s3_worker_secret_access_key || [ -n "$worker_secret" ] +bucket="${S3_BUCKET:?}" + +export MC_CONFIG_DIR=/tmp/.mc +export MC_HOST_stand="http://${root_user}:${root_password}@s3:9000" + +mc mb --ignore-existing "stand/$bucket" +mc anonymous set none "stand/$bucket" + +# The API owns the staging prefix only: it writes the accepted upload there and +# can remove it after the worker commits the result. The final prefix is read +# for variant/original downloads and legacy lab4 originals, but never written. +printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/staging/*"]},{"Effect":"Allow","Action":["s3:GetObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*/original"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"],"Condition":{"StringLike":{"s3:prefix":["staging/*","attachments/*"]}}}]}' "$bucket" "$bucket" "$bucket" "$bucket" > /tmp/messenger-api-policy.json + +# The worker reads staging, writes the final original and variants, and removes +# staging after a safe commit. It never deletes the final prefix. +printf '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/staging/*"]},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject"],"Resource":["arn:aws:s3:::%s/attachments/*"]},{"Effect":"Allow","Action":["s3:DeleteObject"],"Resource":["arn:aws:s3:::%s/attachments/*/v*/*"]},{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::%s"],"Condition":{"StringLike":{"s3:prefix":["staging/*","attachments/*"]}}}]}' "$bucket" "$bucket" "$bucket" "$bucket" > /tmp/messenger-worker-policy.json + +ensure_user() { + name=$1 + secret=$2 + if ! mc admin user info stand "$name" >/dev/null 2>&1; then + mc admin user add stand "$name" "$secret" + fi +} + +# `mc admin policy create` does not overwrite and a policy attached to a user +# cannot be removed. Detach, remove and recreate makes the script converge on +# the current policy even when the stand volume predates lab5. +install_policy() { + name=$1 + file=$2 + user=$3 + if mc admin policy info stand "$name" >/dev/null 2>&1; then + mc admin policy detach stand "$name" --user "$user" >/dev/null 2>&1 || true + mc admin policy remove stand "$name" >/dev/null 2>&1 || true + fi + mc admin policy create stand "$name" "$file" + mc admin policy attach stand "$name" --user "$user" +} + +ensure_user "$api_key" "$api_secret" +ensure_user "$worker_key" "$worker_secret" + +install_policy messenger-api /tmp/messenger-api-policy.json "$api_key" +install_policy messenger-worker /tmp/messenger-worker-policy.json "$worker_key" + +echo "bootstrap ok: bucket=$bucket policies=messenger-api,messenger-worker" diff --git a/infra/rabbitmq/init-worker.sh b/infra/rabbitmq/init-worker.sh new file mode 100755 index 00000000..fc50bba4 --- /dev/null +++ b/infra/rabbitmq/init-worker.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Runs as a one-shot sidecar against rabbit@broker. The broker data volume is +# mounted read-only only to share the Erlang cookie; no queue data is modified. +set -eu + +password=$(cat /run/secrets/broker_worker_password) +configure='^(attachments\.jobs|attachments\.process|attachments\.dlx|attachments\.process\.dlq)$' +write='^attachments\.(jobs|process|dlx|process\.dlq)$' +all_read='^attachments\.(jobs|process|dlx|process\.dlq)$' +api_read='^attachments\.(jobs|dlx)$' + +if ! rabbitmqctl -n rabbit@broker -p messenger add_user messenger-worker "$password" 2>/dev/null; then + rabbitmqctl -n rabbit@broker -q -p messenger change_password messenger-worker "$password" +fi +rabbitmqctl -n rabbit@broker -q -p messenger set_permissions messenger-worker \ + "$configure" "$write" "$all_read" +# The API may publish and declare the topology, but must not consume the job +# queue. The worker identity is used by the worker process. +rabbitmqctl -n rabbit@broker -q -p messenger set_permissions messenger \ + "$configure" "$write" "$api_read" +echo "broker init ok: publisher and consumer identities separated" diff --git a/scripts/app-e2e.sh b/scripts/app-e2e.sh new file mode 100755 index 00000000..3931d1c0 --- /dev/null +++ b/scripts/app-e2e.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Black-box набор приложения (app/test/*.e2e-spec.ts) против поднятого стенда. +# Требует loopback-портов PostgreSQL, Redis и MinIO из compose.yaml. +set -eu + +env_file="${COMPOSE_ENV_FILE:-.env}" +compose_dir="$(pwd)" +# shellcheck disable=SC1090 +set -a +. "./$env_file" +set +a + +export API_BASE_URL="${API_BASE_URL:-http://127.0.0.1:${APP_PUBLIC_PORT:-8080}}" +export DATABASE_URL="postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in $env_file}@127.0.0.1:${POSTGRES_PUBLISHED_PORT:-5432}/${POSTGRES_DB:-messenger}" +export REDIS_URL="redis://127.0.0.1:${REDIS_PUBLISHED_PORT:-6379}" +export E2E_S3_PUBLIC_ENDPOINT="${S3_PUBLIC_ENDPOINT:-http://localhost:9000}" +export E2E_S3_BUCKET="${S3_BUCKET:-messenger-attachments}" +export E2E_COMPOSE_DIR="$compose_dir" +export E2E_COMPOSE_ENV_FILE="$env_file" + +cd "$(dirname "$0")/../app" +exec npm run test:e2e -- "$@" diff --git a/scripts/init-env.sh b/scripts/init-env.sh new file mode 100755 index 00000000..4d340de2 --- /dev/null +++ b/scripts/init-env.sh @@ -0,0 +1,76 @@ +#!/bin/sh +# Идемпотентно создаёт .env и secrets/* для локального стенда. Значения не печатает. +set -eu +cd "$(dirname "$0")/.." + +if [ ! -f .env ]; then + ( + umask 077 + pg=$(openssl rand -hex 24) + mt=$(openssl rand -hex 16) + sed \ + -e "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${pg}|" \ + -e "s|^DATABASE_URL=.*|DATABASE_URL=postgresql://messenger:${pg}@postgres:5432/messenger|" \ + -e "s|^METRICS_TOKEN=.*|METRICS_TOKEN=${mt}|" \ + .env.example > .env + ) + echo "created .env" +else + echo "kept .env" +fi + +mkdir -p secrets +chmod 700 secrets + +write_secret() { + name=$1 + value=$2 + if [ -s "secrets/$name" ]; then + echo "kept secrets/$name" + return + fi + (umask 133; printf '%s' "$value" > "secrets/$name") + chmod 0644 "secrets/$name" + echo "created secrets/$name" +} + +write_secret minio_root_user "root-$(openssl rand -hex 6)" +write_secret minio_root_password "$(openssl rand -hex 24)" +write_secret s3_api_access_key_id "api-$(openssl rand -hex 8)" +write_secret s3_api_secret_access_key "$(openssl rand -hex 24)" +write_secret s3_worker_access_key_id "worker-$(openssl rand -hex 8)" +write_secret s3_worker_secret_access_key "$(openssl rand -hex 24)" + +if [ -s secrets/broker_worker_password ] && [ -s secrets/broker_worker_url ]; then + echo "kept secrets/broker_worker_password secrets/broker_worker_url" +else + wp=$(openssl rand -hex 24) + (umask 133; printf '%s' "$wp" > secrets/broker_worker_password) + chmod 0644 secrets/broker_worker_password + (umask 133; printf '%s' "amqp://messenger-worker:${wp}@broker:5672/messenger" > secrets/broker_worker_url) + chmod 0644 secrets/broker_worker_url + echo "created secrets/broker_worker_password secrets/broker_worker_url" +fi + +# Broker password lives in one place and is rendered into both files, so the +# URL used by the app and the RabbitMQ config cannot drift apart. +if [ -s secrets/broker_url ] && [ -s secrets/rabbitmq.conf ]; then + echo "kept secrets/broker_url secrets/rabbitmq.conf" +else + bp=$(openssl rand -hex 24) + (umask 133; printf '%s' "amqp://messenger:${bp}@broker:5672/messenger" > secrets/broker_url) + chmod 0644 secrets/broker_url + ( + umask 133 + { + printf 'default_vhost = messenger\n' + printf 'default_user = messenger\n' + printf 'default_pass = %s\n' "$bp" + printf 'default_permissions.configure = ^(attachments\\.jobs|attachments\\.process|attachments\\.dlx|attachments\\.process\\.dlq)$\n' + printf 'default_permissions.read = ^attachments\\.(jobs|process|dlx|process\\.dlq)$\n' + printf 'default_permissions.write = ^attachments\\.(jobs|process|dlx|process\\.dlq)$\n' + } > secrets/rabbitmq.conf + ) + chmod 0644 secrets/rabbitmq.conf + echo "created secrets/broker_url secrets/rabbitmq.conf" +fi diff --git a/scripts/lab4/attachments_bench.py b/scripts/lab4/attachments_bench.py new file mode 100755 index 00000000..9e5151d8 --- /dev/null +++ b/scripts/lab4/attachments_bench.py @@ -0,0 +1,340 @@ +#!/usr/bin/env python3 +"""Upload benchmark: API traffic and peak RSS per upload for proxy vs direct mode. + + attachments_bench.py run --mode proxy --size-bytes 1048576 --concurrency 10 \ + --uploads-per-worker 5 --repeat 3 --output evidence/lab4/bench/proxy-1048576-c10.json + attachments_bench.py summarize evidence/lab4/bench/*.json > evidence/lab4/summary.md + +Run from the repository root with the stand up. Tokens, signed URLs and secrets +are never written to the output. +""" +import argparse +import base64 +import hashlib +import http.client +import json +import os +import statistics +import subprocess +import sys +import threading +import time +import urllib.parse +import uuid +from datetime import datetime, timezone + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from pngkit import noise_png # noqa: E402 + + +def sh(*args, check=True): + return subprocess.run(args, check=check, capture_output=True, text=True).stdout.strip() + + +def read_env(path=".env"): + values = {} + with open(path) as handle: + for line in handle: + line = line.strip() + if line and not line.startswith("#") and "=" in line: + key, _, value = line.partition("=") + values[key] = value + return values + + +def request(host, port, method, path, headers=None, body=None): + connection = http.client.HTTPConnection(host, port, timeout=120) + try: + connection.request(method, path, body=body, headers=headers or {}) + response = connection.getresponse() + payload = response.read() + return response.status, payload + finally: + connection.close() + + +def api_json(env, method, path, token=None, body=None, basic=None): + headers = {} + data = None + if body is not None: + headers["content-type"] = "application/json" + data = json.dumps(body).encode() + if token: + headers["authorization"] = f"Bearer {token}" + if basic: + headers["authorization"] = "Basic " + base64.b64encode(basic.encode()).decode() + port = int(env.get("APP_PUBLIC_PORT", "8080")) + status, payload = request("127.0.0.1", port, method, path, headers, data) + return status, json.loads(payload) if payload else None + + +def containers(): + ids = sh("docker", "compose", "--env-file", ".env", "ps", "-q", "api").split() + return sorted(ids) + + +NODE_METRICS = ( + "fetch('http://127.0.0.1:'+(process.env.APP_PORT||8080)+'/internal/metrics'+process.argv[1]," + "{method:process.argv[1]?'POST':'GET',headers:{authorization:'Bearer '+process.env.METRICS_TOKEN}})" + ".then(r=>r.text()).then(console.log)" +) + + +def metrics(container): + return json.loads(sh("docker", "exec", container, "node", "-e", NODE_METRICS, "")) + + +def reset_metrics(container): + sh("docker", "exec", container, "node", "-e", NODE_METRICS, "/reset") + + +def net_bytes(container): + for line in sh("docker", "exec", container, "cat", "/proc/net/dev").splitlines(): + if line.strip().startswith("eth0:"): + fields = line.split(":", 1)[1].split() + return int(fields[0]), int(fields[8]) + raise RuntimeError("eth0 not found") + + +def percentile(values, fraction): + ordered = sorted(values) + if not ordered: + return 0.0 + return ordered[min(len(ordered) - 1, max(0, int(round(fraction * len(ordered) + 0.5)) - 1))] + + +def one_upload(env, mode, token, chat_id, data, seed): + """Returns (ok, elapsed_ms, outcome); outcome names the failing step and status, e.g. "complete:429".""" + digest = hashlib.sha256(data).hexdigest() + filename = f"bench-{seed}.png" + port = int(env.get("APP_PUBLIC_PORT", "8080")) + started = time.perf_counter() + outcome, attachment = "ok", None + try: + if mode == "proxy": + status, body = request( + "127.0.0.1", port, "POST", + f"/api/v1/chats/{chat_id}/attachments?filename={urllib.parse.quote(filename)}", + {"authorization": f"Bearer {token}", "content-type": "image/png"}, data, + ) + if status == 201: + attachment = json.loads(body) + else: + outcome = f"upload:{status}" + else: + headers = {"authorization": f"Bearer {token}", "content-type": "application/json"} + status, body = request( + "127.0.0.1", port, "POST", f"/api/v1/chats/{chat_id}/attachments/direct-uploads", headers, + json.dumps({"filename": filename, "content_type": "image/png", + "size_bytes": len(data), "sha256": digest}).encode(), + ) + if status != 201: + outcome = f"init:{status}" + else: + ticket = json.loads(body) + target = urllib.parse.urlsplit(ticket["url"]) + put_status, _ = request( + target.hostname, target.port, "PUT", target.path + "?" + target.query, + {**ticket["headers"]}, data, + ) + if put_status != 200: + outcome = f"put:{put_status}" + else: + status, body = request( + "127.0.0.1", port, "POST", f"/api/v1/attachments/{ticket['attachment_id']}/complete", + {"authorization": f"Bearer {token}"}, + ) + if status == 200: + attachment = json.loads(body) + else: + outcome = f"complete:{status}" + except (OSError, http.client.HTTPException, ValueError) as error: + outcome = f"error:{type(error).__name__}" + elapsed_ms = (time.perf_counter() - started) * 1000 + if outcome == "ok" and (attachment is None or attachment["sha256"] != digest): + outcome = "checksum_mismatch" + return outcome == "ok", elapsed_ms, outcome + + +def payload(args, seed): + if args.image: + with open(args.image, "rb") as handle: + return handle.read() + return noise_png(args.size_bytes, seed) + + +def run_once(env, args, actors, ids): + for container in ids: + reset_metrics(container) + time.sleep(0.2) + baseline = {c: metrics(c) for c in ids} + net_before = {c: net_bytes(c) for c in ids} + latencies, failures = [], [] + outcomes = {} + lock = threading.Lock() + + def worker(index): + for step in range(args.uploads_per_worker): + seed = index * 1000 + step + data = payload(args, seed) + token, chat_id = actors[index % len(actors)] + ok, elapsed, outcome = one_upload(env, args.mode, token, chat_id, data, seed) + with lock: + (latencies if ok else failures).append(elapsed) + outcomes[outcome] = outcomes.get(outcome, 0) + 1 + + started = time.perf_counter() + threads = [threading.Thread(target=worker, args=(i,)) for i in range(args.concurrency)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + duration = time.perf_counter() - started + time.sleep(0.2) + after = {c: metrics(c) for c in ids} + net_after = {c: net_bytes(c) for c in ids} + + replicas = [] + for c in ids: + base_rss = baseline[c]["memory_bytes"]["rss"] + peak = after[c]["memory_peak_bytes"] + replicas.append({ + "instance_id": after[c]["instance_id"], + "delta_rx_bytes": net_after[c][0] - net_before[c][0], + "delta_tx_bytes": net_after[c][1] - net_before[c][1], + "baseline_rss_bytes": base_rss, + "peak_memory_bytes": peak, + "delta_rss_bytes": peak["rss"] - base_rss, + "counters": {k: v for k, v in after[c]["counters"].items() if k.startswith("attachments.")}, + }) + total = args.concurrency * args.uploads_per_worker + return { + "uploads": total, + "succeeded": len(latencies), + "failed": len(failures), + "outcomes": outcomes, + "duration_s": round(duration, 3), + "latency_ms": { + "p50": round(percentile(latencies, 0.50), 1), + "p95": round(percentile(latencies, 0.95), 1), + "p99": round(percentile(latencies, 0.99), 1), + }, + "replicas": replicas, + "total_delta_rx_bytes": sum(r["delta_rx_bytes"] for r in replicas), + "total_delta_tx_bytes": sum(r["delta_tx_bytes"] for r in replicas), + "rx_bytes_per_upload": round(sum(r["delta_rx_bytes"] for r in replicas) / total), + "tx_bytes_per_upload": round(sum(r["delta_tx_bytes"] for r in replicas) / total), + "max_delta_rss_bytes": max(r["delta_rss_bytes"] for r in replicas), + } + + +def command_run(args): + if args.image: + args.size_bytes = os.path.getsize(args.image) + if args.size_bytes <= 0: + sys.exit("--size-bytes or --image is required") + env = read_env() + ids = containers() + if not ids: + sys.exit("no api containers found") + # One user per worker: the API caps concurrent uploads per user + # (UPLOAD_MAX_IN_FLIGHT_PER_USER), and real traffic comes from many users. + actors = [] + for _ in range(args.concurrency): + name = f"bench_{uuid.uuid4().hex[:10]}" + password = "bench:" + uuid.uuid4().hex + status, user = api_json(env, "POST", "/api/v1/users", + body={"username": name, "display_name": "bench", "password": password}) + assert status == 201, status + status, session = api_json(env, "POST", "/api/v1/auth/sessions", basic=f"{name}:{password}") + assert status == 201, status + token = session["token"] + status, chat = api_json(env, "POST", "/api/v1/chats", token, {"title": "bench", "member_ids": [user["id"]]}) + assert status == 201, status + actors.append((token, chat["id"])) + + # Warm-up: two uploads per replica, not measured. + for seed in range(len(ids) * 2): + token, chat_id = actors[seed % len(actors)] + one_upload(env, args.mode, token, chat_id, payload(args, 900000 + seed), seed) + + limits = [] + for c in ids: + limits.append(sh("docker", "inspect", "-f", "{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}}", c)) + repeats = [run_once(env, args, actors, ids) for _ in range(args.repeat)] + mem_total = next(int(l.split()[1]) * 1024 for l in open("/proc/meminfo") if l.startswith("MemTotal")) + result = { + "date": datetime.now(timezone.utc).isoformat(timespec="seconds"), + "git": sh("git", "rev-parse", "--short", "HEAD", check=False), + "params": { + "mode": args.mode, "size_bytes": args.size_bytes, "concurrency": args.concurrency, + "uploads_per_worker": args.uploads_per_worker, "repeat": args.repeat, + "users": args.concurrency, "image": os.path.basename(args.image) if args.image else None, + }, + "host": {"nproc": os.cpu_count(), "mem_total_bytes": mem_total}, + "api_container_limits_nanocpus_memory": limits, + "repeats": repeats, + } + os.makedirs(os.path.dirname(os.path.abspath(args.output)), exist_ok=True) + with open(args.output, "w") as handle: + json.dump(result, handle, indent=2) + handle.write("\n") + for index, item in enumerate(repeats): + print(f"repeat {index + 1}: ok={item['succeeded']}/{item['uploads']} " + f"rx/upload={item['rx_bytes_per_upload']} tx/upload={item['tx_bytes_per_upload']} " + f"maxΔrss={item['max_delta_rss_bytes']} p50={item['latency_ms']['p50']}ms " + f"outcomes={item['outcomes']}") + + +def mib(value): + return f"{value / 1048576:.2f}" + + +def command_summarize(args): + rows = [] + for path in args.paths: + with open(path) as handle: + data = json.load(handle) + p = data["params"] + reps = data["repeats"] + med = lambda key: statistics.median(r[key] for r in reps) # noqa: E731 + label = p["mode"] + (f" ({p['image']})" if p.get("image") else "") + rows.append((label, p["size_bytes"], p["concurrency"], reps, med, data)) + rows.sort(key=lambda r: (r[0].count("("), r[1], r[0], r[2])) + print("| mode | size, MiB | C | ok | failures | API rx/upload, MiB | API tx/upload, MiB | max Δrss, MiB | peak RSS, MiB | p50, ms | p95, ms |") + print("|---|---|---|---|---|---|---|---|---|---|---|") + for mode, size, conc, reps, med, _data in rows: + peak = max(x["peak_memory_bytes"]["rss"] for r in reps for x in r["replicas"]) + failed = {} + for rep in reps: + for name, count in rep.get("outcomes", {}).items(): + if name != "ok": + failed[name] = failed.get(name, 0) + count + failures = ", ".join(f"{name}×{count}" for name, count in sorted(failed.items())) or "—" + ok = sum(r["succeeded"] for r in reps) + total = sum(r["uploads"] for r in reps) + p50 = statistics.median(r["latency_ms"]["p50"] for r in reps) + p95 = statistics.median(r["latency_ms"]["p95"] for r in reps) + print(f"| {mode} | {mib(size)} | {conc} | {ok}/{total} | {failures} | {mib(med('rx_bytes_per_upload'))} | " + f"{mib(med('tx_bytes_per_upload'))} | {mib(med('max_delta_rss_bytes'))} | {mib(peak)} | {p50:.0f} | {p95:.0f} |") + + +def main(): + parser = argparse.ArgumentParser() + sub = parser.add_subparsers(dest="command", required=True) + run = sub.add_parser("run") + run.add_argument("--mode", choices=["proxy", "direct"], required=True) + run.add_argument("--size-bytes", type=int, default=0) + run.add_argument("--image", help="upload this file instead of generated noise PNGs") + run.add_argument("--concurrency", type=int, default=10) + run.add_argument("--uploads-per-worker", type=int, default=5) + run.add_argument("--repeat", type=int, default=3) + run.add_argument("--output", required=True) + summarize = sub.add_parser("summarize") + summarize.add_argument("paths", nargs="+") + args = parser.parse_args() + {"run": command_run, "summarize": command_summarize}[args.command](args) + + +if __name__ == "__main__": + main() diff --git a/scripts/lab4/check_s3_security.sh b/scripts/lab4/check_s3_security.sh new file mode 100755 index 00000000..497c5ba1 --- /dev/null +++ b/scripts/lab4/check_s3_security.sh @@ -0,0 +1,54 @@ +#!/bin/sh +# Проверка приватности S3 вне API: анонимный доступ, права отдельных credentials +# API, закрытая консоль. Секреты не печатаются. +set -u +cd "$(dirname "$0")/../.." +env_file="${COMPOSE_ENV_FILE:-.env}" +# shellcheck disable=SC1090 +set -a; . "./$env_file"; set +a +endpoint="${S3_PUBLIC_ENDPOINT:-http://localhost:9000}" +bucket="${S3_BUCKET:-messenger-attachments}" +compose="docker compose --env-file $env_file" +fail=0 + +report() { # status name + if [ "$1" = ok ]; then echo "PASS $2"; else echo "FAIL $2"; fail=1; fi +} + +key=$($compose exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -At \ + -c "SELECT object_key FROM attachments WHERE status = 'ready' ORDER BY created_at DESC LIMIT 1") +if [ -z "$key" ]; then + echo "FAIL no ready attachment to check; upload one first (make test)" + exit 1 +fi + +code=$(curl -s -o /dev/null -w '%{http_code}' "$endpoint/$bucket/$key") +[ "$code" = 403 ] && report ok "anonymous GET of a ready object -> 403" || report fail "anonymous GET of a ready object -> $code (want 403)" + +code=$(curl -s -o /dev/null -w '%{http_code}' "$endpoint/$bucket/") +[ "$code" = 403 ] && report ok "anonymous list of the bucket -> 403" || report fail "anonymous list of the bucket -> $code (want 403)" + +code=$(curl -s -o /dev/null -w '%{http_code}' -X PUT --data-binary x "$endpoint/$bucket/attachments/anonymous-write") +[ "$code" = 403 ] && report ok "anonymous PUT -> 403" || report fail "anonymous PUT -> $code (want 403)" + +# The API credentials only, inside a one-off container (secrets come from files). +api_mc() { + $compose run --rm --no-deps -T --entrypoint /bin/sh s3-init -c ' + IFS= read -r k < /run/secrets/s3_api_access_key_id + IFS= read -r s < /run/secrets/s3_api_secret_access_key + export MC_CONFIG_DIR=/tmp/.mc MC_HOST_api="http://$k:$s@s3:9000" + exec mc "$@"' sh "$@" >/dev/null 2>&1 +} + +api_mc mb api/forbidden-bucket && report fail "API credentials can create buckets" || report ok "API credentials cannot create buckets" +api_mc admin info api && report fail "API credentials can use admin API" || report ok "API credentials cannot use admin API" +api_mc stat "api/$bucket/$key" && report ok "API credentials can read attachments/*" || report fail "API credentials cannot read attachments/*" +api_mc stat "api/$bucket/outside-prefix" ; rc=$? +api_mc cp /etc/hostname "api/$bucket/outside-prefix" && report fail "API credentials can write outside attachments/" || report ok "API credentials cannot write outside attachments/" +: "$rc" + +port=$($compose port s3 9001 2>/dev/null || true) +[ -z "$port" ] && report ok "console port 9001 is not published" || report fail "console port 9001 is published ($port)" +if curl -s -o /dev/null --max-time 3 http://localhost:9001; then report fail "console reachable on localhost:9001"; else report ok "console not reachable on localhost:9001"; fi + +exit "$fail" diff --git a/scripts/lab4/consistency_check.sh b/scripts/lab4/consistency_check.sh new file mode 100755 index 00000000..fd97eb7e --- /dev/null +++ b/scripts/lab4/consistency_check.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Сверка bucket и БД (только чтение): orphans — объект без строки, missing — +# ready-строка без объекта. Код выхода 1, если что-то найдено. +set -eu +cd "$(dirname "$0")/../.." +env_file="${COMPOSE_ENV_FILE:-.env}" +# shellcheck disable=SC1090 +set -a; . "./$env_file"; set +a +bucket="${S3_BUCKET:-messenger-attachments}" +compose="docker compose --env-file $env_file" + +objects=$(mktemp) +rows=$(mktemp) +trap 'rm -f "$objects" "$rows"' EXIT + +$compose run --rm --no-deps -T --entrypoint /bin/sh s3-init -c ' + IFS= read -r u < /run/secrets/minio_root_user + IFS= read -r p < /run/secrets/minio_root_password + export MC_CONFIG_DIR=/tmp/.mc MC_HOST_stand="http://$u:$p@s3:9000" + exec mc ls --recursive "stand/$S3_BUCKET"' 2>/dev/null \ + | sed -n 's/.* \(attachments\/.*\)$/\1/p' | sort > "$objects" +$compose exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -At \ + -c "SELECT object_key, status FROM attachments" | sort > "$rows" + +echo "bucket: $bucket" +echo "objects: $(wc -l < "$objects" | tr -d ' ')" +echo "rows by status:" +cut -d'|' -f2 "$rows" | sort | uniq -c | sed 's/^ */ /' +orphans=$(cut -d'|' -f1 "$rows" | sort | comm -13 - "$objects") +missing=$(grep '|ready$' "$rows" | cut -d'|' -f1 | sort | comm -23 - "$objects") +echo "orphans: $(printf '%s' "$orphans" | grep -c . || true)" +[ -z "$orphans" ] || printf '%s\n' "$orphans" | sed 's/^/ orphan /' +echo "missing: $(printf '%s' "$missing" | grep -c . || true)" +[ -z "$missing" ] || printf '%s\n' "$missing" | sed 's/^/ missing /' +[ -z "$orphans$missing" ] diff --git a/scripts/lab4/decode_memory.mjs b/scripts/lab4/decode_memory.mjs new file mode 100644 index 00000000..65bd84b4 --- /dev/null +++ b/scripts/lab4/decode_memory.mjs @@ -0,0 +1,33 @@ +// 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.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); + }, 5); + const started = Date.now(); + 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.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}`, + ); +} diff --git a/scripts/lab4/pngkit.py b/scripts/lab4/pngkit.py new file mode 100644 index 00000000..1ec0b8d5 --- /dev/null +++ b/scripts/lab4/pngkit.py @@ -0,0 +1,78 @@ +"""Synthetic PNG helpers (stdlib only); same semantics as app/test/support/png.ts.""" +import random +import struct +import zlib + +SIGNATURE = b"\x89PNG\r\n\x1a\n" +NOISE_WIDTH = 1024 +NOISE_ROW = 1 + NOISE_WIDTH * 3 + + +def chunk(kind: bytes, data: bytes) -> bytes: + crc = zlib.crc32(kind + data) & 0xFFFFFFFF + return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", crc) + + +def build_png(width: int, height: int, idat: bytes) -> bytes: + ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) + return SIGNATURE + chunk(b"IHDR", ihdr) + chunk(b"IDAT", idat) + chunk(b"IEND", b"") + + +def pad_png(data: bytes, size: int) -> bytes: + """Insert a private ancillary chunk before IEND to reach exactly `size` bytes.""" + extra = size - len(data) + if extra == 0: + return data + if extra < 12: + raise ValueError("padding needs at least 12 bytes") + return data[:-12] + chunk(b"prVt", bytes(extra - 12)) + data[-12:] + + +def _noise_of_height(height: int, rng: random.Random) -> bytes: + raw = bytearray(rng.randbytes(NOISE_ROW * height)) + for row in range(height): + raw[row * NOISE_ROW] = 0 # filter type "none" + compressor = zlib.compressobj(0) + return build_png(NOISE_WIDTH, height, compressor.compress(bytes(raw)) + compressor.flush()) + + +def noise_png(size: int, seed: int = 0) -> bytes: + """Incompressible, structurally valid PNG of exactly `size` bytes.""" + rng = random.Random(seed) + height = max(1, size // NOISE_ROW) + while True: + png = _noise_of_height(height, rng) + if len(png) > size or 0 < size - len(png) < 12: + height -= 1 + if height < 1: + raise ValueError("size is too small") + continue + return pad_png(png, size) + + +ADAM7 = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)] + + +def worst_decode_png(size: int = 10_485_760, width: int = 8000, height: int = 5000) -> bytes: + """The heaviest valid upload to decode: 40 MP RGBA 16-bit, Adam7-interlaced + (the decoder must hold the whole image), padded to exactly `size` bytes.""" + compressor = zlib.compressobj(1) + parts = [] + for x0, y0, dx, dy in ADAM7: + pass_width = (width - x0 + dx - 1) // dx if width > x0 else 0 + pass_height = (height - y0 + dy - 1) // dy if height > y0 else 0 + if pass_width == 0 or pass_height == 0: + continue + row = bytes(1 + pass_width * 8) + for _ in range(pass_height): + parts.append(compressor.compress(row)) + parts.append(compressor.flush()) + ihdr = struct.pack(">IIBBBBB", width, height, 16, 6, 0, 0, 1) + png = SIGNATURE + chunk(b"IHDR", ihdr) + chunk(b"IDAT", b"".join(parts)) + chunk(b"IEND", b"") + return pad_png(png, size) + + +if __name__ == "__main__": + import sys + + sys.stdout.buffer.write(worst_decode_png()) diff --git a/scripts/lab4/run_matrix.sh b/scripts/lab4/run_matrix.sh new file mode 100755 index 00000000..2c6578cf --- /dev/null +++ b/scripts/lab4/run_matrix.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# Experiment matrix: sizes x {proxy,direct} at C=10, plus proxy 9.5 MiB at C=20. +# Needs a running stand (make stand-up) and `make init-env` credentials. +set -eu +cd "$(dirname "$0")/../.." +out=evidence/lab4/bench +mkdir -p "$out" +bench() { + mode=$1 size=$2 conc=$3 + python3 scripts/lab4/attachments_bench.py run --mode "$mode" --size-bytes "$size" \ + --concurrency "$conc" --uploads-per-worker 5 --repeat 3 \ + --output "$out/$mode-$size-c$conc.json" +} +for size in 131072 1048576 9961472; do + for mode in proxy direct; do + echo "== $mode size=$size c=10" + bench "$mode" "$size" 10 + done +done +echo "== proxy size=9961472 c=20" +bench proxy 9961472 20 +# Worst case for memory: every request carries the heaviest valid image +# (40 MP RGBA16 Adam7 PNG padded to 10 MiB, ~340 MiB per decode) and C=32 +# saturates UPLOAD_MAX_IN_FLIGHT (16) on both replicas. +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +worst="$tmp/worst-40mp-rgba16-adam7.png" +python3 scripts/lab4/pngkit.py > "$worst" +for mode in proxy direct; do + echo "== $mode worst-case image c=32" + python3 scripts/lab4/attachments_bench.py run --mode "$mode" --image "$worst" \ + --concurrency 32 --uploads-per-worker 2 --repeat 3 \ + --output "$out/$mode-worst-c32.json" +done +python3 scripts/lab4/attachments_bench.py summarize "$out"/*.json > evidence/lab4/summary.md +cat evidence/lab4/summary.md diff --git a/scripts/lab5/fault_suite.py b/scripts/lab5/fault_suite.py new file mode 100755 index 00000000..ae021456 --- /dev/null +++ b/scripts/lab5/fault_suite.py @@ -0,0 +1,749 @@ +#!/usr/bin/env python3 +"""Automated fault injection for lab5 levels 4 and 5 (track 2).""" +import argparse +import json +import re +import sys +import threading +import time +import uuid +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'lab4')) + +from stand import ( # noqa: E402 + ROOT, + Failure, + check, + clear_fault, + clear_faults, + cli, + compose, + docker, + new_client, + rabbit_queues, + sample_bytes, + set_fault, + sha256, + upload, + variant_sha, + wait_for, + wait_ready, + worker_env, + worker_start_count, +) +from pngkit import worst_decode_png # noqa: E402 + +HINT = ('FAULT_INJECTION_ENABLED=1 JOB_TIMEOUT_SECONDS=10 ' + 'JOB_LEASE_SECONDS=8 JOB_RETRY_BASE_MS=500 JOB_RETRY_MAX_MS=4000 ' + 'make stand-up') + +LEVEL4 = [ + 'crash_after_commit_before_publish', + 'crash_after_side_effect', + 'crash_after_commit_before_ack', + 'retry_and_replay', + 'hung_worker', + 'poison', + 'big_image', +] +TRACK2 = [ + 'duplicate', + 'reorder', + 'crash_before_side_effect', + 'crash_after_side_effect', + 'crash_after_commit_before_ack', + 'relay_crash_after_publish', + 'stale_version', +] + + +class Tracker(threading.Thread): + """Polls metadata while a scenario runs to prove readiness never regresses.""" + + def __init__(self, base_url, actor, ca_file=None): + super().__init__(daemon=True) + self.client = new_client(base_url, ca_file) + self.actor = actor + self.ids = [] + self.records = {} + self.stop_event = threading.Event() + + def track(self, attachment_id): + if attachment_id not in self.records: + self.ids.append(attachment_id) + self.records[attachment_id] = [] + + def run(self): + while not self.stop_event.is_set(): + for attachment_id in list(self.ids): + try: + meta = self.client.call( + 'GET', '/api/v1/attachments/' + attachment_id, + actor=self.actor, schema='Attachment', + )['json'] + self.records[attachment_id].append( + (time.monotonic(), meta['status'], + meta.get('ready_pipeline_version')) + ) + except Exception: + pass + self.stop_event.wait(0.2) + + def stop(self): + self.stop_event.set() + self.join(timeout=5) + + +def build_message(status, job, attempt=None): + created = next( + event for event in status['events'] + if event['job_id'] == job['job_id'] and event['event'] == 'created' + ) + return { + 'schema_version': 1, + 'job_id': job['job_id'], + 'type': 'attachment.process.v1', + 'attachment_id': status['attachment']['id'], + 'pipeline_version': job['pipeline_version'], + 'attempt': attempt if attempt is not None else job['attempt'], + 'created_at': created['created_at'], + } + + +def publish_raw(messages): + text = ''.join(json.dumps(message, separators=(',', ':')) + '\n' + for message in messages) + code, payload = cli('publish-raw', stdin=text) + check(code == 0, 'publish-raw failed: ' + str(payload)) + return payload + + +def status_of(attachment_id): + code, payload = cli('status', attachment_id) + check(code == 0, 'status failed: ' + str(payload)) + return payload + + +def flush_outbox(attachment_id): + wait_for(lambda: status_of(attachment_id)['outbox']['pending'] == 0, + 30, 'outbox publish', 1.0) + + +def new_attachment_id(chat_id, before): + rows = cli('find-by-chat', chat_id)[1] + for row in reversed(rows): + if row['id'] not in before: + return row['id'] + return None + + +def evaluate(tracker, attachment_id, baseline, expect_failed=False, + compare_baseline=True): + if attachment_id is None: + return {'I1': True, 'I2': True, 'I3': True, + 'I4': True, 'I5': True, 'I6': True} + status = status_of(attachment_id) + attachment = status['attachment'] + jobs = status['jobs'] + variants = status['variants'] + ready_version = attachment['ready_pipeline_version'] + + records = tracker.records.get(attachment_id, []) + reached = any(record[1] == ('failed' if expect_failed else 'ready') + for record in records) + if not records: + reached = attachment['status'] == ('failed' if expect_failed else 'ready') + i1 = reached or attachment['status'] in ('ready', 'failed') + + ready_variants = [v for v in variants + if v['pipeline_version'] == ready_version] + succeeded_ids = {job['job_id'] for job in jobs + if job['status'] == 'succeeded'} + succeeded_events = {} + for event in status['events']: + if event['event'] == 'succeeded': + succeeded_events[event['job_id']] = ( + succeeded_events.get(event['job_id'], 0) + 1 + ) + i2 = (len(ready_variants) == 3 + and all(succeeded_events.get(job_id) == 1 + for job_id in succeeded_ids)) + + i3 = True + seen_ready = False + last_version = 0 + for _at, state, version in records: + if seen_ready and state != 'ready': + i3 = False + if state == 'ready': + seen_ready = True + if version is not None: + if version < last_version: + i3 = False + last_version = version + + i4 = bool(succeeded_ids) or expect_failed or attachment['status'] == 'ready' + keys = {(variant['pipeline_version'], variant['variant']) + for variant in variants} + i5 = len(keys) == len(variants) + + if compare_baseline and ready_version == 1 and ready_variants: + observed = {variant['variant']: variant['sha256'] + for variant in ready_variants} + i6 = all(observed.get(name) == digest + for name, digest in baseline.items()) + else: + i6 = True + + return {'I1': i1, 'I2': i2, 'I3': i3, 'I4': i4, + 'I5': i5, 'I6': i6} + + +# --- scenarios ----------------------------------------------------------- + + +def scenario_duplicate(ctx): + attachment = upload(ctx['client'], ctx['alice'], ctx['chat'], + sample_bytes(), 'duplicate.png') + ctx['attachment_id'] = attachment['id'] + ctx['tracker'].track(attachment['id']) + wait_ready(ctx['client'], ctx['alice'], attachment['id']) + status = status_of(attachment['id']) + before = {variant['variant']: variant['s3'] + for variant in status['variants']} + message = build_message(status, status['jobs'][0]) + publish_raw([message] * 5) + wait_for( + lambda: len([event for event in status_of(attachment['id'])['events'] + if event['event'] == 'duplicate_skipped' + and event['detail'].get('reason') == 'already_succeeded']) == 5, + 20, 'five duplicate_skipped events', 1.0, + ) + after_status = status_of(attachment['id']) + after = {variant['variant']: variant['s3'] + for variant in after_status['variants']} + check(before == after, 'duplicate delivery rewrote variant objects') + return {'attachment_id': attachment['id'], + 'job_attempts': [job['attempt'] for job in after_status['jobs']], + 'notes': '5 duplicate deliveries were absorbed by the DB fence'} + + +def scenario_reorder(ctx): + compose('stop', 'worker') + try: + attachment = upload(ctx['client'], ctx['alice'], ctx['chat'], + sample_bytes(), 'reorder.png') + ctx['attachment_id'] = attachment['id'] + ctx['tracker'].track(attachment['id']) + flush_outbox(attachment['id']) + status = status_of(attachment['id']) + message = build_message(status, status['jobs'][0]) + stale = dict(message, attempt=99) + publish_raw([stale, message]) + compose('start', 'worker') + wait_ready(ctx['client'], ctx['alice'], attachment['id']) + finally: + compose('start', 'worker') + after_status = status_of(attachment['id']) + skipped = [event for event in after_status['events'] + if event['event'] == 'duplicate_skipped'] + check(len(skipped) == 2, 'expected exactly two skipped out-of-order messages') + check(len([event for event in after_status['events'] + if event['event'] == 'succeeded']) == 1, + 'expected exactly one successful effect') + return {'attachment_id': attachment['id'], + 'job_attempts': [job['attempt'] for job in after_status['jobs']], + 'notes': 'attempt 99 and a duplicate attempt 1 were acked as skips'} + + +def crash_scenario(ctx, point, filename, expected_attempt, + expect_lease_expired=False, expect_duplicate=False): + compose('stop', 'worker') + try: + attachment = upload(ctx['client'], ctx['alice'], ctx['chat'], + sample_bytes(), filename) + ctx['attachment_id'] = attachment['id'] + ctx['tracker'].track(attachment['id']) + starts = worker_start_count() + set_fault('fault:' + point + ':' + attachment['id'] + ':1', 'crash') + compose('start', 'worker') + wait_ready(ctx['client'], ctx['alice'], attachment['id'], 90) + finally: + compose('start', 'worker') + status = status_of(attachment['id']) + job = status['jobs'][0] + check(job['attempt'] == expected_attempt, + 'expected attempt ' + str(expected_attempt) + + ', got ' + str(job['attempt'])) + check(worker_start_count() >= starts + 2, + 'worker process did not restart after the injected crash') + if expect_lease_expired: + check(any(event['event'] == 'lease_expired' + for event in status['events']), + 'missing lease_expired event') + if expect_duplicate: + check(any(event['event'] == 'duplicate_skipped' + and event['detail'].get('reason') == 'already_succeeded' + for event in status['events']), + 'missing already_succeeded duplicate after redelivery') + return {'attachment_id': attachment['id'], + 'job_attempts': [job['attempt']], + 'notes': 'crashed at ' + point} + + +def scenario_crash_before_side_effect(ctx): + return crash_scenario(ctx, 'worker.before_side_effect', + 'crash-before.png', 2, expect_lease_expired=True) + + +def scenario_crash_after_side_effect(ctx): + return crash_scenario(ctx, 'worker.after_side_effect', + 'crash-after.png', 2, expect_lease_expired=True) + + +def scenario_crash_after_commit_before_ack(ctx): + return crash_scenario(ctx, 'worker.after_commit_before_ack', + 'crash-ack.png', 1, expect_duplicate=True) + + +def scenario_crash_after_commit_before_publish(ctx): + before = {row['id'] for row in cli('find-by-chat', ctx['chat']['id'])[1]} + set_fault('fault:api.after_commit_before_publish', 'crash') + try: + try: + ctx['client'].call( + 'POST', + '/api/v1/chats/' + ctx['chat']['id'] + + '/attachments?filename=crash-publish.png', + (202, 502, 503, 504), + actor=ctx['alice'], + raw=sample_bytes(), + headers={'Content-Type': 'image/png'}, + ) + except Failure: + # A killed replica may close the connection instead of answering. + pass + finally: + clear_fault('fault:api.after_commit_before_publish') + attachment_id = wait_for( + lambda: new_attachment_id(ctx['chat']['id'], before), 30, + 'attachment row after API crash', 1.0, + ) + ctx['attachment_id'] = attachment_id + ctx['tracker'].track(attachment_id) + wait_ready(ctx['client'], ctx['alice'], attachment_id, 90) + status = status_of(attachment_id) + check(len([event for event in status['events'] + if event['event'] == 'created']) == 1, + 'expected exactly one created event') + return {'attachment_id': attachment_id, + 'job_attempts': [job['attempt'] for job in status['jobs']], + 'notes': 'outbox relay published the committed job after API restart'} + + +def scenario_relay_crash_after_publish(ctx): + compose('stop', 'worker') + before = {row['id'] for row in cli('find-by-chat', ctx['chat']['id'])[1]} + try: + set_fault('fault:relay.after_publish_before_mark', 'crash') + try: + ctx['client'].call( + 'POST', + '/api/v1/chats/' + ctx['chat']['id'] + + '/attachments?filename=relay-crash.png', + (202, 502, 503, 504), + actor=ctx['alice'], + raw=sample_bytes(), + headers={'Content-Type': 'image/png'}, + ) + except Failure: + # The relay may crash the handling replica before the reply flushes. + pass + clear_fault('fault:relay.after_publish_before_mark') + attachment_id = wait_for( + lambda: new_attachment_id(ctx['chat']['id'], before), 30, + 'attachment row after relay crash', 1.0, + ) + ctx['attachment_id'] = attachment_id + ctx['tracker'].track(attachment_id) + wait_for(lambda: status_of(attachment_id)['outbox']['pending'] == 0, + 60, 'outbox published after relay crash', 1.0) + queue_depth = rabbit_queues().get('attachments.process', {}).get( + 'messages', 0) + compose('start', 'worker') + wait_ready(ctx['client'], ctx['alice'], attachment_id, 90) + finally: + clear_fault('fault:relay.after_publish_before_mark') + compose('start', 'worker') + status = status_of(attachment_id) + check(len([event for event in status['events'] + if event['event'] == 'succeeded']) == 1, + 'expected exactly one succeeded event') + duplicated = any(event['event'] == 'duplicate_skipped' + for event in status['events']) + note = ('relay republished the same message; duplicate absorbed' + if duplicated else + 'relay published once in this run; I1/I2 still hold') + if queue_depth >= 2: + check(duplicated, 'two queued copies but no duplicate_skipped event') + return {'attachment_id': attachment_id, + 'job_attempts': [job['attempt'] for job in status['jobs']], + 'notes': note} + + +def scenario_stale_version(ctx): + check(int(worker_env('WORKER_CONCURRENCY') or '1') >= 2, + 'stale_version needs WORKER_CONCURRENCY>=2') + compose('stop', 'worker') + try: + attachment = upload(ctx['client'], ctx['alice'], ctx['chat'], + sample_bytes(), 'stale.png') + attachment_id = attachment['id'] + ctx['attachment_id'] = attachment_id + ctx['tracker'].track(attachment_id) + flush_outbox(attachment_id) + code, payload = cli('reprocess', attachment_id, + '--pipeline-version', '2', '--reason', 'suite') + check(code == 0, 'reprocess v2 failed: ' + str(payload)) + set_fault('fault:worker.after_side_effect:' + attachment_id + ':1', + 'delay:8000') + compose('start', 'worker') + v2_etags = None + + def both_terminal(): + nonlocal v2_etags + status = status_of(attachment_id) + jobs = {(job['pipeline_version']): job for job in status['jobs']} + if '2' in jobs or 2 in jobs: + v2_job = jobs.get(2) + if v2_job and v2_job['status'] == 'succeeded' and v2_etags is None: + v2_etags = {variant['variant']: variant['s3'] + for variant in status['variants'] + if variant['pipeline_version'] == 2} + v1_job = jobs.get(1) + v2_job = jobs.get(2) + return (v1_job and v2_job + and v1_job['status'] == 'superseded' + and v2_job['status'] == 'succeeded') + + wait_for(both_terminal, 90, 'v2 ready and v1 superseded', 1.0) + status = status_of(attachment_id) + check(status['attachment']['ready_pipeline_version'] == 2, + 'ready pipeline version is not 2') + check(all(variant['pipeline_version'] == 2 + for variant in status['variants']), + 'variant rows from v1 survived') + after_etags = {variant['variant']: variant['s3'] + for variant in status['variants'] + if variant['pipeline_version'] == 2} + check(v2_etags == after_etags, 'v1 overwrote v2 variant objects') + + served = ctx['client'].call( + 'GET', + '/api/v1/attachments/' + attachment_id + + '/content?variant=thumbnail', + actor=ctx['alice'], + ) + v2_thumbnail = next(variant for variant in status['variants'] + if variant['variant'] == 'thumbnail') + check(sha256(served['bytes']) == v2_thumbnail['sha256'], + 'served thumbnail is not the v2 object') + check(sha256(served['bytes']) != ctx['baseline']['thumbnail'], + 'served thumbnail equals the v1 baseline') + + v1_job = next(job for job in status['jobs'] + if job['pipeline_version'] == 1) + publish_raw([build_message(status, v1_job)]) + wait_for( + lambda: any(event['event'] == 'duplicate_skipped' + and event['detail'].get('reason') == 'superseded' + for event in status_of(attachment_id)['events']), + 20, 'superseded duplicate skip', 1.0, + ) + finally: + clear_fault('fault:worker.after_side_effect:' + + (attachment['id'] if 'attachment' in locals() else '')) + compose('start', 'worker') + return {'attachment_id': attachment_id, + 'job_attempts': [job['attempt'] for job in status['jobs']], + 'notes': 'v1 finished after v2 and was recorded as superseded'} + + +def scenario_poison(ctx): + before = rabbit_queues().get('attachments.process.dlq', {}).get( + 'messages', 0) + publish_raw(['not-json', '{"schema_version":1}']) + wait_for( + lambda: rabbit_queues().get('attachments.process.dlq', {}).get( + 'messages', 0) >= before + 2, + 15, 'two poison messages in DLQ', 0.5, + ) + return {'attachment_id': None, 'job_attempts': [], + 'notes': 'malformed messages moved to the DLQ without touching jobs'} + + +def scenario_retry_and_replay(ctx): + max_attempts = int(worker_env('JOB_MAX_ATTEMPTS') or '5') + base = int(worker_env('JOB_RETRY_BASE_MS') or '500') + cap = int(worker_env('JOB_RETRY_MAX_MS') or '4000') + compose('stop', 'worker') + try: + attachment = upload(ctx['client'], ctx['alice'], ctx['chat'], + sample_bytes(), 'retry.png') + attachment_id = attachment['id'] + ctx['attachment_id'] = attachment_id + ctx['tracker'].track(attachment_id) + set_fault('fault:worker.before_side_effect:' + attachment_id + ':1', + 'transient') + compose('start', 'worker') + failed = wait_ready(ctx['client'], ctx['alice'], attachment_id, + 90, allow_failed=True) + finally: + compose('start', 'worker') + check(failed['status'] == 'failed', 'transient fault was not exhausted') + check(failed['error_code'] == 'retry_exhausted', + 'expected retry_exhausted, got ' + str(failed['error_code'])) + status = status_of(attachment_id) + retries = [event for event in status['events'] + if event['event'] == 'retry_scheduled'] + check(len(retries) == max_attempts - 1, + 'expected ' + str(max_attempts - 1) + ' retries, got ' + + str(len(retries))) + for index, event in enumerate(retries): + limit = min(cap, base * 2 ** index) + check(0 <= event['detail'].get('delay_ms', cap + 1) <= limit, + 'retry delay exceeds its jitter cap') + ctx['client'].call( + 'GET', '/api/v1/attachments/' + attachment_id + '/content', + 409, actor=ctx['alice'], + ) + code, payload = cli('replay', ctx['reference_id'], '--reason', 'suite') + check(code == 3 and payload.get('reason') in ('no_failed_job', + 'already_ready'), + 'replay of a ready attachment was not refused') + + clear_fault('fault:worker.before_side_effect:' + attachment_id + ':1') + code, payload = cli('replay', attachment_id, '--reason', 'suite') + check(code == 0, 'operator replay failed: ' + str(payload)) + wait_ready(ctx['client'], ctx['alice'], attachment_id, 60) + status = status_of(attachment_id) + replayed = [event for event in status['events'] + if event['event'] == 'replayed'] + check(len(replayed) == 1 and replayed[0]['detail'].get('operator'), + 'missing replayed event or operator audit') + return {'attachment_id': attachment_id, + 'job_attempts': [job['attempt'] for job in status['jobs']], + 'notes': 'transient injected fault exhausted retries; replay recovered'} + + +def scenario_hung_worker(ctx): + timeout_seconds = int(worker_env('JOB_TIMEOUT_SECONDS') or '10') + compose('stop', 'worker') + try: + attachment = upload(ctx['client'], ctx['alice'], ctx['chat'], + sample_bytes(), 'hung.png') + attachment_id = attachment['id'] + ctx['attachment_id'] = attachment_id + ctx['tracker'].track(attachment_id) + set_fault('fault:worker.after_side_effect:' + attachment_id + ':1', + 'delay:' + str((timeout_seconds + 5) * 1000)) + compose('start', 'worker') + wait_ready(ctx['client'], ctx['alice'], attachment_id, 90) + finally: + compose('start', 'worker') + status = status_of(attachment_id) + check(any(event['event'] == 'retry_scheduled' + and event['detail'].get('code') == 'processing_timeout' + for event in status['events']), + 'missing processing_timeout retry') + return {'attachment_id': attachment_id, + 'job_attempts': [job['attempt'] for job in status['jobs']], + 'notes': 'hard timeout aborted the hung render and retried'} + + +def scenario_big_image(ctx): + ctx['compare_baseline'] = False + data = worst_decode_png() + containers = compose('ps', '-q', 'worker').stdout.split() + stop = threading.Event() + peak = {'mb': 0.0} + + def sample(): + while not stop.is_set(): + if containers: + result = docker( + 'stats', '--no-stream', '--format', + '{{.Name}} {{.MemUsage}}', *containers, + check_result=False, + ) + for line in result.stdout.splitlines(): + parts = line.split() + if len(parts) < 2: + continue + match = re.match(r'([\d.]+)(KiB|MiB|GiB)', parts[1]) + if not match: + continue + value = float(match.group(1)) + if match.group(2) == 'GiB': + value *= 1024 + elif match.group(2) == 'KiB': + value /= 1024 + peak['mb'] = max(peak['mb'], value) + stop.wait(0.3) + + sampler = threading.Thread(target=sample, daemon=True) + sampler.start() + try: + def do_upload(index): + client = new_client(ctx['base_url']) + return upload(client, ctx['alice'], ctx['chat'], data, + 'big-' + str(index) + '.png') + + with ThreadPoolExecutor(max_workers=2) as pool: + attachments = list(pool.map(do_upload, (1, 2))) + ctx['attachment_id'] = attachments[0]['id'] + for attachment in attachments: + ctx['tracker'].track(attachment['id']) + results = [wait_ready(ctx['client'], ctx['alice'], + attachment['id'], 120, allow_failed=True) + for attachment in attachments] + finally: + stop.set() + sampler.join(timeout=5) + for meta in results: + check(meta['status'] in ('ready', 'failed'), 'big image stuck') + if meta['status'] == 'failed': + check(meta['error_code'] in ('image_too_large', 'invalid_image'), + 'unexpected big-image error code') + for container in containers: + result = docker('inspect', '-f', '{{.State.OOMKilled}}', container, + check_result=False) + check(result.stdout.strip() == 'false', 'worker was OOM-killed') + return {'attachment_id': attachments[0]['id'], + 'job_attempts': [], + 'notes': 'peak worker memory: %.1f MiB / 1280 MiB' % peak['mb']} + + +SCENARIOS = { + 'duplicate': scenario_duplicate, + 'reorder': scenario_reorder, + 'crash_before_side_effect': scenario_crash_before_side_effect, + 'crash_after_side_effect': scenario_crash_after_side_effect, + 'crash_after_commit_before_ack': scenario_crash_after_commit_before_ack, + 'crash_after_commit_before_publish': scenario_crash_after_commit_before_publish, + 'relay_crash_after_publish': scenario_relay_crash_after_publish, + 'stale_version': scenario_stale_version, + 'poison': scenario_poison, + 'retry_and_replay': scenario_retry_and_replay, + 'hung_worker': scenario_hung_worker, + 'big_image': scenario_big_image, +} + + +def select(name): + if name == 'all': + return LEVEL4 + [item for item in TRACK2 if item not in LEVEL4] + if name == 'level4': + return LEVEL4 + if name == 'track2': + return TRACK2 + if name in SCENARIOS: + return [name] + raise SystemExit('unknown scenario: ' + name) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--base-url', default=None) + parser.add_argument('--ca-file', default=None) + parser.add_argument('--scenario', default='level4') + parser.add_argument('--repeat', type=int, default=1) + parser.add_argument('--output', default=None) + args = parser.parse_args() + + if worker_env('FAULT_INJECTION_ENABLED') != '1': + print('fault injection is disabled; restart the stand with:\n ' + HINT, + file=sys.stderr) + raise SystemExit(2) + + client = new_client(args.base_url, args.ca_file) + alice, _bob, _eve, chat = client.bootstrap() + clear_faults() + reference = upload(client, alice, chat, sample_bytes(), 'reference.png') + ready = wait_ready(client, alice, reference['id'], 60) + baseline = variant_sha(ready) + + selected = select(args.scenario) + results = [] + failed = 0 + for name in selected: + for repeat in range(1, args.repeat + 1): + clear_faults() + tracker = Tracker(args.base_url or 'http://localhost:8080', alice, + args.ca_file) + tracker.start() + started = time.monotonic() + ctx = { + 'client': client, + 'base_url': args.base_url or 'http://localhost:8080', + 'alice': alice, + 'chat': chat, + 'tracker': tracker, + 'baseline': baseline, + 'reference_id': reference['id'], + 'ca_file': args.ca_file, + } + scenario = {'attachment_id': None, 'job_attempts': [], + 'notes': ''} + passed = True + try: + scenario.update(SCENARIOS[name](ctx)) + except Exception as error: # noqa: BLE001 - scenario boundary + passed = False + scenario['notes'] = type(error).__name__ + ': ' + str(error) + finally: + tracker.stop() + clear_faults() + compose('start', 'worker', check_result=False) + attachment_id = scenario['attachment_id'] or ctx.get('attachment_id') + try: + invariants = evaluate( + tracker, attachment_id, baseline, + compare_baseline=ctx.get('compare_baseline', True), + ) + except Exception: # noqa: BLE001 + invariants = {key: False for key in + ('I1', 'I2', 'I3', 'I4', 'I5', 'I6')} + passed = passed and all(invariants.values()) + if not passed: + failed += 1 + result = { + 'name': name, + 'repeat': repeat, + 'passed': passed, + 'duration_s': round(time.monotonic() - started, 3), + 'attachment_id': attachment_id, + 'job_attempts': scenario['job_attempts'], + 'events_summary': scenario.get('events_summary', {}), + 'invariants': invariants, + 'notes': scenario['notes'], + } + results.append(result) + print(('PASS ' if passed else 'FAIL ') + name + ' #' + str(repeat) + + ' ' + str(result['duration_s']) + 's ' + + json.dumps(invariants) + ' ' + result['notes'], + flush=True) + + if args.output: + path = Path(args.output) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(results, indent=2, ensure_ascii=False) + '\n') + print(str(len(results) - failed) + '/' + str(len(results)) + ' scenarios passed') + raise SystemExit(1 if failed else 0) + + +if __name__ == '__main__': + main() diff --git a/scripts/lab5/stand.py b/scripts/lab5/stand.py new file mode 100644 index 00000000..387ddff1 --- /dev/null +++ b/scripts/lab5/stand.py @@ -0,0 +1,180 @@ +"""Shared stdlib-only helpers for the lab5 demo and fault suite.""" +import hashlib +import json +import subprocess +import sys +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, Failure, check # noqa: E402 + +ENV_FILE = '.env' +COMPOSE = ['docker', 'compose', '--env-file', ENV_FILE] + + +def compose(*args, stdin=None, check_result=True): + result = subprocess.run( + COMPOSE + list(args), + cwd=ROOT, + input=stdin, + text=True, + capture_output=True, + ) + if check_result and result.returncode != 0: + detail = (result.stderr or result.stdout).strip().splitlines() + raise Failure('compose ' + ' '.join(args) + ' failed: ' + + (detail[-1] if detail else 'unknown error')) + return result + + +def docker(*args, check_result=True): + result = subprocess.run( + ['docker'] + list(args), cwd=ROOT, text=True, capture_output=True, + ) + if check_result and result.returncode != 0: + detail = (result.stderr or result.stdout).strip().splitlines() + raise Failure('docker ' + ' '.join(args) + ' failed: ' + + (detail[-1] if detail else 'unknown error')) + return result + + +def cli(*args, stdin=None): + """Run jobs.cli in the API container; returns (exit_code, parsed_json).""" + for _ in range(5): + result = compose( + 'exec', '-T', 'api', 'node', 'dist/jobs/jobs.cli.js', *args, + stdin=stdin, check_result=False, + ) + text = result.stdout.strip() + if text: + try: + return result.returncode, json.loads(text) + except ValueError: + pass + time.sleep(1) + raise Failure('jobs.cli did not return JSON for ' + ' '.join(args)) + + +def set_fault(key, value): + compose('exec', '-T', 'redis', 'redis-cli', 'SET', key, value) + + +def clear_fault(key): + compose('exec', '-T', 'redis', 'redis-cli', 'DEL', key) + + +def clear_faults(): + script = ( + "local keys=redis.call('keys',ARGV[1]) " + "for i=1,#keys do redis.call('del',keys[i]) end return #keys" + ) + compose('exec', '-T', 'redis', 'redis-cli', 'EVAL', script, '0', 'fault:*') + + +def rabbit_queues(): + result = compose('exec', '-T', 'broker', 'rabbitmqctl', '-q', + '-p', 'messenger', 'list_queues', + 'name', 'messages', 'type') + queues = {} + for line in result.stdout.splitlines(): + parts = line.split() + if len(parts) >= 2 and parts[0] not in ('name', 'Timeout:'): + try: + queues[parts[0]] = {'messages': int(parts[1]), + 'type': parts[2] if len(parts) > 2 else ''} + except ValueError: + continue + return queues + + +def wait_for(predicate, timeout, description='condition', interval=0.5): + deadline = time.monotonic() + timeout + last = None + while time.monotonic() < deadline: + last = predicate() + if last: + return last + time.sleep(interval) + raise Failure('timed out waiting for ' + description) + + +def upload(client, actor, chat, data, filename='sample.png', + content_type='image/png'): + result = client.call( + 'POST', + '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename, + 202, + actor=actor, + raw=data, + headers={'Content-Type': content_type}, + schema='Attachment', + ) + check(result['json']['status'] == 'queued', 'upload did not answer queued') + return result['json'] + + +def metadata(client, actor, attachment_id): + return client.call('GET', '/api/v1/attachments/' + attachment_id, + actor=actor, schema='Attachment')['json'] + + +def wait_ready(client, actor, attachment_id, timeout=60, + allow_failed=False): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + meta = metadata(client, actor, attachment_id) + if meta['status'] == 'failed': + if allow_failed: + return meta + raise Failure('attachment failed: ' + + str(meta.get('error_code'))) + if meta['status'] == 'ready': + return meta + time.sleep(0.2) + raise Failure('attachment did not become ready in ' + str(timeout) + 's') + + +def sha256(data): + return hashlib.sha256(data).hexdigest() + + +def variant_sha(meta): + return {item['name']: item['sha256'] for item in meta['variants']} + + +def sample_bytes(): + return (ROOT / 'tests/fixtures/sample.png').read_bytes() + + +def worker_env(name): + result = compose('exec', '-T', 'worker', 'printenv', name, + check_result=False) + return result.stdout.strip() + + +def worker_start_count(): + result = compose('logs', '--no-color', 'worker', check_result=False) + return result.stdout.count('worker ready:') + + +def worker_memory_peak(containers): + names = [line for line in containers.splitlines() if line.strip()] + if not names: + return 'n/a' + result = subprocess.run( + ['docker', 'stats', '--no-stream', '--format', + '{{.Name}} {{.MemUsage}}'] + names, + text=True, capture_output=True, + ) + return result.stdout.strip() + + +def print_step(number, text): + print('[' + str(number) + '] ' + text, flush=True) + + +def new_client(base_url, ca_file=None): + return Client(base_url, ca_file) diff --git a/scripts/lab5/worker_down_demo.py b/scripts/lab5/worker_down_demo.py new file mode 100755 index 00000000..ab4beeab --- /dev/null +++ b/scripts/lab5/worker_down_demo.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +"""Level-3 demo: work accepted while the worker is down survives restarts.""" +import sys +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) + +from stand import ( # noqa: E402 + Failure, + check, + cli, + compose, + new_client, + print_step, + rabbit_queues, + sample_bytes, + sha256, + upload, + wait_for, + wait_ready, +) +from client import arguments, run # noqa: E402 +from image_probe import jpeg_size # noqa: E402 + + +def main(): + args = arguments('lab5 worker-down durability demo') + client = new_client(args.base_url, args.ca_file) + alice, _bob, _eve, chat = client.bootstrap() + data = sample_bytes() + + print_step(1, 'compose ps') + print(compose('ps', '--format', + 'table {{.Service}}\t{{.Status}}').stdout.strip()) + + print_step(2, 'stop worker') + compose('stop', 'worker') + + print_step(3, 'upload while worker is down (expect 202 queued)') + attachment = upload(client, alice, chat, data, 'durability.png') + attachment_id = attachment['id'] + print('attachment=' + attachment_id + ' status=' + attachment['status']) + + print_step(4, 'content and thumbnail must answer 409 before ready') + for query in ('', '?variant=thumbnail'): + result = client.call( + 'GET', + '/api/v1/attachments/' + attachment_id + '/content' + query, + 409, + actor=alice, + ) + check(result['status'] == 409, 'content was served before ready') + + print_step(5, 'link the queued attachment to messages') + with_attachment = client.message( + alice, chat, '', attachments=[attachment_id], + )['json'] + plain = client.message(alice, chat, 'plain while worker is down')['json'] + check(with_attachment['attachment_ids'] == [attachment_id], + 'message lost the queued attachment') + check(plain['attachment_ids'] == [], 'plain message gained an attachment') + print('message=' + with_attachment['id']) + + print_step(6, 'wait for outbox publish and a message in the queue') + def published(): + _code, status = cli('status', attachment_id) + queues = rabbit_queues() + return ( + status['outbox']['pending'] == 0 + and queues.get('attachments.process', {}).get('messages', 0) >= 1 + ) + wait_for(published, 30, 'outbox publish and queue depth >= 1') + queues = rabbit_queues() + print('queue=' + str(queues.get('attachments.process'))) + + print_step(7, 'restart broker and API with volumes') + compose('restart', 'broker', 'api') + compose('up', '-d', '--no-recreate', '--no-deps', '--wait', 'broker', 'api') + + print_step(8, 'the quorum message survived the broker restart') + def survived(): + depth = rabbit_queues().get('attachments.process', {}).get('messages', 0) + return depth >= 1 + wait_for(survived, 60, 'queue depth >= 1 after broker restart') + print('queue=' + str(rabbit_queues().get('attachments.process'))) + + print_step(9, 'start worker') + compose('start', 'worker') + + print_step(10, 'wait ready and verify the thumbnail') + ready = wait_ready(client, alice, attachment_id, 60) + thumbnail = [v for v in ready['variants'] if v['name'] == 'thumbnail'][0] + content = client.call( + 'GET', + '/api/v1/attachments/' + attachment_id + '/content?variant=thumbnail', + actor=alice, + ) + check(jpeg_size(content['bytes']) == (256, 160), + 'thumbnail is not 256x160 JPEG') + check(len(content['bytes']) == thumbnail['size_bytes'], + 'thumbnail size differs from metadata') + check(sha256(content['bytes']) == thumbnail['sha256'], + 'thumbnail checksum differs from metadata') + history = client.call( + 'GET', + '/api/v1/chats/' + chat['id'] + '/messages?limit=100', + actor=alice, + schema='MessagePage', + )['json'] + check( + any(attachment_id in item['attachment_ids'] + for item in history['items']), + 'message history lost the attachment', + ) + print('ready attachment=' + attachment_id + + ' thumbnail=256x160 sha256=' + thumbnail['sha256'][:16] + '...') + + print_step(11, 'compose ps') + print(compose('ps', '--format', + 'table {{.Service}}\t{{.Status}}').stdout.strip()) + print('PASS: worker-down durability demo') + + +if __name__ == '__main__': + run(main) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..8653b0be --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Restart hook for `make test-persistence ACTION_SCRIPT=scripts/restart.sh`. +# Semantic and evidence notes: tests/README.md ("Проверка сохранности") and REPORT.md. +set -euo pipefail + +compose=(docker compose --env-file .env) +"${compose[@]}" restart postgres redis s3 broker api worker haproxy +"${compose[@]}" up -d --no-recreate --no-deps --wait postgres redis s3 broker api worker haproxy diff --git a/scripts/start-replica.sh b/scripts/start-replica.sh new file mode 100755 index 00000000..bf24b821 --- /dev/null +++ b/scripts/start-replica.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# Восстанавливает недостающую API-реплику до объявленного числа реплик. +set -eu + +env_file="${COMPOSE_ENV_FILE:-.env}" +replicas="${API_REPLICAS:-}" +if [ -z "$replicas" ] && [ -f "$env_file" ]; then + replicas=$(sed -n 's/^API_REPLICAS=//p' "$env_file" | tail -n 1) +fi +replicas="${replicas:-2}" +exec docker compose --env-file "$env_file" up -d --no-deps --scale "api=${replicas}" api diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..b915b602 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Останавливает ровно одну API-реплику стенда — ту, которую обслужила текущая +# сессия. TARGET_INSTANCE_ID приходит из теста (значение X-Instance-Id ответа), +# а в Docker hostname контейнера равен его короткому ID, поэтому сопоставление +# однозначно. Ничего, кроме этой реплики, скрипт не трогает: ни LB, ни БД, ни +# тома. Возврат управления — быстрый (SIGTERM -> дрейн DRAIN_DELAY_MS=3s -> +# выход), останавливать реплику "навсегда" не требуется. +set -eu + +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" +project="${LAB_PROJECT:-mtusi-lab5}" + +target='' +for container in $( + docker ps \ + --filter "label=com.docker.compose.project=${project}" \ + --filter 'label=com.docker.compose.service=api' \ + --format '{{.ID}}' +); do + case "$container" in + "$TARGET_INSTANCE_ID"*) + target="$container" + break + ;; + esac +done + +if [ -z "$target" ]; then + printf 'stop-one: no running api replica of project %s whose id starts with %s\n' \ + "$project" "$TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +printf 'stop-one: stopping api replica %s\n' "$target" >&2 +docker stop --time 10 "$target" >/dev/null