chore: port lab6 solution as lab7 base

This commit is contained in:
2026-11-30 16:36:13 +03:00
parent 20afc2c790
commit f736ff1da4
107 changed files with 23433 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
*.sh text eol=lf
*.cfg text eol=lf
+45
View File
@@ -0,0 +1,45 @@
# syntax=docker/dockerfile:1
FROM node:20.17.0-slim AS base
RUN apt-get update \
&& apt-get install -y --no-install-recommends openssl \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
FROM base AS deps
COPY package.json package-lock.json ./
RUN npm ci
FROM deps AS build
COPY prisma ./prisma
RUN npx prisma generate
COPY tsconfig.json tsconfig.build.json nest-cli.json ./
COPY src ./src
RUN npm run build
FROM build AS test
COPY contracts/openapi.json ./contracts/openapi.json
COPY tests/fixtures/sample.png ./tests/fixtures/sample.png
COPY test ./test
CMD ["npm", "test"]
FROM build AS migrate
CMD ["sh", "-c", "DATABASE_URL=\"$(cat \"$DATABASE_URL_FILE\")\" exec npx prisma migrate deploy"]
FROM build AS prod-deps
RUN npm prune --omit=dev
FROM base AS runtime
ENV NODE_ENV=production
COPY package.json ./
COPY --from=prod-deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY contracts/openapi.json ./contracts/openapi.json
USER node
EXPOSE 8080
HEALTHCHECK --interval=5s --timeout=2s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://127.0.0.1:8080/health/core',r=>process.exit(r.statusCode===200?0:1)).on('error',()=>process.exit(1))"
CMD ["node", "dist/main.js"]
+55
View File
@@ -0,0 +1,55 @@
name: messenger-lab7-test
services:
postgres:
image: postgres:17.2-alpine
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: messenger_test
tmpfs:
- /var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "test", "-d", "messenger_test"]
interval: 2s
timeout: 3s
retries: 15
redis:
image: redis:7.4.1-alpine
command: ["redis-server", "--save", "", "--appendonly", "no"]
s3:
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
command: ["server", "/data", "--address", ":9000"]
environment:
MINIO_ROOT_USER: test-root
MINIO_ROOT_PASSWORD: test-root-password
MINIO_BROWSER: "off"
tmpfs:
- /data
healthcheck:
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
interval: 2s
timeout: 3s
retries: 15
tests:
build:
context: .
target: test
image: mtusi-messenger-lab7-test:dev
environment:
DATABASE_URL: postgresql://test:test@postgres:5432/messenger_test?connection_limit=20
CACHE_URL: redis://redis:6379/0
S3_ENDPOINT: http://s3:9000
S3_BUCKET: messenger-test
S3_ACCESS_KEY_ID: test-root
S3_SECRET_ACCESS_KEY: test-root-password
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_started
s3:
condition: service_healthy
+431
View File
@@ -0,0 +1,431 @@
name: messenger-lab7
x-otel: &otel
OTEL_EXPORTER_OTLP_ENDPOINT: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://otel-collector:4318}
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
OTEL_RESOURCE_ATTRIBUTES: ${OTEL_RESOURCE_ATTRIBUTES:-deployment.environment.name=lab}
services:
secrets-init:
image: alpine:3.20.3
volumes:
- secrets:/secrets
command:
- sh
- -ec
- |
cd /secrets
mkdir -p postgres redis api migrate s3 s3-init reconciler broker rabbitmq relay worker grafana
for entry in pg_password:postgres:24 redis_password:redis:24 s3_root_user:s3:10 s3_root_password:s3:20 s3_access_key_id:api:10 s3_secret_access_key:api:20 broker_password:broker:24 admin_password:grafana:16; do
file=$${entry%%:*}; rest=$${entry#*:}; dir=$${rest%%:*}; size=$${rest##*:}
[ -s "$$dir/$$file" ] || head -c "$$size" /dev/urandom | od -An -tx1 | tr -d ' \n' > "$$dir/$$file"
done
rm -f s3-init/* rabbitmq/* relay/* worker/*
cp s3/s3_root_user s3/s3_root_password api/s3_access_key_id api/s3_secret_access_key s3-init/
db_url() { printf 'postgresql://messenger:%s@postgres:5432/messenger?connection_limit=%s&pool_timeout=5&connect_timeout=5&socket_timeout=10&application_name=%s&options=-c%%20statement_timeout%%3D5000' "$$(cat postgres/pg_password)" "$$1" "$$2"; }
db_url 10 api > api/database_url
db_url 5 worker > worker/database_url
db_url 3 relay > relay/database_url
printf 'redis://:%s@redis:6379/0' "$$(cat redis/redis_password)" > api/cache_url
printf 'postgresql://messenger:%s@postgres:5432/messenger' "$$(cat postgres/pg_password)" > migrate/database_url
printf 'amqp://messenger:%s@rabbitmq:5672/' "$$(cat broker/broker_password)" > worker/broker_url
cp worker/broker_url relay/
printf 'default_user = messenger\ndefault_pass = %s\nloopback_users = none\nlog.console = true\nlisteners.tcp.default = 5672\n' "$$(cat broker/broker_password)" > rabbitmq/10-messenger.conf
cp api/database_url api/s3_access_key_id api/s3_secret_access_key reconciler/
cp api/s3_access_key_id api/s3_secret_access_key worker/
chmod 0444 postgres/* redis/* api/* migrate/* s3/* s3-init/* reconciler/* broker/* rabbitmq/* relay/* worker/* grafana/*
deploy:
resources:
limits: { cpus: "0.1", memory: 32M }
postgres:
image: postgres:17.2-alpine
environment:
POSTGRES_USER: messenger
POSTGRES_DB: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/pg_password
volumes:
- pgdata:/var/lib/postgresql/data
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: postgres } }
deploy:
resources:
limits: { cpus: "1.0", memory: 512M }
healthcheck:
test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"]
interval: 5s
timeout: 3s
retries: 10
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
redis:
image: redis:7.4.1-alpine
user: redis
command:
- sh
- -c
- exec redis-server --save '' --appendonly no --maxmemory 64mb --maxmemory-policy allkeys-lru --requirepass "$$(cat /run/secrets/redis_password)"
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: redis } }
deploy:
resources:
limits: { cpus: "0.25", memory: 128M }
healthcheck:
test: ["CMD-SHELL", "redis-cli --no-auth-warning -a \"$$(cat /run/secrets/redis_password)\" ping | grep -q PONG"]
interval: 5s
timeout: 3s
retries: 10
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
s3:
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
command: ["server", "/data", "--address", ":9000"]
environment:
MINIO_ROOT_USER_FILE: /run/secrets/s3_root_user
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password
MINIO_BROWSER: "off"
volumes:
- s3data:/data
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3 } }
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
healthcheck:
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
interval: 5s
timeout: 3s
retries: 10
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
s3-init:
image: pgsty/mc:RELEASE.2026-10-26T00-00-00Z@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8
entrypoint: ["sh", "-ec"]
command:
- |
cd /run/secrets
export MC_HOST_root="http://$$(cat s3_root_user):$$(cat s3_root_password)@s3:9000"
mc mb --ignore-existing "root/$$S3_BUCKET"
mc anonymous set none "root/$$S3_BUCKET"
cat > /tmp/policy.json <<POLICY
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["s3:ListBucket","s3:GetBucketLocation","s3:ListBucketMultipartUploads"],"Resource":["arn:aws:s3:::$$S3_BUCKET"]},
{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject","s3:AbortMultipartUpload","s3:ListMultipartUploadParts"],"Resource":["arn:aws:s3:::$$S3_BUCKET/*"]}
]}
POLICY
mc admin policy create root messenger-api /tmp/policy.json
mc admin user add root "$$(cat s3_access_key_id)" "$$(cat s3_secret_access_key)" > /dev/null
mc admin policy attach root messenger-api --user "$$(cat s3_access_key_id)" > /dev/null
echo "bucket $$S3_BUCKET ready"
environment:
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3-init } }
deploy:
resources:
limits: { cpus: "0.25", memory: 256M }
depends_on:
s3:
condition: service_healthy
migrate:
build:
context: .
target: migrate
image: mtusi-messenger-lab7-migrate:dev
environment:
DATABASE_URL_FILE: /run/secrets/database_url
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: migrate } }
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
depends_on:
postgres:
condition: service_healthy
api:
build:
context: .
target: runtime
image: mtusi-messenger-lab7-api:dev
environment:
APP_HOST: 0.0.0.0
APP_PORT: 8080
APP_ENV: ${APP_ENV:-lab}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
SHUTDOWN_DELAY_MS: 3000
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
S3_ENDPOINT: http://s3:9000
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${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
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
<<: *otel
OTEL_SERVICE_NAME: messenger-api
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: api } }
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: { cpus: "0.5", memory: 256M }
stop_grace_period: 10s
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_started
s3-init:
condition: service_completed_successfully
restart: unless-stopped
reconciler:
build:
context: .
target: runtime
image: mtusi-messenger-lab7-api:dev
command: ["node", "dist/reconcile.js", "--apply", "--loop", "--grace-seconds=${RECONCILE_GRACE_SECONDS:-3600}", "--interval-seconds=${RECONCILE_INTERVAL_SECONDS:-300}"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
S3_ENDPOINT: http://s3:9000
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${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
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: reconciler } }
deploy:
resources:
limits: { cpus: "0.25", memory: 128M }
healthcheck:
disable: true
depends_on:
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
restart: unless-stopped
rabbitmq:
image: rabbitmq:4.0.5-alpine@sha256:e045b5166b9188a38eee5c99411709aa1f0ffa2ac75664053e64cb69411f7c32
hostname: rabbitmq
volumes:
- rabbitmqdata:/var/lib/rabbitmq
- { type: volume, source: secrets, target: /etc/rabbitmq/conf.d, read_only: true, volume: { subpath: rabbitmq } }
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "-q", "check_port_connectivity"]
interval: 10s
timeout: 10s
retries: 12
start_period: 20s
stop_grace_period: 30s
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
relay:
build:
context: .
target: runtime
image: mtusi-messenger-lab7-api:dev
command: ["node", "dist/relay.js"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
BROKER_URL_FILE: /run/secrets/broker_url
JOB_STALE_SECONDS: ${JOB_STALE_SECONDS:-300}
RELAY_FAULT: ${RELAY_FAULT:-}
<<: *otel
OTEL_SERVICE_NAME: messenger-relay
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: relay } }
deploy:
resources:
limits: { cpus: "0.25", memory: 128M }
healthcheck:
disable: true
stop_grace_period: 10s
depends_on:
migrate:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
restart: unless-stopped
worker:
build:
context: .
target: runtime
image: mtusi-messenger-lab7-api:dev
command: ["node", "dist/worker.js"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
BROKER_URL_FILE: /run/secrets/broker_url
S3_ENDPOINT: http://s3:9000
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${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
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2}
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:-90}
WORKER_FAULT: ${WORKER_FAULT:-}
<<: *otel
OTEL_SERVICE_NAME: messenger-worker
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: worker } }
deploy:
resources:
limits: { cpus: "1.0", memory: 768M }
healthcheck:
disable: true
stop_grace_period: 30s
depends_on:
migrate:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
s3-init:
condition: service_completed_successfully
restart: unless-stopped
lb:
image: haproxy:3.0.6-alpine
ports:
- "127.0.0.1:${APP_PORT:-8080}:8080"
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
deploy:
resources:
limits: { cpus: "0.25", memory: 64M }
depends_on:
api:
condition: service_healthy
restart: unless-stopped
otel-collector:
image: otel/opentelemetry-collector-contrib:0.115.1@sha256:d2da12c4336a79758826700be9e21ecf4a9f7d945b7f8a58ba55ee3fa45427c8
command: ["--config=/etc/otelcol/collector.yaml"]
volumes:
- ./infra/otel/collector.yaml:/etc/otelcol/collector.yaml:ro
networks: [default, telemetry]
deploy:
resources:
limits: { cpus: "0.5", memory: 256M }
restart: unless-stopped
prometheus:
image: prom/prometheus:v3.1.0@sha256:6559acbd5d770b15bb3c954629ce190ac3cbbdb2b7f1c30f0385c4e05104e218
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
- --storage.tsdb.retention.time=7d
- --storage.tsdb.retention.size=1GB
- --web.enable-otlp-receiver
volumes:
- ./infra/prometheus:/etc/prometheus:ro
- prometheusdata:/prometheus
networks: [telemetry]
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:9090/-/ready"]
interval: 5s
timeout: 3s
retries: 20
restart: unless-stopped
loki:
image: grafana/loki:3.3.2@sha256:8af2de1abbdd7aa92b27c9bcc96f0f4140c9096b507c77921ffddf1c6ad6c48f
command: ["-config.file=/etc/loki/loki.yaml"]
volumes:
- ./infra/loki/loki.yaml:/etc/loki/loki.yaml:ro
- lokidata:/loki
networks: [telemetry]
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
restart: unless-stopped
tempo:
image: grafana/tempo:2.6.1@sha256:ef4384fce6e8ad22b95b243d8fc165628cda655376fd50e7850536ad89d71d50
command: ["-config.file=/etc/tempo/tempo.yaml"]
volumes:
- ./infra/tempo/tempo.yaml:/etc/tempo/tempo.yaml:ro
- tempodata:/var/tempo
networks: [telemetry]
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
restart: unless-stopped
grafana:
image: grafana/grafana:11.4.0@sha256:d8ea37798ccc41061a62ab080f2676dda6bf7815558499f901bdb0f533a456fb
ports:
- "127.0.0.1:${GRAFANA_PORT:-3000}:3000"
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD__FILE: /run/secrets/admin_password
GF_AUTH_ANONYMOUS_ENABLED: "true"
GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
GF_USERS_VIEWERS_CAN_EDIT: "true"
GF_ANALYTICS_REPORTING_ENABLED: "false"
GF_ANALYTICS_CHECK_FOR_UPDATES: "false"
GF_NEWS_NEWS_FEED_ENABLED: "false"
volumes:
- ./infra/grafana/provisioning:/etc/grafana/provisioning:ro
- ./infra/grafana/dashboards:/var/lib/grafana/dashboards:ro
- grafanadata:/var/lib/grafana
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: grafana } }
networks: [default, telemetry]
deploy:
resources:
limits: { cpus: "0.5", memory: 256M }
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:3000/api/health"]
interval: 5s
timeout: 3s
retries: 20
depends_on:
secrets-init:
condition: service_completed_successfully
prometheus:
condition: service_healthy
loki:
condition: service_started
tempo:
condition: service_started
restart: unless-stopped
volumes:
pgdata:
s3data:
rabbitmqdata:
prometheusdata:
lokidata:
tempodata:
grafanadata:
secrets:
networks:
telemetry:
internal: true
+428
View File
@@ -0,0 +1,428 @@
{
"uid": "messenger-overview",
"title": "Messenger overview",
"tags": ["messenger", "lab6"],
"timezone": "browser",
"schemaVersion": 39,
"version": 2,
"editable": false,
"graphTooltip": 1,
"refresh": "10s",
"time": { "from": "now-30m", "to": "now" },
"templating": { "list": [] },
"annotations": { "list": [] },
"panels": [
{ "type": "row", "title": "SLO и alerts", "id": 100, "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 0 } },
{
"type": "stat",
"title": "Доступность API, 1 ч (SLO ≥ 99% без 5xx)",
"id": 20,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 5, "w": 6, "x": 0, "y": 1 },
"fieldConfig": {
"defaults": {
"unit": "percentunit", "decimals": 2,
"thresholds": { "mode": "absolute", "steps": [{ "color": "red", "value": null }, { "color": "green", "value": 0.99 }] }
},
"overrides": []
},
"options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "colorMode": "background" },
"targets": [{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "1 - messenger:http_error_ratio:rate1h" }]
},
{
"type": "stat",
"title": "Запросы быстрее 500 мс, 1 ч (SLO ≥ 95%)",
"id": 21,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 5, "w": 6, "x": 6, "y": 1 },
"fieldConfig": {
"defaults": {
"unit": "percentunit", "decimals": 2,
"thresholds": { "mode": "absolute", "steps": [{ "color": "red", "value": null }, { "color": "green", "value": 0.95 }] }
},
"overrides": []
},
"options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "colorMode": "background" },
"targets": [{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "messenger:http_fast_ratio:rate1h" }]
},
{
"type": "stat",
"title": "Вложения готовы за ≤ 10 с, 1 ч (SLO ≥ 95%)",
"id": 22,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 5, "w": 6, "x": 12, "y": 1 },
"fieldConfig": {
"defaults": {
"unit": "percentunit", "decimals": 2,
"thresholds": { "mode": "absolute", "steps": [{ "color": "red", "value": null }, { "color": "green", "value": 0.95 }] }
},
"overrides": []
},
"options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "colorMode": "background" },
"targets": [{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "messenger:time_to_ready_fast_ratio:rate1h" }]
},
{
"type": "state-timeline",
"title": "Alerts (pending / firing)",
"id": 23,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 5, "w": 6, "x": 18, "y": 1 },
"fieldConfig": {
"defaults": {
"color": { "mode": "thresholds" },
"thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }, { "color": "orange", "value": 1 }, { "color": "red", "value": 2 }] },
"mappings": [{ "type": "value", "options": { "1": { "text": "pending" }, "2": { "text": "firing" } } }]
},
"overrides": []
},
"options": { "showValue": "never", "mergeValues": true, "legend": { "showLegend": false } },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "max by (alertname) (ALERTS{alertstate=\"firing\"}) * 2 or max by (alertname) (ALERTS{alertstate=\"pending\"})",
"legendFormat": "{{alertname}}"
}
]
},
{ "type": "row", "title": "API", "id": 101, "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 6 } },
{
"type": "timeseries",
"title": "RPS по маршрутам (без health)",
"id": 1,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 7 },
"fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] },
"options": { "legend": { "displayMode": "table", "placement": "right", "calcs": ["lastNotNull"] } },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "sum by (http_request_method, http_route) (rate(http_server_requests_total{job=\"messenger-api\", http_route!~\"/health.*\"}[$__rate_interval]))",
"legendFormat": "{{http_request_method}} {{http_route}}"
}
]
},
{
"type": "timeseries",
"title": "Ответы по классам статуса (без health)",
"id": 2,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 7 },
"fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "sum by (http_response_status_class) (rate(http_server_requests_total{job=\"messenger-api\", http_route!~\"/health.*\"}[$__rate_interval]))",
"legendFormat": "{{http_response_status_class}}"
}
]
},
{
"type": "timeseries",
"title": "Доля 5xx среди бизнес-запросов (окно 5m, порог alert 5%)",
"id": 3,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 0, "y": 15 },
"fieldConfig": {
"defaults": {
"unit": "percentunit",
"min": 0,
"thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }, { "color": "red", "value": 0.05 }] },
"custom": { "thresholdsStyle": { "mode": "line" } }
},
"overrides": []
},
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "messenger:http_error_ratio:rate5m", "legendFormat": "5xx ratio" }
]
},
{
"type": "timeseries",
"title": "Длительность API p50 / p95 / p99 (все реплики, без health)",
"id": 4,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 8, "y": 15 },
"fieldConfig": { "defaults": { "unit": "s", "min": 0 }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.50, sum by (le) (rate(http_server_request_duration_seconds_bucket{job=\"messenger-api\", http_route!~\"/health.*\"}[$__rate_interval])))",
"legendFormat": "p50"
},
{
"refId": "B",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.95, sum by (le) (rate(http_server_request_duration_seconds_bucket{job=\"messenger-api\", http_route!~\"/health.*\"}[$__rate_interval])))",
"legendFormat": "p95"
},
{
"refId": "C",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.99, sum by (le) (rate(http_server_request_duration_seconds_bucket{job=\"messenger-api\", http_route!~\"/health.*\"}[$__rate_interval])))",
"legendFormat": "p99"
}
]
},
{
"type": "timeseries",
"title": "p95 по маршрутам",
"id": 5,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 16, "y": 15 },
"fieldConfig": { "defaults": { "unit": "s", "min": 0 }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.95, sum by (le, http_request_method, http_route) (rate(http_server_request_duration_seconds_bucket{job=\"messenger-api\", http_route!~\"/health.*\"}[$__rate_interval])))",
"legendFormat": "{{http_request_method}} {{http_route}}"
}
]
},
{ "type": "row", "title": "Очередь и worker", "id": 102, "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 23 } },
{
"type": "timeseries",
"title": "Глубина очереди attachment.jobs (из attachment_jobs)",
"id": 30,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 0, "y": 24 },
"fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "max by (state) (jobs_queue_depth{job=\"messenger-relay\"})",
"legendFormat": "{{state}}"
}
]
},
{
"type": "timeseries",
"title": "Возраст старейшего готового задания (порог alert 60 s)",
"id": 31,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 8, "y": 24 },
"fieldConfig": {
"defaults": {
"unit": "s", "min": 0,
"thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }, { "color": "red", "value": 60 }] },
"custom": { "thresholdsStyle": { "mode": "line" } }
},
"overrides": []
},
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "messenger:jobs_queue_oldest_age:max", "legendFormat": "oldest due job age" }
]
},
{
"type": "timeseries",
"title": "Worker: занято / ёмкость",
"id": 32,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 16, "y": 24 },
"fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(worker_jobs_busy{job=\"messenger-worker\"})", "legendFormat": "busy" },
{ "refId": "B", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(worker_jobs_capacity{job=\"messenger-worker\"})", "legendFormat": "capacity" },
{ "refId": "C", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(avg_over_time(worker_jobs_busy{job=\"messenger-worker\"}[5m])) / sum(worker_jobs_capacity{job=\"messenger-worker\"})", "legendFormat": "saturation 5m" }
]
},
{
"type": "timeseries",
"title": "Задания по исходу (в секунду)",
"id": 6,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 0, "y": 32 },
"fieldConfig": { "defaults": { "unit": "ops" }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "sum by (job_type, outcome) (rate(jobs_processed_total{job=\"messenger-worker\"}[$__rate_interval]))",
"legendFormat": "{{job_type}} {{outcome}}"
}
]
},
{
"type": "timeseries",
"title": "Длительность обработки задания p50 / p95 / p99",
"id": 7,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 8, "y": 32 },
"fieldConfig": { "defaults": { "unit": "s", "min": 0 }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.50, sum by (le) (rate(jobs_duration_seconds_bucket{job=\"messenger-worker\", outcome=\"success\"}[$__rate_interval])))",
"legendFormat": "p50 success"
},
{
"refId": "B",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.95, sum by (le) (rate(jobs_duration_seconds_bucket{job=\"messenger-worker\", outcome=\"success\"}[$__rate_interval])))",
"legendFormat": "p95 success"
},
{
"refId": "C",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.99, sum by (le) (rate(jobs_duration_seconds_bucket{job=\"messenger-worker\", outcome=\"success\"}[$__rate_interval])))",
"legendFormat": "p99 success"
}
]
},
{
"type": "timeseries",
"title": "Время от upload до ready p50 / p95 / p99",
"id": 33,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 16, "y": 32 },
"fieldConfig": { "defaults": { "unit": "s", "min": 0 }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.50, sum by (le) (rate(attachments_time_to_ready_seconds_bucket{job=\"messenger-worker\"}[$__rate_interval])))",
"legendFormat": "p50"
},
{
"refId": "B",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.95, sum by (le) (rate(attachments_time_to_ready_seconds_bucket{job=\"messenger-worker\"}[$__rate_interval])))",
"legendFormat": "p95"
},
{
"refId": "C",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "histogram_quantile(0.99, sum by (le) (rate(attachments_time_to_ready_seconds_bucket{job=\"messenger-worker\"}[$__rate_interval])))",
"legendFormat": "p99"
}
]
},
{ "type": "row", "title": "БД и ресурсы", "id": 103, "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 40 } },
{
"type": "timeseries",
"title": "Пул БД: занято / ожидают соединения",
"id": 40,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 0, "y": 41 },
"fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum by (job) (db_client_connection_count{db_client_connection_state=\"used\"})", "legendFormat": "{{job}} used" },
{ "refId": "B", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum by (job) (db_client_connection_pending_requests)", "legendFormat": "{{job}} waiting" },
{ "refId": "C", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum by (job) (db_client_connection_max)", "legendFormat": "{{job}} max" }
]
},
{
"type": "timeseries",
"title": "CPU процессов (доля ядра)",
"id": 41,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 8, "y": 41 },
"fieldConfig": { "defaults": { "unit": "percentunit", "min": 0 }, "overrides": [] },
"targets": [
{
"refId": "A",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "sum by (job, instance) (rate(process_cpu_time_seconds_total{job=~\"messenger-.+\"}[$__rate_interval]))",
"legendFormat": "{{job}} {{instance}}"
},
{
"refId": "B",
"datasource": { "type": "prometheus", "uid": "prometheus" },
"expr": "rate(otelcol_process_cpu_seconds{job=\"otel-collector\"}[$__rate_interval])",
"legendFormat": "otel-collector"
}
]
},
{
"type": "timeseries",
"title": "RAM процессов (RSS)",
"id": 42,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 8, "x": 16, "y": 41 },
"fieldConfig": { "defaults": { "unit": "bytes", "min": 0 }, "overrides": [] },
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "process_memory_usage_bytes{job=~\"messenger-.+\"}", "legendFormat": "{{job}} {{instance}}" },
{ "refId": "B", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "otelcol_process_memory_rss{job=\"otel-collector\"}", "legendFormat": "otel-collector" }
]
},
{ "type": "row", "title": "Конвейер телеметрии (Collector)", "id": 104, "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 49 } },
{
"type": "timeseries",
"title": "Принято / отправлено / ошибки отправки (в секунду)",
"id": 50,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 50 },
"fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(rate(otelcol_receiver_accepted_spans[$__rate_interval]))", "legendFormat": "spans accepted" },
{ "refId": "B", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(rate(otelcol_exporter_sent_spans[$__rate_interval]))", "legendFormat": "spans sent" },
{ "refId": "C", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(rate(otelcol_exporter_send_failed_spans[$__rate_interval])) or vector(0)", "legendFormat": "spans failed" },
{ "refId": "D", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(rate(otelcol_exporter_sent_log_records[$__rate_interval]))", "legendFormat": "logs sent" },
{ "refId": "E", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(rate(otelcol_exporter_send_failed_log_records[$__rate_interval])) or vector(0)", "legendFormat": "logs failed" },
{ "refId": "F", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "sum(rate(otelcol_receiver_refused_spans[$__rate_interval])) + sum(rate(otelcol_receiver_refused_log_records[$__rate_interval])) or vector(0)", "legendFormat": "refused (memory_limiter)" }
]
},
{
"type": "timeseries",
"title": "Очередь exporter (ограничена queue_size)",
"id": 51,
"datasource": { "type": "prometheus", "uid": "prometheus" },
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 50 },
"fieldConfig": { "defaults": { "unit": "short", "min": 0 }, "overrides": [] },
"targets": [
{ "refId": "A", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "max by (exporter) (otelcol_exporter_queue_size)", "legendFormat": "{{exporter}} size" },
{ "refId": "B", "datasource": { "type": "prometheus", "uid": "prometheus" }, "expr": "max(otelcol_exporter_queue_capacity)", "legendFormat": "capacity" }
]
},
{ "type": "row", "title": "Логи и трассы", "id": 105, "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 58 } },
{
"type": "logs",
"title": "Логи API и worker (WARN и выше)",
"id": 8,
"datasource": { "type": "loki", "uid": "loki" },
"gridPos": { "h": 10, "w": 12, "x": 0, "y": 59 },
"options": { "showTime": true, "wrapLogMessage": true, "sortOrder": "Descending", "enableLogDetails": true },
"targets": [
{ "refId": "A", "datasource": { "type": "loki", "uid": "loki" }, "expr": "{service_name=~\"messenger-.+\"} | severity_text=~\"WARN|ERROR|FATAL\"" }
]
},
{
"type": "logs",
"title": "Все логи (http.request, job.handled, …)",
"id": 9,
"datasource": { "type": "loki", "uid": "loki" },
"gridPos": { "h": 10, "w": 12, "x": 12, "y": 59 },
"options": { "showTime": true, "wrapLogMessage": true, "sortOrder": "Descending", "enableLogDetails": true },
"targets": [
{ "refId": "A", "datasource": { "type": "loki", "uid": "loki" }, "expr": "{service_name=~\"messenger-.+\"}" }
]
},
{
"type": "table",
"title": "Последние трассы (HTTP server и consumer spans)",
"id": 10,
"datasource": { "type": "tempo", "uid": "tempo" },
"gridPos": { "h": 10, "w": 24, "x": 0, "y": 69 },
"targets": [
{
"refId": "A",
"datasource": { "type": "tempo", "uid": "tempo" },
"queryType": "traceql",
"query": "{resource.service.name=~\"messenger-.+\" && (kind=server || kind=consumer)}",
"limit": 20,
"tableType": "traces"
}
]
}
]
}
@@ -0,0 +1,10 @@
apiVersion: 1
providers:
- name: messenger
folder: Messenger
type: file
disableDeletion: true
allowUiUpdates: false
options:
path: /var/lib/grafana/dashboards
@@ -0,0 +1,45 @@
apiVersion: 1
datasources:
- name: Prometheus
uid: prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
jsonData:
httpMethod: POST
exemplarTraceIdDestinations:
- name: trace_id
datasourceUid: tempo
- name: Loki
uid: loki
type: loki
access: proxy
url: http://loki:3100
jsonData:
derivedFields:
- name: trace_id
matcherType: label
matcherRegex: trace_id
url: "$${__value.raw}"
datasourceUid: tempo
- name: Tempo
uid: tempo
type: tempo
access: proxy
url: http://tempo:3200
jsonData:
tracesToLogsV2:
datasourceUid: loki
spanStartTimeShift: -5m
spanEndTimeShift: 5m
filterByTraceID: false
customQuery: true
query: '{service_name=~".+"} | trace_id="$${__span.traceId}"'
tracesToMetrics:
datasourceUid: prometheus
serviceMap:
datasourceUid: prometheus
nodeGraph:
enabled: true
+43
View File
@@ -0,0 +1,43 @@
global
log stdout format raw local0 info
maxconn 2000
defaults
mode http
log global
option httplog
option dontlognull
timeout connect 1s
timeout client 30s
timeout server 10s
timeout http-request 10s
timeout queue 5s
timeout check 1s
retries 2
option redispatch 1
retry-on conn-failure
unique-id-format %[uuid()]
http-error status 502 content-type application/json hdr X-Request-Id "%[unique-id]" lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"bad gateway\",\"request_id\":\"%[unique-id]\"}}"
http-error status 503 content-type application/json hdr X-Request-Id "%[unique-id]" lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"no API replica available\",\"request_id\":\"%[unique-id]\"}}"
http-error status 504 content-type application/json hdr X-Request-Id "%[unique-id]" lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"gateway timeout\",\"request_id\":\"%[unique-id]\"}}"
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 5s
hold obsolete 5s
accepted_payload_size 8192
frontend public
bind :8080
default_backend api
backend api
balance roundrobin
http-request set-timeout server 60s if { method POST } { path_reg ^/api/v1/chats/[^/]+/attachments$ }
option httpchk GET /health/core
http-check expect status 200
default-server check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none
server-template api 1-4 api:8080
+43
View File
@@ -0,0 +1,43 @@
auth_enabled: false
server:
http_listen_address: 0.0.0.0
http_listen_port: 3100
grpc_listen_port: 9096
log_level: warn
common:
instance_addr: 127.0.0.1
path_prefix: /loki
replication_factor: 1
storage:
filesystem:
chunks_directory: /loki/chunks
rules_directory: /loki/rules
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2026-01-01
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
limits_config:
allow_structured_metadata: true
retention_period: 168h
ingestion_rate_mb: 8
ingestion_burst_size_mb: 16
compactor:
working_directory: /loki/compactor
retention_enabled: true
delete_request_store: filesystem
analytics:
reporting_enabled: false
+85
View File
@@ -0,0 +1,85 @@
extensions:
health_check:
endpoint: 0.0.0.0:13133
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
max_request_body_size: 4194304
processors:
memory_limiter:
check_interval: 1s
limit_mib: 160
spike_limit_mib: 40
batch:
send_batch_size: 512
send_batch_max_size: 1024
timeout: 2s
exporters:
otlphttp/prometheus:
endpoint: http://prometheus:9090/api/v1/otlp
timeout: 5s
sending_queue:
enabled: true
num_consumers: 2
queue_size: 200
retry_on_failure:
enabled: true
initial_interval: 1s
max_interval: 10s
max_elapsed_time: 60s
otlphttp/loki:
endpoint: http://loki:3100/otlp
timeout: 5s
sending_queue:
enabled: true
num_consumers: 2
queue_size: 200
retry_on_failure:
enabled: true
initial_interval: 1s
max_interval: 10s
max_elapsed_time: 60s
otlphttp/tempo:
endpoint: http://tempo:4318
timeout: 5s
sending_queue:
enabled: true
num_consumers: 2
queue_size: 200
retry_on_failure:
enabled: true
initial_interval: 1s
max_interval: 10s
max_elapsed_time: 60s
service:
extensions: [health_check]
telemetry:
logs:
level: warn
metrics:
level: normal
readers:
- pull:
exporter:
prometheus:
host: 0.0.0.0
port: 8888
pipelines:
metrics:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/prometheus]
logs:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/loki]
traces:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/tempo]
+23
View File
@@ -0,0 +1,23 @@
global:
scrape_interval: 15s
evaluation_interval: 15s
otlp:
promote_resource_attributes:
- deployment.environment.name
- service.version
storage:
tsdb:
out_of_order_time_window: 30m
rule_files:
- /etc/prometheus/rules/*.yml
scrape_configs:
- job_name: otel-collector
static_configs:
- targets: ["otel-collector:8888"]
- job_name: prometheus
static_configs:
- targets: ["localhost:9090"]
+77
View File
@@ -0,0 +1,77 @@
groups:
- name: messenger-sli
interval: 15s
rules:
- record: messenger:http_requests:rate5m
expr: sum(rate(http_server_requests_total{job="messenger-api", http_route!~"/health.*"}[5m]))
- record: messenger:http_errors:rate5m
expr: sum(rate(http_server_requests_total{job="messenger-api", http_route!~"/health.*", http_response_status_class="5xx"}[5m])) or vector(0)
- record: messenger:http_error_ratio:rate5m
expr: messenger:http_errors:rate5m / messenger:http_requests:rate5m
- record: messenger:http_error_ratio:rate1h
expr: |
(sum(rate(http_server_requests_total{job="messenger-api", http_route!~"/health.*", http_response_status_class="5xx"}[1h])) or vector(0))
/ sum(rate(http_server_requests_total{job="messenger-api", http_route!~"/health.*"}[1h]))
- record: messenger:http_latency_p95:rate5m
expr: histogram_quantile(0.95, sum by (le) (rate(http_server_request_duration_seconds_bucket{job="messenger-api", http_route!~"/health.*"}[5m])))
- record: messenger:http_fast_ratio:rate1h
expr: |
sum(rate(http_server_request_duration_seconds_bucket{job="messenger-api", http_route!~"/health.*", le="0.5"}[1h]))
/ sum(rate(http_server_request_duration_seconds_count{job="messenger-api", http_route!~"/health.*"}[1h]))
- record: messenger:time_to_ready_fast_ratio:rate1h
expr: |
sum(rate(attachments_time_to_ready_seconds_bucket{job="messenger-worker", le="10"}[1h]))
/ sum(rate(attachments_time_to_ready_seconds_count{job="messenger-worker"}[1h]))
- record: messenger:jobs_queue_oldest_age:max
expr: max(jobs_queue_oldest_age_seconds{job="messenger-relay"})
- name: messenger-alerts
rules:
- alert: MessengerHighErrorRate
expr: messenger:http_error_ratio:rate5m > 0.05 and messenger:http_requests:rate5m > 0.05
for: 1m
labels:
severity: critical
annotations:
summary: "Доля 5xx бизнес-запросов API {{ $value | humanizePercentage }} за 5 минут (порог 5%)"
description: "SLO доступности 99% за 1 ч: при 5% ошибок бюджет сгорает в 5 раз быстрее допустимого. Health-запросы не учитываются."
runbook: "docs/runbooks.md#messengerhigherrorrate"
- alert: MessengerApiNotReady
expr: sum(increase(http_server_requests_total{job="messenger-api", http_route="/health/core", http_response_status_class="5xx"}[1m])) > 10
for: 30s
labels:
severity: critical
annotations:
summary: "Реплики API отвечают 503 на /health/core — HAProxy выводит их из балансировки"
description: "Обычно недоступна БД или реплика останавливается. Пока все реплики не готовы, клиенты получают 503 от LB, и такие ответы не попадают в метрики API (MessengerHighErrorRate их не видит)."
runbook: "docs/runbooks.md#messengerapinotready"
- alert: MessengerJobQueueLag
expr: messenger:jobs_queue_oldest_age:max > 60
for: 1m
labels:
severity: warning
annotations:
summary: "Старейшее готовое к обработке задание ждёт {{ $value | humanizeDuration }} (порог 60 s)"
description: "Задания принимаются, но не обрабатываются: worker остановлен, не справляется или не получает сообщения из брокера."
runbook: "docs/runbooks.md#messengerjobqueuelag"
- alert: MessengerSlowApi
expr: messenger:http_latency_p95:rate5m > 1 and messenger:http_requests:rate5m > 0.05
for: 2m
labels:
severity: warning
annotations:
summary: "p95 бизнес-запросов API {{ $value | humanizeDuration }} за 5 минут (порог 1 s)"
description: "Проверьте ожидание пула БД (db_client_connection_pending_requests) и spans prisma:engine:db_query в трассах медленных запросов."
runbook: "docs/runbooks.md#messengerslowapi"
- alert: TelemetryExportFailing
expr: |
sum(rate(otelcol_exporter_send_failed_spans[2m])) + sum(rate(otelcol_exporter_send_failed_log_records[2m]))
+ sum(rate(otelcol_exporter_send_failed_metric_points[2m])) > 0
or max(otelcol_exporter_queue_size / otelcol_exporter_queue_capacity) > 0.5
for: 1m
labels:
severity: warning
annotations:
summary: "Collector не может доставить телеметрию в хранилище"
description: "Растёт очередь exporter или есть отказы отправки. Мессенджер работает, но часть трасс/логов теряется после заполнения ограниченной очереди."
runbook: "docs/runbooks.md#telemetryexportfailing"
+31
View File
@@ -0,0 +1,31 @@
stream_over_http_enabled: true
server:
http_listen_address: 0.0.0.0
http_listen_port: 3200
log_level: warn
distributor:
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
ingester:
max_block_duration: 5m
compactor:
compaction:
block_retention: 168h
storage:
trace:
backend: local
wal:
path: /var/tempo/wal
local:
path: /var/tempo/blocks
usage_report:
reporting_enabled: false
+8
View File
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
+13078
View File
File diff suppressed because it is too large Load Diff
+69
View File
@@ -0,0 +1,69 @@
{
"name": "mtusi-messenger-lab6",
"version": "1.0.0",
"description": "Lab 6: messenger API with S3 attachments and background image processing (NestJS + Fastify)",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"start": "node dist/main.js",
"start:dev": "nest start --watch",
"lint": "eslint \"{src,test}/**/*.ts\"",
"test": "jest --config ./test/jest-e2e.json",
"test:watch": "jest --config ./test/jest-e2e.json --watch"
},
"dependencies": {
"@aws-sdk/client-s3": "3.700.0",
"@fastify/static": "7.0.4",
"@nestjs/common": "^10.4.15",
"@nestjs/core": "^10.4.15",
"@nestjs/platform-fastify": "^10.4.15",
"@nestjs/swagger": "7.4.2",
"@opentelemetry/api": "1.9.0",
"@opentelemetry/api-logs": "0.57.2",
"@opentelemetry/core": "1.30.1",
"@opentelemetry/exporter-logs-otlp-proto": "0.57.2",
"@opentelemetry/exporter-metrics-otlp-proto": "0.57.2",
"@opentelemetry/exporter-trace-otlp-proto": "0.57.2",
"@opentelemetry/instrumentation-aws-sdk": "0.49.1",
"@opentelemetry/instrumentation-http": "0.57.2",
"@opentelemetry/resources": "1.30.1",
"@opentelemetry/sdk-logs": "0.57.2",
"@opentelemetry/sdk-metrics": "1.30.1",
"@opentelemetry/sdk-node": "0.57.2",
"@opentelemetry/sdk-trace-base": "1.30.1",
"@opentelemetry/semantic-conventions": "1.28.0",
"@prisma/client": "6.1.0",
"@prisma/instrumentation": "6.1.0",
"amqplib": "0.10.5",
"argon2": "0.41.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"ioredis": "5.4.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"sharp": "0.33.5"
},
"devDependencies": {
"@nestjs/cli": "^10.4.9",
"@nestjs/schematics": "^10.2.3",
"@nestjs/testing": "^10.4.15",
"@types/amqplib": "0.10.6",
"@types/jest": "^29.5.14",
"@types/node": "^20.17.9",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^8.18.1",
"@typescript-eslint/parser": "^8.18.1",
"ajv": "8.12.0",
"eslint": "^8.57.1",
"jest": "^29.7.0",
"prisma": "6.1.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.7.2"
}
}
@@ -0,0 +1,50 @@
CREATE TABLE "users" (
"id" UUID NOT NULL,
"username" VARCHAR(32) NOT NULL,
"display_name" VARCHAR(100) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "users_pkey" PRIMARY KEY ("id"),
CONSTRAINT "users_username_format" CHECK ("username" ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT "users_display_name_not_blank" CHECK (btrim("display_name") <> '')
);
CREATE TABLE "chats" (
"id" UUID NOT NULL,
"title" VARCHAR(100) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "chats_pkey" PRIMARY KEY ("id"),
CONSTRAINT "chats_title_not_blank" CHECK (btrim("title") <> '')
);
CREATE TABLE "chat_members" (
"chat_id" UUID NOT NULL,
"user_id" UUID NOT NULL,
CONSTRAINT "chat_members_pkey" PRIMARY KEY ("chat_id","user_id")
);
CREATE TABLE "messages" (
"id" UUID NOT NULL,
"chat_id" UUID NOT NULL,
"sender_id" UUID NOT NULL,
"text" VARCHAR(4000) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
CREATE INDEX "chat_members_user_id_idx" ON "chat_members"("user_id");
CREATE INDEX "messages_chat_id_created_at_id_idx" ON "messages"("chat_id", "created_at", "id");
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "messages" ADD CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,14 @@
ALTER TABLE "users" ADD COLUMN "password_hash" TEXT NOT NULL;
CREATE TABLE "sessions" (
"token_hash" CHAR(64) NOT NULL,
"user_id" UUID NOT NULL,
"expires_at" TIMESTAMPTZ(3) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "sessions_pkey" PRIMARY KEY ("token_hash")
);
CREATE INDEX "sessions_user_id_idx" ON "sessions"("user_id");
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,13 @@
CREATE TABLE "idempotency_keys" (
"user_id" UUID NOT NULL,
"chat_id" UUID NOT NULL,
"key" VARCHAR(128) NOT NULL,
"request_hash" CHAR(64) NOT NULL,
"message_id" UUID NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "idempotency_keys_pkey" PRIMARY KEY ("user_id","chat_id","key"),
CONSTRAINT "idempotency_keys_key_format" CHECK ("key" ~ '^[A-Za-z0-9._:-]{1,128}$')
);
ALTER TABLE "idempotency_keys" ADD CONSTRAINT "idempotency_keys_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,45 @@
CREATE TABLE "attachments" (
"id" UUID NOT NULL,
"chat_id" UUID NOT NULL,
"owner_id" UUID NOT NULL,
"filename" VARCHAR(255) NOT NULL,
"content_type" VARCHAR(32) NOT NULL,
"size_bytes" INTEGER NOT NULL,
"sha256" CHAR(64) NOT NULL,
"object_key" VARCHAR(128) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "attachments_pkey" PRIMARY KEY ("id"),
CONSTRAINT "attachments_filename_not_blank" CHECK (btrim("filename") <> ''),
CONSTRAINT "attachments_content_type_allowed" CHECK ("content_type" IN ('image/png', 'image/jpeg')),
CONSTRAINT "attachments_size_range" CHECK ("size_bytes" BETWEEN 1 AND 10485760),
CONSTRAINT "attachments_sha256_format" CHECK ("sha256" ~ '^[a-f0-9]{64}$')
);
CREATE TABLE "message_attachments" (
"message_id" UUID NOT NULL,
"position" SMALLINT NOT NULL,
"attachment_id" UUID NOT NULL,
CONSTRAINT "message_attachments_pkey" PRIMARY KEY ("message_id","position"),
CONSTRAINT "message_attachments_position_range" CHECK ("position" BETWEEN 0 AND 9)
);
CREATE UNIQUE INDEX "attachments_object_key_key" ON "attachments"("object_key");
CREATE INDEX "attachments_chat_id_idx" ON "attachments"("chat_id");
CREATE INDEX "attachments_owner_id_idx" ON "attachments"("owner_id");
CREATE UNIQUE INDEX "message_attachments_message_id_attachment_id_key" ON "message_attachments"("message_id", "attachment_id");
CREATE INDEX "message_attachments_attachment_id_idx" ON "message_attachments"("attachment_id");
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "message_attachments" ADD CONSTRAINT "message_attachments_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "message_attachments" ADD CONSTRAINT "message_attachments_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE NO ACTION ON UPDATE CASCADE;
@@ -0,0 +1,97 @@
ALTER TABLE "attachments"
ADD COLUMN "staging_key" VARCHAR(128),
ADD COLUMN "status" VARCHAR(16) NOT NULL DEFAULT 'ready',
ADD COLUMN "error_code" VARCHAR(64);
ALTER TABLE "attachments" ALTER COLUMN "status" DROP DEFAULT;
ALTER TABLE "attachments"
ADD CONSTRAINT "attachments_status_allowed" CHECK ("status" IN ('queued', 'processing', 'ready', 'failed')),
ADD CONSTRAINT "attachments_error_code_when_failed" CHECK (("status" = 'failed') = ("error_code" IS NOT NULL));
CREATE UNIQUE INDEX "attachments_staging_key_key" ON "attachments"("staging_key");
CREATE TABLE "attachment_variants" (
"attachment_id" UUID NOT NULL,
"pipeline_version" INTEGER NOT NULL,
"name" VARCHAR(16) NOT NULL,
"content_type" VARCHAR(32) NOT NULL,
"object_key" VARCHAR(128) NOT NULL,
"size_bytes" INTEGER NOT NULL,
"width" INTEGER NOT NULL,
"height" INTEGER NOT NULL,
"sha256" CHAR(64) NOT NULL,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "attachment_variants_pkey" PRIMARY KEY ("attachment_id","pipeline_version","name"),
CONSTRAINT "attachment_variants_name_allowed" CHECK ("name" IN ('thumbnail', 'crop', 'optimized')),
CONSTRAINT "attachment_variants_content_type_allowed" CHECK ("content_type" IN ('image/jpeg', 'image/png', 'image/webp')),
CONSTRAINT "attachment_variants_positive" CHECK ("pipeline_version" >= 1 AND "size_bytes" >= 1 AND "width" >= 1 AND "height" >= 1),
CONSTRAINT "attachment_variants_sha256_format" CHECK ("sha256" ~ '^[a-f0-9]{64}$')
);
CREATE TABLE "attachment_jobs" (
"id" UUID NOT NULL,
"attachment_id" UUID NOT NULL,
"pipeline_version" INTEGER NOT NULL,
"status" VARCHAR(16) NOT NULL,
"attempt" INTEGER NOT NULL,
"max_attempts" INTEGER NOT NULL,
"next_attempt_at" TIMESTAMPTZ(3) NOT NULL,
"lease_owner" UUID,
"lease_until" TIMESTAMPTZ(3),
"error_code" VARCHAR(64),
"last_error" VARCHAR(200),
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"finished_at" TIMESTAMPTZ(3),
CONSTRAINT "attachment_jobs_pkey" PRIMARY KEY ("id"),
CONSTRAINT "attachment_jobs_status_allowed" CHECK ("status" IN ('queued', 'processing', 'ready', 'failed')),
CONSTRAINT "attachment_jobs_attempt_range" CHECK ("pipeline_version" >= 1 AND "attempt" >= 1 AND "attempt" <= "max_attempts"),
CONSTRAINT "attachment_jobs_lease_when_processing" CHECK ("status" <> 'processing' OR ("lease_owner" IS NOT NULL AND "lease_until" IS NOT NULL)),
CONSTRAINT "attachment_jobs_error_code_when_failed" CHECK (("status" = 'failed') = ("error_code" IS NOT NULL))
);
CREATE TABLE "job_events" (
"id" BIGSERIAL NOT NULL,
"job_id" UUID NOT NULL,
"attempt" INTEGER NOT NULL,
"event" VARCHAR(32) NOT NULL,
"detail" VARCHAR(200),
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "job_events_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "outbox" (
"id" BIGSERIAL NOT NULL,
"job_id" UUID NOT NULL,
"payload" JSONB NOT NULL,
"available_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"published_at" TIMESTAMPTZ(3),
"publish_attempts" INTEGER NOT NULL DEFAULT 0,
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "outbox_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "attachment_variants_object_key_key" ON "attachment_variants"("object_key");
CREATE UNIQUE INDEX "attachment_jobs_attachment_id_pipeline_version_key" ON "attachment_jobs"("attachment_id", "pipeline_version");
CREATE INDEX "attachment_jobs_status_lease_until_idx" ON "attachment_jobs"("status", "lease_until");
CREATE INDEX "job_events_job_id_idx" ON "job_events"("job_id");
CREATE INDEX "outbox_published_at_available_at_idx" ON "outbox"("published_at", "available_at");
CREATE INDEX "outbox_job_id_idx" ON "outbox"("job_id");
ALTER TABLE "attachment_variants" ADD CONSTRAINT "attachment_variants_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "attachment_jobs" ADD CONSTRAINT "attachment_jobs_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "attachments"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "job_events" ADD CONSTRAINT "job_events_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "attachment_jobs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "outbox" ADD CONSTRAINT "outbox_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "attachment_jobs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+3
View File
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
+191
View File
@@ -0,0 +1,191 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["metrics"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(uuid()) @db.Uuid
username String @unique @db.VarChar(32)
displayName String @map("display_name") @db.VarChar(100)
passwordHash String @map("password_hash")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
memberships ChatMember[]
messages Message[]
sessions Session[]
attachments Attachment[]
@@map("users")
}
model Session {
tokenHash String @id @map("token_hash") @db.Char(64)
userId String @map("user_id") @db.Uuid
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@map("sessions")
}
model Chat {
id String @id @default(uuid()) @db.Uuid
title String @db.VarChar(100)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
members ChatMember[]
messages Message[]
attachments Attachment[]
@@map("chats")
}
model ChatMember {
chatId String @map("chat_id") @db.Uuid
userId String @map("user_id") @db.Uuid
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Restrict)
@@id([chatId, userId])
@@index([userId])
@@map("chat_members")
}
model Message {
id String @id @default(uuid()) @db.Uuid
chatId String @map("chat_id") @db.Uuid
senderId String @map("sender_id") @db.Uuid
text String @db.VarChar(4000)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
sender User @relation(fields: [senderId], references: [id], onDelete: Restrict)
idempotencyKeys IdempotencyKey[]
attachments MessageAttachment[]
@@index([chatId, createdAt, id])
@@map("messages")
}
model Attachment {
id String @id @default(uuid()) @db.Uuid
chatId String @map("chat_id") @db.Uuid
ownerId String @map("owner_id") @db.Uuid
filename String @db.VarChar(255)
contentType String @map("content_type") @db.VarChar(32)
sizeBytes Int @map("size_bytes")
sha256 String @db.Char(64)
objectKey String @unique @map("object_key") @db.VarChar(128)
stagingKey String? @unique @map("staging_key") @db.VarChar(128)
status String @db.VarChar(16)
errorCode String? @map("error_code") @db.VarChar(64)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3)
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict)
messages MessageAttachment[]
variants AttachmentVariant[]
jobs AttachmentJob[]
@@index([chatId])
@@index([ownerId])
@@map("attachments")
}
model AttachmentVariant {
attachmentId String @map("attachment_id") @db.Uuid
pipelineVersion Int @map("pipeline_version")
name String @db.VarChar(16)
contentType String @map("content_type") @db.VarChar(32)
objectKey String @unique @map("object_key") @db.VarChar(128)
sizeBytes Int @map("size_bytes")
width Int
height Int
sha256 String @db.Char(64)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: Cascade)
@@id([attachmentId, pipelineVersion, name])
@@map("attachment_variants")
}
model AttachmentJob {
id String @id @db.Uuid
attachmentId String @map("attachment_id") @db.Uuid
pipelineVersion Int @map("pipeline_version")
status String @db.VarChar(16)
attempt Int
maxAttempts Int @map("max_attempts")
nextAttemptAt DateTime @map("next_attempt_at") @db.Timestamptz(3)
leaseOwner String? @map("lease_owner") @db.Uuid
leaseUntil DateTime? @map("lease_until") @db.Timestamptz(3)
errorCode String? @map("error_code") @db.VarChar(64)
lastError String? @map("last_error") @db.VarChar(200)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3)
finishedAt DateTime? @map("finished_at") @db.Timestamptz(3)
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: Cascade)
events JobEvent[]
outbox OutboxEntry[]
@@unique([attachmentId, pipelineVersion])
@@index([status, leaseUntil])
@@map("attachment_jobs")
}
model JobEvent {
id BigInt @id @default(autoincrement())
jobId String @map("job_id") @db.Uuid
attempt Int
event String @db.VarChar(32)
detail String? @db.VarChar(200)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
job AttachmentJob @relation(fields: [jobId], references: [id], onDelete: Cascade)
@@index([jobId])
@@map("job_events")
}
model OutboxEntry {
id BigInt @id @default(autoincrement())
jobId String @map("job_id") @db.Uuid
payload Json
availableAt DateTime @default(now()) @map("available_at") @db.Timestamptz(3)
publishedAt DateTime? @map("published_at") @db.Timestamptz(3)
publishAttempts Int @default(0) @map("publish_attempts")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
job AttachmentJob @relation(fields: [jobId], references: [id], onDelete: Cascade)
@@index([publishedAt, availableAt])
@@index([jobId])
@@map("outbox")
}
model MessageAttachment {
messageId String @map("message_id") @db.Uuid
position Int @db.SmallInt
attachmentId String @map("attachment_id") @db.Uuid
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: NoAction)
@@id([messageId, position])
@@unique([messageId, attachmentId])
@@index([attachmentId])
@@map("message_attachments")
}
model IdempotencyKey {
userId String @map("user_id") @db.Uuid
chatId String @map("chat_id") @db.Uuid
key String @db.VarChar(128)
requestHash String @map("request_hash") @db.Char(64)
messageId String @map("message_id") @db.Uuid
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
message Message @relation(fields: [messageId], references: [id], onDelete: Cascade)
@@id([userId, chatId, key])
@@map("idempotency_keys")
}
+25
View File
@@ -0,0 +1,25 @@
import { Module } from '@nestjs/common';
import { AttachmentsModule } from './attachments/attachments.module';
import { AuthModule } from './auth/auth.module';
import { CacheModule } from './cache/cache.module';
import { ChatsModule } from './chats/chats.module';
import { DbModule } from './db/db.module';
import { HealthModule } from './health/health.module';
import { MessagesModule } from './messages/messages.module';
import { StorageModule } from './storage/storage.module';
import { UsersModule } from './users/users.module';
@Module({
imports: [
DbModule,
CacheModule,
StorageModule,
AuthModule,
HealthModule,
UsersModule,
ChatsModule,
MessagesModule,
AttachmentsModule,
],
})
export class AppModule {}
+79
View File
@@ -0,0 +1,79 @@
import {
Controller,
Get,
HttpCode,
Param,
Post,
Query,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
import { Readable } from 'stream';
import { AuthGuard } from '../auth/auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { User } from '../common/domain';
import { AttachmentsService } from './attachments.service';
import {
AttachmentResponse,
toAttachmentResponse,
} from './dto/attachment.response';
@Controller()
@UseGuards(AuthGuard)
export class AttachmentsController {
constructor(private readonly attachments: AttachmentsService) {}
@Post('api/v1/chats/:chatId/attachments')
@HttpCode(202)
async upload(
@Param('chatId') chatId: string,
@Query('filename') filename: unknown,
@Req() request: FastifyRequest,
@CurrentUser() user: User,
): Promise<AttachmentResponse> {
const attachment = await this.attachments.upload({
chatId,
ownerId: user.id,
filename,
contentType: request.uploadContentType,
contentLength: request.headers['content-length'],
body: request.body as Readable | undefined,
});
return toAttachmentResponse(attachment);
}
@Get('api/v1/attachments/:attachmentId')
async metadata(
@Param('attachmentId') attachmentId: string,
@CurrentUser() user: User,
): Promise<AttachmentResponse> {
return toAttachmentResponse(
await this.attachments.getVisible(attachmentId, user.id),
);
}
@Get('api/v1/attachments/:attachmentId/content')
async content(
@Param('attachmentId') attachmentId: string,
@Query('variant') variant: unknown,
@CurrentUser() user: User,
@Res() reply: FastifyReply,
): Promise<void> {
const download = await this.attachments.openContent(
attachmentId,
user.id,
variant,
);
await reply
.header('content-type', download.contentType)
.header('content-length', download.sizeBytes)
.header('x-content-type-options', 'nosniff')
.header(
'content-disposition',
`inline; filename*=UTF-8''${encodeURIComponent(download.filename)}`,
)
.send(download.content);
}
}
+9
View File
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AttachmentsController } from './attachments.controller';
import { AttachmentsService } from './attachments.service';
@Module({
controllers: [AttachmentsController],
providers: [AttachmentsService],
})
export class AttachmentsModule {}
+244
View File
@@ -0,0 +1,244 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Attachment, AttachmentVariant, Prisma } from '@prisma/client';
import { randomUUID } from 'crypto';
import { mkdtemp, readdir, rm, stat } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { Readable } from 'stream';
import { assertChatMember } from '../chats/chat-access';
import { codePointLength } from '../common/dto/code-point-length.validator';
import { ApiError } from '../common/filters/api-error';
import { validate as isUuid } from '../common/uuid';
import { isDatabaseUnavailable, PrismaService } from '../db/prisma.service';
import { PIPELINE_VERSION } from '../jobs/job-message';
import { enqueueJob } from '../jobs/job-store';
import { S3Service } from '../storage/s3.service';
import { assertValidImage, isSupportedContentType } from './image-validation';
import { receiveBody } from './upload-body';
const TEMP_PREFIX = 'messenger-upload-';
const STALE_TEMP_MS = 60 * 60 * 1000;
const MAX_FILENAME_CODE_POINTS = 255;
const FORBIDDEN_FILENAME = /[/\\\p{Cc}\p{Zl}\p{Zp}]/u;
const KNOWN_VARIANTS = new Set(['original', 'thumbnail', 'crop', 'optimized']);
export type AttachmentWithVariants = Attachment & { variants: AttachmentVariant[] };
export interface DownloadableContent {
filename: string;
contentType: string;
sizeBytes: number;
content: Readable;
}
export interface UploadInput {
chatId: string;
ownerId: string;
filename: unknown;
contentType: string | undefined;
contentLength: string | undefined;
body: Readable | undefined;
}
@Injectable()
export class AttachmentsService implements OnModuleInit {
private readonly logger = new Logger('AttachmentsService');
private readonly maxBytes = Number(process.env.MAX_UPLOAD_BYTES ?? 10485760);
private readonly maxPixels = Number(process.env.MAX_IMAGE_PIXELS ?? 40000000);
private readonly maxConcurrent = Number(process.env.MAX_CONCURRENT_UPLOADS ?? 16);
private readonly bodyTimeoutMs = Number(process.env.UPLOAD_BODY_TIMEOUT_MS ?? 60000);
private readonly tempRoot = process.env.UPLOAD_TMP_DIR ?? tmpdir();
private activeUploads = 0;
constructor(
private readonly prisma: PrismaService,
private readonly s3: S3Service,
) {}
async onModuleInit(): Promise<void> {
await this.removeStaleTempDirs();
}
async upload(input: UploadInput): Promise<AttachmentWithVariants> {
await assertChatMember(this.prisma, input.chatId, input.ownerId);
const filename = parseFilename(input.filename);
const contentType = parseContentType(input.contentType);
if (Number(input.contentLength) > this.maxBytes) {
throw new ApiError('payload_too_large', 'attachment exceeds size limit');
}
if (this.activeUploads >= this.maxConcurrent) {
throw new ApiError('unavailable', 'too many concurrent uploads');
}
this.activeUploads++;
const dir = await mkdtemp(join(this.tempRoot, TEMP_PREFIX));
try {
return await this.store(input, filename, contentType, join(dir, 'body'));
} finally {
this.activeUploads--;
await rm(dir, { recursive: true, force: true });
}
}
async getVisible(
attachmentId: string,
userId: string,
): Promise<AttachmentWithVariants> {
if (!isUuid(attachmentId)) {
throw new ApiError('invalid_request', 'attachmentId must be a valid UUID');
}
const row = await this.prisma.attachment.findFirst({
where: {
id: attachmentId,
chat: { members: { some: { userId } } },
},
include: { variants: { where: { pipelineVersion: PIPELINE_VERSION } } },
});
if (!row) {
throw new ApiError('not_found', 'attachment not found');
}
return row;
}
async openContent(
attachmentId: string,
userId: string,
variant: unknown,
): Promise<DownloadableContent> {
const attachment = await this.getVisible(attachmentId, userId);
const name = parseVariant(variant);
if (attachment.status !== 'ready') {
throw new ApiError('conflict', `attachment is ${attachment.status}`);
}
if (name === 'original') {
return {
filename: attachment.filename,
contentType: attachment.contentType,
sizeBytes: attachment.sizeBytes,
content: await this.s3.getStream(attachment.objectKey),
};
}
const stored = attachment.variants.find((v) => v.name === name);
if (!stored) {
throw new ApiError('not_found', 'attachment variant not found');
}
return {
filename: attachment.filename,
contentType: stored.contentType,
sizeBytes: stored.sizeBytes,
content: await this.s3.getStream(stored.objectKey),
};
}
private async store(
input: UploadInput,
filename: string,
contentType: string,
path: string,
): Promise<AttachmentWithVariants> {
if (!input.body) {
throw new ApiError('invalid_request', 'request body must not be empty');
}
const { size, sha256 } = await receiveBody(
input.body,
path,
this.maxBytes,
this.bodyTimeoutMs,
);
if (size === 0) {
throw new ApiError('invalid_request', 'request body must not be empty');
}
await assertValidImage(path, size, contentType, this.maxPixels);
const id = randomUUID();
const stagingKey = `staging/${id}`;
await this.s3.putFile({ key: stagingKey, path, size, contentType, sha256 });
const now = new Date();
try {
return await this.prisma.$transaction(async (tx) => {
const attachment = await tx.attachment.create({
data: {
id,
chatId: input.chatId,
ownerId: input.ownerId,
filename,
contentType,
sizeBytes: size,
sha256,
objectKey: `attachments/${id}/original`,
stagingKey,
status: 'queued',
createdAt: now,
updatedAt: now,
},
});
await enqueueJob(tx, id, now);
return { ...attachment, variants: [] };
});
} catch (error) {
if (isDefiniteRejection(error)) {
await this.s3.delete(stagingKey).catch(() => {
this.logger.warn(`compensation failed for ${stagingKey}`);
});
}
if (error instanceof Prisma.PrismaClientUnknownRequestError) {
this.logger.error(`metadata insert failed for ${stagingKey}`);
throw new ApiError('unavailable', 'database unavailable');
}
throw error;
}
}
private async removeStaleTempDirs(): Promise<void> {
const root = this.tempRoot;
const names = await readdir(root);
for (const name of names.filter((n) => n.startsWith(TEMP_PREFIX))) {
const path = join(root, name);
const info = await stat(path).catch(() => null);
if (info && Date.now() - info.mtimeMs > STALE_TEMP_MS) {
await rm(path, { recursive: true, force: true });
}
}
}
}
function parseVariant(raw: unknown): string {
if (raw === undefined) {
return 'original';
}
if (typeof raw === 'string' && KNOWN_VARIANTS.has(raw)) {
return raw;
}
throw new ApiError('invalid_request', 'unknown attachment variant');
}
function isDefiniteRejection(error: unknown): boolean {
return (
error instanceof Prisma.PrismaClientKnownRequestError &&
!isDatabaseUnavailable(error)
);
}
function parseFilename(raw: unknown): string {
if (
typeof raw !== 'string' ||
raw.trim() === '' ||
codePointLength(raw) > MAX_FILENAME_CODE_POINTS ||
FORBIDDEN_FILENAME.test(raw)
) {
throw new ApiError(
'invalid_request',
'filename must be 1-255 characters without path separators or control characters',
);
}
return raw;
}
function parseContentType(raw: string | undefined): string {
const contentType = raw?.split(';')[0].trim().toLowerCase() ?? '';
if (!isSupportedContentType(contentType)) {
throw new ApiError(
'unsupported_media_type',
'Content-Type must be image/png or image/jpeg',
);
}
return contentType;
}
@@ -0,0 +1,55 @@
import { Attachment, AttachmentVariant } from '@prisma/client';
const VARIANT_ORDER = ['thumbnail', 'crop', 'optimized'];
export interface VariantResponse {
name: string;
content_type: string;
size_bytes: number;
width: number;
height: number;
sha256: string;
}
export interface AttachmentResponse {
id: string;
chat_id: string;
owner_id: string;
filename: string;
content_type: string;
size_bytes: number;
sha256: string;
status: string;
variants: VariantResponse[];
error_code: string | null;
created_at: string;
updated_at: string;
}
export function toAttachmentResponse(
row: Attachment & { variants: AttachmentVariant[] },
): AttachmentResponse {
const variants = row.status === 'ready' ? [...row.variants] : [];
variants.sort((a, b) => VARIANT_ORDER.indexOf(a.name) - VARIANT_ORDER.indexOf(b.name));
return {
id: row.id,
chat_id: row.chatId,
owner_id: row.ownerId,
filename: row.filename,
content_type: row.contentType,
size_bytes: row.sizeBytes,
sha256: row.sha256,
status: row.status,
variants: variants.map((variant) => ({
name: variant.name,
content_type: variant.contentType,
size_bytes: variant.sizeBytes,
width: variant.width,
height: variant.height,
sha256: variant.sha256,
})),
error_code: row.errorCode,
created_at: row.createdAt.toISOString(),
updated_at: row.updatedAt.toISOString(),
};
}
+105
View File
@@ -0,0 +1,105 @@
import { open } from 'fs/promises';
import sharp from 'sharp';
import { ApiError } from '../common/filters/api-error';
sharp.cache(false);
sharp.concurrency(1);
const PNG_SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');
const JPEG_SIGNATURE = Buffer.from('ffd8ff', 'hex');
const PNG_END_CHUNK = Buffer.from('0000000049454e44ae426082', 'hex');
const PNG_TAIL_BYTES = 64 * 1024;
const MAX_CONCURRENT_DECODES = 2;
const FORMAT_BY_CONTENT_TYPE: Record<string, string> = {
'image/png': 'png',
'image/jpeg': 'jpeg',
};
let activeDecodes = 0;
const decodeQueue: (() => void)[] = [];
export function isSupportedContentType(contentType: string): boolean {
return contentType in FORMAT_BY_CONTENT_TYPE;
}
export async function assertValidImage(
path: string,
size: number,
contentType: string,
maxPixels: number,
): Promise<void> {
const invalid = new ApiError(
'unsupported_media_type',
'file is not a valid image of the declared type',
);
const format = FORMAT_BY_CONTENT_TYPE[contentType];
if (!(await hasExpectedFraming(path, size, format))) {
throw invalid;
}
const { width, height, format: actual } = await sharp(path, {
limitInputPixels: false,
})
.metadata()
.catch(() => {
throw invalid;
});
if (!width || !height || actual !== format) {
throw invalid;
}
if (width * height > maxPixels) {
throw new ApiError('payload_too_large', 'image exceeds pixel limit');
}
await acquireDecodeSlot();
try {
await sharp(path, {
failOn: 'warning',
sequentialRead: true,
limitInputPixels: maxPixels,
}).stats();
} catch {
throw invalid;
} finally {
releaseDecodeSlot();
}
}
async function hasExpectedFraming(
path: string,
size: number,
format: string,
): Promise<boolean> {
const file = await open(path, 'r');
try {
const head = Buffer.alloc(PNG_SIGNATURE.length);
await file.read(head, 0, head.length, 0);
if (format === 'jpeg') {
return head.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE);
}
if (!head.equals(PNG_SIGNATURE)) {
return false;
}
const tail = Buffer.alloc(Math.min(size, PNG_TAIL_BYTES));
await file.read(tail, 0, tail.length, size - tail.length);
return tail.includes(PNG_END_CHUNK);
} finally {
await file.close();
}
}
function acquireDecodeSlot(): Promise<void> {
if (activeDecodes < MAX_CONCURRENT_DECODES) {
activeDecodes++;
return Promise.resolve();
}
return new Promise((resolve) => decodeQueue.push(resolve));
}
function releaseDecodeSlot(): void {
const next = decodeQueue.shift();
if (next) {
next();
} else {
activeDecodes--;
}
}
+163
View File
@@ -0,0 +1,163 @@
import { PrismaClient } from '@prisma/client';
import { S3Service } from '../storage/s3.service';
const MAX_REPORTED_KEYS = 20;
const DEFAULT_PREFIXES = ['attachments/', 'staging/'];
type ReconcilePrisma = Pick<PrismaClient, 'attachment' | 'attachmentVariant'>;
export interface ReconcileOptions {
apply: boolean;
graceMs: number;
stagingRetentionMs?: number;
prefix?: string;
now?: Date;
pageSize?: number;
}
export interface ReconcileReport {
mode: 'apply' | 'dry-run';
grace_seconds: number;
staging_expired: number;
scanned: number;
fresh_skipped: number;
referenced: number;
orphans: number;
deleted: number;
failed: number;
orphan_keys: string[];
duration_ms: number;
}
export async function reconcile(
prisma: ReconcilePrisma,
s3: S3Service,
options: ReconcileOptions,
): Promise<ReconcileReport> {
const started = Date.now();
const cutoff = (options.now ?? new Date()).getTime() - options.graceMs;
const report: ReconcileReport = {
mode: options.apply ? 'apply' : 'dry-run',
grace_seconds: options.graceMs / 1000,
staging_expired: 0,
scanned: 0,
fresh_skipped: 0,
referenced: 0,
orphans: 0,
deleted: 0,
failed: 0,
orphan_keys: [],
duration_ms: 0,
};
if (options.stagingRetentionMs !== undefined) {
report.staging_expired = await expireFailedStaging(
prisma,
s3,
(options.now ?? new Date()).getTime() - options.stagingRetentionMs,
options.apply,
options.prefix,
);
}
for (const prefix of options.prefix ? [options.prefix] : DEFAULT_PREFIXES) {
await reconcilePrefix(prisma, s3, prefix, cutoff, options, report);
}
report.duration_ms = Date.now() - started;
return report;
}
async function expireFailedStaging(
prisma: ReconcilePrisma,
s3: S3Service,
before: number,
apply: boolean,
prefix?: string,
): Promise<number> {
const rows = await prisma.attachment.findMany({
where: {
stagingKey: { not: null, startsWith: prefix },
OR: [{ status: 'ready' }, { status: 'failed', updatedAt: { lt: new Date(before) } }],
},
select: { id: true, stagingKey: true },
take: 500,
});
if (!apply) {
return rows.length;
}
let expired = 0;
for (const row of rows) {
const released = await prisma.attachment.updateMany({
where: {
id: row.id,
stagingKey: row.stagingKey,
OR: [{ status: 'ready' }, { status: 'failed', updatedAt: { lt: new Date(before) } }],
},
data: { stagingKey: null },
});
if (released.count === 0) {
continue;
}
await s3.delete(row.stagingKey!).then(
() => expired++,
() => undefined,
);
}
return expired;
}
function attachmentIdOf(key: string): string | null {
const match = /^attachments\/([0-9a-f-]{36})(\/|$)/.exec(key);
return match ? match[1] : null;
}
async function reconcilePrefix(
prisma: ReconcilePrisma,
s3: S3Service,
prefix: string,
cutoff: number,
options: ReconcileOptions,
report: ReconcileReport,
): Promise<void> {
let token: string | undefined;
do {
const page = await s3.listObjects(prefix, options.pageSize ?? 500, token);
token = page.nextToken;
report.scanned += page.objects.length;
const old = page.objects.filter((object) => object.lastModified.getTime() < cutoff);
report.fresh_skipped += page.objects.length - old.length;
const keys = old.map((object) => object.key);
const rows = await prisma.attachment.findMany({
where: { OR: [{ objectKey: { in: keys } }, { stagingKey: { in: keys } }] },
select: { objectKey: true, stagingKey: true },
});
const variants = await prisma.attachmentVariant.findMany({
where: { objectKey: { in: keys } },
select: { objectKey: true },
});
const ids = [...new Set(keys.map(attachmentIdOf).filter((id): id is string => id !== null))];
const owners = ids.length
? await prisma.attachment.findMany({ where: { id: { in: ids } }, select: { id: true } })
: [];
const owned = new Set(owners.map((row) => row.id));
const known = new Set([
...rows.flatMap((row) => [row.objectKey, row.stagingKey]),
...variants.map((row) => row.objectKey),
]);
for (const { key } of old) {
const owner = attachmentIdOf(key);
if (known.has(key) || (owner !== null && owned.has(owner))) {
report.referenced++;
continue;
}
report.orphans++;
if (report.orphan_keys.length < MAX_REPORTED_KEYS) {
report.orphan_keys.push(key);
}
if (options.apply) {
await s3.delete(key).then(
() => report.deleted++,
() => report.failed++,
);
}
}
} while (token);
}
+98
View File
@@ -0,0 +1,98 @@
import { createHash } from 'crypto';
import { FastifyInstance, FastifyRequest } from 'fastify';
import { createWriteStream } from 'fs';
import { Readable } from 'stream';
import { ApiError } from '../common/filters/api-error';
const UPLOAD_ROUTE = '/api/v1/chats/:chatId/attachments';
const PASS_THROUGH_TYPE = 'application/x-messenger-upload';
declare module 'fastify' {
interface FastifyRequest {
uploadContentType?: string;
}
}
export function registerUploadBody(instance: FastifyInstance): void {
instance.addContentTypeParser(PASS_THROUGH_TYPE, (_request, payload, done) => {
done(null, payload);
});
instance.addHook('onRequest', (request: FastifyRequest, _reply, done) => {
if (request.method === 'POST' && request.routeOptions.url === UPLOAD_ROUTE) {
request.uploadContentType = request.headers['content-type'];
request.headers['content-type'] = PASS_THROUGH_TYPE;
}
done();
});
}
export interface ReceivedBody {
size: number;
sha256: string;
}
export function receiveBody(
body: Readable,
path: string,
maxBytes: number,
timeoutMs: number,
): Promise<ReceivedBody> {
return new Promise((resolve, reject) => {
const hash = createHash('sha256');
const out = createWriteStream(path, { flags: 'wx', mode: 0o600 });
let size = 0;
let settled = false;
const timer = setTimeout(() => body.destroy(), timeoutMs);
const settle = (done: () => void): void => {
if (settled) {
return;
}
settled = true;
clearTimeout(timer);
body.off('data', onData);
body.off('end', onEnd);
body.off('close', onClose);
done();
};
const fail = (error: ApiError): void => {
settle(() => {
out.destroy();
reject(error);
});
};
const onData = (chunk: Buffer): void => {
size += chunk.length;
if (size > maxBytes) {
fail(new ApiError('payload_too_large', 'attachment exceeds size limit'));
return;
}
hash.update(chunk);
if (!out.write(chunk)) {
body.pause();
out.once('drain', () => {
if (!settled) {
body.resume();
}
});
}
};
const onEnd = (): void => {
settle(() => {
out.end(() =>
resolve({ size, sha256: hash.digest('hex') }),
);
});
};
const onClose = (): void => {
fail(new ApiError('invalid_request', 'upload was interrupted'));
};
out.on('error', () => {
fail(new ApiError('unavailable', 'temporary storage unavailable'));
});
body.on('data', onData);
body.on('end', onEnd);
body.on('close', onClose);
});
}
+51
View File
@@ -0,0 +1,51 @@
import {
Controller,
Delete,
Headers,
HttpCode,
Post,
Req,
UseGuards,
} from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import { ApiError } from '../common/filters/api-error';
import { toUserResponse, UserResponse } from '../users/dto/user.response';
import { AuthGuard } from './auth.guard';
import { AuthService } from './auth.service';
interface SessionResponse {
token: string;
token_type: 'Bearer';
expires_at: string;
user: UserResponse;
}
@Controller('api/v1/auth/sessions')
export class AuthController {
constructor(private readonly auth: AuthService) {}
@Post()
@HttpCode(201)
async create(
@Headers('authorization') authorization: string | undefined,
@Req() request: FastifyRequest,
): Promise<SessionResponse> {
if (request.body !== undefined) {
throw new ApiError('invalid_request', 'request body is not allowed');
}
const session = await this.auth.login(authorization);
return {
token: session.token,
token_type: 'Bearer',
expires_at: session.expiresAt.toISOString(),
user: toUserResponse(session.user),
};
}
@Delete('current')
@HttpCode(204)
@UseGuards(AuthGuard)
async revoke(@Req() request: FastifyRequest): Promise<void> {
await this.auth.revoke(request.sessionTokenHash!);
}
}
+25
View File
@@ -0,0 +1,25 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import { ApiError } from '../common/filters/api-error';
import { AuthService } from './auth.service';
const BEARER_CHALLENGE = 'Bearer realm="messenger"';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private readonly auth: AuthService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<FastifyRequest>();
const match = /^Bearer (\S+)$/i.exec(request.headers.authorization ?? '');
const session = match ? await this.auth.resolve(match[1]) : null;
if (!session) {
throw new ApiError('unauthorized', 'missing or invalid session', {
'WWW-Authenticate': BEARER_CHALLENGE,
});
}
request.currentUser = session.user;
request.sessionTokenHash = session.tokenHash;
return true;
}
}
+12
View File
@@ -0,0 +1,12 @@
import { Global, Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthGuard } from './auth.guard';
import { AuthService } from './auth.service';
@Global()
@Module({
controllers: [AuthController],
providers: [AuthService, AuthGuard],
exports: [AuthService, AuthGuard],
})
export class AuthModule {}
+155
View File
@@ -0,0 +1,155 @@
import { Injectable } from '@nestjs/common';
import { createHash, randomBytes } from 'crypto';
import { CacheService } from '../cache/cache.service';
import { codePointLength } from '../common/dto/code-point-length.validator';
import { User } from '../common/domain';
import { ApiError } from '../common/filters/api-error';
import { PrismaService } from '../db/prisma.service';
import { toUser } from '../users/users.service';
import { verifyPassword } from './password';
const BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"';
const USERNAME_PATTERN = /^[a-z0-9_]{3,32}$/;
const CACHE_TTL_MS = 60_000;
const REVOKED = 'revoked';
interface CachedSession {
user: User;
expiresAt: number;
}
export interface CreatedSession {
token: string;
expiresAt: Date;
user: User;
}
export interface ResolvedSession {
tokenHash: string;
user: User;
}
@Injectable()
export class AuthService {
private readonly ttlSeconds = readSessionTtl();
constructor(
private readonly prisma: PrismaService,
private readonly cache: CacheService,
) {}
async login(authorization: string | undefined): Promise<CreatedSession> {
const credentials = parseBasic(authorization);
if (!credentials) {
throw invalidCredentials();
}
const row = await this.prisma.user.findUnique({
where: { username: credentials.username },
});
const valid = await verifyPassword(
row?.passwordHash ?? null,
credentials.password,
);
if (!row || !valid) {
throw invalidCredentials();
}
const token = randomBytes(32).toString('base64url');
const expiresAt = new Date(Date.now() + this.ttlSeconds * 1000);
await this.prisma.session.create({
data: { tokenHash: hashToken(token), userId: row.id, expiresAt },
});
return { token, expiresAt, user: toUser(row) };
}
async resolve(token: string): Promise<ResolvedSession | null> {
const tokenHash = hashToken(token);
const key = cacheKey(tokenHash);
const cached = await this.cache.get(key);
if (cached === REVOKED) {
return null;
}
if (cached) {
const entry = JSON.parse(cached) as CachedSession;
if (entry.expiresAt > Date.now()) {
return { tokenHash, user: entry.user };
}
}
const session = await this.prisma.session.findUnique({
where: { tokenHash },
include: { user: true },
});
const remainingMs = session
? session.expiresAt.getTime() - Date.now()
: 0;
if (!session || remainingMs <= 0) {
return null;
}
const user = toUser(session.user);
const entry: CachedSession = {
user,
expiresAt: session.expiresAt.getTime(),
};
await this.cache.set(
key,
JSON.stringify(entry),
Math.min(remainingMs, CACHE_TTL_MS),
true,
);
return { tokenHash, user };
}
async revoke(tokenHash: string): Promise<void> {
const marked = await this.cache.setStrict(
cacheKey(tokenHash),
REVOKED,
CACHE_TTL_MS,
);
if (!marked) {
throw new ApiError('unavailable', 'session cache unavailable');
}
await this.prisma.session.deleteMany({ where: { tokenHash } });
}
}
function cacheKey(tokenHash: string): string {
return `session:v1:${tokenHash}`;
}
function parseBasic(
header: string | undefined,
): { username: string; password: string } | null {
const match = /^Basic ([A-Za-z0-9+/]+={0,2})$/i.exec(header ?? '');
if (!match) {
return null;
}
const decoded = Buffer.from(match[1], 'base64').toString('utf8');
const separator = decoded.indexOf(':');
if (separator < 0) {
return null;
}
const username = decoded.slice(0, separator);
const password = decoded.slice(separator + 1);
const length = codePointLength(password);
if (!USERNAME_PATTERN.test(username) || length < 12 || length > 128) {
return null;
}
return { username, password };
}
function hashToken(token: string): string {
return createHash('sha256').update(token).digest('hex');
}
function invalidCredentials(): ApiError {
return new ApiError('unauthorized', 'invalid credentials', {
'WWW-Authenticate': BASIC_CHALLENGE,
});
}
function readSessionTtl(): number {
const ttl = Number(process.env.SESSION_TTL_SECONDS ?? 3600);
if (!Number.isInteger(ttl) || ttl <= 0) {
throw new Error('SESSION_TTL_SECONDS must be a positive integer');
}
return ttl;
}
+20
View File
@@ -0,0 +1,20 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import { User } from '../common/domain';
declare module 'fastify' {
interface FastifyRequest {
currentUser?: User;
sessionTokenHash?: string;
}
}
export const CurrentUser = createParamDecorator(
(_data: unknown, ctx: ExecutionContext): User => {
const request = ctx.switchToHttp().getRequest<FastifyRequest>();
if (!request.currentUser) {
throw new Error('CurrentUser used without AuthGuard');
}
return request.currentUser;
},
);
+17
View File
@@ -0,0 +1,17 @@
import { argon2id, hash, verify } from 'argon2';
const OPTIONS = { type: argon2id, memoryCost: 19456, timeCost: 2, parallelism: 1 };
const dummyHash = hash('timing-equalizer-password', OPTIONS);
export function hashPassword(password: string): Promise<string> {
return hash(password, OPTIONS);
}
export async function verifyPassword(
passwordHash: string | null,
password: string,
): Promise<boolean> {
const valid = await verify(passwordHash ?? (await dummyHash), password);
return passwordHash !== null && valid;
}
+57
View File
@@ -0,0 +1,57 @@
import { Logger } from '@nestjs/common';
import amqp, { Channel, Connection } from 'amqplib';
import { readFileSync } from 'fs';
export const JOB_QUEUE = 'attachment.jobs';
export const DEAD_LETTER_QUEUE = 'attachment.jobs.dlq';
const DELIVERY_LIMIT = 20;
const RECONNECT_MAX_MS = 10000;
export function brokerUrl(): string {
const file = process.env.BROKER_URL_FILE;
const url = file ? readFileSync(file, 'utf8').trim() : process.env.BROKER_URL;
if (!url) {
throw new Error('BROKER_URL_FILE or BROKER_URL must be set');
}
return url;
}
export async function assertTopology(channel: Channel): Promise<void> {
await channel.assertQueue(DEAD_LETTER_QUEUE, {
durable: true,
arguments: { 'x-queue-type': 'quorum' },
});
await channel.assertQueue(JOB_QUEUE, {
durable: true,
arguments: {
'x-queue-type': 'quorum',
'x-delivery-limit': DELIVERY_LIMIT,
'x-dead-letter-exchange': '',
'x-dead-letter-routing-key': DEAD_LETTER_QUEUE,
},
});
}
export async function connectWithRetry(
logger: Logger,
stopping: () => boolean,
): Promise<Connection | null> {
let delay = 500;
while (!stopping()) {
try {
const connection = await amqp.connect(brokerUrl(), { timeout: 5000 });
logger.log('connected to broker');
return connection;
} catch (error) {
const reason = error instanceof Error ? error.message.split(' ')[0] : 'unknown';
logger.warn(`broker unavailable (${reason}), retry in ${delay} ms`);
await sleep(delay);
delay = Math.min(RECONNECT_MAX_MS, delay * 2);
}
}
return null;
}
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
+9
View File
@@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { CacheService } from './cache.service';
@Global()
@Module({
providers: [CacheService],
exports: [CacheService],
})
export class CacheModule {}
+98
View File
@@ -0,0 +1,98 @@
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
import { readFileSync } from 'fs';
import Redis from 'ioredis';
@Injectable()
export class CacheService implements OnModuleDestroy {
private readonly logger = new Logger('CacheService');
private readonly client: Redis | null;
private trusted = false;
constructor() {
const url = readCacheUrl();
this.client = url
? new Redis(url, {
enableOfflineQueue: false,
maxRetriesPerRequest: 0,
connectTimeout: 1000,
commandTimeout: 200,
})
: null;
this.client?.on('error', (error) =>
this.logger.warn(`redis unavailable: ${error.message}`),
);
this.client?.on('close', () => {
this.trusted = false;
});
this.client?.on('ready', () => {
this.client!.flushdb().then(
() => {
this.trusted = true;
},
() => this.distrust(),
);
});
}
async get(key: string): Promise<string | null> {
if (!this.client || !this.trusted) {
return null;
}
try {
return await this.client.get(key);
} catch {
this.distrust();
return null;
}
}
async set(
key: string,
value: string,
ttlMs: number,
onlyIfAbsent = false,
): Promise<void> {
if (!this.client || !this.trusted) {
return;
}
try {
if (onlyIfAbsent) {
await this.client.set(key, value, 'PX', ttlMs, 'NX');
} else {
await this.client.set(key, value, 'PX', ttlMs);
}
} catch {
this.distrust();
}
}
async setStrict(key: string, value: string, ttlMs: number): Promise<boolean> {
if (!this.client) {
return true;
}
if (!this.trusted) {
return false;
}
try {
await this.client.set(key, value, 'PX', ttlMs);
return true;
} catch {
this.distrust();
return false;
}
}
onModuleDestroy(): void {
this.client?.disconnect();
}
private distrust(): void {
this.trusted = false;
this.client?.disconnect(true);
}
}
function readCacheUrl(): string | undefined {
const file = process.env.CACHE_URL_FILE;
return file ? readFileSync(file, 'utf8').trim() : process.env.CACHE_URL;
}
+19
View File
@@ -0,0 +1,19 @@
import { ApiError } from '../common/filters/api-error';
import { validate as isUuid } from '../common/uuid';
import { PrismaService } from '../db/prisma.service';
export async function assertChatMember(
prisma: PrismaService,
chatId: string,
userId: string,
): Promise<void> {
if (!isUuid(chatId)) {
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
}
const member = await prisma.chatMember.findUnique({
where: { chatId_userId: { chatId, userId } },
});
if (!member) {
throw new ApiError('not_found', 'chat not found');
}
}
+57
View File
@@ -0,0 +1,57 @@
import {
Body,
Controller,
Get,
HttpCode,
Param,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { parseLimit, rawCursorParam } from '../common/dto/pagination-query.dto';
import { User } from '../common/domain';
import { ChatsService } from './chats.service';
import {
ChatPageResponse,
ChatResponse,
toChatResponse,
} from './dto/chat.response';
import { CreateChatDto } from './dto/create-chat.dto';
@Controller('api/v1/chats')
@UseGuards(AuthGuard)
export class ChatsController {
constructor(private readonly chats: ChatsService) {}
@Post()
@HttpCode(201)
async create(
@Body() dto: CreateChatDto,
@CurrentUser() user: User,
): Promise<ChatResponse> {
const chat = await this.chats.create(dto.title, dto.member_ids, user.id);
return toChatResponse(chat);
}
@Get()
async list(
@CurrentUser() user: User,
@Query('limit') limitRaw: unknown,
@Query('cursor') cursorRaw: unknown,
): Promise<ChatPageResponse> {
const limit = parseLimit(limitRaw);
const cursor = rawCursorParam(cursorRaw);
return this.chats.listForMember(user.id, limit, cursor);
}
@Get(':chatId')
async getOne(
@Param('chatId') chatId: string,
@CurrentUser() user: User,
): Promise<ChatResponse> {
const chat = await this.chats.getForMember(chatId, user.id);
return toChatResponse(chat);
}
}
+10
View File
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ChatsController } from './chats.controller';
import { ChatsService } from './chats.service';
@Module({
controllers: [ChatsController],
providers: [ChatsService],
exports: [ChatsService],
})
export class ChatsModule {}
+131
View File
@@ -0,0 +1,131 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { decodeChatCursor, encodeChatCursor } from '../common/cursor';
import { Chat } from '../common/domain';
import { ApiError } from '../common/filters/api-error';
import { validate as isUuid } from '../common/uuid';
import { PrismaService, isForeignKeyViolation } from '../db/prisma.service';
import { ChatPageResponse, toChatResponse } from './dto/chat.response';
interface ChatRow {
id: string;
title: string;
createdAt: Date;
memberIds: string[];
}
@Injectable()
export class ChatsService {
constructor(private readonly prisma: PrismaService) {}
async create(
title: string,
rawMemberIds: string[],
callerId: string,
): Promise<Chat> {
const invalid = rawMemberIds.find((id) => !isUuid(id));
if (invalid) {
throw new ApiError('invalid_request', `unknown member_id: ${invalid}`);
}
const memberIds = rawMemberIds.map((id) => id.toLowerCase());
if (new Set(memberIds).size !== memberIds.length) {
throw new ApiError(
'invalid_request',
"All member_ids's elements must be unique",
);
}
if (!memberIds.includes(callerId)) {
throw new ApiError(
'invalid_request',
'member_ids must include the caller',
);
}
try {
const chat = await this.prisma.$transaction(async (tx) => {
const created = await tx.chat.create({
data: { title: title.trim() },
});
await tx.chatMember.createMany({
data: memberIds.map((userId) => ({
chatId: created.id,
userId,
})),
});
return created;
});
return toChat({ ...chat, memberIds });
} catch (error) {
if (isForeignKeyViolation(error)) {
throw new ApiError(
'invalid_request',
'member_ids contain an unknown user',
);
}
throw error;
}
}
async getForMember(chatId: string, callerId: string): Promise<Chat> {
if (!isUuid(chatId)) {
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
}
const chat = await this.prisma.chat.findFirst({
where: { id: chatId, members: { some: { userId: callerId } } },
include: { members: { select: { userId: true } } },
});
if (!chat) {
throw new ApiError('not_found', 'chat not found');
}
return toChat({
...chat,
memberIds: chat.members.map((m) => m.userId),
});
}
async listForMember(
callerId: string,
limit: number,
rawCursor: string | null,
): Promise<ChatPageResponse> {
let after = Prisma.empty;
if (rawCursor !== null) {
const cursor = decodeChatCursor(rawCursor);
const member = cursor
? await this.prisma.chatMember.findUnique({
where: {
chatId_userId: { chatId: cursor.id, userId: callerId },
},
include: { chat: { select: { createdAt: true } } },
})
: null;
if (!member) {
throw new ApiError('invalid_request', 'invalid cursor');
}
after = Prisma.sql`AND (c.created_at, c.id) > (${member.chat.createdAt}::timestamptz, ${member.chatId}::uuid)`;
}
const rows = await this.prisma.$queryRaw<ChatRow[]>`
SELECT c.id, c.title, c.created_at AS "createdAt",
ARRAY(SELECT m.user_id::text FROM chat_members m WHERE m.chat_id = c.id) AS "memberIds"
FROM chat_members cm
JOIN chats c ON c.id = cm.chat_id
WHERE cm.user_id = ${callerId}::uuid ${after}
ORDER BY c.created_at, c.id
LIMIT ${limit + 1}`;
const items = rows.slice(0, limit).map(toChat);
const last = items[items.length - 1];
return {
items: items.map(toChatResponse),
next_cursor:
rows.length > limit ? encodeChatCursor({ id: last.id }) : null,
};
}
}
function toChat(row: ChatRow): Chat {
return {
id: row.id,
title: row.title,
memberIds: row.memberIds,
createdAt: row.createdAt.toISOString(),
};
}
+22
View File
@@ -0,0 +1,22 @@
import { Chat } from '../../common/domain';
export interface ChatResponse {
id: string;
title: string;
member_ids: string[];
created_at: string;
}
export interface ChatPageResponse {
items: ChatResponse[];
next_cursor: string | null;
}
export function toChatResponse(chat: Chat): ChatResponse {
return {
id: chat.id,
title: chat.title,
member_ids: chat.memberIds,
created_at: chat.createdAt,
};
}
+22
View File
@@ -0,0 +1,22 @@
import {
ArrayMaxSize,
ArrayMinSize,
ArrayUnique,
IsArray,
IsString,
} from 'class-validator';
import { CodePointLength } from '../../common/dto/code-point-length.validator';
import { NotBlank } from '../../common/dto/not-blank.validator';
export class CreateChatDto {
@NotBlank()
@CodePointLength(1, 100)
title!: string;
@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(100)
@ArrayUnique()
@IsString({ each: true })
member_ids!: string[];
}
+49
View File
@@ -0,0 +1,49 @@
import { validate as isUuid } from './uuid';
export interface ChatCursor {
id: string;
}
export interface MessageCursor {
chatId: string;
id: string;
}
export function encodeChatCursor(cursor: ChatCursor): string {
return encode(cursor);
}
export function decodeChatCursor(raw: string): ChatCursor | null {
const decoded = decode(raw);
return decoded && isUuidField(decoded.id) ? { id: decoded.id } : null;
}
export function encodeMessageCursor(cursor: MessageCursor): string {
return encode(cursor);
}
export function decodeMessageCursor(raw: string): MessageCursor | null {
const decoded = decode(raw);
return decoded && isUuidField(decoded.chatId) && isUuidField(decoded.id)
? { chatId: decoded.chatId, id: decoded.id }
: null;
}
function isUuidField(value: unknown): value is string {
return typeof value === 'string' && isUuid(value);
}
function encode(value: object): string {
return Buffer.from(JSON.stringify(value), 'utf8').toString('base64url');
}
function decode(raw: string): Record<string, unknown> | null {
try {
const parsed = JSON.parse(
Buffer.from(raw, 'base64url').toString('utf8'),
);
return typeof parsed === 'object' && parsed !== null ? parsed : null;
} catch {
return null;
}
}
+22
View File
@@ -0,0 +1,22 @@
export interface User {
id: string;
username: string;
displayName: string;
createdAt: string;
}
export interface Chat {
id: string;
title: string;
memberIds: string[];
createdAt: string;
}
export interface Message {
id: string;
chatId: string;
senderId: string;
text: string;
createdAt: string;
attachmentIds: string[];
}
@@ -0,0 +1,33 @@
import { registerDecorator, ValidationOptions } from 'class-validator';
export function codePointLength(value: string): number {
return [...value].length;
}
export function CodePointLength(
min: number,
max: number,
options?: ValidationOptions,
) {
return function (object: object, propertyName: string) {
registerDecorator({
name: 'codePointLength',
target: object.constructor,
propertyName,
constraints: [min, max],
options,
validator: {
validate(value: unknown): boolean {
if (typeof value !== 'string') {
return false;
}
const length = codePointLength(value);
return length >= min && length <= max;
},
defaultMessage(): string {
return `${propertyName} must be between ${min} and ${max} Unicode code points`;
},
},
});
};
}
+20
View File
@@ -0,0 +1,20 @@
import { registerDecorator, ValidationOptions } from 'class-validator';
export function NotBlank(options?: ValidationOptions) {
return function (object: object, propertyName: string) {
registerDecorator({
name: 'notBlank',
target: object.constructor,
propertyName,
options,
validator: {
validate(value: unknown): boolean {
return typeof value === 'string' && value.trim().length > 0;
},
defaultMessage(): string {
return `${propertyName} must not be empty or whitespace-only`;
},
},
});
};
}
+35
View File
@@ -0,0 +1,35 @@
import { ApiError } from '../filters/api-error';
const DEFAULT_LIMIT = 50;
const MIN_LIMIT = 1;
const MAX_LIMIT = 100;
export function parseLimit(raw: unknown): number {
if (raw === undefined) {
return DEFAULT_LIMIT;
}
if (typeof raw !== 'string' || !/^\d+$/.test(raw)) {
throw new ApiError('invalid_request', 'limit must be an integer');
}
const limit = Number(raw);
if (limit < MIN_LIMIT || limit > MAX_LIMIT) {
throw new ApiError(
'invalid_request',
`limit must be between ${MIN_LIMIT} and ${MAX_LIMIT}`,
);
}
return limit;
}
export function rawCursorParam(raw: unknown): string | null {
if (raw === undefined) {
return null;
}
if (typeof raw !== 'string' || raw.length === 0) {
throw new ApiError(
'invalid_request',
'cursor must be a non-empty string',
);
}
return raw;
}
+34
View File
@@ -0,0 +1,34 @@
import { HttpException, HttpStatus } from '@nestjs/common';
export type ErrorCode =
| 'invalid_request'
| 'unauthorized'
| 'not_found'
| 'conflict'
| 'payload_too_large'
| 'unsupported_media_type'
| 'rate_limited'
| 'unavailable'
| 'internal_error';
const STATUS_BY_CODE: Record<ErrorCode, number> = {
invalid_request: HttpStatus.BAD_REQUEST,
unauthorized: HttpStatus.UNAUTHORIZED,
not_found: HttpStatus.NOT_FOUND,
conflict: HttpStatus.CONFLICT,
payload_too_large: HttpStatus.PAYLOAD_TOO_LARGE,
unsupported_media_type: HttpStatus.UNSUPPORTED_MEDIA_TYPE,
rate_limited: HttpStatus.TOO_MANY_REQUESTS,
unavailable: HttpStatus.SERVICE_UNAVAILABLE,
internal_error: HttpStatus.INTERNAL_SERVER_ERROR,
};
export class ApiError extends HttpException {
constructor(
public readonly code: ErrorCode,
message: string,
public readonly headers: Record<string, string> = {},
) {
super(message, STATUS_BY_CODE[code]);
}
}
+121
View File
@@ -0,0 +1,121 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
Logger,
} from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
import { isDatabaseUnavailable } from '../../db/prisma.service';
import { routeTemplate } from '../../telemetry/http';
import { ApiError, ErrorCode } from './api-error';
const CODE_BY_STATUS: Partial<Record<number, ErrorCode>> = {
[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',
};
interface Resolved {
status: number;
code: ErrorCode;
message: string;
}
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger('HttpExceptionFilter');
catch(exception: unknown, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const reply = ctx.getResponse<FastifyReply>();
const request = ctx.getRequest<FastifyRequest>();
const requestId = request.requestId;
const { status, code, message } = this.resolve(exception);
if (status >= 500) {
this.logger.error({
event: 'http.error',
message,
method: request.method,
route: routeTemplate(request),
status,
request_id: requestId,
});
}
if (exception instanceof ApiError) {
reply.headers(exception.headers);
}
reply
.code(status)
.header('x-request-id', requestId)
.send({ error: { code, message, request_id: requestId } });
}
private resolve(exception: unknown): Resolved {
if (exception instanceof ApiError) {
return {
status: exception.getStatus(),
code: exception.code,
message: exception.message,
};
}
if (exception instanceof HttpException) {
const status = exception.getStatus();
const code = CODE_BY_STATUS[status] ?? 'internal_error';
const message =
extractMessage(exception.getResponse()) ?? exception.message;
return { status, code, message };
}
const fastifyStatus = (exception as { statusCode?: unknown })
?.statusCode;
if (typeof fastifyStatus === 'number' && CODE_BY_STATUS[fastifyStatus]) {
const message =
exception instanceof Error ? exception.message : 'Invalid request';
return {
status: fastifyStatus,
code: CODE_BY_STATUS[fastifyStatus]!,
message,
};
}
if (isDatabaseUnavailable(exception)) {
return {
status: 503,
code: 'unavailable',
message: 'database unavailable',
};
}
return {
status: 500,
code: 'internal_error',
message: 'Internal server error',
};
}
}
function extractMessage(response: unknown): string | null {
if (typeof response === 'string') {
return response;
}
if (
typeof response === 'object' &&
response !== null &&
'message' in response
) {
const value = (response as { message: unknown }).message;
if (typeof value === 'string') {
return value;
}
if (Array.isArray(value) && value.every((v) => typeof v === 'string')) {
return value.join('; ');
}
}
return null;
}
+23
View File
@@ -0,0 +1,23 @@
import { randomUUID } from 'crypto';
import { FastifyInstance, FastifyRequest } from 'fastify';
import { hostname } from 'os';
const REQUEST_ID_HEADER = 'x-request-id';
const INSTANCE_ID_HEADER = 'x-instance-id';
export const INSTANCE_ID = process.env.INSTANCE_ID ?? hostname();
declare module 'fastify' {
interface FastifyRequest {
requestId: string;
}
}
export function registerRequestId(instance: FastifyInstance): void {
instance.addHook('onRequest', (request: FastifyRequest, reply, done) => {
request.requestId = randomUUID();
reply.header(REQUEST_ID_HEADER, request.requestId);
reply.header(INSTANCE_ID_HEADER, INSTANCE_ID);
done();
});
}
+6
View File
@@ -0,0 +1,6 @@
const UUID_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export function validate(value: string): boolean {
return UUID_PATTERN.test(value);
}
+21
View File
@@ -0,0 +1,21 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import { registerUploadBody } from './attachments/upload-body';
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
import { registerRequestId } from './common/interceptors/request-id';
export function configureApp(app: NestFastifyApplication): void {
const instance = app.getHttpAdapter().getInstance();
registerRequestId(instance);
registerUploadBody(instance);
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
forbidUnknownValues: true,
}),
);
app.useGlobalFilters(new HttpExceptionFilter());
}
+9
View File
@@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class DbModule {}
+52
View File
@@ -0,0 +1,52 @@
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';
import { readFileSync } from 'fs';
import { registerDbPool } from '../telemetry/db-pool';
const UNAVAILABLE_CODES = new Set(['P1001', 'P1002', 'P1008', 'P1017', 'P2024']);
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleDestroy {
constructor() {
const url = readDatabaseUrl();
super({ datasourceUrl: url });
registerDbPool(this, url);
}
async onModuleDestroy(): Promise<void> {
await this.$disconnect();
}
}
function readDatabaseUrl(): string {
const file = process.env.DATABASE_URL_FILE;
const url = file ? readFileSync(file, 'utf8').trim() : process.env.DATABASE_URL;
if (!url) {
throw new Error('DATABASE_URL_FILE or DATABASE_URL must be set');
}
return url;
}
export function isUniqueViolation(error: unknown): boolean {
return (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === 'P2002'
);
}
export function isForeignKeyViolation(error: unknown): boolean {
return (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === 'P2003'
);
}
export function isDatabaseUnavailable(error: unknown): boolean {
if (error instanceof Prisma.PrismaClientInitializationError) {
return true;
}
return (
error instanceof Prisma.PrismaClientKnownRequestError &&
UNAVAILABLE_CODES.has(error.code)
);
}
+351
View File
@@ -0,0 +1,351 @@
const ALICE_ID = '11111111-1111-4111-8111-111111111111';
const BOB_ID = '22222222-2222-4222-8222-222222222222';
const CHAT_ID = '33333333-3333-4333-8333-333333333333';
const MESSAGE_ID = '44444444-4444-4444-8444-444444444444';
const CURSOR =
'eyJjaGF0SWQiOiIzMzMzMzMzMy0zMzMzLTQzMzMtODMzMy0zMzMzMzMzMzMzMzMiLCJpZCI6IjQ0NDQ0NDQ0LTQ0NDQtNDQ0NC04NDQ0LTQ0NDQ0NDQ0NDQ0NCJ9';
const ALICE = {
id: ALICE_ID,
username: 'alice',
display_name: 'Алиса',
created_at: '2026-09-01T12:00:00.000Z',
};
const CHAT = {
id: CHAT_ID,
title: 'Проект по ВНП',
member_ids: [ALICE_ID, BOB_ID],
created_at: '2026-09-01T12:01:00.000Z',
};
const MESSAGE = {
id: MESSAGE_ID,
chat_id: CHAT_ID,
sender_id: ALICE_ID,
text: 'Привет, Боб! 👋',
created_at: '2026-09-01T12:02:00.000Z',
attachment_ids: [],
};
export interface Example {
summary: string;
value: unknown;
}
export interface OperationExamples {
request?: Record<string, Example>;
responses: Record<string, Record<string, Example>>;
}
function example(summary: string, value: unknown): Example {
return { summary, value };
}
function error(code: string, message: string): unknown {
return {
error: {
code,
message,
request_id: '9b2c6f1e-5d4a-4c3b-8a2f-1e0d9c8b7a6f',
},
};
}
const UNAUTHORIZED = {
missing: example(
'Нет/неверный/истёкший/отозванный Bearer-токен',
error('unauthorized', 'missing or invalid session'),
),
};
const UNAVAILABLE = {
database: example(
'PostgreSQL недоступен',
error('unavailable', 'database unavailable'),
),
};
const CHAT_NOT_FOUND = {
notFound: example(
'Чата нет или вызывающий не участник (не 403)',
error('not_found', 'chat not found'),
),
};
const INVALID_CHAT_ID = example(
'chat_id не UUID',
error('invalid_request', 'chatId must be a valid UUID'),
);
export const GUIDE = `
Учебный мессенджер, лабораторная 6: PostgreSQL, сессии, Redis-кэш сессий, вложения в S3 и их фоновая обработка.
Документ — обязательный контракт \`contracts/openapi.json\`, дополненный примерами запросов и ответов.
**Как пройти сценарий в Swagger UI**
1. \`POST /api/v1/users\` — зарегистрируйте пользователей (примеры alice, bob, eve в списке Examples). Повторная регистрация того же username — 409, меняйте username.
2. **Authorize → basicAuth**: username и password из шага 1 → \`POST /api/v1/auth/sessions\` → скопируйте \`token\`.
3. **Authorize → bearerAuth**: вставьте token (без слова Bearer). Basic отправляется только на вход, остальные запросы идут с Bearer.
4. \`GET /api/v1/users/me\` — свой id; id второго участника — из ответа его регистрации.
5. \`POST /api/v1/chats\` с \`member_ids\` = [свой id, id собеседника] → \`chat_id\`.
6. \`POST /api/v1/chats/{chat_id}/messages\` с заголовком \`Idempotency-Key\` (любая строка из \`A-Za-z0-9._:-\`, до 128 символов). Повтор с тем же ключом и телом вернёт то же сообщение, с другим телом — 409. Затем \`GET …/messages?limit=1\` и следующая страница с \`cursor\` = \`next_cursor\`.
7. \`POST /api/v1/chats/{chat_id}/attachments?filename=photo.png\` с телом PNG/JPEG → 202 \`queued\`. \`GET /api/v1/attachments/{id}\` повторять, пока \`status\` не станет \`ready\` (до этого \`/content\` отвечает 409), затем \`/content?variant=thumbnail\`, \`crop\` или \`optimized\`.
8. Чтобы действовать от другого пользователя: Logout в Authorize, вход им и новый token.
9. \`DELETE /api/v1/auth/sessions/current\` — logout; тот же token дальше даёт 401.
Сессия живёт \`SESSION_TTL_SECONDS\` (по умолчанию 3600 с). Пароль — 12–128 символов Unicode, двоеточие допустимо.
`;
export const OPERATION_EXAMPLES: Record<string, OperationExamples> = {
'GET /health/live': {
responses: {
'200': { ok: example('Процесс жив', { status: 'ok' }) },
},
},
'GET /health/ready': {
responses: {
'200': {
ok: example('PostgreSQL доступен (Redis не критичен)', {
status: 'ok',
}),
},
'503': UNAVAILABLE,
},
},
'POST /api/v1/users': {
request: {
alice: example('Алиса', {
username: 'alice',
display_name: 'Алиса',
password: 'Alice-password-123',
}),
bob: example('Боб', {
username: 'bob',
display_name: 'Боб',
password: 'Bob-password-123',
}),
eve: example('Ева (посторонняя)', {
username: 'eve',
display_name: 'Ева',
password: 'Eve-password-123',
}),
colon: example('Пароль с двоеточием и эмодзи', {
username: 'carol',
display_name: 'Кэрол 🌸',
password: 'пароль:с:двоеточием🔑',
}),
},
responses: {
'201': { created: example('Пользователь создан', ALICE) },
'400': {
username: example(
'username не соответствует [a-z0-9_]{3,32}',
error('invalid_request', 'username must match [a-z0-9_]{3,32}'),
),
password: example(
'password короче 12 символов',
error(
'invalid_request',
'password must be between 12 and 128 Unicode code points',
),
),
extra: example(
'Лишнее поле в JSON',
error('invalid_request', 'property extra should not exist'),
),
},
'409': {
taken: example(
'username уже занят',
error('conflict', 'username is already taken'),
),
},
'503': UNAVAILABLE,
},
},
'POST /api/v1/auth/sessions': {
responses: {
'201': {
created: example('Сессия создана', {
token: 'q8Hn3vXl0ZbJ7mWcY2sRkT5uPaE9dFgVhN1oLxI4w6A',
token_type: 'Bearer',
expires_at: '2026-09-01T13:00:00.000Z',
user: ALICE,
}),
},
'401': {
invalid: example(
'Неверный пароль, неизвестный пользователь или нет Basic — ответ одинаковый',
error('unauthorized', 'invalid credentials'),
),
},
'503': UNAVAILABLE,
},
},
'DELETE /api/v1/auth/sessions/current': {
responses: {
'401': UNAUTHORIZED,
'503': {
...UNAVAILABLE,
cache: example(
'Redis недоступен — отзыв не выполнен, повторите позже',
error('unavailable', 'session cache unavailable'),
),
},
},
},
'GET /api/v1/users/me': {
responses: {
'200': { me: example('Текущий пользователь из сессии', ALICE) },
'401': UNAUTHORIZED,
'503': UNAVAILABLE,
},
},
'POST /api/v1/chats': {
request: {
pair: example('Чат Алисы и Боба', {
title: CHAT.title,
member_ids: CHAT.member_ids,
}),
solo: example('Чат только с собой', {
title: 'Заметки',
member_ids: [ALICE_ID],
}),
},
responses: {
'201': { created: example('Чат создан атомарно', CHAT) },
'400': {
noCaller: example(
'Вызывающий не в member_ids',
error('invalid_request', 'member_ids must include the caller'),
),
unknown: example(
'Неизвестный участник — транзакция откатывается',
error('invalid_request', 'member_ids contain an unknown user'),
),
duplicate: example(
'Дубликаты в member_ids',
error(
'invalid_request',
"All member_ids's elements must be unique",
),
),
},
'401': UNAUTHORIZED,
'503': UNAVAILABLE,
},
},
'GET /api/v1/chats': {
responses: {
'200': {
page: example('Страница чатов с продолжением', {
items: [CHAT],
next_cursor: 'eyJpZCI6IjMzMzMzMzMzLTMzMzMtNDMzMy04MzMzLTMzMzMzMzMzMzMzMyJ9',
}),
last: example('Последняя страница', {
items: [CHAT],
next_cursor: null,
}),
},
'400': {
limit: example(
'limit вне 1–100',
error('invalid_request', 'limit must be between 1 and 100'),
),
cursor: example(
'Испорченный или чужой cursor',
error('invalid_request', 'invalid cursor'),
),
},
'401': UNAUTHORIZED,
'503': UNAVAILABLE,
},
},
'GET /api/v1/chats/{chat_id}': {
responses: {
'200': { chat: example('Чат, где вызывающий — участник', CHAT) },
'400': { chatId: INVALID_CHAT_ID },
'401': UNAUTHORIZED,
'404': CHAT_NOT_FOUND,
'503': UNAVAILABLE,
},
},
'POST /api/v1/chats/{chat_id}/messages': {
request: {
text: example('Обычное сообщение', { text: MESSAGE.text }),
long: example('Максимум — 4000 символов Unicode', {
text: 'Длинный текст…',
}),
},
responses: {
'201': {
created: example('Отправитель берётся из сессии', MESSAGE),
},
'400': {
blank: example(
'Пустой или пробельный текст',
error(
'invalid_request',
'text must not be empty or whitespace-only',
),
),
sender: example(
'sender_id в теле не принимается',
error('invalid_request', 'property sender_id should not exist'),
),
chatId: INVALID_CHAT_ID,
key: example(
'Нет Idempotency-Key или неверный формат',
error(
'invalid_request',
'Idempotency-Key header must match [A-Za-z0-9._:-]{1,128}',
),
),
},
'401': UNAUTHORIZED,
'404': CHAT_NOT_FOUND,
'409': {
keyReused: example(
'Тот же Idempotency-Key с другим телом',
error(
'conflict',
'Idempotency-Key was already used with a different request',
),
),
},
'503': UNAVAILABLE,
},
},
'GET /api/v1/chats/{chat_id}/messages': {
responses: {
'200': {
page: example('Страница истории (limit=1) с cursor', {
items: [MESSAGE],
next_cursor: CURSOR,
}),
last: example('Последняя страница', {
items: [MESSAGE],
next_cursor: null,
}),
},
'400': {
otherChat: example(
'cursor от другого чата',
error('invalid_request', 'cursor belongs to a different chat'),
),
cursor: example(
'Испорченный cursor или cursor несуществующего сообщения',
error('invalid_request', 'invalid cursor'),
),
limit: example(
'limit вне 1–100',
error('invalid_request', 'limit must be between 1 and 100'),
),
},
'401': UNAUTHORIZED,
'404': CHAT_NOT_FOUND,
'503': UNAVAILABLE,
},
},
};
+43
View File
@@ -0,0 +1,43 @@
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import { OpenAPIObject, SwaggerModule } from '@nestjs/swagger';
import { readFileSync } from 'fs';
import { join } from 'path';
import { GUIDE, OPERATION_EXAMPLES } from './examples';
const CONTRACT_PATH = join(process.cwd(), 'contracts', 'openapi.json');
export function setupSwagger(app: NestFastifyApplication): void {
SwaggerModule.setup('docs', app, loadDocument(), {
customSiteTitle: 'Messenger lab6 API',
swaggerOptions: {
persistAuthorization: true,
displayRequestDuration: true,
tryItOutEnabled: true,
},
});
}
export function loadDocument(): OpenAPIObject {
const document = JSON.parse(readFileSync(CONTRACT_PATH, 'utf8'));
document.info.description = GUIDE;
document.servers = [{ url: '/', description: 'Этот экземпляр API' }];
for (const [key, examples] of Object.entries(OPERATION_EXAMPLES)) {
const [method, path] = key.split(' ');
const operation = document.paths[path]?.[method.toLowerCase()];
if (!operation) {
throw new Error(`examples for unknown operation ${key}`);
}
if (examples.request) {
operation.requestBody.content['application/json'].examples =
examples.request;
}
for (const [status, named] of Object.entries(examples.responses)) {
const content = operation.responses[status]?.content;
if (!content) {
throw new Error(`examples for unknown response ${key} ${status}`);
}
content['application/json'].examples = named;
}
}
return document;
}
+44
View File
@@ -0,0 +1,44 @@
import { Controller, Get } from '@nestjs/common';
import { ApiError } from '../common/filters/api-error';
import { PrismaService } from '../db/prisma.service';
import { S3Service } from '../storage/s3.service';
import { isDraining } from './shutdown';
@Controller('health')
export class HealthController {
constructor(
private readonly prisma: PrismaService,
private readonly s3: S3Service,
) {}
@Get('live')
live(): { status: 'ok' } {
return { status: 'ok' };
}
@Get('core')
async core(): Promise<{ status: 'ok' }> {
await this.assertCoreReady();
return { status: 'ok' };
}
@Get('ready')
async ready(): Promise<{ status: 'ok' }> {
await this.assertCoreReady();
if (!(await this.s3.isAvailable())) {
throw new ApiError('unavailable', 'object storage unavailable');
}
return { status: 'ok' };
}
private async assertCoreReady(): Promise<void> {
if (isDraining()) {
throw new ApiError('unavailable', 'shutting down');
}
try {
await this.prisma.$queryRaw`SELECT 1`;
} catch {
throw new ApiError('unavailable', 'database unavailable');
}
}
}
+7
View File
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { HealthController } from './health.controller';
@Module({
controllers: [HealthController],
})
export class HealthModule {}
+33
View File
@@ -0,0 +1,33 @@
import { INestApplication, Logger } from '@nestjs/common';
import { shutdownTelemetry } from '../telemetry/sdk';
let draining = false;
export function isDraining(): boolean {
return draining;
}
export function enableGracefulShutdown(app: INestApplication): void {
const delayMs = Number(process.env.SHUTDOWN_DELAY_MS ?? 3000);
const shutdown = (signal: string): void => {
if (draining) {
return;
}
draining = true;
Logger.log(`${signal}: readiness → 503, closing in ${delayMs} ms`, 'Shutdown');
setTimeout(() => {
app
.close()
.then(
() => 0,
() => 1,
)
.then(async (code) => {
await shutdownTelemetry();
process.exit(code);
});
}, delayMs);
};
process.once('SIGTERM', () => shutdown('SIGTERM'));
process.once('SIGINT', () => shutdown('SIGINT'));
}
+98
View File
@@ -0,0 +1,98 @@
import { context, propagation } from '@opentelemetry/api';
import { validate as isUuid } from '../common/uuid';
export const PIPELINE_VERSION = 1;
export const JOB_TYPE = 'attachment.process.v1';
const TRACEPARENT = /^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$/;
const TRACESTATE_MAX = 512;
export interface TraceCarrier {
traceparent?: string;
tracestate?: string;
}
export interface JobMessage extends TraceCarrier {
schema_version: 1;
job_id: string;
type: typeof JOB_TYPE;
attachment_id: string;
pipeline_version: number;
attempt: number;
created_at: string;
}
const FIELDS = [
'schema_version',
'job_id',
'type',
'attachment_id',
'pipeline_version',
'attempt',
'created_at',
'traceparent',
'tracestate',
];
export function jobMessage(
job: { id: string; attachmentId: string; pipelineVersion: number; attempt: number },
createdAt: Date,
): JobMessage {
return {
schema_version: 1,
job_id: job.id,
type: JOB_TYPE,
attachment_id: job.attachmentId,
pipeline_version: job.pipelineVersion,
attempt: job.attempt,
created_at: createdAt.toISOString(),
...currentTraceCarrier(),
};
}
export function currentTraceCarrier(): TraceCarrier {
const carrier: Record<string, string> = {};
propagation.inject(context.active(), carrier);
const result: TraceCarrier = {};
if (TRACEPARENT.test(carrier.traceparent ?? '')) {
result.traceparent = carrier.traceparent;
if (carrier.tracestate && carrier.tracestate.length <= TRACESTATE_MAX) {
result.tracestate = carrier.tracestate;
}
}
return result;
}
export function parseJobMessage(raw: Buffer): JobMessage | null {
let value: any;
try {
value = JSON.parse(raw.toString('utf8'));
} catch {
return null;
}
if (
typeof value !== 'object' ||
value === null ||
Array.isArray(value) ||
Object.keys(value).some((key) => !FIELDS.includes(key)) ||
value.schema_version !== 1 ||
value.type !== JOB_TYPE ||
typeof value.job_id !== 'string' ||
!isUuid(value.job_id) ||
typeof value.attachment_id !== 'string' ||
!isUuid(value.attachment_id) ||
!Number.isInteger(value.pipeline_version) ||
value.pipeline_version < 1 ||
!Number.isInteger(value.attempt) ||
value.attempt < 1 ||
typeof value.created_at !== 'string' ||
Number.isNaN(Date.parse(value.created_at)) ||
(value.traceparent !== undefined &&
(typeof value.traceparent !== 'string' || !TRACEPARENT.test(value.traceparent))) ||
(value.tracestate !== undefined &&
(typeof value.tracestate !== 'string' || value.tracestate.length > TRACESTATE_MAX))
) {
return null;
}
return value as JobMessage;
}
+257
View File
@@ -0,0 +1,257 @@
import { AttachmentJob, Prisma, PrismaClient } from '@prisma/client';
import { randomUUID } from 'crypto';
import { jobMessage, PIPELINE_VERSION } from './job-message';
type Tx = Prisma.TransactionClient;
export const RETRY_EXHAUSTED = 'retry_exhausted';
export function maxAttempts(): number {
return Number(process.env.JOB_MAX_ATTEMPTS ?? 5);
}
export function backoffMs(attempt: number, random = Math.random): number {
const base = Number(process.env.JOB_RETRY_BASE_MS ?? 1000);
const cap = Number(process.env.JOB_RETRY_MAX_MS ?? 30000);
const delay = Math.min(cap, base * 2 ** (attempt - 1));
return Math.round(delay / 2 + random() * (delay / 2));
}
export async function enqueueJob(
tx: Tx,
attachmentId: string,
now: Date,
): Promise<AttachmentJob> {
const job = await tx.attachmentJob.create({
data: {
id: randomUUID(),
attachmentId,
pipelineVersion: PIPELINE_VERSION,
status: 'queued',
attempt: 1,
maxAttempts: maxAttempts(),
nextAttemptAt: now,
createdAt: now,
updatedAt: now,
},
});
await tx.jobEvent.create({
data: { jobId: job.id, attempt: 1, event: 'enqueued', createdAt: now },
});
await publishLater(tx, job, now);
return job;
}
export async function scheduleRetry(
tx: Tx,
job: AttachmentJob,
reason: string,
now: Date,
event = 'retry_scheduled',
): Promise<AttachmentJob> {
const delay = backoffMs(job.attempt);
const next = await tx.attachmentJob.update({
where: { id: job.id },
data: {
status: 'queued',
attempt: job.attempt + 1,
nextAttemptAt: new Date(now.getTime() + delay),
leaseOwner: null,
leaseUntil: null,
lastError: reason,
},
});
await tx.attachment.update({
where: { id: job.attachmentId },
data: { status: 'queued' },
});
await tx.jobEvent.create({
data: {
jobId: job.id,
attempt: job.attempt,
event,
detail: `${reason}; next attempt ${next.attempt} in ${delay} ms`,
createdAt: now,
},
});
await publishLater(tx, next, next.nextAttemptAt);
return next;
}
export async function markFailed(
tx: Tx,
job: AttachmentJob,
errorCode: string,
reason: string,
now: Date,
): Promise<void> {
await tx.attachmentJob.update({
where: { id: job.id },
data: {
status: 'failed',
errorCode,
lastError: reason,
leaseOwner: null,
leaseUntil: null,
finishedAt: now,
},
});
await tx.attachment.update({
where: { id: job.attachmentId },
data: { status: 'failed', errorCode },
});
await tx.jobEvent.create({
data: {
jobId: job.id,
attempt: job.attempt,
event: 'failed',
detail: `${errorCode}: ${reason}`,
createdAt: now,
},
});
}
export async function retryOrFail(
tx: Tx,
job: AttachmentJob,
reason: string,
now: Date,
event?: string,
): Promise<'retry' | 'failed'> {
if (job.attempt >= job.maxAttempts) {
await markFailed(tx, job, RETRY_EXHAUSTED, reason, now);
return 'failed';
}
await scheduleRetry(tx, job, reason, now, event);
return 'retry';
}
export async function recoverExpiredLeases(
prisma: PrismaClient,
now = new Date(),
limit = 100,
): Promise<number> {
const expired = await prisma.attachmentJob.findMany({
where: { status: 'processing', leaseUntil: { lt: now } },
orderBy: { leaseUntil: 'asc' },
take: limit,
});
let recovered = 0;
for (const candidate of expired) {
await prisma.$transaction(async (tx) => {
const taken = await tx.attachmentJob.updateMany({
where: {
id: candidate.id,
status: 'processing',
leaseOwner: candidate.leaseOwner,
leaseUntil: { lt: now },
},
data: { leaseOwner: null, leaseUntil: null, status: 'queued' },
});
if (taken.count === 0) {
return;
}
await retryOrFail(tx, candidate, 'lease expired', now, 'lease_expired');
recovered++;
});
}
return recovered;
}
export async function republishStale(
prisma: PrismaClient,
staleMs: number,
now = new Date(),
limit = 100,
): Promise<number> {
const cutoff = new Date(now.getTime() - staleMs);
const stale = await prisma.$queryRaw<AttachmentJob[]>`
SELECT j.id, j.attachment_id AS "attachmentId", j.pipeline_version AS "pipelineVersion", j.attempt
FROM attachment_jobs j
WHERE j.status = 'queued'
AND j.next_attempt_at < ${cutoff}
AND NOT EXISTS (
SELECT 1 FROM outbox o
WHERE o.job_id = j.id AND (o.published_at IS NULL OR o.published_at > ${cutoff})
)
ORDER BY j.next_attempt_at
LIMIT ${limit}`;
for (const job of stale) {
await prisma.$transaction(async (tx) => {
await tx.jobEvent.create({
data: { jobId: job.id, attempt: job.attempt, event: 'republished', createdAt: now },
});
await publishLater(tx, job, now);
});
}
return stale.length;
}
export type ReplayResult =
| 'replayed'
| 'already_ready'
| 'not_failed'
| 'source_missing'
| 'not_found';
export async function replayJob(
prisma: PrismaClient,
jobId: string,
actor: string,
now = new Date(),
): Promise<ReplayResult> {
return prisma.$transaction(async (tx) => {
const job = await tx.attachmentJob.findUnique({ where: { id: jobId } });
if (!job) {
return 'not_found';
}
if (job.status === 'ready') {
return 'already_ready';
}
if (job.status !== 'failed') {
return 'not_failed';
}
const source = await tx.attachment.updateMany({
where: { id: job.attachmentId, status: 'failed', stagingKey: { not: null } },
data: { status: 'queued', errorCode: null },
});
if (source.count === 0) {
return 'source_missing';
}
const taken = await tx.attachmentJob.updateMany({
where: { id: jobId, status: 'failed' },
data: {
status: 'queued',
attempt: 1,
errorCode: null,
nextAttemptAt: now,
finishedAt: null,
},
});
if (taken.count === 0) {
throw new Error('job changed during replay');
}
await tx.jobEvent.create({
data: {
jobId,
attempt: 1,
event: 'replayed',
detail: `by ${actor}; previous ${job.errorCode} after attempt ${job.attempt}`,
createdAt: now,
},
});
await publishLater(tx, { ...job, attempt: 1 }, now);
return 'replayed';
});
}
async function publishLater(tx: Tx, job: AttachmentJob, availableAt: Date): Promise<void> {
await tx.outboxEntry.create({
data: {
jobId: job.id,
payload: jobMessage(job, availableAt) as unknown as Prisma.InputJsonObject,
availableAt,
createdAt: new Date(),
},
});
}
+298
View File
@@ -0,0 +1,298 @@
import { Logger } from '@nestjs/common';
import { metrics, SpanStatusCode, trace } from '@opentelemetry/api';
import { Attachment, AttachmentJob, PrismaClient } from '@prisma/client';
import { createHash, randomUUID } from 'crypto';
import {
ObjectMissingError,
ObjectTooLargeError,
S3Service,
} from '../storage/s3.service';
import { JOB_TYPE, JobMessage, PIPELINE_VERSION } from './job-message';
import { markFailed, retryOrFail } from './job-store';
import {
InvalidImageError,
RenderedVariant,
RenderTimeoutError,
renderVariants,
} from './variants';
export type Outcome = 'ready' | 'retry' | 'failed' | 'skipped';
export interface ProcessorOptions {
timeoutMs: number;
leaseMs: number;
maxBytes: number;
maxPixels: number;
}
export function processorOptions(): ProcessorOptions {
const timeoutMs = Number(process.env.JOB_TIMEOUT_SECONDS ?? 60) * 1000;
return {
timeoutMs,
leaseMs: Number(process.env.JOB_LEASE_SECONDS ?? timeoutMs / 1000 + 30) * 1000,
maxBytes: Number(process.env.MAX_UPLOAD_BYTES ?? 10485760),
maxPixels: Number(process.env.MAX_IMAGE_PIXELS ?? 40000000),
};
}
export interface ProcessorHooks {
afterStore?: () => Promise<void>;
afterCommit?: () => Promise<void>;
}
class PermanentError extends Error {
constructor(
readonly code: string,
reason: string,
) {
super(reason);
}
}
class TimeoutError extends Error {}
const tracer = trace.getTracer('messenger-worker');
const timeToReady = metrics.getMeter('messenger-worker').createHistogram('attachments.time_to_ready', {
description: 'From durable upload acceptance to the committed ready state',
unit: 's',
advice: {
explicitBucketBoundaries: [
0.1, 0.25, 0.5, 1, 2, 3, 5, 10, 20, 30, 45, 60, 90, 120, 150, 180, 240, 300, 450, 600,
],
},
});
export class JobProcessor {
private readonly logger = new Logger('JobProcessor');
constructor(
private readonly prisma: PrismaClient,
private readonly s3: S3Service,
private readonly options: ProcessorOptions = processorOptions(),
private readonly hooks: ProcessorHooks = {},
) {}
async handle(message: JobMessage): Promise<Outcome> {
const job = await this.prisma.attachmentJob.findUnique({
where: { id: message.job_id },
include: { attachment: true },
});
if (
!job ||
job.attachmentId !== message.attachment_id ||
job.pipelineVersion !== message.pipeline_version ||
job.pipelineVersion !== PIPELINE_VERSION
) {
this.logger.warn(`job ${message.job_id}: unknown or mismatched, dropped`);
return 'skipped';
}
const lease = await this.claim(job);
if (!lease) {
if (job.status === 'ready') {
await this.removeStaging(job.attachment);
}
this.logger.log(`job ${job.id}: ${job.status}, not due or leased elsewhere, skipped`);
return 'skipped';
}
let variants: RenderedVariant[];
try {
variants = await this.withTimeout(this.produce(job.attachment));
} catch (error) {
return this.recordFailure(lease, error);
}
await this.hooks.afterStore?.();
return this.commit(lease, job.attachment, variants);
}
private async claim(job: AttachmentJob): Promise<AttachmentJob | null> {
const now = new Date();
const owner = randomUUID();
return this.prisma.$transaction(async (tx) => {
const taken = await tx.attachmentJob.updateMany({
where: { id: job.id, status: 'queued', nextAttemptAt: { lte: now } },
data: {
status: 'processing',
leaseOwner: owner,
leaseUntil: new Date(now.getTime() + this.options.leaseMs),
},
});
if (taken.count === 0) {
return null;
}
await tx.attachment.update({
where: { id: job.attachmentId },
data: { status: 'processing' },
});
const leased = await tx.attachmentJob.findUniqueOrThrow({ where: { id: job.id } });
await tx.jobEvent.create({
data: { jobId: job.id, attempt: leased.attempt, event: 'processing', createdAt: now },
});
return leased;
});
}
private async produce(attachment: Attachment): Promise<RenderedVariant[]> {
if (!attachment.stagingKey) {
throw new PermanentError('staging_missing', 'staging object was already removed');
}
let original: Buffer;
try {
original = await this.s3.getBuffer(attachment.stagingKey, this.options.maxBytes);
} catch (error) {
if (error instanceof ObjectMissingError) {
throw new PermanentError('staging_missing', 'staging object not found');
}
if (error instanceof ObjectTooLargeError) {
throw new PermanentError('invalid_image', 'staging object exceeds size limit');
}
throw error;
}
const sha256 = createHash('sha256').update(original).digest('hex');
if (original.length !== attachment.sizeBytes || sha256 !== attachment.sha256) {
throw new PermanentError('checksum_mismatch', 'staging object differs from accepted upload');
}
let variants: RenderedVariant[];
try {
variants = await tracer.startActiveSpan('attachment.render_variants', async (span) => {
span.setAttribute('messenger.image.bytes', original.length);
try {
return await renderVariants(
original,
this.options.maxPixels,
Math.ceil(this.options.timeoutMs / 1000),
);
} catch (error) {
span.setStatus({ code: SpanStatusCode.ERROR, message: error instanceof Error ? error.name : 'error' });
throw error;
} finally {
span.end();
}
});
} catch (error) {
if (error instanceof InvalidImageError) {
throw new PermanentError('invalid_image', 'image cannot be decoded');
}
throw error;
}
await this.s3.putBuffer(attachment.objectKey, original, attachment.contentType, sha256);
for (const variant of variants) {
await this.s3.putBuffer(
variantKey(attachment.id, variant),
variant.body,
variant.contentType,
variant.sha256,
);
}
return variants;
}
private async commit(
lease: AttachmentJob,
attachment: Attachment,
variants: RenderedVariant[],
): Promise<Outcome> {
const now = new Date();
const committed = await this.prisma.$transaction(async (tx) => {
const owned = await tx.attachmentJob.updateMany({
where: { id: lease.id, status: 'processing', leaseOwner: lease.leaseOwner },
data: {
status: 'ready',
leaseOwner: null,
leaseUntil: null,
lastError: null,
finishedAt: now,
},
});
if (owned.count === 0) {
return false;
}
await tx.attachmentVariant.createMany({
data: variants.map((variant) => ({
attachmentId: attachment.id,
pipelineVersion: PIPELINE_VERSION,
name: variant.name,
contentType: variant.contentType,
objectKey: variantKey(attachment.id, variant),
sizeBytes: variant.body.length,
width: variant.width,
height: variant.height,
sha256: variant.sha256,
createdAt: now,
})),
skipDuplicates: true,
});
await tx.attachment.update({
where: { id: attachment.id },
data: { status: 'ready', errorCode: null },
});
await tx.jobEvent.create({
data: { jobId: lease.id, attempt: lease.attempt, event: 'ready', createdAt: now },
});
return true;
});
if (!committed) {
this.logger.warn(`job ${lease.id}: lease lost before commit, result left to owner`);
return 'skipped';
}
timeToReady.record((now.getTime() - attachment.createdAt.getTime()) / 1000, {
'job.type': JOB_TYPE,
});
await this.hooks.afterCommit?.();
await this.removeStaging(attachment);
return 'ready';
}
private async recordFailure(lease: AttachmentJob, error: unknown): Promise<Outcome> {
const now = new Date();
const reason =
error instanceof PermanentError
? error.message
: error instanceof TimeoutError || error instanceof RenderTimeoutError
? 'processing timed out'
: `temporary failure: ${error instanceof Error ? error.name : 'unknown'}`;
this.logger.warn(`job ${lease.id} attempt ${lease.attempt}: ${reason}`);
return this.prisma.$transaction(async (tx) => {
const owned = await tx.attachmentJob.count({
where: { id: lease.id, status: 'processing', leaseOwner: lease.leaseOwner },
});
if (owned === 0) {
return 'skipped' as const;
}
if (error instanceof PermanentError) {
await markFailed(tx, lease, error.code, reason, now);
return 'failed' as const;
}
return retryOrFail(tx, lease, reason, now);
});
}
private async removeStaging(attachment: Attachment): Promise<void> {
if (!attachment.stagingKey) {
return;
}
try {
await this.s3.delete(attachment.stagingKey);
await this.prisma.attachment.update({
where: { id: attachment.id },
data: { stagingKey: null },
});
} catch {
this.logger.warn(`staging cleanup postponed for ${attachment.id}`);
}
}
private withTimeout<T>(work: Promise<T>): Promise<T> {
let timer: NodeJS.Timeout;
const deadline = new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(new TimeoutError()), this.options.timeoutMs);
});
return Promise.race([work, deadline]).finally(() => clearTimeout(timer));
}
}
export function variantKey(
attachmentId: string,
variant: Pick<RenderedVariant, 'name' | 'extension'>,
): string {
return `attachments/${attachmentId}/v${PIPELINE_VERSION}/${variant.name}.${variant.extension}`;
}
+111
View File
@@ -0,0 +1,111 @@
import { createHash } from 'crypto';
import sharp, { Sharp } from 'sharp';
sharp.cache(false);
sharp.concurrency(1);
const WHITE = { r: 255, g: 255, b: 255 };
const JPEG_QUALITY = 82;
const WEBP_QUALITY = 80;
export interface RenderedVariant {
name: 'thumbnail' | 'crop' | 'optimized';
contentType: string;
extension: string;
body: Buffer;
width: number;
height: number;
sha256: string;
}
export class InvalidImageError extends Error {
constructor(reason: string) {
super(reason);
this.name = 'InvalidImageError';
}
}
export class RenderTimeoutError extends Error {
constructor() {
super('image processing exceeded its time limit');
this.name = 'RenderTimeoutError';
}
}
export function fitInside(
width: number,
height: number,
box: number,
): { width: number; height: number } {
if (width <= box && height <= box) {
return { width, height };
}
if (width >= height) {
return { width: box, height: Math.max(1, Math.floor((height * box) / width)) };
}
return { width: Math.max(1, Math.floor((width * box) / height)), height: box };
}
export async function renderVariants(
original: Buffer,
maxPixels: number,
timeoutSeconds = 0,
): Promise<RenderedVariant[]> {
const open = (): Sharp =>
sharp(original, { failOn: 'error', limitInputPixels: maxPixels })
.timeout({ seconds: timeoutSeconds })
.rotate();
try {
const meta = await sharp(original, { limitInputPixels: maxPixels }).metadata();
if (!meta.width || !meta.height) {
throw new InvalidImageError('image has no dimensions');
}
const swapped = (meta.orientation ?? 1) >= 5;
const width = swapped ? meta.height : meta.width;
const height = swapped ? meta.width : meta.height;
const thumb = fitInside(width, height, 256);
const large = fitInside(width, height, 1280);
const thumbnail = open()
.resize(thumb.width, thumb.height, { fit: 'fill' })
.flatten({ background: WHITE })
.jpeg({ quality: JPEG_QUALITY });
const crop = open()
.resize(128, 128, { fit: 'cover', position: 'centre' })
.flatten({ background: WHITE })
.jpeg({ quality: JPEG_QUALITY });
const optimized = open()
.resize(large.width, large.height, { fit: 'fill' })
.webp({ quality: WEBP_QUALITY, effort: 4 });
return [
await finish('thumbnail', 'image/jpeg', 'jpg', thumbnail),
await finish('crop', 'image/jpeg', 'jpg', crop),
await finish('optimized', 'image/webp', 'webp', optimized),
];
} catch (error) {
if (error instanceof InvalidImageError) {
throw error;
}
if (error instanceof Error && /timeout/i.test(error.message)) {
throw new RenderTimeoutError();
}
throw new InvalidImageError(error instanceof Error ? error.message : 'decode failed');
}
}
async function finish(
name: RenderedVariant['name'],
contentType: string,
extension: string,
pipeline: Sharp,
): Promise<RenderedVariant> {
const { data, info } = await pipeline.toBuffer({ resolveWithObject: true });
return {
name,
contentType,
extension,
body: data,
width: info.width,
height: info.height,
sha256: createHash('sha256').update(data).digest('hex'),
};
}
+39
View File
@@ -0,0 +1,39 @@
import './telemetry/register';
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { configureApp } from './configure-app';
import { setupSwagger } from './docs/swagger';
import { enableGracefulShutdown } from './health/shutdown';
import { registerHttpTelemetry } from './telemetry/http';
import { JsonLogger } from './telemetry/json-logger';
async function bootstrap(): Promise<void> {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
{
bufferLogs: true,
logger: new JsonLogger(),
},
);
configureApp(app);
registerHttpTelemetry(app.getHttpAdapter().getInstance());
setupSwagger(app);
enableGracefulShutdown(app);
const host = process.env.APP_HOST ?? '0.0.0.0';
const port = Number(process.env.APP_PORT ?? 8080);
await app.listen(port, host);
Logger.log(`Listening on http://${host}:${port}`, 'Bootstrap');
}
bootstrap().catch((error) => {
Logger.error('Fatal error during bootstrap', error);
process.exit(1);
});
+41
View File
@@ -0,0 +1,41 @@
import {
ArrayMaxSize,
IsArray,
IsString,
Validate,
ValidateIf,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
import { CodePointLength } from '../../common/dto/code-point-length.validator';
import { validate as isUuid } from '../../common/uuid';
@ValidatorConstraint({ name: 'uniqueUuidList' })
class UniqueUuidList implements ValidatorConstraintInterface {
validate(value: unknown): boolean {
if (
!Array.isArray(value) ||
!value.every((id) => typeof id === 'string' && isUuid(id))
) {
return false;
}
const normalized = (value as string[]).map((id) => id.toLowerCase());
return new Set(normalized).size === normalized.length;
}
defaultMessage(): string {
return 'attachment_ids must contain unique UUIDs';
}
}
export class CreateMessageDto {
@IsString()
@CodePointLength(0, 4000)
text!: string;
@ValidateIf((dto: CreateMessageDto) => dto.attachment_ids !== undefined)
@IsArray()
@ArrayMaxSize(10)
@Validate(UniqueUuidList)
attachment_ids?: string[];
}
+26
View File
@@ -0,0 +1,26 @@
import { Message } from '../../common/domain';
export interface MessageResponse {
id: string;
chat_id: string;
sender_id: string;
text: string;
created_at: string;
attachment_ids: string[];
}
export interface MessagePageResponse {
items: MessageResponse[];
next_cursor: string | null;
}
export function toMessageResponse(message: Message): MessageResponse {
return {
id: message.id,
chat_id: message.chatId,
sender_id: message.senderId,
text: message.text,
created_at: message.createdAt,
attachment_ids: message.attachmentIds,
};
}
+70
View File
@@ -0,0 +1,70 @@
import {
Body,
Controller,
Get,
Headers,
HttpCode,
Param,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { parseLimit, rawCursorParam } from '../common/dto/pagination-query.dto';
import { User } from '../common/domain';
import { ApiError } from '../common/filters/api-error';
import { CreateMessageDto } from './dto/create-message.dto';
import {
MessagePageResponse,
MessageResponse,
toMessageResponse,
} from './dto/message.response';
import { MessagesService } from './messages.service';
@Controller('api/v1/chats/:chatId/messages')
@UseGuards(AuthGuard)
export class MessagesController {
constructor(private readonly messages: MessagesService) {}
@Post()
@HttpCode(201)
async create(
@Param('chatId') chatId: string,
@Body() dto: CreateMessageDto,
@Headers('idempotency-key') keyRaw: unknown,
@CurrentUser() user: User,
): Promise<MessageResponse> {
const key = parseIdempotencyKey(keyRaw);
const message = await this.messages.create(
chatId,
user.id,
dto.text,
(dto.attachment_ids ?? []).map((id) => id.toLowerCase()),
key,
);
return toMessageResponse(message);
}
@Get()
async list(
@Param('chatId') chatId: string,
@CurrentUser() user: User,
@Query('limit') limitRaw: unknown,
@Query('cursor') cursorRaw: unknown,
): Promise<MessagePageResponse> {
const limit = parseLimit(limitRaw);
const cursor = rawCursorParam(cursorRaw);
return this.messages.list(chatId, user.id, limit, cursor);
}
}
function parseIdempotencyKey(raw: unknown): string {
if (typeof raw !== 'string' || !/^[A-Za-z0-9._:-]{1,128}$/.test(raw)) {
throw new ApiError(
'invalid_request',
'Idempotency-Key header must match [A-Za-z0-9._:-]{1,128}',
);
}
return raw;
}
+9
View File
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { MessagesController } from './messages.controller';
import { MessagesService } from './messages.service';
@Module({
controllers: [MessagesController],
providers: [MessagesService],
})
export class MessagesModule {}
+198
View File
@@ -0,0 +1,198 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { createHash } from 'crypto';
import { assertChatMember } from '../chats/chat-access';
import { codePointLength } from '../common/dto/code-point-length.validator';
import { decodeMessageCursor, encodeMessageCursor } from '../common/cursor';
import { Message } from '../common/domain';
import { ApiError } from '../common/filters/api-error';
import { PrismaService } from '../db/prisma.service';
import { MessagePageResponse, toMessageResponse } from './dto/message.response';
interface MessageRow {
id: string;
chatId: string;
senderId: string;
text: string;
createdAt: Date;
attachmentIds: string[];
}
const ATTACHMENT_LINKS = {
attachments: {
orderBy: { position: 'asc' },
select: { attachmentId: true },
},
} satisfies Prisma.MessageInclude;
type MessageWithLinks = Prisma.MessageGetPayload<{
include: typeof ATTACHMENT_LINKS;
}>;
class KeyAlreadyUsed extends Error {}
@Injectable()
export class MessagesService {
private readonly keyTtlMs =
Number(process.env.IDEMPOTENCY_TTL_SECONDS ?? 86400) * 1000;
constructor(private readonly prisma: PrismaService) {}
async create(
chatId: string,
callerId: string,
text: string,
attachmentIds: string[],
key: string,
): Promise<Message> {
await this.assertMember(chatId, callerId);
if (codePointLength(text.trim()) === 0 && attachmentIds.length === 0) {
throw new ApiError(
'invalid_request',
'text must not be empty or whitespace-only without attachments',
);
}
await this.assertAttachmentsInChat(chatId, attachmentIds);
const scope = { userId: callerId, chatId, key };
const requestHash = createHash('sha256')
.update(JSON.stringify({ text, attachment_ids: attachmentIds }))
.digest('hex');
try {
return await this.prisma.$transaction(async (tx) => {
await tx.idempotencyKey.deleteMany({
where: {
...scope,
createdAt: { lt: new Date(Date.now() - this.keyTtlMs) },
},
});
const row = await tx.message.create({
data: {
chatId,
senderId: callerId,
text,
attachments: {
create: attachmentIds.map((attachmentId, position) => ({
attachmentId,
position,
})),
},
},
include: ATTACHMENT_LINKS,
});
const { count } = await tx.idempotencyKey.createMany({
data: [{ ...scope, requestHash, messageId: row.id }],
skipDuplicates: true,
});
if (count === 0) {
throw new KeyAlreadyUsed();
}
return linkedMessage(row);
});
} catch (error) {
if (!(error instanceof KeyAlreadyUsed)) {
throw error;
}
}
const stored = await this.prisma.idempotencyKey.findUnique({
where: { userId_chatId_key: scope },
include: { message: { include: ATTACHMENT_LINKS } },
});
if (!stored || stored.requestHash !== requestHash) {
throw new ApiError(
'conflict',
'Idempotency-Key was already used with a different request',
);
}
return linkedMessage(stored.message);
}
async list(
chatId: string,
callerId: string,
limit: number,
rawCursor: string | null,
): Promise<MessagePageResponse> {
await this.assertMember(chatId, callerId);
let after = Prisma.empty;
if (rawCursor !== null) {
const cursor = decodeMessageCursor(rawCursor);
if (cursor && cursor.chatId !== chatId) {
throw new ApiError(
'invalid_request',
'cursor belongs to a different chat',
);
}
const anchor = cursor
? await this.prisma.message.findFirst({
where: { id: cursor.id, chatId },
select: { id: true, createdAt: true },
})
: null;
if (!anchor) {
throw new ApiError('invalid_request', 'invalid cursor');
}
after = Prisma.sql`AND (created_at, id) > (${anchor.createdAt}::timestamptz, ${anchor.id}::uuid)`;
}
const rows = await this.prisma.$queryRaw<MessageRow[]>`
SELECT id, chat_id AS "chatId", sender_id AS "senderId", text, created_at AS "createdAt",
ARRAY(
SELECT attachment_id::text FROM message_attachments
WHERE message_id = messages.id
ORDER BY position
) AS "attachmentIds"
FROM messages
WHERE chat_id = ${chatId}::uuid ${after}
ORDER BY created_at, id
LIMIT ${limit + 1}`;
const items = rows.slice(0, limit).map(toMessage);
const last = items[items.length - 1];
return {
items: items.map(toMessageResponse),
next_cursor:
rows.length > limit
? encodeMessageCursor({ chatId, id: last.id })
: null,
};
}
private assertMember(chatId: string, callerId: string): Promise<void> {
return assertChatMember(this.prisma, chatId, callerId);
}
private async assertAttachmentsInChat(
chatId: string,
attachmentIds: string[],
): Promise<void> {
if (attachmentIds.length === 0) {
return;
}
const found = await this.prisma.attachment.findMany({
where: { id: { in: attachmentIds }, chatId },
select: { status: true },
});
if (found.length !== attachmentIds.length) {
throw new ApiError('not_found', 'attachment not found');
}
if (found.some((row) => row.status === 'failed')) {
throw new ApiError('conflict', 'failed attachment cannot be attached');
}
}
}
function linkedMessage(row: MessageWithLinks): Message {
return toMessage({
...row,
attachmentIds: row.attachments.map((link) => link.attachmentId),
});
}
function toMessage(row: MessageRow): Message {
return {
id: row.id,
chatId: row.chatId,
senderId: row.senderId,
text: row.text,
createdAt: row.createdAt.toISOString(),
attachmentIds: row.attachmentIds,
};
}
+57
View File
@@ -0,0 +1,57 @@
import { Logger } from '@nestjs/common';
import { reconcile } from './attachments/reconciler';
import { PrismaService } from './db/prisma.service';
import { S3Service } from './storage/s3.service';
const DEFAULT_GRACE_SECONDS = 3600;
const DEFAULT_INTERVAL_SECONDS = 300;
const DEFAULT_STAGING_RETENTION_SECONDS = 86400;
function option(name: string, fallback: number): number {
const prefix = `--${name}=`;
const raw = process.argv.find((arg) => arg.startsWith(prefix));
const value = raw ? Number(raw.slice(prefix.length)) : fallback;
if (!Number.isInteger(value) || value < 0) {
throw new Error(`--${name} must be a non-negative integer`);
}
return value;
}
async function main(): Promise<void> {
process.once('SIGTERM', () => process.exit(0));
process.once('SIGINT', () => process.exit(0));
const apply = process.argv.includes('--apply');
const loop = process.argv.includes('--loop');
const graceSeconds = option('grace-seconds', DEFAULT_GRACE_SECONDS);
const intervalSeconds = option('interval-seconds', DEFAULT_INTERVAL_SECONDS);
const stagingRetentionSeconds = option(
'staging-retention-seconds',
Number(process.env.STAGING_RETENTION_SECONDS ?? DEFAULT_STAGING_RETENTION_SECONDS),
);
const prisma = new PrismaService();
const s3 = new S3Service();
do {
try {
const report = await reconcile(prisma, s3, {
apply,
graceMs: graceSeconds * 1000,
stagingRetentionMs: stagingRetentionSeconds * 1000,
});
process.stdout.write(`${JSON.stringify(report)}\n`);
} catch (error) {
const reason = error instanceof Error ? error.name : 'unknown error';
Logger.error(`reconcile pass aborted: ${reason}`, 'Reconciler');
process.exitCode = loop ? 0 : 1;
}
if (loop) {
await new Promise((resolve) => setTimeout(resolve, intervalSeconds * 1000));
}
} while (loop);
await prisma.$disconnect();
s3.onModuleDestroy();
}
main().catch((error) => {
Logger.error(error instanceof Error ? error.message : 'fatal error', 'Reconciler');
process.exit(1);
});
+214
View File
@@ -0,0 +1,214 @@
import './telemetry/register';
import { Logger } from '@nestjs/common';
import {
metrics,
propagation,
ROOT_CONTEXT,
Span,
SpanKind,
SpanStatusCode,
trace,
} from '@opentelemetry/api';
import { ConfirmChannel } from 'amqplib';
import { assertTopology, connectWithRetry, JOB_QUEUE, sleep } from './broker/broker';
import { PrismaService } from './db/prisma.service';
import { JobMessage } from './jobs/job-message';
import { recoverExpiredLeases, republishStale } from './jobs/job-store';
import { JsonLogger } from './telemetry/json-logger';
import { shutdownTelemetry } from './telemetry/sdk';
Logger.overrideLogger(new JsonLogger());
const BATCH_SIZE = 100;
const POLL_MS = Number(process.env.RELAY_POLL_MS ?? 500);
const SWEEP_MS = Number(process.env.RELAY_SWEEP_MS ?? 5000);
const STALE_MS = Number(process.env.JOB_STALE_SECONDS ?? 300) * 1000;
const FAULT = process.env.RELAY_FAULT;
const logger = new Logger('OutboxRelay');
const prisma = new PrismaService();
const tracer = trace.getTracer('messenger-relay');
let stopping = false;
const meter = metrics.getMeter('messenger-relay');
const queueDepth = meter.createObservableGauge('jobs.queue.depth', {
description: 'Attachment jobs by state (source of truth: attachment_jobs)',
});
const oldestAge = meter.createObservableGauge('jobs.queue.oldest_age', {
description: 'Age of the oldest due queued job (since accepted or retry became due)',
unit: 's',
});
meter.addBatchObservableCallback(
async (result) => {
const [row] = await prisma.$queryRaw<
{ queued: number; due: number; processing: number; oldest: number }[]
>`
SELECT
count(*) FILTER (WHERE status = 'queued')::int AS queued,
count(*) FILTER (WHERE status = 'queued' AND next_attempt_at <= now())::int AS due,
count(*) FILTER (WHERE status = 'processing')::int AS processing,
COALESCE(EXTRACT(EPOCH FROM now() - min(next_attempt_at)
FILTER (WHERE status = 'queued' AND next_attempt_at <= now())), 0)::float8 AS oldest
FROM attachment_jobs
WHERE status IN ('queued', 'processing')`.catch(() => []);
if (!row) {
return;
}
const queue = { 'messaging.destination.name': JOB_QUEUE };
result.observe(queueDepth, row.queued, { ...queue, state: 'queued' });
result.observe(queueDepth, row.due, { ...queue, state: 'due' });
result.observe(queueDepth, row.processing, { ...queue, state: 'processing' });
result.observe(oldestAge, row.oldest, queue);
},
[queueDepth, oldestAge],
);
function startPublishSpan(payload: JobMessage, headers: Record<string, string>): Span {
const parent = propagation.extract(ROOT_CONTEXT, payload);
const span = tracer.startSpan(
`${JOB_QUEUE} publish`,
{
kind: SpanKind.PRODUCER,
attributes: {
'messaging.system': 'rabbitmq',
'messaging.operation.type': 'publish',
'messaging.destination.name': JOB_QUEUE,
'messaging.message.id': payload.job_id,
'messaging.job.attempt': payload.attempt,
},
},
parent,
);
propagation.inject(trace.setSpan(parent, span), headers);
return span;
}
async function publishBatch(channel: ConfirmChannel): Promise<number> {
return prisma.$transaction(
async (tx) => {
const rows = await tx.$queryRaw<{ id: bigint; payload: JobMessage }[]>`
SELECT id, payload FROM outbox
WHERE published_at IS NULL AND available_at <= ${new Date()}
ORDER BY id
LIMIT ${BATCH_SIZE}
FOR UPDATE SKIP LOCKED`;
if (rows.length === 0) {
return 0;
}
const spans: Span[] = [];
for (const { payload } of rows) {
const headers: Record<string, string> = {};
spans.push(startPublishSpan(payload, headers));
channel.sendToQueue(JOB_QUEUE, Buffer.from(JSON.stringify(payload)), {
persistent: true,
contentType: 'application/json',
messageId: payload.job_id,
type: payload.type,
headers,
});
}
try {
await channel.waitForConfirms();
} catch (error) {
for (const span of spans) {
span.setStatus({ code: SpanStatusCode.ERROR, message: 'broker did not confirm' });
span.end();
}
throw error;
}
for (const span of spans) {
span.end();
}
logger.log(`published ${rows.length} job message(s), broker confirmed`);
if (FAULT === 'exit-after-confirm') {
logger.warn('fault injection: exit after broker confirm, before outbox is marked');
process.exit(137);
}
await tx.outboxEntry.updateMany({
where: { id: { in: rows.map((row) => row.id) } },
data: { publishedAt: new Date(), publishAttempts: { increment: 1 } },
});
return rows.length;
},
{ timeout: 15000 },
);
}
async function sweep(): Promise<void> {
const recovered = await recoverExpiredLeases(prisma);
const republished = await republishStale(prisma, STALE_MS);
if (recovered + republished > 0) {
logger.log(`lease expired: ${recovered}, stale queued republished: ${republished}`);
}
}
async function run(): Promise<void> {
let lastSweep = 0;
while (!stopping) {
const connection = await connectWithRetry(logger, () => stopping);
if (!connection) {
break;
}
let closed = false;
connection.on('close', () => {
closed = true;
});
connection.on('error', () => {
closed = true;
});
try {
const channel = await connection.createConfirmChannel();
channel.on('error', () => {
closed = true;
});
channel.on('close', () => {
closed = true;
});
await assertTopology(channel);
while (!stopping && !closed) {
let published = 0;
try {
published = await publishBatch(channel);
if (Date.now() - lastSweep >= SWEEP_MS) {
await sweep();
lastSweep = Date.now();
}
} catch (error) {
const reason = error instanceof Error ? error.name : 'unknown error';
logger.error(`relay pass failed: ${reason}`);
await sleep(1000);
}
if (published < BATCH_SIZE) {
await sleep(POLL_MS);
}
}
} catch (error) {
const reason = error instanceof Error ? error.name : 'unknown error';
logger.error(`broker channel failed: ${reason}`);
}
await connection.close().catch(() => undefined);
if (!stopping) {
logger.warn('broker connection lost, reconnecting');
await sleep(1000);
}
}
}
for (const signal of ['SIGTERM', 'SIGINT'] as const) {
process.once(signal, () => {
stopping = true;
});
}
run()
.then(async () => {
await prisma.$disconnect();
logger.log('stopped');
await shutdownTelemetry();
process.exit(0);
})
.catch((error) => {
logger.error(error instanceof Error ? error.message : 'fatal error');
process.exit(1);
});
+54
View File
@@ -0,0 +1,54 @@
import { Logger } from '@nestjs/common';
import { validate as isUuid } from './common/uuid';
import { PrismaService } from './db/prisma.service';
import { PIPELINE_VERSION } from './jobs/job-message';
import { replayJob } from './jobs/job-store';
const USAGE = 'usage: replay.js --list | <job_id|attachment_id>...';
async function main(): Promise<void> {
const args = process.argv.slice(2);
if (args.length === 0 || (args[0] !== '--list' && args.some((arg) => !isUuid(arg)))) {
throw new Error(USAGE);
}
const prisma = new PrismaService();
try {
if (args[0] === '--list') {
const failed = await prisma.attachmentJob.findMany({
where: { status: 'failed' },
orderBy: { finishedAt: 'asc' },
take: 100,
});
for (const job of failed) {
process.stdout.write(
`${JSON.stringify({
job_id: job.id,
attachment_id: job.attachmentId,
error_code: job.errorCode,
attempt: job.attempt,
last_error: job.lastError,
finished_at: job.finishedAt?.toISOString(),
})}\n`,
);
}
return;
}
const actor = process.env.OPERATOR ?? 'operator';
for (const id of args) {
const job = await prisma.attachmentJob.findFirst({
where: {
OR: [{ id }, { attachmentId: id, pipelineVersion: PIPELINE_VERSION }],
},
});
const result = job ? await replayJob(prisma, job.id, actor) : 'not_found';
process.stdout.write(`${JSON.stringify({ id, job_id: job?.id ?? null, result })}\n`);
}
} finally {
await prisma.$disconnect();
}
}
main().catch((error) => {
Logger.error(error instanceof Error ? error.message : 'fatal error', 'Replay');
process.exit(1);
});
+245
View File
@@ -0,0 +1,245 @@
import {
DeleteObjectCommand,
GetObjectCommand,
HeadBucketCommand,
ListObjectsV2Command,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3';
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
import { createReadStream, readFileSync } from 'fs';
import { Readable } from 'stream';
import { ApiError } from '../common/filters/api-error';
const CONNECT_TIMEOUT_MS = 1000;
const IDLE_TIMEOUT_MS = 5000;
const OPERATION_TIMEOUT_MS = 20000;
const HEALTH_TIMEOUT_MS = 1000;
export interface ObjectPage {
objects: { key: string; lastModified: Date }[];
nextToken?: string;
}
export interface StoredFile {
key: string;
path: string;
size: number;
contentType: string;
sha256: string;
}
@Injectable()
export class S3Service implements OnModuleDestroy {
private readonly logger = new Logger('S3Service');
private readonly client: S3Client;
private readonly streamClient: S3Client;
private readonly bucket = process.env.S3_BUCKET ?? 'messenger-attachments';
constructor() {
const config = {
endpoint: process.env.S3_ENDPOINT ?? 'http://s3:9000',
region: process.env.S3_REGION ?? 'us-east-1',
forcePathStyle: true,
maxAttempts: 1,
credentials: {
accessKeyId: readSecret('S3_ACCESS_KEY_ID'),
secretAccessKey: readSecret('S3_SECRET_ACCESS_KEY'),
},
};
this.client = new S3Client({
...config,
requestHandler: {
connectionTimeout: CONNECT_TIMEOUT_MS,
requestTimeout: IDLE_TIMEOUT_MS,
},
});
this.streamClient = new S3Client({
...config,
requestHandler: { connectionTimeout: CONNECT_TIMEOUT_MS },
});
}
async putFile(file: StoredFile): Promise<void> {
await this.guard(`put ${file.key}`, () =>
this.client.send(
new PutObjectCommand({
Bucket: this.bucket,
Key: file.key,
Body: createReadStream(file.path),
ContentLength: file.size,
ContentType: file.contentType,
ChecksumSHA256: Buffer.from(file.sha256, 'hex').toString('base64'),
}),
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
),
);
}
async putBuffer(
key: string,
body: Buffer,
contentType: string,
sha256: string,
): Promise<void> {
await this.guard(`put ${key}`, () =>
this.client.send(
new PutObjectCommand({
Bucket: this.bucket,
Key: key,
Body: body,
ContentLength: body.length,
ContentType: contentType,
ChecksumSHA256: Buffer.from(sha256, 'hex').toString('base64'),
}),
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
),
);
}
async getBuffer(key: string, maxBytes: number): Promise<Buffer> {
let body: Readable;
try {
const response = await this.client.send(
new GetObjectCommand({ Bucket: this.bucket, Key: key }),
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
);
if ((response.ContentLength ?? 0) > maxBytes) {
throw new ObjectTooLargeError(key);
}
body = response.Body as Readable;
} catch (error) {
if (error instanceof ObjectTooLargeError) {
throw error;
}
const name = error instanceof Error ? error.name : 'unknown error';
this.logger.error(`get ${key} failed: ${name}`);
if (name === 'NoSuchKey') {
throw new ObjectMissingError(key);
}
throw new ApiError('unavailable', 'object storage unavailable');
}
const chunks: Buffer[] = [];
let size = 0;
try {
for await (const chunk of body) {
size += chunk.length;
if (size > maxBytes) {
body.destroy();
throw new ObjectTooLargeError(key);
}
chunks.push(chunk);
}
} catch (error) {
if (error instanceof ObjectTooLargeError) {
throw error;
}
throw new ApiError('unavailable', 'object storage unavailable');
}
return Buffer.concat(chunks);
}
async getStream(key: string): Promise<Readable> {
const deadline = new AbortController();
const timer = setTimeout(() => deadline.abort(), IDLE_TIMEOUT_MS);
try {
const { Body } = await this.streamClient.send(
new GetObjectCommand({ Bucket: this.bucket, Key: key }),
{ abortSignal: deadline.signal },
);
return Body as Readable;
} catch (error) {
const name = error instanceof Error ? error.name : 'unknown error';
this.logger.error(`get ${key} failed: ${name}`);
if (name === 'NoSuchKey') {
throw new Error(`object ${key} is missing from storage`);
}
throw new ApiError('unavailable', 'object storage unavailable');
} finally {
clearTimeout(timer);
}
}
async isAvailable(): Promise<boolean> {
try {
await this.client.send(new HeadBucketCommand({ Bucket: this.bucket }), {
abortSignal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
});
return true;
} catch {
return false;
}
}
async listObjects(
prefix: string,
pageSize: number,
token?: string,
): Promise<ObjectPage> {
const page = await this.guard(`list ${prefix}`, () =>
this.client.send(
new ListObjectsV2Command({
Bucket: this.bucket,
Prefix: prefix,
MaxKeys: pageSize,
ContinuationToken: token,
}),
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
),
);
return {
objects: (page.Contents ?? []).map((object) => ({
key: object.Key!,
lastModified: object.LastModified!,
})),
nextToken: page.NextContinuationToken,
};
}
async delete(key: string): Promise<void> {
await this.guard(`delete ${key}`, () =>
this.client.send(
new DeleteObjectCommand({ Bucket: this.bucket, Key: key }),
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
),
);
}
onModuleDestroy(): void {
this.client.destroy();
this.streamClient.destroy();
}
private async guard<T>(action: string, run: () => Promise<T>): Promise<T> {
try {
return await run();
} catch (error) {
const reason = error instanceof Error ? error.name : 'unknown error';
this.logger.error(`${action} failed: ${reason}`);
throw new ApiError('unavailable', 'object storage unavailable');
}
}
}
export class ObjectMissingError extends Error {
constructor(key: string) {
super(`object ${key} is missing from storage`);
this.name = 'ObjectMissingError';
}
}
export class ObjectTooLargeError extends Error {
constructor(key: string) {
super(`object ${key} exceeds size limit`);
this.name = 'ObjectTooLargeError';
}
}
function readSecret(name: string): string {
const file = process.env[`${name}_FILE`];
const value = file ? readFileSync(file, 'utf8').trim() : process.env[name];
if (!value) {
throw new Error(`${name}_FILE or ${name} must be set`);
}
return value;
}
+9
View File
@@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { S3Service } from './s3.service';
@Global()
@Module({
providers: [S3Service],
exports: [S3Service],
})
export class StorageModule {}
+52
View File
@@ -0,0 +1,52 @@
import { metrics } from '@opentelemetry/api';
import { PrismaClient } from '@prisma/client';
interface Pool {
client: PrismaClient;
max: number;
}
const pools: Pool[] = [];
const meter = metrics.getMeter('messenger-db');
const connections = meter.createObservableGauge('db.client.connection.count', {
description: 'Prisma pool connections by state',
});
const pending = meter.createObservableGauge('db.client.connection.pending_requests', {
description: 'Queries waiting for a free pool connection',
});
const max = meter.createObservableGauge('db.client.connection.max', {
description: 'Configured pool size (connection_limit)',
});
meter.addBatchObservableCallback(
async (result) => {
for (const pool of pools) {
let gauges: Record<string, number>;
try {
const snapshot = await pool.client.$metrics.json();
gauges = Object.fromEntries(snapshot.gauges.map((gauge) => [gauge.key, gauge.value]));
} catch {
continue;
}
result.observe(connections, gauges.prisma_pool_connections_busy ?? 0, {
'db.client.connection.state': 'used',
});
result.observe(connections, gauges.prisma_pool_connections_idle ?? 0, {
'db.client.connection.state': 'idle',
});
result.observe(pending, gauges.prisma_client_queries_wait ?? 0);
result.observe(max, pool.max);
}
},
[connections, pending, max],
);
export function registerDbPool(client: PrismaClient, url: string): void {
let limit = 0;
try {
limit = Number(new URL(url).searchParams.get('connection_limit') ?? 0);
} catch {
limit = 0;
}
pools.push({ client, max: limit });
}
+66
View File
@@ -0,0 +1,66 @@
import { Logger } from '@nestjs/common';
import { metrics, trace } from '@opentelemetry/api';
import { FastifyInstance, FastifyRequest } from 'fastify';
export const LATENCY_BUCKETS = [
0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 2.5, 5, 10,
];
const METHODS = new Set(['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']);
const meter = metrics.getMeter('messenger-api');
const requests = meter.createCounter('http.server.requests', {
description: 'HTTP requests by method, route template and status class',
});
const duration = meter.createHistogram('http.server.request.duration', {
description: 'HTTP request duration',
unit: 's',
advice: { explicitBucketBoundaries: LATENCY_BUCKETS },
});
const logger = new Logger('Http');
export function routeTemplate(request: FastifyRequest): string {
const url = request.routeOptions?.url;
return url
? url.replace(/:([A-Za-z0-9_]+)/g, (_, name: string) => `{${name.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`)}}`)
: 'unmatched';
}
export function registerHttpTelemetry(instance: FastifyInstance): void {
instance.addHook('onRequest', (request, _reply, done) => {
const span = trace.getActiveSpan();
if (span) {
const route = routeTemplate(request);
span.updateName(`${methodOf(request)} ${route}`);
span.setAttribute('http.route', route);
span.setAttribute('messenger.request_id', request.requestId);
}
done();
});
instance.addHook('onResponse', (request, reply, done) => {
const route = routeTemplate(request);
const method = methodOf(request);
const attributes = {
'http.request.method': method,
'http.route': route,
'http.response.status_class': `${Math.floor(reply.statusCode / 100)}xx`,
};
requests.add(1, attributes);
duration.record(reply.elapsedTime / 1000, attributes);
if (!route.startsWith('/health')) {
logger.log({
event: 'http.request',
method,
route,
status: reply.statusCode,
duration_ms: Math.round(reply.elapsedTime * 100) / 100,
request_id: request.requestId,
});
}
done();
});
}
function methodOf(request: FastifyRequest): string {
return METHODS.has(request.method) ? request.method : '_OTHER';
}
+99
View File
@@ -0,0 +1,99 @@
import { LoggerService } from '@nestjs/common';
import { trace, TraceFlags } from '@opentelemetry/api';
import { AnyValueMap, logs, SeverityNumber } from '@opentelemetry/api-logs';
type Level = 'fatal' | 'error' | 'warn' | 'log' | 'debug' | 'verbose';
const SEVERITY: Record<Level, [SeverityNumber, string]> = {
fatal: [SeverityNumber.FATAL, 'FATAL'],
error: [SeverityNumber.ERROR, 'ERROR'],
warn: [SeverityNumber.WARN, 'WARN'],
log: [SeverityNumber.INFO, 'INFO'],
debug: [SeverityNumber.DEBUG, 'DEBUG'],
verbose: [SeverityNumber.TRACE, 'TRACE'],
};
type Field = string | number | boolean;
export class JsonLogger implements LoggerService {
private readonly otel = logs.getLogger('messenger');
constructor(private readonly service = process.env.OTEL_SERVICE_NAME ?? 'messenger') {}
fatal(message: unknown, ...params: unknown[]): void {
this.write('fatal', message, params);
}
error(message: unknown, ...params: unknown[]): void {
this.write('error', message, params);
}
warn(message: unknown, ...params: unknown[]): void {
this.write('warn', message, params);
}
log(message: unknown, ...params: unknown[]): void {
this.write('log', message, params);
}
debug(message: unknown, ...params: unknown[]): void {
this.write('debug', message, params);
}
verbose(message: unknown, ...params: unknown[]): void {
this.write('verbose', message, params);
}
private write(level: Level, message: unknown, params: unknown[]): void {
const last = params[params.length - 1];
const logger = typeof last === 'string' ? last : 'app';
const fields = toFields(message);
const event = typeof fields.event === 'string' ? fields.event : eventName(logger);
delete fields.event;
const [severityNumber, severityText] = SEVERITY[level];
const active = trace.getActiveSpan()?.spanContext();
const span = active && active.traceFlags & TraceFlags.SAMPLED ? active : undefined;
const line = JSON.stringify({
timestamp: new Date().toISOString(),
severity: severityText,
'service.name': this.service,
event,
logger,
...fields,
trace_id: span?.traceId,
span_id: span?.spanId,
});
process.stdout.write(line + '\n');
const attributes: AnyValueMap = { event, logger };
for (const [key, value] of Object.entries(fields)) {
if (key !== 'message') {
attributes[key] = value;
}
}
this.otel.emit({
severityNumber,
severityText,
body: line,
attributes,
});
}
}
function toFields(message: unknown): Record<string, Field> {
if (typeof message === 'object' && message !== null && !(message instanceof Error)) {
const fields: Record<string, Field> = {};
for (const [key, value] of Object.entries(message)) {
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
fields[key] = value;
}
}
return fields;
}
return { message: message instanceof Error ? message.message : String(message) };
}
function eventName(logger: string): string {
return logger
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
.toLowerCase();
}
+3
View File
@@ -0,0 +1,3 @@
import { startTelemetry } from './sdk';
startTelemetry();
+174
View File
@@ -0,0 +1,174 @@
import { Attributes, Context, Link, metrics, SpanKind } from '@opentelemetry/api';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { envDetectorSync, Resource } from '@opentelemetry/resources';
import { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';
import {
DropAggregation,
PeriodicExportingMetricReader,
View,
} from '@opentelemetry/sdk-metrics';
import { NodeSDK } from '@opentelemetry/sdk-node';
import {
BatchSpanProcessor,
ParentBasedSampler,
Sampler,
SamplingDecision,
SamplingResult,
} from '@opentelemetry/sdk-trace-base';
import { PrismaInstrumentation } from '@prisma/instrumentation';
import { readFileSync } from 'fs';
import { IncomingMessage } from 'http';
import { hostname } from 'os';
import { join } from 'path';
const EXPORT_TIMEOUT_MS = 5000;
const NOISY_LEAF_SPANS = new Set([
'prisma:client:serialize',
'prisma:engine:serialize',
'prisma:engine:response_json_serialization',
]);
const NOT_RECORDED: SamplingResult = { decision: SamplingDecision.NOT_RECORD };
class SkipRootDatabaseSpans implements Sampler {
shouldSample(
_context: Context,
_traceId: string,
name: string,
): SamplingResult {
return name.startsWith('prisma:')
? NOT_RECORDED
: { decision: SamplingDecision.RECORD_AND_SAMPLED };
}
toString(): string {
return 'SkipRootDatabaseSpans';
}
}
class MessengerSampler implements Sampler {
private readonly parentBased = new ParentBasedSampler({ root: new SkipRootDatabaseSpans() });
shouldSample(
context: Context,
traceId: string,
name: string,
kind: SpanKind,
attributes: Attributes,
links: Link[],
): SamplingResult {
if (NOISY_LEAF_SPANS.has(name)) {
return NOT_RECORDED;
}
return this.parentBased.shouldSample(context, traceId, name, kind, attributes, links);
}
toString(): string {
return 'MessengerSampler';
}
}
let sdk: NodeSDK | null = null;
export function telemetryEnabled(): boolean {
return Boolean(process.env.OTEL_EXPORTER_OTLP_ENDPOINT) && process.env.OTEL_SDK_DISABLED !== 'true';
}
export function startTelemetry(): void {
if (!telemetryEnabled() || sdk) {
return;
}
sdk = new NodeSDK({
resource: new Resource({
'service.version': serviceVersion(),
'service.instance.id': process.env.INSTANCE_ID ?? hostname(),
}),
resourceDetectors: [envDetectorSync],
sampler: new MessengerSampler(),
spanProcessors: [
new BatchSpanProcessor(new OTLPTraceExporter({ timeoutMillis: EXPORT_TIMEOUT_MS }), {
maxQueueSize: 2048,
maxExportBatchSize: 512,
exportTimeoutMillis: EXPORT_TIMEOUT_MS,
}),
],
logRecordProcessors: [
new BatchLogRecordProcessor(new OTLPLogExporter({ timeoutMillis: EXPORT_TIMEOUT_MS }), {
maxQueueSize: 2048,
maxExportBatchSize: 512,
exportTimeoutMillis: EXPORT_TIMEOUT_MS,
}),
],
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({ timeoutMillis: EXPORT_TIMEOUT_MS }),
exportIntervalMillis: 10000,
exportTimeoutMillis: EXPORT_TIMEOUT_MS,
}),
views: [
new View({
meterName: '@opentelemetry/instrumentation-http',
aggregation: new DropAggregation(),
}),
],
instrumentations: [
new HttpInstrumentation({
ignoreIncomingRequestHook: (request) => (request.url ?? '').startsWith('/health'),
ignoreOutgoingRequestHook: () => true,
requestHook: (span, request) => {
if (request instanceof IncomingMessage) {
const path = (request.url ?? '/').split('?')[0];
span.setAttribute('http.target', path);
span.setAttribute('http.url', path);
}
},
}),
new AwsInstrumentation({ suppressInternalInstrumentation: true }),
new PrismaInstrumentation(),
],
});
sdk.start();
registerProcessMetrics();
}
function registerProcessMetrics(): void {
const meter = metrics.getMeter('messenger-process');
meter
.createObservableCounter('process.cpu.time', { description: 'Process CPU time', unit: 's' })
.addCallback((result) => {
const usage = process.cpuUsage();
result.observe(usage.user / 1e6, { 'cpu.mode': 'user' });
result.observe(usage.system / 1e6, { 'cpu.mode': 'system' });
});
meter
.createObservableGauge('process.memory.usage', { description: 'Resident set size', unit: 'By' })
.addCallback((result) => result.observe(process.memoryUsage.rss()));
meter
.createObservableGauge('process.memory.heap_used', { description: 'V8 heap used', unit: 'By' })
.addCallback((result) => result.observe(process.memoryUsage().heapUsed));
}
export async function shutdownTelemetry(): Promise<void> {
if (!sdk) {
return;
}
const current = sdk;
sdk = null;
await Promise.race([
current.shutdown().catch(() => undefined),
new Promise((resolve) => setTimeout(resolve, EXPORT_TIMEOUT_MS)),
]);
}
function serviceVersion(): string {
try {
const raw = readFileSync(join(__dirname, '..', '..', 'package.json'), 'utf8');
return String(JSON.parse(raw).version);
} catch {
return 'unknown';
}
}
+18
View File
@@ -0,0 +1,18 @@
import { Matches } from 'class-validator';
import { CodePointLength } from '../../common/dto/code-point-length.validator';
import { NotBlank } from '../../common/dto/not-blank.validator';
export class CreateUserDto {
@Matches(/^[a-z0-9_]{3,32}$/, {
message: 'username must match [a-z0-9_]{3,32}',
})
username!: string;
@NotBlank()
@CodePointLength(1, 100)
display_name!: string;
@NotBlank()
@CodePointLength(12, 128)
password!: string;
}
+17
View File
@@ -0,0 +1,17 @@
import { User } from '../../common/domain';
export interface UserResponse {
id: string;
username: string;
display_name: string;
created_at: string;
}
export function toUserResponse(user: User): UserResponse {
return {
id: user.id,
username: user.username,
display_name: user.displayName,
created_at: user.createdAt,
};
}
+36
View File
@@ -0,0 +1,36 @@
import {
Body,
Controller,
Get,
HttpCode,
Post,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { User } from '../common/domain';
import { CreateUserDto } from './dto/create-user.dto';
import { toUserResponse, UserResponse } from './dto/user.response';
import { UsersService } from './users.service';
@Controller('api/v1/users')
export class UsersController {
constructor(private readonly users: UsersService) {}
@Post()
@HttpCode(201)
async create(@Body() dto: CreateUserDto): Promise<UserResponse> {
const user = await this.users.register(
dto.username,
dto.display_name,
dto.password,
);
return toUserResponse(user);
}
@Get('me')
@UseGuards(AuthGuard)
me(@CurrentUser() user: User): UserResponse {
return toUserResponse(user);
}
}
+10
View File
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
@Module({
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
+48
View File
@@ -0,0 +1,48 @@
import { Injectable } from '@nestjs/common';
import { hashPassword } from '../auth/password';
import { ApiError } from '../common/filters/api-error';
import { User } from '../common/domain';
import { PrismaService, isUniqueViolation } from '../db/prisma.service';
@Injectable()
export class UsersService {
constructor(private readonly prisma: PrismaService) {}
async register(
username: string,
displayName: string,
password: string,
): Promise<User> {
const passwordHash = await hashPassword(password);
try {
const row = await this.prisma.user.create({
data: { username, displayName: displayName.trim(), passwordHash },
});
return toUser(row);
} catch (error) {
if (isUniqueViolation(error)) {
throw new ApiError('conflict', 'username is already taken');
}
throw error;
}
}
async findById(id: string): Promise<User | null> {
const row = await this.prisma.user.findUnique({ where: { id } });
return row ? toUser(row) : null;
}
}
export function toUser(row: {
id: string;
username: string;
displayName: string;
createdAt: Date;
}): User {
return {
id: row.id,
username: row.username,
displayName: row.displayName,
createdAt: row.createdAt.toISOString(),
};
}
+223
View File
@@ -0,0 +1,223 @@
import './telemetry/register';
import { Logger } from '@nestjs/common';
import {
context,
Context,
metrics,
propagation,
ROOT_CONTEXT,
SpanKind,
SpanStatusCode,
trace,
} from '@opentelemetry/api';
import { Channel, ConsumeMessage } from 'amqplib';
import { assertTopology, connectWithRetry, JOB_QUEUE, sleep } from './broker/broker';
import { PrismaService } from './db/prisma.service';
import { JOB_TYPE, JobMessage, parseJobMessage } from './jobs/job-message';
import { JobProcessor, ProcessorHooks, processorOptions } from './jobs/processor';
import { S3Service } from './storage/s3.service';
import { JsonLogger } from './telemetry/json-logger';
import { shutdownTelemetry } from './telemetry/sdk';
Logger.overrideLogger(new JsonLogger());
const CONCURRENCY = Number(process.env.WORKER_CONCURRENCY ?? 2);
const INFRA_RETRY_MS = Number(process.env.WORKER_INFRA_RETRY_MS ?? 5000);
const DRAIN_TIMEOUT_MS = Number(process.env.WORKER_DRAIN_TIMEOUT_MS ?? 25000);
const FAULT = process.env.WORKER_FAULT;
const logger = new Logger('Worker');
const prisma = new PrismaService();
const s3 = new S3Service();
const processor = new JobProcessor(prisma, s3, processorOptions(), faultHooks());
let stopping = false;
let inFlight = 0;
const JOB_BUCKETS = [0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 2.5, 5, 10, 30, 60, 120];
const tracer = trace.getTracer('messenger-worker');
const meter = metrics.getMeter('messenger-worker');
const jobsProcessed = meter.createCounter('jobs.processed', {
description: 'Job deliveries handled by the worker, by job type and outcome',
});
const jobDuration = meter.createHistogram('jobs.duration', {
description: 'Job delivery handling duration',
unit: 's',
advice: { explicitBucketBoundaries: JOB_BUCKETS },
});
meter
.createObservableGauge('worker.jobs.busy', { description: 'Deliveries being handled now' })
.addCallback((result) => result.observe(inFlight));
meter
.createObservableGauge('worker.jobs.capacity', { description: 'Concurrent deliveries allowed (prefetch)' })
.addCallback((result) => result.observe(CONCURRENCY));
function parentContext(delivery: ConsumeMessage, message: JobMessage | null): Context {
const headers = delivery.properties.headers ?? {};
if (typeof headers.traceparent === 'string') {
return propagation.extract(ROOT_CONTEXT, headers);
}
return message ? propagation.extract(ROOT_CONTEXT, message) : ROOT_CONTEXT;
}
function faultHooks(): ProcessorHooks {
const crash = (point: string) => async () => {
logger.warn(`fault injection: worker killed ${point}`);
process.exit(137);
};
if (FAULT === 'exit-after-store') {
return { afterStore: crash('after storing objects, before commit and ack') };
}
if (FAULT === 'exit-after-commit') {
return { afterCommit: crash('after commit, before ack') };
}
return {};
}
async function handle(channel: Channel, delivery: ConsumeMessage): Promise<void> {
inFlight++;
const started = performance.now();
const message = parseJobMessage(delivery.content);
const span = tracer.startSpan(
`${JOB_QUEUE} process`,
{
kind: SpanKind.CONSUMER,
attributes: {
'messaging.system': 'rabbitmq',
'messaging.operation.type': 'process',
'messaging.destination.name': JOB_QUEUE,
'messaging.message.id': message?.job_id ?? 'malformed',
'messaging.rabbitmq.redelivered': delivery.fields.redelivered,
'messaging.job.attempt': message?.attempt ?? 0,
},
},
parentContext(delivery, message),
);
const active = trace.setSpan(context.active(), span);
let outcome = 'interrupted';
let requeue = false;
try {
await context.with(active, async () => {
if (!message) {
outcome = 'malformed';
logger.warn('malformed job message moved to dead letter queue');
channel.nack(delivery, false, false);
return;
}
const result = await processor.handle(message);
outcome = result === 'ready' ? 'success' : result;
logger.log({
event: 'job.handled',
job_id: message.job_id,
attachment_id: message.attachment_id,
attempt: message.attempt,
redelivered: delivery.fields.redelivered,
outcome,
duration_ms: Math.round(performance.now() - started),
});
channel.ack(delivery);
});
span.setAttribute('messenger.job.outcome', outcome);
if (outcome === 'failed' || outcome === 'malformed') {
span.setStatus({ code: SpanStatusCode.ERROR, message: outcome });
}
} catch (error) {
const reason = error instanceof Error ? error.name : 'unknown error';
span.setAttribute('messenger.job.outcome', outcome);
span.setStatus({ code: SpanStatusCode.ERROR, message: reason });
context.with(active, () =>
logger.error(`job handling interrupted (${reason}), returning message after ${INFRA_RETRY_MS} ms`),
);
requeue = true;
} finally {
span.end();
const attributes = { 'job.type': JOB_TYPE, outcome };
jobsProcessed.add(1, attributes);
jobDuration.record((performance.now() - started) / 1000, attributes);
}
if (requeue) {
await sleep(INFRA_RETRY_MS);
try {
channel.nack(delivery, false, true);
} catch {
logger.warn('channel closed, broker will redeliver unacked message');
}
}
inFlight--;
}
async function run(): Promise<void> {
while (!stopping) {
const connection = await connectWithRetry(logger, () => stopping);
if (!connection) {
break;
}
const closed = new Promise<void>((resolve) => {
connection.on('close', () => resolve());
connection.on('error', () => resolve());
});
try {
const channel = await connection.createChannel();
await assertTopology(channel);
await channel.prefetch(CONCURRENCY);
const { consumerTag } = await channel.consume(
JOB_QUEUE,
(delivery) => {
if (delivery) {
void handle(channel, delivery);
}
},
{ noAck: false },
);
logger.log(`consuming ${JOB_QUEUE} with prefetch ${CONCURRENCY}`);
const stop = new Promise<void>((resolve) => {
const timer = setInterval(() => {
if (stopping) {
clearInterval(timer);
resolve();
}
}, 200);
void closed.then(() => {
clearInterval(timer);
resolve();
});
});
await stop;
if (stopping) {
await channel.cancel(consumerTag).catch(() => undefined);
const deadline = Date.now() + DRAIN_TIMEOUT_MS;
while (inFlight > 0 && Date.now() < deadline) {
await sleep(100);
}
logger.log(`drained, ${inFlight} job(s) left unacked for redelivery`);
}
} catch (error) {
const reason = error instanceof Error ? error.name : 'unknown error';
logger.error(`broker channel failed: ${reason}`);
}
await connection.close().catch(() => undefined);
if (!stopping) {
logger.warn('broker connection lost, reconnecting');
await sleep(1000);
}
}
}
for (const signal of ['SIGTERM', 'SIGINT'] as const) {
process.once(signal, () => {
stopping = true;
});
}
run()
.then(async () => {
await prisma.$disconnect();
s3.onModuleDestroy();
logger.log('stopped');
await shutdownTelemetry();
process.exit(0);
})
.catch((error) => {
logger.error(error instanceof Error ? error.message : 'fatal error');
process.exit(1);
});
+686
View File
@@ -0,0 +1,686 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import { PrismaService } from '../src/db/prisma.service';
import {
bearer,
createTestApp,
PASSWORD,
registerUser,
TestUser,
} from './helpers';
describe('API мессенджера (e2e) — граничные случаи сверх публичного smoke', () => {
let app: INestApplication;
let server: any;
beforeAll(async () => {
({ app, server } = await createTestApp());
});
afterAll(async () => {
await app.close();
});
function createUser(label: string): Promise<TestUser> {
return registerUser(server, label);
}
describe('POST /api/v1/users', () => {
it('отклоняет некорректный JSON с кодом 400', async () => {
const res = await request(server)
.post('/api/v1/users')
.set('Content-Type', 'application/json')
.send('{not valid json')
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
expect(res.body.error.request_id).toBeTruthy();
});
it('отклоняет тело с лишними полями', async () => {
const res = await request(server)
.post('/api/v1/users')
.send({
username: 'validname1',
display_name: 'Valid',
password: PASSWORD,
extra: 'nope',
})
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('отклоняет username с заглавными буквами', async () => {
await request(server)
.post('/api/v1/users')
.send({ username: 'InvalidName', display_name: 'X', password: PASSWORD })
.expect(400);
});
it('отклоняет display_name из одних пробелов', async () => {
await request(server)
.post('/api/v1/users')
.send({ username: 'wsonlyname1', display_name: ' ', password: PASSWORD })
.expect(400);
});
it('отклоняет дубликат username с кодом 409', async () => {
const user = await createUser('dup');
const res = await request(server)
.post('/api/v1/users')
.send({ username: user.username, display_name: 'Someone else', password: PASSWORD })
.expect(409);
expect(res.body.error.code).toBe('conflict');
});
it('никогда не возвращает поле пароля или его хэша', async () => {
const res = await request(server)
.post('/api/v1/users')
.send({
username: `nopass_${randomUUID().slice(0, 8)}`,
display_name: 'X',
password: PASSWORD,
})
.expect(201);
expect(res.body).not.toHaveProperty('password');
expect(res.body).not.toHaveProperty('password_hash');
});
it('отклоняет username короче 3 символов', async () => {
await request(server)
.post('/api/v1/users')
.send({ username: 'ab', display_name: 'X', password: PASSWORD })
.expect(400);
});
it('отклоняет username длиннее 32 символов', async () => {
await request(server)
.post('/api/v1/users')
.send({ username: 'a'.repeat(33), display_name: 'X', password: PASSWORD })
.expect(400);
});
it('отклоняет username с недопустимыми символами (дефис)', async () => {
await request(server)
.post('/api/v1/users')
.send({ username: 'has-hyphen', display_name: 'X', password: PASSWORD })
.expect(400);
});
it('принимает username ровно на границах длины 3 и 32 символа', async () => {
await request(server)
.post('/api/v1/users')
.send({
username: `a${randomUUID().slice(0, 2)}`,
display_name: 'X',
password: PASSWORD,
})
.expect(201);
const longest = ('z' + randomUUID().replace(/-/g, '')).slice(0, 32);
await request(server)
.post('/api/v1/users')
.send({ username: longest, display_name: 'X', password: PASSWORD })
.expect(201);
});
it('отклоняет display_name длиннее 100 Unicode code points', async () => {
await request(server)
.post('/api/v1/users')
.send({
username: `toolongname_${randomUUID().slice(0, 6)}`,
display_name: 'x'.repeat(101),
password: PASSWORD,
})
.expect(400);
});
it('принимает display_name ровно из 100 code points, считая эмодзи вне BMP за один символ', async () => {
const displayName = '👋'.repeat(100);
expect([...displayName].length).toBe(100);
await request(server)
.post('/api/v1/users')
.send({
username: `emojiname_${randomUUID().slice(0, 6)}`,
display_name: displayName,
password: PASSWORD,
})
.expect(201);
});
it('отклоняет display_name из 101 code point, состоящий из эмодзи вне BMP', async () => {
const displayName = '👋'.repeat(100) + 'x';
expect([...displayName].length).toBe(101);
await request(server)
.post('/api/v1/users')
.send({
username: `emojitoolong_${randomUUID().slice(0, 6)}`,
display_name: displayName,
password: PASSWORD,
})
.expect(400);
});
});
describe('контроль доступа', () => {
it('возвращает 401 раньше 404, если и актор, и ресурс невалидны', async () => {
const res = await request(server)
.get(`/api/v1/chats/${randomUUID()}`)
.set('Authorization', 'Bearer not-a-token')
.expect(401);
expect(res.body.error.code).toBe('unauthorized');
});
it('возвращает 404 (не 403), когда чат запрашивает не его участник', async () => {
const alice = await createUser('alice');
const bob = await createUser('bob');
const eve = await createUser('eve');
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Private', member_ids: [alice.id, bob.id] })
.expect(201);
const res = await request(server)
.get(`/api/v1/chats/${chat.body.id}`)
.set('Authorization', bearer(eve))
.expect(404);
expect(res.body.error.code).toBe('not_found');
});
it('отклоняет создание чата, если вызывающий не входит в member_ids', async () => {
const alice = await createUser('alice2');
const bob = await createUser('bob2');
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Missing caller', member_ids: [bob.id] })
.expect(400);
});
it('отклоняет создание чата с дублирующимися member_ids', async () => {
const alice = await createUser('alice3');
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({
title: 'Dup members',
member_ids: [alice.id, alice.id],
})
.expect(400);
});
it('считает один UUID в разном регистре дубликатом и узнаёт вызывающего в верхнем регистре', async () => {
const alice = await createUser('alicecase');
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Case', member_ids: [alice.id, alice.id.toUpperCase()] })
.expect(400);
const res = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Upper', member_ids: [alice.id.toUpperCase()] })
.expect(201);
expect(res.body.member_ids).toEqual([alice.id]);
});
it('принимает ровно 100 участников и отклоняет 101', async () => {
const alice = await createUser('max');
const ids = Array.from({ length: 100 }, () => randomUUID());
await app.get(PrismaService).user.createMany({
data: ids.map((id) => ({
id,
username: `m_${id.replace(/-/g, '').slice(0, 30)}`,
displayName: 'Member',
passwordHash: 'not-a-real-hash',
})),
});
const ok = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Max', member_ids: [alice.id, ...ids.slice(0, 99)] })
.expect(201);
expect(ok.body.member_ids).toHaveLength(100);
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Too many', member_ids: [alice.id, ...ids] })
.expect(400);
});
it('отклоняет создание чата с неизвестным участником', async () => {
const alice = await createUser('alice4');
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({
title: 'Unknown member',
member_ids: [alice.id, randomUUID()],
})
.expect(400);
});
it('отклоняет создание чата с пустым/пробельным title', async () => {
const alice = await createUser('alice4b');
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: ' ', member_ids: [alice.id] })
.expect(400);
});
it('возвращает 400 для синтаксически некорректного chat_id (не UUID)', async () => {
const alice = await createUser('alice4c');
const res = await request(server)
.get('/api/v1/chats/not-a-uuid')
.set('Authorization', bearer(alice))
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('возвращает 404 для корректного по формату, но несуществующего chat_id', async () => {
const alice = await createUser('alice4d');
await request(server)
.get(`/api/v1/chats/${randomUUID()}`)
.set('Authorization', bearer(alice))
.expect(404);
});
});
describe('GET /api/v1/chats (пагинация)', () => {
it('использует limit=50 по умолчанию, если limit не передан', async () => {
const alice = await createUser('pgalice1');
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Only chat', member_ids: [alice.id] })
.expect(201);
const res = await request(server)
.get('/api/v1/chats')
.set('Authorization', bearer(alice))
.expect(200);
expect(res.body.items.length).toBeGreaterThanOrEqual(1);
expect(res.body.items.length).toBeLessThanOrEqual(50);
});
it('отклоняет нечисловой limit', async () => {
const alice = await createUser('pgalice2');
await request(server)
.get('/api/v1/chats?limit=abc')
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет limit=0', async () => {
const alice = await createUser('pgalice3');
await request(server)
.get('/api/v1/chats?limit=0')
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет повторённые limit и cursor', async () => {
const alice = await createUser('pgrepeat');
for (const query of ['limit=1&limit=2', 'cursor=a&cursor=b']) {
await request(server)
.get(`/api/v1/chats?${query}`)
.set('Authorization', bearer(alice))
.expect(400);
}
});
it('отклоняет cursor, не являющийся корректным base64url JSON', async () => {
const alice = await createUser('pgalice4');
await request(server)
.get('/api/v1/chats')
.query({ cursor: 'not-a-real-cursor!!!' })
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет корректно закодированный cursor с id чата, который никогда не существовал', async () => {
const alice = await createUser('pgalice4b');
const bogusCursor = Buffer.from(
JSON.stringify({ id: randomUUID() }),
'utf8',
).toString('base64url');
const res = await request(server)
.get('/api/v1/chats')
.query({ cursor: bogusCursor })
.set('Authorization', bearer(alice))
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('обходит весь список чатов по страницам без пропусков и дублей', async () => {
const alice = await createUser('pgalice5');
const titles = ['c1', 'c2', 'c3', 'c4', 'c5'];
for (const title of titles) {
await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title, member_ids: [alice.id] })
.expect(201);
}
const seen: string[] = [];
let cursor: string | null = null;
do {
const res: request.Response = await request(server)
.get('/api/v1/chats')
.query({ limit: 2, ...(cursor ? { cursor } : {}) })
.set('Authorization', bearer(alice))
.expect(200);
seen.push(
...res.body.items.map((c: { title: string }) => c.title),
);
cursor = res.body.next_cursor;
} while (cursor);
expect(seen).toEqual(titles);
});
});
describe('сообщения', () => {
async function createChat(alice: TestUser, bob: TestUser) {
const res = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Chat', member_ids: [alice.id, bob.id] })
.expect(201);
return res.body as { id: string };
}
it('не принимает sender_id, переданный клиентом', async () => {
const alice = await createUser('alice5');
const bob = await createUser('bob5');
const chat = await createChat(alice, bob);
const res = await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'hi', sender_id: bob.id })
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('считает attachment_ids: [] равным отсутствию поля, неизвестный ID — 404, неверный тип — 400', async () => {
const alice = await createUser('aliceatt');
const bob = await createUser('bobatt');
const chat = await createChat(alice, bob);
const path = `/api/v1/chats/${chat.id}/messages`;
const key = randomUUID();
const first = await request(server)
.post(path)
.set('Idempotency-Key', key)
.set('Authorization', bearer(alice))
.send({ text: 'att' })
.expect(201);
const replay = await request(server)
.post(path)
.set('Idempotency-Key', key)
.set('Authorization', bearer(alice))
.send({ attachment_ids: [], text: 'att' })
.expect(201);
expect(replay.body).toEqual(first.body);
await request(server)
.post(path)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'x', attachment_ids: [randomUUID()] })
.expect(404);
for (const attachment_ids of [null, 'x']) {
await request(server)
.post(path)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'x', attachment_ids })
.expect(400);
}
});
it('отклоняет пустое сообщение', async () => {
const alice = await createUser('alice6');
const bob = await createUser('bob6');
const chat = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: '' })
.expect(400);
});
it('отклоняет сообщение из одних пробелов', async () => {
const alice = await createUser('alice7');
const bob = await createUser('bob7');
const chat = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: ' \n\t ' })
.expect(400);
});
it('отклоняет отправку в чат, где вызывающий не участник', async () => {
const alice = await createUser('alice8');
const bob = await createUser('bob8');
const eve = await createUser('eve8');
const chat = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(eve))
.send({ text: 'intrusion' })
.expect(404);
});
it('отклоняет limit=0 при чтении истории', async () => {
const alice = await createUser('alice9');
const bob = await createUser('bob9');
const chat = await createChat(alice, bob);
await request(server)
.get(`/api/v1/chats/${chat.id}/messages?limit=0`)
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет limit=101 при чтении истории', async () => {
const alice = await createUser('alice10');
const bob = await createUser('bob10');
const chat = await createChat(alice, bob);
await request(server)
.get(`/api/v1/chats/${chat.id}/messages?limit=101`)
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет нечисловой limit', async () => {
const alice = await createUser('alice10b');
const bob = await createUser('bob10b');
const chat = await createChat(alice, bob);
await request(server)
.get(`/api/v1/chats/${chat.id}/messages?limit=abc`)
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет текст сообщения длиннее 4000 Unicode code points', async () => {
const alice = await createUser('alice10c');
const bob = await createUser('bob10c');
const chat = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'x'.repeat(4001) })
.expect(400);
});
it('принимает текст сообщения ровно из 4000 Unicode code points', async () => {
const alice = await createUser('alice10d');
const bob = await createUser('bob10d');
const chat = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'x'.repeat(4000) })
.expect(201);
});
it('отклоняет текст сообщения свыше 4000 code points, состоящий из эмодзи вне BMP', async () => {
const alice = await createUser('alice10e');
const bob = await createUser('bob10e');
const chat = await createChat(alice, bob);
const text = '👋'.repeat(4001);
expect([...text].length).toBe(4001);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text })
.expect(400);
});
it('отклоняет cursor, не являющийся корректным base64url JSON', async () => {
const alice = await createUser('alice10f');
const bob = await createUser('bob10f');
const chat = await createChat(alice, bob);
await request(server)
.get(`/api/v1/chats/${chat.id}/messages`)
.query({ cursor: '!!!not-valid!!!' })
.set('Authorization', bearer(alice))
.expect(400);
});
it('отклоняет корректно закодированный cursor с id сообщения, которое никогда не существовало в этом чате', async () => {
const alice = await createUser('alice10i');
const bob = await createUser('bob10i');
const chat = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'only message' })
.expect(201);
const bogusCursor = Buffer.from(
JSON.stringify({ chatId: chat.id, id: randomUUID() }),
'utf8',
).toString('base64url');
const res = await request(server)
.get(`/api/v1/chats/${chat.id}/messages`)
.query({ cursor: bogusCursor })
.set('Authorization', bearer(alice))
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('возвращает 400 для синтаксически некорректного chat_id при отправке сообщения', async () => {
const alice = await createUser('alice10g');
const res = await request(server)
.post('/api/v1/chats/not-a-uuid/messages')
.set('Authorization', bearer(alice))
.send({ text: 'hi' })
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('возвращает 400 для синтаксически некорректного chat_id при чтении истории', async () => {
const alice = await createUser('alice10h');
const res = await request(server)
.get('/api/v1/chats/not-a-uuid/messages')
.set('Authorization', bearer(alice))
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('отклоняет cursor, взятый из другого чата', async () => {
const alice = await createUser('alice11');
const bob = await createUser('bob11');
const chatA = await createChat(alice, bob);
const chatB = await createChat(alice, bob);
await request(server)
.post(`/api/v1/chats/${chatA.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'first' })
.expect(201);
await request(server)
.post(`/api/v1/chats/${chatA.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text: 'second' })
.expect(201);
const page1 = await request(server)
.get(`/api/v1/chats/${chatA.id}/messages?limit=1`)
.set('Authorization', bearer(alice))
.expect(200);
expect(page1.body.next_cursor).toBeTruthy();
const res = await request(server)
.get(`/api/v1/chats/${chatB.id}/messages`)
.query({ cursor: page1.body.next_cursor })
.set('Authorization', bearer(alice))
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
});
it('обходит всю историю по страницам без пропусков и дублей', async () => {
const alice = await createUser('alice12');
const bob = await createUser('bob12');
const chat = await createChat(alice, bob);
const sentTexts = ['m1', 'm2', 'm3', 'm4', 'm5'];
for (const text of sentTexts) {
await request(server)
.post(`/api/v1/chats/${chat.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(alice))
.send({ text })
.expect(201);
}
const seen: string[] = [];
let cursor: string | null = null;
do {
const res: request.Response = await request(server)
.get(`/api/v1/chats/${chat.id}/messages`)
.query({ limit: 2, ...(cursor ? { cursor } : {}) })
.set('Authorization', bearer(alice))
.expect(200);
seen.push(
...res.body.items.map((m: { text: string }) => m.text),
);
cursor = res.body.next_cursor;
} while (cursor);
expect(seen).toEqual(sentTexts);
});
});
describe('X-Request-Id', () => {
it('присутствует и уникален для независимых запросов', async () => {
const first = await request(server).get('/health/live').expect(200);
const second = await request(server)
.get('/health/live')
.expect(200);
expect(first.headers['x-request-id']).toBeTruthy();
expect(second.headers['x-request-id']).toBeTruthy();
expect(first.headers['x-request-id']).not.toBe(
second.headers['x-request-id'],
);
});
it('совпадает с request_id внутри тела ошибки', async () => {
const res = await request(server)
.get('/api/v1/users/me')
.expect(401);
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
});
});
});
+302
View File
@@ -0,0 +1,302 @@
import { INestApplication } from '@nestjs/common';
import { createHash, randomUUID } from 'crypto';
import request from 'supertest';
import { PrismaService } from '../src/db/prisma.service';
import { S3Service } from '../src/storage/s3.service';
import {
bearer,
createTestApp,
processAttachment,
registerUser,
TestUser,
} from './helpers';
import { jpeg, SAMPLE_PNG } from './images';
describe('Скачивание и привязка вложений (e2e)', () => {
let app: INestApplication;
let server: any;
let alice: TestUser;
let bob: TestUser;
let eve: TestUser;
let chatId: string;
let otherChatId: string;
const createChat = async (owner: TestUser, members: TestUser[]) => {
const res = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(owner))
.send({ title: 'Files', member_ids: members.map((m) => m.id) })
.expect(201);
return res.body.id as string;
};
const upload = async (
user = alice,
chat = chatId,
body: Buffer = SAMPLE_PNG,
type = 'image/png',
) => {
const res = await request(server)
.post(`/api/v1/chats/${chat}/attachments?filename=${encodeURIComponent('фото 1.png')}`)
.set('Authorization', bearer(user))
.set('Content-Type', type)
.send(body)
.expect(202);
expect(await processAttachment(app, res.body.id)).toBe('ready');
const ready = await request(server)
.get(`/api/v1/attachments/${res.body.id}`)
.set('Authorization', bearer(user))
.expect(200);
return ready.body;
};
const get = (path: string, user: TestUser | null = bob) => {
const req = request(server).get(path).buffer(true).parse(binaryParser);
return user ? req.set('Authorization', bearer(user)) : req;
};
const send = (
body: unknown,
{ user = alice, chat = chatId, key = randomUUID() }: { user?: TestUser; chat?: string; key?: string } = {},
) =>
request(server)
.post(`/api/v1/chats/${chat}/messages`)
.set('Authorization', bearer(user))
.set('Idempotency-Key', key)
.send(body as object);
beforeAll(async () => {
({ app, server } = await createTestApp());
alice = await registerUser(server, 'acc_alice');
bob = await registerUser(server, 'acc_bob');
eve = await registerUser(server, 'acc_eve');
chatId = await createChat(alice, [alice, bob]);
otherChatId = await createChat(eve, [eve]);
});
afterAll(async () => {
await app.close();
});
describe('чтение', () => {
it('участник получает метаданные и оригинал побайтно', async () => {
const attachment = await upload();
const meta = await request(server)
.get(`/api/v1/attachments/${attachment.id}`)
.set('Authorization', bearer(bob))
.expect(200);
expect(meta.body).toEqual(attachment);
const res = await get(`/api/v1/attachments/${attachment.id}/content`).expect(200);
expect(res.headers['content-type']).toBe('image/png');
expect(res.headers['content-length']).toBe(String(SAMPLE_PNG.length));
expect(res.headers['x-content-type-options']).toBe('nosniff');
expect(res.headers['x-request-id']).toBeTruthy();
expect(res.headers['x-instance-id']).toBeTruthy();
expect(res.headers['location']).toBeUndefined();
expect(createHash('sha256').update(res.body).digest('hex')).toBe(attachment.sha256);
});
it('JPEG отдаётся с истинным Content-Type', async () => {
const body = await jpeg();
const attachment = await upload(alice, chatId, body, 'image/jpeg');
const res = await get(`/api/v1/attachments/${attachment.id}/content`).expect(200);
expect(res.headers['content-type']).toBe('image/jpeg');
expect(Buffer.compare(res.body, body)).toBe(0);
});
it('посторонний и неизвестный ID получают одинаковый 404', async () => {
const attachment = await upload();
for (const suffix of ['', '/content']) {
const foreign = await get(`/api/v1/attachments/${attachment.id}${suffix}`, eve);
const unknown = await get(`/api/v1/attachments/${randomUUID()}${suffix}`, eve);
expect(foreign.status).toBe(404);
expect(unknown.status).toBe(404);
const parsed = JSON.parse(foreign.body.toString());
expect(parsed.error.code).toBe('not_found');
expect(parsed.error.message).toBe(JSON.parse(unknown.body.toString()).error.message);
}
});
it.each([
['nil', '00000000-0000-0000-0000-000000000000'],
['v7', '018f6f3a-7b1c-7def-8abc-0123456789ab'],
])('UUID %s корректного формата — 404, а не 400', async (_name, id) => {
await get(`/api/v1/attachments/${id}`).expect(404);
await get(`/api/v1/attachments/${id}/content`).expect(404);
await get(`/api/v1/chats/${id}`, alice).expect(404);
await send({ text: 'x', attachment_ids: [id] }).expect(404);
await send({ text: 'x' }, { chat: id }).expect(404);
await request(server)
.post(`/api/v1/chats/${id}/attachments?filename=a.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG)
.expect(404);
});
it('без токена 401, неверный UUID 400', async () => {
const attachment = await upload();
for (const suffix of ['', '/content']) {
await get(`/api/v1/attachments/${attachment.id}${suffix}`, null).expect(401);
await get(`/api/v1/attachments/not-a-uuid${suffix}`).expect(400);
}
});
it('variant: original явно, отсутствующий 404, неизвестный и повторённый 400', async () => {
const { id } = await upload();
await get(`/api/v1/attachments/${id}/content?variant=original`).expect(200);
await get(`/api/v1/attachments/${id}/content?variant=thumbnail`).expect(200);
await app.get(PrismaService).attachmentVariant.deleteMany({
where: { attachmentId: id, name: 'crop' },
});
await get(`/api/v1/attachments/${id}/content?variant=crop`).expect(404);
await get(`/api/v1/attachments/${id}/content?variant=nonsense`).expect(400);
await get(`/api/v1/attachments/${id}/content?variant=`).expect(400);
await get(`/api/v1/attachments/${id}/content?variant=original&variant=original`).expect(400);
await get(`/api/v1/attachments/${id}/content?variant=thumbnail`, eve).expect(404);
});
it('нет доступа к S3 — метаданные 200, content 503 с заголовками', async () => {
const attachment = await upload();
const { app: down, server: downServer } = await createTestApp({
S3_ENDPOINT: 'http://127.0.0.1:9',
});
try {
await request(downServer)
.get(`/api/v1/attachments/${attachment.id}`)
.set('Authorization', bearer(bob))
.expect(200);
const res = await request(downServer)
.get(`/api/v1/attachments/${attachment.id}/content`)
.set('Authorization', bearer(bob))
.expect(503);
expect(res.body.error.code).toBe('unavailable');
expect(res.headers['x-instance-id']).toBeTruthy();
} finally {
await down.close();
}
});
it('объект пропал из S3 — 500 без внутренних деталей', async () => {
const attachment = await upload();
const row = await app.get(PrismaService).attachment.findUniqueOrThrow({
where: { id: attachment.id },
});
await app.get(S3Service).delete(row.objectKey);
const res = await request(server)
.get(`/api/v1/attachments/${attachment.id}/content`)
.set('Authorization', bearer(bob))
.expect(500);
expect(res.body.error.code).toBe('internal_error');
expect(JSON.stringify(res.body)).not.toContain(row.objectKey);
});
});
describe('attachment_ids в сообщениях', () => {
it('text="" с вложением принимается, Message читается вторым участником', async () => {
const attachment = await upload();
const created = await send({ text: '', attachment_ids: [attachment.id] }).expect(201);
expect(created.body.attachment_ids).toEqual([attachment.id]);
const page = await request(server)
.get(`/api/v1/chats/${chatId}/messages?limit=100`)
.set('Authorization', bearer(bob))
.expect(200);
expect(page.body.items).toContainEqual(created.body);
});
it('порядок [B, A] сохраняется в ответе, истории и повторе; перестановка с тем же ключом — 409', async () => {
const a = await upload();
const b = await upload();
const key = randomUUID();
const body = { text: 'order', attachment_ids: [b.id, a.id] };
const created = await send(body, { key }).expect(201);
expect(created.body.attachment_ids).toEqual([b.id, a.id]);
const replay = await send(body, { key }).expect(201);
expect(replay.body).toEqual(created.body);
const page = await request(server)
.get(`/api/v1/chats/${chatId}/messages?limit=100`)
.set('Authorization', bearer(alice))
.expect(200);
expect(page.body.items.find((m: any) => m.id === created.body.id)).toEqual(created.body);
const conflict = await send({ text: 'order', attachment_ids: [a.id, b.id] }, { key }).expect(409);
expect(conflict.body.error.code).toBe('conflict');
});
it('отсутствующий attachment_ids и [] равны при повторе, изменение списка — 409', async () => {
const attachment = await upload();
const key = randomUUID();
const first = await send({ text: 'same' }, { key }).expect(201);
expect(first.body.attachment_ids).toEqual([]);
const replay = await send({ attachment_ids: [], text: 'same' }, { key }).expect(201);
expect(replay.body).toEqual(first.body);
await send({ text: 'same', attachment_ids: [attachment.id] }, { key }).expect(409);
});
it('вложение другого чата и неизвестное — одинаковый 404, сообщение не создаётся', async () => {
const foreign = await upload(eve, otherChatId);
const key = randomUUID();
const first = await send({ text: 'x', attachment_ids: [foreign.id] }, { key }).expect(404);
const second = await send({ text: 'x', attachment_ids: [randomUUID()] }).expect(404);
expect(first.body.error.code).toBe('not_found');
expect(first.body.error.message).toBe(second.body.error.message);
await send({ text: 'x' }, { key }).expect(201);
});
it('смесь своего и чужого вложения — 404', async () => {
const own = await upload();
const foreign = await upload(eve, otherChatId);
await send({ text: 'x', attachment_ids: [own.id, foreign.id] }).expect(404);
});
it('посторонний не может отправить вложение в чужой чат', async () => {
const attachment = await upload();
await send({ text: 'x', attachment_ids: [attachment.id] }, { user: eve }).expect(404);
});
it('любой участник может сослаться на вложение чата, вложение можно использовать повторно', async () => {
const attachment = await upload();
await send({ text: 'first', attachment_ids: [attachment.id] }).expect(201);
const res = await send({ text: 'second', attachment_ids: [attachment.id] }, { user: bob }).expect(201);
expect(res.body.sender_id).toBe(bob.id);
expect(res.body.attachment_ids).toEqual([attachment.id]);
});
it('ровно 10 вложений принимается, 11 — 400', async () => {
const attachment = await upload();
const links = await Promise.all(Array.from({ length: 9 }, () => upload()));
const ten = [attachment.id, ...links.map((l) => l.id)];
const created = await send({ text: 'ten', attachment_ids: ten }).expect(201);
expect(created.body.attachment_ids).toEqual(ten);
await send({ text: 'eleven', attachment_ids: [...ten, randomUUID()] }).expect(400);
});
it('UUID в другом регистре — тот же ID и дубль', async () => {
const attachment = await upload();
const upper = attachment.id.toUpperCase();
const created = await send({ text: 'case', attachment_ids: [upper] }).expect(201);
expect(created.body.attachment_ids).toEqual([attachment.id]);
await send({ text: 'dup', attachment_ids: [attachment.id, upper] }).expect(400);
});
it.each([
['text отсутствует', (id: string) => ({ attachment_ids: [id] })],
['text null', (id: string) => ({ text: null, attachment_ids: [id] })],
['attachment_ids null', () => ({ text: 'x', attachment_ids: null })],
['attachment_ids строка', (id: string) => ({ text: 'x', attachment_ids: id })],
['элемент не UUID', () => ({ text: 'x', attachment_ids: ['nope'] })],
['элемент число', () => ({ text: 'x', attachment_ids: [1] })],
['дубликат', (id: string) => ({ text: 'x', attachment_ids: [id, id] })],
['пустой текст без вложений', () => ({ text: '', attachment_ids: [] })],
['пробельный текст без вложений', () => ({ text: ' ' })],
])('%s — 400', async (_name, build) => {
const attachment = await upload();
await send(build(attachment.id)).expect(400);
});
});
});
function binaryParser(res: any, done: (error: Error | null, body: Buffer) => void): void {
const chunks: Buffer[] = [];
res.on('data', (chunk: Buffer) => chunks.push(chunk));
res.on('end', () => done(null, Buffer.concat(chunks)));
}
+401
View File
@@ -0,0 +1,401 @@
import { INestApplication } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import Ajv2020 from 'ajv/dist/2020';
import { createHash, randomUUID } from 'crypto';
import { mkdtempSync, readdirSync, rmSync } from 'fs';
import { connect } from 'net';
import { tmpdir } from 'os';
import { join } from 'path';
import request from 'supertest';
import { PrismaService } from '../src/db/prisma.service';
import { loadDocument } from '../src/docs/swagger';
import { S3Service } from '../src/storage/s3.service';
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
import {
corruptedJpegs,
corruptedPngs,
jpeg,
png,
pngHeaderOnly,
pngOfExactSize,
SAMPLE_PNG,
} from './images';
const MAX_BYTES = 10 * 1024 * 1024;
describe('Загрузка вложений (e2e)', () => {
let app: INestApplication;
let server: any;
let alice: TestUser;
let eve: TestUser;
let chatId: string;
const upload = (
body: Buffer | undefined,
{
type = 'image/png',
filename = 'sample.png',
user = alice,
chat = chatId,
}: {
type?: string | null;
filename?: string | null;
user?: TestUser | null;
chat?: string;
} = {},
) => {
const query = filename === null ? '' : `?filename=${encodeURIComponent(filename)}`;
const req = request(server).post(`/api/v1/chats/${chat}/attachments${query}`);
if (user) {
req.set('Authorization', bearer(user));
}
if (type) {
req.set('Content-Type', type);
}
return body ? req.send(body) : req;
};
const expectError = (res: request.Response, status: number, code: string) => {
expect({ status: res.status, code: res.body.error?.code }).toEqual({ status, code });
expect(res.headers['x-request-id']).toBeTruthy();
expect(res.headers['x-instance-id']).toBeTruthy();
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
};
const uploadTmp = mkdtempSync(join(tmpdir(), 'attachments-test-'));
const activeTempDirs = (): string[] =>
readdirSync(uploadTmp).filter((name) => name.startsWith('messenger-upload-'));
beforeAll(async () => {
({ app, server } = await createTestApp({ UPLOAD_TMP_DIR: uploadTmp }));
alice = await registerUser(server, 'att_alice');
eve = await registerUser(server, 'att_eve');
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Files', member_ids: [alice.id] })
.expect(201);
chatId = chat.body.id;
});
afterAll(async () => {
await app.close();
rmSync(uploadTmp, { recursive: true, force: true });
});
describe('успешная загрузка', () => {
it('возвращает 202 Attachment queued по контракту, staging и задание сохранены до ответа', async () => {
const res = await upload(SAMPLE_PNG).expect(202);
const document: any = loadDocument();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
ajv.addSchema(document, 'contract');
const validate = ajv.compile({ $ref: 'contract#/components/schemas/Attachment' });
expect({ valid: validate(res.body), errors: validate.errors }).toEqual({
valid: true,
errors: null,
});
expect(res.body).toMatchObject({
chat_id: chatId,
owner_id: alice.id,
filename: 'sample.png',
content_type: 'image/png',
size_bytes: SAMPLE_PNG.length,
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
status: 'queued',
variants: [],
error_code: null,
});
const prisma = app.get(PrismaService);
const row = await prisma.attachment.findUnique({
where: { id: res.body.id },
include: { jobs: { include: { outbox: true } } },
});
expect(row).toMatchObject({
status: 'queued',
objectKey: `attachments/${res.body.id}/original`,
stagingKey: `staging/${res.body.id}`,
});
expect(row!.jobs).toHaveLength(1);
expect(row!.jobs[0]).toMatchObject({ status: 'queued', attempt: 1, pipelineVersion: 1 });
expect(row!.jobs[0].outbox).toHaveLength(1);
expect(row!.jobs[0].outbox[0].payload).toEqual({
schema_version: 1,
job_id: row!.jobs[0].id,
type: 'attachment.process.v1',
attachment_id: res.body.id,
pipeline_version: 1,
attempt: 1,
created_at: expect.any(String),
});
const staged = await app.get(S3Service).getBuffer(`staging/${res.body.id}`, MAX_BYTES);
expect(Buffer.compare(staged, SAMPLE_PNG)).toBe(0);
expect(JSON.stringify(res.body)).not.toMatch(/attachments\/|staging\//);
});
it('принимает реальный JPEG', async () => {
const res = await upload(await jpeg(), {
type: 'image/jpeg',
filename: 'фото 1.jpg',
}).expect(202);
expect(res.body).toMatchObject({
content_type: 'image/jpeg',
filename: 'фото 1.jpg',
});
});
it('игнорирует параметры и регистр Content-Type', async () => {
await upload(SAMPLE_PNG, { type: 'IMAGE/PNG; charset=binary' }).expect(202);
});
it('принимает файл ровно 10 MiB, filename ровно 255 символов и PNG с байтами после IEND', async () => {
const exact = pngOfExactSize(MAX_BYTES);
expect(exact.length).toBe(MAX_BYTES);
const res = await upload(exact).expect(202);
expect(res.body.size_bytes).toBe(MAX_BYTES);
await upload(SAMPLE_PNG, { filename: `${'ф'.repeat(251)}.png` }).expect(202);
await upload(Buffer.concat([png(4, 4), Buffer.from('tail')])).expect(202);
});
it('принимает ровно 40 млн пикселей', async () => {
const res = await upload(await bigPng(8000, 5000));
expect(res.status).toBe(202);
});
});
describe('порядок проверок и доступ', () => {
it('без токена — 401 раньше любых проверок содержимого', async () => {
const cases: [string | null, string | null][] = [
['image/png', 'a.png'],
['text/plain', 'a.png'],
['application/json', 'a.png'],
['image/png', '../a.png'],
[null, null],
];
for (const [type, filename] of cases) {
const res = await upload(Buffer.from('{bad'), { type, filename, user: null });
expectError(res, 401, 'unauthorized');
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
}
});
it('посторонний и несуществующий чат — одинаковый 404, чужой файл не сохраняется', async () => {
const prisma = app.get(PrismaService);
const before = await prisma.attachment.count({ where: { chatId } });
expectError(await upload(SAMPLE_PNG, { user: eve }), 404, 'not_found');
expectError(await upload(SAMPLE_PNG, { chat: randomUUID() }), 404, 'not_found');
expect(await prisma.attachment.count({ where: { chatId } })).toBe(before);
});
it('неверный UUID чата — 400', async () => {
expectError(await upload(SAMPLE_PNG, { chat: 'not-a-uuid' }), 400, 'invalid_request');
});
});
describe('filename', () => {
const invalid = {
'отсутствует': null,
'пустой': '',
'только пробелы': ' ',
'256 символов': 'x'.repeat(256),
'путь через /': '../x.png',
'путь через \\': 'a\\b.png',
'NUL': 'a\u0000b.png',
'перевод строки': 'a\nb.png',
'DEL': 'bad\u007f.png',
'C1 U+0085': 'c1\u0085.png',
'разделитель строк U+2028': 'a
b.png',
};
it.each(Object.entries(invalid))('%s — 400', async (_name, filename) => {
expectError(await upload(SAMPLE_PNG, { filename }), 400, 'invalid_request');
});
it('повторённый параметр — 400', async () => {
const res = await request(server)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png&filename=b.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG);
expectError(res, 400, 'invalid_request');
});
});
describe('тип содержимого', () => {
it.each(['text/plain', 'application/json', 'application/octet-stream', 'multipart/form-data; boundary=x', 'image/gif', 'image/webp'])(
'Content-Type %s — 415',
async (type) => {
expectError(await upload(SAMPLE_PNG, { type }), 415, 'unsupported_media_type');
},
);
it('без Content-Type — 415', async () => {
expectError(await upload(SAMPLE_PNG, { type: null }), 415, 'unsupported_media_type');
});
it('подмена MIME в обе стороны и чужие форматы — 415', async () => {
const realJpeg = await jpeg();
expectError(await upload(SAMPLE_PNG, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
expectError(await upload(realJpeg, { type: 'image/png' }), 415, 'unsupported_media_type');
for (const body of [
Buffer.from('просто текст'),
Buffer.from('GIF89a\u0001\u0000\u0001\u0000'),
Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>'),
]) {
expectError(await upload(body), 415, 'unsupported_media_type');
}
});
it('пустое тело — 400', async () => {
expectError(await upload(undefined), 400, 'invalid_request');
expectError(await upload(Buffer.alloc(0)), 400, 'invalid_request');
});
});
describe('повреждённые изображения', () => {
it.each(Object.entries(corruptedPngs()))('PNG: %s — 415', async (_name, body) => {
expectError(await upload(body), 415, 'unsupported_media_type');
});
it('JPEG: все повреждения — 415', async () => {
for (const body of Object.values(corruptedJpegs(await jpeg()))) {
expectError(await upload(body, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
}
});
});
describe('лимиты', () => {
it('10 MiB + 1 байт — 413 и без чтения объявленного тела', async () => {
expectError(await upload(pngOfExactSize(MAX_BYTES + 1)), 413, 'payload_too_large');
});
it('превышение при chunked без Content-Length — 413', async () => {
const res = await new Promise<request.Response>((resolve, reject) => {
const req = upload(undefined);
req.set('Transfer-Encoding', 'chunked');
req.write(pngOfExactSize(MAX_BYTES));
req.write(Buffer.alloc(1024));
req.end((error, response) => (response ? resolve(response) : reject(error)));
});
expectError(res, 413, 'payload_too_large');
});
it('превышение лимита пикселей — 413 до декодирования', async () => {
for (const body of [pngHeaderOnly(8001, 5000), pngHeaderOnly(20000, 10000)]) {
expectError(await upload(body), 413, 'payload_too_large');
}
});
it('оборванная загрузка не оставляет временных файлов и строк в БД', async () => {
const prisma = app.get(PrismaService);
const before = await prisma.attachment.count({ where: { chatId } });
const { port } = server.address();
await new Promise<void>((resolve, reject) => {
const socket = connect(port, '127.0.0.1', () => {
socket.write(
`POST /api/v1/chats/${chatId}/attachments?filename=a.png HTTP/1.1\r\n` +
`Host: test\r\nAuthorization: ${bearer(alice)}\r\n` +
`Content-Type: image/png\r\nContent-Length: ${SAMPLE_PNG.length}\r\n\r\n`,
);
socket.write(SAMPLE_PNG.subarray(0, 1000));
setTimeout(() => {
socket.destroy();
resolve();
}, 300);
});
socket.on('error', reject);
});
await new Promise((resolve) => setTimeout(resolve, 500));
expect(await prisma.attachment.count({ where: { chatId } })).toBe(before);
expect(activeTempDirs()).toEqual([]);
});
});
describe('отказы', () => {
it('S3 недоступен — 503 с заголовками, метаданные не создаются', async () => {
const { app: down, server: downServer } = await createTestApp({
S3_ENDPOINT: 'http://127.0.0.1:9',
});
try {
const prisma = app.get(PrismaService);
const before = await prisma.attachment.count({ where: { chatId } });
const started = Date.now();
const res = await request(downServer)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG);
expectError(res, 503, 'unavailable');
expect(Date.now() - started).toBeLessThan(3000);
expect(await prisma.attachment.count({ where: { chatId } })).toBe(before);
} finally {
await down.close();
}
});
it('явный отказ транзакции после PUT удаляет объект staging', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError('rejected', {
code: 'P2003',
clientVersion: 'test',
}),
);
try {
const res = await upload(SAMPLE_PNG);
expect(res.status).toBe(500);
expect(remove).toHaveBeenCalledTimes(1);
expect(remove.mock.calls[0][0]).toMatch(/^staging\//);
} finally {
create.mockRestore();
remove.mockRestore();
}
});
it('необработанная ошибка PostgreSQL при INSERT — 503, объект не удаляется', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').mockRejectedValueOnce(
new Prisma.PrismaClientUnknownRequestError('server closed the connection', {
clientVersion: 'test',
}),
);
try {
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
expect(remove).not.toHaveBeenCalled();
} finally {
create.mockRestore();
remove.mockRestore();
}
});
it('при неясном исходе COMMIT объект не удаляется', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError('connection lost', {
code: 'P1017',
clientVersion: 'test',
}),
);
try {
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
expect(remove).not.toHaveBeenCalled();
} finally {
create.mockRestore();
remove.mockRestore();
}
});
});
});
async function bigPng(width: number, height: number): Promise<Buffer> {
const { default: sharp } = await import('sharp');
return sharp({ create: { width, height, channels: 3, background: '#336699' } })
.png({ compressionLevel: 9 })
.toBuffer();
}
+179
View File
@@ -0,0 +1,179 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import { PrismaService } from '../src/db/prisma.service';
import {
basic,
bearer,
createTestApp,
login,
PASSWORD,
registerUser,
} from './helpers';
describe('Аутентификация и сессии (e2e)', () => {
let app: INestApplication;
let server: any;
beforeAll(async () => {
({ app, server } = await createTestApp());
});
afterAll(async () => {
await app.close();
});
describe('регистрация', () => {
it('требует password', async () => {
await request(server)
.post('/api/v1/users')
.send({ username: `nopw_${randomUUID().slice(0, 8)}`, display_name: 'X' })
.expect(400);
});
it('отклоняет password короче 12 code points', async () => {
await request(server)
.post('/api/v1/users')
.send({
username: `short_${randomUUID().slice(0, 8)}`,
display_name: 'X',
password: 'x'.repeat(11),
})
.expect(400);
});
it('хранит пароль как Argon2id-хеш, а не открытым текстом', async () => {
const user = await registerUser(server, 'hash');
const row = await app
.get(PrismaService)
.user.findUniqueOrThrow({ where: { id: user.id } });
expect(row.passwordHash.startsWith('$argon2id$')).toBe(true);
expect(row.passwordHash).not.toContain(PASSWORD);
});
});
describe('POST /api/v1/auth/sessions', () => {
it('выдаёт непрозрачный Bearer-токен с expires_at и пользователем', async () => {
const user = await registerUser(server, 'login');
const res = await request(server)
.post('/api/v1/auth/sessions')
.set('Authorization', basic(user.username, PASSWORD))
.expect(201);
expect(res.body.token_type).toBe('Bearer');
expect(res.body.token.length).toBeGreaterThanOrEqual(43);
expect(res.body.token).not.toContain(user.id);
expect(res.body.user.id).toBe(user.id);
const expiresIn = Date.parse(res.body.expires_at) - Date.now();
expect(expiresIn).toBeGreaterThan(3500 * 1000);
expect(expiresIn).toBeLessThanOrEqual(3600 * 1000);
});
it('принимает пароль с двоеточием и не-ASCII символами', async () => {
const username = `colon_${randomUUID().slice(0, 8)}`;
const password = 'пароль:с:двоеточием🔑';
await request(server)
.post('/api/v1/users')
.send({ username, display_name: 'X', password })
.expect(201);
await login(server, username, password);
});
it('отвечает одинаковым 401 на неверный пароль и неизвестного пользователя', async () => {
const user = await registerUser(server, 'wrong');
const wrong = await request(server)
.post('/api/v1/auth/sessions')
.set('Authorization', basic(user.username, 'wrong-password-123'))
.expect(401);
const unknown = await request(server)
.post('/api/v1/auth/sessions')
.set('Authorization', basic(`ghost_${randomUUID().slice(0, 8)}`, PASSWORD))
.expect(401);
for (const res of [wrong, unknown]) {
expect(res.headers['www-authenticate']).toBe(
'Basic realm="messenger", charset="UTF-8"',
);
expect(res.body.error.code).toBe('unauthorized');
}
expect(wrong.body.error.message).toBe(unknown.body.error.message);
});
it('отклоняет JSON body при входе даже с верным Basic', async () => {
const user = await registerUser(server, 'body');
for (const body of [{ extra: 'forbidden' }, {}]) {
const res = await request(server)
.post('/api/v1/auth/sessions')
.set('Authorization', basic(user.username, PASSWORD))
.send(body)
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
}
});
it('отвечает 401 без заголовка Authorization', async () => {
const res = await request(server)
.post('/api/v1/auth/sessions')
.expect(401);
expect(res.headers['www-authenticate']).toContain('Basic');
});
});
describe('защищённые endpoint', () => {
it('не принимают Basic', async () => {
const user = await registerUser(server, 'basic');
const res = await request(server)
.get('/api/v1/users/me')
.set('Authorization', basic(user.username, PASSWORD))
.expect(401);
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
});
it('не принимают X-User-Id сам по себе', async () => {
const user = await registerUser(server, 'xuid');
await request(server)
.get('/api/v1/users/me')
.set('X-User-Id', user.id)
.expect(401);
});
it('игнорируют X-User-Id рядом с Bearer', async () => {
const alice = await registerUser(server, 'alice');
const eve = await registerUser(server, 'eve');
const res = await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(alice))
.set('X-User-Id', eve.id)
.expect(200);
expect(res.body.id).toBe(alice.id);
});
it('отвечают 401 на неизвестный токен', async () => {
await request(server)
.get('/api/v1/users/me')
.set('Authorization', 'Bearer unknown-token-value')
.expect(401);
});
});
describe('DELETE /api/v1/auth/sessions/current', () => {
it('отзывает только текущий токен: 204, затем 401 на повтор', async () => {
const user = await registerUser(server, 'logout');
const other = await login(server, user.username);
await request(server)
.delete('/api/v1/auth/sessions/current')
.set('Authorization', bearer(user))
.expect(204);
await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(401);
await request(server)
.delete('/api/v1/auth/sessions/current')
.set('Authorization', bearer(user))
.expect(401);
await request(server)
.get('/api/v1/users/me')
.set('Authorization', `Bearer ${other}`)
.expect(200);
});
});
});
+119
View File
@@ -0,0 +1,119 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import {
bearer,
createTestApp,
login,
PASSWORD,
registerUser,
} from './helpers';
describe('Безопасность при конкурентном доступе (e2e)', () => {
let app: INestApplication;
let server: any;
beforeAll(async () => {
({ app, server } = await createTestApp());
});
afterAll(async () => {
await app.close();
});
it('принимает 100 конкурентных сообщений от 10 клиентов и сохраняет ровно 100 уникальных', async () => {
const CLIENTS = 10;
const MESSAGES_PER_CLIENT = 10;
const owner = await registerUser(server, 'owner');
const clients = await Promise.all(
Array.from({ length: CLIENTS }, (_, i) =>
registerUser(server, `racer${i}`),
),
);
const memberIds = [owner.id, ...clients.map((c) => c.id)];
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(owner))
.send({ title: 'Race', member_ids: memberIds })
.expect(201);
const sendTasks: Promise<request.Response>[] = [];
for (let c = 0; c < CLIENTS; c++) {
for (let m = 0; m < MESSAGES_PER_CLIENT; m++) {
sendTasks.push(
request(server)
.post(`/api/v1/chats/${chat.body.id}/messages`)
.set('Idempotency-Key', randomUUID())
.set('Authorization', bearer(clients[c]))
.send({ text: `client-${c}-message-${m}` }),
);
}
}
const responses = await Promise.all(sendTasks);
for (const res of responses) {
expect(res.status).toBe(201);
}
const sentIds = new Set(responses.map((r) => r.body.id));
expect(sentIds.size).toBe(CLIENTS * MESSAGES_PER_CLIENT);
const collected: { id: string; text: string }[] = [];
let cursor: string | null = null;
do {
const res: request.Response = await request(server)
.get(`/api/v1/chats/${chat.body.id}/messages`)
.query({ limit: 25, ...(cursor ? { cursor } : {}) })
.set('Authorization', bearer(owner))
.expect(200);
collected.push(...res.body.items);
cursor = res.body.next_cursor;
} while (cursor);
expect(collected.length).toBe(CLIENTS * MESSAGES_PER_CLIENT);
const uniqueIds = new Set(collected.map((m) => m.id));
expect(uniqueIds.size).toBe(CLIENTS * MESSAGES_PER_CLIENT);
const uniqueTexts = new Set(collected.map((m) => m.text));
expect(uniqueTexts.size).toBe(CLIENTS * MESSAGES_PER_CLIENT);
const fullHistory: { id: string; created_at: string }[] =
await request(server)
.get(`/api/v1/chats/${chat.body.id}/messages`)
.query({ limit: 100 })
.set('Authorization', bearer(owner))
.expect(200)
.then((res) => res.body.items);
const sortKeys = fullHistory.map((m) => `${m.created_at}#${m.id}`);
const sortedKeys = [...sortKeys].sort();
expect(sortKeys).toEqual(sortedKeys);
});
it('регистрирует ровно одного пользователя при гонке 10 запросов за один и тот же username', async () => {
const username = `racer_username_${randomUUID().slice(0, 8)}`;
const attempts = 10;
const responses = await Promise.all(
Array.from({ length: attempts }, () =>
request(server)
.post('/api/v1/users')
.send({ username, display_name: 'Racer', password: PASSWORD }),
),
);
const succeeded = responses.filter((r) => r.status === 201);
const conflicted = responses.filter((r) => r.status === 409);
expect(succeeded.length).toBe(1);
expect(conflicted.length).toBe(attempts - 1);
const token = await login(server, username);
const me = await request(server)
.get('/api/v1/users/me')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(me.body.id).toBe(succeeded[0].body.id);
});
});
+241
View File
@@ -0,0 +1,241 @@
import { INestApplication } from '@nestjs/common';
import Ajv2020 from 'ajv/dist/2020';
import { randomUUID } from 'crypto';
import request from 'supertest';
import { PrismaService } from '../src/db/prisma.service';
import { loadDocument } from '../src/docs/swagger';
import {
basic,
bearer,
createTestApp,
PASSWORD,
processAttachment,
registerUser,
TestUser,
} from './helpers';
import { jpeg, SAMPLE_PNG } from './images';
const CONTRACT_ID = 'https://contract.local/openapi.json';
describe('Все ответы соответствуют контракту: коды, схемы, заголовки (e2e)', () => {
let app: INestApplication;
let server: any;
let alice: TestUser;
let bob: TestUser;
let eve: TestUser;
const document: any = loadDocument();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
ajv.addSchema(document, CONTRACT_ID);
const covered = new Set<string>();
const call = async (
method: 'get' | 'post' | 'delete',
template: string,
values: Record<string, string>,
expected: number,
{
user,
auth,
query = '',
body,
raw,
type,
headers = {},
}: {
user?: TestUser | null;
auth?: string;
query?: string;
body?: unknown;
raw?: Buffer | string;
type?: string;
headers?: Record<string, string>;
} = {},
) => {
const path = template.replace(/\{(\w+)\}/g, (_, name) => values[name]);
let req = request(server)[method](path + query).buffer(true).parse(binaryParser);
if (user) {
req = req.set('Authorization', bearer(user));
}
if (auth) {
req = req.set('Authorization', auth);
}
for (const [name, value] of Object.entries(headers)) {
req = req.set(name, value);
}
if (type) {
req = req.set('Content-Type', type);
}
const res = raw !== undefined ? await req.send(raw) : body !== undefined ? await req.send(body as object) : await req;
const operation = document.paths[template][method];
const label = `${method.toUpperCase()} ${template} -> ${res.status}`;
expect({ label, status: res.status }).toEqual({ label, status: expected });
expect({ label, declared: String(res.status) in operation.responses }).toEqual({ label, declared: true });
expect({ label, requestId: Boolean(res.headers['x-request-id']) }).toEqual({ label, requestId: true });
expect({ label, instanceId: Boolean(res.headers['x-instance-id']) }).toEqual({ label, instanceId: true });
const declared = operation.responses[String(res.status)];
const content = declared.content ?? {};
const type_ = (res.headers['content-type'] ?? '').split(';')[0];
if (res.status === 204) {
expect({ label, length: res.body.length }).toEqual({ label, length: 0 });
} else if (content['application/json']) {
expect({ label, type: type_ }).toEqual({ label, type: 'application/json' });
const json = JSON.parse(res.body.toString('utf8'));
const schema = content['application/json'].schema;
const validate = ajv.compile(schema.$ref ? { $ref: CONTRACT_ID + schema.$ref } : schema);
const valid = validate(json);
expect({ label, valid, errors: validate.errors }).toEqual({ label, valid: true, errors: null });
if (json && typeof json === 'object' && 'error' in json) {
expect({ label, same: json.error.request_id === res.headers['x-request-id'] }).toEqual({ label, same: true });
}
} else {
expect({ label, type: Object.keys(content).includes(type_) }).toEqual({ label, type: true });
}
covered.add(label);
return res.status === 204 || !content['application/json'] ? res.body : JSON.parse(res.body.toString('utf8'));
};
beforeAll(async () => {
({ app, server } = await createTestApp());
alice = await registerUser(server, 'ct_alice');
bob = await registerUser(server, 'ct_bob');
eve = await registerUser(server, 'ct_eve');
});
afterAll(async () => {
await app.close();
});
it('health, пользователи и сессии', async () => {
await call('get', '/health/live', {}, 200);
await call('get', '/health/ready', {}, 200);
const username = `ct_${randomUUID().slice(0, 8)}`;
await call('post', '/api/v1/users', {}, 201, { body: { username, display_name: 'C', password: PASSWORD } });
await call('post', '/api/v1/users', {}, 409, { body: { username, display_name: 'C', password: PASSWORD } });
await call('post', '/api/v1/users', {}, 400, { body: { username: 'BAD NAME', display_name: 'C', password: PASSWORD } });
await call('post', '/api/v1/users', {}, 400, { raw: '{bad', type: 'application/json' });
await call('post', '/api/v1/auth/sessions', {}, 201, { auth: basic(username, PASSWORD) });
await call('post', '/api/v1/auth/sessions', {}, 401, { auth: basic(username, 'wrong-password-123') });
await call('post', '/api/v1/auth/sessions', {}, 400, {
auth: basic(username, PASSWORD),
body: { username },
});
await call('get', '/api/v1/users/me', {}, 200, { user: alice });
await call('get', '/api/v1/users/me', {}, 401);
await call('get', '/api/v1/users/me', {}, 401, { auth: basic(username, PASSWORD) });
const temp = await registerUser(server, 'ct_tmp');
await call('delete', '/api/v1/auth/sessions/current', {}, 204, { user: temp });
await call('delete', '/api/v1/auth/sessions/current', {}, 401, { user: temp });
});
it('чаты и сообщения', async () => {
const chat = await call('post', '/api/v1/chats', {}, 201, {
user: alice,
body: { title: 'Contract', member_ids: [alice.id, bob.id] },
});
await call('post', '/api/v1/chats', {}, 400, { user: alice, body: { title: 'x', member_ids: [bob.id] } });
await call('post', '/api/v1/chats', {}, 401, { body: { title: 'x', member_ids: [] } });
await call('get', '/api/v1/chats', {}, 200, { user: bob, query: '?limit=1' });
await call('get', '/api/v1/chats', {}, 400, { user: bob, query: '?limit=0' });
await call('get', '/api/v1/chats', {}, 400, { user: bob, query: '?cursor=garbage' });
const ids = { chat_id: chat.id };
await call('get', '/api/v1/chats/{chat_id}', ids, 200, { user: bob });
await call('get', '/api/v1/chats/{chat_id}', ids, 404, { user: eve });
await call('get', '/api/v1/chats/{chat_id}', { chat_id: 'nope' }, 400, { user: bob });
const key = randomUUID();
const send = (status: number, body: unknown, user: TestUser = alice, idem: string | null = randomUUID()) =>
call('post', '/api/v1/chats/{chat_id}/messages', ids, status, {
user,
body,
headers: idem ? { 'Idempotency-Key': idem } : {},
});
await send(201, { text: 'привет' }, alice, key);
await send(201, { text: 'привет' }, alice, key);
await send(201, { text: 'второе' }, bob);
await send(409, { text: 'другое' }, alice, key);
await send(400, { text: 'без ключа' }, alice, null);
await send(400, { text: '' });
await send(400, { text: 'x', sender_id: alice.id });
await send(404, { text: 'x' }, eve);
await send(404, { text: 'x', attachment_ids: [randomUUID()] });
const page = await call('get', '/api/v1/chats/{chat_id}/messages', ids, 200, { user: bob, query: '?limit=1' });
await call('get', '/api/v1/chats/{chat_id}/messages', ids, 200, {
user: bob,
query: `?limit=1&cursor=${encodeURIComponent(page.next_cursor)}`,
});
await call('get', '/api/v1/chats/{chat_id}/messages', ids, 400, { user: bob, query: '?limit=101' });
await call('get', '/api/v1/chats/{chat_id}/messages', ids, 404, { user: eve });
});
it('вложения во всех состояниях', async () => {
const chat = await call('post', '/api/v1/chats', {}, 201, {
user: alice,
body: { title: 'Files', member_ids: [alice.id, bob.id] },
});
const ids = { chat_id: chat.id };
const upload = (status: number, body: Buffer, type = 'image/png', query = '?filename=a.png', user: TestUser | null = alice) =>
call('post', '/api/v1/chats/{chat_id}/attachments', ids, status, { user, raw: body, type, query });
const queued = await upload(202, SAMPLE_PNG);
expect(queued.status).toBe('queued');
await upload(202, await jpeg(), 'image/jpeg');
await upload(400, SAMPLE_PNG, 'image/png', '?filename=../a.png');
await upload(415, SAMPLE_PNG, 'image/jpeg');
await upload(415, Buffer.from('text'), 'text/plain');
await upload(413, Buffer.alloc(10 * 1024 * 1024 + 1), 'image/png');
await upload(404, SAMPLE_PNG, 'image/png', '?filename=a.png', eve);
await upload(401, SAMPLE_PNG, 'image/png', '?filename=a.png', null);
const one = { attachment_id: queued.id };
const meta = '/api/v1/attachments/{attachment_id}';
const content = '/api/v1/attachments/{attachment_id}/content';
expect((await call('get', meta, one, 200, { user: bob })).status).toBe('queued');
await call('get', content, one, 409, { user: bob });
await call('get', content, one, 409, { user: bob, query: '?variant=thumbnail' });
await call('post', '/api/v1/chats/{chat_id}/messages', ids, 201, {
user: alice,
body: { text: '', attachment_ids: [queued.id] },
headers: { 'Idempotency-Key': randomUUID() },
});
expect(await processAttachment(app, queued.id)).toBe('ready');
const ready = await call('get', meta, one, 200, { user: bob });
expect(ready.variants.map((v: any) => v.name)).toEqual(['thumbnail', 'crop', 'optimized']);
for (const variant of ['', '?variant=original', '?variant=thumbnail', '?variant=crop', '?variant=optimized']) {
await call('get', content, one, 200, { user: bob, query: variant });
}
await call('get', content, one, 400, { user: bob, query: '?variant=huge' });
await call('get', content, one, 404, { user: eve });
await call('get', meta, one, 404, { user: eve });
await call('get', meta, { attachment_id: 'nope' }, 400, { user: bob });
await call('get', meta, one, 401);
const broken = await upload(202, SAMPLE_PNG);
const prisma = app.get(PrismaService);
await prisma.attachmentJob.updateMany({ where: { attachmentId: broken.id }, data: { maxAttempts: 1 } });
await prisma.attachment.update({ where: { id: broken.id }, data: { sha256: 'f'.repeat(64) } });
expect(await processAttachment(app, broken.id)).toBe('failed');
const failed = await call('get', meta, { attachment_id: broken.id }, 200, { user: bob });
expect(failed).toMatchObject({ status: 'failed', error_code: 'checksum_mismatch', variants: [] });
await call('get', content, { attachment_id: broken.id }, 409, { user: bob });
await call('post', '/api/v1/chats/{chat_id}/messages', ids, 409, {
user: alice,
body: { text: '', attachment_ids: [broken.id] },
headers: { 'Idempotency-Key': randomUUID() },
});
});
it('каждая операция контракта вызвана хотя бы раз', () => {
const operations = Object.entries(document.paths).flatMap(([path, ops]: [string, any]) =>
Object.keys(ops)
.filter((method) => ['get', 'post', 'delete'].includes(method))
.map((method) => `${method.toUpperCase()} ${path}`),
);
const called = new Set([...covered].map((label) => label.split(' -> ')[0]));
expect(operations.filter((operation) => !called.has(operation))).toEqual([]);
});
});
function binaryParser(res: any, done: (error: Error | null, body: Buffer) => void): void {
const chunks: Buffer[] = [];
res.on('data', (chunk: Buffer) => chunks.push(chunk));
res.on('end', () => done(null, Buffer.concat(chunks)));
}
+69
View File
@@ -0,0 +1,69 @@
import { INestApplication } from '@nestjs/common';
import Ajv2020 from 'ajv/dist/2020';
import request from 'supertest';
import { OPERATION_EXAMPLES } from '../src/docs/examples';
import { loadDocument, setupSwagger } from '../src/docs/swagger';
import { createTestApp } from './helpers';
const CONTRACT_ID = 'https://contract.local/openapi.json';
describe('Swagger UI и примеры (e2e)', () => {
let app: INestApplication;
let server: any;
beforeAll(async () => {
({ app, server } = await createTestApp({}, setupSwagger));
});
afterAll(async () => {
await app.close();
});
it('отдаёт Swagger UI и документ контракта со всеми путями', async () => {
await request(server).get('/docs').expect(200);
const res = await request(server).get('/docs-json').expect(200);
expect(res.body.openapi).toBe('3.1.0');
expect(Object.keys(res.body.paths)).toEqual(
Object.keys(loadDocument().paths),
);
expect(res.body.components.securitySchemes).toHaveProperty('basicAuth');
expect(res.body.components.securitySchemes).toHaveProperty('bearerAuth');
});
it('каждый пример запроса и ответа проходит JSON Schema контракта', () => {
const document: any = loadDocument();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
ajv.addSchema(document, CONTRACT_ID);
let checked = 0;
for (const [key, examples] of Object.entries(OPERATION_EXAMPLES)) {
const [method, path] = key.split(' ');
const operation = document.paths[path][method.toLowerCase()];
const targets: [string, any, Record<string, any>][] = [];
if (examples.request) {
targets.push([
'request',
operation.requestBody.content['application/json'].schema,
examples.request,
]);
}
for (const [status, named] of Object.entries(examples.responses)) {
targets.push([
status,
operation.responses[status].content['application/json'].schema,
named,
]);
}
for (const [where, schema, named] of targets) {
const validate = ajv.compile({ $ref: CONTRACT_ID + schema.$ref });
for (const [name, example] of Object.entries(named)) {
const valid = validate(example.value);
expect({ key, where, name, valid, errors: validate.errors }).toEqual(
{ key, where, name, valid: true, errors: null },
);
checked++;
}
}
}
expect(checked).toBeGreaterThan(40);
});
});
+166
View File
@@ -0,0 +1,166 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import Redis from 'ioredis';
import { CacheService } from '../src/cache/cache.service';
import { ChatsService } from '../src/chats/chats.service';
import { PrismaService } from '../src/db/prisma.service';
import { bearer, createTestApp, registerUser } from './helpers';
const UNREACHABLE_REDIS = 'redis://127.0.0.1:1/0';
const UNREACHABLE_DB = 'postgresql://test:test@127.0.0.1:1/none?connect_timeout=1';
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
describe('Уровень 4: expiry, отказы зависимостей, rollback (e2e)', () => {
const apps: INestApplication[] = [];
async function start(env: Record<string, string> = {}) {
const started = await createTestApp(env);
apps.push(started.app);
return started;
}
afterAll(async () => {
await Promise.all(apps.map((app) => app.close()));
});
it('сессия истекает при коротком SESSION_TTL_SECONDS, в том числе из кэша', async () => {
const { server } = await start({ SESSION_TTL_SECONDS: '2' });
const user = await registerUser(server, 'ttl');
await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(200);
await sleep(2500);
const res = await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(401);
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
});
it('без Redis вход и проверка сессии работают через PostgreSQL, logout — 503 без отзыва', async () => {
const { server } = await start({ CACHE_URL: UNREACHABLE_REDIS });
const user = await registerUser(server, 'noredis');
await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(200);
const res = await request(server)
.delete('/api/v1/auth/sessions/current')
.set('Authorization', bearer(user))
.expect(503);
expect(res.body.error.code).toBe('unavailable');
await request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(200);
await request(server).get('/api/v1/users/me').expect(401);
});
it('logout через реплику без связи с Redis не создаёт расхождения с репликой, у которой сессия в кэше', async () => {
const a = await start();
const b = await start();
const user = await registerUser(a.server, 'partition');
const me = (server: any, status: number) =>
request(server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(status);
const logout = (status: number) =>
request(a.server)
.delete('/api/v1/auth/sessions/current')
.set('Authorization', bearer(user))
.expect(status);
const cache = a.app.get(CacheService) as any;
const redis = cache.client as Redis;
await me(b.server, 200);
redis.disconnect();
await logout(503);
await me(b.server, 200);
await me(a.server, 200);
await redis.connect();
await sleep(200);
expect(cache.trusted).toBe(true);
await me(b.server, 200);
expect((await redis.keys('session:v1:*')).length).toBeGreaterThan(0);
await logout(204);
await me(b.server, 401);
await me(a.server, 401);
});
it('без PostgreSQL отвечает ограниченным 503 и не пускает анонима', async () => {
const healthy = await start();
const user = await registerUser(healthy.server, 'nodb');
await request(healthy.server)
.get('/api/v1/users/me')
.set('Authorization', bearer(user))
.expect(200);
const { server } = await start({ DATABASE_URL: UNREACHABLE_DB });
const ready = await request(server).get('/health/ready').expect(503);
expect(ready.body.error.code).toBe('unavailable');
expect(JSON.stringify(ready.body)).not.toMatch(/127\.0\.0\.1|postgresql:|prisma/i);
await request(server).get('/api/v1/chats').expect(401);
await request(server)
.get('/api/v1/chats')
.set('X-User-Id', user.id)
.expect(401);
await request(server)
.get('/api/v1/chats')
.set('Authorization', 'Bearer unknown-token-value')
.expect(503);
const res = await request(server)
.get('/api/v1/chats')
.set('Authorization', bearer(user))
.expect(503);
expect(res.body.error.code).toBe('unavailable');
await request(server)
.post('/api/v1/users')
.send({
username: `nodb_${randomUUID().slice(0, 8)}`,
display_name: 'X',
password: 'Long-enough-pass-1',
})
.expect(503);
});
it('неожиданное исключение — 500 internal_error без деталей, с X-Request-Id и X-Instance-Id', async () => {
const { app, server } = await start();
const user = await registerUser(server, 'boom');
jest
.spyOn(app.get(ChatsService), 'listForMember')
.mockRejectedValueOnce(new Error('secret internal details'));
const res = await request(server)
.get('/api/v1/chats')
.set('Authorization', bearer(user))
.expect(500);
expect(res.body.error.code).toBe('internal_error');
expect(JSON.stringify(res.body)).not.toMatch(/secret/);
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
expect(res.headers['x-instance-id']).toBeTruthy();
});
it('откатывает создание чата целиком, если один из участников не существует', async () => {
const { app, server } = await start();
const alice = await registerUser(server, 'rollback');
const title = `rollback ${randomUUID()}`;
const res = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title, member_ids: [alice.id, randomUUID()] })
.expect(400);
expect(res.body.error.code).toBe('invalid_request');
const prisma = app.get(PrismaService);
expect(await prisma.chat.count({ where: { title } })).toBe(0);
expect(
await prisma.chatMember.count({ where: { userId: alice.id } }),
).toBe(0);
});
});
+28
View File
@@ -0,0 +1,28 @@
import {
BucketAlreadyOwnedByYou,
CreateBucketCommand,
S3Client,
} from '@aws-sdk/client-s3';
import { execSync } from 'child_process';
export default async function globalSetup(): Promise<void> {
execSync('npx prisma migrate deploy', { stdio: 'inherit' });
const client = new S3Client({
endpoint: process.env.S3_ENDPOINT,
region: 'us-east-1',
forcePathStyle: true,
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID!,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
},
});
try {
await client.send(new CreateBucketCommand({ Bucket: process.env.S3_BUCKET! }));
} catch (error) {
if (!(error instanceof BucketAlreadyOwnedByYou)) {
throw error;
}
} finally {
client.destroy();
}
}
+110
View File
@@ -0,0 +1,110 @@
import { INestApplication } from '@nestjs/common';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import { randomUUID } from 'crypto';
import request from 'supertest';
import { AppModule } from '../src/app.module';
import { configureApp } from '../src/configure-app';
import { PrismaService } from '../src/db/prisma.service';
import { parseJobMessage } from '../src/jobs/job-message';
import {
JobProcessor,
Outcome,
ProcessorHooks,
ProcessorOptions,
processorOptions,
} from '../src/jobs/processor';
import { S3Service } from '../src/storage/s3.service';
export const PASSWORD = 'Correct-Horse-Battery-42';
export interface TestUser {
id: string;
username: string;
token: string;
}
export async function createTestApp(
env: Record<string, string> = {},
setup: (app: NestFastifyApplication) => void = () => undefined,
): Promise<{
app: INestApplication;
server: any;
}> {
const saved = { ...process.env };
Object.assign(process.env, env);
let app: NestFastifyApplication;
try {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleRef.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);
} finally {
process.env = saved;
}
configureApp(app);
setup(app);
await app.listen(0, '127.0.0.1');
return { app, server: app.getHttpServer() };
}
export async function processAttachment(
app: INestApplication,
attachmentId: string,
hooks: ProcessorHooks = {},
options: Partial<ProcessorOptions> = {},
): Promise<Outcome> {
const prisma = app.get(PrismaService);
const entry = await prisma.outboxEntry.findFirstOrThrow({
where: { job: { attachmentId } },
orderBy: { id: 'desc' },
});
const message = parseJobMessage(Buffer.from(JSON.stringify(entry.payload)));
if (!message) {
throw new Error('outbox payload does not match jobs.schema.json');
}
const processor = new JobProcessor(
prisma,
app.get(S3Service),
{ ...processorOptions(), ...options },
hooks,
);
return processor.handle(message);
}
export function basic(username: string, password: string): string {
return `Basic ${Buffer.from(`${username}:${password}`, 'utf8').toString('base64')}`;
}
export function bearer(user: TestUser): string {
return `Bearer ${user.token}`;
}
export async function login(
server: any,
username: string,
password = PASSWORD,
): Promise<string> {
const res = await request(server)
.post('/api/v1/auth/sessions')
.set('Authorization', basic(username, password))
.expect(201);
return res.body.token;
}
export async function registerUser(
server: any,
label: string,
): Promise<TestUser> {
const username = `u_${label}_${randomUUID().slice(0, 8)}`;
const res = await request(server)
.post('/api/v1/users')
.send({ username, display_name: `User ${label}`, password: PASSWORD })
.expect(201);
return { id: res.body.id, username, token: await login(server, username) };
}
+139
View File
@@ -0,0 +1,139 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { randomUUID } from 'crypto';
import { PrismaService } from '../src/db/prisma.service';
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
describe('Идемпотентность отправки и X-Instance-Id (e2e)', () => {
const apps: INestApplication[] = [];
let server: any;
let replica: any;
let prisma: PrismaService;
beforeAll(async () => {
const first = await createTestApp();
const second = await createTestApp();
apps.push(first.app, second.app);
server = first.server;
replica = second.server;
prisma = first.app.get(PrismaService);
});
afterAll(async () => {
await Promise.all(apps.map((app) => app.close()));
});
async function setup(label: string) {
const alice = await registerUser(server, label);
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Idem', member_ids: [alice.id] })
.expect(201);
return { alice, path: `/api/v1/chats/${chat.body.id}/messages` };
}
function send(target: any, user: TestUser, path: string, key: string | null, body: object) {
const req = request(target).post(path).set('Authorization', bearer(user));
return (key === null ? req : req.set('Idempotency-Key', key)).send(body);
}
async function countMessages(path: string, user: TestUser): Promise<number> {
const res = await request(server)
.get(`${path}?limit=100`)
.set('Authorization', bearer(user))
.expect(200);
return res.body.items.length;
}
it('требует Idempotency-Key правильного формата', async () => {
const { alice, path } = await setup('keyfmt');
for (const key of [null, '', 'has space', 'semi;colon', 'slash/key', 'x'.repeat(129)]) {
const res = await send(server, alice, path, key, { text: 'x' });
expect(res.status).toBe(400);
expect(res.body.error.code).toBe('invalid_request');
}
await send(server, alice, path, 'A-z_0.9:' + 'k'.repeat(120), { text: 'x' }).expect(201);
});
it('повтор с тем же ключом и телом возвращает тот же 201 и то же сообщение', async () => {
const { alice, path } = await setup('replay');
const key = randomUUID();
const first = await send(server, alice, path, key, { text: 'Привет 👋' }).expect(201);
const again = await send(replica, alice, path, key, { text: 'Привет 👋' }).expect(201);
expect(again.body).toEqual(first.body);
expect(await countMessages(path, alice)).toBe(1);
});
it('тот же ключ с другим телом — 409, сообщение не создаётся', async () => {
const { alice, path } = await setup('conflict');
const key = randomUUID();
await send(server, alice, path, key, { text: 'первое' }).expect(201);
const res = await send(server, alice, path, key, { text: 'другое' }).expect(409);
expect(res.body.error.code).toBe('conflict');
expect(await countMessages(path, alice)).toBe(1);
});
it('20 параллельных запросов с одним ключом через два экземпляра создают ровно одно сообщение', async () => {
const { alice, path } = await setup('parallel');
const key = randomUUID();
const responses = await Promise.all(
Array.from({ length: 20 }, (_, i) =>
send(i % 2 ? replica : server, alice, path, key, { text: 'одно' }),
),
);
expect(responses.map((r) => r.status)).toEqual(Array(20).fill(201));
expect(new Set(responses.map((r) => r.body.id)).size).toBe(1);
expect(await countMessages(path, alice)).toBe(1);
});
it('область ключа — пользователь и чат: одинаковый ключ у разных пользователей и чатов независим', async () => {
const a = await setup('scopea');
const b = await setup('scopeb');
const key = randomUUID();
const one = await send(server, a.alice, a.path, key, { text: 'x' }).expect(201);
const two = await send(server, b.alice, b.path, key, { text: 'x' }).expect(201);
expect(one.body.id).not.toBe(two.body.id);
});
it('повтор заново проверяет сессию', async () => {
const { alice, path } = await setup('recheck');
const key = randomUUID();
await send(server, alice, path, key, { text: 'x' }).expect(201);
await request(server)
.delete('/api/v1/auth/sessions/current')
.set('Authorization', bearer(alice))
.expect(204);
await send(server, alice, path, key, { text: 'x' }).expect(401);
});
it('после IDEMPOTENCY_TTL_SECONDS ключ можно использовать заново', async () => {
const { app, server: short } = await createTestApp({ IDEMPOTENCY_TTL_SECONDS: '1' });
apps.push(app);
const { alice, path } = await setup('ttl');
const key = randomUUID();
const first = await send(short, alice, path, key, { text: 'x' }).expect(201);
await sleep(1500);
const second = await send(short, alice, path, key, { text: 'y' }).expect(201);
expect(second.body.id).not.toBe(first.body.id);
expect(await prisma.idempotencyKey.count({ where: { key } })).toBe(1);
});
it('X-Instance-Id есть в успешных ответах, ошибках и health и стабилен в процессе', async () => {
const ids = new Set<string>();
for (const res of [
await request(server).get('/health/live').expect(200),
await request(server).get('/health/ready').expect(200),
await request(server).get('/api/v1/users/me').expect(401),
await request(server).post('/api/v1/users').send('{bad').set('Content-Type', 'application/json').expect(400),
]) {
expect(res.headers['x-instance-id']).toBeTruthy();
ids.add(res.headers['x-instance-id']);
}
expect(ids.size).toBe(1);
});
});

Some files were not shown because too many files have changed in this diff Show More